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 bcdd5ab252..53ceed17a8 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 @@ -2,7 +2,9 @@ package info.nightscout.androidaps.plugins.ProfileCircadianPercentage; import android.app.Activity; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; @@ -123,6 +125,9 @@ public class CircadianPercentageProfileFragment extends SubscriberFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + + showDeprecatedDialog(); + View layout = inflater.inflate(R.layout.circadianpercentageprofile_fragment, container, false); fl = (FrameLayout) layout.findViewById(R.id.circadianpercentageprofile_framelayout); fl.requestFocusFromTouch(); @@ -230,7 +235,7 @@ public class CircadianPercentageProfileFragment extends SubscriberFragment { } }); - timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + /*timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { @@ -266,7 +271,7 @@ public class CircadianPercentageProfileFragment extends SubscriberFragment { } } } - }); + });*/ diaView.setOnFocusChangeListener(new View.OnFocusChangeListener() { @@ -315,6 +320,25 @@ public class CircadianPercentageProfileFragment extends SubscriberFragment { return layout; } + private void showDeprecatedDialog() { + AlertDialog.Builder adb = new AlertDialog.Builder(getContext()); + adb.setTitle("DEPRECATED! Please migrate!"); + adb.setMessage("CircadianPercentageProfile has been deprecated. " + + "It is recommended to migrate to LocalProfile.\n\n" + + "Good news: You won't lose any functionality! Percentage and Timeshift have been ported to the ProfileSwitch :) \n\n " + + "How to migrate:\n" + + "1) Press MIGRATE, the system will automatically fill the LocalProfile for you.\n" + + "2) Switch to LocalProfile in the ConfigBuilder\n" + + "3) CHECK that all settings are correct in the LocalProfile!!!"); + adb.setIcon(android.R.drawable.ic_dialog_alert); + adb.setPositiveButton("MIGRATE", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + CircadianPercentageProfilePlugin.migrateToLP(); + } }); + adb.setNegativeButton("Cancel", null); + adb.show(); + } + public void updateGUI() { updateProfileInfo(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfilePlugin.java index e98a949f3e..1fcec2b9ff 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileCircadianPercentage/CircadianPercentageProfilePlugin.java @@ -21,6 +21,8 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.ProfileLocal.LocalProfilePlugin; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.NSUpload; import info.nightscout.utils.SP; @@ -211,6 +213,90 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte return msg; } + public static void migrateToLP(){ + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = settings.edit(); + editor.putBoolean("LocalProfile" + "mmol", SP.getBoolean(SETTINGS_PREFIX + "mmol", false)); + editor.putBoolean("LocalProfile" + "mgdl", SP.getBoolean(SETTINGS_PREFIX + "mgdl", true)); + editor.putString("LocalProfile" + "dia", "" + SP.getDouble(SETTINGS_PREFIX + "dia", Constants.defaultDIA)); + editor.putString("LocalProfile" + "ic", getLPisf()); + editor.putString("LocalProfile" + "isf", getLPisf()); + editor.putString("LocalProfile" + "basal", getLPbasal()); + try { + JSONArray targetLow = new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", SP.getDouble(SETTINGS_PREFIX + "targetlow", 120d))); + JSONArray targetHigh = new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", SP.getDouble(SETTINGS_PREFIX + "targethigh", 120d))); + editor.putString("LocalProfile" + "targetlow", targetLow.toString()); + editor.putString("LocalProfile" + "targethigh", targetHigh.toString()); + } catch (JSONException e) { + e.printStackTrace(); + } + editor.commit(); + LocalProfilePlugin lp = MainApp.getSpecificPlugin(LocalProfilePlugin.class); + lp.loadSettings(); + + /* TODO: remove Settings and switch to LP later on + * For now only nag the user every time (s)he opens the CPP fragment and offer to migrate. + * Keep settings for now in order to allow the user to check that the migration went well. + */ + //removeSettings(); + + } + + public static String getLPisf(){ + return getLPConversion("baseisf", 35d); + } + + public static String getLPic(){ + return getLPConversion("baseic", 4); + } + + public static String getLPbasal(){ + return getLPConversion("basebasal", 1); + } + + public static String getLPConversion(String type, double defaultValue){ + try { + JSONArray jsonArray = new JSONArray(); + double last = -1d; + for (int i = 0; i < 24; i++) { + double value = SP.getDouble(SETTINGS_PREFIX + type + i, defaultValue); + String time; + DecimalFormat df = new DecimalFormat("00"); + time = df.format(i) + ":00"; + if(last != value) { + jsonArray.put(new JSONObject().put("time", time).put("timeAsSeconds", i * 60 * 60).put("value", value)); + } + last = value; + return jsonArray.toString(); + } + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + return LocalProfilePlugin.DEFAULTARRAY; + } + + static void removeSettings() { + if (Config.logPrefsChange) + log.debug("Removing settings"); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = settings.edit(); + editor.remove(SETTINGS_PREFIX + "mmol"); + editor.remove(SETTINGS_PREFIX + "mgdl"); + editor.remove(SETTINGS_PREFIX + "dia"); + editor.remove(SETTINGS_PREFIX + "targetlow"); + editor.remove(SETTINGS_PREFIX + "targethigh"); + editor.remove(SETTINGS_PREFIX + "timeshift"); + editor.remove(SETTINGS_PREFIX + "percentage"); + + + for (int i = 0; i < 24; i++) { + editor.remove(SETTINGS_PREFIX + "basebasal"); + editor.remove(SETTINGS_PREFIX + "baseisf" + i); + editor.remove(SETTINGS_PREFIX + "baseic" + i); + } + editor.commit(); + } + private void createConvertedProfile() { JSONObject json = new JSONObject(); JSONObject store = new JSONObject(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java index 2a2d45e2ef..ec4318da01 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java @@ -30,7 +30,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { private ProfileStore convertedProfile = null; private String convertedProfileName = null; - final private String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]"; + public static final String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]"; boolean mgdl; boolean mmol; @@ -124,7 +124,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { log.debug("Storing settings: " + getProfile().getData().toString()); } - private void loadSettings() { + public void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings");