Disable some notifications also for update-firmware operation

(introduce a common superclass to do that)
here
cpfeiffer 2015-12-06 23:39:41 +01:00
parent 579546c9f8
commit aa60ce4b56
4 changed files with 95 additions and 19 deletions

View File

@ -30,6 +30,42 @@ public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport>
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.

View File

@ -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<MiBandSupport> {
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);
}
}

View File

@ -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<MiBandSupport> {
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<MiBandSupport>
}
@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<MiBandSupport>
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();
}

View File

@ -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<MiBandSupport> {
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<MiBandSupport
}
@Override
public void perform() throws IOException {
protected void doPerform() throws IOException {
if (getSupport().getDeviceInfo().isMili1A()) {
throw new IOException("Firmware update is not supported for the Mi Band 1A, yet.");
}
@ -56,7 +56,8 @@ public class UpdateFirmwareOperation extends AbstractBTLEOperation<MiBandSupport
}
private void done() {
getDevice().unsetBusyTask();
operationFinished();
unsetBusy();
}
@Override
@ -124,9 +125,8 @@ public class UpdateFirmwareOperation extends AbstractBTLEOperation<MiBandSupport
break;
default:
for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
getSupport().logMessageContent(value);
break;
}
}