From 5c57392b8561f719565e0c5a6d546e0d50075690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Mon, 15 Apr 2024 20:59:53 +0200 Subject: [PATCH] Garmin: Fetch activity on demand --- .../devices/garmin/GarminCoordinator.java | 5 +++ .../service/devices/garmin/GarminSupport.java | 40 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 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 6b6cc0bc9..6fd275c7b 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 @@ -42,6 +42,11 @@ public abstract class GarminCoordinator extends AbstractBLEDeviceCoordinator { return deviceSpecificSettings; } + @Override + public boolean supportsActivityDataFetching() { + return true; + } + @Override public boolean supportsFindDevice() { return true; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java index e37c02095..c24ad43ae 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/GarminSupport.java @@ -23,6 +23,7 @@ import java.util.Timer; import java.util.TimerTask; import java.util.UUID; +import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; @@ -55,6 +56,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.SetD import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.SupportedFileTypesMessage; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.SystemEventMessage; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; +import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; @@ -68,7 +70,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni private ICommunicator communicator; private MusicStateSpec musicStateSpec; private Timer musicStateTimer; - private List supportedFileTypeList; + private final List supportedFileTypeList = new ArrayList<>(); public GarminSupport() { super(LOG); @@ -218,13 +220,25 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni notificationsHandler.setEnabled(enable); LOG.info("NOTIFICATIONS ARE NOW {}", enable ? "ON" : "OFF"); } else if (deviceEvent instanceof SupportedFileTypesDeviceEvent) { - this.supportedFileTypeList = ((SupportedFileTypesDeviceEvent) deviceEvent).getSupportedFileTypes(); - sendOutgoingMessage(fileTransferHandler.initiateDownload()); + this.supportedFileTypeList.clear(); + this.supportedFileTypeList.addAll(((SupportedFileTypesDeviceEvent) deviceEvent).getSupportedFileTypes()); } super.evaluateGBDeviceEvent(deviceEvent); } + @Override + public void onFetchRecordedData(final int dataTypes) { + if (this.supportedFileTypeList.isEmpty()) { + LOG.warn("No known supported file types"); + return; + } + + // FIXME respect dataTypes? + + sendOutgoingMessage(fileTransferHandler.initiateDownload()); + } + @Override public void onNotification(NotificationSpec notificationSpec) { sendOutgoingMessage(notificationsHandler.onNotification(notificationSpec)); @@ -346,6 +360,12 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni private void processDownloadQueue() { if (!filesToDownload.isEmpty() && !fileTransferHandler.isDownloading()) { + if (!gbDevice.isBusy()) { + GB.updateTransferNotification(getContext().getString(R.string.busy_task_fetch_activity_data), "", true, 0, getContext()); + getDevice().setBusyTask(getContext().getString(R.string.busy_task_fetch_activity_data)); + getDevice().sendDeviceUpdateIntent(getContext()); + } + try { FileTransferHandler.DirectoryEntry directoryEntry = filesToDownload.remove(); while (checkFileExists(directoryEntry.getFileName())) { @@ -359,7 +379,19 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni LOG.debug("File: {} already downloaded, not downloading again, from inside.", directoryEntry.getFileName()); } } catch (NoSuchElementException e) { - //ignore + // we ran out of files to download + // FIXME this is ugly + if (gbDevice.isBusy() && gbDevice.getBusyTask().equals(getContext().getString(R.string.busy_task_fetch_activity_data))) { + getDevice().unsetBusyTask(); + GB.updateTransferNotification(null, "", false, 100, getContext()); + getDevice().sendDeviceUpdateIntent(getContext()); + } + } + } else if (filesToDownload.isEmpty() && !fileTransferHandler.isDownloading()) { + if (gbDevice.isBusy() && gbDevice.getBusyTask().equals(getContext().getString(R.string.busy_task_fetch_activity_data))) { + getDevice().unsetBusyTask(); + GB.updateTransferNotification(null, "", false, 100, getContext()); + getDevice().sendDeviceUpdateIntent(getContext()); } } }