diff --git a/src/com/fsck/k9/preferences/Settings.java b/src/com/fsck/k9/preferences/Settings.java index 7cc1e701c..305224f83 100644 --- a/src/com/fsck/k9/preferences/Settings.java +++ b/src/com/fsck/k9/preferences/Settings.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.SortedMap; import java.util.TreeMap; import java.util.Map.Entry; @@ -44,15 +45,25 @@ public class Settings { for (Map.Entry> versionedSetting : settings.entrySet()) { - Entry setting = - versionedSetting.getValue().floorEntry(version); + // Get the setting description with the highest version lower than or equal to the + // supplied content version. + TreeMap versions = versionedSetting.getValue(); + SortedMap headMap = versions.headMap(version + 1); - if (setting == null) { + // Skip this setting if it was introduced after 'version' + if (headMap.size() == 0) { + continue; + } + + Integer settingVersion = headMap.lastKey(); + SettingsDescription desc = versions.get(settingVersion); + + // Skip this setting if it is no longer used in 'version' + if (desc == null) { continue; } String key = versionedSetting.getKey(); - SettingsDescription desc = setting.getValue(); boolean useDefaultValue; if (!importedSettings.containsKey(key)) { @@ -127,7 +138,7 @@ public class Settings { // Check if it was already added to upgradedSettings by the SettingsUpgrader if (!upgradedSettings.containsKey(settingName)) { // Insert default value to upgradedSettings - SettingsDescription setting = versionedSettings.firstEntry().getValue(); + SettingsDescription setting = versionedSettings.get(toVersion); Object defaultValue = setting.getDefaultValue(); upgradedSettings.put(settingName, defaultValue); @@ -140,8 +151,9 @@ public class Settings { } // Handle removed settings - Entry lastEntry = versionedSettings.lastEntry(); - if (lastEntry.getKey().intValue() == toVersion && lastEntry.getValue() == null) { + Integer highestVersion = versionedSettings.lastKey(); + if (highestVersion.intValue() == toVersion && + versionedSettings.get(highestVersion) == null) { upgradedSettings.remove(settingName); if (deletedSettings == null) { deletedSettings = new HashSet(); @@ -179,8 +191,10 @@ public class Settings { String settingName = setting.getKey(); Object internalValue = setting.getValue(); - SettingsDescription settingDesc = - settingDescriptions.get(settingName).lastEntry().getValue(); + TreeMap versionedSetting = + settingDescriptions.get(settingName); + Integer highestVersion = versionedSetting.lastKey(); + SettingsDescription settingDesc = versionedSetting.get(highestVersion); if (settingDesc != null) { String stringValue = settingDesc.toString(internalValue); diff --git a/src/com/fsck/k9/preferences/SettingsExporter.java b/src/com/fsck/k9/preferences/SettingsExporter.java index cb0ea4431..2497376aa 100644 --- a/src/com/fsck/k9/preferences/SettingsExporter.java +++ b/src/com/fsck/k9/preferences/SettingsExporter.java @@ -172,7 +172,9 @@ public class SettingsExporter { String key = versionedSetting.getKey(); String valueString = (String) prefs.get(key); - SettingsDescription setting = versionedSetting.getValue().lastEntry().getValue(); + TreeMap versions = versionedSetting.getValue(); + Integer highestVersion = versions.lastKey(); + SettingsDescription setting = versions.get(highestVersion); if (setting == null) { // Setting was removed. continue; @@ -322,7 +324,8 @@ public class SettingsExporter { AccountSettings.SETTINGS.get(keyPart); if (versionedSetting != null) { - SettingsDescription setting = versionedSetting.lastEntry().getValue(); + Integer highestVersion = versionedSetting.lastKey(); + SettingsDescription setting = versionedSetting.get(highestVersion); if (setting != null) { // Only export account settings that can be found in AccountSettings.SETTINGS @@ -416,7 +419,8 @@ public class SettingsExporter { IdentitySettings.SETTINGS.get(identityKey); if (versionedSetting != null) { - SettingsDescription setting = versionedSetting.lastEntry().getValue(); + Integer highestVersion = versionedSetting.lastKey(); + SettingsDescription setting = versionedSetting.get(highestVersion); if (setting != null) { // Only write settings that have an entry in IdentitySettings.SETTINGS @@ -467,7 +471,8 @@ public class SettingsExporter { FolderSettings.SETTINGS.get(folderKey); if (versionedSetting != null) { - SettingsDescription setting = versionedSetting.lastEntry().getValue(); + Integer highestVersion = versionedSetting.lastKey(); + SettingsDescription setting = versionedSetting.get(highestVersion); if (setting != null) { // Only write settings that have an entry in FolderSettings.SETTINGS