diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/evaluators/PersistentHigh.java b/app/src/main/java/com/eveningoutpost/dexdrip/evaluators/PersistentHigh.java index 0bc4ab48e5..d0da3c1ce9 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/evaluators/PersistentHigh.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/evaluators/PersistentHigh.java @@ -8,6 +8,14 @@ * out of sequence with two end points that are both above threshold. */ +/** + * Navid + * + * The same setting, highValue, has been used both for statistics high as well as persistent high alert. + * This imposed a limitation on user. + * We are changing this by adding a new setting, persistent_high_threshold, to be used only for the alert. + */ + import com.eveningoutpost.dexdrip.Home; import com.eveningoutpost.dexdrip.models.BgReading; import com.eveningoutpost.dexdrip.models.JoH; @@ -40,19 +48,20 @@ public static boolean checkForPersistentHigh() { final List last = BgReading.latest(1); if ((last != null) && (last.size() > 0)) { - final double highMarkMgDl = Home.convertToMgDlIfMmol( - JoH.tolerantParseDouble(Pref.getString("highValue", "170"), 170d)); + final double vThreshold = Home.convertToMgDlIfMmol( + JoH.tolerantParseDouble(Pref.getString("persistent_high_threshold", "170"), 170d)); final long now = JoH.tsl(); final long since = now - last.get(0).timestamp; // only process if last reading <10 mins if (since < MINUTE_IN_MS * 10) { - // check if exceeding high - if (last.get(0).getDg_mgdl() > highMarkMgDl) { + // check if exceeding persistent high threshold + if (last.get(0).getDg_mgdl() > vThreshold) { final double this_slope = last.get(0).getDg_slope() * MINUTE_IN_MS; Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope)+ " "+JoH.dateTimeText(last.get(0).timestamp)); + // TODO What if it is dropping at an extremely low slope? What if it is dropping at 1mg/dL per day?! // if not falling if (this_slope > 0 && !last.get(0).hide_slope) { final long high_since = Pref.getLong(PERSISTENT_HIGH_SINCE, 0); @@ -78,7 +87,7 @@ public static boolean checkForPersistentHigh() { return false; } - if (!dataQualityCheck(high_since, highMarkMgDl)) { + if (!dataQualityCheck(high_since, vThreshold)) { Log.d(TAG, "Insufficient data quality to raise persistent high alert"); return false; } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/models/BgReading.java b/app/src/main/java/com/eveningoutpost/dexdrip/models/BgReading.java index cc1a2c7cd3..85bcdd1277 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/models/BgReading.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/models/BgReading.java @@ -1834,10 +1834,10 @@ public static boolean checkForPersistentHigh() { final long since = now - last.get(0).timestamp; // only process if last reading <10 mins if (since < 600000) { - // check if exceeding high + // check if exceeding persistent high threshold if (last.get(0).calculated_value > Home.convertToMgDlIfMmol( - JoH.tolerantParseDouble(Pref.getString("highValue", "170")))) { + JoH.tolerantParseDouble(Pref.getString("persistent_high_threshold", "170")))) { final double this_slope = last.get(0).calculated_value_slope * 60000; //Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope)); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java index a9803735e7..461310132f 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java @@ -17,6 +17,7 @@ import com.eveningoutpost.dexdrip.models.LibreData; import com.eveningoutpost.dexdrip.models.PenData; import com.eveningoutpost.dexdrip.models.Prediction; +import com.eveningoutpost.dexdrip.models.UserError; import com.eveningoutpost.dexdrip.models.UserNotification; import com.eveningoutpost.dexdrip.R; import com.eveningoutpost.dexdrip.SnoozeActivity; @@ -132,6 +133,33 @@ private void migrateToNewStyleRestUris() { } } + // When adding a new setting to an existing function, this method lets us control what happens to an existing + // xDrip install after update. The new added setting should offer more flexibility to the user rather than modifying + // the xDrip behavior without the user's knowledge. + public static void startup() { + if (!Pref.isPreferenceSet("warning_agreed_to")) { // This is the very first time xDrip is running after a fresh install + try { // Everything here runs only once after a fresh install + + Pref.setBoolean("migrate_persistent_high_alert_threshold", false); // Never migrate; we will use the default + + } catch (Exception e) { + UserError.Log.wtf(TAG, "Initial startup settings failed! Please report."); + } + } + try { // Everything here runs after startup, fresh install or not + + if (Pref.getBoolean("migrate_persistent_high_alert_threshold", true)) { // Only if not migrated yet + final String vThreshold = Pref.getString("highValue", "170"); + UserError.Log.e(TAG, "You can now set a persistent high alert threshold. Until you do, we set it equal to your current High Value setting: " + vThreshold); + Pref.setString("persistent_high_threshold", vThreshold); // Set the persistent high alert threshold equal to the high value + Pref.setBoolean("migrate_persistent_high_alert_threshold", false); // Done - never migrate again + + } + } catch (Exception e) { + UserError.Log.wtf(TAG, "Startup settings failed! Please report"); + } + } + // This function moves us from calibrate_external_libre_2_algorithm which is a boolean to a // multi value list option public static void migrateOOP2CalibrationPreferences() { diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java index b3a2a518bb..a6e9b4ebbc 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java @@ -1000,6 +1000,7 @@ public void onCreate(Bundle savedInstanceState) { bindPreferenceSummaryToValue(findPreference("rising_bg_val")); bindPreferenceSummaryToValue(findPreference("other_alerts_sound")); bindPreferenceSummaryToValue(findPreference("bridge_battery_alert_level")); + bindPreferenceSummaryToValueAndEnsureNumeric(findPreference("persistent_high_threshold")); addPreferencesFromResource(R.xml.pref_data_source); @@ -2972,6 +2973,7 @@ public static void handleUnitsChange(Preference preference, Object newValue, All final Double lowVal = Double.parseDouble(preferences.getString("lowValue", "0")); final Double default_insulin_sensitivity = Double.parseDouble(preferences.getString("profile_insulin_sensitivity_default", "54")); final Double default_target_glucose = Double.parseDouble(preferences.getString("plus_target_range", "100")); + final Double persistent_high_Val = Double.parseDouble(preferences.getString("persistent_high_threshold", "0")); static_units = newValue.toString(); @@ -2990,6 +2992,11 @@ public static void handleUnitsChange(Preference preference, Object newValue, All preferences.edit().putString("plus_target_range", Long.toString(Math.round(default_target_glucose * Constants.MMOLL_TO_MGDL))).apply(); Profile.invalidateProfile(); } + if (persistent_high_Val < 36) { + ProfileEditor.convertData(Constants.MMOLL_TO_MGDL); + preferences.edit().putString("persistent_high_threshold", Long.toString(Math.round(persistent_high_Val * Constants.MMOLL_TO_MGDL))).apply(); + Profile.invalidateProfile(); + } } else { if (highVal > 35) { @@ -3006,11 +3013,17 @@ public static void handleUnitsChange(Preference preference, Object newValue, All preferences.edit().putString("plus_target_range", JoH.qs(default_target_glucose * Constants.MGDL_TO_MMOLL,1)).apply(); Profile.invalidateProfile(); } + if (persistent_high_Val > 35) { + ProfileEditor.convertData(Constants.MGDL_TO_MMOLL); + preferences.edit().putString("persistent_high_threshold", JoH.qs(persistent_high_Val * Constants.MGDL_TO_MMOLL, 1)).apply(); + Profile.invalidateProfile(); + } } if (preference != null) preference.setSummary(newValue.toString()); if (allPrefsFragment != null) { allPrefsFragment.setSummary("highValue"); allPrefsFragment.setSummary("lowValue"); + allPrefsFragment.setSummary("persistent_high_threshold"); } if (profile_insulin_sensitivity_default != null) { Log.d(TAG, "refreshing profile insulin sensitivity default display"); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/xdrip.java b/app/src/main/java/com/eveningoutpost/dexdrip/xdrip.java index 0c513b2c1e..a47ce0e44f 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/xdrip.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/xdrip.java @@ -81,6 +81,7 @@ public void onCreate() { executor = new PlusAsyncExecutor(); IdempotentMigrations.migrateOOP2CalibrationPreferences(); // needs to run before preferences get defaults + IdempotentMigrations.startup(); // Must run before defaults are loaded PreferenceManager.setDefaultValues(this, R.xml.pref_general, true); PreferenceManager.setDefaultValues(this, R.xml.pref_data_sync, true); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0c3743c9e7..565339e46e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -236,6 +236,7 @@ Other Alerts Extra Alerts (xDrip+) Persistent High Alert + Notify if above threshold, and not falling, for long Forecasted Low Alert Extrapolate data to try to predict lows Alarm at Forecasted Low mins @@ -1501,7 +1502,6 @@ Noisy Readings Falling/Rising BG Alert Preferences (for these alerts) - When above High level for too long and not heading downwards SEARCH ALL Full Screen mode @@ -1786,6 +1786,8 @@ Select tone for Alerts Show unsmoothed Show unsmoothed data also if Graph Smoothing is enabled. If calibration plugin is used, will show unsmoothed data instead of plugin data. + Persistent high threshold + Persistent high alert threshold You can use Google Drive for backup Select Backup Location Do Backup Now diff --git a/app/src/main/res/xml/pref_notifications.xml b/app/src/main/res/xml/pref_notifications.xml index 587c470d52..103f6a7e4b 100644 --- a/app/src/main/res/xml/pref_notifications.xml +++ b/app/src/main/res/xml/pref_notifications.xml @@ -299,8 +299,15 @@ +