diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 384fa7f992..b351ea4632 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -78,14 +78,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain private static ArrayList pluginList; - private PowerManager.WakeLock mWakeLock; - private static CommandQueue commandQueue = new CommandQueue(); public ConfigBuilderPlugin() { MainApp.bus().register(this); - PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); - mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "ConfigBuilderPlugin"); } @Override @@ -479,33 +475,13 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - mWakeLock.acquire(); - PumpEnactResult result; - detailedBolusInfo.insulin = applyBolusConstraints(detailedBolusInfo.insulin); - detailedBolusInfo.carbs = applyCarbsConstraints((int) detailedBolusInfo.carbs); - - BolusProgressDialog bolusProgressDialog = null; - if (detailedBolusInfo.context != null) { - bolusProgressDialog = new BolusProgressDialog(); - bolusProgressDialog.setInsulin(detailedBolusInfo.insulin); - bolusProgressDialog.show(((AppCompatActivity) detailedBolusInfo.context).getSupportFragmentManager(), "BolusProgress"); - } else { - Intent i = new Intent(); - i.putExtra("insulin", detailedBolusInfo.insulin); - i.setClass(MainApp.instance(), BolusProgressHelperActivity.class); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - - MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin)); - result = activePump.deliverTreatment(detailedBolusInfo); + PumpEnactResult result = activePump.deliverTreatment(detailedBolusInfo); BolusProgressDialog.bolusEnded = true; MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); - mWakeLock.release(); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index 6545e91406..e8168baaa4 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.queue; +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; import android.text.Html; import android.text.Spanned; @@ -13,6 +15,8 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog; +import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressHelperActivity; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.queue.commands.CommandBolus; import info.nightscout.androidaps.queue.commands.CommandCancelExtendedBolus; @@ -144,6 +148,23 @@ public class CommandQueue { notifyAboutNewCommand(); + // Bring up bolus progress dialog + detailedBolusInfo.insulin = MainApp.getConfigBuilder().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.carbs = MainApp.getConfigBuilder().applyCarbsConstraints((int) detailedBolusInfo.carbs); + + BolusProgressDialog bolusProgressDialog = null; + if (detailedBolusInfo.context != null) { + bolusProgressDialog = new BolusProgressDialog(); + bolusProgressDialog.setInsulin(detailedBolusInfo.insulin); + bolusProgressDialog.show(((AppCompatActivity) detailedBolusInfo.context).getSupportFragmentManager(), "BolusProgress"); + } else { + Intent i = new Intent(); + i.putExtra("insulin", detailedBolusInfo.insulin); + i.setClass(MainApp.instance(), BolusProgressHelperActivity.class); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + return true; } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java index c4892414a9..7dda80a0da 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.queue; +import android.content.Context; +import android.os.PowerManager; import android.os.SystemClock; import org.slf4j.Logger; @@ -8,9 +10,11 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning; import info.nightscout.androidaps.queue.events.EventQueueChanged; /** @@ -24,65 +28,75 @@ public class QueueThread extends Thread { private long connectionStartTime = 0; + private PowerManager.WakeLock mWakeLock; + public QueueThread(CommandQueue queue) { super(QueueThread.class.toString()); this.queue = queue; + PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); + mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "QueueThread"); } @Override public final void run() { + mWakeLock.acquire(); MainApp.bus().post(new EventQueueChanged()); connectionStartTime = System.currentTimeMillis(); PumpInterface pump = ConfigBuilderPlugin.getActivePump(); - while (true) { - log.debug("Looping ..."); - long secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000; - if (pump.isConnecting()) { - log.debug("State: connecting"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed)); - SystemClock.sleep(1000); - continue; - } - - if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) { - log.debug("State: timed out"); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.connectiontimedout))); - pump.stopConnecting(); - queue.clear(); - return; - } - - if (!pump.isConnected()) { - log.debug("State: connect"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed)); - pump.connect("Connection needed"); - SystemClock.sleep(1000); - continue; - } - - if (queue.performing() == null) { - // Pickup 1st command and set performing variable - if (queue.size() > 0) { - log.debug("State: performing"); - queue.pickup(); - MainApp.bus().post(new EventQueueChanged()); - queue.performing().execute(); - queue.resetPerforming(); - MainApp.bus().post(new EventQueueChanged()); - SystemClock.sleep(100); + try { + while (true) { + log.debug("Looping ..."); + long secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000; + if (pump.isConnecting()) { + log.debug("State: connecting"); + MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed)); + SystemClock.sleep(1000); continue; } - } - if (queue.size() == 0 && queue.performing() == null) { - log.debug("State: queue empty. disconnect"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); - pump.disconnect("Queue empty"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); - return; + if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) { + log.debug("State: timed out"); + MainApp.bus().post(new EventDismissBolusprogressIfRunning(new PumpEnactResult())); + MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.connectiontimedout))); + pump.stopConnecting(); + queue.clear(); + return; + } + + if (!pump.isConnected()) { + log.debug("State: connect"); + MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed)); + pump.connect("Connection needed"); + SystemClock.sleep(1000); + continue; + } + + if (queue.performing() == null) { + // Pickup 1st command and set performing variable + if (queue.size() > 0) { + log.debug("State: performing"); + queue.pickup(); + MainApp.bus().post(new EventQueueChanged()); + queue.performing().execute(); + queue.resetPerforming(); + MainApp.bus().post(new EventQueueChanged()); + SystemClock.sleep(100); + continue; + } + } + + if (queue.size() == 0 && queue.performing() == null) { + log.debug("State: queue empty. disconnect"); + MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + pump.disconnect("Queue empty"); + MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); + return; + } } + } finally { + mWakeLock.release(); } }