From 2ef44e766e2234d461855dfc90330fb19174c646 Mon Sep 17 00:00:00 2001 From: Davis Mosenkovs Date: Mon, 2 Oct 2023 21:48:46 +0300 Subject: [PATCH] PineTime: Improve notification handling * If setting enabled, add source application (or SMS) * Fallback to using notification subject if body is unavailable * Trim/cut sender or title at 25+ chars if necessary * Better support for notifications without body or subject --- .../DeviceSettingsPreferenceConst.java | 2 ++ .../DeviceSpecificSettingsFragment.java | 2 ++ .../pinetime/PineTimeJFCoordinator.java | 3 +- .../devices/pinetime/PineTimeJFSupport.java | 34 +++++++++++++++---- app/src/main/res/values/strings.xml | 3 ++ ...esettings_prefix_notification_with_app.xml | 9 +++++ 6 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_prefix_notification_with_app.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java index 4d5b514f5..a13986ae8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java @@ -364,4 +364,6 @@ public class DeviceSettingsPreferenceConst { public static final String PREF_TEMPERATURE_SCALE_CF = "temperature_scale_cf"; public static final String PREF_FEMOMETER_MEASUREMENT_MODE = "femometer_measurement_mode"; + + public static final String PREF_PREFIX_NOTIFICATION_WITH_APP = "pref_prefix_notification_with_app"; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java index c9e8ef028..f986a930a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java @@ -549,6 +549,8 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i addPreferenceHandlerFor(PREF_TEMPERATURE_SCALE_CF); + addPreferenceHandlerFor(PREF_PREFIX_NOTIFICATION_WITH_APP); + addPreferenceHandlerFor("lock"); String sleepTimeState = prefs.getString(PREF_SLEEP_TIME, PREF_DO_NOT_DISTURB_OFF); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeJFCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeJFCoordinator.java index 716795d51..5574bf5ca 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeJFCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pinetime/PineTimeJFCoordinator.java @@ -166,7 +166,8 @@ public class PineTimeJFCoordinator extends AbstractBLEDeviceCoordinator { public int[] getSupportedDeviceSpecificSettings(GBDevice device) { return new int[]{ R.xml.devicesettings_transliteration, - R.xml.devicesettings_world_clocks + R.xml.devicesettings_world_clocks, + R.xml.devicesettings_prefix_notification_with_app }; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java index b683e6114..e7ed1f60b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pinetime/PineTimeJFSupport.java @@ -91,6 +91,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec; import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec; import nodomain.freeyourgadget.gadgetbridge.model.NavigationInfoSpec; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; +import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.model.WorldClock; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport; @@ -118,6 +119,7 @@ public class PineTimeJFSupport extends AbstractBTLEDeviceSupport implements DfuL private final BatteryInfoProfile batteryInfoProfile; private final int MaxNotificationLength = 100; + private final int CutNotificationTitleMinAt = 25; private int firmwareVersionMajor = 0; private int firmwareVersionMinor = 0; private int firmwareVersionPatch = 0; @@ -306,15 +308,35 @@ public class PineTimeJFSupport extends AbstractBTLEDeviceSupport implements DfuL TransactionBuilder builder = new TransactionBuilder("notification"); String message; - if (notificationSpec.body == null) { - notificationSpec.body = ""; + String source = null; + String bodyOrSubject = nodomain.freeyourgadget.gadgetbridge.util.StringUtils.getFirstOf(notificationSpec.body, notificationSpec.subject); + String senderOrTitle = nodomain.freeyourgadget.gadgetbridge.util.StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title); + if (!nodomain.freeyourgadget.gadgetbridge.util.StringUtils.isNullOrEmpty(notificationSpec.sourceName)) { + source = notificationSpec.sourceName; + } else if (notificationSpec.type == NotificationType.GENERIC_SMS) { + source = getContext().getString(R.string.pref_title_notifications_sms); } - if (isFirmwareAtLeastVersion0_15()) { - String senderOrTitle = nodomain.freeyourgadget.gadgetbridge.util.StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title); - message = senderOrTitle + "\0" + notificationSpec.body; + if (bodyOrSubject.length() > 0){ + if (isFirmwareAtLeastVersion0_15()) { + if (!GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_PREFIX_NOTIFICATION_WITH_APP, true)) { + source = null; + } + int cutLength = Math.max(CutNotificationTitleMinAt, MaxNotificationLength - 3 - bodyOrSubject.length() - (source != null ? source.length() + 2 : 0)); + if (cutLength < senderOrTitle.length() - 1) { + for (; cutLength > 0 && senderOrTitle.charAt(cutLength - 1) == ' '; cutLength--); + senderOrTitle = senderOrTitle.substring(0, cutLength) + ">"; + } + message = nodomain.freeyourgadget.gadgetbridge.util.StringUtils.join(": ", source, senderOrTitle) + "\0" + bodyOrSubject; + } else { + message = bodyOrSubject; + } } else { - message = notificationSpec.body; + if (isFirmwareAtLeastVersion0_15()) { + message = (source != null ? source : "") + "\0" + senderOrTitle; + } else { + message = senderOrTitle; + } } NewAlert alert = new NewAlert(AlertCategory.CustomHuami, 1, message); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a137f4739..2d7151688 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2397,4 +2397,7 @@ Ignore (silence) Call rejection method Which action is taken when an incoming call is rejected from the watch + + App name in notification + Prefix notification title with name of source application diff --git a/app/src/main/res/xml/devicesettings_prefix_notification_with_app.xml b/app/src/main/res/xml/devicesettings_prefix_notification_with_app.xml new file mode 100644 index 000000000..4398bb873 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_prefix_notification_with_app.xml @@ -0,0 +1,9 @@ + + + +