diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-18 14:33:44 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-18 14:33:44 +0100 |
commit | 7e3105adc3a32132d3adbcde0bc46808a6de34e9 (patch) | |
tree | a839457308e1ee630c6879f8a032661db043876f /app/src | |
parent | fe238450f161a503d61c5ae59ecdd82c60c0e9ec (diff) | |
download | io.trygvis.soilmoisture-android-7e3105adc3a32132d3adbcde0bc46808a6de34e9.tar.gz io.trygvis.soilmoisture-android-7e3105adc3a32132d3adbcde0bc46808a6de34e9.tar.bz2 io.trygvis.soilmoisture-android-7e3105adc3a32132d3adbcde0bc46808a6de34e9.tar.xz io.trygvis.soilmoisture-android-7e3105adc3a32132d3adbcde0bc46808a6de34e9.zip |
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.
Diffstat (limited to 'app/src')
6 files changed, 46 insertions, 21 deletions
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<A> { } 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<Boolean> 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; } diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java index 25dac5c..86a1917 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java @@ -169,7 +169,7 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS return stop(); }). - onFinally(() -> { + onFinally(success -> { if (smDevice.getIsUseful() == null) { smDevice.setIsUseful(false); } @@ -344,9 +344,10 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS return stop(); }). - onFinally(() -> { - Toast.makeText(context, R.string.error_could_not_read_value, Toast.LENGTH_SHORT). - show(); + onFinally(success -> { + if (!success) { + sendBroadcast(createToast(R.string.error_could_not_read_value, Toast.LENGTH_SHORT)); + } }); sensor.getDevice().getBtDevice().withConnection(promise); @@ -356,6 +357,13 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS // Event creation and dispatching // ----------------------------------------------------------------------- + private Intent createToast(int id, int length) { + return new Intent(SoilMoistureListener.INTENT_NAME). + putExtra("event", "toast"). + putExtra("id", id). + putExtra("length", length); + } + private Intent createReady(boolean success) { return new Intent(SoilMoistureListener.INTENT_NAME). putExtra("event", "ready"). @@ -389,6 +397,11 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS String event = intent.getStringExtra("event"); Log.i(TAG, "Dispatching event " + intent.getAction() + "/" + event); switch (event) { + case "toast": + listener.onToast( + intent.getIntExtra("id", 0), + intent.getIntExtra("length", 0)); + break; case "ready": listener.onReady( intent.getBooleanExtra("success", false)); diff --git a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java index c6293c6..79b09c4 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java +++ b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java @@ -584,6 +584,14 @@ public class MainActivity extends ListActivity { private class MySoilMoistureListener extends SoilMoistureListener { @Override + public void onToast(int id, int length) { + CharSequence text = getText(id); + + Log.i(TAG, "Toast: " + text); + Toast.makeText(context, text, length).show(); + } + + @Override public void onReady(boolean ok) { if (!ok) { Toast.makeText(context, diff --git a/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java index 5e3ddbb..b7f2060 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java @@ -35,6 +35,9 @@ public interface SoilMoistureService { DefaultSoilMoistureService.dispatchEvent(intent, this); } + public void onToast(int id, int length) { + } + public void onReady(boolean ok) { } |