From d74d54931bdbe7817d939b090c9c41e39ac13cb2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 17 Jun 2016 14:42:02 +0200 Subject: [PATCH] save state on openapsma --- .../nightscout/androidaps/MainActivity.java | 38 ++++---- .../androidaps/db/DatabaseHelper.java | 33 ++++++- .../LowSuspend/LowSuspendFragment.java | 9 +- .../OpenAPSMA/DetermineBasalAdapterJS.java | 73 +++++++++++++-- .../plugins/OpenAPSMA/OpenAPSMAFragment.java | 91 +++++++++++++++---- .../plugins/Overview/OverviewFragment.java | 6 +- build.gradle | 2 +- 7 files changed, 201 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 4258eafa95..f45aa9c0ae 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -38,7 +38,7 @@ public class MainActivity extends AppCompatActivity { private ViewPager mPager; private static TabPageAdapter pageAdapter; - private static ArrayList pluginsList = new ArrayList(); + private static ArrayList pluginsList = null; private static ConfigBuilderFragment configBuilderFragment; @@ -53,25 +53,27 @@ public class MainActivity extends AppCompatActivity { if (Config.logFunctionCalls) log.debug("onCreate"); - // Register all tabs in app here - pluginsList.add(OverviewFragment.newInstance()); - pluginsList.add(VirtualPumpFragment.newInstance()); - pluginsList.add(LowSuspendFragment.newInstance()); - pluginsList.add(OpenAPSMAFragment.newInstance()); - pluginsList.add(NSProfileViewerFragment.newInstance()); - pluginsList.add(SimpleProfileFragment.newInstance()); - pluginsList.add(TreatmentsFragment.newInstance()); - pluginsList.add(TempBasalsFragment.newInstance()); - pluginsList.add(ObjectivesFragment.newInstance()); - pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance()); + if (pluginsList == null) { + pluginsList = new ArrayList(); + // Register all tabs in app here + pluginsList.add(OverviewFragment.newInstance()); + pluginsList.add(VirtualPumpFragment.newInstance()); + pluginsList.add(LowSuspendFragment.newInstance()); + pluginsList.add(OpenAPSMAFragment.newInstance()); + pluginsList.add(NSProfileViewerFragment.newInstance()); + pluginsList.add(SimpleProfileFragment.newInstance()); + pluginsList.add(TreatmentsFragment.newInstance()); + pluginsList.add(TempBasalsFragment.newInstance()); + pluginsList.add(ObjectivesFragment.newInstance()); + pluginsList.add(configBuilderFragment = ConfigBuilderFragment.newInstance()); + toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); - toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + registerBus(); - registerBus(); - - configBuilderFragment.initialize(); - setUpTabs(false); + configBuilderFragment.initialize(); + setUpTabs(false); + } } @Subscribe diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 56b3db443d..6da9b48a8b 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -8,6 +8,8 @@ import java.util.List; import android.content.Context; import android.database.sqlite.SQLiteDatabase; +import android.os.Parcel; +import android.os.Parcelable; import android.support.annotation.Nullable; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; @@ -168,7 +170,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { /* * Returns glucose_status for openAPS or null if no actual data available */ - public static class GlucoseStatus { + public static class GlucoseStatus implements Parcelable { public double glucose = 0d; public double delta = 0d; public double avgdelta = 0d; @@ -183,6 +185,35 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { context.getString(R.string.delta) + " " + formatNumber0decimalplaces.format(delta) + "\n" + context.getString(R.string.avgdelta) + " " + formatNumber2decimalplaces.format(avgdelta); } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeDouble(avgdelta); + dest.writeDouble(delta); + dest.writeDouble(glucose); + } + public final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public GlucoseStatus createFromParcel(Parcel in) { + return new GlucoseStatus(in); + } + + public GlucoseStatus[] newArray(int size) { + return new GlucoseStatus[size]; + } + }; + + private GlucoseStatus(Parcel in) { + avgdelta = in.readDouble(); + delta = in.readDouble(); + glucose = in.readDouble(); + } + + public GlucoseStatus() {} } @Nullable diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java index bfc2ec127c..1b1733ffbe 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java @@ -65,9 +65,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener dest.writeInt(lastLowProjected ? 1 : 0); dest.writeDouble(lastMinBg); dest.writeString(lastUnits); - dest.writeDouble(lastGlucoseStatus.avgdelta); - dest.writeDouble(lastGlucoseStatus.delta); - dest.writeDouble(lastGlucoseStatus.glucose); + dest.writeParcelable(lastGlucoseStatus, 0); dest.writeLong(lastAPSRun.getTime()); dest.writeParcelable(lastAPSResult, 0); } @@ -87,10 +85,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener lastLowProjected = in.readInt() == 1; lastMinBg = in.readDouble(); lastUnits = in.readString(); - lastGlucoseStatus = new DatabaseHelper.GlucoseStatus(); - lastGlucoseStatus.avgdelta = in.readDouble(); - lastGlucoseStatus.delta = in.readDouble(); - lastGlucoseStatus.glucose = in.readDouble(); + lastGlucoseStatus = in.readParcelable(DatabaseHelper.GlucoseStatus.class.getClassLoader()); lastAPSRun = new Date(in.readLong()); lastAPSResult = in.readParcelable(APSResult.class.getClassLoader()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java index 4b029f5cd5..de09782ddd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java @@ -1,5 +1,8 @@ package info.nightscout.androidaps.plugins.OpenAPSMA; +import android.os.Parcel; +import android.os.Parcelable; + import com.eclipsesource.v8.JavaVoidCallback; import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Array; @@ -19,11 +22,11 @@ import info.nightscout.androidaps.plugins.ScriptReader; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.client.data.NSProfile; -public class DetermineBasalAdapterJS { +public class DetermineBasalAdapterJS implements Parcelable { private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterJS.class); - private final ScriptReader mScriptReader; + private ScriptReader mScriptReader = null; V8 mV8rt; private V8Object mProfile; private V8Object mGlucoseStatus; @@ -37,6 +40,54 @@ public class DetermineBasalAdapterJS { private final String PARAM_profile = "profile"; private final String PARAM_meal_data = "meal_data"; + private String storedCurrentTemp = null; + private String storedIobData = null; + private String storedGlucoseStatus = null; + private String storedProfile = null; + private String storedMeal_data = null; + + /** + * Parcelable implementation + * result string for display only + **/ + protected DetermineBasalAdapterJS(Parcel in) { + storedCurrentTemp = in.readString(); + storedIobData = in.readString(); + storedGlucoseStatus = in.readString(); + storedProfile = in.readString(); + storedMeal_data = in.readString(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(storedCurrentTemp); + dest.writeString(storedIobData); + dest.writeString(storedGlucoseStatus); + dest.writeString(storedProfile); + dest.writeString(storedMeal_data); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public DetermineBasalAdapterJS createFromParcel(Parcel in) { + return new DetermineBasalAdapterJS(in); + } + + @Override + public DetermineBasalAdapterJS[] newArray(int size) { + return new DetermineBasalAdapterJS[size]; + } + }; + + /** + * Main code + */ + public DetermineBasalAdapterJS(ScriptReader scriptReader) throws IOException { mV8rt = V8.createV8Runtime(); mScriptReader = scriptReader; @@ -125,27 +176,35 @@ public class DetermineBasalAdapterJS { } catch (JSONException e) { e.printStackTrace(); } + + // Store input params for Parcelable + storedGlucoseStatus = mV8rt.executeStringScript("JSON.stringify(" + PARAM_glucoseStatus + ");"); + storedIobData = mV8rt.executeStringScript("JSON.stringify(" + PARAM_iobData + ");"); + storedCurrentTemp = mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"); + storedProfile = mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"); + storedMeal_data = mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"); + return result; } String getGlucoseStatusParam() { - return mV8rt.executeStringScript("JSON.stringify(" + PARAM_glucoseStatus + ");"); + return storedGlucoseStatus; } String getCurrentTempParam() { - return mV8rt.executeStringScript("JSON.stringify(" + PARAM_currentTemp + ");"); + return storedCurrentTemp; } String getIobDataParam() { - return mV8rt.executeStringScript("JSON.stringify(" + PARAM_iobData + ");"); + return storedIobData; } String getProfileParam() { - return mV8rt.executeStringScript("JSON.stringify(" + PARAM_profile + ");"); + return storedProfile; } String getMealDataParam() { - return mV8rt.executeStringScript("JSON.stringify(" + PARAM_meal_data + ");"); + return storedMeal_data; } private void loadScript() throws IOException { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java index 6a09ff978e..52337b82bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java @@ -3,6 +3,8 @@ package info.nightscout.androidaps.plugins.OpenAPSMA; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -53,8 +55,45 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, TextView resultView; TextView requestView; - Date lastAPSRun = null; - APSResult lastAPSResult = null; + // last values + class LastRun implements Parcelable { + DetermineBasalAdapterJS lastDetermineBasalAdapterJS = null; + Date lastAPSRun = null; + DetermineBasalResult lastAPSResult = null; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeParcelable(lastDetermineBasalAdapterJS, 0); + dest.writeParcelable(lastAPSResult, 0); + dest.writeLong(lastAPSRun.getTime()); + dest.writeParcelable(lastAPSResult, 0); + } + public final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public LastRun createFromParcel(Parcel in) { + return new LastRun(in); + } + + public LastRun[] newArray(int size) { + return new LastRun[size]; + } + }; + + private LastRun(Parcel in) { + lastDetermineBasalAdapterJS = in.readParcelable(DetermineBasalAdapterJS.class.getClassLoader()); + lastAPSResult = in.readParcelable(DetermineBasalResult.class.getClassLoader()); + lastAPSRun = new Date(in.readLong()); + lastAPSResult = in.readParcelable(APSResult.class.getClassLoader()); + } + + public LastRun() {} + } + + LastRun lastRun = null; boolean fragmentEnabled = false; boolean fragmentVisible = true; @@ -101,12 +140,12 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, @Override public APSResult getLastAPSResult() { - return lastAPSResult; + return lastRun.lastAPSResult; } @Override public Date getLastAPSRun() { - return lastAPSRun; + return lastRun.lastAPSRun; } public static OpenAPSMAFragment newInstance() { @@ -135,9 +174,19 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, resultView = (TextView) view.findViewById(R.id.openapsma_result); requestView = (TextView) view.findViewById(R.id.openapsma_request); + if (savedInstanceState != null) { + lastRun = savedInstanceState.getParcelable("lastrun"); + } + updateGUI(); return view; } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putParcelable("lastrun", lastRun); + } + private void registerBus() { try { MainApp.bus().unregister(this); @@ -227,6 +276,8 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, minBgDefault = "5"; } + Date now = new Date(); + // TODO: objectives limits double maxIob = Double.parseDouble(SP.getString("max_iob", "1.5").replace(",", ".")); double maxBasal = Double.parseDouble(SP.getString("max_basal", "1").replace(",", ".")); @@ -247,29 +298,37 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, determineBasalAdapterJS.setData(profile, maxIob, maxBasal, minBg, maxBg, pump, iobTotal, glucoseStatus, mealData); - glucoseStatusView.setText(determineBasalAdapterJS.getGlucoseStatusParam()); - currentTempView.setText(determineBasalAdapterJS.getCurrentTempParam()); - iobDataView.setText(determineBasalAdapterJS.getIobDataParam()); - profileView.setText(determineBasalAdapterJS.getProfileParam()); - mealDataView.setText(determineBasalAdapterJS.getMealDataParam()); DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke(); - resultView.setText(determineBasalResult.json.toString()); - requestView.setText(determineBasalResult.toString()); - lastRunView.setText(new Date().toLocaleString()); - determineBasalAdapterJS.release(); try { - determineBasalResult.json.put("timestamp", DateUtil.toISOString(new Date())); + determineBasalResult.json.put("timestamp", DateUtil.toISOString(now)); } catch (JSONException e) { e.printStackTrace(); } - lastAPSResult = determineBasalResult; - lastAPSRun = new Date(); + + lastRun = new LastRun(); + lastRun.lastDetermineBasalAdapterJS = determineBasalAdapterJS; + lastRun.lastAPSResult = determineBasalResult; + lastRun.lastAPSRun = now; + updateGUI(); //deviceStatus.suggested = determineBasalResult.json; + } + + void updateGUI() { + if (lastRun != null) { + glucoseStatusView.setText(lastRun.lastDetermineBasalAdapterJS.getGlucoseStatusParam()); + currentTempView.setText(lastRun.lastDetermineBasalAdapterJS.getCurrentTempParam()); + iobDataView.setText(lastRun.lastDetermineBasalAdapterJS.getIobDataParam()); + profileView.setText(lastRun.lastDetermineBasalAdapterJS.getProfileParam()); + mealDataView.setText(lastRun.lastDetermineBasalAdapterJS.getMealDataParam()); + resultView.setText(lastRun.lastAPSResult.json.toString()); + requestView.setText(lastRun.lastAPSResult.toString()); + lastRunView.setText(lastRun.lastAPSRun.toLocaleString()); + } } } 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 1ff3b6b0ac..78a819bed2 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 @@ -268,8 +268,12 @@ public class OverviewFragment extends Fragment implements PluginBase { public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); - if (isVisibleToUser) + if (isVisibleToUser) { updateData(); + log.debug("Overview visible"); + } else { + log.debug("Overview hidden"); + } } } diff --git a/build.gradle b/build.gradle index 03bced9f3f..aff4f415e9 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files