String prettyValue = importedSettings.get(key); try { Object internalValue = desc.fromPrettyString(prettyValue); validatedSettings.put(key, internalValue); useDefaultValue = false; Object defaultValue = desc.getDefaultValue(); validatedSettings.put(key, defaultValue);
private static <T> void upgradeSettingInsertDefault(Map<String, Object> validatedSettingsMutable, String settingName, SettingsDescription<T> setting) { T defaultValue = setting.getDefaultValue(); validatedSettingsMutable.put(settingName, defaultValue); if (K9.isDebug()) { String prettyValue = setting.toPrettyString(defaultValue); Timber.v("Added new setting \"%s\" with default value \"%s\"", settingName, prettyValue); } }
/** * Convert settings from the internal representation to the string representation used in the * preference storage. * * @param settings * The map of settings to convert. * @param settingDescriptions * The structure containing the {@link SettingsDescription} objects that will be used * to convert the setting values. * * @return The settings converted to the string representation used in the preference storage. */ public static Map<String, String> convert(Map<String, Object> settings, Map<String, TreeMap<Integer, SettingsDescription>> settingDescriptions) { Map<String, String> serializedSettings = new HashMap<>(); for (Entry<String, Object> setting : settings.entrySet()) { String settingName = setting.getKey(); Object internalValue = setting.getValue(); TreeMap<Integer, SettingsDescription> versionedSetting = settingDescriptions.get(settingName); Integer highestVersion = versionedSetting.lastKey(); SettingsDescription settingDesc = versionedSetting.get(highestVersion); if (settingDesc != null) { String stringValue = settingDesc.toString(internalValue); serializedSettings.put(settingName, stringValue); } else { Timber.w("Settings.convert() called with a setting that should have been removed: %s", settingName); } } return serializedSettings; }
/** * Convert the pretty-printed version of a setting's value to the internal representation. * * @param value * The pretty-printed version of the setting's value. See * {@link #toPrettyString(Object)}. * * @return The internal representation of the setting's value. * * @throws InvalidSettingValueException * If {@code value} contains an invalid value. */ public T fromPrettyString(String value) throws InvalidSettingValueException { return fromString(value); } }
/** * Convert a setting value to the "pretty" string representation. * * @param value * The setting's value. * * @return A pretty-printed version of the setting's value. */ public String toPrettyString(T value) { return toString(value); }
private static <T> void writeKeyAndDefaultValueFromSetting(XmlSerializer serializer, String key, SettingsDescription<T> setting) throws IllegalArgumentException, IllegalStateException, IOException { T value = setting.getDefaultValue(); String outputValue = setting.toPrettyString(value); writeKeyAndPrettyValueFromSetting(serializer, key, outputValue); }
private static <T> void writeKeyAndPrettyValueFromSetting(XmlSerializer serializer, String key, SettingsDescription<T> setting, String valueString) throws IllegalArgumentException, IllegalStateException, IOException, InvalidSettingValueException { T value = setting.fromString(valueString); String outputValue = setting.toPrettyString(value); writeKeyAndPrettyValueFromSetting(serializer, key, outputValue); }