From 4ccf68af0a5037ed3dec2da17e886b07deb311cc Mon Sep 17 00:00:00 2001 From: Me7c7 Date: Sun, 5 Jan 2025 14:41:27 +0200 Subject: [PATCH] Huawei: Fix notifications handling without key --- .../externalevents/NotificationListener.java | 1 - .../devices/huawei/HuaweiNotificationsManager.java | 12 +++++++++--- .../huawei/requests/SendNotificationRequest.java | 10 ++++------ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index 316416f89..4ba7d90a4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -55,7 +55,6 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiNotificationsManager.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiNotificationsManager.java index 77066f73e..282b31af9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiNotificationsManager.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiNotificationsManager.java @@ -42,6 +42,12 @@ public class HuaweiNotificationsManager { notificationSpecCache.offer(notificationSpec); } + public static String getNotificationKey(NotificationSpec notificationSpec) { + if(!TextUtils.isEmpty(notificationSpec.key)) { + return notificationSpec.key; + } + return "0|" + notificationSpec.sourceAppId + "|" + notificationSpec.getId() + "||0"; + } public void onNotification(NotificationSpec notificationSpec) { @@ -79,7 +85,7 @@ public class HuaweiNotificationsManager { SendNotificationRemoveRequest sendNotificationReq = new SendNotificationRemoveRequest(this.support, SendNotificationRequest.getNotificationType(notificationSpec.type), // notificationType notificationSpec.sourceAppId, - notificationSpec.key, + getNotificationKey(notificationSpec), id, notificationSpec.channelId, notificationSpec.category); @@ -105,8 +111,8 @@ public class HuaweiNotificationsManager { } NotificationSpec notificationSpec = null; for (NotificationSpec spec : notificationSpecCache) { - notificationSpec = spec; - if (notificationSpec.key.equals(response.key)) { + if (getNotificationKey(spec).equals(response.key)) { + notificationSpec = spec; break; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SendNotificationRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SendNotificationRequest.java index 65deee9b4..03a0e3198 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SendNotificationRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/SendNotificationRequest.java @@ -16,7 +16,7 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests; -import android.text.TextUtils; +import static nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiNotificationsManager.getNotificationKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,7 +31,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider; public class SendNotificationRequest extends Request { - private static final Logger LOG = LoggerFactory.getLogger(SendNotificationRequest.class); private HuaweiPacket packet; @@ -54,8 +53,7 @@ public class SendNotificationRequest extends Request { return Notifications.NotificationType.sms; } } - - + public void buildNotificationTLVFromNotificationSpec(NotificationSpec notificationSpec) { String title; if (notificationSpec.title != null) @@ -76,7 +74,7 @@ public class SendNotificationRequest extends Request { final NotificationSpec.Action action = notificationSpec.attachedActions.get(i); if (action.type == NotificationSpec.Action.TYPE_WEARABLE_REPLY || action.type == NotificationSpec.Action.TYPE_SYNTECTIC_REPLY_PHONENR) { //NOTE: store notification key instead action key. The watch returns this key so it is more easier to find action by notification key - replyKey = notificationSpec.key; + replyKey = getNotificationKey(notificationSpec); break; } } @@ -91,7 +89,7 @@ public class SendNotificationRequest extends Request { params.supportsTimestamp = supportProvider.getHuaweiCoordinator().supportsNotificationsTimestamp(); params.notificationId = notificationSpec.getId(); - params.notificationKey = notificationSpec.key; + params.notificationKey = getNotificationKey(notificationSpec); params.replyKey = replyKey; params.channelId = notificationSpec.channelId; params.category = notificationSpec.category;