From b19cf85a127afb849918b285dfcfd6deabab105a Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Fri, 18 Aug 2017 16:21:54 +0200 Subject: [PATCH] Amazfit Bip: support E-Mail icon and do also send notifications from unknown sources --- .../devices/amazfitbip/AmazfitBipSupport.java | 25 ++++++++++++++++ .../AmazfitBipTextNotificationStrategy.java | 30 +++++++++++++++---- .../devices/miband2/MiBand2Support.java | 4 +-- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java index 1a874e64..afaf3824 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipSupport.java @@ -17,9 +17,16 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.amazfitbip; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl; +import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; +import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; +import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; +import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions; +import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support; +import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils; +import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class AmazfitBipSupport extends MiBand2Support { @Override @@ -27,6 +34,24 @@ public class AmazfitBipSupport extends MiBand2Support { return new AmazfitBipTextNotificationStrategy(this); } + @Override + public void onNotification(NotificationSpec notificationSpec) { + if (notificationSpec.type == NotificationType.GENERIC_ALARM_CLOCK) { + onAlarmClock(notificationSpec); + return; + } + String message = NotificationUtils.getPreferredTextFor(notificationSpec, 40, 40, getContext()).trim(); + + // Fixup purging notification text in getPreferredTextFor() in case of UNKNOWN + if (notificationSpec.type == NotificationType.UNKNOWN) { + message = StringUtils.getFirstOf(notificationSpec.title, notificationSpec.body); + + } + String origin = notificationSpec.type.getGenericType(); + SimpleNotification simpleNotification = new SimpleNotification(message, BLETypeConversions.toAlertCategory(notificationSpec.type)); + performPreferredNotification(origin + " received", origin, simpleNotification, MiBand2Service.ALERT_LEVEL_MESSAGE, null); + } + @Override public void onFindDevice(boolean start) { CallSpec callSpec = new CallSpec(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipTextNotificationStrategy.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipTextNotificationStrategy.java index 0df1475a..eb3da164 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipTextNotificationStrategy.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/AmazfitBipTextNotificationStrategy.java @@ -16,10 +16,15 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.service.devices.amazfitbip; +import android.support.annotation.NonNull; + import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory; +import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile; +import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert; +import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.OverflowStrategy; import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.Mi2TextNotificationStrategy; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.MiBand2Support; @@ -33,14 +38,27 @@ class AmazfitBipTextNotificationStrategy extends Mi2TextNotificationStrategy { @Override protected void sendCustomNotification(VibrationProfile vibrationProfile, SimpleNotification simpleNotification, BtLEAction extraAction, TransactionBuilder builder) { - if (simpleNotification != null && simpleNotification.getAlertCategory() == AlertCategory.IncomingCall) { - // incoming calls are notified solely via NewAlert including caller ID - sendAlert(simpleNotification, builder); - return; - } - if (simpleNotification != null && !StringUtils.isEmpty(simpleNotification.getMessage())) { sendAlert(simpleNotification, builder); } } + + @Override + protected void sendAlert(@NonNull SimpleNotification simpleNotification, TransactionBuilder builder) { + AlertNotificationProfile profile = new AlertNotificationProfile<>(getSupport()); + + AlertCategory category = simpleNotification.getAlertCategory(); + switch (simpleNotification.getAlertCategory()) { + // only these are confirmed working so far on Amazfit Bip + case Email: + case IncomingCall: + case SMS: + break; + // default to SMS for non working categories + default: + category = AlertCategory.SMS; + } + NewAlert alert = new NewAlert(category, 1, simpleNotification.getMessage()); + profile.newAlert(builder, alert, OverflowStrategy.MAKE_MULTIPLE); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java index a7ab3362..012aeef1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java @@ -435,7 +435,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { } } - private void performPreferredNotification(String task, String notificationOrigin, SimpleNotification simpleNotification, int alertLevel, BtLEAction extraAction) { + protected void performPreferredNotification(String task, String notificationOrigin, SimpleNotification simpleNotification, int alertLevel, BtLEAction extraAction) { try { TransactionBuilder builder = performInitialized(task); Prefs prefs = GBApplication.getPrefs(); @@ -529,7 +529,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport { performPreferredNotification(origin + " received", origin, simpleNotification, alertLevel, null); } - private void onAlarmClock(NotificationSpec notificationSpec) { + protected void onAlarmClock(NotificationSpec notificationSpec) { alarmClockRinging = true; AbortTransactionAction abortAction = new StopNotificationAction(getCharacteristic(GattCharacteristic.UUID_CHARACTERISTIC_ALERT_LEVEL)) { @Override