mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-24 02:12:15 -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:
parent
345f4c2939
commit
61ca1a2ae2
@ -211,15 +211,19 @@ public class AccountSettings {
|
|||||||
UPGRADERS = Collections.unmodifiableMap(u);
|
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) {
|
boolean useDefaultValues) {
|
||||||
return Settings.validate(version, SETTINGS, importedSettings, 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);
|
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) {
|
public static Map<String, String> getAccountSettings(SharedPreferences storage, String uuid) {
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String, String> result = new HashMap<String, String>();
|
||||||
String prefix = uuid + ".";
|
String prefix = uuid + ".";
|
||||||
|
@ -42,15 +42,19 @@ public class FolderSettings {
|
|||||||
UPGRADERS = Collections.unmodifiableMap(u);
|
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) {
|
boolean useDefaultValues) {
|
||||||
return Settings.validate(version, SETTINGS, importedSettings, 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);
|
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,
|
public static Map<String, String> getFolderSettings(SharedPreferences storage, String uuid,
|
||||||
String folderName) {
|
String folderName) {
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String, String> result = new HashMap<String, String>();
|
||||||
|
@ -198,14 +198,18 @@ public class GlobalSettings {
|
|||||||
UPGRADERS = Collections.unmodifiableMap(u);
|
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);
|
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);
|
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) {
|
public static Map<String, String> getGlobalSettings(SharedPreferences storage) {
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String, String> result = new HashMap<String, String>();
|
||||||
for (String key : SETTINGS.keySet()) {
|
for (String key : SETTINGS.keySet()) {
|
||||||
|
@ -38,15 +38,19 @@ public class IdentitySettings {
|
|||||||
UPGRADERS = Collections.unmodifiableMap(u);
|
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) {
|
boolean useDefaultValues) {
|
||||||
return Settings.validate(version, SETTINGS, importedSettings, 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);
|
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,
|
public static Map<String, String> getIdentitySettings(SharedPreferences storage, String uuid,
|
||||||
int identityIndex) {
|
int identityIndex) {
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String, String> result = new HashMap<String, String>();
|
||||||
@ -104,6 +108,11 @@ public class IdentitySettings {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(Object value) {
|
||||||
|
return (value != null) ? value.toString() : null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toPrettyString(Object value) {
|
public String toPrettyString(Object value) {
|
||||||
return (value == null) ? "" : value.toString();
|
return (value == null) ? "" : value.toString();
|
||||||
|
@ -36,11 +36,11 @@ public class Settings {
|
|||||||
*/
|
*/
|
||||||
public static final int VERSION = 3;
|
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,
|
TreeMap<Integer, SettingsDescription>> settings,
|
||||||
Map<String, String> importedSettings, boolean useDefaultValues) {
|
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 :
|
for (Map.Entry<String, TreeMap<Integer, SettingsDescription>> versionedSetting :
|
||||||
settings.entrySet()) {
|
settings.entrySet()) {
|
||||||
|
|
||||||
@ -63,8 +63,7 @@ public class Settings {
|
|||||||
String prettyValue = importedSettings.get(key);
|
String prettyValue = importedSettings.get(key);
|
||||||
try {
|
try {
|
||||||
Object internalValue = desc.fromPrettyString(prettyValue);
|
Object internalValue = desc.fromPrettyString(prettyValue);
|
||||||
String importedValue = desc.toString(internalValue);
|
validatedSettings.put(key, internalValue);
|
||||||
validatedSettings.put(key, importedValue);
|
|
||||||
useDefaultValue = false;
|
useDefaultValue = false;
|
||||||
} catch (InvalidSettingValueException e) {
|
} catch (InvalidSettingValueException e) {
|
||||||
Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + prettyValue +
|
Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + prettyValue +
|
||||||
@ -76,8 +75,7 @@ public class Settings {
|
|||||||
|
|
||||||
if (useDefaultValue) {
|
if (useDefaultValue) {
|
||||||
Object defaultValue = desc.getDefaultValue();
|
Object defaultValue = desc.getDefaultValue();
|
||||||
String value = (defaultValue != null) ? desc.toString(defaultValue) : null;
|
validatedSettings.put(key, defaultValue);
|
||||||
validatedSettings.put(key, value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,9 +101,9 @@ public class Settings {
|
|||||||
*/
|
*/
|
||||||
public static Set<String> upgrade(int version, Map<Integer, SettingsUpgrader> upgraders,
|
public static Set<String> upgrade(int version, Map<Integer, SettingsUpgrader> upgraders,
|
||||||
Map<String, TreeMap<Integer, SettingsDescription>> settings,
|
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;
|
Set<String> deletedSettings = null;
|
||||||
|
|
||||||
for (int toVersion = version + 1; toVersion <= VERSION; toVersion++) {
|
for (int toVersion = version + 1; toVersion <= VERSION; toVersion++) {
|
||||||
@ -131,7 +129,7 @@ public class Settings {
|
|||||||
// Insert default value to upgradedSettings
|
// Insert default value to upgradedSettings
|
||||||
SettingsDescription setting = versionedSettings.firstEntry().getValue();
|
SettingsDescription setting = versionedSettings.firstEntry().getValue();
|
||||||
Object defaultValue = setting.getDefaultValue();
|
Object defaultValue = setting.getDefaultValue();
|
||||||
upgradedSettings.put(settingName, setting.toString(defaultValue));
|
upgradedSettings.put(settingName, defaultValue);
|
||||||
|
|
||||||
if (K9.DEBUG) {
|
if (K9.DEBUG) {
|
||||||
String prettyValue = setting.toPrettyString(defaultValue);
|
String prettyValue = setting.toPrettyString(defaultValue);
|
||||||
@ -160,6 +158,45 @@ public class Settings {
|
|||||||
return deletedSettings;
|
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.
|
* 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
|
* @return A set of setting names that were removed during the upgrade process or
|
||||||
* {@code null} if none were removed.
|
* {@code null} if none were removed.
|
||||||
*/
|
*/
|
||||||
public Set<String> upgrade(Map<String, String> settings);
|
public Set<String> upgrade(Map<String, Object> settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -304,7 +304,7 @@ public class SettingsImporter {
|
|||||||
SharedPreferences.Editor editor, int contentVersion, ImportedSettings settings) {
|
SharedPreferences.Editor editor, int contentVersion, ImportedSettings settings) {
|
||||||
|
|
||||||
// Validate global settings
|
// Validate global settings
|
||||||
Map<String, String> validatedSettings = GlobalSettings.validate(contentVersion,
|
Map<String, Object> validatedSettings = GlobalSettings.validate(contentVersion,
|
||||||
settings.settings);
|
settings.settings);
|
||||||
|
|
||||||
// Upgrade global settings to current content version
|
// Upgrade global settings to current content version
|
||||||
@ -312,11 +312,14 @@ public class SettingsImporter {
|
|||||||
GlobalSettings.upgrade(contentVersion, validatedSettings);
|
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
|
// Use current global settings as base and overwrite with validated settings read from the
|
||||||
// import file.
|
// import file.
|
||||||
Map<String, String> mergedSettings =
|
Map<String, String> mergedSettings =
|
||||||
new HashMap<String, String>(GlobalSettings.getGlobalSettings(storage));
|
new HashMap<String, String>(GlobalSettings.getGlobalSettings(storage));
|
||||||
mergedSettings.putAll(validatedSettings);
|
mergedSettings.putAll(stringSettings);
|
||||||
|
|
||||||
for (Map.Entry<String, String> setting : mergedSettings.entrySet()) {
|
for (Map.Entry<String, String> setting : mergedSettings.entrySet()) {
|
||||||
String key = setting.getKey();
|
String key = setting.getKey();
|
||||||
@ -398,7 +401,7 @@ public class SettingsImporter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate account settings
|
// Validate account settings
|
||||||
Map<String, String> validatedSettings =
|
Map<String, Object> validatedSettings =
|
||||||
AccountSettings.validate(contentVersion, account.settings.settings,
|
AccountSettings.validate(contentVersion, account.settings.settings,
|
||||||
!mergeImportedAccount);
|
!mergeImportedAccount);
|
||||||
|
|
||||||
@ -407,14 +410,17 @@ public class SettingsImporter {
|
|||||||
AccountSettings.upgrade(contentVersion, validatedSettings);
|
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
|
// Merge account settings if necessary
|
||||||
Map<String, String> writeSettings;
|
Map<String, String> writeSettings;
|
||||||
if (mergeImportedAccount) {
|
if (mergeImportedAccount) {
|
||||||
writeSettings = new HashMap<String, String>(
|
writeSettings = new HashMap<String, String>(
|
||||||
AccountSettings.getAccountSettings(prefs.getPreferences(), uuid));
|
AccountSettings.getAccountSettings(prefs.getPreferences(), uuid));
|
||||||
writeSettings.putAll(validatedSettings);
|
writeSettings.putAll(stringSettings);
|
||||||
} else {
|
} else {
|
||||||
writeSettings = validatedSettings;
|
writeSettings = stringSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write account settings
|
// Write account settings
|
||||||
@ -456,7 +462,7 @@ public class SettingsImporter {
|
|||||||
String uuid, ImportedFolder folder, boolean overwrite, Preferences prefs) {
|
String uuid, ImportedFolder folder, boolean overwrite, Preferences prefs) {
|
||||||
|
|
||||||
// Validate folder settings
|
// Validate folder settings
|
||||||
Map<String, String> validatedSettings =
|
Map<String, Object> validatedSettings =
|
||||||
FolderSettings.validate(contentVersion, folder.settings.settings, !overwrite);
|
FolderSettings.validate(contentVersion, folder.settings.settings, !overwrite);
|
||||||
|
|
||||||
// Upgrade folder settings to current content version
|
// Upgrade folder settings to current content version
|
||||||
@ -464,14 +470,17 @@ public class SettingsImporter {
|
|||||||
FolderSettings.upgrade(contentVersion, validatedSettings);
|
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
|
// Merge folder settings if necessary
|
||||||
Map<String, String> writeSettings;
|
Map<String, String> writeSettings;
|
||||||
if (overwrite) {
|
if (overwrite) {
|
||||||
writeSettings = FolderSettings.getFolderSettings(prefs.getPreferences(),
|
writeSettings = FolderSettings.getFolderSettings(prefs.getPreferences(),
|
||||||
uuid, folder.name);
|
uuid, folder.name);
|
||||||
writeSettings.putAll(validatedSettings);
|
writeSettings.putAll(stringSettings);
|
||||||
} else {
|
} else {
|
||||||
writeSettings = validatedSettings;
|
writeSettings = stringSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write folder settings
|
// Write folder settings
|
||||||
@ -550,7 +559,7 @@ public class SettingsImporter {
|
|||||||
|
|
||||||
if (identity.settings != null) {
|
if (identity.settings != null) {
|
||||||
// Validate identity settings
|
// Validate identity settings
|
||||||
Map<String, String> validatedSettings = IdentitySettings.validate(
|
Map<String, Object> validatedSettings = IdentitySettings.validate(
|
||||||
contentVersion, identity.settings.settings, !mergeSettings);
|
contentVersion, identity.settings.settings, !mergeSettings);
|
||||||
|
|
||||||
// Upgrade identity settings to current content version
|
// Upgrade identity settings to current content version
|
||||||
@ -558,14 +567,17 @@ public class SettingsImporter {
|
|||||||
IdentitySettings.upgrade(contentVersion, validatedSettings);
|
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
|
// Merge identity settings if necessary
|
||||||
Map<String, String> writeSettings;
|
Map<String, String> writeSettings;
|
||||||
if (mergeSettings) {
|
if (mergeSettings) {
|
||||||
writeSettings = new HashMap<String, String>(IdentitySettings.getIdentitySettings(
|
writeSettings = new HashMap<String, String>(IdentitySettings.getIdentitySettings(
|
||||||
prefs.getPreferences(), uuid, writeIdentityIndex));
|
prefs.getPreferences(), uuid, writeIdentityIndex));
|
||||||
writeSettings.putAll(validatedSettings);
|
writeSettings.putAll(stringSettings);
|
||||||
} else {
|
} else {
|
||||||
writeSettings = validatedSettings;
|
writeSettings = stringSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write identity settings
|
// Write identity settings
|
||||||
|
Loading…
Reference in New Issue
Block a user