Skip to content

Commit

Permalink
triggerDoNotDisturb
Browse files Browse the repository at this point in the history
adds bedtime option for using dnd rules, or overriding dnd directly (#818)
  • Loading branch information
forrestguice committed Sep 30, 2024
1 parent 00526a7 commit a3c081c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,9 @@
<string name="configLabel_bedtime_tile_active">Bedtime (Active)</string>
<string name="configLabel_bedtime_tile_paused">Bedtime (Paused)</string>

<string name="configLabel_dnd_rulebased">Use \'Do Not Disturb\' rule</string> <!-- TODO -->
<string name="configLabel_dnd_rulebased_summary">Trigger \'Do Not Disturb\' settings using an automatic rule (recommended).</string> <!-- TODO -->

<string name="configLabel_bedtime_zenrule_name">Bedtime (Suntimes)</string> <!-- do-not-disturb rule name; appears in global-dnd-settings/schedule -->

<string name="configAction_dnd_allow">Allow Access</string> <!-- button label when requesting permission -->
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml-v11/preference_alarms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
android:title="@string/configLabel_dnd"
android:summary="@string/configLabel_permissionGranted" />

<CheckBoxPreference
android:key="app_bedtime_dnd_rulebased"
android:title="@string/configLabel_dnd_rulebased"
android:summary="@string/configLabel_dnd_rulebased_summary" />

</PreferenceCategory>

<PreferenceCategory
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml-v34/preference_alarms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
android:title="@string/configLabel_dnd"
android:summary="@string/configLabel_permissionGranted" />

<CheckBoxPreference
android:key="app_bedtime_dnd_rulebased"
android:title="@string/configLabel_dnd_rulebased"
android:summary="@string/configLabel_dnd_rulebased_summary" />

</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit a3c081c

Please sign in to comment.