From 9ca3fff101cfd2d40796d8cd83b79e7d53fd0792 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Sun, 15 May 2022 16:51:41 +0200 Subject: [PATCH] Add support for native `Do Not Disturb` call/SMS functionality on Fossil watches --- .../freeyourgadget/gadgetbridge/GBApplication.java | 6 ++---- .../externalevents/PhoneCallReceiver.java | 10 ++++++++-- .../gadgetbridge/externalevents/SMSReceiver.java | 10 ++++++++-- .../gadgetbridge/impl/GBDeviceService.java | 3 ++- .../gadgetbridge/model/CallSpec.java | 1 + .../gadgetbridge/model/DeviceService.java | 1 + .../service/DeviceCommunicationService.java | 2 ++ .../adapter/fossil_hr/FossilHRWatchAdapter.java | 6 +++--- .../notification/PlayCallNotificationRequest.java | 14 ++++++++------ 9 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 10ac359e1..f746608d3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -438,10 +438,8 @@ public class GBApplication extends Application { @TargetApi(Build.VERSION_CODES.M) public static int getGrantedInterruptionFilter() { - if (prefs.getBoolean("notification_filter", false) && GBApplication.isRunningMarshmallowOrLater()) { - if (notificationManager.isNotificationPolicyAccessGranted()) { - return notificationManager.getCurrentInterruptionFilter(); - } + if (GBApplication.isRunningMarshmallowOrLater() && notificationManager.isNotificationPolicyAccessGranted()) { + return notificationManager.getCurrentInterruptionFilter(); } return NotificationManager.INTERRUPTION_FILTER_ALL; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java index 027772269..d4922d543 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/PhoneCallReceiver.java @@ -107,22 +107,28 @@ public class PhoneCallReceiver extends BroadcastReceiver { if ("never".equals(prefs.getString("notification_mode_calls", "always"))) { return; } + int dndSuppressed = 0; switch (GBApplication.getGrantedInterruptionFilter()) { case NotificationManager.INTERRUPTION_FILTER_ALL: break; case NotificationManager.INTERRUPTION_FILTER_ALARMS: case NotificationManager.INTERRUPTION_FILTER_NONE: - return; + dndSuppressed = 1; + break; case NotificationManager.INTERRUPTION_FILTER_PRIORITY: if (GBApplication.isPriorityNumber(Policy.PRIORITY_CATEGORY_CALLS, mSavedNumber)) { break; } // FIXME: Handle Repeat callers if it is enabled in Do Not Disturb - return; + dndSuppressed = 1; + } + if (prefs.getBoolean("notification_filter", false) && dndSuppressed == 1) { + return; } CallSpec callSpec = new CallSpec(); callSpec.number = mSavedNumber; callSpec.command = callCommand; + callSpec.dndSuppressed = dndSuppressed; GBApplication.deviceService().onSetCallState(callSpec); } mLastState = state; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java index 7065c39bb..6d1812133 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/SMSReceiver.java @@ -87,18 +87,24 @@ public class SMSReceiver extends BroadcastReceiver { dismissAllAction.type = NotificationSpec.Action.TYPE_SYNTECTIC_DISMISS_ALL; notificationSpec.attachedActions.add(dismissAllAction); + int dndSuppressed = 0; switch (GBApplication.getGrantedInterruptionFilter()) { case NotificationManager.INTERRUPTION_FILTER_ALL: break; case NotificationManager.INTERRUPTION_FILTER_ALARMS: case NotificationManager.INTERRUPTION_FILTER_NONE: - return; + dndSuppressed = 1; + break; case NotificationManager.INTERRUPTION_FILTER_PRIORITY: if (GBApplication.isPriorityNumber(Policy.PRIORITY_CATEGORY_MESSAGES, notificationSpec.phoneNumber)) { break; } - return; + dndSuppressed = 1; } + if (prefs.getBoolean("notification_filter", false) && dndSuppressed == 1) { + return; + } + notificationSpec.dndSuppressed = dndSuppressed; GBApplication.deviceService().onNotification(notificationSpec); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java index 15f842bb7..d0f12cc73 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -201,7 +201,8 @@ public class GBDeviceService implements DeviceService { Intent intent = createIntent().setAction(ACTION_CALLSTATE) .putExtra(EXTRA_CALL_PHONENUMBER, callSpec.number) .putExtra(EXTRA_CALL_DISPLAYNAME, callSpec.name) - .putExtra(EXTRA_CALL_COMMAND, callSpec.command); + .putExtra(EXTRA_CALL_COMMAND, callSpec.command) + .putExtra(EXTRA_CALL_DNDSUPPRESSED, callSpec.dndSuppressed); invokeService(intent); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CallSpec.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CallSpec.java index 817ba872c..97a4efd94 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CallSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/CallSpec.java @@ -28,4 +28,5 @@ public class CallSpec { public String number; public String name; public int command; + public int dndSuppressed; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java index edb88e8b6..bd22e9af3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -91,6 +91,7 @@ public interface DeviceService extends EventHandler { String EXTRA_CALL_COMMAND = "call_command"; String EXTRA_CALL_PHONENUMBER = "call_phonenumber"; String EXTRA_CALL_DISPLAYNAME = "call_displayname"; + String EXTRA_CALL_DNDSUPPRESSED = "call_dndsuppressed"; String EXTRA_CANNEDMESSAGES = "cannedmessages"; String EXTRA_CANNEDMESSAGES_TYPE = "cannedmessages_type"; String EXTRA_MUSIC_ARTIST = "music_artist"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 0708899dd..d83db85c6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -141,6 +141,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CAL import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_TYPE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_COMMAND; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_DISPLAYNAME; +import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_DNDSUPPRESSED; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_PHONENUMBER; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES_TYPE; @@ -511,6 +512,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere callSpec.command = intent.getIntExtra(EXTRA_CALL_COMMAND, CallSpec.CALL_UNDEFINED); callSpec.number = intent.getStringExtra(EXTRA_CALL_PHONENUMBER); callSpec.name = sanitizeNotifText(intent.getStringExtra(EXTRA_CALL_DISPLAYNAME)); + callSpec.dndSuppressed = intent.getIntExtra(EXTRA_CALL_DNDSUPPRESSED, 0); mDeviceSupport.onSetCallState(callSpec); break; case ACTION_SETCANNEDMESSAGES: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java index 3f17f3029..bdd0a63f6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/adapter/fossil_hr/FossilHRWatchAdapter.java @@ -1157,10 +1157,10 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { boolean quickRepliesEnabled = quickReplies.length > 0 && callSpec.number != null && callSpec.number.matches("^\\+(?:[0-9] ?){6,14}[0-9]$"); if (callSpec.command == CallSpec.CALL_INCOMING) { currentCallSpec = callSpec; - queueWrite(new PlayCallNotificationRequest(StringUtils.getFirstOf(callSpec.name, callSpec.number), true, quickRepliesEnabled, this)); + queueWrite(new PlayCallNotificationRequest(StringUtils.getFirstOf(callSpec.name, callSpec.number), true, quickRepliesEnabled, callSpec.dndSuppressed, this)); } else { currentCallSpec = null; - queueWrite(new PlayCallNotificationRequest(StringUtils.getFirstOf(callSpec.name, callSpec.number), false, quickRepliesEnabled, this)); + queueWrite(new PlayCallNotificationRequest(StringUtils.getFirstOf(callSpec.name, callSpec.number), false, quickRepliesEnabled, callSpec.dndSuppressed, this)); } } @@ -1616,7 +1616,7 @@ public class FossilHRWatchAdapter extends FossilWatchAdapter { private void handleCallRequest(byte[] value) { boolean acceptCall = value[7] == (byte) 0x00; - queueWrite(new PlayCallNotificationRequest("", false, false, this)); + queueWrite(new PlayCallNotificationRequest("", false, false, 0,this)); GBDeviceEventCallControl callControlEvent = new GBDeviceEventCallControl(); callControlEvent.event = acceptCall ? GBDeviceEventCallControl.Event.START : GBDeviceEventCallControl.Event.REJECT; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayCallNotificationRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayCallNotificationRequest.java index 15d2d0994..64906aa85 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayCallNotificationRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/qhybrid/requests/fossil/notification/PlayCallNotificationRequest.java @@ -24,18 +24,20 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.adapter.foss public class PlayCallNotificationRequest extends PlayNotificationRequest { private final static int MESSAGE_ID_CALL = 1; - private static int notificationFlags(boolean callStart, boolean quickReplies) { + private static int notificationFlags(boolean callStart, boolean quickReplies, int dndSuppressed) { + int flags = 0; if (callStart && quickReplies) { - return 0b00111000; + flags = 0b00111000; } else if (callStart) { - return 0b00011000; + flags = 0b00011000; } else { - return 0b00000010; + flags = 0b00000010; } + return (flags | dndSuppressed); } - public PlayCallNotificationRequest(String number, boolean callStart, boolean quickReplies, FossilWatchAdapter adapter) { - super(callStart ? NotificationType.INCOMING_CALL : NotificationType.DISMISS_NOTIFICATION, notificationFlags(callStart, quickReplies), + public PlayCallNotificationRequest(String number, boolean callStart, boolean quickReplies, int dndSuppressed, FossilWatchAdapter adapter) { + super(callStart ? NotificationType.INCOMING_CALL : NotificationType.DISMISS_NOTIFICATION, notificationFlags(callStart, quickReplies, dndSuppressed), ByteBuffer.wrap(new byte[]{(byte) 0x80, (byte) 0x00, (byte) 0x59, (byte) 0xB7}).order(ByteOrder.LITTLE_ENDIAN).getInt(), number, "Incoming Call", MESSAGE_ID_CALL, adapter); }