From a7d3dca752144f92bbb1125d8511b903699b56b7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 4 Aug 2016 09:17:26 +0200 Subject: [PATCH] Bolusing dialog --- .idea/misc.xml | 2 +- app/build.gradle | 6 +- .../androidaps/interfaces/PumpInterface.java | 5 +- .../ConfigBuilder/ConfigBuilderFragment.java | 27 ++- .../plugins/DanaR/DanaRFragment.java | 18 +- .../DanaR/History/DanaRHistoryActivity.java | 1 - .../plugins/DanaR/SerialIOThread.java | 4 + .../DanaR/Services/ExecutionService.java | 50 +++--- .../plugins/DanaR/comm/MsgBolusProgress.java | 3 +- .../plugins/DanaR/comm/MsgBolusStop.java | 7 +- .../plugins/DanaR/comm/MsgOcclusion.java | 5 +- .../DanaR/events/EventDanaRBolusProgress.java | 24 +-- .../DanaR/events/EventDanaRBolusStart.java | 7 + .../plugins/MM640g/MM640gFragment.java | 8 +- .../Overview/Dialogs/BolusProgressDialog.java | 155 ++++++++++++++++++ .../Overview/Dialogs/NewTreatmentDialog.java | 17 +- .../Overview/Dialogs/WizardDialog.java | 7 +- .../plugins/Overview/OverviewFragment.java | 4 +- .../SmsCommunicatorFragment.java | 2 +- .../VirtualPump/VirtualPumpFragment.java | 8 +- .../layout/overview_bolusprogress_dialog.xml | 50 ++++++ app/src/main/res/values-cs/strings.xml | 9 +- app/src/main/res/values/strings.xml | 7 + 23 files changed, 344 insertions(+), 82 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusStart.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/BolusProgressDialog.java create mode 100644 app/src/main/res/layout/overview_bolusprogress_dialog.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981032..fbb68289f4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 268a7951f3..8cb1da059d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,10 +46,10 @@ android { buildConfigField "boolean", "CLOSEDLOOP", "false" } full { - buildConfigField "boolean", "APS", "true" + buildConfigField "boolean", "APS", "true" buildConfigField "boolean", "PUMPDRIVERS", "true" - buildConfigField "boolean", "OBJECTIVES", "true" - buildConfigField "boolean", "CLOSEDLOOP", "true" + buildConfigField "boolean", "OBJECTIVES", "true" + buildConfigField "boolean", "CLOSEDLOOP", "true" } full_noobjectives { buildConfigField "boolean", "APS", "true" diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 4a8cfabe4d..c9159e6177 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.interfaces; +import android.content.Context; + import org.json.JSONObject; import java.util.Date; @@ -27,7 +29,8 @@ public interface PumpInterface { TempBasal getTempBasal(); TempBasal getExtendedBolus(); - PumpEnactResult deliverTreatment(Double insulin, Integer carbs); + PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context); + void stopBolusDelivering(); PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes); PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes); PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index 3156029edf..eaa2fd1c56 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.ConfigBuilder; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -9,6 +10,7 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -47,10 +49,12 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusProgress; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.DeviceStatus; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResult; +import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog; import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; @@ -318,11 +322,11 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } */ - public PumpEnactResult deliverTreatmentFromBolusWizard(Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { + public PumpEnactResult deliverTreatmentFromBolusWizard(Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { insulin = applyBolusConstraints(insulin); carbs = applyCarbsConstraints(carbs); - PumpEnactResult result = activePump.deliverTreatment(insulin, carbs); + PumpEnactResult result = activePump.deliverTreatment(insulin, carbs, context); if (Config.logCongigBuilderActions) log.debug("deliverTreatmentFromBolusWizard insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); @@ -345,11 +349,21 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } @Override - public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { insulin = applyBolusConstraints(insulin); carbs = applyCarbsConstraints(carbs); - PumpEnactResult result = activePump.deliverTreatment(insulin, carbs); + BolusProgressDialog bolusProgressDialog = null; + if (context != null) { + bolusProgressDialog = new BolusProgressDialog(insulin); + bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); + } + + PumpEnactResult result = activePump.deliverTreatment(insulin, carbs, context); + + if (bolusProgressDialog != null) { + bolusProgressDialog.dismiss(); + } if (Config.logCongigBuilderActions) log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); @@ -371,6 +385,11 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI return result; } + @Override + public void stopBolusDelivering() { + activePump.stopBolusDelivering(); + } + /** * apply constraints, set temp based on absolute valus and expecting absolute result * diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java index 1ab2da12e6..89b819ec46 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java @@ -57,7 +57,6 @@ import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; -import info.nightscout.utils.SafeParse; import info.nightscout.utils.SetWarnColor; import info.nightscout.utils.ToastUtils; @@ -190,6 +189,7 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface return view; } + @SuppressWarnings("UnusedParameters") @Subscribe public void onStatusEvent(final EventAppExit e) { MainApp.instance().getApplicationContext().unbindService(mConnection); @@ -238,9 +238,9 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface new Runnable() { @Override public void run() { - if (c.sStatus == c.CONNECTING) + if (c.sStatus == EventDanaRConnectionStatus.CONNECTING) btConnectionView.setText("{fa-bluetooth-b spin} " + c.sSecondsElapsed + "s"); - else if (c.sStatus == c.CONNECTED) + else if (c.sStatus == EventDanaRConnectionStatus.CONNECTED) btConnectionView.setText("{fa-bluetooth}"); else btConnectionView.setText("{fa-bluetooth-b}"); @@ -399,12 +399,11 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface } @Override - public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { ConfigBuilderFragment configBuilderFragment = MainApp.getConfigBuilder(); insulin = configBuilderFragment.applyBolusConstraints(insulin); if (insulin > 0 || carbs > 0) { Treatment t = new Treatment(); - t.insulin = insulin; boolean connectionOK = false; if (carbs > 0) connectionOK = mExecutionService.carbsEntry(carbs); if (insulin > 0) connectionOK = mExecutionService.bolus(insulin, t); @@ -427,6 +426,15 @@ public class DanaRFragment extends Fragment implements PluginBase, PumpInterface } } + @Override + public void stopBolusDelivering() { + if (mExecutionService == null) { + log.error("stopBolusDelivering mExecutionService is null"); + return; + } + mExecutionService.bolusStop(); + } + // This is called from APS @Override public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java index 9661f40448..929c89e4da 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/History/DanaRHistoryActivity.java @@ -432,7 +432,6 @@ public class DanaRHistoryActivity extends Activity { @Subscribe public void onStatusEvent(final EventDanaRConnectionStatus c) { - log.debug("EventDanaRConnectionStatus: " + c.sStatus); runOnUiThread( new Runnable() { @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/SerialIOThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/SerialIOThread.java index 3293690f60..f4fc489d0c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/SerialIOThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/SerialIOThread.java @@ -170,6 +170,10 @@ public class SerialIOThread extends Thread { } } + try { + Thread.sleep(200); + } catch (InterruptedException e) { + } if (!message.received) { log.warn("Reply not received " + message.getMessageName()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index 2b4f202f37..aa6e9d176c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -26,7 +26,6 @@ import java.util.Set; import java.util.UUID; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.Treatment; @@ -73,6 +72,7 @@ import info.nightscout.androidaps.plugins.DanaR.comm.MsgStatusBolusExtended; import info.nightscout.androidaps.plugins.DanaR.comm.MsgStatusTempBasal; import info.nightscout.androidaps.plugins.DanaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusProgress; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusStart; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; import info.nightscout.client.data.NSProfile; @@ -181,10 +181,10 @@ public class ExecutionService extends Service { ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.wrongpumppassword), R.raw.error); return; } - if (isConnected()) { + while (isConnected() || isConnecting()) { if (Config.logDanaBTComm) - log.debug("already connected from:" + from); - return; + log.debug("already connected from: " + from); + waitMsec(3000); } final long maxConnectionTime = 5 * 60 * 1000L; // 5 min synchronized (connectionLock) { @@ -199,7 +199,7 @@ public class ExecutionService extends Service { long secondsElapsed = (new Date().getTime() - startTime) / 1000L; MainApp.bus().post(new EventDanaRConnectionStatus(EventDanaRConnectionStatus.CONNECTING, (int) secondsElapsed)); if (Config.logDanaBTComm) - log.debug("connect waiting " + secondsElapsed + "sec from:" + from); + log.debug("connect waiting " + secondsElapsed + "sec from: " + from); try { mRfcommSocket.connect(); } catch (IOException e) { @@ -265,7 +265,6 @@ public class ExecutionService extends Service { mSerialIOThread.sendMessage(tempStatusMsg); // do this before statusBasic because here is temp duration mSerialIOThread.sendMessage(exStatusMsg); mSerialIOThread.sendMessage(statusMsg); - waitMsec(100); mSerialIOThread.sendMessage(statusBasicMsg); mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); // TODO: show it somewhere @@ -323,7 +322,6 @@ public class ExecutionService extends Service { connect("tempBasal"); if (!isConnected()) return false; mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); - waitMsec(200); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); return true; } @@ -332,7 +330,6 @@ public class ExecutionService extends Service { connect("tempBasalStop"); if (!isConnected()) return false; mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); - waitMsec(200); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); return true; } @@ -341,7 +338,6 @@ public class ExecutionService extends Service { connect("extendedBolus"); if (!isConnected()) return false; mSerialIOThread.sendMessage(new MsgSetExtendedBolusStart(insulin, (byte) (durationInHalfHours & 0xFF))); - waitMsec(200); mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); return true; } @@ -350,46 +346,49 @@ public class ExecutionService extends Service { connect("extendedBolusStop"); if (!isConnected()) return false; mSerialIOThread.sendMessage(new MsgSetExtendedBolusStop()); - waitMsec(200); mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); return true; } public boolean bolus(Double amount, Treatment t) { - connect("bolus"); - if (!isConnected()) return false; - // TODO: progress dialog bolusingTreatment = t; MsgBolusStart start = new MsgBolusStart(amount); MsgBolusProgress progress = new MsgBolusProgress(MainApp.bus(), amount, t); MsgBolusStop stop = new MsgBolusStop(MainApp.bus(), amount, t); - mSerialIOThread.sendMessage(start); + connect("bolus"); + if (!isConnected()) return false; + + MainApp.bus().post(new EventDanaRBolusStart()); + + if (!stop.stopped) { + mSerialIOThread.sendMessage(start); + } else { + t.insulin = 0d; + return false; + } while (!stop.stopped && !start.failed) { waitMsec(100); } bolusingTreatment = null; - waitMsec(200); getPumpStatus(); return true; } public void bolusStop() { - Treatment lastBolusingTreatment = bolusingTreatment; if (Config.logDanaBTComm) log.debug("bolusStop >>>>> @ " + (bolusingTreatment == null ? "" : bolusingTreatment.insulin)); MsgBolusStop stop = new MsgBolusStop(); stop.forced = true; - mSerialIOThread.sendMessage(stop); - while (!stop.stopped) { + if (isConnected()) { mSerialIOThread.sendMessage(stop); - waitMsec(200); + while (!stop.stopped) { + mSerialIOThread.sendMessage(stop); + waitMsec(200); + } + } else { + stop.stopped = true; } - // and update ns status to last amount - waitMsec(60000); - EventDanaRBolusProgress be = EventDanaRBolusProgress.getInstance(); - be.sStatus = ""; - MainApp.bus().post(be); } public boolean carbsEntry(int amount) { @@ -398,7 +397,6 @@ public class ExecutionService extends Service { Calendar time = Calendar.getInstance(); MsgSetCarbsEntry msg = new MsgSetCarbsEntry(time, amount); mSerialIOThread.sendMessage(msg); - waitMsec(200); return true; } @@ -453,10 +451,8 @@ public class ExecutionService extends Service { double[] basal = buildDanaRProfileRecord(profile); MsgSetBasalProfile msgSet = new MsgSetBasalProfile((byte) 0, basal); mSerialIOThread.sendMessage(msgSet); - waitMsec(200); MsgSetActivateBasalProfile msgActivate = new MsgSetActivateBasalProfile((byte) 0); mSerialIOThread.sendMessage(msgActivate); - waitMsec(200); getPumpStatus(); return true; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java index c7643d6924..9f26bf5654 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusProgress.java @@ -36,8 +36,9 @@ public class MsgBolusProgress extends MessageBase { Double done = (amount * 100 - progress) / 100d; t.insulin = done; EventDanaRBolusProgress bolusingEvent = EventDanaRBolusProgress.getInstance(); - bolusingEvent.sStatus = "Delivering " + DecimalFormatter.to1Decimal(done) + "U"; + bolusingEvent.status = "Delivering " + DecimalFormatter.to1Decimal(done) + "U"; bolusingEvent.t = t; + bolusingEvent.percent = Math.min((int) (done / amount * 100), 100); if (Config.logDanaMessageDetail) { log.debug("Bolus remaining: " + progress + " delivered: " + done); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java index 1be0e77775..bd84ff39d9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgBolusStop.java @@ -5,6 +5,8 @@ import com.squareup.otto.Bus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusProgress; @@ -36,9 +38,10 @@ public class MsgBolusStop extends MessageBase { stopped = true; if (!forced) { t.insulin = amount; - bolusingEvent.sStatus = "Delivered"; + bolusingEvent.status = MainApp.sResources.getString(R.string.overview_bolusprogress_delivered); + bolusingEvent.percent = 100; } else { - bolusingEvent.sStatus = "Stopped"; + bolusingEvent.status = MainApp.sResources.getString(R.string.overview_bolusprogress_stoped); } bus.post(bolusingEvent); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgOcclusion.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgOcclusion.java index 043429edd0..51ca3f91c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgOcclusion.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/comm/MsgOcclusion.java @@ -13,6 +13,7 @@ import java.util.Date; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusProgress; import info.nightscout.client.data.DbLogger; @@ -31,7 +32,7 @@ public class MsgOcclusion extends MessageBase { log.debug("Oclusion detected"); EventDanaRBolusProgress bolusingEvent = EventDanaRBolusProgress.getInstance(); MsgBolusStop.stopped = true; - bolusingEvent.sStatus = "Oclusion"; + bolusingEvent.status = MainApp.sResources.getString(R.string.overview_bolusiprogress_occlusion); MainApp.bus().post(bolusingEvent); sendToNSClient(); } @@ -45,7 +46,7 @@ public class MsgOcclusion extends MessageBase { try { data.put("eventType", "Announcement"); data.put("created_at", DateUtil.toISOString(new Date())); - data.put("notes", "Occlusion detected"); + data.put("notes", MainApp.sResources.getString(R.string.overview_bolusiprogress_occlusion)); data.put("isAnnouncement", true); } catch (JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusProgress.java index 2ab77d6e98..dcf40a57d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusProgress.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusProgress.java @@ -17,8 +17,9 @@ import info.nightscout.client.data.DbLogger; public class EventDanaRBolusProgress { private static Logger log = LoggerFactory.getLogger(EventDanaRBolusProgress.class); - public String sStatus = ""; + public String status = ""; public Treatment t = null; + public int percent = 0; private static EventDanaRBolusProgress eventDanaRBolusProgress = null; public EventDanaRBolusProgress() { @@ -31,25 +32,4 @@ public class EventDanaRBolusProgress { return eventDanaRBolusProgress; } - public void sendToNSClient() { - if (t == null || t._id == null || t._id.equals("")) return; - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbUpdate"); - bundle.putString("collection", "treatments"); - JSONObject data = new JSONObject(); - try { - data.put("status", sStatus); - } catch (JSONException e) { - e.printStackTrace(); - } - bundle.putString("data", data.toString()); - bundle.putString("_id", t._id); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString(), EventDanaRBolusProgress.class); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusStart.java new file mode 100644 index 0000000000..0be3cfb770 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/events/EventDanaRBolusStart.java @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.DanaR.events; + +/** + * Created by mike on 03.08.2016. + */ +public class EventDanaRBolusStart { +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java index 90b6708040..98c49677a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/MM640g/MM640gFragment.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.MM640g; +import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -164,10 +165,15 @@ public class MM640gFragment extends Fragment implements PluginBase, PumpInterfac } @Override - public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { return new PumpEnactResult(); } + @Override + public void stopBolusDelivering() { + + } + @Override public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { return new PumpEnactResult(); 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 new file mode 100644 index 0000000000..3644b0489d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/BolusProgressDialog.java @@ -0,0 +1,155 @@ +package info.nightscout.androidaps.plugins.Overview.Dialogs; + + +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusProgress; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRBolusStart; +import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRConnectionStatus; +import info.nightscout.utils.ToastUtils; + +public class BolusProgressDialog extends DialogFragment implements View.OnClickListener { + private static Logger log = LoggerFactory.getLogger(BolusProgressDialog.class); + Button stopButton; + TextView statusView; + TextView stopPressedView; + ProgressBar progressBar; + + double amount; + + boolean started = false; + + public BolusProgressDialog(double amount) { + this.amount = amount; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + getDialog().setTitle(String.format(MainApp.sResources.getString(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.setOnClickListener(this); + progressBar.setMax(100); + statusView.setText(MainApp.sResources.getString(R.string.waitingforpump)); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.overview_bolusprogress_stop: + log.debug("Stop bolus delivery button pressed"); + stopPressedView.setVisibility(View.VISIBLE); + PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); + pump.stopBolusDelivering(); + break; + } + } + + @Subscribe + public void onStatusEvent(final EventDanaRBolusProgress 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(); + } + } + }); + } + } + + @Subscribe + public void onStatusEvent(final EventDanaRBolusStart ev) { + started = true; + } + + @Subscribe + public void onStatusEvent(final EventDanaRConnectionStatus c) { + + Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread( + new Runnable() { + @Override + public void run() { + if (c.sStatus == c.CONNECTING) { + statusView.setText(String.format(getString(R.string.danar_history_connectingfor), c.sSecondsElapsed)); + } else if (c.sStatus == c.CONNECTED) { + statusView.setText(MainApp.sResources.getString(R.string.connected)); + } else { + statusView.setText(MainApp.sResources.getString(R.string.disconnected)); + if (started) scheduleDismiss(); + } + } + } + ); + } + + } + + private void scheduleDismiss() { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Activity activity = getActivity(); + if (activity != null) { + activity.runOnUiThread( + new Runnable() { + @Override + public void run() { + dismiss(); + } + }); + } + } + }); + t.start(); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index ef36d5518f..414f2e01a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.plugins.Overview.Dialogs; +import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; @@ -33,10 +35,13 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene Handler mHandler; public static HandlerThread mHandlerThread; - public NewTreatmentDialog() { - mHandlerThread = new HandlerThread(NewExtendedBolusDialog.class.getSimpleName()); + Context parentContext; + + public NewTreatmentDialog(Context context) { + mHandlerThread = new HandlerThread(NewTreatmentDialog.class.getSimpleName()); mHandlerThread.start(); this.mHandler = new Handler(mHandlerThread.getLooper()); + parentContext = context; } @Override @@ -93,12 +98,12 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene mHandler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints); + PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints, parentContext); if (!result.success) { - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); + AlertDialog.Builder builder = new AlertDialog.Builder(parentContext); + builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); builder.setMessage(result.comment); - builder.setPositiveButton(getContext().getString(R.string.ok), null); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); builder.show(); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 7ee1c6b1bb..2becceff7a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.Overview.Dialogs; +import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; @@ -77,10 +78,13 @@ public class WizardDialog extends DialogFragment implements OnClickListener { Handler mHandler; public static HandlerThread mHandlerThread; - public WizardDialog() { + Context parentContext; + + public WizardDialog(Context context) { mHandlerThread = new HandlerThread(NewExtendedBolusDialog.class.getSimpleName()); mHandlerThread.start(); mHandler = new Handler(mHandlerThread.getLooper()); + parentContext = context; } final private TextWatcher textWatcher = new TextWatcher() { @@ -193,6 +197,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { @Override public void run() { PumpEnactResult result = pump.deliverTreatmentFromBolusWizard( + parentContext, finalInsulinAfterConstraints, finalCarbsAfterConstraints, SafeParse.stringToDouble(bgInput.getText().toString()), diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 9c34df97e1..cd015ca904 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -185,7 +185,7 @@ public class OverviewFragment extends Fragment implements PluginBase { @Override public void onClick(View view) { FragmentManager manager = getFragmentManager(); - NewTreatmentDialog treatmentDialogFragment = new NewTreatmentDialog(); + NewTreatmentDialog treatmentDialogFragment = new NewTreatmentDialog(getContext()); treatmentDialogFragment.show(manager, "TreatmentDialog"); } }); @@ -194,7 +194,7 @@ public class OverviewFragment extends Fragment implements PluginBase { @Override public void onClick(View view) { FragmentManager manager = getFragmentManager(); - WizardDialog wizardDialog = new WizardDialog(); + WizardDialog wizardDialog = new WizardDialog(getContext()); wizardDialog.show(manager, "WizardDialog"); } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java index 37a633239b..9270e0674c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java @@ -234,7 +234,7 @@ public class SmsCommunicatorFragment extends Fragment implements PluginBase { PumpInterface pumpInterface = MainApp.getConfigBuilder().getActivePump(); if (pumpInterface != null) { danaRFragment = (DanaRFragment) MainApp.getSpecificPlugin(DanaRFragment.class); - PumpEnactResult result = pumpInterface.deliverTreatment(bolusWaitingForConfirmation.bolusRequested, 0); + PumpEnactResult result = pumpInterface.deliverTreatment(bolusWaitingForConfirmation.bolusRequested, 0, null); if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), bolusWaitingForConfirmation.bolusRequested); if (danaRFragment != null) reply += "\n" + danaRFragment.shortStatus(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java index af94dc27bb..01b7c1804b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.VirtualPump; import android.app.Activity; +import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.support.annotation.Nullable; @@ -181,7 +182,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { PumpEnactResult result = new PumpEnactResult(); result.success = true; result.bolusDelivered = insulin; @@ -194,6 +195,11 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt return result; } + @Override + public void stopBolusDelivering() { + + } + @Override public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { PumpEnactResult result = cancelTempBasal(); diff --git a/app/src/main/res/layout/overview_bolusprogress_dialog.xml b/app/src/main/res/layout/overview_bolusprogress_dialog.xml new file mode 100644 index 0000000000..78cfb75d91 --- /dev/null +++ b/app/src/main/res/layout/overview_bolusprogress_dialog.xml @@ -0,0 +1,50 @@ + + + + + + + + + + +