diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb68289f4..5d19981032 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 665ca1f253..6c92f385da 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -62,6 +63,7 @@ public class MainApp extends Application { pluginsList = new ArrayList<>(); // Register all tabs in app here pluginsList.add(OverviewFragment.getPlugin()); + pluginsList.add(ActionsFragment.getPlugin()); if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin()); if (Config.MM640G) pluginsList.add(MM640gFragment.getPlugin()); if (Config.CAREPORTALENABLED) pluginsList.add(CareportalFragment.getPlugin()); 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 new file mode 100644 index 0000000000..245d189660 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -0,0 +1,65 @@ +package info.nightscout.androidaps.plugins.Actions; + + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; +import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog; +import info.nightscout.androidaps.plugins.Actions.dialogs.NewTempBasalDialog; + +/** + * A simple {@link Fragment} subclass. + */ +public class ActionsFragment extends Fragment implements FragmentBase, View.OnClickListener { + + static ActionsPlugin actionsPlugin = new ActionsPlugin(); + static public ActionsPlugin getPlugin() { + return actionsPlugin; + } + + public ActionsFragment() { + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.actions_fragment, container, false); + + view.findViewById(R.id.actions_profileswitch).setOnClickListener(this); + view.findViewById(R.id.actions_extendedbolus).setOnClickListener(this); + view.findViewById(R.id.actions_settempbasal).setOnClickListener(this); + + return view; + } + + @Override + public void onClick(View view) { + FragmentManager manager = getFragmentManager(); + switch (view.getId()) { + case R.id.actions_profileswitch: + 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(manager, "NewNSTreatmentDialog"); + break; + case R.id.actions_extendedbolus: + NewExtendedBolusDialog newExtendedDialog = new NewExtendedBolusDialog(); + newExtendedDialog.show(manager, "NewExtendedDialog"); + break; + case R.id.actions_settempbasal: + NewTempBasalDialog newTempDialog = new NewTempBasalDialog(); + newTempDialog.show(manager, "NewTempDialog"); + break; + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java new file mode 100644 index 0000000000..d591f67556 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java @@ -0,0 +1,56 @@ +package info.nightscout.androidaps.plugins.Actions; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginBase; + +/** + * Created by mike on 05.11.2016. + */ + +public class ActionsPlugin implements PluginBase { + + boolean fragmentEnabled = true; + boolean fragmentVisible = true; + + @Override + public int getType() { + return PluginBase.GENERAL; + } + + @Override + public String getFragmentClass() { + return ActionsFragment.class.getName(); + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.actions); + } + + @Override + public boolean isEnabled(int type) { + return fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + this.fragmentVisible = fragmentVisible; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java similarity index 99% rename from app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java rename to app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java index 8ca775e25f..56ffa69d5f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.Overview.Dialogs; +package info.nightscout.androidaps.plugins.Actions.dialogs; import android.content.Context; import android.content.DialogInterface; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java similarity index 99% rename from app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java rename to app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java index 3b27f869d7..e0dae0bcd8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.Overview.Dialogs; +package info.nightscout.androidaps.plugins.Actions.dialogs; import android.content.Context; import android.content.DialogInterface; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java index d308363b03..047dd3e7c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalFragment.java @@ -22,44 +22,6 @@ public class CareportalFragment extends Fragment implements FragmentBase, View.O return careportalPlugin; } - public class OptionsToShow { - public int eventType; - public int eventName; - public boolean bg; - public boolean insulin; - public boolean carbs; - public boolean prebolus; - public boolean duration; - public boolean percent; - public boolean absolute; - public boolean profile; - public boolean split; - - public OptionsToShow(int eventType, - int eventName, - boolean bg, - boolean insulin, - boolean carbs, - boolean prebolus, - boolean duration, - boolean percent, - boolean absolute, - boolean profile, - boolean split) { - this.eventType = eventType; - this.eventName = eventName; - this.bg = bg; - this.insulin = insulin; - this.carbs = carbs; - this.prebolus = prebolus; - this.duration = duration; - this.percent = percent; - this.absolute = absolute; - this.profile = profile; - this.split = split; - } - } - // bg,insulin,carbs,prebolus,duration,percent,absolute,profile,split final OptionsToShow bgcheck = new OptionsToShow(R.id.careportal_bgcheck, R.string.careportal_bgcheck, true, true, true, false, false, false, false, false, false); final OptionsToShow snackbolus = new OptionsToShow(R.id.careportal_snackbolus, R.string.careportal_snackbolus, true, true, true, true, false, false, false, false, false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index dac8968b64..1011caf40a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -2,11 +2,11 @@ package info.nightscout.androidaps.plugins.Careportal.Dialogs; import android.app.Activity; -import android.content.Context; import android.content.DialogInterface; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; @@ -43,13 +43,10 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.Services.Intents; -import info.nightscout.androidaps.db.BgReading; -import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; +import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog; -import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.PlusMinusEditText; @@ -62,7 +59,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick private FragmentActivity context; - private static CareportalFragment.OptionsToShow options; + private static OptionsToShow options; NSProfile profile; String units; @@ -107,10 +104,23 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick Date eventTime; - public void setOptions(CareportalFragment.OptionsToShow options) { + private static Handler sHandler; + private static HandlerThread sHandlerThread; + + + public void setOptions(OptionsToShow options) { this.options = options; } + public NewNSTreatmentDialog() { + super(); + if (sHandlerThread == null) { + sHandlerThread = new HandlerThread(NewNSTreatmentDialog.class.getSimpleName()); + sHandlerThread.start(); + sHandler = new Handler(sHandlerThread.getLooper()); + } + } + @Override public void onAttach(Activity activity) { context = (FragmentActivity) activity; @@ -532,6 +542,30 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { ConfigBuilderPlugin.uploadCareportalEntryToNS(data); + if (options.executeProfileSwitch) { + if (data.has("profile")) { + sHandler.post(new Runnable() { + @Override + public void run() { + try { + String profile = data.getString("profile"); + NSProfile nsProfile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + nsProfile.setActiveProfile(profile); + PumpInterface pump = MainApp.getConfigBuilder(); + if (pump != null) { + pump.setNewBasalProfile(nsProfile); + log.debug("Setting new profile: " + profile); + MainApp.bus().post(new EventNewBasalProfile(nsProfile)); + } else { + log.error("No active pump selected"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + }); + } + } } }); builder.setNegativeButton(getContext().getString(R.string.cancel), null); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java new file mode 100644 index 0000000000..e5a086c07e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/OptionsToShow.java @@ -0,0 +1,46 @@ +package info.nightscout.androidaps.plugins.Careportal; + +/** + * Created by mike on 05.11.2016. + */ + +public class OptionsToShow { + public int eventType; + public int eventName; + public boolean bg; + public boolean insulin; + public boolean carbs; + public boolean prebolus; + public boolean duration; + public boolean percent; + public boolean absolute; + public boolean profile; + public boolean split; + + // perform direct actions + public boolean executeProfileSwitch = false; + + public OptionsToShow(int eventType, + int eventName, + boolean bg, + boolean insulin, + boolean carbs, + boolean prebolus, + boolean duration, + boolean percent, + boolean absolute, + boolean profile, + boolean split) { + this.eventType = eventType; + this.eventName = eventName; + this.bg = bg; + this.insulin = insulin; + this.carbs = carbs; + this.prebolus = prebolus; + this.duration = duration; + this.percent = percent; + this.absolute = absolute; + this.profile = profile; + this.split = split; + } +} 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 d052d80d02..f842ac9a5b 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 @@ -43,7 +43,7 @@ import info.nightscout.androidaps.plugins.Loop.DeviceStatus; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; 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.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog; import info.nightscout.client.data.DbLogger; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; 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 401c612c75..6efdfd0b0a 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 @@ -11,7 +11,6 @@ import android.os.Handler; import android.os.HandlerThread; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; -import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; @@ -60,8 +59,6 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.Objectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal; -import info.nightscout.androidaps.plugins.Overview.Dialogs.NewExtendedBolusDialog; -import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTempBasalDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.WizardDialog; import info.nightscout.androidaps.plugins.Overview.GraphSeriesExtension.PointsWithLabelGraphSeries; @@ -70,7 +67,6 @@ import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; -import info.nightscout.utils.SafeParse; public class OverviewFragment extends Fragment { @@ -91,14 +87,11 @@ public class OverviewFragment extends Fragment { GraphView bgGraph; LinearLayout cancelTempLayout; - LinearLayout setTempLayout; LinearLayout acceptTempLayout; LinearLayout quickWizardLayout; Button cancelTempButton; Button treatmentButton; Button wizardButton; - Button setTempButton; - Button setExtenedButton; Button acceptTempButton; Button quickWizardButton; @@ -131,10 +124,7 @@ public class OverviewFragment extends Fragment { cancelTempButton = (Button) view.findViewById(R.id.overview_canceltemp); treatmentButton = (Button) view.findViewById(R.id.overview_treatment); wizardButton = (Button) view.findViewById(R.id.overview_wizard); - setExtenedButton = (Button) view.findViewById(R.id.overview_extendedbolus); - setTempButton = (Button) view.findViewById(R.id.overview_settempbasal); cancelTempButton = (Button) view.findViewById(R.id.overview_canceltemp); - setTempLayout = (LinearLayout) view.findViewById(R.id.overview_settemplayout); cancelTempLayout = (LinearLayout) view.findViewById(R.id.overview_canceltemplayout); acceptTempButton = (Button) view.findViewById(R.id.overview_accepttempbutton); acceptTempLayout = (LinearLayout) view.findViewById(R.id.overview_accepttemplayout); @@ -183,23 +173,6 @@ public class OverviewFragment extends Fragment { } }); - setTempButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - FragmentManager manager = getFragmentManager(); - NewTempBasalDialog newTempDialog = new NewTempBasalDialog(); - newTempDialog.show(manager, "NewTempDialog"); - } - }); - - setExtenedButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - FragmentManager manager = getFragmentManager(); - NewExtendedBolusDialog newExtendedDialog = new NewExtendedBolusDialog(); - newExtendedDialog.show(manager, "NewExtendedDialog"); - } - }); acceptTempButton.setOnClickListener(new View.OnClickListener() { @Override @@ -465,26 +438,20 @@ public class OverviewFragment extends Fragment { if (pump.isTempBasalInProgress()) { TempBasal activeTemp = pump.getTempBasal(); cancelTempLayout.setVisibility(View.VISIBLE); - setTempLayout.setVisibility(View.GONE); cancelTempButton.setText(MainApp.instance().getString(R.string.cancel) + ": " + activeTemp.toString()); runningTempView.setText(activeTemp.toString()); } else { cancelTempLayout.setVisibility(View.GONE); - setTempLayout.setVisibility(View.VISIBLE); Double currentBasal = pump.getBaseBasalRate(); runningTempView.setText(DecimalFormatter.to2Decimal(currentBasal) + " U/h"); } if (profile == null) { // disable all treatment buttons because we are not able to check constraints without profile - setExtenedButton.setVisibility(View.INVISIBLE); - setTempLayout.setVisibility(View.INVISIBLE); wizardButton.setVisibility(View.INVISIBLE); treatmentButton.setVisibility(View.INVISIBLE); return; } else { - setExtenedButton.setVisibility(View.VISIBLE); - setTempLayout.setVisibility(View.VISIBLE); wizardButton.setVisibility(View.VISIBLE); treatmentButton.setVisibility(View.VISIBLE); } diff --git a/app/src/main/java/info/nightscout/client/data/NSProfile.java b/app/src/main/java/info/nightscout/client/data/NSProfile.java index 5b919466ee..c9a0c0723e 100644 --- a/app/src/main/java/info/nightscout/client/data/NSProfile.java +++ b/app/src/main/java/info/nightscout/client/data/NSProfile.java @@ -349,6 +349,19 @@ public class NSProfile { return activeProfile; } + public void setActiveProfile(String newProfile) { + try { + JSONObject store = json.getJSONObject("store"); + if (newProfile != null && store.has(newProfile)) { + activeProfile = newProfile; + } else { + log.error("Attempt to set wrong active profile"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + public Double getMaxDailyBasal() { Double max = 0d; for (Integer hour = 0; hour < 24; hour++) { diff --git a/app/src/main/res/layout/actions_fragment.xml b/app/src/main/res/layout/actions_fragment.xml new file mode 100644 index 0000000000..daa2f52ecf --- /dev/null +++ b/app/src/main/res/layout/actions_fragment.xml @@ -0,0 +1,53 @@ + + + + +