Avoid NPEs when aborting an erroneous sync #205

here
cpfeiffer 2016-01-07 00:33:20 +01:00
parent a6d3c50f94
commit ae5417b9cc
4 changed files with 23 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import java.io.IOException;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations.OperationStatus;
/**
* Abstract base class for a BTLEOperation, i.e. an operation that does more than
@ -25,6 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
*/
public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport> implements GattCallback, BTLEOperation {
private final T mSupport;
protected OperationStatus operationStatus = OperationStatus.INITIAL;
protected AbstractBTLEOperation(T support) {
mSupport = support;
@ -38,7 +40,9 @@ public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport>
*/
@Override
public final void perform() throws IOException {
operationStatus = OperationStatus.STARTED;
prePerform();
operationStatus = OperationStatus.RUNNING;
doPerform();
}
@ -101,6 +105,10 @@ public abstract class AbstractBTLEOperation<T extends AbstractBTLEDeviceSupport>
getDevice().sendDeviceUpdateIntent(getContext());
}
public boolean isOperationRunning() {
return operationStatus == OperationStatus.RUNNING;
}
public T getSupport() {
return mSupport;
}

View File

@ -25,6 +25,7 @@ public abstract class AbstractMiBandOperation extends AbstractBTLEOperation<MiBa
@Override
protected void operationFinished() {
operationStatus = OperationStatus.FINISHED;
if (getDevice() != null && getDevice().isConnected()) {
try {
TransactionBuilder builder = performInitialized("reenabling disabled notifications");

View File

@ -174,6 +174,12 @@ public class FetchActivityOperation extends AbstractMiBandOperation {
* @see #bufferActivityData(byte[])
*/
private void handleActivityNotif(byte[] value) {
if (!isOperationRunning()) {
LOG.error("ignoring activity data notification because operation is not running. Data length: " + value.length);
getSupport().logMessageContent(value);
return;
}
if (value.length == activityMetadataLength) {
handleActivityMetadata(value);
} else {

View File

@ -0,0 +1,8 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations;
public enum OperationStatus {
INITIAL,
STARTED,
RUNNING,
FINISHED
}