aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/bt/BtPromise.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/android/bt/BtPromise.java')
-rw-r--r--app/src/main/java/io/trygvis/android/bt/BtPromise.java116
1 files changed, 64 insertions, 52 deletions
diff --git a/app/src/main/java/io/trygvis/android/bt/BtPromise.java b/app/src/main/java/io/trygvis/android/bt/BtPromise.java
index 6af62c0..d4b3b67 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtPromise.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtPromise.java
@@ -6,12 +6,9 @@ import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.util.Log;
-import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import java.util.ListIterator;
-import java.util.Queue;
import io.trygvis.android.F2;
import io.trygvis.android.F3;
@@ -32,8 +29,8 @@ import static io.trygvis.android.bt.BtPromise.EventType.onServicesDiscovered;
public class BtPromise {
private final static String TAG = BtPromise.class.getSimpleName();
private final List<BtCallback> actionQ = new ArrayList<>();
- private final Queue<BtCallback> failureQ = new ArrayDeque<>();
- private final Queue<BtCallback> finallyQ = new ArrayDeque<>();
+ private final List<BtCallback> failureQ = new ArrayList<>();
+ private final List<BtCallback> finallyQ = new ArrayList<>();
private static final PromiseResult waitForNextEvent = new WaitForNextEvent();
private static final PromiseResult stop = new Stop();
@@ -84,7 +81,7 @@ public class BtPromise {
}
private boolean stopOnFailure() {
- if(stopOnFailure != null) {
+ if (stopOnFailure != null) {
boolean b = stopOnFailure;
stopOnFailure = null;
return b;
@@ -96,12 +93,12 @@ public class BtPromise {
stopOnFailure = true;
return this;
}
-
+
public synchronized BtPromise onConnectionStateChange(F3<BluetoothGatt, Integer, Integer, PromiseResult> callback) {
actionQ.add(new BtCallback(stopOnFailure(), "onConnectionStateChange") {
@Override
public PromiseResult onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
- return callback.apply(gatt, status,newState);
+ return callback.apply(gatt, status, newState);
}
});
return this;
@@ -296,17 +293,17 @@ public class BtPromise {
private final String address;
private List<BtCallback> actionQ;
- private ListIterator<BtCallback> it;
- private ArrayDeque<BtCallback> failureQ;
- ArrayDeque<BtCallback> finallyQ;
+ private int currentAction = 0;
+ private List<BtCallback> failureQ;
+ private List<BtCallback> finallyQ;
private List<String> events = new ArrayList<>();
- private BtBluetoothGattCallback(String address, List<BtCallback> actionQ, Queue<BtCallback> failureQ, Queue<BtCallback> finallyQ) {
+ private BtBluetoothGattCallback(String address, List<BtCallback> actionQ,
+ List<BtCallback> failureQ, List<BtCallback> finallyQ) {
this.address = address;
this.actionQ = new ArrayList<>(actionQ);
- this.failureQ = new ArrayDeque<>(failureQ);
- this.finallyQ = new ArrayDeque<>(finallyQ);
- this.it = actionQ.listIterator();
+ this.failureQ = new ArrayList<>(failureQ);
+ this.finallyQ = new ArrayList<>(finallyQ);
}
void onEvent(EventType key, String values, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
@@ -316,22 +313,28 @@ public class BtPromise {
Log.i(TAG, "event: " + key + "(" + values + "), success=" + success);
- if (!success) {
- doFailure();
- return;
- }
-
BtCallback btCallback;
synchronized (this) {
- if (!it.hasNext()) {
- Log.d(TAG, "All Bluetooth actions are done");
+ if (!hasNext()) {
+ Log.d(TAG, "All Bluetooth actions are done, no handler for last event.");
+ showEvents();
doFinally();
return;
}
- btCallback = it.next();
- Log.i(TAG, "Executing bt action: " + btCallback.name);
+ btCallback = actionQ.get(currentAction++);
+
+ if (!success) {
+ if (btCallback.stopOnFailure) {
+ doFailure();
+ return;
+ } else {
+ Log.i(TAG, "Last status was a failure, but the callback still want it.");
+ }
+ }
+
+ Log.i(TAG, "Graceful sleep" + btCallback.name);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
@@ -340,6 +343,7 @@ public class BtPromise {
}
try {
+ Log.i(TAG, "Executing bt action: " + btCallback.name);
PromiseResult result = callCallback(key, gatt, characteristic, descriptor, status, newState, btCallback,
value);
@@ -347,20 +351,23 @@ public class BtPromise {
Log.i(TAG, "The chain want to stop.");
doFinally();
} else if (result instanceof Detour) {
- Log.i(TAG, "Adding detour");
- BtPromise promise = ((Detour) result).promise;
- events.add("detour " + promise.actionQ.size());
- if (!promise.failureQ.isEmpty()) {
- Log.i(TAG, "Ignoring " + promise.failureQ.size() + " items from the failure Q");
- }
- if (!promise.finallyQ.isEmpty()) {
- Log.i(TAG, "Ignoring " + promise.finallyQ.size() + " items from the finally Q");
+ BtPromise detour = ((Detour) result).promise;
+ Log.i(TAG, "Adding detour with " + detour.actionQ.size() + " actions.");
+ events.add("detour, action size=" + detour.actionQ.size() + ", failure size=" +
+ detour.failureQ.size() + ", finally size=" + detour.finallyQ.size());
+
+ Log.i(TAG, "hasNext(): " + hasNext());
+ Log.i(TAG, "currentAction: " + currentAction);
+ if (hasNext()) {
+ Log.i(TAG, "next action: " + actionQ.get(currentAction).name);
}
- synchronized (this) {
- for (BtCallback cb : promise.actionQ) {
- it.add(cb);
- }
+ actionQ.addAll(currentAction, detour.actionQ);
+ failureQ.addAll(detour.failureQ);
+ finallyQ.addAll(detour.finallyQ);
+ Log.i(TAG, "hasNext(): " + hasNext());
+ if (hasNext()) {
+ Log.i(TAG, "next action: " + actionQ.get(currentAction).name);
}
} else if (result instanceof ContinueDirectly) {
value = ((ContinueDirectly) result).value;
@@ -369,8 +376,9 @@ public class BtPromise {
return;
}
- if (!it.hasNext()) {
+ if (!hasNext()) {
Log.i(TAG, "The queue is empty");
+ showEvents();
}
} catch (NotOverriddenException e) {
Log.w(TAG, "Unexpected callback by listener: " + key);
@@ -378,33 +386,37 @@ public class BtPromise {
}
}
+ private boolean hasNext() {
+ return currentAction < actionQ.size();
+ }
+
private void doFailure() {
+ showEvents();
+
+ Log.w(TAG, "Executing " + failureQ.size() + " failure handlers");
+
+ for (BtCallback callback : failureQ) {
+ callCallback(onFailure, null, null, null, 0, 0, callback, null);
+ }
+
+ doFinally();
+ }
+
+ private void showEvents() {
StringBuilder msg = new StringBuilder();
msg.append("Address: ").append(address).append("\n");
- msg.append("Expected events: \n");
+ msg.append("Event handlers: \n");
for (BtCallback cb : actionQ) {
msg.append("- ").append(cb.name).append("\n");
}
- msg.append("Actual events: \n");
+ msg.append("Events received: \n");
for (String event : events) {
msg.append("- ").append(event).append("\n");
}
Log.w(TAG, msg.toString());
-
- Log.w(TAG, "Executing " + failureQ.size() + " failure handlers");
-
- for (BtCallback callback = failureQ.poll(); callback != null; callback = failureQ.poll()) {
- try {
- callCallback(onFailure, null, null, null, 0, 0, callback, null);
- } catch (NotOverriddenException e) {
- return;
- }
- }
-
- doFinally();
}
private void doFinally() {
@@ -412,7 +424,7 @@ public class BtPromise {
Log.w(TAG, "Executing " + finallyQ.size() + " finally handlers");
- for (BtCallback callback = finallyQ.poll(); callback != null; callback = finallyQ.poll()) {
+ for (BtCallback callback : finallyQ) {
try {
callCallback(onFinally, null, null, null, 0, 0, callback, null);
} catch (NotOverriddenException e) {