From 25196a932ed4e7d6bc37db511ee15497febc271b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sat, 9 Dec 2023 11:01:55 +0000 Subject: [PATCH] Allow toggling DND through device actions --- .../service/AbstractDeviceSupport.java | 37 +++++++++++++++++-- app/src/main/res/values/arrays.xml | 8 ++++ app/src/main/res/values/strings.xml | 4 ++ app/src/main/res/values/values.xml | 4 ++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index 8a140d689..832c09f74 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -19,10 +19,9 @@ package nodomain.freeyourgadget.gadgetbridge.service; import android.app.Notification; +import android.app.NotificationManager; import android.app.PendingIntent; import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothGatt; import android.companion.CompanionDeviceManager; import android.content.Context; import android.content.Intent; @@ -106,7 +105,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID; -import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID; // TODO: support option for a single reminder notification when notifications could not be delivered? // conditions: app was running and received notifications, but device was not connected. @@ -552,6 +550,10 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { final String actionMediaPlay = getContext().getString(R.string.pref_media_play_value); final String actionMediaPause = getContext().getString(R.string.pref_media_pause_value); final String actionMediaPlayPause = getContext().getString(R.string.pref_media_playpause_value); + final String actionDndOff = getContext().getString(R.string.pref_device_action_dnd_off_value); + final String actionDndpriority = getContext().getString(R.string.pref_device_action_dnd_priority_value); + final String actionDndAlarms = getContext().getString(R.string.pref_device_action_dnd_alarms_value); + final String actionDndOn = getContext().getString(R.string.pref_device_action_dnd_on_value); if (actionBroadcast.equals(action)) { if (message != null) { @@ -588,6 +590,35 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { return; } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + final int interruptionFilter; + if (actionDndOff.equals(action)) { + interruptionFilter = NotificationManager.INTERRUPTION_FILTER_ALL; + } else if (actionDndpriority.equals(action)) { + interruptionFilter = NotificationManager.INTERRUPTION_FILTER_PRIORITY; + } else if (actionDndAlarms.equals(action)) { + interruptionFilter = NotificationManager.INTERRUPTION_FILTER_ALARMS; + } else if (actionDndOn.equals(action)) { + interruptionFilter = NotificationManager.INTERRUPTION_FILTER_NONE; + } else { + interruptionFilter = NotificationManager.INTERRUPTION_FILTER_UNKNOWN; + } + + if (interruptionFilter != NotificationManager.INTERRUPTION_FILTER_UNKNOWN) { + LOG.debug("Setting do not disturb to {} for {}", interruptionFilter, action); + + if (!notificationManager.isNotificationPolicyAccessGranted()) { + LOG.warn("Do not disturb permissions not granted"); + return; + } + + notificationManager.setInterruptionFilter(interruptionFilter); + return; + } + } + LOG.warn("Unhandled device state change action (action: {}, message: {})", action, message); } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 476aed6a6..176b46307 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -2562,6 +2562,10 @@ @string/pref_device_action_fitness_app_control_start @string/pref_device_action_fitness_app_control_stop @string/pref_device_action_fitness_app_control_toggle + @string/pref_device_action_dnd_off + @string/pref_device_action_dnd_priority + @string/pref_device_action_dnd_alarms + @string/pref_device_action_dnd_on @@ -2573,6 +2577,10 @@ @string/pref_device_action_fitness_app_control_start_value @string/pref_device_action_fitness_app_control_stop_value @string/pref_device_action_fitness_app_control_toggle_value + @string/pref_device_action_dnd_off_value + @string/pref_device_action_dnd_priority_value + @string/pref_device_action_dnd_alarms_value + @string/pref_device_action_dnd_on_value diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c43545adc..089c7029c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2161,6 +2161,10 @@ Fitness App Tracking Stop Toggle Fitness App Tracking GPS Location Listener Stop + Do not disturb - Off + Do not disturb - Priority only + Do not disturb - Alarms only + Do not disturb - On ###m diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index 3cddf520d..d1d4187bb 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -125,6 +125,10 @@ FITNESS_CONTROL_START FITNESS_CONTROL_STOP FITNESS_CONTROL_TOGGLE + DO_NOT_DISTURB_OFF + DO_NOT_DISTURB_PRIORITY + DO_NOT_DISTURB_ALARMS + DO_NOT_DISTURB_ON system light