From 22e90743aabfd181035776b84e42e99e64729555 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 2 May 2018 21:24:06 +0200 Subject: [PATCH 1/3] Fix #939. --- .../Overview/Dialogs/BolusProgressDialog.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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 fbd4e0ece7..192746f54b 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 @@ -70,16 +70,25 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL @Override public void onResume() { super.onResume(); - if (getDialog() != null) - getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - MainApp.bus().register(this); - running = true; - if (bolusEnded) dismiss(); + if (bolusEnded) { + dismiss(); + } else { + if (getDialog() != null) + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + MainApp.bus().register(this); + running = true; + } } @Override public void dismiss() { - super.dismiss(); + try { + super.dismiss(); + } catch (IllegalStateException e) { + // dialog not running yet. onResume will try again. Set bolusEnded to make extra + // sure onResume will catch this + bolusEnded = true; + } if (helperActivity != null) { helperActivity.finish(); } From 3b19db14fd5af1d7ac1ace2502dd28594bce1a0c Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 2 May 2018 21:25:04 +0200 Subject: [PATCH 2/3] Clean up. --- .../Overview/Dialogs/BolusProgressDialog.java | 69 +++++++------------ 1 file changed, 25 insertions(+), 44 deletions(-) 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 192746f54b..a47cc8800e 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 @@ -42,7 +42,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL } public void setInsulin(double amount) { - this.amount = amount; + BolusProgressDialog.amount = amount; bolusEnded = false; } @@ -55,10 +55,10 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL Bundle savedInstanceState) { getDialog().setTitle(String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount)); View view = inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false); - stopButton = (Button) view.findViewById(R.id.overview_bolusprogress_stop); - statusView = (TextView) view.findViewById(R.id.overview_bolusprogress_status); - stopPressedView = (TextView) view.findViewById(R.id.overview_bolusprogress_stoppressed); - progressBar = (ProgressBar) view.findViewById(R.id.overview_bolusprogress_progressbar); + stopButton = view.findViewById(R.id.overview_bolusprogress_stop); + statusView = view.findViewById(R.id.overview_bolusprogress_status); + stopPressedView = view.findViewById(R.id.overview_bolusprogress_stoppressed); + progressBar = view.findViewById(R.id.overview_bolusprogress_progressbar); stopButton.setOnClickListener(this); progressBar.setMax(100); statusView.setText(MainApp.gs(R.string.waitingforpump)); @@ -118,16 +118,13 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL public void onStatusEvent(final EventOverviewBolusProgress ev) { Activity activity = getActivity(); if (activity != null) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - log.debug("Status: " + ev.status + " Percent: " + ev.percent); - statusView.setText(ev.status); - progressBar.setProgress(ev.percent); - if (ev.percent == 100) { - stopButton.setVisibility(View.INVISIBLE); - scheduleDismiss(); - } + activity.runOnUiThread(() -> { + log.debug("Status: " + ev.status + " Percent: " + ev.percent); + statusView.setText(ev.status); + progressBar.setProgress(ev.percent); + if (ev.percent == 100) { + stopButton.setVisibility(View.INVISIBLE); + scheduleDismiss(); } }); } @@ -142,41 +139,25 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL @Subscribe public void onStatusEvent(final EventPumpStatusChanged c) { - Activity activity = getActivity(); if (activity != null) { - activity.runOnUiThread( - new Runnable() { - @Override - public void run() { - statusView.setText(c.textStatus()); - } - } - ); + activity.runOnUiThread(() -> statusView.setText(c.textStatus())); } - } private void scheduleDismiss() { - Thread t = new Thread(new Runnable() { - @Override - public void run() { - SystemClock.sleep(5000); - BolusProgressDialog.bolusEnded = true; - Activity activity = getActivity(); - if (activity != null) { - activity.runOnUiThread( - new Runnable() { - @Override - public void run() { - try { - dismiss(); - } catch (Exception e) { - log.error("Unhandled exception", e); - } - } - }); - } + Thread t = new Thread(() -> { + SystemClock.sleep(5000); + BolusProgressDialog.bolusEnded = true; + Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread(() -> { + try { + dismiss(); + } catch (Exception e) { + log.error("Unhandled exception", e); + } + }); } }); t.start(); From 79458a1c736040c34d6ea83a25cc056b9dddf999 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Wed, 2 May 2018 22:36:52 +0200 Subject: [PATCH 3/3] 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; }