mirror of
https://github.com/moparisthebest/k-9
synced 2025-01-12 22:28:10 -05:00
Use current values for missing or malformed global settings in the import file
This commit is contained in:
parent
7e24377bb8
commit
42987cee51
@ -158,8 +158,9 @@ public class AccountSettings {
|
|||||||
return new SettingsDescription(type, defaultValue, validator);
|
return new SettingsDescription(type, defaultValue, validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, String> validate(Map<String, String> importedSettings) {
|
public static Map<String, String> validate(Map<String, String> importedSettings,
|
||||||
return Settings.validate(SETTINGS, importedSettings);
|
boolean useDefaultValues) {
|
||||||
|
return Settings.validate(SETTINGS, importedSettings, useDefaultValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class StorageProviderDefaultValue implements IDefaultValue {
|
public static class StorageProviderDefaultValue implements IDefaultValue {
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.fsck.k9.preferences;
|
package com.fsck.k9.preferences;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import com.fsck.k9.FontSizes;
|
import com.fsck.k9.FontSizes;
|
||||||
import com.fsck.k9.K9;
|
import com.fsck.k9.K9;
|
||||||
import com.fsck.k9.R;
|
import com.fsck.k9.R;
|
||||||
@ -129,9 +131,21 @@ public class GlobalSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, String> validate(Map<String, String> importedSettings) {
|
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 {
|
public static class GalleryBugWorkaroundDefaultValue implements IDefaultValue {
|
||||||
@Override
|
@Override
|
||||||
public Object computeDefaultValue(String key, Map<String, String> validatedSettings) {
|
public Object computeDefaultValue(String key, Map<String, String> validatedSettings) {
|
||||||
|
@ -28,7 +28,7 @@ public class Settings {
|
|||||||
public static final ISettingValidator SOLID_COLOR_VALIDATOR = new SolidColorValidator();
|
public static final ISettingValidator SOLID_COLOR_VALIDATOR = new SolidColorValidator();
|
||||||
|
|
||||||
public static Map<String, String> validate(Map<String, SettingsDescription> settings,
|
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>();
|
Map<String, String> validatedSettings = new HashMap<String, String>();
|
||||||
for (Map.Entry<String, SettingsDescription> setting : settings.entrySet()) {
|
for (Map.Entry<String, SettingsDescription> setting : settings.entrySet()) {
|
||||||
@ -37,17 +37,19 @@ public class Settings {
|
|||||||
|
|
||||||
boolean useDefaultValue;
|
boolean useDefaultValue;
|
||||||
if (!importedSettings.containsKey(key)) {
|
if (!importedSettings.containsKey(key)) {
|
||||||
Log.v(K9.LOG_TAG, "Key \"" + key + "\" wasn't found in the imported file. Using default value.");
|
Log.v(K9.LOG_TAG, "Key \"" + key + "\" wasn't found in the imported file." +
|
||||||
useDefaultValue = true;
|
((useDefaultValues) ? " Using default value." : ""));
|
||||||
|
useDefaultValue = useDefaultValues;
|
||||||
} else {
|
} else {
|
||||||
String importedValue = importedSettings.get(key);
|
String importedValue = importedSettings.get(key);
|
||||||
if (Settings.isValid(desc, key, importedValue, validatedSettings)) {
|
if (Settings.isValid(desc, key, importedValue, validatedSettings)) {
|
||||||
validatedSettings.put(key, importedValue);
|
validatedSettings.put(key, importedValue);
|
||||||
useDefaultValue = false;
|
useDefaultValue = false;
|
||||||
} else {
|
} else {
|
||||||
Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + importedValue + "\" in " +
|
Log.v(K9.LOG_TAG, "Key \"" + key + "\" has invalid value \"" + importedValue +
|
||||||
"imported file. Using default value.");
|
"\" in imported file. " +
|
||||||
useDefaultValue = true;
|
((useDefaultValues) ? "Using default value." : "Skipping."));
|
||||||
|
useDefaultValue = useDefaultValues;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +170,7 @@ public class StorageImporter {
|
|||||||
try {
|
try {
|
||||||
SharedPreferences.Editor editor = storage.edit();
|
SharedPreferences.Editor editor = storage.edit();
|
||||||
if (imported.globalSettings != null) {
|
if (imported.globalSettings != null) {
|
||||||
importGlobalSettings(editor, imported.globalSettings);
|
importGlobalSettings(storage, editor, imported.globalSettings);
|
||||||
} else {
|
} else {
|
||||||
Log.w(K9.LOG_TAG, "Was asked to import global settings but none found.");
|
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,
|
private static void importGlobalSettings(SharedPreferences storage,
|
||||||
ImportedSettings settings) {
|
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 key = setting.getKey();
|
||||||
String value = setting.getValue();
|
String value = setting.getValue();
|
||||||
Log.v(K9.LOG_TAG, "Write " + key + "=" + value);
|
Log.v(K9.LOG_TAG, "Write " + key + "=" + value);
|
||||||
@ -271,7 +277,7 @@ public class StorageImporter {
|
|||||||
|
|
||||||
// Validate input and ignore malformed values when possible
|
// Validate input and ignore malformed values when possible
|
||||||
Map<String, String> validatedSettings =
|
Map<String, String> validatedSettings =
|
||||||
AccountSettings.validate(account.settings.settings);
|
AccountSettings.validate(account.settings.settings, true);
|
||||||
|
|
||||||
//TODO: validate account name
|
//TODO: validate account name
|
||||||
//TODO: validate identity settings
|
//TODO: validate identity settings
|
||||||
|
Loading…
Reference in New Issue
Block a user