Skip to content

Commit

Permalink
Add setting for exact alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
Futsch1 committed Feb 18, 2024
1 parent e72605a commit dc14470
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
Expand Down
Binary file modified app/src/main/feature-graphic-playstore.xcf
Binary file not shown.
44 changes: 30 additions & 14 deletions app/src/main/java/com/futsch1/medtimer/PreferencesFragment.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.futsch1.medtimer;

import static android.Manifest.permission.POST_NOTIFICATIONS;
import static android.Manifest.permission.SCHEDULE_EXACT_ALARM;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Intent;
Expand All @@ -11,16 +11,16 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.Settings;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.SwitchPreference;

import com.futsch1.medtimer.database.MedicineRepository;
import com.futsch1.medtimer.helpers.PathHelper;
Expand All @@ -31,15 +31,6 @@
import java.net.URLConnection;

public class PreferencesFragment extends PreferenceFragmentCompat {

private final ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
result -> {
if (!result) {
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit().putBoolean("exact_reminders", false).apply();
}
}
);
private MedicineViewModel medicineViewModel;
private HandlerThread backgroundThread;

Expand Down Expand Up @@ -90,8 +81,21 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
if (preference != null) {
preference.setOnPreferenceChangeListener((preference13, newValue) -> {
if ((Boolean) newValue) {
if (ActivityCompat.checkSelfPermission(requireContext(), SCHEDULE_EXACT_ALARM) != PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(SCHEDULE_EXACT_ALARM);
AlarmManager alarmManager = requireContext().getSystemService(AlarmManager.class);
if (!alarmManager.canScheduleExactAlarms()) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.enable_alarm_dialog).
setPositiveButton(R.string.ok, (dialog, id) -> {
Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
requireContext().startActivity(intent);
}).
setNegativeButton(R.string.cancel, (dialog, id) -> {
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit().putBoolean("exact_reminders", false).apply();
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.root_preferences);
});

builder.create().show();
}
}
return true;
Expand Down Expand Up @@ -131,6 +135,18 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
}

@Override
public void onResume() {
super.onResume();
SwitchPreference preference = getPreferenceScreen().findPreference("exact_reminders");
if (preference != null) {
AlarmManager alarmManager = requireContext().getSystemService(AlarmManager.class);
if (!alarmManager.canScheduleExactAlarms()) {
preference.setChecked(false);
}
}
}

@Override
public void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
Expand Down Expand Up @@ -62,7 +64,12 @@ private void schedule(Context context, AlarmManager alarmManager, Instant timest

// Cancel potentially already running alarm and set new
alarmManager.cancel(pendingIntent);
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timestamp.toEpochMilli(), pendingIntent);

if (canScheduleExactAlarms(alarmManager)) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timestamp.toEpochMilli(), pendingIntent);
} else {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timestamp.toEpochMilli(), pendingIntent);
}

// Notify GUI listener
NextReminderListener.sendNextReminder(context, reminder.reminderId, timestamp);
Expand All @@ -77,4 +84,11 @@ private void schedule(Context context, AlarmManager alarmManager, Instant timest
WorkManager.getInstance(getApplicationContext()).enqueue(reminderWork);
}
}

private boolean canScheduleExactAlarms(AlarmManager alarmManager) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean exactReminders = sharedPref.getBoolean("exact_reminders", true);

return exactReminders && alarmManager.canScheduleExactAlarms();
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@
<string name="exact_reminders">Exakte Erinnerungen</string>
<string name="change_color_toast">Änderungen an den Farben wirken sich nur auf neue Erinnerungen aus</string>
<string name="permission_not_granted">Berechtigung nicht erteilt</string>
<string name="exact_reminders_summary">Erinnerungen werden genau zur eingestellten Zeit erzeugt (erhöhter Batterieverbrauch)</string>
<string name="enable_alarm_dialog">Um die Erstellung von exakten Erinnerungen zu ermöglichen, erlaube dies im folgenden Menü</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@
<string name="exact_reminders">Exact reminders</string>
<string name="change_color_toast">Note that changes in color only take effect in future reminders</string>
<string name="permission_not_granted">Permission not granted</string>
<string name="exact_reminders_summary">Reminders are scheduled at the exact time (increases battery usage)</string>
<string name="enable_alarm_dialog">In order to allow scheduling exact alarms, enable this for MedTimer in the following menu</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:summary="@string/exact_reminders_summary"
android:title="@string/exact_reminders"
app:isPreferenceVisible="false"
app:key="exact_reminders" />
<ListPreference
android:layout_width="wrap_content"
Expand Down

0 comments on commit dc14470

Please sign in to comment.