From 915d059c1c3a035355472aa986c5e901b8dc0b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Wed, 24 Apr 2024 19:49:33 +0100 Subject: [PATCH] Garmin: Auto-detect canned messages support --- .../devices/garmin/GarminCoordinator.java | 15 +++++++++++++++ .../devices/garmin/GarminPreferences.java | 1 + .../instinct2s/GarminInstinct2SCoordinator.java | 6 ------ .../garmin/venu3/GarminVenu3Coordinator.java | 6 ------ .../devices/garmin/ProtocolBufferHandler.java | 17 ++++++++++------- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminCoordinator.java index 817f13d0c..316b8c713 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminCoordinator.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import java.util.List; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettings; @@ -14,6 +15,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.GarminSupport; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public abstract class GarminCoordinator extends AbstractBLEDeviceCoordinator { @Override @@ -68,4 +70,17 @@ public abstract class GarminCoordinator extends AbstractBLEDeviceCoordinator { public boolean supportsWeather() { return true; } + + @Override + public int getCannedRepliesSlotCount(final GBDevice device) { + if (getPrefs(device).getBoolean(GarminPreferences.PREF_FEAT_CANNED_MESSAGES, false)) { + return 16; + } + + return 0; + } + + protected static Prefs getPrefs(final GBDevice device) { + return new Prefs(GBApplication.getDeviceSpecificSharedPrefs(device.getAddress())); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminPreferences.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminPreferences.java index f1d5c3159..bc98d4f14 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminPreferences.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/GarminPreferences.java @@ -2,4 +2,5 @@ package nodomain.freeyourgadget.gadgetbridge.devices.garmin; public class GarminPreferences { public static final String PREF_GARMIN_CAPABILITIES = "garmin_capabilities"; + public static final String PREF_FEAT_CANNED_MESSAGES = "feat_canned_messages"; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/instinct2s/GarminInstinct2SCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/instinct2s/GarminInstinct2SCoordinator.java index 0881265e1..ee656d92a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/instinct2s/GarminInstinct2SCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/instinct2s/GarminInstinct2SCoordinator.java @@ -4,7 +4,6 @@ import java.util.regex.Pattern; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.garmin.GarminCoordinator; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; public class GarminInstinct2SCoordinator extends GarminCoordinator { @Override @@ -12,11 +11,6 @@ public class GarminInstinct2SCoordinator extends GarminCoordinator { return Pattern.compile("Instinct 2S"); } - @Override - public int getCannedRepliesSlotCount(final GBDevice device) { - return 16; - } - @Override public int getDeviceNameResource() { return R.string.devicetype_garmin_instinct_2s; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/venu3/GarminVenu3Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/venu3/GarminVenu3Coordinator.java index e9f751f02..6c378a03d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/venu3/GarminVenu3Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/garmin/venu3/GarminVenu3Coordinator.java @@ -4,7 +4,6 @@ import java.util.regex.Pattern; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.garmin.GarminCoordinator; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; public class GarminVenu3Coordinator extends GarminCoordinator { @Override @@ -16,9 +15,4 @@ public class GarminVenu3Coordinator extends GarminCoordinator { public int getDeviceNameResource() { return R.string.devicetype_garmin_vivomove_style; } - - @Override - public int getCannedRepliesSlotCount(final GBDevice device) { - return 16; - } } 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 46b96faac..b6e1e4ea8 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 @@ -12,8 +12,11 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences; +import nodomain.freeyourgadget.gadgetbridge.devices.garmin.GarminPreferences; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.proto.vivomovehr.GdiCalendarService; import nodomain.freeyourgadget.gadgetbridge.proto.vivomovehr.GdiCore; @@ -38,7 +41,7 @@ public class ProtocolBufferHandler implements MessageHandler { private final int maxChunkSize = 375; //tested on VĂ­vomove Style private int lastProtobufRequestId; - private Map cannedListTypeMap; + private final Map cannedListTypeMap = new HashMap<>(); public ProtocolBufferHandler(GarminSupport deviceSupport) { this.deviceSupport = deviceSupport; @@ -239,9 +242,12 @@ public class ProtocolBufferHandler implements MessageHandler { private GdiSmartProto.Smart processProtobufSmsNotificationMessage(GdiSmsNotification.SmsNotificationService smsNotificationService) { if (smsNotificationService.hasSmsCannedListRequest()) { - if (null == this.cannedListTypeMap || this.cannedListTypeMap.isEmpty()) { - this.cannedListTypeMap = new HashMap<>(); + LOG.debug("Got request for sms canned list"); + // 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) { @@ -277,7 +283,7 @@ public class ProtocolBufferHandler implements MessageHandler { for (GdiSmsNotification.SmsNotificationService.CannedListType requestedType : requestedTypes) { if (this.cannedListTypeMap.containsKey(requestedType)) { builder.addLists(GdiSmsNotification.SmsNotificationService.SmsCannedList.newBuilder() - .addAllResponse(Arrays.asList(this.cannedListTypeMap.get(requestedType))) + .addAllResponse(Arrays.asList(Objects.requireNonNull(this.cannedListTypeMap.get(requestedType)))) .setType(requestedType) ); } else { @@ -348,9 +354,6 @@ public class ProtocolBufferHandler implements MessageHandler { return null; } - if (null == this.cannedListTypeMap) { - this.cannedListTypeMap = new HashMap<>(); - } this.cannedListTypeMap.put(cannedListType, cannedMessagesSpec.cannedMessages); GdiSmartProto.Smart smart = GdiSmartProto.Smart.newBuilder()