aboutsummaryrefslogtreecommitdiff
path: root/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java
diff options
context:
space:
mode:
Diffstat (limited to 'bt/src/main/java/io/trygvis/android/bt/BtSequencer.java')
-rw-r--r--bt/src/main/java/io/trygvis/android/bt/BtSequencer.java69
1 files changed, 48 insertions, 21 deletions
diff --git a/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java b/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java
index 7eecbed..f0863ae 100644
--- a/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java
+++ b/bt/src/main/java/io/trygvis/android/bt/BtSequencer.java
@@ -3,13 +3,16 @@ package io.trygvis.android.bt;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
-import android.util.Log;
+import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+
+import io.trygvis.android.Consumer;
import static io.trygvis.android.bt.BtSequence.ContinueDirectly;
import static io.trygvis.android.bt.BtSequence.Detour;
@@ -20,7 +23,7 @@ import static io.trygvis.android.bt.BtSequencer.EventType.onDirect;
import static io.trygvis.android.bt.BtSequencer.EventType.onFinally;
class BtSequencer {
- private final static String TAG = BtSequencer.class.getSimpleName();
+ public final static String TAG = BtSequencer.class.getSimpleName();
enum EventType {
onConnectionStateChange,
@@ -39,6 +42,14 @@ class BtSequencer {
onFinally,
}
+ public static Consumer<String> d = System.out::println;
+ public static Consumer<String> i = System.out::println;
+ public static Consumer<String> w = System.out::println;
+ public static Consumer<Map.Entry<String, Exception>> we = pair -> {
+ System.out.println(pair.getKey());
+ pair.getValue().printStackTrace(System.out);
+ };
+
private final String address;
private final Deque<BtSequence> sequences = new ArrayDeque<>();
private final Deque<Iterator<BtCallback>> iterators = new ArrayDeque<>();
@@ -69,8 +80,8 @@ class BtSequencer {
this.callbacks = sequence.actionQ();
}
- public void onDirect() {
- onEvent(onDirect, "initial onDirect", null, null, null, BluetoothGatt.GATT_SUCCESS,
+ public void onDirect(BluetoothGatt gatt) {
+ onEvent(onDirect, "initial onDirect", gatt, null, null, BluetoothGatt.GATT_SUCCESS,
BluetoothGatt.STATE_CONNECTED);
}
@@ -78,19 +89,19 @@ class BtSequencer {
BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor,
int status, int newState) {
if (finallyDone) {
- Log.w(TAG, "Got event after finally has been executed: " + key + "(" + values + ").");
+ w("Got event after finally has been executed: " + key + "(" + values + ").");
return;
}
boolean success = status == BluetoothGatt.GATT_SUCCESS;
events.add(key + "(" + values + "), success=" + success);
- Log.i(TAG, "event: " + key + "(" + values + "), success=" + success);
+ i("event: " + key + "(" + values + "), success=" + success);
BtCallback callback;
synchronized (this) {
if (!callbacks.hasNext() && sequence.getNext().isPresent()) {
- Log.i(TAG, "Switching to next sequence");
+ i("Switching to next sequence");
doFinally(success, sequence);
@@ -100,11 +111,11 @@ class BtSequencer {
if (!callbacks.hasNext()) {
if (!sequences.isEmpty()) {
- Log.d(TAG, "Sequence is done, continuing on previous sequence");
+ d("Sequence is done, continuing on previous sequence");
doFinally(true, sequence);
pop();
} else {
- Log.d(TAG, "Sequence is done, no more sequences");
+ d("Sequence is done, no more sequences");
doFinally(true);
return;
}
@@ -117,31 +128,31 @@ class BtSequencer {
doFinally(false);
return;
} else {
- Log.i(TAG, "Last status was a failure, but the callback still want it.");
+ i("Last status was a failure, but the callback still want it.");
}
}
}
try {
- Log.i(TAG, "Executing bt action: " + callback.type);
+ i("Executing bt action: " + callback.type);
SequenceResult result = callCallback(key, gatt, characteristic, descriptor, status, newState, null,
callback);
if (result instanceof Stop) {
- Log.i(TAG, "The sequence stopped.");
+ i("The sequence stopped.");
doFinally(true);
return;
}
if (result instanceof Fail) {
- Log.i(TAG, "The sequence failed.");
+ i("The sequence failed.");
doFinally(false);
return;
}
if (result instanceof Detour) {
BtSequence detour = ((Detour) result).sequence;
- Log.i(TAG, "Adding detour: " + detour);
+ i("Adding detour: " + detour);
if (!detour.firstIsOnDirect()) {
throw new IllegalArgumentException("The first handler in a detour must be an onDirect.");
@@ -161,20 +172,20 @@ class BtSequencer {
if (!callbacks.hasNext()) {
if (!sequences.isEmpty()) {
- Log.d(TAG, "Sequence is done, continuing on previous sequence");
+ d("Sequence is done, continuing on previous sequence");
doFinally(true, sequence);
pop();
} else {
- Log.d(TAG, "Sequence is done, no more sequences");
+ d("Sequence is done, no more sequences");
doFinally(true);
}
}
} catch (NotOverriddenException e) {
- Log.w(TAG, "Unexpected callback by listener: " + key);
+ w("Unexpected callback by listener: " + key);
// doFailure();
doFinally(false);
} catch (Exception e) {
- Log.w(TAG, "Exception in callback", e);
+ w("Exception in callback", e);
// doFailure();
doFinally(false);
}
@@ -194,7 +205,7 @@ class BtSequencer {
private void doFinally(boolean success, BtSequence s) {
List<BtCallback> q = s.finallyQ();
- Log.w(TAG, "Executing " + q.size() + " finally handlers, success=" + success);
+ w("Executing " + q.size() + " finally handlers, success=" + success);
for (BtCallback callback : q) {
try {
callCallback(onFinally, null, null, null, 0, 0, success, callback);
@@ -224,7 +235,7 @@ class BtSequencer {
msg.append("- ").append(event).append("\n");
}
- Log.w(TAG, msg.toString());
+ w(msg.toString());
}
private static SequenceResult callCallback(EventType key, BluetoothGatt gatt,
@@ -258,8 +269,24 @@ class BtSequencer {
btCallback.onFinally(success);
return null;
default:
- Log.w(TAG, "Unknown callback: " + key);
+ w("Unknown callback: " + key);
return null;
}
}
+
+ public static void d(String msg) {
+ d.accept(msg);
+ }
+
+ public static void i(String msg) {
+ i.accept(msg);
+ }
+
+ public static void w(String msg) {
+ w.accept(msg);
+ }
+
+ public static void w(String msg, Exception e) {
+ we.accept(new AbstractMap.SimpleEntry<>(msg, e));
+ }
}