From a3c081cae66fabc13d14f01a75885d7164d89329 Mon Sep 17 00:00:00 2001 From: Forrest Guice Date: Mon, 30 Sep 2024 12:38:40 -0700 Subject: [PATCH] triggerDoNotDisturb adds bedtime option for using dnd rules, or overriding dnd directly (#818) --- .../alarmclock/bedtime/BedtimeSettings.java | 24 ++++++++++++++----- app/src/main/res/values/strings.xml | 3 +++ .../main/res/xml-v11/preference_alarms.xml | 5 ++++ .../main/res/xml-v34/preference_alarms.xml | 5 ++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/bedtime/BedtimeSettings.java b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/bedtime/BedtimeSettings.java index b444834bb..e7fd3112a 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/bedtime/BedtimeSettings.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/bedtime/BedtimeSettings.java @@ -75,6 +75,9 @@ public class BedtimeSettings public static final String PREF_KEY_BEDTIME_DND_FILTER = "app_bedtime_dnd_filter"; public static final int PREF_DEF_BEDTIME_DND_FILTER = DND_FILTER_PRIORITY; + public static final String PREF_KEY_BEDTIME_DND_RULEBASED = "app_bedtime_dnd_rulebased"; + public static final boolean PREF_DEF_BEDTIME_DND_RULEBASED = true; + public static final String PREF_KEY_BEDTIME_REMINDER = "app_bedtime_reminder"; public static final boolean PREF_DEF_BEDTIME_REMINDER = false; @@ -147,6 +150,12 @@ public static void savePrefBedtimeDoNotDisturb(Context context, boolean value) prefs.apply(); } + public static boolean loadPrefBedtimeDoNotDisturbRuleBased(Context context) + { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean(PREF_KEY_BEDTIME_DND_RULEBASED, PREF_DEF_BEDTIME_DND_RULEBASED); + } + public static int loadPrefBedtimeDoNotDisturbFilter(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); return prefs.getInt(PREF_KEY_BEDTIME_DND_FILTER, PREF_DEF_BEDTIME_DND_FILTER); @@ -411,17 +420,20 @@ public static boolean isAutomaticZenRuleEnabled(Context context) public static void triggerDoNotDisturb(Context context, boolean value) { - if (Build.VERSION.SDK_INT >= 24) { + boolean useDndRule = BedtimeSettings.loadPrefBedtimeDoNotDisturbRuleBased(context); + if (Build.VERSION.SDK_INT >= 24 && useDndRule) { BedtimeConditionService.triggerBedtimeAutomaticZenRule(context, value); } else if (Build.VERSION.SDK_INT >= 23) { NotificationManager notifications = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (notifications != null && hasDoNotDisturbPermission(context)) - try { - int policy = (value ? NotificationManager.INTERRUPTION_FILTER_ALARMS : NotificationManager.INTERRUPTION_FILTER_ALL); - notifications.setInterruptionFilter(policy); // do-not-disturb requires `android.permission.ACCESS_NOTIFICATION_POLICY` - } catch (SecurityException e) { - Log.w("BedtimeSettings", "Failed to toggle do-not-disturb! " + e); + { + try { + int policy = (value ? NotificationManager.INTERRUPTION_FILTER_ALARMS : NotificationManager.INTERRUPTION_FILTER_ALL); + notifications.setInterruptionFilter(policy); // do-not-disturb requires `android.permission.ACCESS_NOTIFICATION_POLICY` + } catch (SecurityException e) { + Log.w("BedtimeSettings", "Failed to toggle do-not-disturb! " + e); + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bba008341..b5b3c87ed 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1407,6 +1407,9 @@ Bedtime (Active) Bedtime (Paused) + Use \'Do Not Disturb\' rule + Trigger \'Do Not Disturb\' settings using an automatic rule (recommended). + Bedtime (Suntimes) Allow Access diff --git a/app/src/main/res/xml-v11/preference_alarms.xml b/app/src/main/res/xml-v11/preference_alarms.xml index d12d07bbe..6afffe08a 100644 --- a/app/src/main/res/xml-v11/preference_alarms.xml +++ b/app/src/main/res/xml-v11/preference_alarms.xml @@ -88,6 +88,11 @@ android:title="@string/configLabel_dnd" android:summary="@string/configLabel_permissionGranted" /> + + + +