From 42987cee512c4d52fb72f2bdc05bf7f5ea89f186 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 2 May 2011 04:06:22 +0200 Subject: [PATCH] Use current values for missing or malformed global settings in the import file --- .../fsck/k9/preferences/AccountSettings.java | 5 +++-- .../fsck/k9/preferences/GlobalSettings.java | 16 +++++++++++++++- src/com/fsck/k9/preferences/Settings.java | 14 ++++++++------ .../fsck/k9/preferences/StorageImporter.java | 18 ++++++++++++------ 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/com/fsck/k9/preferences/AccountSettings.java b/src/com/fsck/k9/preferences/AccountSettings.java index 8e691eaf6..89ab53905 100644 --- a/src/com/fsck/k9/preferences/AccountSettings.java +++ b/src/com/fsck/k9/preferences/AccountSettings.java @@ -158,8 +158,9 @@ public class AccountSettings { return new SettingsDescription(type, defaultValue, validator); } - public static Map validate(Map importedSettings) { - return Settings.validate(SETTINGS, importedSettings); + public static Map validate(Map importedSettings, + boolean useDefaultValues) { + return Settings.validate(SETTINGS, importedSettings, useDefaultValues); } public static class StorageProviderDefaultValue implements IDefaultValue { diff --git a/src/com/fsck/k9/preferences/GlobalSettings.java b/src/com/fsck/k9/preferences/GlobalSettings.java index ba12440dc..8064c67ef 100644 --- a/src/com/fsck/k9/preferences/GlobalSettings.java +++ b/src/com/fsck/k9/preferences/GlobalSettings.java @@ -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 validate(Map importedSettings) { - return Settings.validate(SETTINGS, importedSettings); + return Settings.validate(SETTINGS, importedSettings, false); } + public static Map getGlobalSettings(SharedPreferences storage) { + Map result = new HashMap(); + 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 validatedSettings) { diff --git a/src/com/fsck/k9/preferences/Settings.java b/src/com/fsck/k9/preferences/Settings.java index 29b0068b8..c1b763c5f 100644 --- a/src/com/fsck/k9/preferences/Settings.java +++ b/src/com/fsck/k9/preferences/Settings.java @@ -28,7 +28,7 @@ public class Settings { public static final ISettingValidator SOLID_COLOR_VALIDATOR = new SolidColorValidator(); public static Map validate(Map settings, - Map importedSettings) { + Map importedSettings, boolean useDefaultValues) { Map validatedSettings = new HashMap(); for (Map.Entry 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; } } diff --git a/src/com/fsck/k9/preferences/StorageImporter.java b/src/com/fsck/k9/preferences/StorageImporter.java index 3768a06bb..de97d2da3 100644 --- a/src/com/fsck/k9/preferences/StorageImporter.java +++ b/src/com/fsck/k9/preferences/StorageImporter.java @@ -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 writeSettings = GlobalSettings.validate(settings.settings); + Map validatedSettings = GlobalSettings.validate(settings.settings); - for (Map.Entry setting : writeSettings.entrySet()) { + // 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); + + for (Map.Entry 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 validatedSettings = - AccountSettings.validate(account.settings.settings); + AccountSettings.validate(account.settings.settings, true); //TODO: validate account name //TODO: validate identity settings