map = new TreeMap<>();
- for (V v : versionDescriptions) {
- map.put(v.version, v.description);
- }
- return map;
- }
-
-
- static class InvalidSettingValueException extends Exception {
- private static final long serialVersionUID = 1L;
-
- public InvalidSettingValueException() {
- }
-
- public InvalidSettingValueException(String message) {
- super(message);
- }
- }
-
- /**
- * Describes a setting.
- *
- *
- * Instances of this class are used to convert the string representations of setting values to
- * an internal representation (e.g. an integer) and back.
- *
- * Currently we use two different string representations:
- *
- *
- * -
- * The one that is used by the internal preference {@link Storage}. It is usually obtained by
- * calling {@code toString()} on the internal representation of the setting value (see e.g.
- * {@link K9#save(StorageEditor)}).
- *
- * -
- * The "pretty" version that is used by the import/export settings file (e.g. colors are
- * exported in #rrggbb format instead of a integer string like "-8734021").
- *
- *
- *
- * Note:
- * For the future we should aim to get rid of the "internal" string representation. The
- * "pretty" version makes reading a database dump easier and the performance impact should be
- * negligible.
- *
- */
- abstract static class SettingsDescription {
- /**
- * The setting's default value (internal representation).
- */
- T defaultValue;
-
- SettingsDescription(T defaultValue) {
- this.defaultValue = defaultValue;
- }
-
- /**
- * Get the default value.
- *
- * @return The internal representation of the default value.
- */
- public T getDefaultValue() {
- return defaultValue;
- }
-
- /**
- * Convert a setting's value to the string representation.
- *
- * @param value
- * The internal representation of a setting's value.
- *
- * @return The string representation of {@code value}.
- */
- public String toString(T value) {
- return value.toString();
- }
-
- /**
- * Parse the string representation of a setting's value .
- *
- * @param value
- * The string representation of a setting's value.
- *
- * @return The internal representation of the setting's value.
- *
- * @throws InvalidSettingValueException
- * If {@code value} contains an invalid value.
- */
- public abstract T fromString(String value) throws InvalidSettingValueException;
-
- /**
- * 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);
- }
-
- /**
- * 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);
- }
- }
-
- public static class V {
- public final Integer version;
- public final SettingsDescription description;
-
- V(Integer version, SettingsDescription description) {
- this.version = version;
- this.description = description;
- }
- }
-
- /**
- * Used for a nontrivial settings upgrade.
- *
- * @see Settings#upgrade(int, Map, Map, Map)
- */
- public interface SettingsUpgrader {
- /**
- * Upgrade the provided settings.
- *
- * @param settings
- * The settings to upgrade. This map is modified and contains the upgraded
- * settings when this method returns.
- *
- * @return A set of setting names that were removed during the upgrade process or
- * {@code null} if none were removed.
- */
- Set upgrade(Map settings);
- }
-
-
- static class StringSetting extends SettingsDescription {
- StringSetting(String defaultValue) {
- super(defaultValue);
- }
-
- @Override
- public String fromString(String value) {
- return value;
- }
-
- @Override
- public String toString(String value) {
- return value;
- }
- }
-
- static class BooleanSetting extends SettingsDescription {
- BooleanSetting(boolean defaultValue) {
- super(defaultValue);
- }
-
- @Override
- public Boolean fromString(String value) throws InvalidSettingValueException {
- if (Boolean.TRUE.toString().equals(value)) {
- return true;
- } else if (Boolean.FALSE.toString().equals(value)) {
- return false;
- }
- throw new InvalidSettingValueException();
- }
- }
-
- static class ColorSetting extends SettingsDescription {
- ColorSetting(int defaultValue) {
- super(defaultValue);
- }
-
- @Override
- public Integer fromString(String value) throws InvalidSettingValueException {
- try {
- return Integer.parseInt(value);
- } catch (NumberFormatException e) {
- throw new InvalidSettingValueException();
- }
- }
-
- @Override
- public String toPrettyString(Integer value) {
- int color = value & 0x00FFFFFF;
- return String.format("#%06x", color);
- }
-
- @Override
- public Integer fromPrettyString(String value) throws InvalidSettingValueException {
- try {
- if (value.length() == 7) {
- return Integer.parseInt(value.substring(1), 16) | 0xFF000000;
- }
- } catch (NumberFormatException e) { /* do nothing */ }
-
- throw new InvalidSettingValueException();
- }
- }
-
- static class EnumSetting> extends SettingsDescription {
- private Class enumClass;
-
- EnumSetting(Class enumClass, T defaultValue) {
- super(defaultValue);
- this.enumClass = enumClass;
- }
-
- @Override
- public T fromString(String value) throws InvalidSettingValueException {
- try {
- return Enum.valueOf(enumClass, value);
- } catch (Exception e) {
- throw new InvalidSettingValueException();
- }
- }
- }
-
- /**
- * A setting that has multiple valid values but doesn't use an {@link Enum} internally.
- *
- * @param
- * The type of the internal representation (e.g. {@code Integer}).
- */
- abstract static class PseudoEnumSetting extends SettingsDescription {
- PseudoEnumSetting(T defaultValue) {
- super(defaultValue);
- }
-
- protected abstract Map getMapping();
-
- @Override
- public String toPrettyString(T value) {
- return getMapping().get(value);
- }
-
- @Override
- public T fromPrettyString(String value) throws InvalidSettingValueException {
- for (Entry entry : getMapping().entrySet()) {
- if (entry.getValue().equals(value)) {
- return entry.getKey();
- }
- }
-
- throw new InvalidSettingValueException();
- }
- }
-
- static class FontSizeSetting extends PseudoEnumSetting {
- private final Map mapping;
-
- FontSizeSetting(int defaultValue) {
- super(defaultValue);
-
- Map mapping = new HashMap<>();
- mapping.put(FontSizes.FONT_10SP, "tiniest");
- mapping.put(FontSizes.FONT_12SP, "tiny");
- mapping.put(FontSizes.SMALL, "smaller");
- mapping.put(FontSizes.FONT_16SP, "small");
- mapping.put(FontSizes.MEDIUM, "medium");
- mapping.put(FontSizes.FONT_20SP, "large");
- mapping.put(FontSizes.LARGE, "larger");
- this.mapping = Collections.unmodifiableMap(mapping);
- }
-
- @Override
- protected Map