From aa60ce4b562cdec6fc5cd1b6c936f5cf6925bf2e Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 6 Dec 2015 23:39:41 +0100 Subject: [PATCH] Disable some notifications also for update-firmware operation (introduce a common superclass to do that) --- .../service/btle/AbstractBTLEOperation.java | 36 +++++++++++++ .../operations/AbstractMiBandOperation.java | 50 +++++++++++++++++++ .../operations/FetchActivityOperation.java | 16 ++---- .../operations/UpdateFirmwareOperation.java | 12 ++--- 4 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/AbstractMiBandOperation.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java index 165a72cb..746342bf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java @@ -30,6 +30,42 @@ public abstract class AbstractBTLEOperation mSupport = support; } + /** + * Performs this operation. The whole operation is asynchronous, i.e. + * this method quickly returns before the actual operation is finished. + * Calls #prePerform() and, if successful, #doPerform(). + * @throws IOException + */ + @Override + public final void perform() throws IOException { + prePerform(); + doPerform(); + } + + /** + * Hook for subclasses to perform something before #doPerform() is invoked. + * @throws IOException + */ + protected void prePerform() throws IOException { + } + + /** + * Subclasses must implement this. When invoked, #prePerform() returned + * successfully. + * Note that subclasses HAVE TO call #operationFinished() when the entire + * opreation is done (successful or not). + * @throws IOException + */ + protected abstract void doPerform() throws IOException; + + /** + * You MUST call this method when the operation has finished, either + * successfull or unsuccessfully. + * @throws IOException + */ + protected void operationFinished() throws IOException { + } + /** * Delegates to the DeviceSupport instance and additionally sets this instance as the Gatt * callback for the transaction. diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/AbstractMiBandOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/AbstractMiBandOperation.java new file mode 100644 index 00000000..729e7fa5 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/AbstractMiBandOperation.java @@ -0,0 +1,50 @@ +package nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations; + +import android.widget.Toast; + +import java.io.IOException; + +import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService; +import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation; +import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; +import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport; +import nodomain.freeyourgadget.gadgetbridge.util.GB; + +public abstract class AbstractMiBandOperation extends AbstractBTLEOperation { + protected AbstractMiBandOperation(MiBandSupport support) { + super(support); + } + + @Override + protected void prePerform() throws IOException { + super.prePerform(); + TransactionBuilder builder = performInitialized("disabling some notifications"); + enableOtherNotifications(builder, false); + builder.queue(getQueue()); + } + + @Override + protected void operationFinished() { + if (getDevice() != null && getDevice().isConnected()) { + try { + TransactionBuilder builder = performInitialized("reenabling disabled notifications"); + enableOtherNotifications(builder, true); + builder.queue(getQueue()); + } catch (IOException ex) { + GB.toast(getContext(), "Error enabling Mi Band notifications, you may need to connect and disconnect", Toast.LENGTH_LONG, GB.ERROR, ex); + } + } + } + + /** + * Enables or disables certain kinds of notifications that could interfere with this + * operation. Call this method once initially to disable other notifications, and once + * when this operation has finished. + * @param builder + * @param enable true to enable, false to disable the other notifications + */ + private void enableOtherNotifications(TransactionBuilder builder, boolean enable) { + builder.notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_REALTIME_STEPS), enable) + .notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_SENSOR_DATA), enable); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java index 4bd2fcfd..14f92119 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java @@ -42,7 +42,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; * An operation that fetches activity data. For every fetch, a new operation must * be created, i.e. an operation may not be reused for multiple fetches. */ -public class FetchActivityOperation extends AbstractBTLEOperation { +public class FetchActivityOperation extends AbstractMiBandOperation { private static final Logger LOG = LoggerFactory.getLogger(FetchActivityOperation.class); private static final byte[] fetch = new byte[]{MiBandService.COMMAND_FETCH_DATA}; @@ -134,24 +134,16 @@ public class FetchActivityOperation extends AbstractBTLEOperation } @Override - public void perform() throws IOException { + protected void doPerform() throws IOException { // scheduleTaskExecutor = Executors.newScheduledThreadPool(1); TransactionBuilder builder = performInitialized("fetch activity data"); // builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_LE_PARAMS), getLowLatency()); - - enableOtherNotifications(builder, false); builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext())); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), fetch); builder.queue(getQueue()); } - private void enableOtherNotifications(TransactionBuilder builder, boolean enable) { - builder.notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_REALTIME_STEPS), enable) - .notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_SENSOR_DATA), enable); - } - - @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { @@ -166,9 +158,7 @@ public class FetchActivityOperation extends AbstractBTLEOperation private void handleActivityFetchFinish() throws IOException { LOG.info("Fetching activity data has finished."); activityStruct = null; - TransactionBuilder builder = performInitialized("enabling other notifications again"); - enableOtherNotifications(builder, true); - builder.queue(getQueue()); + operationFinished(); unsetBusy(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/UpdateFirmwareOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/UpdateFirmwareOperation.java index 029daff6..e4ccf4fe 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/UpdateFirmwareOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/UpdateFirmwareOperation.java @@ -23,7 +23,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport import nodomain.freeyourgadget.gadgetbridge.util.CheckSums; import nodomain.freeyourgadget.gadgetbridge.util.GB; -public class UpdateFirmwareOperation extends AbstractBTLEOperation { +public class UpdateFirmwareOperation extends AbstractMiBandOperation { private static final Logger LOG = LoggerFactory.getLogger(UpdateFirmwareOperation.class); private final Uri uri; @@ -37,7 +37,7 @@ public class UpdateFirmwareOperation extends AbstractBTLEOperation