1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 19:52:17 -05:00

Use internal representation for settings during the upgrade process

This avoids constant conversion between the string representation used
in preference storage and the internal representation.
This commit is contained in:
cketti 2011-12-11 06:02:31 +01:00
parent 345f4c2939
commit 61ca1a2ae2
6 changed files with 99 additions and 29 deletions

View File

@ -211,15 +211,19 @@ public class AccountSettings {
UPGRADERS = Collections.unmodifiableMap(u);
}
public static Map<String, String> validate(int version, Map<String, String> importedSettings,
public static Map<String, Object> validate(int version, Map<String, String> importedSettings,
boolean useDefaultValues) {
return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues);
}
public static Set<String> upgrade(int version, Map<String, String> validatedSettings) {
public static Set<String> upgrade(int version, Map<String, Object> validatedSettings) {
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}
public static Map<String, String> getAccountSettings(SharedPreferences storage, String uuid) {
Map<String, String> result = new HashMap<String, String>();
String prefix = uuid + ".";

View File

@ -42,15 +42,19 @@ public class FolderSettings {
UPGRADERS = Collections.unmodifiableMap(u);
}
public static Map<String, String> validate(int version, Map<String, String> importedSettings,
public static Map<String, Object> validate(int version, Map<String, String> importedSettings,
boolean useDefaultValues) {
return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues);
}
public static Set<String> upgrade(int version, Map<String, String> validatedSettings) {
public static Set<String> upgrade(int version, Map<String, Object> validatedSettings) {
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}
public static Map<String, String> getFolderSettings(SharedPreferences storage, String uuid,
String folderName) {
Map<String, String> result = new HashMap<String, String>();

View File

@ -198,14 +198,18 @@ public class GlobalSettings {
UPGRADERS = Collections.unmodifiableMap(u);
}
public static Map<String, String> validate(int version, Map<String, String> importedSettings) {
public static Map<String, Object> validate(int version, Map<String, String> importedSettings) {
return Settings.validate(version, SETTINGS, importedSettings, false);
}
public static Set<String> upgrade(int version, Map<String, String> validatedSettings) {
public static Set<String> upgrade(int version, Map<String, Object> validatedSettings) {
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}
public static Map<String, String> getGlobalSettings(SharedPreferences storage) {
Map<String, String> result = new HashMap<String, String>();
for (String key : SETTINGS.keySet()) {

View File

@ -38,15 +38,19 @@ public class IdentitySettings {
UPGRADERS = Collections.unmodifiableMap(u);
}
public static Map<String, String> validate(int version, Map<String, String> importedSettings,
public static Map<String, Object> validate(int version, Map<String, String> importedSettings,
boolean useDefaultValues) {
return Settings.validate(version, SETTINGS, importedSettings, useDefaultValues);
}
public static Set<String> upgrade(int version, Map<String, String> validatedSettings) {
public static Set<String> upgrade(int version, Map<String, Object> validatedSettings) {
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
}
public static Map<String, String> convert(Map<String, Object> settings) {
return Settings.convert(settings, SETTINGS);
}
public static Map<String, String> getIdentitySettings(SharedPreferences storage, String uuid,
int identityIndex) {
Map<String, String> result = new HashMap<String, String>();
@ -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();

View File

@ -36,11 +36,11 @@ public class Settings {
*/
public static final int VERSION = 3;
public static Map<String, String> validate(int version, Map<String,
public static Map<String, Object> validate(int version, Map<String,
TreeMap<Integer, SettingsDescription>> settings,
Map<String, String> importedSettings, boolean useDefaultValues) {
Map<String, String> validatedSettings = new HashMap<String, String>();
Map<String, Object> validatedSettings = new HashMap<String, Object>();
for (Map.Entry<String, TreeMap<Integer, SettingsDescription>> 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<String> upgrade(int version, Map<Integer, SettingsUpgrader> upgraders,
Map<String, TreeMap<Integer, SettingsDescription>> settings,
Map<String, String> validatedSettings) {
Map<String, Object> validatedSettings) {
Map<String, String> upgradedSettings = validatedSettings;
Map<String, Object> upgradedSettings = validatedSettings;
Set<String> 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<String, String> convert(Map<String, Object> settings,
Map<String, TreeMap<Integer, SettingsDescription>> settingDescriptions) {
Map<String, String> serializedSettings = new HashMap<String, String>();
for (Entry<String, Object> 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<String> upgrade(Map<String, String> settings);
public Set<String> upgrade(Map<String, Object> settings);
}

View File

@ -304,7 +304,7 @@ public class SettingsImporter {
SharedPreferences.Editor editor, int contentVersion, ImportedSettings settings) {
// Validate global settings
Map<String, String> validatedSettings = GlobalSettings.validate(contentVersion,
Map<String, Object> 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<String, String> stringSettings = GlobalSettings.convert(validatedSettings);
// Use current global settings as base and overwrite with validated settings read from the
// import file.
Map<String, String> mergedSettings =
new HashMap<String, String>(GlobalSettings.getGlobalSettings(storage));
mergedSettings.putAll(validatedSettings);
mergedSettings.putAll(stringSettings);
for (Map.Entry<String, String> setting : mergedSettings.entrySet()) {
String key = setting.getKey();
@ -398,7 +401,7 @@ public class SettingsImporter {
}
// Validate account settings
Map<String, String> validatedSettings =
Map<String, Object> 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<String, String> stringSettings = AccountSettings.convert(validatedSettings);
// Merge account settings if necessary
Map<String, String> writeSettings;
if (mergeImportedAccount) {
writeSettings = new HashMap<String, String>(
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<String, String> validatedSettings =
Map<String, Object> 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<String, String> stringSettings = FolderSettings.convert(validatedSettings);
// Merge folder settings if necessary
Map<String, String> 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<String, String> validatedSettings = IdentitySettings.validate(
Map<String, Object> 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<String, String> stringSettings = IdentitySettings.convert(validatedSettings);
// Merge identity settings if necessary
Map<String, String> writeSettings;
if (mergeSettings) {
writeSettings = new HashMap<String, String>(IdentitySettings.getIdentitySettings(
prefs.getPreferences(), uuid, writeIdentityIndex));
writeSettings.putAll(validatedSettings);
writeSettings.putAll(stringSettings);
} else {
writeSettings = validatedSettings;
writeSettings = stringSettings;
}
// Write identity settings