diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index c46a13c91e..5643d445fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -39,6 +39,7 @@ import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.tabs.SlidingTabLayout; import info.nightscout.androidaps.tabs.TabPageAdapter; import info.nightscout.utils.ImportExportPrefs; @@ -300,7 +301,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe builder.setIcon(R.mipmap.yellowowl); else builder.setIcon(R.mipmap.blueowl); - builder.setMessage("Build: " + BuildConfig.BUILDVERSION); + String message = "Build: " + BuildConfig.BUILDVERSION + "\n"; + message += MainApp.sResources.getString(R.string.configbuilder_nightscoutversion_label) + " " + ConfigBuilderPlugin.nightscoutVersionName; + builder.setMessage(message); builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); AlertDialog alertDialog = builder.create(); alertDialog.show(); diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 1e49b03d73..b820393c33 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -26,10 +26,10 @@ import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragment; -import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyFragment; +import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment; import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment; -import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorFragment; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAFragment; @@ -46,17 +46,18 @@ import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanFragment; import info.nightscout.androidaps.plugins.PumpDanaRKorean.services.DanaRKoreanExecutionService; import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Fragment; import info.nightscout.androidaps.plugins.PumpDanaRv2.services.DanaRv2ExecutionService; -import info.nightscout.androidaps.plugins.PumpMDI.MDIFragment; -import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpFragment; +import info.nightscout.androidaps.plugins.PumpMDI.MDIPlugin; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.androidaps.plugins.SensitivityMK.SensitivityMKPlugin; +import info.nightscout.androidaps.plugins.SensitivityOref0.SensitivityOref0Plugin; import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorFragment; -import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpFragment; -import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gFragment; -import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; -import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; +import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpPlugin; +import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin; +import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; +import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.Wear.WearFragment; -import info.nightscout.androidaps.plugins.XDripStatusline.StatuslineFragment; +import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin; import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.utils.NSUpload; import io.fabric.sdk.android.Fabric; @@ -94,15 +95,17 @@ public class MainApp extends Application { pluginsList = new ArrayList<>(); // Register all tabs in app here pluginsList.add(OverviewFragment.getPlugin()); - pluginsList.add(IobCobCalculatorFragment.getPlugin()); + pluginsList.add(IobCobCalculatorPlugin.getPlugin()); if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin()); pluginsList.add(InsulinFastactingFragment.getPlugin()); pluginsList.add(InsulinFastactingProlongedFragment.getPlugin()); + pluginsList.add(SensitivityOref0Plugin.getPlugin()); + pluginsList.add(SensitivityMKPlugin.getPlugin()); if (Config.DANAR) pluginsList.add(DanaRFragment.getPlugin()); if (Config.DANAR) pluginsList.add(DanaRKoreanFragment.getPlugin()); if (Config.DANARv2) pluginsList.add(DanaRv2Fragment.getPlugin()); pluginsList.add(CareportalFragment.getPlugin()); - if (Config.MDI) pluginsList.add(MDIFragment.getPlugin()); + if (Config.MDI) pluginsList.add(MDIPlugin.getPlugin()); if (Config.VIRTUALPUMP) pluginsList.add(VirtualPumpPlugin.getInstance()); if (Config.LOOPENABLED) pluginsList.add(LoopFragment.getPlugin()); if (Config.OPENAPSENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin()); @@ -113,19 +116,19 @@ public class MainApp extends Application { if (Config.OTHERPROFILES) pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); pluginsList.add(TreatmentsFragment.getPlugin()); - if (Config.SAFETY) pluginsList.add(SafetyFragment.getPlugin()); + if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin()); if (Config.APS) pluginsList.add(ObjectivesFragment.getPlugin()); if (!Config.NSCLIENT) - pluginsList.add(SourceXdripFragment.getPlugin()); - pluginsList.add(SourceNSClientFragment.getPlugin()); + pluginsList.add(SourceXdripPlugin.getPlugin()); + pluginsList.add(SourceNSClientPlugin.getPlugin()); if (!Config.NSCLIENT) - pluginsList.add(SourceMM640gFragment.getPlugin()); + pluginsList.add(SourceMM640gPlugin.getPlugin()); if (!Config.NSCLIENT) - pluginsList.add(SourceGlimpFragment.getPlugin()); + pluginsList.add(SourceGlimpPlugin.getPlugin()); if (Config.SMSCOMMUNICATORENABLED) pluginsList.add(SmsCommunicatorFragment.getPlugin()); if (Config.WEAR) pluginsList.add(WearFragment.getPlugin(this)); - pluginsList.add(StatuslineFragment.getPlugin(this)); + pluginsList.add(StatuslinePlugin.getPlugin(this)); pluginsList.add(new PersistentNotificationPlugin(this)); pluginsList.add(NSClientInternalFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index fa19e9b596..71c3a3981e 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -21,6 +21,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugi import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.androidaps.plugins.SensitivityMK.SensitivityMKPlugin; import info.nightscout.androidaps.plugins.Wear.WearPlugin; import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin; import info.nightscout.utils.LocaleHelper; @@ -105,6 +106,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class) != null && MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS)) addPreferencesFromResource(R.xml.pref_openapsama); } + if (MainApp.getSpecificPlugin(SensitivityMKPlugin.class) != null && MainApp.getSpecificPlugin(SensitivityMKPlugin.class).isEnabled(PluginBase.SENSITIVITY)) + addPreferencesFromResource(R.xml.pref_absorption); if (Config.ALLPREFERENCES) { addPreferencesFromResource(R.xml.pref_profile); } @@ -134,8 +137,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResource(R.xml.pref_smscommunicator); if (Config.ALLPREFERENCES) { addPreferencesFromResource(R.xml.pref_others); - addPreferencesFromResource(R.xml.pref_advanced); } + addPreferencesFromResource(R.xml.pref_advanced); if (Config.WEAR) { WearPlugin wearPlugin = (WearPlugin) MainApp.getSpecificPlugin(WearPlugin.class); diff --git a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java index 85ab07edaf..4f7c662b05 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java @@ -10,6 +10,7 @@ import java.util.Date; import java.util.Objects; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.Profile; @@ -18,6 +19,7 @@ import info.nightscout.androidaps.interfaces.Interval; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SP; /** * Created by mike on 21.05.2017. @@ -271,6 +273,15 @@ public class TemporaryBasal implements Interval { public String toStringShort() { if (isAbsolute) { + if(SP.getBoolean(R.string.key_danar_visualizeextendedaspercentage, false) && SP.getBoolean(R.string.key_danar_useextended, false)){ + Profile profile = MainApp.getConfigBuilder().getProfile(); + if(profile != null) { + double basal = profile.getBasal(System.currentTimeMillis()); + if(basal != 0){ + return Math.round(absoluteRate*100d/basal) + "% "; + } + } + } return DecimalFormatter.to2Decimal(absoluteRate) + "U/h "; } else { // percent return percentRate + "% "; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java index ba4faf4ba6..bf5e66f2a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java @@ -8,7 +8,7 @@ import java.util.Date; public interface PluginBase { int GENERAL = 1; int TREATMENT = 2; - //int TEMPBASAL = 3; + int SENSITIVITY = 3; int PROFILE = 4; int APS = 5; int PUMP = 6; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java new file mode 100644 index 0000000000..deb649c21f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/SensitivityInterface.java @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.interfaces; + +import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; + +/** + * Created by mike on 24.06.2017. + */ + +public interface SensitivityInterface { + AutosensResult detectSensitivity(long fromTime, long toTime); +} 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 6e0bf61add..bc2c0adb86 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 @@ -169,14 +169,14 @@ public class ActionsFragment extends Fragment implements View.OnClickListener { NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); final OptionsToShow profileswitch = CareportalFragment.profileswitch; profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch); + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); newDialog.show(manager, "NewNSTreatmentDialog"); break; case R.id.actions_temptarget: NewNSTreatmentDialog newTTDialog = new NewNSTreatmentDialog(); final OptionsToShow temptarget = CareportalFragment.temptarget; temptarget.executeTempTarget = true; - newTTDialog.setOptions(temptarget); + newTTDialog.setOptions(temptarget, R.string.careportal_temporarytarget); newTTDialog.show(manager, "NewNSTreatmentDialog"); break; case R.id.actions_extendedbolus: 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 d9a7621a56..309ccecb6e 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 @@ -100,66 +100,66 @@ public class CareportalFragment extends Fragment implements View.OnClickListener NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); switch (id) { case R.id.careportal_bgcheck: - newDialog.setOptions(bgcheck); + newDialog.setOptions(bgcheck, R.string.careportal_bgcheck); break; case R.id.careportal_announcement: - newDialog.setOptions(announcement); + newDialog.setOptions(announcement, R.string.careportal_announcement); break; case R.id.careportal_cgmsensorinsert: - newDialog.setOptions(sensorchange); + newDialog.setOptions(sensorchange, R.string.careportal_cgmsensorinsert); break; case R.id.careportal_cgmsensorstart: - newDialog.setOptions(sensorstart); + newDialog.setOptions(sensorstart, R.string.careportal_cgmsensorstart); break; case R.id.careportal_combobolus: - newDialog.setOptions(combobolus); + newDialog.setOptions(combobolus, R.string.careportal_combobolus); break; case R.id.careportal_correctionbolus: - newDialog.setOptions(correctionbolus); + newDialog.setOptions(correctionbolus, R.string.careportal_correctionbolus); break; case R.id.careportal_carbscorrection: - newDialog.setOptions(carbcorrection); + newDialog.setOptions(carbcorrection, R.string.careportal_carbscorrection); break; case R.id.careportal_exercise: - newDialog.setOptions(exercise); + newDialog.setOptions(exercise, R.string.careportal_exercise); break; case R.id.careportal_insulincartridgechange: - newDialog.setOptions(insulinchange); + newDialog.setOptions(insulinchange, R.string.careportal_insulincartridgechange); break; case R.id.careportal_pumpbatterychange: - newDialog.setOptions(pumpbatterychange); + newDialog.setOptions(pumpbatterychange, R.string.careportal_pumpbatterychange); break; case R.id.careportal_mealbolus: - newDialog.setOptions(mealbolus); + newDialog.setOptions(mealbolus, R.string.careportal_mealbolus); break; case R.id.careportal_note: - newDialog.setOptions(note); + newDialog.setOptions(note, R.string.careportal_note); break; case R.id.careportal_profileswitch: profileswitch.executeProfileSwitch = false; - newDialog.setOptions(profileswitch); + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); break; case R.id.careportal_pumpsitechange: - newDialog.setOptions(sitechange); + newDialog.setOptions(sitechange, R.string.careportal_pumpsitechange); break; case R.id.careportal_question: - newDialog.setOptions(question); + newDialog.setOptions(question, R.string.careportal_question); break; case R.id.careportal_snackbolus: - newDialog.setOptions(snackbolus); + newDialog.setOptions(snackbolus, R.string.careportal_snackbolus); break; case R.id.careportal_tempbasalstart: - newDialog.setOptions(tempbasalstart); + newDialog.setOptions(tempbasalstart, R.string.careportal_tempbasalstart); break; case R.id.careportal_tempbasalend: - newDialog.setOptions(tempbasalend); + newDialog.setOptions(tempbasalend, R.string.careportal_tempbasalend); break; case R.id.careportal_openapsoffline: - newDialog.setOptions(openapsoffline); + newDialog.setOptions(openapsoffline, R.string.careportal_openapsoffline); break; case R.id.careportal_temporarytarget: temptarget.executeTempTarget = false; - newDialog.setOptions(temptarget); + newDialog.setOptions(temptarget, R.string.careportal_temporarytarget); break; default: newDialog = null; 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 f440311d18..ff1811b3ed 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 @@ -15,12 +15,10 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; -import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; -import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.TextView; @@ -44,6 +42,8 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.Source; @@ -52,12 +52,10 @@ 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.data.Profile; -import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.plugins.ProfileCircadianPercentage.CircadianPercentageProfilePlugin; import info.nightscout.utils.DateUtil; import info.nightscout.utils.NSUpload; -import info.nightscout.utils.PlusMinusEditText; +import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.Translator; @@ -68,53 +66,44 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick private Activity context; private static OptionsToShow options; + private static String event; Profile profile; ProfileStore profileStore; String units; - RelativeLayout layoutBg; + TextView eventTypeText; + LinearLayout layoutBg; LinearLayout layoutBgSource; - RelativeLayout layoutInsulin; - RelativeLayout layoutCarbs; - RelativeLayout layoutSplit; - RelativeLayout layoutDuration; - RelativeLayout layoutPercent; - RelativeLayout layoutAbsolute; - RelativeLayout layoutCarbTime; - RelativeLayout layoutProfile; + LinearLayout layoutInsulin; + LinearLayout layoutCarbs; + LinearLayout layoutSplit; + LinearLayout layoutDuration; + LinearLayout layoutPercent; + LinearLayout layoutAbsolute; + LinearLayout layoutCarbTime; + LinearLayout layoutProfile; LinearLayout layoutTempTarget; - Button dateButton; - Button timeButton; - Button okButton; - Button cancelButton; + TextView dateButton; + TextView timeButton; TextView bgUnitsView; RadioButton meterRadioButton; RadioButton sensorRadioButton; RadioButton otherRadioButton; EditText notesEdit; - EditText bgInputEdit; - EditText insulinEdit; - EditText carbsEdit; - EditText percentEdit; - EditText absoluteEdit; - EditText durationeEdit; - EditText carbTimeEdit; - EditText splitEdit; Spinner profileSpinner; - EditText low; - EditText high; Spinner reasonSpinner; - PlusMinusEditText editBg; - PlusMinusEditText editCarbs; - PlusMinusEditText editInsulin; - PlusMinusEditText editSplit; - PlusMinusEditText editDuration; - PlusMinusEditText editPercent; - PlusMinusEditText editAbsolute; - PlusMinusEditText editCarbTime; + NumberPicker editBg; + NumberPicker editCarbs; + NumberPicker editInsulin; + NumberPicker editSplit; + NumberPicker editDuration; + NumberPicker editPercent; + NumberPicker editAbsolute; + NumberPicker editCarbTime; + NumberPicker editTemptarget; Date eventTime; @@ -122,8 +111,9 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick private static HandlerThread sHandlerThread; - public void setOptions(OptionsToShow options) { + public void setOptions(OptionsToShow options, int event) { this.options = options; + this.event = MainApp.sResources.getString(event); } public NewNSTreatmentDialog() { @@ -154,88 +144,44 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick setStyle(DialogFragment.STYLE_NORMAL, getTheme()); View view = inflater.inflate(R.layout.careportal_newnstreatment_dialog, container, false); - layoutBg = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_bg_layout); + layoutBg = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_bg_layout); layoutBgSource = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_bgsource_layout); - layoutInsulin = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_insulin_layout); - layoutCarbs = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_carbs_layout); - layoutSplit = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_split_layout); - layoutDuration = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_duration_layout); - layoutPercent = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_percent_layout); - layoutAbsolute = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_absolute_layout); - layoutCarbTime = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_carbtime_layout); - layoutProfile = (RelativeLayout) view.findViewById(R.id.careportal_newnstreatment_profile_layout); + layoutInsulin = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_insulin_layout); + layoutCarbs = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_carbs_layout); + layoutSplit = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_split_layout); + layoutDuration = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_duration_layout); + layoutPercent = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_percent_layout); + layoutAbsolute = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_absolute_layout); + layoutCarbTime = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_carbtime_layout); + layoutProfile = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_profile_layout); layoutTempTarget = (LinearLayout) view.findViewById(R.id.careportal_newnstreatment_temptarget_layout); + eventTypeText = (TextView) view.findViewById(R.id.careportal_newnstreatment_eventtype); + eventTypeText.setText(event); bgUnitsView = (TextView) view.findViewById(R.id.careportal_newnstreatment_bgunits); meterRadioButton = (RadioButton) view.findViewById(R.id.careportal_newnstreatment_meter); sensorRadioButton = (RadioButton) view.findViewById(R.id.careportal_newnstreatment_sensor); otherRadioButton = (RadioButton) view.findViewById(R.id.careportal_newnstreatment_other); profileSpinner = (Spinner) view.findViewById(R.id.careportal_newnstreatment_profile); - bgInputEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_bginput); - insulinEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_insulininput); - carbsEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_carbsinput); - percentEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_percentinput); - percentEdit.addTextChangedListener(new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - layoutPercent.setVisibility(View.VISIBLE); - layoutAbsolute.setVisibility(View.GONE); - } - }); - absoluteEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); - absoluteEdit.addTextChangedListener(new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - layoutPercent.setVisibility(View.GONE); - layoutAbsolute.setVisibility(View.VISIBLE); - } - }); - durationeEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_durationinput); - carbTimeEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput); notesEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_notes); - splitEdit = (EditText) view.findViewById(R.id.careportal_newnstreatment_splitinput); reasonSpinner = (Spinner) view.findViewById(R.id.careportal_newnstreatment_temptarget_reason); - low = (EditText) view.findViewById(R.id.careportal_temptarget_low); - high = (EditText) view.findViewById(R.id.careportal_temptarget_high); eventTime = new Date(); - dateButton = (Button) view.findViewById(R.id.careportal_newnstreatment_eventdate); - timeButton = (Button) view.findViewById(R.id.careportal_newnstreatment_eventtime); + dateButton = (TextView) view.findViewById(R.id.careportal_newnstreatment_eventdate); + timeButton = (TextView) view.findViewById(R.id.careportal_newnstreatment_eventtime); dateButton.setText(DateUtil.dateString(eventTime)); timeButton.setText(DateUtil.timeString(eventTime)); dateButton.setOnClickListener(this); timeButton.setOnClickListener(this); - okButton = (Button) view.findViewById(R.id.ok); - okButton.setOnClickListener(this); - cancelButton = (Button) view.findViewById(R.id.cancel); - cancelButton.setOnClickListener(this); + view.findViewById(R.id.ok).setOnClickListener(this); + view.findViewById(R.id.cancel).setOnClickListener(this); // profile profile = MainApp.getConfigBuilder().getProfile(); - profileStore = MainApp.getConfigBuilder().getActiveProfileInterface().getProfile(); + profileStore = ConfigBuilderPlugin.getActiveProfileInterface().getProfile(); ArrayList profileList; units = profile.getUnits(); profileList = profileStore.getProfileList(); @@ -260,25 +206,20 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick // bg bgUnitsView.setText(units); - // Set BG if not old -// BgReading lastBg = MainApp.getDbHelper().lastBg(); -// Double lastBgValue = 0d; -// if (lastBg != null) { -// lastBgValue = lastBg.valueToUnits(units); -// sensorRadioButton.setChecked(true); -// } else { -// meterRadioButton.setChecked(true); -// } - Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL); - if (profile == null) - editBg = new PlusMinusEditText(view, R.id.careportal_newnstreatment_bginput, R.id.careportal_newnstreatment_bg_plus, R.id.careportal_newnstreatment_bg_minus, bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); - else if (profile.getUnits().equals(Constants.MMOL)) - editBg = new PlusMinusEditText(view, R.id.careportal_newnstreatment_bginput, R.id.careportal_newnstreatment_bg_plus, R.id.careportal_newnstreatment_bg_minus, bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); - else - editBg = new PlusMinusEditText(view, R.id.careportal_newnstreatment_bginput, R.id.careportal_newnstreatment_bg_plus, R.id.careportal_newnstreatment_bg_minus, bg, 0d, 500d, 1d, new DecimalFormat("0"), false); - bgInputEdit.addTextChangedListener(new TextWatcher() { - + editBg = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_bginput); + editTemptarget = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_temptarget); + if (profile == null) { + editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); + editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); + } else if (profile.getUnits().equals(Constants.MMOL)) { + editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); + editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); + } else { + editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false); + editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false); + } + editBg.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { } @@ -289,6 +230,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick if (sensorRadioButton.isChecked()) meterRadioButton.setChecked(true); } }); + sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { @@ -298,21 +240,63 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick }); Integer maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); - editCarbs = new PlusMinusEditText(view, R.id.careportal_newnstreatment_carbsinput, R.id.careportal_newnstreatment_carbs_plus, R.id.careportal_newnstreatment_carbs_minus, 0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); + editCarbs = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbsinput); + editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); - editInsulin = new PlusMinusEditText(view, R.id.careportal_newnstreatment_insulininput, R.id.careportal_newnstreatment_insulin_plus, R.id.careportal_newnstreatment_insulin_minus, 0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); + editInsulin = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_insulininput); + editInsulin.setParams(0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); - editSplit = new PlusMinusEditText(view, R.id.careportal_newnstreatment_splitinput, R.id.careportal_newnstreatment_split_plus, R.id.careportal_newnstreatment_split_minus, 100d, 0d, 100d, 5d, new DecimalFormat("0"), true); - editDuration = new PlusMinusEditText(view, R.id.careportal_newnstreatment_durationinput, R.id.careportal_newnstreatment_duration_plus, R.id.careportal_newnstreatment_duration_minus, 0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false); + editSplit = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_splitinput); + editSplit.setParams(100d, 0d, 100d, 5d, new DecimalFormat("0"), true); + editDuration = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_durationinput); + editDuration.setParams(0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false); Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); - editPercent = new PlusMinusEditText(view, R.id.careportal_newnstreatment_percentinput, R.id.careportal_newnstreatment_percent_plus, R.id.careportal_newnstreatment_percent_minus, 0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true); + editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); + editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true); + editPercent.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + layoutPercent.setVisibility(View.VISIBLE); + layoutAbsolute.setVisibility(View.GONE); + } + }); Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); - editAbsolute = new PlusMinusEditText(view, R.id.careportal_newnstreatment_absoluteinput, R.id.careportal_newnstreatment_absolute_plus, R.id.careportal_newnstreatment_absolute_minus, 0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true); + editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); + editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true); + editAbsolute.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + layoutPercent.setVisibility(View.GONE); + layoutAbsolute.setVisibility(View.VISIBLE); + } + }); + + editCarbTime = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput); + editCarbTime.setParams(0d, -60d, 60d, 5d, new DecimalFormat("0"), false); - editCarbTime = new PlusMinusEditText(view, R.id.careportal_newnstreatment_carbtimeinput, R.id.careportal_newnstreatment_carbtime_plus, R.id.careportal_newnstreatment_carbtime_minus, 0d, -60d, 60d, 5d, new DecimalFormat("0"), false); showOrHide(layoutBg, options.bg); showOrHide(layoutBgSource, options.bg); @@ -329,13 +313,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick 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) { Calendar calendar = Calendar.getInstance(); @@ -353,7 +330,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick dpd.show(context.getFragmentManager(), "Datepickerdialog"); break; case R.id.careportal_newnstreatment_eventtime: - android.text.format.DateFormat df = new android.text.format.DateFormat(); TimePickerDialog tpd = TimePickerDialog.newInstance( this, calendar.get(Calendar.HOUR_OF_DAY), @@ -417,8 +393,8 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick data.put("eventType", "Sensor Start"); break; case R.id.careportal_combobolus: - data.put("splitNow", SafeParse.stringToDouble(splitEdit.getText().toString())); - data.put("splitExt", 100 - SafeParse.stringToDouble(splitEdit.getText().toString())); + data.put("splitNow", SafeParse.stringToDouble(editSplit.getText())); + data.put("splitExt", 100 - SafeParse.stringToDouble(editSplit.getText())); data.put("eventType", CareportalEvent.COMBOBOLUS); break; case R.id.careportal_correctionbolus: @@ -468,42 +444,42 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick data.put("eventType", CareportalEvent.TEMPORARYTARGET); if (!reasonSpinner.getSelectedItem().toString().equals("")) data.put("reason", reasonSpinner.getSelectedItem().toString()); - if (SafeParse.stringToDouble(low.getText().toString()) != 0d) - data.put("targetBottom", SafeParse.stringToDouble(low.getText().toString())); - if (SafeParse.stringToDouble(high.getText().toString()) != 0d) - data.put("targetTop", SafeParse.stringToDouble(high.getText().toString())); + if (SafeParse.stringToDouble(editTemptarget.getText()) != 0d) { + data.put("targetBottom", SafeParse.stringToDouble(editTemptarget.getText())); + data.put("targetTop", SafeParse.stringToDouble(editTemptarget.getText())); + } allowZeroDuration = true; break; } - if (SafeParse.stringToDouble(bgInputEdit.getText().toString()) != 0d) { - data.put("glucose", SafeParse.stringToDouble(bgInputEdit.getText().toString())); + if (SafeParse.stringToDouble(editBg.getText()) != 0d) { + data.put("glucose", SafeParse.stringToDouble(editBg.getText())); if (meterRadioButton.isChecked()) data.put("glucoseType", "Finger"); if (sensorRadioButton.isChecked()) data.put("glucoseType", "Sensor"); if (otherRadioButton.isChecked()) data.put("glucoseType", "Manual"); } - if (SafeParse.stringToDouble(carbsEdit.getText().toString()) != 0d) - data.put("carbs", SafeParse.stringToDouble(carbsEdit.getText().toString())); - if (SafeParse.stringToDouble(insulinEdit.getText().toString()) != 0d) - data.put("insulin", SafeParse.stringToDouble(insulinEdit.getText().toString())); - if (allowZeroDuration || SafeParse.stringToDouble(durationeEdit.getText().toString()) != 0d) - data.put("duration", SafeParse.stringToDouble(durationeEdit.getText().toString())); + if (SafeParse.stringToDouble(editCarbs.getText()) != 0d) + data.put("carbs", SafeParse.stringToDouble(editCarbs.getText())); + if (SafeParse.stringToDouble(editInsulin.getText()) != 0d) + data.put("insulin", SafeParse.stringToDouble(editInsulin.getText())); + if (allowZeroDuration || SafeParse.stringToDouble(editDuration.getText()) != 0d) + data.put("duration", SafeParse.stringToDouble(editDuration.getText())); if (layoutPercent.getVisibility() != View.GONE) - data.put("percent", SafeParse.stringToDouble(percentEdit.getText().toString())); + data.put("percent", SafeParse.stringToDouble(editPercent.getText())); if (layoutAbsolute.getVisibility() != View.GONE) - data.put("absolute", SafeParse.stringToDouble(absoluteEdit.getText().toString())); + data.put("absolute", SafeParse.stringToDouble(editAbsolute.getText())); if (options.profile && profileSpinner.getSelectedItem() != null) data.put("profile", profileSpinner.getSelectedItem().toString()); - if (SafeParse.stringToDouble(carbTimeEdit.getText().toString()) != 0d) - data.put("preBolus", SafeParse.stringToDouble(carbTimeEdit.getText().toString())); + if (SafeParse.stringToDouble(editCarbTime.getText()) != 0d) + data.put("preBolus", SafeParse.stringToDouble(editCarbTime.getText())); if (!notesEdit.getText().toString().equals("")) data.put("notes", notesEdit.getText().toString()); data.put("units", units); if (!enteredBy.equals("")) data.put("enteredBy", enteredBy); if (options.eventType == R.id.careportal_combobolus) { - Double enteredInsulin = SafeParse.stringToDouble(insulinEdit.getText().toString()); + Double enteredInsulin = SafeParse.stringToDouble(editInsulin.getText()); data.put("enteredinsulin", enteredInsulin); - data.put("insulin", enteredInsulin * SafeParse.stringToDouble(splitEdit.getText().toString()) / 100); - data.put("relative", enteredInsulin * (100 - SafeParse.stringToDouble(splitEdit.getText().toString())) / 100 / SafeParse.stringToDouble(durationeEdit.getText().toString()) * 60); + data.put("insulin", enteredInsulin * SafeParse.stringToDouble(editInsulin.getText()) / 100); + data.put("relative", enteredInsulin * (100 - SafeParse.stringToDouble(editSplit.getText())) / 100 / SafeParse.stringToDouble(editDuration.getText()) * 60); } } catch (JSONException e) { e.printStackTrace(); 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 8aa43831f5..9695abf280 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 @@ -34,9 +34,11 @@ import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingPlugin; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.androidaps.plugins.SensitivityOref0.SensitivityOref0Plugin; import info.nightscout.utils.PasswordProtection; @@ -49,6 +51,7 @@ public class ConfigBuilderFragment extends Fragment { } ListView insulinListView; + ListView sensitivityListView; ListView bgsourceListView; TextView bgsourceLabel; ListView pumpListView; @@ -64,13 +67,12 @@ public class ConfigBuilderFragment extends Fragment { ListView constraintsListView; TextView constraintsLabel; ListView generalListView; - TextView nsclientVerView; - TextView nightscoutVerView; LinearLayout mainLayout; Button unlock; PluginCustomAdapter insulinDataAdapter = null; + PluginCustomAdapter sensivityDataAdapter = null; PluginCustomAdapter bgsourceDataAdapter = null; PluginCustomAdapter pumpDataAdapter = null; PluginCustomAdapter loopDataAdapter = null; @@ -96,6 +98,7 @@ public class ConfigBuilderFragment extends Fragment { smallWidth = screen_width < Constants.SMALL_WIDTH; insulinListView = (ListView) view.findViewById(R.id.configbuilder_insulinlistview); + sensitivityListView = (ListView) view.findViewById(R.id.configbuilder_sensitivitylistview); bgsourceListView = (ListView) view.findViewById(R.id.configbuilder_bgsourcelistview); bgsourceLabel = (TextView) view.findViewById(R.id.configbuilder_bgsourcelabel); pumpListView = (ListView) view.findViewById(R.id.configbuilder_pumplistview); @@ -111,17 +114,10 @@ public class ConfigBuilderFragment extends Fragment { constraintsListView = (ListView) view.findViewById(R.id.configbuilder_constraintslistview); constraintsLabel = (TextView) view.findViewById(R.id.configbuilder_constraintslabel); generalListView = (ListView) view.findViewById(R.id.configbuilder_generallistview); - nsclientVerView = (TextView) view.findViewById(R.id.configbuilder_nsclientversion); - nightscoutVerView = (TextView) view.findViewById(R.id.configbuilder_nightscoutversion); mainLayout = (LinearLayout) view.findViewById(R.id.configbuilder_mainlayout); unlock = (Button) view.findViewById(R.id.configbuilder_unlock); - nsclientVerView.setText(ConfigBuilderPlugin.nsClientVersionName); - nightscoutVerView.setText(ConfigBuilderPlugin.nightscoutVersionName); - if (ConfigBuilderPlugin.nsClientVersionCode < 117) nsclientVerView.setTextColor(Color.RED); - if (ConfigBuilderPlugin.nightscoutVersionCode < 900) - nightscoutVerView.setTextColor(Color.RED); setViews(); if (PasswordProtection.isLocked("settings_password")) { @@ -178,6 +174,9 @@ public class ConfigBuilderFragment extends Fragment { setListViewHeightBasedOnChildren(apsListView); if (MainApp.getSpecificPluginsVisibleInList(PluginBase.APS).size() == 0) apsLabel.setVisibility(View.GONE); + sensivityDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(SensitivityInterface.class, PluginBase.SENSITIVITY), PluginBase.SENSITIVITY); + sensitivityListView.setAdapter(sensivityDataAdapter); + setListViewHeightBasedOnChildren(sensitivityListView); constraintsDataAdapter = new PluginCustomAdapter(getContext(), smallWidth?R.layout.configbuilder_smallitem :R.layout.configbuilder_simpleitem, MainApp.getSpecificPluginsVisibleInListByInterface(ConstraintsInterface.class, PluginBase.BGSOURCE), PluginBase.CONSTRAINTS); constraintsListView.setAdapter(constraintsDataAdapter); setListViewHeightBasedOnChildren(constraintsListView); @@ -277,7 +276,7 @@ public class ConfigBuilderFragment extends Fragment { } // Hide enabled control and force enabled plugin if there is only one plugin available - if (type == PluginBase.INSULIN || type == PluginBase.PUMP || type == PluginBase.TREATMENT || type == PluginBase.PROFILE) + if (type == PluginBase.INSULIN || type == PluginBase.PUMP || type == PluginBase.TREATMENT || type == PluginBase.PROFILE || type == PluginBase.SENSITIVITY) if (pluginList.size() < 2) { holder.checkboxEnabled.setEnabled(false); plugin.setFragmentEnabled(type, true); @@ -326,6 +325,9 @@ public class ConfigBuilderFragment extends Fragment { case PluginBase.INSULIN: pluginsInCategory = MainApp.getSpecificPluginsListByInterface(InsulinInterface.class); break; + case PluginBase.SENSITIVITY: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class); + break; case PluginBase.APS: pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class); break; @@ -356,6 +358,8 @@ public class ConfigBuilderFragment extends Fragment { MainApp.getSpecificPlugin(VirtualPumpPlugin.class).setFragmentEnabled(type, true); else if (type == PluginBase.INSULIN) MainApp.getSpecificPlugin(InsulinFastactingPlugin.class).setFragmentEnabled(type, true); + else if (type == PluginBase.SENSITIVITY) + MainApp.getSpecificPlugin(SensitivityOref0Plugin.class).setFragmentEnabled(type, true); else if (type == PluginBase.PROFILE) MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(type, true); else 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 c444603090..28821e0242 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 @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.ConfigBuilder; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.PowerManager; import android.preference.PreferenceManager; @@ -40,16 +41,19 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog; +import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressHelperActivity; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. @@ -64,6 +68,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain private static APSInterface activeAPS; private static LoopPlugin activeLoop; private static InsulinInterface activeInsulin; + private static SensitivityInterface activeSensitivity; static public String nightscoutVersionName = ""; static public Integer nightscoutVersionCode = 0; @@ -212,11 +217,16 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return activePump; } + public static SensitivityInterface getActiveSensitivity() { + return activeSensitivity; + } + void logPluginStatus() { for (PluginBase p : pluginList) { log.debug(p.getName() + ":" + (p.isEnabled(1) ? " GENERAL" : "") + (p.isEnabled(2) ? " TREATMENT" : "") + + (p.isEnabled(3) ? " SENSITIVITY" : "") + (p.isEnabled(4) ? " PROFILE" : "") + (p.isEnabled(5) ? " APS" : "") + (p.isEnabled(6) ? " PUMP" : "") + @@ -255,6 +265,17 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } } + // PluginBase.SENSITIVITY + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class); + activeSensitivity = (SensitivityInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.SENSITIVITY); + if (Config.logConfigBuilder) + log.debug("Selected sensitivity interface: " + ((PluginBase) activeSensitivity).getName()); + for (PluginBase p : pluginsInCategory) { + if (!p.getName().equals(((PluginBase) activeSensitivity).getName())) { + p.setFragmentVisible(PluginBase.SENSITIVITY, false); + } + } + // PluginBase.PROFILE pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE); @@ -410,43 +431,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return 0d; } - /* - public PumpEnactResult deliverTreatmentFromBolusWizard(InsulinInterface insulinType, Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { - mWakeLock.acquire(); - PumpEnactResult result; - insulin = applyBolusConstraints(insulin); - carbs = applyCarbsConstraints(carbs); - - BolusProgressDialog bolusProgressDialog = null; - if (context != null) { - bolusProgressDialog = new BolusProgressDialog(); - bolusProgressDialog.setInsulin(insulin); - bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); - } - - MainApp.bus().post(new EventBolusRequested(insulin)); - - result = activePump.deliverTreatment(insulinType, insulin, carbs, context); - - BolusProgressDialog.bolusEnded = true; - - MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); - - if (result.success) { - Treatment t = new Treatment(insulinType); - t.insulin = result.bolusDelivered; - if (carbTime == 0) - t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout - t.date = System.currentTimeMillis(); - t.mealBolus = result.carbsDelivered > 0; - addToHistoryTreatment(t); - t.carbs = (double) result.carbsDelivered; - NSUpload.uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc); - } - mWakeLock.release(); - return result; - } - */ @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { mWakeLock.acquire(); @@ -459,8 +443,15 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain 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); @@ -472,56 +463,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return result; } - /* - @Override - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { - return deliverTreatment(insulinType, insulin, carbs, context, true); - } - - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context, boolean createTreatment) { - mWakeLock.acquire(); - PumpEnactResult result; - insulin = applyBolusConstraints(insulin); - carbs = applyCarbsConstraints(carbs); - - BolusProgressDialog bolusProgressDialog = null; - if (context != null) { - bolusProgressDialog = new BolusProgressDialog(); - bolusProgressDialog.setInsulin(insulin); - bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); - } else { - Intent i = new Intent(); - i.putExtra("insulin", insulin.doubleValue()); - i.setClass(MainApp.instance(), BolusProgressHelperActivity.class); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - - MainApp.bus().post(new EventBolusRequested(insulin)); - - result = activePump.deliverTreatment(insulinType, insulin, carbs, context); - - BolusProgressDialog.bolusEnded = true; - - MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); - - if (Config.logCongigBuilderActions) - log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); - - if (result.success && createTreatment) { - Treatment t = new Treatment(insulinType); - t.insulin = result.bolusDelivered; - t.carbs = (double) result.carbsDelivered; - t.date = System.currentTimeMillis(); - t.mealBolus = t.carbs > 0; - addToHistoryTreatment(t); - NSUpload.uploadTreatment(t); - } - mWakeLock.release(); - return result; - } - - */ @Override public void stopBolusDelivering() { activePump.stopBolusDelivering(); @@ -1016,19 +957,22 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain if (activeTreatments == null) return null; //app not initialized //log.debug("Profile for: " + new Date(time).toLocaleString() + " : " + getProfileName(time)); - ProfileSwitch profileSwitch = getProfileSwitchFromHistory(time); - if (profileSwitch != null) { - if (profileSwitch.profileJson != null) { - return profileSwitch.getProfileObject(); - } else if (activeProfile.getProfile() != null){ - Profile profile = activeProfile.getProfile().getSpecificProfile(profileSwitch.profileName); - if (profile != null) - return profile; + boolean ignoreProfileSwitchEvents = SP.getBoolean(R.string.key_do_not_track_profile_switch, false); + if (!ignoreProfileSwitchEvents) { + ProfileSwitch profileSwitch = getProfileSwitchFromHistory(time); + if (profileSwitch != null) { + if (profileSwitch.profileJson != null) { + return profileSwitch.getProfileObject(); + } else if (activeProfile.getProfile() != null) { + Profile profile = activeProfile.getProfile().getSpecificProfile(profileSwitch.profileName); + if (profile != null) + return profile; + } } + // Unable to determine profile, failover to default + if (activeProfile.getProfile() == null) + return null; //app not initialized } - // Unable to determine profile, failover to default - if (activeProfile.getProfile() == null) - return null; //app not initialized Profile defaultProfile = activeProfile.getProfile().getDefaultProfile(); if (defaultProfile != null) return defaultProfile; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyFragment.java deleted file mode 100644 index 07583e7622..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyFragment.java +++ /dev/null @@ -1,17 +0,0 @@ -package info.nightscout.androidaps.plugins.ConstraintsSafety; - - -import android.support.v4.app.Fragment; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SafetyFragment extends Fragment { - private static Logger log = LoggerFactory.getLogger(SafetyFragment.class); - - private static SafetyPlugin safetyPlugin = new SafetyPlugin(); - - public static SafetyPlugin getPlugin() { - return safetyPlugin; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java index 213bba47fa..c685cd472d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java @@ -21,9 +21,17 @@ import info.nightscout.utils.SP; public class SafetyPlugin implements PluginBase, ConstraintsInterface { private static Logger log = LoggerFactory.getLogger(SafetyPlugin.class); + static SafetyPlugin plugin = null; + + public static SafetyPlugin getPlugin() { + if (plugin == null) + plugin = new SafetyPlugin(); + return plugin; + } + @Override public String getFragmentClass() { - return SafetyFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java index edf16f36fc..7efcf083b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java @@ -1,28 +1,95 @@ package info.nightscout.androidaps.plugins.IobCobCalculator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; import java.util.Date; +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.SensitivityMK.SensitivityMKPlugin; +import info.nightscout.utils.SP; /** * Created by mike on 25.04.2017. */ public class AutosensData { - long time = 0L; + private static Logger log = LoggerFactory.getLogger(AutosensData.class); + + static class CarbsInPast { + long time = 0L; + double carbs = 0d; + double min5minCarbImpact = 0d; + double remaining = 0d; + + public CarbsInPast(Treatment t) { + time = t.date; + carbs = t.carbs; + remaining = t.carbs; + if (MainApp.getSpecificPlugin(SensitivityMKPlugin.class) != null && MainApp.getSpecificPlugin(SensitivityMKPlugin.class).isEnabled(PluginBase.SENSITIVITY)) { + double maxAbsorptionHours = SP.getDouble(R.string.key_absorption_maxtime, 4d); + Profile profile = MainApp.getConfigBuilder().getProfile(t.date); + double sens = Profile.toMgdl(profile.getIsf(t.date), profile.getUnits()); + double ic = profile.getIc(t.date); + min5minCarbImpact = t.carbs / (maxAbsorptionHours * 60 / 5) * sens / ic; + log.debug("Min 5m carbs impact for " + carbs + "g @" + new Date(t.date).toLocaleString() + " for " + maxAbsorptionHours + "h calculated to " + min5minCarbImpact + " ISF: " + sens + " IC: " + ic); + } else { + min5minCarbImpact = SP.getDouble("openapsama_min_5m_carbimpact", 3.0); + } + } + } + + public long time = 0L; public String pastSensitivity = ""; public double deviation = 0d; - boolean calculateWithDeviation = false; + boolean nonCarbsDeviation = false; + public boolean nonEqualDeviation = false; + List activeCarbsList = new ArrayList<>(); double absorbed = 0d; public double carbsFromBolus = 0d; public double cob = 0; public double bgi = 0d; public double delta = 0d; + public double autosensRatio = 1d; + public String log(long time) { - return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " Bgi=" + bgi + " Deviation=" + deviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob; + return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " Bgi=" + bgi + " Deviation=" + deviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob + " autosensRatio=" + autosensRatio; } public int minOld() { return (int) ((System.currentTimeMillis() - time) / 1000 / 60); } + // remove carbs older than 4h + public void removeOldCarbs(long toTime) { + for (int i = 0; i < activeCarbsList.size(); i++) { + CarbsInPast c = activeCarbsList.get(i); + if (c.time + 4 * 60 * 60 * 1000L < toTime) { + activeCarbsList.remove(i--); + if (c.remaining > 0) + cob -= c.remaining; + log.debug("Removing carbs at "+ new Date(toTime).toLocaleString() + " + after 4h :" + new Date(c.time).toLocaleString()); + } + } + } + + public void substractAbosorbedCarbs() { + double ac = absorbed; + for (int i = 0; i < activeCarbsList.size() && ac > 0; i++) { + CarbsInPast c = activeCarbsList.get(i); + if (c.remaining > 0) { + double sub = Math.min(ac, c.remaining); + c.remaining -= sub; + ac -= sub; + } + } + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java deleted file mode 100644 index db1db95998..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorFragment.java +++ /dev/null @@ -1,16 +0,0 @@ -package info.nightscout.androidaps.plugins.IobCobCalculator; - -import android.support.v4.app.Fragment; - -/** - * Created by adrian on 17/11/16. - */ - -public class IobCobCalculatorFragment extends Fragment { - - private static IobCobCalculatorPlugin iobCobCalculatorPlugin = new IobCobCalculatorPlugin(); - - public static IobCobCalculatorPlugin getPlugin() { - return iobCobCalculatorPlugin; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index 6b26cc00a4..60779e85aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -19,14 +19,18 @@ import java.util.List; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.events.BasalData; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData; @@ -53,7 +57,23 @@ public class IobCobCalculatorPlugin implements PluginBase { private static Handler sHandler = null; private static HandlerThread sHandlerThread = null; - private static Object dataLock = new Object(); + private static final Object dataLock = new Object(); + + private static IobCobCalculatorPlugin plugin = null; + + public static IobCobCalculatorPlugin getPlugin() { + if (plugin == null) + plugin = new IobCobCalculatorPlugin(); + return plugin; + } + + public static LongSparseArray getAutosensDataTable() { + return autosensDataTable; + } + + public static List getBucketedData() { + return bucketed_data; + } @Override public int getType() { @@ -62,7 +82,7 @@ public class IobCobCalculatorPlugin implements PluginBase { @Override public String getFragmentClass() { - return IobCobCalculatorFragment.class.getName(); + return null; } @Override @@ -110,7 +130,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } - public IobCobCalculatorPlugin() { + IobCobCalculatorPlugin() { MainApp.bus().register(this); if (sHandlerThread == null) { sHandlerThread = new HandlerThread(IobCobCalculatorPlugin.class.getSimpleName()); @@ -188,14 +208,14 @@ public class IobCobCalculatorPlugin implements PluginBase { } } - public void createBucketedData() { + private void createBucketedData() { if (isAbout5minData()) createBucketedData5min(); else createBucketedDataRecalculated(); } - public void createBucketedDataRecalculated() { + private void createBucketedDataRecalculated() { synchronized (dataLock) { if (bgReadings == null || bgReadings.size() < 3) { bucketed_data = null; @@ -300,12 +320,14 @@ public class IobCobCalculatorPlugin implements PluginBase { //log.debug("Releasing createBucketedData"); } - public void calculateSensitivityData() { + private void calculateSensitivityData() { if (MainApp.getConfigBuilder() == null) return; // app still initializing if (MainApp.getConfigBuilder().getProfile() == null) return; // app still initializing //log.debug("Locking calculateSensitivityData"); + long oldestTimeWithData = oldestDataAvailable(); + synchronized (dataLock) { if (bucketed_data == null || bucketed_data.size() < 3) { @@ -333,6 +355,10 @@ public class IobCobCalculatorPlugin implements PluginBase { AutosensData autosensData = new AutosensData(); autosensData.time = bgTime; + if (previous != null) + autosensData.activeCarbsList = new ArrayList<>(previous.activeCarbsList); + else + autosensData.activeCarbsList = new ArrayList<>(); //console.error(bgTime , bucketed_data[i].glucose); double bg; @@ -353,17 +379,28 @@ public class IobCobCalculatorPlugin implements PluginBase { List recentTreatments = MainApp.getConfigBuilder().getTreatments5MinBackFromHistory(bgTime); for (int ir = 0; ir < recentTreatments.size(); ir++) { autosensData.carbsFromBolus += recentTreatments.get(ir).carbs; + autosensData.activeCarbsList.add(new AutosensData.CarbsInPast(recentTreatments.get(ir))); } + // if we are absorbing carbs if (previous != null && previous.cob > 0) { + // calculate sum of min carb impact from all active treatments + double totalMinCarbsImpact = 0d; + for (int ii = 0; ii < autosensData.activeCarbsList.size(); ++ii) { + AutosensData.CarbsInPast c = autosensData.activeCarbsList.get(ii); + totalMinCarbsImpact += c.min5minCarbImpact; + } + // figure out how many carbs that represents - // but always assume at least 3mg/dL/5m (default) absorption - double ci = Math.max(deviation, SP.getDouble("openapsama_min_5m_carbimpact", 3.0)); + // but always assume at least 3mg/dL/5m (default) absorption per active treatment + double ci = Math.max(deviation, totalMinCarbsImpact); autosensData.absorbed = ci * profile.getIc(bgTime) / sens; // and add that to the running total carbsAbsorbed autosensData.cob = Math.max(previous.cob - autosensData.absorbed, 0d); + autosensData.substractAbosorbedCarbs(); } + autosensData.removeOldCarbs(bgTime); autosensData.cob += autosensData.carbsFromBolus; autosensData.deviation = deviation; autosensData.bgi = bgi; @@ -373,12 +410,14 @@ public class IobCobCalculatorPlugin implements PluginBase { if (autosensData.cob <= 0) { if (Math.abs(deviation) < Constants.DEVIATION_TO_BE_EQUAL) { autosensData.pastSensitivity += "="; + autosensData.nonEqualDeviation = true; } else if (deviation > 0) { autosensData.pastSensitivity += "+"; + autosensData.nonEqualDeviation = true; } else { autosensData.pastSensitivity += "-"; } - autosensData.calculateWithDeviation = true; + autosensData.nonCarbsDeviation = true; } else { autosensData.pastSensitivity += "C"; } @@ -386,6 +425,7 @@ public class IobCobCalculatorPlugin implements PluginBase { previous = autosensData; autosensDataTable.put(bgTime, autosensData); + autosensData.autosensRatio = detectSensitivity(oldestTimeWithData, bgTime).ratio; if (Config.logAutosensData) log.debug(autosensData.log(bgTime)); } @@ -394,6 +434,15 @@ public class IobCobCalculatorPlugin implements PluginBase { //log.debug("Releasing calculateSensitivityData"); } + public static long oldestDataAvailable() { + long now = System.currentTimeMillis(); + + long oldestDataAvailable = MainApp.getConfigBuilder().oldestDataAvailable(); + long getBGDataFrom = Math.max(oldestDataAvailable, (long) (now - 60 * 60 * 1000L * (24 + MainApp.getConfigBuilder().getProfile().getDia()))); + log.debug("Limiting data to oldest available temps: " + new Date(oldestDataAvailable).toString()); + return getBGDataFrom; + } + public static IobTotal calulateFromTreatmentsAndTemps(long time) { long now = System.currentTimeMillis(); time = roundUpTime(time); @@ -449,6 +498,7 @@ public class IobCobCalculatorPlugin implements PluginBase { return retval; } + @Nullable public static AutosensData getAutosensData(long time) { long now = System.currentTimeMillis(); if (time > now) @@ -467,6 +517,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } } + @Nullable public static AutosensData getLastAutosensData() { if (autosensDataTable.size() < 1) return null; @@ -495,104 +546,15 @@ public class IobCobCalculatorPlugin implements PluginBase { return array; } - public static AutosensResult detectSensitivity(long fromTime) { - //log.debug("Locking detectSensitivity"); + public static AutosensResult detectSensitivityWithLock(long fromTime, long toTime) { synchronized (dataLock) { - if (autosensDataTable == null || autosensDataTable.size() < 4) { - log.debug("No autosens data available"); - return new AutosensResult(); - } - - AutosensData current = getLastAutosensData(); - if (current == null) { - log.debug("No current autosens data available"); - return new AutosensResult(); - } - - - List deviationsArray = new ArrayList<>(); - String pastSensitivity = ""; - int index = 0; - while (index < autosensDataTable.size()) { - AutosensData autosensData = autosensDataTable.valueAt(index); - - if (autosensData.time < fromTime) { - index++; - continue; - } - - if (autosensData.calculateWithDeviation) - deviationsArray.add(autosensData.deviation); - - pastSensitivity += autosensData.pastSensitivity; - int secondsFromMidnight = Profile.secondsFromMidnight(autosensData.time); - if (secondsFromMidnight % 3600 < 2.5 * 60 || secondsFromMidnight % 3600 > 57.5 * 60) { - pastSensitivity += "(" + Math.round(secondsFromMidnight / 3600d) + ")"; - } - index++; - } - - Double[] deviations = new Double[deviationsArray.size()]; - deviations = deviationsArray.toArray(deviations); - - Profile profile = MainApp.getConfigBuilder().getProfile(); - - double sens = profile.getIsf(); - - double ratio = 1; - String ratioLimit = ""; - String sensResult = ""; - - log.debug("Records: " + index + " " + pastSensitivity); - Arrays.sort(deviations); - - for (double i = 0.9; i > 0.1; i = i - 0.02) { - if (percentile(deviations, (i + 0.02)) >= 0 && percentile(deviations, i) < 0) { - log.debug(Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)"); - } - } - double pSensitive = percentile(deviations, 0.50); - double pResistant = percentile(deviations, 0.45); - - double basalOff = 0; - - if (pSensitive < 0) { // sensitive - basalOff = pSensitive * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); - sensResult = "Excess insulin sensitivity detected"; - } else if (pResistant > 0) { // resistant - basalOff = pResistant * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); - sensResult = "Excess insulin resistance detected"; - } else { - sensResult = "Sensitivity normal"; - } - log.debug(sensResult); - ratio = 1 + (basalOff / profile.getMaxDailyBasal()); - - double rawRatio = ratio; - ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_min", "0.7"))); - ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_max", "1.2"))); - - if (ratio != rawRatio) { - ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio; - log.debug(ratioLimit); - } - - double newisf = Math.round(Profile.toMgdl(sens, profile.getUnits()) / ratio); - if (ratio != 1) { - log.debug("ISF adjusted from " + Profile.toMgdl(sens, profile.getUnits()) + " to " + newisf); - } - - AutosensResult output = new AutosensResult(); - output.ratio = Round.roundTo(ratio, 0.01); - output.carbsAbsorbed = Round.roundTo(current.cob, 0.01); - output.pastSensitivity = pastSensitivity; - output.ratioLimit = ratioLimit; - output.sensResult = sensResult; - return output; + return detectSensitivity(fromTime, toTime); } - //log.debug("Releasing detectSensitivity"); } + private static AutosensResult detectSensitivity(long fromTime, long toTime) { + return ConfigBuilderPlugin.getActiveSensitivity().detectSensitivity(fromTime, toTime); + } public static JSONArray convertToJSONArray(IobTotal[] iobArray) { JSONArray array = new JSONArray(); @@ -638,6 +600,41 @@ public class IobCobCalculatorPlugin implements PluginBase { }); } + @Subscribe + public void onStatusEvent(EventPreferenceChange ev) { + if (ev.isChanged(R.string.key_openapsama_autosens_period) || + ev.isChanged(R.string.key_age) || + ev.isChanged(R.string.key_absorption_maxtime) + ) { + synchronized (dataLock) { + log.debug("Invalidating cached data because of preference change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + } + sHandler.post(new Runnable() { + @Override + public void run() { + calculateSensitivityData(); + } + }); + } + } + + @Subscribe + public void onStatusEvent(EventConfigBuilderChange ev) { + synchronized (dataLock) { + log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + } + sHandler.post(new Runnable() { + @Override + public void run() { + calculateSensitivityData(); + } + }); + } + // When historical data is changed (comming from NS etc) finished calculations after this date must be invalidated @Subscribe public void onNewHistoryData(EventNewHistoryData ev) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index f7adaef6a8..5e0c97d23d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -214,14 +214,9 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (!checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.1, 10)) return; if (!checkOnlyHardLimits(pump.getBaseBasalRate(), "current_basal", 0.01, 5)) return; - long oldestDataAvailable = MainApp.getConfigBuilder().oldestDataAvailable(); - long getBGDataFrom = Math.max(oldestDataAvailable, (long) (System.currentTimeMillis() - 60 * 60 * 1000L * (24 + profile.getDia()))); - log.debug("Limiting data to oldest available temps: " + new Date(oldestDataAvailable).toString()); - startPart = new Date(); if (MainApp.getConfigBuilder().isAMAModeEnabled()) { - //lastAutosensResult = Autosens.detectSensitivityandCarbAbsorption(getBGDataFrom, null); - lastAutosensResult = IobCobCalculatorPlugin.detectSensitivity(getBGDataFrom); + lastAutosensResult = IobCobCalculatorPlugin.detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); } 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 c58e462c2a..f536af5860 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 @@ -71,7 +71,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL public void onResume() { super.onResume(); if (getDialog() != null) - getDialog().getWindow().setLayout(1000, ViewGroup.LayoutParams.WRAP_CONTENT); + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); MainApp.bus().register(this); running = true; if (bolusEnded) dismiss(); 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 b50ea56d89..0a1b5417a8 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 @@ -21,7 +21,6 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; -import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; @@ -62,7 +61,6 @@ import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.NumberPicker; import info.nightscout.utils.OKDialog; -import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; @@ -521,7 +519,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com if (calculatedTotalInsulin > 0d || calculatedCarbs > 0d) { String insulinText = calculatedTotalInsulin > 0d ? (DecimalFormatter.to2Decimal(calculatedTotalInsulin) + "U") : ""; String carbsText = calculatedCarbs > 0d ? (DecimalFormatter.to0Decimal(calculatedCarbs) + "g") : ""; - total.setText(getString(R.string.send) + " " + insulinText + " " + carbsText); + total.setText(getString(R.string.result) + ": " + insulinText + " " + carbsText); okButton.setVisibility(View.VISIBLE); } else { total.setText(getString(R.string.missing) + " " + DecimalFormatter.to0Decimal(wizard.carbsEquivalent) + "g"); 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 04ce6f535a..16caf2d6c5 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 @@ -169,6 +169,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, CheckBox showIobView; CheckBox showCobView; CheckBox showDeviationsView; + CheckBox showRatiosView; RecyclerView notificationsView; LinearLayoutManager llm; @@ -284,36 +285,25 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, showIobView = (CheckBox) view.findViewById(R.id.overview_showiob); showCobView = (CheckBox) view.findViewById(R.id.overview_showcob); showDeviationsView = (CheckBox) view.findViewById(R.id.overview_showdeviations); + showRatiosView = (CheckBox) view.findViewById(R.id.overview_showratios); showPredictionView.setChecked(SP.getBoolean("showprediction", false)); showBasalsView.setChecked(SP.getBoolean("showbasals", true)); showIobView.setChecked(SP.getBoolean("showiob", false)); showCobView.setChecked(SP.getBoolean("showcob", false)); showDeviationsView.setChecked(SP.getBoolean("showdeviations", false)); + showRatiosView.setChecked(SP.getBoolean("showratios", false)); showPredictionView.setOnCheckedChangeListener(this); showBasalsView.setOnCheckedChangeListener(this); showIobView.setOnCheckedChangeListener(this); showCobView.setOnCheckedChangeListener(this); showDeviationsView.setOnCheckedChangeListener(this); + showRatiosView.setOnCheckedChangeListener(this); notificationsView = (RecyclerView) view.findViewById(R.id.overview_notifications); notificationsView.setHasFixedSize(true); llm = new LinearLayoutManager(view.getContext()); notificationsView.setLayoutManager(llm); -/* - final LinearLayout graphs = (LinearLayout)view.findViewById(R.id.overview_graphs_layout); - ViewTreeObserver observer = graphs.getViewTreeObserver(); - observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - log.debug("Height: " + graphs.getHeight()); - graphs.getViewTreeObserver().removeGlobalOnLayoutListener( - this); - int heightNeeded = Math.max(320, graphs.getHeight() - 200); - if (heightNeeded != bgGraph.getHeight()) - bgGraph.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heightNeeded)); - } - }); -*/ + bgGraph.getGridLabelRenderer().setGridColor(MainApp.sResources.getColor(R.color.graphgrid)); bgGraph.getGridLabelRenderer().reloadStyles(); iobGraph.getGridLabelRenderer().setGridColor(MainApp.sResources.getColor(R.color.graphgrid)); @@ -323,11 +313,14 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, iobGraph.getGridLabelRenderer().setLabelVerticalWidth(50); iobGraph.getGridLabelRenderer().setNumVerticalLabels(5); + rangeToDisplay = SP.getInt(R.string.key_rangetodisplay, 6); + bgGraph.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { rangeToDisplay += 6; rangeToDisplay = rangeToDisplay > 24 ? 6 : rangeToDisplay; + SP.putInt(R.string.key_rangetodisplay, rangeToDisplay); updateGUI("rangeChange"); return false; } @@ -367,34 +360,29 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()) { case R.id.overview_showprediction: - SP.putBoolean("showprediction", showPredictionView.isChecked()); - updateGUI("onPredictionCheckedChanged"); - break; case R.id.overview_showbasals: - SP.putBoolean("showbasals", showBasalsView.isChecked()); - updateGUI("onBasalsCheckedChanged"); - break; case R.id.overview_showiob: - SP.putBoolean("showiob", showIobView.isChecked()); - updateGUI("onIobCheckedChanged"); break; case R.id.overview_showcob: showDeviationsView.setOnCheckedChangeListener(null); showDeviationsView.setChecked(false); showDeviationsView.setOnCheckedChangeListener(this); - SP.putBoolean("showcob", showCobView.isChecked()); - SP.putBoolean("showdeviations", showDeviationsView.isChecked()); - updateGUI("onCobCheckedChanged"); break; case R.id.overview_showdeviations: showCobView.setOnCheckedChangeListener(null); showCobView.setChecked(false); showCobView.setOnCheckedChangeListener(this); - SP.putBoolean("showcob", showCobView.isChecked()); - SP.putBoolean("showdeviations", showDeviationsView.isChecked()); - updateGUI("onDeviationsCheckedChanged"); + break; + case R.id.overview_showratios: break; } + SP.putBoolean("showiob", showIobView.isChecked()); + SP.putBoolean("showprediction", showPredictionView.isChecked()); + SP.putBoolean("showbasals", showBasalsView.isChecked()); + SP.putBoolean("showcob", showCobView.isChecked()); + SP.putBoolean("showdeviations", showDeviationsView.isChecked()); + SP.putBoolean("showratios", showRatiosView.isChecked()); + scheduleUpdateGUI("onGraphCheckboxesCheckedChanged"); } @Override @@ -1038,7 +1026,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); final OptionsToShow profileswitch = CareportalFragment.profileswitch; profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch); + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); return true; } @@ -1053,7 +1041,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, NewNSTreatmentDialog newTTDialog = new NewNSTreatmentDialog(); final OptionsToShow temptarget = CareportalFragment.temptarget; temptarget.executeTempTarget = true; - newTTDialog.setOptions(temptarget); + newTTDialog.setOptions(temptarget, R.string.careportal_temporarytarget); newTTDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); return true; } @@ -1343,15 +1331,18 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, FixedLineGraphSeries iobSeries; FixedLineGraphSeries cobSeries; BarGraphSeries devSeries; + LineGraphSeries ratioSeries; Double maxIobValueFound = 0d; Double maxCobValueFound = 0d; Double maxDevValueFound = 0d; + Double maxRatioValueFound = 0d; - if (showIobView.isChecked() || showCobView.isChecked() || showDeviationsView.isChecked()) { + if (showIobView.isChecked() || showCobView.isChecked() || showDeviationsView.isChecked() || showRatiosView.isChecked()) { //Date start = new Date(); List iobArray = new ArrayList<>(); List cobArray = new ArrayList<>(); List devArray = new ArrayList<>(); + List ratioArray = new ArrayList<>(); double lastIob = 0; int lastCob = 0; for (long time = fromTime; time <= now; time += 5 * 60 * 1000L) { @@ -1365,7 +1356,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, lastIob = iob; } } - if (showCobView.isChecked() || showDeviationsView.isChecked()) { + if (showCobView.isChecked() || showDeviationsView.isChecked() || showRatiosView.isChecked()) { AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); if (autosensData != null && showCobView.isChecked()) { int cob = (int) autosensData.cob; @@ -1385,6 +1376,10 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, devArray.add(new DeviationDataPoint(time, autosensData.deviation, color)); maxDevValueFound = Math.max(maxDevValueFound, Math.abs(autosensData.deviation)); } + if (autosensData != null && showRatiosView.isChecked()) { + ratioArray.add(new DataPoint(time, autosensData.autosensRatio)); + maxRatioValueFound = Math.max(maxRatioValueFound, Math.abs(autosensData.autosensRatio)); + } } } //Profiler.log(log, "IOB processed", start); @@ -1397,18 +1392,49 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, iobSeries.setThickness(3); - if (showIobView.isChecked() && (showCobView.isChecked() || showDeviationsView.isChecked())) { - List cobArrayRescaled = new ArrayList<>(); - List devArrayRescaled = new ArrayList<>(); - for (int ci = 0; ci < cobArray.size(); ci++) { - cobArrayRescaled.add(new DataPoint(cobArray.get(ci).getX(), cobArray.get(ci).getY() * maxIobValueFound / maxCobValueFound / 2)); - } - for (int ci = 0; ci < devArray.size(); ci++) { - devArrayRescaled.add(new DeviationDataPoint(devArray.get(ci).getX(), devArray.get(ci).getY() * maxIobValueFound / maxDevValueFound, devArray.get(ci).color)); - } - cobArray = cobArrayRescaled; - devArray = devArrayRescaled; + Double maxByScale = null; + int graphsToShow = 0; + if (showIobView.isChecked()) { + if (maxByScale == null) maxByScale = maxIobValueFound; + graphsToShow++; } + if (showCobView.isChecked()) { + if (maxByScale == null) maxByScale = maxCobValueFound; + graphsToShow++; + } + if (showDeviationsView.isChecked()) { + if (maxByScale == null) maxByScale = maxDevValueFound; + graphsToShow++; + } + if (showRatiosView.isChecked()) { + if (maxByScale == null) maxByScale = maxRatioValueFound; + graphsToShow++; + } + + if (graphsToShow > 1) { + if (!maxByScale.equals(maxCobValueFound)) { + List cobArrayRescaled = new ArrayList<>(); + for (int ci = 0; ci < cobArray.size(); ci++) { + cobArrayRescaled.add(new DataPoint(cobArray.get(ci).getX(), cobArray.get(ci).getY() * maxByScale / maxCobValueFound / 2)); + } + cobArray = cobArrayRescaled; + } + if (!maxByScale.equals(maxDevValueFound)) { + List devArrayRescaled = new ArrayList<>(); + for (int ci = 0; ci < devArray.size(); ci++) { + devArrayRescaled.add(new DeviationDataPoint(devArray.get(ci).getX(), devArray.get(ci).getY() * maxByScale / maxDevValueFound, devArray.get(ci).color)); + } + devArray = devArrayRescaled; + } + if (!maxByScale.equals(maxRatioValueFound)) { + List ratioArrayRescaled = new ArrayList<>(); + for (int ci = 0; ci < ratioArray.size(); ci++) { + ratioArrayRescaled.add(new DataPoint(ratioArray.get(ci).getX(), (ratioArray.get(ci).getY() - 1) * maxByScale / maxRatioValueFound)); + } + ratioArray = ratioArrayRescaled; + } + } + // COB DataPoint[] cobData = new DataPoint[cobArray.size()]; cobData = cobArray.toArray(cobData); @@ -1428,9 +1454,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, return data.color; } }); - //devSeries.setBackgroundColor(0xB0FFFFFF & MainApp.sResources.getColor(R.color.cob)); //50% - //devSeries.setColor(MainApp.sResources.getColor(R.color.cob)); - //devSeries.setThickness(3); + + // RATIOS + DataPoint[] ratioData = new DataPoint[ratioArray.size()]; + ratioData = ratioArray.toArray(ratioData); + ratioSeries = new LineGraphSeries<>(ratioData); + ratioSeries.setColor(MainApp.sResources.getColor(R.color.ratio)); + ratioSeries.setThickness(3); iobGraph.getSeries().clear(); @@ -1443,6 +1473,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, if (showDeviationsView.isChecked() && devData.length > 0) { addSeriesWithoutInvalidate(devSeries, iobGraph); } + if (showRatiosView.isChecked() && ratioData.length > 0) { + addSeriesWithoutInvalidate(ratioSeries, iobGraph); + } iobGraph.setVisibility(View.VISIBLE); } else { iobGraph.setVisibility(View.GONE); @@ -1504,7 +1537,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, maxBgValue = Profile.fromMgdlToUnits(maxBgValue, units); maxBgValue = units.equals(Constants.MGDL) ? Round.roundTo(maxBgValue, 40d) + 80 : Round.roundTo(maxBgValue, 2d) + 4; if (highLine > maxBgValue) maxBgValue = highLine; - Integer numOfHorizLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1); + Integer numOfVertLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1); DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()]; bg = bgListArray.toArray(bg); @@ -1572,7 +1605,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, bgGraph.getViewport().setMaxY(maxBgValue); bgGraph.getViewport().setMinY(0); bgGraph.getViewport().setYAxisBoundsManual(true); - bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfHorizLines); + bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfVertLines); // set second scale if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) { @@ -1645,20 +1678,18 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, //Notifications - public static class RecyclerViewAdapter extends RecyclerView.Adapter { + static class RecyclerViewAdapter extends RecyclerView.Adapter { List notificationsList; RecyclerViewAdapter(List notificationsList) { this.notificationsList = notificationsList; - log.debug("RecyclerViewAdapter"); } @Override public NotificationsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.overview_notification_item, viewGroup, false); - NotificationsViewHolder notificationsViewHolder = new NotificationsViewHolder(v); - return notificationsViewHolder; + return new NotificationsViewHolder(v); } @Override @@ -1689,7 +1720,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, super.onAttachedToRecyclerView(recyclerView); } - public static class NotificationsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + static class NotificationsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { CardView cv; TextView time; TextView text; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/TimeAsXAxisLabelFormatter.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/TimeAsXAxisLabelFormatter.java index c8ba40369d..881e472a1d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/TimeAsXAxisLabelFormatter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/TimeAsXAxisLabelFormatter.java @@ -23,10 +23,8 @@ public class TimeAsXAxisLabelFormatter extends DefaultLabelFormatter { public String formatLabel(double value, boolean isValueX) { if (isValueX) { // format as date - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis((long) value); DateFormat dateFormat = new SimpleDateFormat(mFormat); - return dateFormat.format(calendar.getTimeInMillis()); + return dateFormat.format((long) value); } else { return super.formatLabel(value, isValueX); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationFragment.java deleted file mode 100644 index 78e7791c2d..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationFragment.java +++ /dev/null @@ -1,12 +0,0 @@ -package info.nightscout.androidaps.plugins.Persistentnotification; - -import android.support.v4.app.Fragment; - -/** - * Created by adrian on 23/12/16. - */ - -public class PersistentNotificationFragment extends Fragment { - - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java index e64e3375e2..68d1ed487a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java @@ -54,7 +54,7 @@ public class PersistentNotificationPlugin implements PluginBase { @Override public String getFragmentClass() { - return PersistentNotificationFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java index 05cb4b5a40..112ead21fa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfileFragment.java @@ -143,7 +143,7 @@ public class CircadianPercentageProfileFragment extends Fragment { NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); final OptionsToShow profileswitch = CareportalFragment.profileswitch; profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch); + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java index 4080d67ba8..97f32e232a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java @@ -104,7 +104,7 @@ public class LocalProfileFragment extends Fragment { NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); final OptionsToShow profileswitch = CareportalFragment.profileswitch; profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch); + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java index 3490e15873..7d1f829849 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfileFragment.java @@ -102,7 +102,7 @@ public class SimpleProfileFragment extends Fragment { NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); final OptionsToShow profileswitch = CareportalFragment.profileswitch; profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch); + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/activities/DanaRStatsActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/activities/DanaRStatsActivity.java index 808c827ccd..fbe828a25f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/activities/DanaRStatsActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/activities/DanaRStatsActivity.java @@ -35,7 +35,9 @@ import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.Date; +import java.util.LinkedList; import java.util.List; import info.nightscout.androidaps.MainApp; @@ -73,6 +75,7 @@ public class DanaRStatsActivity extends Activity { DecimalFormat decimalFormat; List historyList = new ArrayList<>(); + List dummies; public DanaRStatsActivity() { super(); @@ -302,6 +305,34 @@ public class DanaRStatsActivity extends Activity { private void loadDataFromDB(byte type) { historyList = MainApp.getDbHelper().getDanaRHistoryRecordsByType(type); + //only use newest 10 + historyList = historyList.subList(0, Math.min(10, historyList.size())); + + //fill single gaps + dummies = new LinkedList(); + DateFormat df = new SimpleDateFormat("dd.MM."); + for(int i = 0; i < historyList.size()-1; i++){ + DanaRHistoryRecord elem1 = historyList.get(i); + DanaRHistoryRecord elem2 = historyList.get(i+1); + + if (!df.format(new Date(elem1.recordDate)).equals(df.format(new Date(elem2.recordDate + 25*60*60*1000)))){ + DanaRHistoryRecord dummy = new DanaRHistoryRecord(); + dummy.recordDate = elem1.recordDate - 24*60*60*1000; + dummy.recordDailyBasal = elem1.recordDailyBasal/2; + dummy.recordDailyBolus = elem1.recordDailyBolus/2; + dummies.add(dummy); + elem1.recordDailyBasal /= 2; + elem1.recordDailyBolus /= 2; + } + } + historyList.addAll(dummies); + Collections.sort(historyList, new Comparator() { + @Override + public int compare(DanaRHistoryRecord lhs, DanaRHistoryRecord rhs) { + return (int) (rhs.recordDate-lhs.recordDate); + } + }); + runOnUiThread(new Runnable() { @Override public void run() { @@ -332,6 +363,9 @@ public class DanaRStatsActivity extends Activity { // Create the table row TableRow tr = new TableRow(DanaRStatsActivity.this); if (i % 2 != 0) tr.setBackgroundColor(Color.DKGRAY); + if(dummies.contains(record)){ + tr.setBackgroundColor(Color.argb(125, 255, 0, 0)); + } tr.setId(100 + i); tr.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIFragment.java deleted file mode 100644 index f6221d627e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIFragment.java +++ /dev/null @@ -1,12 +0,0 @@ -package info.nightscout.androidaps.plugins.PumpMDI; - - -import android.support.v4.app.Fragment; - -public class MDIFragment extends Fragment { - private static MDIPlugin mdiPlugin = new MDIPlugin(); - - public static MDIPlugin getPlugin() { - return mdiPlugin; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index b0c55ba036..b51a0bb66e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -12,11 +12,11 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; 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.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.data.Profile; import info.nightscout.utils.DateUtil; /** @@ -30,6 +30,14 @@ public class MDIPlugin implements PluginBase, PumpInterface { PumpDescription pumpDescription = new PumpDescription(); + static MDIPlugin plugin = null; + + public static MDIPlugin getPlugin() { + if (plugin == null) + plugin = new MDIPlugin(); + return plugin; + } + public MDIPlugin() { pumpDescription.isBolusCapable = true; pumpDescription.bolusStep = 0.5d; @@ -42,7 +50,7 @@ public class MDIPlugin implements PluginBase, PumpInterface { @Override public String getFragmentClass() { - return MDIFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityMK/SensitivityMKPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityMK/SensitivityMKPlugin.java new file mode 100644 index 0000000000..5506b2b4c3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityMK/SensitivityMKPlugin.java @@ -0,0 +1,200 @@ +package info.nightscout.androidaps.plugins.SensitivityMK; + +import android.support.v4.util.LongSparseArray; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.SensitivityInterface; +import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; +import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.utils.Round; +import info.nightscout.utils.SP; +import info.nightscout.utils.SafeParse; + +/** + * Created by mike on 24.06.2017. + */ + +public class SensitivityMKPlugin implements PluginBase, SensitivityInterface{ + private static Logger log = LoggerFactory.getLogger(SensitivityMKPlugin.class); + + private static boolean fragmentEnabled = true; + private static boolean fragmentVisible = false; + + static SensitivityMKPlugin plugin = null; + + public static SensitivityMKPlugin getPlugin() { + if (plugin == null) + plugin = new SensitivityMKPlugin(); + return plugin; + } + + @Override + public int getType() { + return SENSITIVITY; + } + + @Override + public String getFragmentClass() { + return null; + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.sensitivitymk); + } + + @Override + public String getNameShort() { + return MainApp.sResources.getString(R.string.sensitivity_shortname); + } + + @Override + public boolean isEnabled(int type) { + return type == SENSITIVITY && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == SENSITIVITY && fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public boolean hasFragment() { + return false; + } + + @Override + public boolean showInList(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == SENSITIVITY) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == SENSITIVITY) this.fragmentVisible = fragmentVisible; + } + + + @Override + public AutosensResult detectSensitivity(long fromTime, long toTime) { + LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getAutosensDataTable(); + + String age = SP.getString(R.string.key_age, ""); + int defaultHours = 24; + if (age.equals(MainApp.sResources.getString(R.string.key_adult))) defaultHours = 24; + if (age.equals(MainApp.sResources.getString(R.string.key_teenage))) defaultHours = 4; + if (age.equals(MainApp.sResources.getString(R.string.key_child))) defaultHours = 4; + int hoursForDetection = SP.getInt(R.string.key_openapsama_autosens_period, defaultHours); + + long now = System.currentTimeMillis(); + + if (autosensDataTable == null || autosensDataTable.size() < 4) { + log.debug("No autosens data available"); + return new AutosensResult(); + } + + AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime); + if (current == null) { + log.debug("No autosens data available"); + return new AutosensResult(); + } + + + List deviationsArray = new ArrayList<>(); + String pastSensitivity = ""; + int index = 0; + while (index < autosensDataTable.size()) { + AutosensData autosensData = autosensDataTable.valueAt(index); + + if (autosensData.time < fromTime) { + index++; + continue; + } + + if (autosensData.time > toTime) { + index++; + continue; + } + + if (autosensData.time > now - hoursForDetection * 60 * 60 * 1000L) + deviationsArray.add(autosensData.nonEqualDeviation ? autosensData.deviation : 0d); + if (deviationsArray.size() > hoursForDetection * 60 / 5) + deviationsArray.remove(0); + + + pastSensitivity += autosensData.pastSensitivity; + int secondsFromMidnight = Profile.secondsFromMidnight(autosensData.time); + if (secondsFromMidnight % 3600 < 2.5 * 60 || secondsFromMidnight % 3600 > 57.5 * 60) { + pastSensitivity += "(" + Math.round(secondsFromMidnight / 3600d) + ")"; + } + index++; + } + + Double[] deviations = new Double[deviationsArray.size()]; + deviations = deviationsArray.toArray(deviations); + + Profile profile = MainApp.getConfigBuilder().getProfile(); + + double sens = profile.getIsf(); + + String ratioLimit = ""; + String sensResult = ""; + + log.debug("Records: " + index + " " + pastSensitivity); + Arrays.sort(deviations); + + double percentile = IobCobCalculatorPlugin.percentile(deviations, 0.50); + double basalOff = percentile * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); + double ratio = 1 + (basalOff / profile.getMaxDailyBasal()); + + if (percentile < 0) { // sensitive + sensResult = "Excess insulin sensitivity detected"; + } else if (percentile > 0) { // resistant + sensResult = "Excess insulin resistance detected"; + } else { + sensResult = "Sensitivity normal"; + } + + log.debug(sensResult); + + double rawRatio = ratio; + ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_min", "0.7"))); + ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_max", "1.2"))); + + if (ratio != rawRatio) { + ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio; + log.debug(ratioLimit); + } + + log.error("Sensitivity to: " + new Date(toTime).toLocaleString() + " percentile: " + percentile); + + AutosensResult output = new AutosensResult(); + output.ratio = Round.roundTo(ratio, 0.01); + output.carbsAbsorbed = Round.roundTo(current.cob, 0.01); + output.pastSensitivity = pastSensitivity; + output.ratioLimit = ratioLimit; + output.sensResult = sensResult; + return output; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java new file mode 100644 index 0000000000..5ccc9107b0 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java @@ -0,0 +1,210 @@ +package info.nightscout.androidaps.plugins.SensitivityOref0; + +import android.support.v4.util.LongSparseArray; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.SensitivityInterface; +import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; +import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; +import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.utils.Round; +import info.nightscout.utils.SP; +import info.nightscout.utils.SafeParse; + +/** + * Created by mike on 24.06.2017. + */ + +public class SensitivityOref0Plugin implements PluginBase, SensitivityInterface { + private static Logger log = LoggerFactory.getLogger(IobCobCalculatorPlugin.class); + + private static boolean fragmentEnabled = true; + private static boolean fragmentVisible = false; + + static SensitivityOref0Plugin plugin = null; + + public static SensitivityOref0Plugin getPlugin() { + if (plugin == null) + plugin = new SensitivityOref0Plugin(); + return plugin; + } + + @Override + public int getType() { + return SENSITIVITY; + } + + @Override + public String getFragmentClass() { + return null; + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.sensitivityoref0); + } + + @Override + public String getNameShort() { + return MainApp.sResources.getString(R.string.sensitivity_shortname); + } + + @Override + public boolean isEnabled(int type) { + return type == SENSITIVITY && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == SENSITIVITY && fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public boolean hasFragment() { + return false; + } + + @Override + public boolean showInList(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == SENSITIVITY) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == SENSITIVITY) this.fragmentVisible = fragmentVisible; + } + + + @Override + public AutosensResult detectSensitivity(long fromTime, long toTime) { + LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getAutosensDataTable(); + + String age = SP.getString(R.string.key_age, ""); + int defaultHours = 24; + if (age.equals(MainApp.sResources.getString(R.string.key_adult))) defaultHours = 24; + if (age.equals(MainApp.sResources.getString(R.string.key_teenage))) defaultHours = 24; + if (age.equals(MainApp.sResources.getString(R.string.key_child))) defaultHours = 24; + int hoursForDetection = SP.getInt(R.string.key_openapsama_autosens_period, defaultHours); + + long now = System.currentTimeMillis(); + + if (autosensDataTable == null || autosensDataTable.size() < 4) { + log.debug("No autosens data available"); + return new AutosensResult(); + } + + AutosensData current = IobCobCalculatorPlugin.getLastAutosensData(); + if (current == null) { + log.debug("No current autosens data available"); + return new AutosensResult(); + } + + + List deviationsArray = new ArrayList<>(); + String pastSensitivity = ""; + int index = 0; + while (index < autosensDataTable.size()) { + AutosensData autosensData = autosensDataTable.valueAt(index); + + if (autosensData.time < fromTime) { + index++; + continue; + } + + if (autosensData.time > toTime) { + index++; + continue; + } + + if (autosensData.time > now - hoursForDetection * 60 * 60 * 1000L) + deviationsArray.add(autosensData.nonEqualDeviation ? autosensData.deviation : 0d); + if (deviationsArray.size() > hoursForDetection * 60 / 5) + deviationsArray.remove(0); + + pastSensitivity += autosensData.pastSensitivity; + int secondsFromMidnight = Profile.secondsFromMidnight(autosensData.time); + if (secondsFromMidnight % 3600 < 2.5 * 60 || secondsFromMidnight % 3600 > 57.5 * 60) { + pastSensitivity += "(" + Math.round(secondsFromMidnight / 3600d) + ")"; + } + index++; + } + + Double[] deviations = new Double[deviationsArray.size()]; + deviations = deviationsArray.toArray(deviations); + + Profile profile = MainApp.getConfigBuilder().getProfile(); + + double sens = profile.getIsf(); + + double ratio = 1; + String ratioLimit = ""; + String sensResult = ""; + + log.debug("Records: " + index + " " + pastSensitivity); + Arrays.sort(deviations); + + for (double i = 0.9; i > 0.1; i = i - 0.02) { + if (IobCobCalculatorPlugin.percentile(deviations, (i + 0.02)) >= 0 && IobCobCalculatorPlugin.percentile(deviations, i) < 0) { + log.debug(Math.round(100 * i) + "% of non-meal deviations negative (target 45%-50%)"); + } + } + double pSensitive = IobCobCalculatorPlugin.percentile(deviations, 0.50); + double pResistant = IobCobCalculatorPlugin.percentile(deviations, 0.45); + + double basalOff = 0; + + if (pSensitive < 0) { // sensitive + basalOff = pSensitive * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); + sensResult = "Excess insulin sensitivity detected"; + } else if (pResistant > 0) { // resistant + basalOff = pResistant * (60 / 5) / Profile.toMgdl(sens, profile.getUnits()); + sensResult = "Excess insulin resistance detected"; + } else { + sensResult = "Sensitivity normal"; + } + log.debug(sensResult); + ratio = 1 + (basalOff / profile.getMaxDailyBasal()); + + double rawRatio = ratio; + ratio = Math.max(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_min", "0.7"))); + ratio = Math.min(ratio, SafeParse.stringToDouble(SP.getString("openapsama_autosens_max", "1.2"))); + + if (ratio != rawRatio) { + ratioLimit = "Ratio limited from " + rawRatio + " to " + ratio; + log.debug(ratioLimit); + } + + double newisf = Math.round(Profile.toMgdl(sens, profile.getUnits()) / ratio); + if (ratio != 1) { + log.debug("ISF adjusted from " + Profile.toMgdl(sens, profile.getUnits()) + " to " + newisf); + } + + AutosensResult output = new AutosensResult(); + output.ratio = Round.roundTo(ratio, 0.01); + output.carbsAbsorbed = Round.roundTo(current.cob, 0.01); + output.pastSensitivity = pastSensitivity; + output.ratioLimit = ratioLimit; + output.sensResult = sensResult; + return output; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpFragment.java deleted file mode 100644 index 63ab27deee..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpFragment.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.nightscout.androidaps.plugins.SourceGlimp; - - -import android.support.v4.app.Fragment; - -public class SourceGlimpFragment extends Fragment { - - private static SourceGlimpPlugin sourceGlimpPlugin = new SourceGlimpPlugin(); - - public static SourceGlimpPlugin getPlugin() { - return sourceGlimpPlugin; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java index 3c1248c4de..0f914feddc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java @@ -4,7 +4,6 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gFragment; /** * Created by mike on 05.08.2016. @@ -12,9 +11,17 @@ import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gFragment; public class SourceGlimpPlugin implements PluginBase, BgSourceInterface { boolean fragmentEnabled = false; + static SourceGlimpPlugin plugin = null; + + public static SourceGlimpPlugin getPlugin() { + if (plugin == null) + plugin = new SourceGlimpPlugin(); + return plugin; + } + @Override public String getFragmentClass() { - return SourceGlimpFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gFragment.java deleted file mode 100644 index 28bfabf813..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gFragment.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.nightscout.androidaps.plugins.SourceMM640g; - - -import android.support.v4.app.Fragment; - -public class SourceMM640gFragment extends Fragment { - - private static SourceMM640gPlugin sourceMM640gPlugin = new SourceMM640gPlugin(); - - public static SourceMM640gPlugin getPlugin() { - return sourceMM640gPlugin; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java index df8111b5cd..fcef6a565b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java @@ -4,7 +4,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; +import info.nightscout.androidaps.plugins.SourceGlimp.SourceGlimpPlugin; /** * Created by mike on 05.08.2016. @@ -12,9 +12,17 @@ import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; public class SourceMM640gPlugin implements PluginBase, BgSourceInterface { boolean fragmentEnabled = false; + static SourceGlimpPlugin plugin = null; + + public static SourceGlimpPlugin getPlugin() { + if (plugin == null) + plugin = new SourceGlimpPlugin(); + return plugin; + } + @Override public String getFragmentClass() { - return SourceMM640gFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientFragment.java deleted file mode 100644 index b2eddc09fc..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientFragment.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.nightscout.androidaps.plugins.SourceNSClient; - - -import android.support.v4.app.Fragment; - -public class SourceNSClientFragment extends Fragment { - - private static SourceNSClientPlugin sourceNSClientPlugin = new SourceNSClientPlugin(); - - public static SourceNSClientPlugin getPlugin() { - return sourceNSClientPlugin; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java index 62ae6b93da..a10d0d1d1e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java @@ -12,9 +12,17 @@ import info.nightscout.androidaps.interfaces.PluginBase; public class SourceNSClientPlugin implements PluginBase, BgSourceInterface { boolean fragmentEnabled = true; + static SourceNSClientPlugin plugin = null; + + public static SourceNSClientPlugin getPlugin() { + if (plugin == null) + plugin = new SourceNSClientPlugin(); + return plugin; + } + @Override public String getFragmentClass() { - return SourceNSClientFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripFragment.java deleted file mode 100644 index c9be1298d2..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripFragment.java +++ /dev/null @@ -1,13 +0,0 @@ -package info.nightscout.androidaps.plugins.SourceXdrip; - - -import android.support.v4.app.Fragment; - -public class SourceXdripFragment extends Fragment { - - private static SourceXdripPlugin sourceXdripPlugin = new SourceXdripPlugin(); - - public static SourceXdripPlugin getPlugin() { - return sourceXdripPlugin; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java index b32040a2e8..6d214dc028 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java @@ -4,16 +4,23 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; /** * Created by mike on 05.08.2016. */ public class SourceXdripPlugin implements PluginBase, BgSourceInterface { + static SourceXdripPlugin plugin = null; + + public static SourceXdripPlugin getPlugin() { + if (plugin == null) + plugin = new SourceXdripPlugin(); + return plugin; + } + @Override public String getFragmentClass() { - return SourceNSClientFragment.class.getName(); + return null; } private static boolean fragmentEnabled = false; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 97fe9070dd..6f38155394 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -182,7 +182,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { Iob tIOB = t.iobCalc(time, dia); total.iob += tIOB.iobContrib; total.activity += tIOB.activityContrib; - Iob bIOB = t.iobCalc(time, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2)); + Iob bIOB = t.iobCalc(time, dia / SP.getDouble("openapsama_bolussnooze_dia_divisor", 2.0)); total.bolussnooze += bIOB.iobContrib; } @@ -242,7 +242,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { List in5minback = new ArrayList<>(); for (Integer pos = 0; pos < treatments.size(); pos++) { Treatment t = treatments.get(pos); - if (t.date <= time && t.date > time - 5 * 60 * 1000) + if (t.date <= time && t.date > time - 5 * 60 * 1000 && t.carbs > 0) in5minback.add(t); } return in5minback; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslineFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslineFragment.java deleted file mode 100644 index ba2cbc0783..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslineFragment.java +++ /dev/null @@ -1,29 +0,0 @@ -package info.nightscout.androidaps.plugins.XDripStatusline; - -import android.content.Context; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import info.nightscout.androidaps.R; - -/** - * Created by adrian on 17/11/16. - */ - -public class StatuslineFragment extends Fragment { - - private static StatuslinePlugin statuslinePlugin; - - public static StatuslinePlugin getPlugin(Context ctx) { - - if (statuslinePlugin == null) { - statuslinePlugin = new StatuslinePlugin(ctx); - } - - return statuslinePlugin; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java index 1d3e427f1b..3f35d2af42 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java @@ -44,6 +44,17 @@ public class StatuslinePlugin implements PluginBase { private final Context ctx; SharedPreferences mPrefs; + private static StatuslinePlugin statuslinePlugin; + + public static StatuslinePlugin getPlugin(Context ctx) { + + if (statuslinePlugin == null) { + statuslinePlugin = new StatuslinePlugin(ctx); + } + + return statuslinePlugin; + } + StatuslinePlugin(Context ctx) { this.ctx = ctx; this.mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); @@ -56,7 +67,7 @@ public class StatuslinePlugin implements PluginBase { @Override public String getFragmentClass() { - return StatuslineFragment.class.getName(); + return null; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java b/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java index b6eaf73ce4..8dff2aec93 100644 --- a/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java @@ -59,7 +59,7 @@ public class TabPageAdapter extends FragmentStatePagerAdapter { } public void registerNewFragment(PluginBase plugin) { - if (plugin.isVisibleInTabs(plugin.getType())) { + if (plugin.hasFragment() && plugin.isVisibleInTabs(plugin.getType())) { visibleFragmentList.add(plugin); notifyDataSetChanged(); } diff --git a/app/src/main/java/info/nightscout/utils/SP.java b/app/src/main/java/info/nightscout/utils/SP.java index 4733c42d04..acb727d864 100644 --- a/app/src/main/java/info/nightscout/utils/SP.java +++ b/app/src/main/java/info/nightscout/utils/SP.java @@ -49,11 +49,19 @@ public class SP { } static public int getInt(int resourceID, Integer defaultValue) { - return SafeParse.stringToInt(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString())); + try { + return sharedPreferences.getInt(MainApp.sResources.getString(resourceID), defaultValue); + } catch (Exception e) { + return SafeParse.stringToInt(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString())); + } } static public int getInt(String key, Integer defaultValue) { - return SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString())); + try { + return sharedPreferences.getInt(key, defaultValue); + } catch (Exception e) { + return SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString())); + } } static public long getLong(int resourceID, Long defaultValue) { @@ -92,6 +100,24 @@ public class SP { editor.apply(); } + static public void putLong(int resourceID, long value) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putLong(MainApp.sResources.getString(resourceID), value); + editor.apply(); + } + + static public void putInt(String key, int value) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putInt(key, value); + editor.apply(); + } + + static public void putInt(int resourceID, int value) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putInt(MainApp.sResources.getString(resourceID), value); + editor.apply(); + } + static public void putString(int resourceID, String value) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(MainApp.sResources.getString(resourceID), value); diff --git a/app/src/main/res/layout/careportal_newnstreatment_dialog.xml b/app/src/main/res/layout/careportal_newnstreatment_dialog.xml index 59c43c3cad..5128262950 100644 --- a/app/src/main/res/layout/careportal_newnstreatment_dialog.xml +++ b/app/src/main/res/layout/careportal_newnstreatment_dialog.xml @@ -17,72 +17,49 @@ android:layout_height="wrap_content" android:orientation="vertical"> - + + + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - + - + + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - - - - - - - - - - - + - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - + android:layout_centerInParent="true" + android:layout_gravity="center_vertical" /> - - - + - - - - - - - - - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> + android:minWidth="120dp" + android:orientation="vertical" + android:padding="10dp"> - + android:text="@string/target_range" + android:textAppearance="@android:style/TextAppearance.Material.Small" + android:textStyle="bold" /> - + android:layout_centerHorizontal="true" /> - + - + - + android:layout_height="wrap_content" + android:orientation="horizontal"> @@ -813,61 +417,44 @@ android:id="@+id/careportal_newnstreatment_notes" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_centerHorizontal="true" - android:width="200dp" /> + android:width="180dp" /> - + - - - + android:layout_height="wrap_content" + android:orientation="horizontal"> - + android:layout_gravity="center_horizontal" + android:padding="10dp" + android:text="2017/05/05" /> -