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.java89
1 files changed, 55 insertions, 34 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 d4b3b67..c569ec2 100644
--- a/app/src/main/java/io/trygvis/android/bt/BtPromise.java
+++ b/app/src/main/java/io/trygvis/android/bt/BtPromise.java
@@ -21,7 +21,6 @@ import static io.trygvis.android.bt.BtPromise.EventType.onConnectionStateChange;
import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorRead;
import static io.trygvis.android.bt.BtPromise.EventType.onDescriptorWrite;
import static io.trygvis.android.bt.BtPromise.EventType.onDirect;
-import static io.trygvis.android.bt.BtPromise.EventType.onFailure;
import static io.trygvis.android.bt.BtPromise.EventType.onFinally;
import static io.trygvis.android.bt.BtPromise.EventType.onReliableWriteCompleted;
import static io.trygvis.android.bt.BtPromise.EventType.onServicesDiscovered;
@@ -29,7 +28,7 @@ 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 List<BtCallback> failureQ = new ArrayList<>();
+ // private final List<BtCallback> failureQ = new ArrayList<>();
private final List<BtCallback> finallyQ = new ArrayList<>();
private static final PromiseResult waitForNextEvent = new WaitForNextEvent();
@@ -194,15 +193,15 @@ public class BtPromise {
return this;
}
- public synchronized BtPromise onFailure(Runnable callback) {
- failureQ.add(new BtCallback(stopOnFailure(), "onFailure") {
- @Override
- public void onFailure() {
- callback.run();
- }
- });
- return this;
- }
+// public synchronized BtPromise onFailure(Runnable callback) {
+// failureQ.add(new BtCallback(stopOnFailure(), "onFailure") {
+// @Override
+// public void onFailure() {
+// callback.run();
+// }
+// });
+// return this;
+// }
public synchronized BtPromise onFinally(Runnable callback) {
finallyQ.add(new BtCallback(stopOnFailure(), "finally") {
@@ -233,7 +232,7 @@ public class BtPromise {
}
BtBluetoothGattCallback asCallback(String address) {
- return new BtBluetoothGattCallback(address, actionQ, failureQ, finallyQ);
+ return new BtBluetoothGattCallback(address, actionQ, /*failureQ, */finallyQ);
}
enum EventType {
@@ -294,15 +293,15 @@ public class BtPromise {
private final String address;
private List<BtCallback> actionQ;
private int currentAction = 0;
- private List<BtCallback> failureQ;
+ // private List<BtCallback> failureQ;
private List<BtCallback> finallyQ;
private List<String> events = new ArrayList<>();
private BtBluetoothGattCallback(String address, List<BtCallback> actionQ,
- List<BtCallback> failureQ, List<BtCallback> finallyQ) {
+ /*List<BtCallback> failureQ, */List<BtCallback> finallyQ) {
this.address = address;
this.actionQ = new ArrayList<>(actionQ);
- this.failureQ = new ArrayList<>(failureQ);
+// this.failureQ = new ArrayList<>(failureQ);
this.finallyQ = new ArrayList<>(finallyQ);
}
@@ -318,7 +317,6 @@ public class BtPromise {
if (!hasNext()) {
Log.d(TAG, "All Bluetooth actions are done, no handler for last event.");
- showEvents();
doFinally();
return;
}
@@ -327,7 +325,8 @@ public class BtPromise {
if (!success) {
if (btCallback.stopOnFailure) {
- doFailure();
+// doFailure();
+ doFinally();
return;
} else {
Log.i(TAG, "Last status was a failure, but the callback still want it.");
@@ -350,11 +349,15 @@ public class BtPromise {
if (result instanceof Stop) {
Log.i(TAG, "The chain want to stop.");
doFinally();
- } else if (result instanceof Detour) {
+ return;
+ }
+
+ if (result instanceof Detour) {
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());
+ 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);
@@ -363,13 +366,17 @@ public class BtPromise {
}
actionQ.addAll(currentAction, detour.actionQ);
- failureQ.addAll(detour.failureQ);
+// 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) {
+
+ result = PromiseResult.continueDirectly(gatt);
+ }
+
+ if (result instanceof ContinueDirectly) {
value = ((ContinueDirectly) result).value;
onEvent(onDirect, "value=" + value, null, null, null, BluetoothGatt.GATT_SUCCESS, 0,
value);
@@ -382,7 +389,8 @@ public class BtPromise {
}
} catch (NotOverriddenException e) {
Log.w(TAG, "Unexpected callback by listener: " + key);
- doFailure();
+// doFailure();
+ doFinally();
}
}
@@ -390,27 +398,38 @@ public class BtPromise {
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 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("Event handlers: \n");
for (BtCallback cb : actionQ) {
msg.append("- ").append(cb.name).append("\n");
}
+// msg.append("Failure handlers: \n");
+// for (BtCallback cb : failureQ) {
+// msg.append("- ").append(cb.name).append("\n");
+// }
+
+ msg.append("Finally handlers: \n");
+ for (BtCallback cb : finallyQ) {
+ msg.append("- ").append(cb.name).append("\n");
+ }
+
msg.append("Events received: \n");
for (String event : events) {
msg.append("- ").append(event).append("\n");
@@ -420,6 +439,8 @@ public class BtPromise {
}
private void doFinally() {
+ showEvents();
+
actionQ.clear();
Log.w(TAG, "Executing " + finallyQ.size() + " finally handlers");