diff --git a/src/com/fsck/k9/preferences/AccountSettings.java b/src/com/fsck/k9/preferences/AccountSettings.java index 0a5142a81..8beeb7fa3 100644 --- a/src/com/fsck/k9/preferences/AccountSettings.java +++ b/src/com/fsck/k9/preferences/AccountSettings.java @@ -211,15 +211,19 @@ public class AccountSettings { UPGRADERS = Collections.unmodifiableMap(u); } - public static Map validate(int version, Map importedSettings, + public static Map validate(int version, Map importedSettings, boolean useDefaultValues) { return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues); } - public static Set upgrade(int version, Map validatedSettings) { + public static Set upgrade(int version, Map validatedSettings) { return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings); } + public static Map convert(Map settings) { + return Settings.convert(settings, SETTINGS); + } + public static Map getAccountSettings(SharedPreferences storage, String uuid) { Map result = new HashMap(); String prefix = uuid + "."; diff --git a/src/com/fsck/k9/preferences/FolderSettings.java b/src/com/fsck/k9/preferences/FolderSettings.java index 88a050c14..6759a71ae 100644 --- a/src/com/fsck/k9/preferences/FolderSettings.java +++ b/src/com/fsck/k9/preferences/FolderSettings.java @@ -42,15 +42,19 @@ public class FolderSettings { UPGRADERS = Collections.unmodifiableMap(u); } - public static Map validate(int version, Map importedSettings, + public static Map validate(int version, Map importedSettings, boolean useDefaultValues) { return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues); } - public static Set upgrade(int version, Map validatedSettings) { + public static Set upgrade(int version, Map validatedSettings) { return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings); } + public static Map convert(Map settings) { + return Settings.convert(settings, SETTINGS); + } + public static Map getFolderSettings(SharedPreferences storage, String uuid, String folderName) { Map result = new HashMap(); diff --git a/src/com/fsck/k9/preferences/GlobalSettings.java b/src/com/fsck/k9/preferences/GlobalSettings.java index 53c0f94f7..5dcae70bb 100644 --- a/src/com/fsck/k9/preferences/GlobalSettings.java +++ b/src/com/fsck/k9/preferences/GlobalSettings.java @@ -198,14 +198,18 @@ public class GlobalSettings { UPGRADERS = Collections.unmodifiableMap(u); } - public static Map validate(int version, Map importedSettings) { + public static Map validate(int version, Map importedSettings) { return Settings.validate(version, SETTINGS, importedSettings, false); } - public static Set upgrade(int version, Map validatedSettings) { + public static Set upgrade(int version, Map validatedSettings) { return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings); } + public static Map convert(Map settings) { + return Settings.convert(settings, SETTINGS); + } + public static Map getGlobalSettings(SharedPreferences storage) { Map result = new HashMap(); for (String key : SETTINGS.keySet()) { diff --git a/src/com/fsck/k9/preferences/IdentitySettings.java b/src/com/fsck/k9/preferences/IdentitySettings.java index 343972c66..f5ff270f5 100644 --- a/src/com/fsck/k9/preferences/IdentitySettings.java +++ b/src/com/fsck/k9/preferences/IdentitySettings.java @@ -38,15 +38,19 @@ public class IdentitySettings { UPGRADERS = Collections.unmodifiableMap(u); } - public static Map validate(int version, Map importedSettings, + public static Map validate(int version, Map importedSettings, boolean useDefaultValues) { return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues); } - public static Set upgrade(int version, Map validatedSettings) { + public static Set upgrade(int version, Map validatedSettings) { return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings); } + public static Map convert(Map settings) { + return Settings.convert(settings, SETTINGS); + } + public static Map getIdentitySettings(SharedPreferences storage, String uuid, int identityIndex) { Map result = new HashMap(); @@ -104,6 +108,11 @@ public class IdentitySettings { return value; } + @Override + public String toString(Object value) { + return (value != null) ? value.toString() : null; + } + @Override public String toPrettyString(Object value) { return (value == null) ? "" : value.toString(); diff --git a/src/com/fsck/k9/preferences/Settings.java b/src/com/fsck/k9/preferences/Settings.java index 6619b84a6..207c63464 100644 --- a/src/com/fsck/k9/preferences/Settings.java +++ b/src/com/fsck/k9/preferences/Settings.java @@ -36,11 +36,11 @@ public class Settings { */ public static final int VERSION = 3; - public static Map validate(int version, Map validate(int version, Map> settings, Map importedSettings, boolean useDefaultValues) { - Map validatedSettings = new HashMap(); + Map validatedSettings = new HashMap(); for (Map.Entry> versionedSetting : settings.entrySet()) { @@ -63,8 +63,7 @@ public class Settings { String prettyValue = importedSettings.get(key); try { Object internalValue = desc.fromPrettyString(prettyValue); - String importedValue = desc.toString(internalValue); - validatedSettings.put(key, importedValue); + validatedSettings.put(key, internalValue); useDefaultValue = false; } catch (InvalidSettingValueException e) { Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + prettyValue + @@ -76,8 +75,7 @@ public class Settings { if (useDefaultValue) { Object defaultValue = desc.getDefaultValue(); - String value = (defaultValue != null) ? desc.toString(defaultValue) : null; - validatedSettings.put(key, value); + validatedSettings.put(key, defaultValue); } } @@ -103,9 +101,9 @@ public class Settings { */ public static Set upgrade(int version, Map upgraders, Map> settings, - Map validatedSettings) { + Map validatedSettings) { - Map upgradedSettings = validatedSettings; + Map upgradedSettings = validatedSettings; Set deletedSettings = null; for (int toVersion = version + 1; toVersion <= VERSION; toVersion++) { @@ -131,7 +129,7 @@ public class Settings { // Insert default value to upgradedSettings SettingsDescription setting = versionedSettings.firstEntry().getValue(); Object defaultValue = setting.getDefaultValue(); - upgradedSettings.put(settingName, setting.toString(defaultValue)); + upgradedSettings.put(settingName, defaultValue); if (K9.DEBUG) { String prettyValue = setting.toPrettyString(defaultValue); @@ -160,6 +158,45 @@ public class Settings { return deletedSettings; } + /** + * 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 convert(Map settings, + Map> settingDescriptions) { + + Map serializedSettings = new HashMap(); + + for (Entry setting : settings.entrySet()) { + String settingName = setting.getKey(); + Object internalValue = setting.getValue(); + + SettingsDescription settingDesc = + settingDescriptions.get(settingName).lastEntry().getValue(); + + if (settingDesc != null) { + String stringValue = settingDesc.toString(internalValue); + + serializedSettings.put(settingName, stringValue); + } else { + if (K9.DEBUG) { + Log.w(K9.LOG_TAG, "Settings.serialize() called with a setting that should " + + "have been removed: " + settingName); + } + } + } + + return serializedSettings; + } + /** * Creates a {@link TreeMap} linking version numbers to {@link SettingsDescription} instances. * @@ -327,7 +364,7 @@ public class Settings { * @return A set of setting names that were removed during the upgrade process or * {@code null} if none were removed. */ - public Set upgrade(Map settings); + public Set upgrade(Map settings); } diff --git a/src/com/fsck/k9/preferences/SettingsImporter.java b/src/com/fsck/k9/preferences/SettingsImporter.java index bfaa1a996..aa307ba66 100644 --- a/src/com/fsck/k9/preferences/SettingsImporter.java +++ b/src/com/fsck/k9/preferences/SettingsImporter.java @@ -304,7 +304,7 @@ public class SettingsImporter { SharedPreferences.Editor editor, int contentVersion, ImportedSettings settings) { // Validate global settings - Map validatedSettings = GlobalSettings.validate(contentVersion, + Map validatedSettings = GlobalSettings.validate(contentVersion, settings.settings); // Upgrade global settings to current content version @@ -312,11 +312,14 @@ public class SettingsImporter { GlobalSettings.upgrade(contentVersion, validatedSettings); } + // Convert global settings to the string representation used in preference storage + Map stringSettings = GlobalSettings.convert(validatedSettings); + // Use current global settings as base and overwrite with validated settings read from the // import file. Map mergedSettings = new HashMap(GlobalSettings.getGlobalSettings(storage)); - mergedSettings.putAll(validatedSettings); + mergedSettings.putAll(stringSettings); for (Map.Entry setting : mergedSettings.entrySet()) { String key = setting.getKey(); @@ -398,7 +401,7 @@ public class SettingsImporter { } // Validate account settings - Map validatedSettings = + Map validatedSettings = AccountSettings.validate(contentVersion, account.settings.settings, !mergeImportedAccount); @@ -407,14 +410,17 @@ public class SettingsImporter { AccountSettings.upgrade(contentVersion, validatedSettings); } + // Convert account settings to the string representation used in preference storage + Map stringSettings = AccountSettings.convert(validatedSettings); + // Merge account settings if necessary Map writeSettings; if (mergeImportedAccount) { writeSettings = new HashMap( AccountSettings.getAccountSettings(prefs.getPreferences(), uuid)); - writeSettings.putAll(validatedSettings); + writeSettings.putAll(stringSettings); } else { - writeSettings = validatedSettings; + writeSettings = stringSettings; } // Write account settings @@ -456,7 +462,7 @@ public class SettingsImporter { String uuid, ImportedFolder folder, boolean overwrite, Preferences prefs) { // Validate folder settings - Map validatedSettings = + Map validatedSettings = FolderSettings.validate(contentVersion, folder.settings.settings, !overwrite); // Upgrade folder settings to current content version @@ -464,14 +470,17 @@ public class SettingsImporter { FolderSettings.upgrade(contentVersion, validatedSettings); } + // Convert folder settings to the string representation used in preference storage + Map stringSettings = FolderSettings.convert(validatedSettings); + // Merge folder settings if necessary Map writeSettings; if (overwrite) { writeSettings = FolderSettings.getFolderSettings(prefs.getPreferences(), uuid, folder.name); - writeSettings.putAll(validatedSettings); + writeSettings.putAll(stringSettings); } else { - writeSettings = validatedSettings; + writeSettings = stringSettings; } // Write folder settings @@ -550,7 +559,7 @@ public class SettingsImporter { if (identity.settings != null) { // Validate identity settings - Map validatedSettings = IdentitySettings.validate( + Map validatedSettings = IdentitySettings.validate( contentVersion, identity.settings.settings, !mergeSettings); // Upgrade identity settings to current content version @@ -558,14 +567,17 @@ public class SettingsImporter { IdentitySettings.upgrade(contentVersion, validatedSettings); } + // Convert identity settings to the representation used in preference storage + Map stringSettings = IdentitySettings.convert(validatedSettings); + // Merge identity settings if necessary Map writeSettings; if (mergeSettings) { writeSettings = new HashMap(IdentitySettings.getIdentitySettings( prefs.getPreferences(), uuid, writeIdentityIndex)); - writeSettings.putAll(validatedSettings); + writeSettings.putAll(stringSettings); } else { - writeSettings = validatedSettings; + writeSettings = stringSettings; } // Write identity settings