From 7aa7de6463543813f13174a15551c2f08b9e39d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Thu, 21 Nov 2024 22:52:01 +0000 Subject: [PATCH] Colmi R0x: Remove all background tasks on dispose Somehow, the tasks were not being fully removed on dispose, which would result in a crash. I am not able to reproduce the issue reliably, but removing all the background tasks on dispose should fix the issue. --- .../devices/colmi/ColmiR0xDeviceSupport.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java index 4ba79c24e..1ef773e99 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/colmi/ColmiR0xDeviceSupport.java @@ -64,8 +64,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { private static final Logger LOG = LoggerFactory.getLogger(ColmiR0xDeviceSupport.class); - Handler backgroundTasksHandler = new Handler(Looper.getMainLooper()); - Runnable backgroundTask; + private final Handler backgroundTasksHandler = new Handler(Looper.getMainLooper()); private final DeviceInfoProfile deviceInfoProfile; private String cachedFirmwareVersion = null; @@ -97,9 +96,7 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { @Override public void dispose() { - if (backgroundTasksHandler != null) { - backgroundTasksHandler.removeCallbacks(backgroundTask); - } + backgroundTasksHandler.removeCallbacksAndMessages(null); super.dispose(); } @@ -121,7 +118,7 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { } private void handleDeviceInfo(DeviceInfo info) { - LOG.debug("Device info: " + info); + LOG.debug("Device info: {}", info); GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo(); versionCmd.hwVersion = info.getHardwareRevision(); @@ -149,13 +146,8 @@ public class ColmiR0xDeviceSupport extends AbstractBTLEDeviceSupport { builder.notify(getCharacteristic(ColmiR0xConstants.CHARACTERISTIC_NOTIFY_V2), true); // Delay initialization with 2 seconds to give the ring time to settle - backgroundTask = new Runnable() { - @Override - public void run() { - postConnectInitialization(); - } - }; - backgroundTasksHandler.postDelayed(backgroundTask, 2000); + backgroundTasksHandler.removeCallbacksAndMessages(null); + backgroundTasksHandler.postDelayed(this::postConnectInitialization, 2000); return builder; }