Use current values for missing or malformed global settings in the import file

This commit is contained in:
cketti 2011-05-02 04:06:22 +02:00
parent 7e24377bb8
commit 42987cee51
4 changed files with 38 additions and 15 deletions

View file

@ -158,8 +158,9 @@ public class AccountSettings {
return new SettingsDescription(type, defaultValue, validator);
}
public static Map<String, String> validate(Map<String, String> importedSettings) {
return Settings.validate(SETTINGS, importedSettings);
public static Map<String, String> validate(Map<String, String> importedSettings,
boolean useDefaultValues) {
return Settings.validate(SETTINGS, importedSettings, useDefaultValues);
}
public static class StorageProviderDefaultValue implements IDefaultValue {

View file

@ -1,8 +1,10 @@
package com.fsck.k9.preferences;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import android.content.SharedPreferences;
import com.fsck.k9.FontSizes;
import com.fsck.k9.K9;
import com.fsck.k9.R;
@ -129,9 +131,21 @@ public class GlobalSettings {
}
public static Map<String, String> validate(Map<String, String> importedSettings) {
return Settings.validate(SETTINGS, importedSettings);
return Settings.validate(SETTINGS, importedSettings, false);
}
public static Map<String, String> getGlobalSettings(SharedPreferences storage) {
Map<String, String> result = new HashMap<String, String>();
for (String key : SETTINGS.keySet()) {
String value = storage.getString(key, null);
if (value != null) {
result.put(key, value);
}
}
return result;
}
public static class GalleryBugWorkaroundDefaultValue implements IDefaultValue {
@Override
public Object computeDefaultValue(String key, Map<String, String> validatedSettings) {

View file

@ -28,7 +28,7 @@ public class Settings {
public static final ISettingValidator SOLID_COLOR_VALIDATOR = new SolidColorValidator();
public static Map<String, String> validate(Map<String, SettingsDescription> settings,
Map<String, String> importedSettings) {
Map<String, String> importedSettings, boolean useDefaultValues) {
Map<String, String> validatedSettings = new HashMap<String, String>();
for (Map.Entry<String, SettingsDescription> setting : settings.entrySet()) {
@ -37,17 +37,19 @@ public class Settings {
boolean useDefaultValue;
if (!importedSettings.containsKey(key)) {
Log.v(K9.LOG_TAG, "Key \"" + key + "\" wasn't found in the imported file. Using default value.");
useDefaultValue = true;
Log.v(K9.LOG_TAG, "Key \"" + key + "\" wasn't found in the imported file." +
((useDefaultValues) ? " Using default value." : ""));
useDefaultValue = useDefaultValues;
} else {
String importedValue = importedSettings.get(key);
if (Settings.isValid(desc, key, importedValue, validatedSettings)) {
validatedSettings.put(key, importedValue);
useDefaultValue = false;
} else {
Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + importedValue + "\" in " +
"imported file. Using default value.");
useDefaultValue = true;
Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + importedValue +
"\" in imported file. " +
((useDefaultValues) ? "Using default value." : "Skipping."));
useDefaultValue = useDefaultValues;
}
}

View file

@ -170,7 +170,7 @@ public class StorageImporter {
try {
SharedPreferences.Editor editor = storage.edit();
if (imported.globalSettings != null) {
importGlobalSettings(editor, imported.globalSettings);
importGlobalSettings(storage, editor, imported.globalSettings);
} else {
Log.w(K9.LOG_TAG, "Was asked to import global settings but none found.");
}
@ -251,12 +251,18 @@ public class StorageImporter {
}
}
private static void importGlobalSettings(SharedPreferences.Editor editor,
ImportedSettings settings) {
private static void importGlobalSettings(SharedPreferences storage,
SharedPreferences.Editor editor, ImportedSettings settings) {
Map<String, String> writeSettings = GlobalSettings.validate(settings.settings);
Map<String, String> validatedSettings = GlobalSettings.validate(settings.settings);
for (Map.Entry<String, String> setting : writeSettings.entrySet()) {
// 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);
for (Map.Entry<String, String> setting : mergedSettings.entrySet()) {
String key = setting.getKey();
String value = setting.getValue();
Log.v(K9.LOG_TAG, "Write " + key + "=" + value);
@ -271,7 +277,7 @@ public class StorageImporter {
// Validate input and ignore malformed values when possible
Map<String, String> validatedSettings =
AccountSettings.validate(account.settings.settings);
AccountSettings.validate(account.settings.settings, true);
//TODO: validate account name
//TODO: validate identity settings