From 7e3105adc3a32132d3adbcde0bc46808a6de34e9 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 18 Jan 2015 14:33:44 +0100 Subject: BtPromise: Letting the finally know if the promise was a success or failure. SM: adding a toast callback so the activity can show a toast message. Needed to make sure the toast comes from the correct thread. Can be called from the BT callback thread. --- .../java/io/trygvis/android/bt/BtCallback.java | 2 +- .../main/java/io/trygvis/android/bt/BtDevice.java | 4 +-- .../main/java/io/trygvis/android/bt/BtPromise.java | 29 +++++++++++----------- 3 files changed, 18 insertions(+), 17 deletions(-) (limited to 'app/src/main/java/io/trygvis/android') diff --git a/app/src/main/java/io/trygvis/android/bt/BtCallback.java b/app/src/main/java/io/trygvis/android/bt/BtCallback.java index 3b1fb10..0f7287f 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtCallback.java +++ b/app/src/main/java/io/trygvis/android/bt/BtCallback.java @@ -59,7 +59,7 @@ public class BtCallback { throw new NotOverriddenException(); } - public void onFinally() { + public void onFinally(boolean success) { throw new NotOverriddenException(); } diff --git a/app/src/main/java/io/trygvis/android/bt/BtDevice.java b/app/src/main/java/io/trygvis/android/bt/BtDevice.java index 23fd397..fd9e2b9 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtDevice.java +++ b/app/src/main/java/io/trygvis/android/bt/BtDevice.java @@ -139,8 +139,8 @@ public class BtDevice { } callback = newPromise. - onFinally(() -> { - Log.i(TAG, "Promise done, device is available again: address=" + address); + onFinally(success -> { + Log.i(TAG, "Promise done, device is available again: address=" + address + ", success=" + success); callback = null; }). asCallback(bluetoothDevice.getAddress()); 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 91c0359..edb2894 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtPromise.java +++ b/app/src/main/java/io/trygvis/android/bt/BtPromise.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import io.trygvis.android.Consumer; import io.trygvis.android.F2; import io.trygvis.android.F3; import io.trygvis.android.Function; @@ -207,11 +208,11 @@ public class BtPromise { // return this; // } - public synchronized BtPromise onFinally(Runnable callback) { + public synchronized BtPromise onFinally(Consumer callback) { finallyQ.add(new BtCallback(stopOnFailure(), "finally") { @Override - public void onFinally() { - callback.run(); + public void onFinally(boolean success) { + callback.accept(success); } }); return this; @@ -259,7 +260,7 @@ public class BtPromise { private static PromiseResult callCallback(EventType key, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, BluetoothGattDescriptor descriptor, int status, int newState, - BtCallback btCallback) { + Boolean success, BtCallback btCallback) { switch (key) { case onConnectionStateChange: return btCallback.onConnectionStateChange(gatt, status, newState); @@ -284,7 +285,7 @@ public class BtPromise { btCallback.onFailure(); return null; case onFinally: - btCallback.onFinally(); + btCallback.onFinally(success); return null; default: Log.w(TAG, "Unknown callback: " + key); @@ -337,18 +338,18 @@ public class BtPromise { } } - Log.i(TAG, "Graceful sleep" + btCallback.name); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // ignore - } +// Log.i(TAG, "Graceful sleep" + btCallback.name); +// try { +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// // ignore +// } } try { Log.i(TAG, "Executing bt action: " + btCallback.name); - PromiseResult result = callCallback(key, gatt, characteristic, descriptor, status, newState, btCallback - ); + PromiseResult result = callCallback(key, gatt, characteristic, descriptor, status, newState, null, + btCallback); if (result instanceof Stop) { Log.i(TAG, "The chain want to stop."); @@ -463,7 +464,7 @@ public class BtPromise { for (BtCallback callback : finallyQ) { try { - callCallback(onFinally, null, null, null, 0, 0, callback); + callCallback(onFinally, null, null, null, 0, 0, success, callback); } catch (NotOverriddenException e) { return; } -- cgit v1.2.3