From 3f8eeebc5a3082bf3f513bdfbe84196cab7c7b2e Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Fri, 16 Aug 2024 19:18:50 +0200 Subject: [PATCH] Garmin: simplify handling canned replies and only send error if no reply is set at all --- .../devices/garmin/ProtocolBufferHandler.java | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java index 10f60d8e3..14c55d958 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/ProtocolBufferHandler.java @@ -350,50 +350,26 @@ public class ProtocolBufferHandler implements MessageHandler { // Mark canned messages as supported deviceSupport.evaluateGBDeviceEvent(new GBDeviceEventUpdatePreferences(GarminPreferences.PREF_FEAT_CANNED_MESSAGES, true)); - if (this.cannedListTypeMap.isEmpty()) { - List requestedTypes = smsNotificationService.getSmsCannedListRequest().getRequestedTypesList(); - for (GdiSmsNotification.SmsNotificationService.CannedListType type : - requestedTypes) { - if (GdiSmsNotification.SmsNotificationService.CannedListType.SMS_MESSAGE_RESPONSE.equals(type)) { - final ArrayList messages = new ArrayList<>(); - for (int i = 1; i <= 16; i++) { - String message = deviceSupport.getDevicePrefs().getString("canned_reply_" + i, null); - if (message != null && !message.isEmpty()) { - messages.add(message); - } - } - if (!messages.isEmpty()) - this.cannedListTypeMap.put(type, messages.toArray(new String[0])); - } else if (GdiSmsNotification.SmsNotificationService.CannedListType.PHONE_CALL_RESPONSE.equals(type)) { - final ArrayList messages = new ArrayList<>(); - for (int i = 1; i <= 16; i++) { - String message = deviceSupport.getDevicePrefs().getString("canned_message_dismisscall_" + i, null); - if (message != null && !message.isEmpty()) { - messages.add(message); - } - } - if (!messages.isEmpty()) - this.cannedListTypeMap.put(type, messages.toArray(new String[0])); - } - } - - } - List requestedTypes = smsNotificationService.getSmsCannedListRequest().getRequestedTypesList(); + populateCannedListTypeMap(requestedTypes); + GdiSmsNotification.SmsNotificationService.SmsCannedListResponse.Builder builder = GdiSmsNotification.SmsNotificationService.SmsCannedListResponse.newBuilder() .setStatus(GdiSmsNotification.SmsNotificationService.ResponseStatus.SUCCESS); + boolean found = false; for (GdiSmsNotification.SmsNotificationService.CannedListType requestedType : requestedTypes) { if (this.cannedListTypeMap.containsKey(requestedType)) { + found = true; builder.addLists(GdiSmsNotification.SmsNotificationService.SmsCannedList.newBuilder() .addAllResponse(Arrays.asList(Objects.requireNonNull(this.cannedListTypeMap.get(requestedType)))) .setType(requestedType) ); } else { - builder.setStatus(GdiSmsNotification.SmsNotificationService.ResponseStatus.GENERIC_ERROR); - LOG.info("Missing canned messages data for type {}", requestedType); + LOG.warn("Missing canned messages data for type {}", requestedType); } } + if (!found) + builder.setStatus(GdiSmsNotification.SmsNotificationService.ResponseStatus.GENERIC_ERROR); return GdiSmartProto.Smart.newBuilder().setSmsNotificationService(GdiSmsNotification.SmsNotificationService.newBuilder().setSmsCannedListResponse(builder)).build(); } else { @@ -402,6 +378,30 @@ public class ProtocolBufferHandler implements MessageHandler { } } + private void populateCannedListTypeMap(List requestedTypes) { + if (this.cannedListTypeMap.isEmpty()) { + for (GdiSmsNotification.SmsNotificationService.CannedListType type : + requestedTypes) { + String preferencesPrefix = ""; + if (GdiSmsNotification.SmsNotificationService.CannedListType.SMS_MESSAGE_RESPONSE.equals(type)) + preferencesPrefix = "canned_reply_"; + else if (GdiSmsNotification.SmsNotificationService.CannedListType.PHONE_CALL_RESPONSE.equals(type)) + preferencesPrefix = "canned_message_dismisscall_"; + else + continue; + final ArrayList messages = new ArrayList<>(); + for (int i = 1; i <= 16; i++) { + String message = deviceSupport.getDevicePrefs().getString(preferencesPrefix + i, null); + if (message != null && !message.isEmpty()) { + messages.add(message); + } + } + if (!messages.isEmpty()) + this.cannedListTypeMap.put(type, messages.toArray(new String[0])); + } + } + } + private void processProtobufFindMyWatchResponse(GdiFindMyWatch.FindMyWatchService findMyWatchService) { if (findMyWatchService.hasCancelRequest()) { LOG.info("Watch found");