From 79458a1c736040c34d6ea83a25cc056b9dddf999 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 2 May 2018 22:36:52 +0200 Subject: [PATCH] Ignore harmless errors (un)subscribing to the message bus. --- .../info/nightscout/androidaps/MainApp.java | 17 ++++++++++++++++- .../Overview/Dialogs/BolusProgressDialog.java | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 9e8f3f9c7f..45aff91654 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -245,12 +245,27 @@ public class MainApp extends Application { } } - public void stopKeepAliveService() { if (keepAliveReceiver != null) KeepAliveReceiver.cancelAlarm(this); } + public static void subscribe(Object subscriber) { + try { + bus().register(subscriber); + } catch (IllegalArgumentException e) { + // already registered + } + } + + public static void unsubscribe(Object subscriber) { + try { + bus().unregister(subscriber); + } catch (IllegalArgumentException e) { + // already unregistered + } + } + public static Bus bus() { return sBus; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/BolusProgressDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/BolusProgressDialog.java index a47cc8800e..2ca8e6961a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/BolusProgressDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/BolusProgressDialog.java @@ -75,7 +75,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL } else { if (getDialog() != null) getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - MainApp.bus().register(this); + MainApp.subscribe(this); running = true; } } @@ -97,7 +97,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL @Override public void onPause() { super.onPause(); - MainApp.bus().unregister(this); + MainApp.unsubscribe(this); running = false; }