diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Constants.java b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Constants.java
index 49cd91fc46..ca14feca31 100644
--- a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Constants.java
+++ b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Constants.java
@@ -61,6 +61,7 @@ public class Constants {
public static final int ZXING_CAM_REQ_CODE = 49374;
public static final int ZXING_FILE_REQ_CODE = 49375; // This is created by just incrementing the existing camera scan code from the zxing package
public static final int SENSORY_EXPIRY_NOTIFICATION_ID = 2003;
+ public static final int OUT_OF_RANGE_GLUCOSE_ENTRY_ID = 2004; // Preference setting out of range
// increments from this start number
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 65c0c5776a..f38fe38bb9 100644
--- a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java
+++ b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java
@@ -1,5 +1,8 @@
package com.eveningoutpost.dexdrip.utilitymodels;
+import static com.eveningoutpost.dexdrip.utils.Preferences.MAX_GLUCOSE_INPUT;
+import static com.eveningoutpost.dexdrip.utils.Preferences.MIN_GLUCOSE_INPUT;
+
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
@@ -20,6 +23,7 @@
import com.eveningoutpost.dexdrip.models.UserNotification;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.SnoozeActivity;
+import com.eveningoutpost.dexdrip.utils.Preferences;
import java.util.ArrayList;
import java.util.Iterator;
@@ -60,6 +64,7 @@ public void performAll() {
IncompatibleApps.notifyAboutIncompatibleApps();
CompatibleApps.notifyAboutCompatibleApps();
legacySettingsMoveLanguageFromNoToNb();
+ prefSettingRangeVerification();
}
@@ -160,4 +165,11 @@ private static void legacySettingsMoveLanguageFromNoToNb() {
Pref.setString("forced_language", "nb");
}
}
+
+ // Correct preference setting values if the values are out of range.
+ // Include new preference settings here that represent glucose values.
+ private static void prefSettingRangeVerification() {
+ Preferences.applyPrefSettingRange("persistent_high_threshold", "170", MIN_GLUCOSE_INPUT, MAX_GLUCOSE_INPUT);
+ }
+
}
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 4d43f46ad0..95b744ccff 100644
--- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java
+++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java
@@ -1,5 +1,8 @@
package com.eveningoutpost.dexdrip.utils;
+import static com.eveningoutpost.dexdrip.models.JoH.showNotification;
+import static com.eveningoutpost.dexdrip.models.JoH.tolerantParseDouble;
+import static com.eveningoutpost.dexdrip.utilitymodels.Constants.OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName;
import static com.eveningoutpost.dexdrip.xdrip.gs;
@@ -192,6 +195,8 @@ public class Preferences extends BasePreferenceActivity implements SearchPrefere
private void refreshFragments() {
refreshFragments(null);
}
+ public static final double MIN_GLUCOSE_INPUT = 40; // The smallest acceptable input glucose value in mg/dL
+ public static final double MAX_GLUCOSE_INPUT = 400; // The largest acceptable input glucose value in mg/dL
private void refreshFragments(final String jumpTo) {
this.preferenceFragment = new AllPrefsFragment(jumpTo);
@@ -1017,6 +1022,33 @@ private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Prefere
.getString(preference.getKey(), ""));
}
+ public static void applyPrefSettingRange(String pref_key, String def, Double min, Double max) { // Correct a preference glucose setting if the value is out of range
+ val notificationId = OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
+ String mySettingString = Pref.getString(pref_key, def);
+ final boolean doMgdl = (Pref.getString("units", "mgdl").equals("mgdl"));
+ double mySettingMgdl = doMgdl ? tolerantParseDouble(mySettingString) : tolerantParseDouble(mySettingString) * Constants.MMOLL_TO_MGDL; // The preference value in mg/dL
+ if (mySettingMgdl > max) { // If the preference value is greater than max
+ if (!doMgdl && mySettingString.equals(def)) { // If the setting value in mmol/L is the same as the default, which is in mg/dL, we correct the value next.
+ // This will only happen if user has chosen mmol/L and updates to a version that has a new preference setting with default in mg/dL
+ UserError.Log.d(TAG, "Setting " + pref_key + " to default converted to mmol/L");
+ Pref.setString(pref_key, JoH.qs(tolerantParseDouble(def) * Constants.MGDL_TO_MMOLL, 1)); // Set the preference to the default value converted to mmol/L
+ } else { // The preference has been set to a value greater than the max allowed. Let's fix it and notify.
+ // This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged.
+ mySettingString = doMgdl ? max + "" : JoH.qs(max * Constants.MGDL_TO_MMOLL, 1) + "";
+ Pref.setString(pref_key, mySettingString); // Set the preference to max
+ UserError.Log.uel(TAG, xdrip.gs(R.string.pref_was_greater_than_max, pref_key)); // Inform the user that xDrip is changing the setting value
+ showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_max), null, notificationId, null, false, false, null, null, null, true);
+ }
+ } else if (mySettingMgdl < min) { // If the preference value is less than min, correct it and notify.
+ // This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged.
+ mySettingString = doMgdl ? min + "" : JoH.qs(min * Constants.MGDL_TO_MMOLL, 1) + "";
+ Pref.setString(pref_key, mySettingString); // Set the preference to min
+ UserError.Log.uel(TAG, xdrip.gs(R.string.pref_was_less_than_min, pref_key)); // Inform the user that xDrip is changing the setting value
+ showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_min), null, notificationId, null, false, false, null, null, null, true);
+
+ }
+ }
+
@RequiredArgsConstructor
public static class AllPrefsFragment extends PreferenceFragment {
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 124349aa02..31c4a11249 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -244,6 +244,10 @@
Enable
Threshold
Forecasted Low Alert
+ Set to max
+ %1$s was set to a value greater than maximum allowed. It has been changed to maximum.
+ Set to min
+ %1$s was set to a value less than minimum allowed. It has been changed to minimum.
Extrapolate data to try to predict lows
Alarm at Forecasted Low mins
Other xDrip+ alerts