1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00

Refactor known preference values code to be reusable

This commit is contained in:
José Rebelo 2023-12-05 11:45:32 +00:00
parent 2dacdcface
commit 3ab1ac26db
10 changed files with 248 additions and 158 deletions

View File

@ -54,6 +54,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
@ -121,7 +122,7 @@ public class GBApplication extends Application {
private static SharedPreferences sharedPrefs;
private static final String PREFS_VERSION = "shared_preferences_version";
//if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version
private static final int CURRENT_PREFS_VERSION = 26;
private static final int CURRENT_PREFS_VERSION = 27;
private static LimitedQueue mIDSenderLookup = new LimitedQueue(16);
private static Prefs prefs;
@ -1372,6 +1373,37 @@ public class GBApplication extends Application {
}
}
if (oldVersion < 27) {
try (DBHandler db = acquireDB()) {
final DaoSession daoSession = db.getDaoSession();
final List<Device> activeDevices = DBHelper.getActiveDevices(daoSession);
for (final Device dbDevice : activeDevices) {
final SharedPreferences deviceSharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(dbDevice.getIdentifier());
final SharedPreferences.Editor deviceSharedPrefsEdit = deviceSharedPrefs.edit();
for (final Map.Entry<String, ?> entry : deviceSharedPrefs.getAll().entrySet()) {
final String key = entry.getKey();
if (key.startsWith("huami_2021_known_config_")) {
deviceSharedPrefsEdit.putString(
key.replace("huami_2021_known_config_", "") + "_is_known",
entry.getValue().toString()
);
} else if (key.endsWith("_huami_2021_possible_values")) {
deviceSharedPrefsEdit.putString(
key.replace("_huami_2021_possible_values", "") + "_possible_values",
entry.getValue().toString()
);
}
}
deviceSharedPrefsEdit.apply();
}
} catch (Exception e) {
Log.w(TAG, "error acquiring DB lock");
}
}
editor.putString(PREFS_VERSION, Integer.toString(CURRENT_PREFS_VERSION));
editor.apply();
}

View File

@ -0,0 +1,184 @@
/* Copyright (C) 2023 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.Preference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public final class DeviceSettingsUtils {
private static final Logger LOG = LoggerFactory.getLogger(DeviceSettingsUtils.class);
private DeviceSettingsUtils() {
// utility class
}
/**
* Returns the preference key where to save the list of possible value for a preference, comma-separated.
*/
public static String getPrefPossibleValuesKey(final String key) {
return String.format(Locale.ROOT, "%s_possible_values", key);
}
/**
* Returns the preference key where to that a config was reported as supported (boolean).
*/
public static String getPrefKnownConfig(final String key) {
return String.format(Locale.ROOT, "%s_is_known", key);
}
/**
* Removes all unsupported elements from a list preference. If they are not known, the preference
* is hidden.
*/
public static void removeUnsupportedElementsFromListPreference(final String prefKey,
final DeviceSpecificSettingsHandler handler,
final Prefs prefs) {
final Preference pref = handler.findPreference(prefKey);
if (pref == null) {
return;
}
// Get the list of possible values for this preference, as reported by the band
final List<String> possibleValues = prefs.getList(getPrefPossibleValuesKey(prefKey), null);
if (possibleValues == null || possibleValues.isEmpty()) {
// The band hasn't reported this setting, so we don't know the possible values.
// Hide it
pref.setVisible(false);
return;
}
final CharSequence[] originalEntries;
final CharSequence[] originalValues;
if (pref instanceof ListPreference) {
originalEntries = ((ListPreference) pref).getEntries();
originalValues = ((ListPreference) pref).getEntryValues();
} else if (pref instanceof MultiSelectListPreference) {
originalEntries = ((MultiSelectListPreference) pref).getEntries();
originalValues = ((MultiSelectListPreference) pref).getEntryValues();
} else {
LOG.error("Unknown list pref class {}", pref.getClass().getName());
return;
}
final List<String> prefValues = new ArrayList<>(originalValues.length);
for (final CharSequence entryValue : originalValues) {
prefValues.add(entryValue.toString());
}
final CharSequence[] entries = new CharSequence[possibleValues.size()];
final CharSequence[] values = new CharSequence[possibleValues.size()];
for (int i = 0; i < possibleValues.size(); i++) {
final String possibleValue = possibleValues.get(i);
final int idxPrefValue = prefValues.indexOf(possibleValue);
if (idxPrefValue >= 0) {
entries[i] = originalEntries[idxPrefValue];
} else {
entries[i] = handler.getContext().getString(R.string.menuitem_unknown_app, possibleValue);
}
values[i] = possibleValue;
}
if (pref instanceof ListPreference) {
((ListPreference) pref).setEntries(entries);
((ListPreference) pref).setEntryValues(values);
} else if (pref instanceof MultiSelectListPreference) {
((MultiSelectListPreference) pref).setEntries(entries);
((MultiSelectListPreference) pref).setEntryValues(values);
}
}
/**
* Hides the the prefToHide preference if none of the preferences in the preferences list are
* visible.
*/
public static void hidePrefIfNoneVisible(final DeviceSpecificSettingsHandler handler,
final String prefToHide,
final List<String> subPrefs) {
final Preference pref = handler.findPreference(prefToHide);
if (pref == null) {
return;
}
for (final String subPrefKey : subPrefs) {
final Preference subPref = handler.findPreference(subPrefKey);
if (subPref == null) {
continue;
}
if (subPref.isVisible()) {
// At least one preference is visible
return;
}
}
// No preference was visible, hide
pref.setVisible(false);
}
public static void enforceMinMax(final EditTextPreference pref, final int minValue, final int maxValue) {
if (minValue >= maxValue) {
LOG.warn("Invalid min/max values for {}: {}/{}", pref.getKey(), minValue, maxValue);
return;
}
pref.setOnBindEditTextListener(p -> {
p.setInputType(InputType.TYPE_CLASS_NUMBER);
p.setFilters(new InputFilter[]{new MinMaxInputFilter(minValue, maxValue)});
p.setSelection(p.getText().length());
});
}
public static final class MinMaxInputFilter implements InputFilter {
private final int min;
private final int max;
public MinMaxInputFilter(final int min, final int max) {
this.min = min;
this.max = max;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
final int input = Integer.parseInt(dest.toString() + source.toString());
if (input >= min && input <= max) {
return null;
}
} catch (final NumberFormatException ignored) {
}
return "";
}
}
}

View File

@ -33,6 +33,7 @@ import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AppManagerActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer;
import nodomain.freeyourgadget.gadgetbridge.capabilities.HeartRateCapability;
import nodomain.freeyourgadget.gadgetbridge.capabilities.password.PasswordCapabilityImpl;
@ -560,22 +561,8 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
return ZeppOsConfigService.deviceHasConfig(getPrefs(device), config);
}
/**
* Returns the preference key where to save the list of possible value for a preference, comma-separated.
*/
public static String getPrefPossibleValuesKey(final String key) {
return String.format(Locale.ROOT, "%s_huami_2021_possible_values", key);
}
/**
* Returns the preference key where to that a config was reported as supported (boolean).
*/
public static String getPrefKnownConfig(final String key) {
return String.format(Locale.ROOT, "huami_2021_known_config_%s", key);
}
public static boolean deviceHasConfig(final Prefs devicePrefs, final ZeppOsConfigService.ConfigArg config) {
return devicePrefs.getBoolean(Huami2021Coordinator.getPrefKnownConfig(config.name()), false);
return devicePrefs.getBoolean(DeviceSettingsUtils.getPrefKnownConfig(config.name()), false);
}
public static boolean experimentalFeatures(final GBDevice device) {

View File

@ -16,6 +16,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.huami;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils.hidePrefIfNoneVisible;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils.removeUnsupportedElementsFromListPreference;
import android.os.Parcel;
import android.text.InputFilter;
import android.text.InputType;
@ -41,6 +44,7 @@ import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsHandler;
import nodomain.freeyourgadget.gadgetbridge.activities.loyaltycards.LoyaltyCardsSettingsConst;
import nodomain.freeyourgadget.gadgetbridge.capabilities.GpsCapability;
@ -388,70 +392,6 @@ public class Huami2021SettingsCustomizer extends HuamiSettingsCustomizer {
}
}
/**
* Removes all unsupported elements from a list preference. If they are not known, the preference
* is hidden.
*/
private void removeUnsupportedElementsFromListPreference(final String prefKey,
final DeviceSpecificSettingsHandler handler,
final Prefs prefs) {
final Preference pref = handler.findPreference(prefKey);
if (pref == null) {
return;
}
// Get the list of possible values for this preference, as reported by the band
final List<String> possibleValues = prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(prefKey), null);
if (possibleValues == null || possibleValues.isEmpty()) {
// The band hasn't reported this setting, so we don't know the possible values.
// Hide it
pref.setVisible(false);
return;
}
final CharSequence[] originalEntries;
final CharSequence[] originalValues;
if (pref instanceof ListPreference) {
originalEntries = ((ListPreference) pref).getEntries();
originalValues = ((ListPreference) pref).getEntryValues();
} else if (pref instanceof MultiSelectListPreference) {
originalEntries = ((MultiSelectListPreference) pref).getEntries();
originalValues = ((MultiSelectListPreference) pref).getEntryValues();
} else {
LOG.error("Unknown list pref class {}", pref.getClass().getName());
return;
}
final List<String> prefValues = new ArrayList<>(originalValues.length);
for (final CharSequence entryValue : originalValues) {
prefValues.add(entryValue.toString());
}
final CharSequence[] entries = new CharSequence[possibleValues.size()];
final CharSequence[] values = new CharSequence[possibleValues.size()];
for (int i = 0; i < possibleValues.size(); i++) {
final String possibleValue = possibleValues.get(i);
final int idxPrefValue = prefValues.indexOf(possibleValue);
if (idxPrefValue >= 0) {
entries[i] = originalEntries[idxPrefValue];
} else {
entries[i] = handler.getContext().getString(R.string.menuitem_unknown_app, possibleValue);
}
values[i] = possibleValue;
}
if (pref instanceof ListPreference) {
((ListPreference) pref).setEntries(entries);
((ListPreference) pref).setEntryValues(values);
} else if (pref instanceof MultiSelectListPreference) {
((MultiSelectListPreference) pref).setEntries(entries);
((MultiSelectListPreference) pref).setEntryValues(values);
}
}
/**
* Hides prefToHide if no configuration from the list has been reported by the band.
*/
@ -465,7 +405,7 @@ public class Huami2021SettingsCustomizer extends HuamiSettingsCustomizer {
}
for (final String prefKey : supportedPref) {
final boolean deviceHasConfig = prefs.getBoolean(Huami2021Coordinator.getPrefKnownConfig(prefKey), false);
final boolean deviceHasConfig = prefs.getBoolean(DeviceSettingsUtils.getPrefKnownConfig(prefKey), false);
if (deviceHasConfig) {
// This preference is supported, don't hide
return;
@ -476,33 +416,6 @@ public class Huami2021SettingsCustomizer extends HuamiSettingsCustomizer {
pref.setVisible(false);
}
/**
* Hides the the prefToHide preference if none of the preferences in the preferences list are
* visible.
*/
private void hidePrefIfNoneVisible(final DeviceSpecificSettingsHandler handler,
final String prefToHide,
final List<String> subPrefs) {
final Preference pref = handler.findPreference(prefToHide);
if (pref == null) {
return;
}
for (final String subPrefKey : subPrefs) {
final Preference subPref = handler.findPreference(subPrefKey);
if (subPref == null) {
continue;
}
if (subPref.isVisible()) {
// At least one preference is visible
return;
}
}
// No preference was visible, hide
pref.setVisible(false);
}
private void enforceMinMax(final DeviceSpecificSettingsHandler handler, final Prefs prefs, final ZeppOsConfigService.ConfigArg config) {
final String prefKey = config.getPrefKey();
final Preference pref = handler.findPreference(prefKey);
@ -526,17 +439,7 @@ public class Huami2021SettingsCustomizer extends HuamiSettingsCustomizer {
return;
}
if (minValue >= maxValue) {
LOG.warn("Invalid min/max values: {}/{}", minValue, maxValue);
return;
}
final EditTextPreference textPref = (EditTextPreference) pref;
textPref.setOnBindEditTextListener(editText -> {
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setFilters(new InputFilter[]{new MinMaxInputFilter(minValue, maxValue)});
editText.setSelection(editText.getText().length());
});
DeviceSettingsUtils.enforceMinMax((EditTextPreference) pref, minValue, maxValue);
}
public static final Creator<Huami2021SettingsCustomizer> CREATOR = new Creator<Huami2021SettingsCustomizer>() {
@ -553,26 +456,4 @@ public class Huami2021SettingsCustomizer extends HuamiSettingsCustomizer {
return new Huami2021SettingsCustomizer[size];
}
};
public static final class MinMaxInputFilter implements InputFilter {
private final int min;
private final int max;
public MinMaxInputFilter(final int min, final int max) {
this.min = min;
this.max = max;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
final int input = Integer.parseInt(dest.toString() + source.toString());
if (input >= min && input <= max) {
return null;
}
} catch (final NumberFormatException ignored) {
}
return "";
}
}
}

View File

@ -34,6 +34,7 @@ import java.util.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
@ -385,7 +386,7 @@ public class ZeppOsAlexaService extends AbstractZeppOsService {
final GBDeviceEventUpdatePreferences evt = new GBDeviceEventUpdatePreferences()
.withPreference(PREF_VOICE_SERVICE_LANGUAGE, currentLanguage.replace("-", "_"))
.withPreference(Huami2021Coordinator.getPrefPossibleValuesKey(PREF_VOICE_SERVICE_LANGUAGE), String.join(",", allLanguages).replace("-", "_"));
.withPreference(DeviceSettingsUtils.getPrefPossibleValuesKey(PREF_VOICE_SERVICE_LANGUAGE), String.join(",", allLanguages).replace("-", "_"));
getSupport().evaluateGBDeviceEvent(evt);
}

View File

@ -60,6 +60,7 @@ import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.capabilities.GpsCapability;
import nodomain.freeyourgadget.gadgetbridge.capabilities.WorkoutDetectionCapability;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
@ -663,7 +664,7 @@ public class ZeppOsConfigService extends AbstractZeppOsService {
}
public static boolean deviceHasConfig(final Prefs devicePrefs, final ZeppOsConfigService.ConfigArg config) {
return devicePrefs.getBoolean(Huami2021Coordinator.getPrefKnownConfig(config.name()), false);
return devicePrefs.getBoolean(DeviceSettingsUtils.getPrefKnownConfig(config.name()), false);
}
public ConfigSetter newSetter() {
@ -973,7 +974,7 @@ public class ZeppOsConfigService extends AbstractZeppOsService {
}
if (configArg != null && argPrefs != null && configType == configArg.getConfigType(mGroupVersions)) {
prefs.put(Huami2021Coordinator.getPrefKnownConfig(configArg.name()), true);
prefs.put(DeviceSettingsUtils.getPrefKnownConfig(configArg.name()), true);
// Special cases for "follow phone" preferences. We need to ensure that "auto"
// always has precedence
@ -1062,7 +1063,7 @@ public class ZeppOsConfigService extends AbstractZeppOsService {
prefs = singletonMap(configArg.getPrefKey(), decoder.decode(str.getValue()));
if (includesConstraints) {
prefs.put(
Huami2021Coordinator.getPrefPossibleValuesKey(configArg.getPrefKey()),
DeviceSettingsUtils.getPrefPossibleValuesKey(configArg.getPrefKey()),
decodeStringValues(possibleValues, decoder)
);
}
@ -1147,7 +1148,7 @@ public class ZeppOsConfigService extends AbstractZeppOsService {
prefs = singletonMap(configArg.getPrefKey(), new HashSet<>(valuesList));
if (includesConstraints) {
prefs.put(
Huami2021Coordinator.getPrefPossibleValuesKey(configArg.getPrefKey()),
DeviceSettingsUtils.getPrefPossibleValuesKey(configArg.getPrefKey()),
String.join(",", decodeByteValues(possibleValues, decoder))
);
}
@ -1183,7 +1184,7 @@ public class ZeppOsConfigService extends AbstractZeppOsService {
possibleLanguages.add(languageByteToLocale(possibleValue));
}
possibleLanguages.removeAll(Collections.singleton(null));
prefs.put(Huami2021Coordinator.getPrefPossibleValuesKey(configArg.getPrefKey()), String.join(",", possibleLanguages));
prefs.put(DeviceSettingsUtils.getPrefPossibleValuesKey(configArg.getPrefKey()), String.join(",", possibleLanguages));
}
}
decoder = null;
@ -1239,7 +1240,7 @@ public class ZeppOsConfigService extends AbstractZeppOsService {
prefs = singletonMap(configArg.getPrefKey(), decoder.decode(value.getValue()));
if (includesConstraints) {
prefs.put(
Huami2021Coordinator.getPrefPossibleValuesKey(configArg.getPrefKey()),
DeviceSettingsUtils.getPrefPossibleValuesKey(configArg.getPrefKey()),
String.join(",", decodeByteValues(possibleValues, decoder))
);
}

View File

@ -32,6 +32,7 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst;
@ -98,7 +99,7 @@ public class ZeppOsDisplayItemsService extends AbstractZeppOsService {
case HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE:
setDisplayItems(
DISPLAY_ITEMS_MENU,
new ArrayList<>(prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE), Collections.emptyList())),
new ArrayList<>(prefs.getList(DeviceSettingsUtils.getPrefPossibleValuesKey(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE), Collections.emptyList())),
new ArrayList<>(prefs.getList(HuamiConst.PREF_DISPLAY_ITEMS_SORTABLE, Collections.emptyList()))
);
return true;
@ -106,14 +107,14 @@ public class ZeppOsDisplayItemsService extends AbstractZeppOsService {
case HuamiConst.PREF_SHORTCUTS_SORTABLE:
setDisplayItems(
DISPLAY_ITEMS_SHORTCUTS,
new ArrayList<>(prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(HuamiConst.PREF_SHORTCUTS_SORTABLE), Collections.emptyList())),
new ArrayList<>(prefs.getList(DeviceSettingsUtils.getPrefPossibleValuesKey(HuamiConst.PREF_SHORTCUTS_SORTABLE), Collections.emptyList())),
new ArrayList<>(prefs.getList(HuamiConst.PREF_SHORTCUTS_SORTABLE, Collections.emptyList()))
);
return true;
case HuamiConst.PREF_CONTROL_CENTER_SORTABLE:
setDisplayItems(
DISPLAY_ITEMS_CONTROL_CENTER,
new ArrayList<>(prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(HuamiConst.PREF_CONTROL_CENTER_SORTABLE), Collections.emptyList())),
new ArrayList<>(prefs.getList(DeviceSettingsUtils.getPrefPossibleValuesKey(HuamiConst.PREF_CONTROL_CENTER_SORTABLE), Collections.emptyList())),
new ArrayList<>(prefs.getList(HuamiConst.PREF_CONTROL_CENTER_SORTABLE, Collections.emptyList()))
);
return true;
@ -169,7 +170,7 @@ public class ZeppOsDisplayItemsService extends AbstractZeppOsService {
LOG.error("Unknown display items type {}", String.format("0x%x", payload[1]));
return;
}
final String allScreensPrefKey = Huami2021Coordinator.getPrefPossibleValuesKey(prefKey);
final String allScreensPrefKey = DeviceSettingsUtils.getPrefPossibleValuesKey(prefKey);
final boolean menuHasMoreSection;

View File

@ -30,6 +30,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@ -92,7 +93,7 @@ public class ZeppOsMorningUpdatesService extends AbstractZeppOsService {
return;
}
final GBDeviceEventUpdatePreferences gbDeviceEventUpdatePreferences = new GBDeviceEventUpdatePreferences()
.withPreference(Huami2021Coordinator.getPrefKnownConfig(DeviceSettingsPreferenceConst.MORNING_UPDATES_ENABLED), true)
.withPreference(DeviceSettingsUtils.getPrefKnownConfig(DeviceSettingsPreferenceConst.MORNING_UPDATES_ENABLED), true)
.withPreference(DeviceSettingsPreferenceConst.MORNING_UPDATES_ENABLED, enabled);
getSupport().evaluateGBDeviceEvent(gbDeviceEventUpdatePreferences);
LOG.info("Morning updates enabled = {}", enabled);
@ -122,7 +123,7 @@ public class ZeppOsMorningUpdatesService extends AbstractZeppOsService {
return true;
case DeviceSettingsPreferenceConst.MORNING_UPDATES_CATEGORIES_SORTABLE:
final List<String> categories = new ArrayList<>(prefs.getList(config, Collections.emptyList()));
final List<String> allCategories = new ArrayList<>(prefs.getList(Huami2021Coordinator.getPrefPossibleValuesKey(config), Collections.emptyList()));
final List<String> allCategories = new ArrayList<>(prefs.getList(DeviceSettingsUtils.getPrefPossibleValuesKey(config), Collections.emptyList()));
LOG.info("Setting morning updates categories = {}", categories);
setCategories(categories, allCategories);
return true;
@ -222,12 +223,12 @@ public class ZeppOsMorningUpdatesService extends AbstractZeppOsService {
}
final String prefKey = DeviceSettingsPreferenceConst.MORNING_UPDATES_CATEGORIES_SORTABLE;
final String allCategoriesPrefKey = Huami2021Coordinator.getPrefPossibleValuesKey(prefKey);
final String allCategoriesPrefKey = DeviceSettingsUtils.getPrefPossibleValuesKey(prefKey);
final String allCategoriesPrefValue = StringUtils.join(",", allCategories.toArray(new String[0])).toString();
final String prefValue = StringUtils.join(",", enabledCategories.toArray(new String[0])).toString();
final GBDeviceEventUpdatePreferences eventUpdatePreferences = new GBDeviceEventUpdatePreferences()
.withPreference(Huami2021Coordinator.getPrefKnownConfig(prefKey), true)
.withPreference(DeviceSettingsUtils.getPrefKnownConfig(prefKey), true)
.withPreference(allCategoriesPrefKey, allCategoriesPrefValue)
.withPreference(prefKey, prefValue);

View File

@ -34,6 +34,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
@ -218,7 +219,7 @@ public class ZeppOsShortcutCardsService extends AbstractZeppOsService {
final GBDeviceEventUpdatePreferences evt = new GBDeviceEventUpdatePreferences()
.withPreference(SHORTCUT_CARDS_SORTABLE, String.join(",", enabledCards))
.withPreference(Huami2021Coordinator.getPrefPossibleValuesKey(SHORTCUT_CARDS_SORTABLE), String.join(",", allCards));
.withPreference(DeviceSettingsUtils.getPrefPossibleValuesKey(SHORTCUT_CARDS_SORTABLE), String.join(",", allCards));
getSupport().evaluateGBDeviceEvent(evt);
}

View File

@ -34,6 +34,7 @@ import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsUtils;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.Huami2021Coordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
@ -228,7 +229,7 @@ public class ZeppOsWatchfaceService extends AbstractZeppOsService {
// TODO broadcast something to update app manager
final GBDeviceEventUpdatePreferences evt = new GBDeviceEventUpdatePreferences()
.withPreference(Huami2021Coordinator.getPrefPossibleValuesKey(PREF_WATCHFACE), String.join(",", watchfacePrefValues));
.withPreference(DeviceSettingsUtils.getPrefPossibleValuesKey(PREF_WATCHFACE), String.join(",", watchfacePrefValues));
getSupport().evaluateGBDeviceEvent(evt);
}