1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +01:00

Allow toggling DND through device actions

This commit is contained in:
José Rebelo 2023-12-09 11:01:55 +00:00
parent 577ee27c14
commit 25196a932e
4 changed files with 50 additions and 3 deletions

View File

@ -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);
}

View File

@ -2562,6 +2562,10 @@
<item>@string/pref_device_action_fitness_app_control_start</item>
<item>@string/pref_device_action_fitness_app_control_stop</item>
<item>@string/pref_device_action_fitness_app_control_toggle</item>
<item>@string/pref_device_action_dnd_off</item>
<item>@string/pref_device_action_dnd_priority</item>
<item>@string/pref_device_action_dnd_alarms</item>
<item>@string/pref_device_action_dnd_on</item>
</string-array>
<string-array name="device_action_values">
@ -2573,6 +2577,10 @@
<item>@string/pref_device_action_fitness_app_control_start_value</item>
<item>@string/pref_device_action_fitness_app_control_stop_value</item>
<item>@string/pref_device_action_fitness_app_control_toggle_value</item>
<item>@string/pref_device_action_dnd_off_value</item>
<item>@string/pref_device_action_dnd_priority_value</item>
<item>@string/pref_device_action_dnd_alarms_value</item>
<item>@string/pref_device_action_dnd_on_value</item>
</string-array>
<string-array name="pref_hybridhr_buttonfunctions">

View File

@ -2161,6 +2161,10 @@
<string name="pref_device_action_fitness_app_control_stop">Fitness App Tracking Stop</string>
<string name="pref_device_action_fitness_app_control_toggle">Toggle Fitness App Tracking</string>
<string name="pref_device_action_phone_gps_location_listener_stop">GPS Location Listener Stop</string>
<string name="pref_device_action_dnd_off">Do not disturb - Off</string>
<string name="pref_device_action_dnd_priority">Do not disturb - Priority only</string>
<string name="pref_device_action_dnd_alarms">Do not disturb - Alarms only</string>
<string name="pref_device_action_dnd_on">Do not disturb - On</string>
<!-- Translators: the ### indicate number of digits, keep intact -->
<string name="distance_format_meters">###m</string>
<!-- Translators: the ### indicate number of digits, keep intact -->

View File

@ -125,6 +125,10 @@
<item name="pref_device_action_fitness_app_control_start_value" translatable="false" type="string">FITNESS_CONTROL_START</item>
<item name="pref_device_action_fitness_app_control_stop_value" translatable="false" type="string">FITNESS_CONTROL_STOP</item>
<item name="pref_device_action_fitness_app_control_toggle_value" translatable="false" type="string">FITNESS_CONTROL_TOGGLE</item>
<item name="pref_device_action_dnd_off_value" translatable="false" type="string">DO_NOT_DISTURB_OFF</item>
<item name="pref_device_action_dnd_priority_value" translatable="false" type="string">DO_NOT_DISTURB_PRIORITY</item>
<item name="pref_device_action_dnd_alarms_value" translatable="false" type="string">DO_NOT_DISTURB_ALARMS</item>
<item name="pref_device_action_dnd_on_value" translatable="false" type="string">DO_NOT_DISTURB_ON</item>
<item name="pref_theme_value_system" translatable="false" type="string">system</item>
<item name="pref_theme_value_light" translatable="false" type="string">light</item>