Skip to content

Commit a2bbca9

Browse files
committed
2 parents ff1dbd7 + d8e96f9 commit a2bbca9

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class Constants {
6161
public static final int ZXING_CAM_REQ_CODE = 49374;
6262
public static final int ZXING_FILE_REQ_CODE = 49375; // This is created by just incrementing the existing camera scan code from the zxing package
6363
public static final int SENSORY_EXPIRY_NOTIFICATION_ID = 2003;
64+
public static final int OUT_OF_RANGE_GLUCOSE_ENTRY_ID = 2004; // Preference setting out of range
6465

6566

6667
// increments from this start number

app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.eveningoutpost.dexdrip.utilitymodels;
22

3+
import static com.eveningoutpost.dexdrip.utils.Preferences.MAX_GLUCOSE_INPUT;
4+
import static com.eveningoutpost.dexdrip.utils.Preferences.MIN_GLUCOSE_INPUT;
5+
36
import android.content.Context;
47
import android.content.SharedPreferences;
58
import android.net.Uri;
@@ -20,6 +23,7 @@
2023
import com.eveningoutpost.dexdrip.models.UserNotification;
2124
import com.eveningoutpost.dexdrip.R;
2225
import com.eveningoutpost.dexdrip.SnoozeActivity;
26+
import com.eveningoutpost.dexdrip.utils.Preferences;
2327

2428
import java.util.ArrayList;
2529
import java.util.Iterator;
@@ -60,6 +64,7 @@ public void performAll() {
6064
IncompatibleApps.notifyAboutIncompatibleApps();
6165
CompatibleApps.notifyAboutCompatibleApps();
6266
legacySettingsMoveLanguageFromNoToNb();
67+
prefSettingRangeVerification();
6368

6469
}
6570

@@ -160,4 +165,11 @@ private static void legacySettingsMoveLanguageFromNoToNb() {
160165
Pref.setString("forced_language", "nb");
161166
}
162167
}
168+
169+
// Correct preference setting values if the values are out of range.
170+
// Include new preference settings here that represent glucose values.
171+
private static void prefSettingRangeVerification() {
172+
Preferences.applyPrefSettingRange("persistent_high_threshold", "170", MIN_GLUCOSE_INPUT, MAX_GLUCOSE_INPUT);
173+
}
174+
163175
}

app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.eveningoutpost.dexdrip.utils;
22

3+
4+
import static com.eveningoutpost.dexdrip.EditAlertActivity.unitsConvert2Disp;
5+
import static com.eveningoutpost.dexdrip.models.JoH.showNotification;
6+
import static com.eveningoutpost.dexdrip.models.JoH.tolerantParseDouble;
7+
import static com.eveningoutpost.dexdrip.utilitymodels.Constants.OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
38
import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName;
49
import static com.eveningoutpost.dexdrip.xdrip.gs;
510

@@ -192,6 +197,8 @@ public class Preferences extends BasePreferenceActivity implements SearchPrefere
192197
private void refreshFragments() {
193198
refreshFragments(null);
194199
}
200+
public static final double MIN_GLUCOSE_INPUT = 40; // The smallest acceptable input glucose value in mg/dL
201+
public static final double MAX_GLUCOSE_INPUT = 400; // The largest acceptable input glucose value in mg/dL
195202

196203
private void refreshFragments(final String jumpTo) {
197204
this.preferenceFragment = new AllPrefsFragment(jumpTo);
@@ -749,16 +756,21 @@ public boolean onPreferenceChange(Preference preference, Object value) {
749756
}
750757
};
751758

752-
private static Preference.OnPreferenceChangeListener sBindNumericUnitizedPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { // This listener adds glucose unit in addition to the value to the summary
759+
private static Preference.OnPreferenceChangeListener sBindNumericUnitizedPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { // This listener adds glucose unit in addition to the value to the summary and rejects out-of-range inputs
753760
@Override
754761
public boolean onPreferenceChange(Preference preference, Object value) {
755762
String stringValue = value.toString();
756763
if (isNumeric(stringValue)) {
757764
final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen
765+
double submissionMgdl = domgdl ? tolerantParseDouble(stringValue) : tolerantParseDouble(stringValue) * Constants.MMOLL_TO_MGDL;
766+
if (submissionMgdl > MAX_GLUCOSE_INPUT || submissionMgdl < MIN_GLUCOSE_INPUT) {
767+
JoH.static_toast_long(xdrip.gs(R.string.the_value_must_be_between_min_and_max, unitsConvert2Disp(domgdl, MIN_GLUCOSE_INPUT), unitsConvert2Disp(domgdl, MAX_GLUCOSE_INPUT)));
768+
return false; // Reject input if out of range
769+
}
758770
preference.setSummary(stringValue + " " + (domgdl ? "mg/dl" : "mmol/l")); // Set the summary to show the value followed by the chosen unit
759-
return true;
771+
return true; // Accept input as it is numeric and in range
760772
}
761-
return false;
773+
return false; // Reject input if not numeric
762774
}
763775
};
764776
private static Preference.OnPreferenceChangeListener sBindPreferenceTitleAppendToValueListenerUpdateChannel = new Preference.OnPreferenceChangeListener() {
@@ -1009,14 +1021,41 @@ private static void bindPreferenceSummaryToValueAndEnsureNumeric(Preference pref
10091021
.getString(preference.getKey(), ""));
10101022
}
10111023

1012-
private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Preference preference) { // Use this to show the value as well as the corresponding glucose unit as the summary
1024+
private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Preference preference) { // Use this to show the value as well as the corresponding glucose unit as the summary, and reject out-of-range inputs
10131025
preference.setOnPreferenceChangeListener(sBindNumericUnitizedPreferenceSummaryToValueListener);
10141026
sBindNumericUnitizedPreferenceSummaryToValueListener.onPreferenceChange(preference,
10151027
PreferenceManager
10161028
.getDefaultSharedPreferences(preference.getContext())
10171029
.getString(preference.getKey(), ""));
10181030
}
10191031

1032+
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
1033+
val notificationId = OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
1034+
String mySettingString = Pref.getString(pref_key, def);
1035+
final boolean doMgdl = (Pref.getString("units", "mgdl").equals("mgdl"));
1036+
double mySettingMgdl = doMgdl ? tolerantParseDouble(mySettingString) : tolerantParseDouble(mySettingString) * Constants.MMOLL_TO_MGDL; // The preference value in mg/dL
1037+
if (mySettingMgdl > max) { // If the preference value is greater than max
1038+
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.
1039+
// 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
1040+
UserError.Log.d(TAG, "Setting " + pref_key + " to default converted to mmol/L");
1041+
Pref.setString(pref_key, JoH.qs(tolerantParseDouble(def) * Constants.MGDL_TO_MMOLL, 1)); // Set the preference to the default value converted to mmol/L
1042+
} else { // The preference has been set to a value greater than the max allowed. Let's fix it and notify.
1043+
// This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged.
1044+
mySettingString = doMgdl ? max + "" : JoH.qs(max * Constants.MGDL_TO_MMOLL, 1) + "";
1045+
Pref.setString(pref_key, mySettingString); // Set the preference to max
1046+
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
1047+
showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_max), null, notificationId, null, false, false, null, null, null, true);
1048+
}
1049+
} else if (mySettingMgdl < min) { // If the preference value is less than min, correct it and notify.
1050+
// This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged.
1051+
mySettingString = doMgdl ? min + "" : JoH.qs(min * Constants.MGDL_TO_MMOLL, 1) + "";
1052+
Pref.setString(pref_key, mySettingString); // Set the preference to min
1053+
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
1054+
showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_min), null, notificationId, null, false, false, null, null, null, true);
1055+
1056+
}
1057+
}
1058+
10201059
@RequiredArgsConstructor
10211060
public static class AllPrefsFragment extends PreferenceFragment {
10221061

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,12 @@
243243
<string name="persistent_high_alert">Persistent High Alert</string>
244244
<string name="persistent_high_alert_enable">Enable</string>
245245
<string name="title_persistent_high_threshold">Threshold</string>
246+
<string name="the_value_must_be_between_min_and_max">The value must be between %1$s and %2$s</string>
246247
<string name="forecasted_low_alert">Forecasted Low Alert</string>
248+
<string name="setting_pref_to_max">Set to max</string>
249+
<string name="pref_was_greater_than_max">%1$s was set to a value greater than maximum allowed. It has been changed to maximum.</string>
250+
<string name="setting_pref_to_min">Set to min</string>
251+
<string name="pref_was_less_than_min">%1$s was set to a value less than minimum allowed. It has been changed to minimum.</string>
247252
<string name="extrapolate_data_to_try_to_predict_lows">Extrapolate data to try to predict lows</string>
248253
<string name="alarm_at_forecasted_low_mins">Alarm at Forecasted Low mins</string>
249254
<string name="other_xdrip_plus_alerts">Other xDrip+ alerts</string>

0 commit comments

Comments
 (0)