diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java index 245d189660..f263adcc99 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -10,6 +10,7 @@ import android.view.ViewGroup; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog; @@ -37,6 +38,7 @@ public class ActionsFragment extends Fragment implements FragmentBase, View.OnCl view.findViewById(R.id.actions_profileswitch).setOnClickListener(this); view.findViewById(R.id.actions_extendedbolus).setOnClickListener(this); view.findViewById(R.id.actions_settempbasal).setOnClickListener(this); + view.findViewById(R.id.actions_fill).setOnClickListener(this); return view; } @@ -60,6 +62,10 @@ public class ActionsFragment extends Fragment implements FragmentBase, View.OnCl NewTempBasalDialog newTempDialog = new NewTempBasalDialog(); newTempDialog.show(manager, "NewTempDialog"); break; + case R.id.actions_fill: + FillDialog fillDialog = new FillDialog(); + fillDialog.show(manager, "FillDialog"); + break; } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java new file mode 100644 index 0000000000..2317c59983 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -0,0 +1,125 @@ +package info.nightscout.androidaps.plugins.Actions.dialogs; + +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.view.LayoutInflater; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.TextView; + +import java.text.DecimalFormat; + +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.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.utils.PlusMinusEditText; +import info.nightscout.utils.SafeParse; + +public class FillDialog extends DialogFragment implements OnClickListener { + + Button deliverButton; + TextView insulin; + + PlusMinusEditText editInsulin; + + Handler mHandler; + public static HandlerThread mHandlerThread; + + public FillDialog() { + mHandlerThread = new HandlerThread(FillDialog.class.getSimpleName()); + mHandlerThread.start(); + this.mHandler = new Handler(mHandlerThread.getLooper()); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.actions_fill_dialog, null, false); + + deliverButton = (Button) view.findViewById(R.id.treatments_newtreatment_deliverbutton); + + deliverButton.setOnClickListener(this); + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + + insulin = (TextView) view.findViewById(R.id.treatments_newtreatment_insulinamount); + Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + + editInsulin = new PlusMinusEditText(view, R.id.treatments_newtreatment_insulinamount, R.id.treatments_newtreatment_insulinamount_plus, R.id.treatments_newtreatment_insulinamount_minus, 0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + if (getDialog() != null) + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.treatments_newtreatment_deliverbutton: + + try { + Double insulin = SafeParse.stringToDouble(this.insulin.getText().toString()); + + String confirmMessage = getString(R.string.fillwarning) + "\n"; + + Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); + confirmMessage += getString(R.string.bolus) + ": " + insulinAfterConstraints + "U"; + if (insulinAfterConstraints - insulin != 0) + confirmMessage += "\n" + getString(R.string.constraintapllied); + + final Double finalInsulinAfterConstraints = insulinAfterConstraints; + + final Context context = getContext(); + AlertDialog.Builder builder = new AlertDialog.Builder(context); + + builder.setTitle(this.getContext().getString(R.string.confirmation)); + builder.setMessage(confirmMessage); + builder.setPositiveButton(getString(R.string.primefill), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + if (finalInsulinAfterConstraints > 0) { + final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); + mHandler.post(new Runnable() { + @Override + public void run() { + PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, 0, context, false); + if (!result.success) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); + builder.setMessage(result.comment); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); + builder.show(); + } + } + }); + } + } + }); + builder.setNegativeButton(getString(R.string.cancel), null); + builder.show(); + dismiss(); + } catch (Exception e) { + e.printStackTrace(); + } + break; + } + + } + +} \ No newline at end of file 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 6aea91ca05..2e0696c228 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 @@ -390,6 +390,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context) { + return deliverTreatment(insulin, carbs, context, true); + } + + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs, Context context, boolean createTreatment) { mWakeLock.acquire(); insulin = applyBolusConstraints(insulin); carbs = applyCarbsConstraints(carbs); @@ -416,7 +420,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain if (Config.logCongigBuilderActions) log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); - if (result.success) { + if (result.success && createTreatment) { Treatment t = new Treatment(); t.insulin = result.bolusDelivered; t.carbs = (double) result.carbsDelivered; @@ -435,6 +439,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return result; } + + @Override public void stopBolusDelivering() { activePump.stopBolusDelivering(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java index 579407e365..6c9bf14e77 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java @@ -22,6 +22,8 @@ public class DanaRPump { public static final int UNITS_MGDL = 0; public static final int UNITS_MMOL = 1; + public static final String PROFILE_PREFIX = "DanaR-"; + public Date lastConnection = new Date(0); public Date lastSettingsRead = new Date(0); @@ -106,7 +108,7 @@ public class DanaRPump { double car = SafeParse.stringToDouble(SP.getString("danarprofile_car", "20")); try { - json.put("defaultProfile", "" + (activeProfile + 1)); + json.put("defaultProfile", PROFILE_PREFIX + (activeProfile + 1)); json.put("store", store); profile.put("dia", dia); @@ -139,20 +141,20 @@ public class DanaRPump { } else { time = df.format(h) + ":00"; } - basals.put(new JSONObject().put("time", time).put("timeAsSeconds", h * basalIncrement).put("value", pumpProfiles[activeProfile][h])); + basals.put(new JSONObject().put("time", time).put("timeAsSeconds", h * basalIncrement).put("value", pumpProfiles[activeProfile][h])); } profile.put("basal", basals); profile.put("target_low", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", currentTarget))); profile.put("target_high", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", currentTarget))); profile.put("units", units == UNITS_MGDL ? Constants.MGDL : Constants.MMOL); - store.put("" + (activeProfile + 1), profile); + store.put(PROFILE_PREFIX + (activeProfile + 1), profile); } catch (JSONException e) { e.printStackTrace(); } catch (Exception e) { return null; } - return new NSProfile(json, "" + (activeProfile + 1)); + return new NSProfile(json, PROFILE_PREFIX + (activeProfile + 1)); } } 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 f76743bc4f..e255c54ace 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 @@ -50,12 +50,15 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventNewBG; +import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; @@ -69,6 +72,7 @@ import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; +import info.nightscout.utils.ToastUtils; public class OverviewFragment extends Fragment { @@ -370,6 +374,9 @@ public class OverviewFragment extends Fragment { updateGUIIfVisible(); } + @Subscribe + public void onStatusEvent(final EventNewBasalProfile ev) { updateGUIIfVisible(); } + private void hideTempRecommendation() { Activity activity = getActivity(); if (activity != null) @@ -480,6 +487,19 @@ public class OverviewFragment extends Fragment { } baseBasalView.setText(DecimalFormatter.to2Decimal(pump.getBaseBasalRate()) + " U/h"); activeProfileView.setText(profile.getActiveProfile()); + activeProfileView.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View view) { + view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); + NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); + final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false); + profileswitch.executeProfileSwitch = true; + newDialog.setOptions(profileswitch); + newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + return true; + } + }); + activeProfileView.setLongClickable(true); if (profile == null) { // disable all treatment buttons because we are not able to check constraints without profile diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java index b6b4667d64..baa3da71f6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java @@ -217,7 +217,7 @@ public class SimpleProfilePlugin implements PluginBase, ProfileInterface { JSONObject profile = new JSONObject(); try { - json.put("defaultProfile", "Profile"); + json.put("defaultProfile", "SimpleProfile"); json.put("store", store); profile.put("dia", dia); profile.put("carbratio", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", ic))); @@ -227,11 +227,11 @@ public class SimpleProfilePlugin implements PluginBase, ProfileInterface { profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow))); profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh))); profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL); - store.put("Profile", profile); + store.put("SimpleProfile", profile); } catch (JSONException e) { e.printStackTrace(); } - convertedProfile = new NSProfile(json, null); + convertedProfile = new NSProfile(json, "SimpleProfile"); } @Override diff --git a/app/src/main/res/layout/actions_fill_dialog.xml b/app/src/main/res/layout/actions_fill_dialog.xml new file mode 100644 index 0000000000..6394ccf304 --- /dev/null +++ b/app/src/main/res/layout/actions_fill_dialog.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + +