|
1 | 1 | package com.eveningoutpost.dexdrip.utils; |
2 | 2 |
|
| 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; |
3 | 8 | import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName; |
4 | 9 | import static com.eveningoutpost.dexdrip.xdrip.gs; |
5 | 10 |
|
@@ -192,6 +197,8 @@ public class Preferences extends BasePreferenceActivity implements SearchPrefere |
192 | 197 | private void refreshFragments() { |
193 | 198 | refreshFragments(null); |
194 | 199 | } |
| 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 |
195 | 202 |
|
196 | 203 | private void refreshFragments(final String jumpTo) { |
197 | 204 | this.preferenceFragment = new AllPrefsFragment(jumpTo); |
@@ -749,16 +756,21 @@ public boolean onPreferenceChange(Preference preference, Object value) { |
749 | 756 | } |
750 | 757 | }; |
751 | 758 |
|
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 |
753 | 760 | @Override |
754 | 761 | public boolean onPreferenceChange(Preference preference, Object value) { |
755 | 762 | String stringValue = value.toString(); |
756 | 763 | if (isNumeric(stringValue)) { |
757 | 764 | 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 | + } |
758 | 770 | 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 |
760 | 772 | } |
761 | | - return false; |
| 773 | + return false; // Reject input if not numeric |
762 | 774 | } |
763 | 775 | }; |
764 | 776 | private static Preference.OnPreferenceChangeListener sBindPreferenceTitleAppendToValueListenerUpdateChannel = new Preference.OnPreferenceChangeListener() { |
@@ -1009,14 +1021,41 @@ private static void bindPreferenceSummaryToValueAndEnsureNumeric(Preference pref |
1009 | 1021 | .getString(preference.getKey(), "")); |
1010 | 1022 | } |
1011 | 1023 |
|
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 |
1013 | 1025 | preference.setOnPreferenceChangeListener(sBindNumericUnitizedPreferenceSummaryToValueListener); |
1014 | 1026 | sBindNumericUnitizedPreferenceSummaryToValueListener.onPreferenceChange(preference, |
1015 | 1027 | PreferenceManager |
1016 | 1028 | .getDefaultSharedPreferences(preference.getContext()) |
1017 | 1029 | .getString(preference.getKey(), "")); |
1018 | 1030 | } |
1019 | 1031 |
|
| 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 | + |
1020 | 1059 | @RequiredArgsConstructor |
1021 | 1060 | public static class AllPrefsFragment extends PreferenceFragment { |
1022 | 1061 |
|
|
0 commit comments