From 8a374f1b3533890c3bc0bed52535b6c4e7d75473 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 17 Feb 2017 16:16:20 +0100 Subject: [PATCH] Shared preferences cleanup --- .../info/nightscout/androidaps/Constants.java | 12 +- .../nightscout/androidaps/MainActivity.java | 4 +- .../androidaps/PreferencesActivity.java | 2 +- .../events/EventConfigBuilderChange.java | 8 ++ .../events/EventPreferenceChange.java | 14 +++ .../plugins/Actions/dialogs/FillDialog.java | 8 +- .../Dialogs/NewNSTreatmentDialog.java | 7 +- .../CircadianPercentageProfilePlugin.java | 93 +++----------- .../ConfigBuilder/ConfigBuilderFragment.java | 2 + .../plugins/DanaR/DanaRFragment.java | 6 - .../androidaps/plugins/DanaR/DanaRPump.java | 10 +- .../DanaR/Services/ExecutionService.java | 6 +- .../DanaRKorean/DanaRKoreanFragment.java | 6 - .../plugins/DanaRKorean/DanaRKoreanPump.java | 10 +- .../Services/ExecutionService.java | 11 +- .../LocalProfile/LocalProfilePlugin.java | 118 ++++-------------- .../NSClientInternalFragment.java | 12 +- .../NSClientInternalPlugin.java | 2 +- .../plugins/NSClientInternal/UploadQueue.java | 8 +- .../services/NSClientService.java | 29 ++++- .../plugins/NSProfile/NSProfilePlugin.java | 8 +- .../plugins/Objectives/ObjectivesPlugin.java | 12 +- .../plugins/OpenAPSAMA/Autosens.java | 7 +- .../DetermineBasalAdapterAMAJS.java | 10 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 18 +-- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 21 ++-- .../plugins/Overview/OverviewFragment.java | 18 +-- .../plugins/Overview/OverviewPlugin.java | 7 +- .../plugins/SafetyFragment/SafetyPlugin.java | 26 ++-- .../SimpleProfile/SimpleProfilePlugin.java | 68 ++-------- .../SmsCommunicatorPlugin.java | 16 ++- .../plugins/Treatments/TreatmentsPlugin.java | 2 +- .../plugins/Wear/ActionStringHandler.java | 24 ++-- .../info/nightscout/utils/LocaleHelper.java | 3 +- .../nightscout/utils/PasswordProtection.java | 4 - .../main/java/info/nightscout/utils/SP.java | 72 +++++++++++ app/src/main/res/values-bg/strings.xml | 2 +- app/src/main/res/values-cs/strings.xml | 2 +- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-ko/strings.xml | 2 +- app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/pref_danar.xml | 8 +- app/src/main/res/xml/pref_danarprofile.xml | 2 +- 44 files changed, 299 insertions(+), 411 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java create mode 100644 app/src/main/java/info/nightscout/utils/SP.java diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 2873d29611..da0feef01f 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -32,12 +32,12 @@ public class Constants { public static final int CPP_MAX_PERCENTAGE = 200; // Defaults for settings - public static final String MAX_BG_DEFAULT_MGDL = "180"; - public static final String MAX_BG_DEFAULT_MMOL = "10"; - public static final String MIN_BG_DEFAULT_MGDL = "100"; - public static final String MIN_BG_DEFAULT_MMOL = "5"; - public static final String TARGET_BG_DEFAULT_MGDL = "150"; - public static final String TARGET_BG_DEFAULT_MMOL = "7"; + public static final Double MAX_BG_DEFAULT_MGDL = 180d; + public static final Double MAX_BG_DEFAULT_MMOL = 10d; + public static final Double MIN_BG_DEFAULT_MGDL = 100d; + public static final Double MIN_BG_DEFAULT_MMOL = 5d; + public static final Double TARGET_BG_DEFAULT_MGDL = 150d; + public static final Double TARGET_BG_DEFAULT_MMOL = 7d; // Very Hard Limits Ranges // First value is the Lowest and second value is the Highest a Limit can define diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 36931ccd5b..603b0e2699 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -36,10 +36,11 @@ import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.tabs.SlidingTabLayout; import info.nightscout.androidaps.tabs.TabPageAdapter; -import info.nightscout.utils.LogDialog; import info.nightscout.utils.ImportExportPrefs; import info.nightscout.utils.LocaleHelper; +import info.nightscout.utils.LogDialog; import info.nightscout.utils.PasswordProtection; +import info.nightscout.utils.SP; public class MainActivity extends AppCompatActivity { private static Logger log = LoggerFactory.getLogger(MainActivity.class); @@ -86,7 +87,6 @@ public class MainActivity extends AppCompatActivity { @Subscribe public void onStatusEvent(final EventRefreshGui ev) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String lang = SP.getString("language", "en"); LocaleHelper.setLocale(getApplicationContext(), lang); runOnUiThread(new Runnable() { diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 02169ac473..d04615fc2f 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -36,7 +36,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - MainApp.bus().post(new EventPreferenceChange()); + MainApp.bus().post(new EventPreferenceChange(key)); if (key.equals("language")) { String lang = sharedPreferences.getString("language", "en"); LocaleHelper.setLocale(getApplicationContext(), lang); diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java new file mode 100644 index 0000000000..38331ca581 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.events; + +/** + * Created by mike on 17.02.2017. + */ + +public class EventConfigBuilderChange { +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java index b4b430bb1e..f8b38a4321 100644 --- a/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java +++ b/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java @@ -1,7 +1,21 @@ package info.nightscout.androidaps.events; +import info.nightscout.androidaps.MainApp; + /** * Created by mike on 19.06.2016. */ public class EventPreferenceChange { + public String changedKey; + public EventPreferenceChange(String key) { + changedKey = key; + } + + public boolean isChanged(int id) { + return changedKey.equals(MainApp.sResources.getString(id)); + } + + public boolean isChanged(String id) { + return changedKey.equals(id); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index a170ea0e02..3331228f65 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -28,6 +28,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.PlusMinusEditText; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; public class FillDialog extends DialogFragment implements OnClickListener { @@ -72,10 +73,9 @@ public class FillDialog extends DialogFragment implements OnClickListener { Button button3 = (Button) view.findViewById(R.id.fill_preset_button3); View divider = view.findViewById(R.id.fill_preset_divider); - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - amount1 = SafeParse.stringToDouble(DecimalFormatter.to2Decimal(SafeParse.stringToDouble(sp.getString("fill_button1", "0.3")))); - amount2 = SafeParse.stringToDouble(DecimalFormatter.to2Decimal(SafeParse.stringToDouble(sp.getString("fill_button2", "0")))); - amount3 = SafeParse.stringToDouble(DecimalFormatter.to2Decimal(SafeParse.stringToDouble(sp.getString("fill_button3", "0")))); + amount1 = SP.getDouble("fill_button1", 0.3); + amount2 = SP.getDouble("fill_button2", 0d); + amount3 = SP.getDouble("fill_button3", 0d); if(amount1 >0) { button1.setVisibility(View.VISIBLE); 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 3fe6865a8e..b7fcab69a7 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 @@ -3,11 +3,9 @@ package info.nightscout.androidaps.plugins.Careportal.Dialogs; import android.app.Activity; import android.content.DialogInterface; -import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; -import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import android.support.v7.app.AlertDialog; @@ -42,7 +40,6 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Date; - import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -53,10 +50,11 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.CircadianPercentageProfile.CircadianPercentageProfilePlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; import info.nightscout.utils.DateUtil; import info.nightscout.utils.PlusMinusEditText; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; import info.nightscout.utils.Translator; @@ -391,7 +389,6 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick JSONObject gatherData() { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String enteredBy = SP.getString("careportal_enteredby", ""); JSONObject data = new JSONObject(); try { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java index e56651eee1..201b2ac37b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; @@ -66,7 +67,7 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte @Override public String getNameShort() { String name = MainApp.sResources.getString(R.string.circadian_percentage_profile_shortname); - if (!name.trim().isEmpty()){ + if (!name.trim().isEmpty()) { //only if translation exists return name; } @@ -125,75 +126,19 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings"); - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (settings.contains(SETTINGS_PREFIX + "mgdl")) - try { - mgdl = settings.getBoolean(SETTINGS_PREFIX + "mgdl", true); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else mgdl = true; - if (settings.contains(SETTINGS_PREFIX + "mmol")) - try { - mmol = settings.getBoolean(SETTINGS_PREFIX + "mmol", false); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else mmol = false; - if (settings.contains(SETTINGS_PREFIX + "dia")) - try { - dia = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "dia", "3")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else dia = 3d; - if (settings.contains(SETTINGS_PREFIX + "targetlow")) - try { - targetLow = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "targetlow", "80")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else targetLow = 80d; - if (settings.contains(SETTINGS_PREFIX + "targethigh")) - try { - targetHigh = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "targethigh", "120")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else targetHigh = 120d; - if (settings.contains(SETTINGS_PREFIX + "percentage")) - try { - percentage = SafeParse.stringToInt(settings.getString(SETTINGS_PREFIX + "percentage", "100")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else percentage = 100; - - if (settings.contains(SETTINGS_PREFIX + "timeshift")) - try { - timeshift = SafeParse.stringToInt(settings.getString(SETTINGS_PREFIX + "timeshift", "0")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else timeshift = 0; + mgdl = SP.getBoolean(SETTINGS_PREFIX + "mgdl", true); + mmol = SP.getBoolean(SETTINGS_PREFIX + "mmol", false); + dia = SP.getDouble(SETTINGS_PREFIX + "dia", 3d); + targetLow = SP.getDouble(SETTINGS_PREFIX + "targetlow", 80d); + targetHigh = SP.getDouble(SETTINGS_PREFIX + "targethigh", 120d); + percentage = SP.getInt(SETTINGS_PREFIX + "percentage", 100); + timeshift = SP.getInt(SETTINGS_PREFIX + "timeshift", 0); for (int i = 0; i < 24; i++) { - try { - basebasal[i] = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "basebasal" + i, DecimalFormatter.to2Decimal(basebasal[i]))); - } catch (Exception e) { - log.debug(e.getMessage()); - } - try { - baseic[i] = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "baseic" + i, DecimalFormatter.to2Decimal(baseic[i]))); - } catch (Exception e) { - log.debug(e.getMessage()); - } - try { - baseisf[i] = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "baseisf" + i, DecimalFormatter.to2Decimal(baseisf[i]))); - } catch (Exception e) { - log.debug(e.getMessage()); - } + basebasal[i] = SP.getDouble(SETTINGS_PREFIX + "basebasal" + i, basebasal[i]); + baseic[i] = SP.getDouble(SETTINGS_PREFIX + "baseic" + i, baseic[i]); + baseisf[i] = SP.getDouble(SETTINGS_PREFIX + "baseisf" + i, baseisf[i]); } @@ -227,7 +172,7 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte } profile.put("carbratio", icArray); - JSONArray isfArray = new JSONArray(); + JSONArray isfArray = new JSONArray(); for (int i = 0; i < 24; i++) { isfArray.put(new JSONObject().put("timeAsSeconds", i * 60 * 60).put("value", baseisf[(offset + i) % 24] * 100d / percentage)); } @@ -259,7 +204,7 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte } private void performLimitCheck() { - if (percentage < Constants.CPP_MIN_PERCENTAGE || percentage > Constants.CPP_MAX_PERCENTAGE){ + if (percentage < Constants.CPP_MIN_PERCENTAGE || percentage > Constants.CPP_MAX_PERCENTAGE) { String msg = String.format(MainApp.sResources.getString(R.string.openapsma_valueoutofrange), "Profile-Percentage"); log.error(msg); MainApp.getConfigBuilder().uploadError(msg); @@ -289,13 +234,15 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte return profileString(baseisf, 0, 100, false); } - String baseBasalString() {return profileString(basebasal, 0, 100, true);} + String baseBasalString() { + return profileString(basebasal, 0, 100, true); + } - public double baseBasalSum(){ + public double baseBasalSum() { return sum(basebasal); } - public double percentageBasalSum(){ + public double percentageBasalSum() { double result = 0; for (int i = 0; i < basebasal.length; i++) { result += SafeParse.stringToDouble(DecimalFormatter.to2Decimal(basebasal[i] * percentage / 100d)); @@ -304,7 +251,7 @@ public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInte } - public static double sum(double values[]){ + public static double sum(double values[]) { double result = 0; for (int i = 0; i < values.length; i++) { result += values[i]; 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 bf9f3fb0b2..4b0ce3bd0a 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 @@ -20,6 +20,7 @@ import java.util.ArrayList; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface; @@ -197,6 +198,7 @@ public class ConfigBuilderFragment extends Fragment implements FragmentBase { onEnabledCategoryChanged(plugin, type); configBuilderPlugin.storeSettings(); MainApp.bus().post(new EventRefreshGui(true)); + MainApp.bus().post(new EventConfigBuilderChange()); getPlugin().logPluginStatus(); } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java index 7c59790e35..a9d4ed5b36 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRFragment.java @@ -24,7 +24,6 @@ import java.util.Date; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog; @@ -195,11 +194,6 @@ public class DanaRFragment extends Fragment implements FragmentBase { updateGUI(); } - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - updateGUI(); - } - // GUI functions private void updateGUI() { Activity activity = getActivity(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java index b5d085b185..c68bac86ac 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPump.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.DanaR; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -11,9 +8,9 @@ import java.text.DecimalFormat; import java.util.Date; import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; -import info.nightscout.utils.SafeParse; +import info.nightscout.utils.SP; /** * Created by mike on 04.07.2016. @@ -122,8 +119,7 @@ public class DanaRPump { // Evening / 17:00–21:59 // Night / 22:00–5:59 - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - double dia = SafeParse.stringToDouble(SP.getString("danarprofile_dia", "3")); + double dia = SP.getDouble(R.string.key_danarprofile_dia, 3d); try { json.put("defaultProfile", PROFILE_PREFIX + (activeProfile + 1)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index ef707ef2f1..cc36efac9a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -82,13 +82,13 @@ import info.nightscout.androidaps.plugins.DanaR.events.EventDanaRNewStatus; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; public class ExecutionService extends Service { private static Logger log = LoggerFactory.getLogger(ExecutionService.class); - private SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); private String devName; private SerialIOThread mSerialIOThread; @@ -187,7 +187,7 @@ public class ExecutionService extends Service { } public void connect(String from) { - if (danaRPump.password != -1 && danaRPump.password != SafeParse.stringToInt(SP.getString("danar_password", "-1"))) { + if (danaRPump.password != -1 && danaRPump.password != SP.getInt(R.string.key_danar_password, -1)) { ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.wrongpumppassword), R.raw.error); return; } @@ -246,7 +246,7 @@ public class ExecutionService extends Service { } private void getBTSocketForSelectedPump() { - devName = SP.getString("danar_bt_name", ""); + devName = SP.getString(MainApp.sResources.getString(R.string.key_danar_bt_name), ""); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java index 4142b43ff6..6e854f85e5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanFragment.java @@ -24,7 +24,6 @@ import java.util.Date; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.DanaR.Dialogs.ProfileViewDialog; @@ -192,11 +191,6 @@ public class DanaRKoreanFragment extends Fragment implements FragmentBase { updateGUI(); } - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - updateGUI(); - } - // GUI functions private void updateGUI() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java index faf8318710..b8a7016956 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPump.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.DanaRKorean; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -11,9 +8,9 @@ import java.text.DecimalFormat; import java.util.Date; import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; -import info.nightscout.utils.SafeParse; +import info.nightscout.utils.SP; /** * Created by mike on 04.07.2016. @@ -115,8 +112,7 @@ public class DanaRKoreanPump { // Evening / 17:00–21:59 // Night / 22:00–5:59 - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - double dia = SafeParse.stringToDouble(SP.getString("danarprofile_dia", "3")); + double dia = SP.getDouble(R.string.key_danarprofile_dia, 3d); try { json.put("defaultProfile", PROFILE_PREFIX + (activeProfile + 1)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java index 8b3a3287a1..cb48424be0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java @@ -8,11 +8,9 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.os.Binder; import android.os.IBinder; import android.os.PowerManager; -import android.preference.PreferenceManager; import com.squareup.otto.Subscribe; @@ -75,16 +73,15 @@ import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgSettingShippingInf import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusBasic; import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusBolusExtended; import info.nightscout.androidaps.plugins.DanaRKorean.comm.MsgStatusTempBasal; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; -import info.nightscout.utils.SafeParse; +import info.nightscout.utils.SP; import info.nightscout.utils.ToastUtils; public class ExecutionService extends Service { private static Logger log = LoggerFactory.getLogger(ExecutionService.class); - private SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); private String devName; private SerialIOThread mSerialIOThread; @@ -183,7 +180,7 @@ public class ExecutionService extends Service { } public void connect(String from) { - if (danaRKoreanPump.password != -1 && danaRKoreanPump.password != SafeParse.stringToInt(SP.getString("danar_password", "-1"))) { + if (danaRKoreanPump.password != -1 && danaRKoreanPump.password != SP.getInt(R.string.key_danar_password, -1)) { ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.wrongpumppassword), R.raw.error); return; } @@ -242,7 +239,7 @@ public class ExecutionService extends Service { } private void getBTSocketForSelectedPump() { - devName = SP.getString("danar_bt_name", ""); + devName = SP.getString(MainApp.sResources.getString(R.string.key_danar_bt_name), ""); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java index 6b917b44da..a4c65e83dd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; /** @@ -62,7 +63,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { @Override public String getNameShort() { String name = MainApp.sResources.getString(R.string.localprofile_shortname); - if (!name.trim().isEmpty()){ + if (!name.trim().isEmpty()) { //only if translation exists return name; } @@ -116,117 +117,48 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { private void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings"); - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (settings.contains("LocalProfile" + "mgdl")) - try { - mgdl = settings.getBoolean("LocalProfile" + "mgdl", false); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else mgdl = false; - if (settings.contains("LocalProfile" + "mmol")) - try { - mmol = settings.getBoolean("LocalProfile" + "mmol", true); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else mmol = true; - if (settings.contains("LocalProfile" + "dia")) - try { - dia = SafeParse.stringToDouble(settings.getString("LocalProfile" + "dia", "3")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else dia = 3d; - if (settings.contains("LocalProfile" + "ic")) - try { - ic = new JSONArray(settings.getString("LocalProfile" + "ic", DEFAULTARRAY)); - } catch (Exception e) { - log.debug(e.getMessage()); - try { - ic = new JSONArray(DEFAULTARRAY); - } catch (JSONException e1) { - e1.printStackTrace(); - } - } - else { + mgdl = SP.getBoolean("LocalProfile" + "mgdl", false); + mmol = SP.getBoolean("LocalProfile" + "mmol", true); + dia = SP.getDouble("LocalProfile" + "dia", 3d); + try { + ic = new JSONArray(SP.getString("LocalProfile" + "ic", DEFAULTARRAY)); + } catch (JSONException e1) { try { ic = new JSONArray(DEFAULTARRAY); - } catch (JSONException e) { - e.printStackTrace(); + } catch (JSONException e2) { } } - if (settings.contains("LocalProfile" + "isf")) - try { - isf = new JSONArray(settings.getString("LocalProfile" + "isf", DEFAULTARRAY)); - } catch (Exception e) { - log.debug(e.getMessage()); - try { - isf = new JSONArray(DEFAULTARRAY); - } catch (JSONException e1) { - e1.printStackTrace(); - } - } - else { + try { + isf = new JSONArray(SP.getString("LocalProfile" + "isf", DEFAULTARRAY)); + } catch (JSONException e1) { try { isf = new JSONArray(DEFAULTARRAY); - } catch (JSONException e) { - e.printStackTrace(); + } catch (JSONException e2) { } } - if (settings.contains("LocalProfile" + "basal")) - try { - basal = new JSONArray(settings.getString("LocalProfile" + "basal", DEFAULTARRAY)); - } catch (Exception e) { - log.debug(e.getMessage()); - try { - basal = new JSONArray(DEFAULTARRAY); - } catch (JSONException e1) { - e1.printStackTrace(); - } - } - else { + try { + basal = new JSONArray(SP.getString("LocalProfile" + "basal", DEFAULTARRAY)); + } catch (JSONException e1) { try { basal = new JSONArray(DEFAULTARRAY); - } catch (JSONException e) { - e.printStackTrace(); + } catch (JSONException e2) { } } - if (settings.contains("LocalProfile" + "targetlow")) - try { - targetLow = new JSONArray(settings.getString("LocalProfile" + "targetlow", DEFAULTARRAY)); - } catch (Exception e) { - log.debug(e.getMessage()); - try { - targetLow = new JSONArray(DEFAULTARRAY); - } catch (JSONException e1) { - e1.printStackTrace(); - } - } - else { + try { + targetLow = new JSONArray(SP.getString("LocalProfile" + "targetlow", DEFAULTARRAY)); + } catch (JSONException e1) { try { targetLow = new JSONArray(DEFAULTARRAY); - } catch (JSONException e) { - e.printStackTrace(); + } catch (JSONException e2) { } } - if (settings.contains("LocalProfile" + "targethigh")) - try { - targetHigh = new JSONArray(settings.getString("LocalProfile" + "targethigh", DEFAULTARRAY)); - } catch (Exception e) { - log.debug(e.getMessage()); - try { - targetHigh = new JSONArray(DEFAULTARRAY); - } catch (JSONException e1) { - e1.printStackTrace(); - } - } - else { + try { + targetHigh = new JSONArray(SP.getString("LocalProfile" + "targethigh", DEFAULTARRAY)); + } catch (JSONException e1) { try { targetHigh = new JSONArray(DEFAULTARRAY); - } catch (JSONException e) { - e.printStackTrace(); + } catch (JSONException e2) { } } createConvertedProfile(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java index 4a9b5f963c..d101b3f5fe 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalFragment.java @@ -2,10 +2,8 @@ package info.nightscout.androidaps.plugins.NSClientInternal; import android.app.Activity; -import android.content.SharedPreferences; import android.graphics.Paint; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; @@ -25,8 +23,8 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart; -import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI; +import info.nightscout.utils.SP; public class NSClientInternalFragment extends Fragment implements FragmentBase, View.OnClickListener, CompoundButton.OnCheckedChangeListener { private static Logger log = LoggerFactory.getLogger(NSClientInternalFragment.class); @@ -106,19 +104,15 @@ public class NSClientInternalFragment extends Fragment implements FragmentBase, @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - SharedPreferences.Editor editor = SP.edit(); switch (buttonView.getId()) { case R.id.nsclientinternal_paused: - editor.putBoolean("nsclientinternal_paused", isChecked); - editor.apply(); + SP.putBoolean("nsclientinternal_paused", isChecked); getPlugin().paused = isChecked; // TODO updateGUI(); break; case R.id.nsclientinternal_autoscroll: - editor.putBoolean("nsclientinternal_autoscroll", isChecked); - editor.apply(); + SP.putBoolean("nsclientinternal_autoscroll", isChecked); updateGUI(); break; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java index ed4e74ec50..ff530e6410 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java @@ -36,6 +36,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientR import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientUpdateGUI; import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; +import info.nightscout.utils.SP; public class NSClientInternalPlugin implements PluginBase { private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class); @@ -59,7 +60,6 @@ public class NSClientInternalPlugin implements PluginBase { public NSClientInternalPlugin() { MainApp.bus().register(this); - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); paused = SP.getBoolean("nsclientinternal_paused", false); autoscroll = SP.getBoolean("nsclientinternal_autoscroll", true); url = SP.getString("nsclientinternal_url", ""); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java index f0486b2511..8c22120c0e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/UploadQueue.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.NSClientInternal; -import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; @@ -13,13 +12,11 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSAddAck; -import info.nightscout.androidaps.plugins.NSClientInternal.acks.NSUpdateAck; import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastQueueStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.DbRequest; import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; +import info.nightscout.utils.SP; /** * Created by mike on 21.02.2016. @@ -120,9 +117,8 @@ public class UploadQueue { private void loadMap() { queue = new HashMap(); - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); try { - String jsonString = sp.getString(KEY, (new JSONObject()).toString()); + String jsonString = SP.getString(KEY, (new JSONObject()).toString()); JSONObject jsonObject = new JSONObject(jsonString); Iterator keysItr = jsonObject.keys(); while (keysItr.hasNext()) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java index 3c5e8aac82..2f42814f5d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java @@ -31,6 +31,8 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.events.EventConfigBuilderChange; +import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin; @@ -54,6 +56,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import io.socket.client.IO; import io.socket.client.Socket; @@ -144,6 +147,25 @@ public class NSClientService extends Service { log.debug("EventAppExit finished"); } + @Subscribe + public void onStatusEvent(EventPreferenceChange ev) { + if (ev.isChanged(R.string.key_nsclientinternal_url) || + ev.isChanged(R.string.key_nsclientinternal_api_secret) || + ev.isChanged(R.string.key_nsclientinternal_hours) + ) { + destroy(); + initialize(); + } + } + + @Subscribe + public void onStatusEvent(EventConfigBuilderChange ev) { + if (nsEnabled != MainApp.getSpecificPlugin(NSClientInternalPlugin.class).isEnabled(PluginBase.GENERAL)) { + destroy(); + initialize(); + } + } + public static void setNsProfile(NSProfile profile) { nsProfile = profile; } @@ -251,11 +273,10 @@ public class NSClientService extends Service { } public void readPreferences() { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); nsEnabled = MainApp.getSpecificPlugin(NSClientInternalPlugin.class).isEnabled(PluginBase.GENERAL); - nsURL = SP.getString(MainApp.sResources.getString(R.string.key_nsclientinternal_url), ""); - nsAPISecret = SP.getString(MainApp.sResources.getString(R.string.key_nsclientinternal_api_secret), ""); - nsHours = SafeParse.stringToInt(SP.getString(MainApp.sResources.getString(R.string.key_nsclientinternal_hours), "3")); + nsURL = SP.getString(R.string.key_nsclientinternal_url, ""); + nsAPISecret = SP.getString(R.string.key_nsclientinternal_api_secret, ""); + nsHours = SP.getInt(R.string.key_nsclientinternal_hours, 3); nsDevice = SP.getString("careportal_enteredby", ""); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java index 5a4098a26a..3c19b5a5c4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSProfile/NSProfilePlugin.java @@ -19,8 +19,9 @@ import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.androidaps.plugins.NSProfile.events.EventNSProfileUpdateGUI; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.androidaps.plugins.NSProfile.events.EventNSProfileUpdateGUI; +import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. @@ -110,9 +111,8 @@ public class NSProfilePlugin implements PluginBase, ProfileInterface { private void loadNSProfile() { if (Config.logPrefsChange) log.debug("Loading stored profile"); - SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - String activeProfile = store.getString("activeProfile", null); - String profileString = store.getString("profile", null); + String activeProfile = SP.getString("activeProfile", null); + String profileString = SP.getString("profile", null); if (profileString != null) { if (Config.logPrefsChange) { log.debug("Loaded profile: " + profileString); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java index 030878b6e9..5d33fdae5e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Objectives/ObjectivesPlugin.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; /** @@ -211,20 +212,19 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } void loadProgress() { - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); for (int num = 0; num < objectives.size(); num++) { Objective o = objectives.get(num); try { - o.started = new Date(SafeParse.stringToLong(settings.getString("Objectives" + num + "started", "0"))); - o.accomplished = new Date(SafeParse.stringToLong(settings.getString("Objectives" + num + "accomplished", "0"))); + o.started = new Date(SP.getLong("Objectives" + num + "started", 0L)); + o.accomplished = new Date(SP.getLong("Objectives" + num + "accomplished", 0L)); } catch (Exception e) { e.printStackTrace(); } } - bgIsAvailableInNS = settings.getBoolean("Objectives" + "bgIsAvailableInNS", false); - pumpStatusIsAvailableInNS = settings.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false); + bgIsAvailableInNS = SP.getBoolean("Objectives" + "bgIsAvailableInNS", false); + pumpStatusIsAvailableInNS = SP.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false); try { - manualEnacts = SafeParse.stringToInt(settings.getString("Objectives" + "manualEnacts", "0")); + manualEnacts = SP.getInt("Objectives" + "manualEnacts", 0); } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java index e670ac7138..44c3970ac5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/Autosens.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.OpenAPSAMA; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,6 +13,7 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.Round; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; @@ -23,7 +21,6 @@ public class Autosens { private static Logger log = LoggerFactory.getLogger(Autosens.class); public static AutosensResult detectSensitivityandCarbAbsorption(List glucose_data, Long mealTime) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); //console.error(mealTime); @@ -131,7 +128,7 @@ public class Autosens { if (mealTime != null && bgTime > mealTime) { // figure out how many carbs that represents // but always assume at least 3mg/dL/5m (default) absorption - double ci = Math.max(deviation, SafeParse.stringToDouble(SP.getString("openapsama_min_5m_carbimpact", "3.0"))); + double ci = Math.max(deviation, SP.getDouble("openapsama_min_5m_carbimpact", 3.0)); double absorbed = ci * profile.getIc(secondsFromMidnight) / sens; // and add that to the running total carbsAbsorbed carbsAbsorbed += absorbed; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java index 4f33a42284..16d0eb6409 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; public class DetermineBasalAdapterAMAJS { @@ -206,7 +207,6 @@ public class DetermineBasalAdapterAMAJS { double min_5m_carbimpact) { String units = profile.getUnits(); - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); mProfile = new V8Object(mV8rt); mProfile.add("max_iob", maxIob); @@ -219,13 +219,13 @@ public class DetermineBasalAdapterAMAJS { mProfile.add("target_bg", targetBg); mProfile.add("carb_ratio", profile.getIc(profile.secondsFromMidnight())); mProfile.add("sens", NSProfile.toMgdl(profile.getIsf(NSProfile.secondsFromMidnight()).doubleValue(), units)); - mProfile.add("max_daily_safety_multiplier", SafeParse.stringToInt(preferences.getString("openapsama_max_daily_safety_multiplier", "3"))); - mProfile.add("current_basal_safety_multiplier", SafeParse.stringToInt(preferences.getString("openapsama_max_basal_safety_multiplier", "4"))); + mProfile.add("max_daily_safety_multiplier", SP.getInt("openapsama_max_daily_safety_multiplier", 3)); + mProfile.add("current_basal_safety_multiplier", SP.getInt("openapsama_max_basal_safety_multiplier", 4)); mProfile.add("skip_neutral_temps", true); mProfile.add("current_basal", pump.getBaseBasalRate()); mProfile.add("temptargetSet", tempTargetSet); - mProfile.add("autosens_adjust_targets", preferences.getBoolean("openapsama_autosens_adjusttargets", true)); - mProfile.add("min_5m_carbimpact", SafeParse.stringToDouble(preferences.getString("openapsama_min_5m_carbimpact", "3.0"))); + mProfile.add("autosens_adjust_targets", SP.getBoolean("openapsama_autosens_adjusttargets", true)); + mProfile.add("min_5m_carbimpact", SP.getDouble("openapsama_min_5m_carbimpact", 3d)); mV8rt.add(PARAM_profile, mProfile); mCurrentTemp = new V8Object(mV8rt); 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 7bf0f169e1..2cbffae52a 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 @@ -32,6 +32,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; @@ -155,12 +156,11 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { return; } - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String units = profile.getUnits(); - String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; - String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; - String targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; + Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + Double minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + Double targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; if (!units.equals(Constants.MGDL)) { maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; @@ -169,11 +169,11 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { Date now = new Date(); - double maxIob = SafeParse.stringToDouble(SP.getString("openapsma_max_iob", "1.5")); - double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); - double minBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_min_bg", minBgDefault)), units); - double maxBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_max_bg", maxBgDefault)), units); - double targetBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_target_bg", targetBgDefault)), units); + double maxIob = SP.getDouble("openapsma_max_iob", 1.5d); + double maxBasal = SP.getDouble("openapsma_max_basal", 1d); + double minBg = NSProfile.toMgdl(SP.getDouble("openapsma_min_bg", minBgDefault), units); + double maxBg = NSProfile.toMgdl(SP.getDouble("openapsma_max_bg", maxBgDefault), units); + double targetBg = NSProfile.toMgdl(SP.getDouble("openapsma_target_bg", targetBgDefault), units); minBg = Round.roundTo(minBg, 0.1d); maxBg = Round.roundTo(maxBg, 0.1d); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java index b6032b9dcc..c4b8f3e34a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.OpenAPSMA; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,13 +22,14 @@ import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateResultGui; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import static info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin.checkOnlyHardLimits; @@ -156,12 +154,11 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { return; } - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String units = profile.getUnits(); - String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; - String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; - String targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; + Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + Double minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + Double targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; if (!units.equals(Constants.MGDL)) { maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; @@ -170,11 +167,11 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { Date now = new Date(); - double maxIob = SafeParse.stringToDouble(SP.getString("openapsma_max_iob", "1.5")); + double maxIob = SP.getDouble("openapsma_max_iob", 1.5d); double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); - double minBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_min_bg", minBgDefault)), units); - double maxBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_max_bg", maxBgDefault)), units); - double targetBg = NSProfile.toMgdl(SafeParse.stringToDouble(SP.getString("openapsma_target_bg", targetBgDefault)), units); + double minBg = NSProfile.toMgdl(SP.getDouble("openapsma_min_bg", minBgDefault), units); + double maxBg = NSProfile.toMgdl(SP.getDouble("openapsma_max_bg", maxBgDefault), units); + double targetBg = NSProfile.toMgdl(SP.getDouble("openapsma_target_bg", targetBgDefault), units); minBg = Round.roundTo(minBg, 0.1d); maxBg = Round.roundTo(maxBg, 0.1d); 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 9f0efee0e1..618aed3aee 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 @@ -92,6 +92,7 @@ import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.Round; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; @@ -99,7 +100,6 @@ public class OverviewFragment extends Fragment { private static Logger log = LoggerFactory.getLogger(OverviewFragment.class); private static OverviewPlugin overviewPlugin = new OverviewPlugin(); - private SharedPreferences prefs; public static OverviewPlugin getPlugin() { return overviewPlugin; @@ -151,7 +151,6 @@ public class OverviewFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); View view = inflater.inflate(R.layout.overview_fragment, container, false); bgView = (TextView) view.findViewById(R.id.overview_bg); @@ -185,12 +184,13 @@ public class OverviewFragment extends Fragment { llm = new LinearLayoutManager(view.getContext()); notificationsView.setLayoutManager(llm); - showPredictionView.setChecked(prefs.getBoolean("showprediction", false)); + showPredictionView.setChecked(SP.getBoolean("showprediction", false)); showPredictionView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - SharedPreferences.Editor editor = prefs.edit(); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("showprediction", showPredictionView.isChecked()); editor.apply(); updateGUI(); @@ -623,15 +623,15 @@ public class OverviewFragment extends Fragment { tempTargetView.setText(NSProfile.toUnitsString(tempTarget.low, NSProfile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()) + " - " + NSProfile.toUnitsString(tempTarget.high, NSProfile.fromMgdlToUnits(tempTarget.high, profile.getUnits()), profile.getUnits())); } else { - String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; - String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + Double minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; if (!profile.getUnits().equals(Constants.MGDL)) { maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; } tempTargetView.setTextColor(Color.WHITE); tempTargetView.setBackgroundResource(R.drawable.temptargetborderdisabled); - tempTargetView.setText(prefs.getString("openapsma_min_bg", minBgDefault) + " - " + prefs.getString("openapsma_max_bg", maxBgDefault)); + tempTargetView.setText(SP.getDouble("openapsma_min_bg", minBgDefault) + " - " + SP.getDouble("openapsma_max_bg", maxBgDefault)); tempTargetView.setVisibility(View.VISIBLE); } } else { @@ -799,8 +799,8 @@ public class OverviewFragment extends Fragment { endTime = toTime; } - Double lowLine = SafeParse.stringToDouble(prefs.getString("low_mark", "0")); - Double highLine = SafeParse.stringToDouble(prefs.getString("high_mark", "0")); + Double lowLine = SP.getDouble("low_mark", 0d); + Double highLine = SP.getDouble("high_mark", 0d); if (lowLine < 1) { lowLine = NSProfile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java index 9caa2fde10..a8f0083afc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.Overview; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import com.squareup.otto.Subscribe; import org.json.JSONArray; @@ -13,6 +10,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. @@ -27,8 +25,7 @@ public class OverviewPlugin implements PluginBase { public NotificationStore notificationStore = new NotificationStore(); public OverviewPlugin() { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - String storedData = preferences.getString("QuickWizard", "[]"); + String storedData = SP.getString("QuickWizard", "[]"); try { quickWizard.setData(new JSONArray(storedData)); } catch (JSONException e) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java index ddc0333306..12ec4f5d4f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyPlugin.java @@ -1,8 +1,5 @@ package info.nightscout.androidaps.plugins.SafetyFragment; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,7 +12,7 @@ import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.Round; -import info.nightscout.utils.SafeParse; +import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. @@ -78,7 +75,6 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { **/ @Override public boolean isClosedModeEnabled() { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String mode = SP.getString("aps_mode", "open"); return mode.equals("closed") && BuildConfig.CLOSEDLOOP; } @@ -96,15 +92,14 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Double applyBasalConstraints(Double absoluteRate) { Double origAbsoluteRate = absoluteRate; - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); + Double maxBasal = SP.getDouble("openapsma_max_basal", 1d); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (profile == null) return absoluteRate; if (absoluteRate < 0) absoluteRate = 0d; - Integer maxBasalMult = SafeParse.stringToInt(SP.getString("openapsama_max_basal_safety_multiplier", "4")); - Integer maxBasalFromDaily = SafeParse.stringToInt(SP.getString("openapsama_max_daily_safety_multiplier", "3")); + Integer maxBasalMult = SP.getInt("openapsama_max_basal_safety_multiplier", 4); + Integer maxBasalFromDaily = SP.getInt("openapsama_max_daily_safety_multiplier", 3); // Check percentRate but absolute rate too, because we know real current basal in pump Double origRate = absoluteRate; if (absoluteRate > maxBasal) { @@ -128,8 +123,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Integer applyBasalConstraints(Integer percentRate) { Integer origPercentRate = percentRate; - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); + Double maxBasal = SP.getDouble("openapsma_max_basal", 1d); NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (profile == null) return percentRate; @@ -142,8 +136,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { if (absoluteRate < 0) absoluteRate = 0d; - Integer maxBasalMult = SafeParse.stringToInt(SP.getString("openapsama_max_basal_safety_multiplier", "4")); - Integer maxBasalFromDaily = SafeParse.stringToInt(SP.getString("openapsama_max_daily_safety_multiplier", "3")); + Integer maxBasalMult = SP.getInt("openapsama_max_basal_safety_multiplier", 4); + Integer maxBasalFromDaily = SP.getInt("openapsama_max_daily_safety_multiplier", 3); // Check percentRate but absolute rate too, because we know real current basal in pump Double origRate = absoluteRate; if (absoluteRate > maxBasal) { @@ -174,9 +168,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Double applyBolusConstraints(Double insulin) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); try { - Double maxBolus = SafeParse.stringToDouble(SP.getString("treatmentssafety_maxbolus", "3")); + Double maxBolus = SP.getDouble("treatmentssafety_maxbolus", 3d); if (insulin < 0) insulin = 0d; if (insulin > maxBolus) insulin = maxBolus; @@ -189,9 +182,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Integer applyCarbsConstraints(Integer carbs) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); try { - Integer maxCarbs = SafeParse.stringToInt(SP.getString("treatmentssafety_maxcarbs", "48")); + Integer maxCarbs = SP.getInt("treatmentssafety_maxcarbs", 48); if (carbs < 0) carbs = 0; if (carbs > maxCarbs) carbs = maxCarbs; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java index 8b3659d8ca..f40d85371e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfilePlugin.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; /** @@ -60,7 +61,7 @@ public class SimpleProfilePlugin implements PluginBase, ProfileInterface { @Override public String getNameShort() { String name = MainApp.sResources.getString(R.string.simpleprofile_shortname); - if (!name.trim().isEmpty()){ + if (!name.trim().isEmpty()) { //only if translation exists return name; } @@ -114,64 +115,15 @@ public class SimpleProfilePlugin implements PluginBase, ProfileInterface { private void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings"); - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (settings.contains("SimpleProfile" + "mgdl")) - try { - mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else mgdl = true; - if (settings.contains("SimpleProfile" + "mmol")) - try { - mmol = settings.getBoolean("SimpleProfile" + "mmol", false); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else mmol = false; - if (settings.contains("SimpleProfile" + "dia")) - try { - dia = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "dia", "3")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else dia = 3d; - if (settings.contains("SimpleProfile" + "ic")) - try { - ic = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "ic", "20")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else ic = 20d; - if (settings.contains("SimpleProfile" + "isf")) - try { - isf = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "isf", "200")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else isf = 200d; - if (settings.contains("SimpleProfile" + "basal")) - try { - basal = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "basal", "1")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else basal = 1d; - if (settings.contains("SimpleProfile" + "targetlow")) - try { - targetLow = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targetlow", "80")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else targetLow = 80d; - if (settings.contains("SimpleProfile" + "targethigh")) - try { - targetHigh = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targethigh", "120")); - } catch (Exception e) { - log.debug(e.getMessage()); - } - else targetHigh = 120d; + mgdl = SP.getBoolean("SimpleProfile" + "mgdl", true); + mmol = SP.getBoolean("SimpleProfile" + "mmol", false); + dia = SP.getDouble("SimpleProfile" + "dia", 3d); + ic = SP.getDouble("SimpleProfile" + "ic", 20d); + isf = SP.getDouble("SimpleProfile" + "isf", 200d); + basal = SP.getDouble("SimpleProfile" + "basal", 1d); + targetLow = SP.getDouble("SimpleProfile" + "targetlow", 80d); + targetHigh = SP.getDouble("SimpleProfile" + "targethigh", 120d); createConvertedProfile(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index c71f036477..e216764a3d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -22,6 +22,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.data.GlucoseStatus; +import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.events.EventPreferenceChange; @@ -30,13 +31,13 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; -import info.nightscout.androidaps.data.IobTotal; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.XdripCalibrations; @@ -159,8 +160,7 @@ public class SmsCommunicatorPlugin implements PluginBase { @Subscribe public void processSettings(final EventPreferenceChange ev) { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - String settings = sharedPreferences.getString("smscommunicator_allowednumbers", ""); + String settings = SP.getString("smscommunicator_allowednumbers", ""); String pattern = ";"; @@ -191,8 +191,6 @@ public class SmsCommunicatorPlugin implements PluginBase { } private void processSms(Sms receivedSms) { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (!isEnabled(PluginBase.GENERAL)) { log.debug("Ignoring SMS. Plugin disabled."); return; @@ -319,7 +317,7 @@ public class SmsCommunicatorPlugin implements PluginBase { break; case "BASAL": if (splited.length > 1) { - boolean remoteCommandsAllowed = sharedPreferences.getBoolean("smscommunicator_remotecommandsallowed", false); + boolean remoteCommandsAllowed = SP.getBoolean("smscommunicator_remotecommandsallowed", false); if (splited[1].toUpperCase().equals("CANCEL") || splited[1].toUpperCase().equals("STOP")) { if (remoteCommandsAllowed) { passCode = generatePasscode(); @@ -355,7 +353,7 @@ public class SmsCommunicatorPlugin implements PluginBase { } else if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); - boolean remoteCommandsAllowed = sharedPreferences.getBoolean("smscommunicator_remotecommandsallowed", false); + boolean remoteCommandsAllowed = SP.getBoolean("smscommunicator_remotecommandsallowed", false); if (amount > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); @@ -372,7 +370,7 @@ public class SmsCommunicatorPlugin implements PluginBase { case "CAL": if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); - boolean remoteCommandsAllowed = sharedPreferences.getBoolean("smscommunicator_remotecommandsallowed", false); + boolean remoteCommandsAllowed = SP.getBoolean("smscommunicator_remotecommandsallowed", false); if (amount > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_calibrationreplywithcode), amount, passCode); 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 e77db84e26..cbc2a336ce 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 @@ -136,7 +136,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { Iob tIOB = t.iobCalc(now, dia); total.iob += tIOB.iobContrib; total.activity += tIOB.activityContrib; - Iob bIOB = t.iobCalc(now, dia / SafeParse.stringToInt(SP.getString("openapsama_bolussnooze_dia_divisor", "2"))); + Iob bIOB = t.iobCalc(now, dia / SP.getInt("openapsama_bolussnooze_dia_divisor", 2)); total.bolussnooze += bIOB.iobContrib; } return total; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index c4e348a11c..6afb51c5c6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -1,9 +1,7 @@ package info.nightscout.androidaps.plugins.Wear; -import android.content.SharedPreferences; import android.os.Handler; import android.os.HandlerThread; -import android.preference.PreferenceManager; import android.support.annotation.NonNull; import com.j256.ormlite.dao.Dao; @@ -25,12 +23,13 @@ import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.ToastUtils; @@ -46,9 +45,6 @@ public class ActionStringHandler { private static String lastConfirmActionString = null; private static BolusWizard lastBolusWizard = null; - private static SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - - private static HandlerThread handlerThread = new HandlerThread(FillDialog.class.getSimpleName()); static { handlerThread.start(); @@ -73,11 +69,11 @@ public class ActionStringHandler { ///////////////////////////////////// PRIME/FILL double amount = 0d; if ("1".equals(act[1])) { - amount = SafeParse.stringToDouble(DecimalFormatter.to2Decimal(SafeParse.stringToDouble(sp.getString("fill_button1", "0.3")))); + amount = SP.getDouble("fill_button1", 0.3); } else if ("2".equals(act[1])) { - amount = SafeParse.stringToDouble(DecimalFormatter.to2Decimal(SafeParse.stringToDouble(sp.getString("fill_button2", "0")))); + amount = SP.getDouble("fill_button2", 0d); } else if ("3".equals(act[1])) { - amount = SafeParse.stringToDouble(DecimalFormatter.to2Decimal(SafeParse.stringToDouble(sp.getString("fill_button3", "0")))); + amount = SP.getDouble("fill_button3", 0d); } else { return; } @@ -295,17 +291,17 @@ public class ActionStringHandler { } //Default Range/Target - String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; - String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; - String targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; + Double maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + Double minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + Double targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; if (!profile.getUnits().equals(Constants.MGDL)) { maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; targetBgDefault = Constants.TARGET_BG_DEFAULT_MMOL; } ret += "DEFAULT RANGE: "; - ret += sp.getString("openapsma_min_bg", minBgDefault) + " - " + sp.getString("openapsma_max_bg", maxBgDefault); - ret += " target: " + sp.getString("openapsma_target_bg", targetBgDefault); + ret += SP.getDouble("openapsma_min_bg", minBgDefault) + " - " + SP.getDouble("openapsma_max_bg", maxBgDefault); + ret += " target: " + SP.getDouble("openapsma_target_bg", targetBgDefault); return ret; } diff --git a/app/src/main/java/info/nightscout/utils/LocaleHelper.java b/app/src/main/java/info/nightscout/utils/LocaleHelper.java index 1d89c7d689..c8faa83b9d 100644 --- a/app/src/main/java/info/nightscout/utils/LocaleHelper.java +++ b/app/src/main/java/info/nightscout/utils/LocaleHelper.java @@ -40,8 +40,7 @@ public class LocaleHelper { } private static String getPersistedData(Context context, String defaultLanguage) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - return preferences.getString(SELECTED_LANGUAGE, defaultLanguage); + return SP.getString(SELECTED_LANGUAGE, defaultLanguage); } private static void persist(Context context, String language) { diff --git a/app/src/main/java/info/nightscout/utils/PasswordProtection.java b/app/src/main/java/info/nightscout/utils/PasswordProtection.java index 2b8d7a59c1..1af10eb2d2 100644 --- a/app/src/main/java/info/nightscout/utils/PasswordProtection.java +++ b/app/src/main/java/info/nightscout/utils/PasswordProtection.java @@ -3,8 +3,6 @@ package info.nightscout.utils; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; @@ -19,7 +17,6 @@ import info.nightscout.androidaps.R; public class PasswordProtection { static public boolean isLocked(String preference) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); final String password = SP.getString(preference, ""); if (password.equals("")) { return false; @@ -28,7 +25,6 @@ public class PasswordProtection { } static public void QueryPassword(final Context context, int stringID, String preference, final Runnable ok, final Runnable fail) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); final String password = SP.getString(preference, ""); if (password.equals("")) { if (ok != null) ok.run(); diff --git a/app/src/main/java/info/nightscout/utils/SP.java b/app/src/main/java/info/nightscout/utils/SP.java new file mode 100644 index 0000000000..4305be2076 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/SP.java @@ -0,0 +1,72 @@ +package info.nightscout.utils; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import info.nightscout.androidaps.MainApp; + +/** + * Created by mike on 17.02.2017. + */ + +public class SP { + static SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + + static public boolean contains(String key) { + return sharedPreferences.contains(key); + } + + static public String getString(int resourceID, String defaultValue) { + return sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue); + } + + static public String getString(String key, String defaultValue) { + return sharedPreferences.getString(key, defaultValue); + } + + static public boolean getBoolean(int resourceID, boolean defaultValue) { + try { + return sharedPreferences.getBoolean(MainApp.sResources.getString(resourceID), defaultValue); + } catch (Exception e) { + return defaultValue; + } + } + + static public boolean getBoolean(String key, boolean defaultValue) { + try { + return sharedPreferences.getBoolean(key, defaultValue); + } catch (Exception e) { + return defaultValue; + } + } + + static public Double getDouble(int resourceID, Double defaultValue) { + return SafeParse.stringToDouble(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString())); + } + + static public Double getDouble(String key, Double defaultValue) { + return SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString())); + } + + static public int getInt(int resourceID, Integer defaultValue) { + 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())); + } + + static public long getLong(int resourceID, Long defaultValue) { + return SafeParse.stringToLong(sharedPreferences.getString(MainApp.sResources.getString(resourceID), defaultValue.toString())); + } + + static public long getLong(String key, Long defaultValue) { + return SafeParse.stringToLong(sharedPreferences.getString(key, defaultValue.toString())); + } + + static public void putBoolean(String key, boolean value) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putBoolean(key, value); + editor.apply(); + } +} diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 9d8d44f4b9..a265c239e5 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -267,7 +267,7 @@ Прекъсване Презареждане IOB на помпата - Парола за помпа + Парола за помпа пълнене Общо %d записа са изпратени Приложено diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 835421fbb0..001d624c96 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -265,7 +265,7 @@ Nahrávám Pumpa je zaneprázdněna Špatné heslo k pumpě - Heslo k pumpě + Heslo k pumpě Okluze Podáno Stop diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index abf9b60b58..77af750cf3 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -259,7 +259,7 @@ Glukose Füllmenge Unterbrechungen - Pumpen-Passwort + Pumpen-Passwort Essensbolus Kein Profil gewählt Stop diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index bd95743215..7d28d6ef7f 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -275,7 +275,7 @@ Rellenar Suspender Conexión de %d s - Contraseña de la bomba + Contraseña de la bomba Contraseña de la bomba incorrecta! Bomba ocupada Entregado diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index b45c7393b0..9c947244c0 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -278,7 +278,7 @@ 교체 중지 %d 초 동안 연결중 - 펌프 비밀번호 + 펌프 비밀번호 펌프 비밀번호가 잘못되었습니다! 펌프가 바쁩니다 주입됨 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4038b83300..cab260d268 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -294,7 +294,7 @@ Refill Suspend Connecting for %d s - Pump password + Pump password Wrong pump password! Pump is busy Delivered @@ -532,4 +532,8 @@ nsclientinternal_url nsclientinternal_api_secret nsclientinternal_hours + danar_bt_name + danar_password + danar_useextended + danarprofile_dia diff --git a/app/src/main/res/xml/pref_danar.xml b/app/src/main/res/xml/pref_danar.xml index d0ca8a997a..9d5c86a535 100644 --- a/app/src/main/res/xml/pref_danar.xml +++ b/app/src/main/res/xml/pref_danar.xml @@ -6,16 +6,16 @@ diff --git a/app/src/main/res/xml/pref_danarprofile.xml b/app/src/main/res/xml/pref_danarprofile.xml index 5e864d7561..68be80566a 100644 --- a/app/src/main/res/xml/pref_danarprofile.xml +++ b/app/src/main/res/xml/pref_danarprofile.xml @@ -5,7 +5,7 @@ android:title="@string/danarprofile">