Skip to content

Commit

Permalink
Merge pull request #7220 from thundernest/extract_settings_upgrader
Browse files Browse the repository at this point in the history
Extract `SettingsUpgrader` implementations to separate files
  • Loading branch information
cketti committed Oct 6, 2023
2 parents fc7e3ce + 67839f3 commit 288e0dd
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.fsck.k9.NotificationLight;
import com.fsck.k9.core.R;
import com.fsck.k9.mailstore.StorageManager;
import com.fsck.k9.notification.NotificationLightDecoder;
import com.fsck.k9.preferences.Settings.BooleanSetting;
import com.fsck.k9.preferences.Settings.ColorSetting;
import com.fsck.k9.preferences.Settings.EnumSetting;
Expand All @@ -37,7 +36,13 @@
import com.fsck.k9.preferences.Settings.SettingsUpgrader;
import com.fsck.k9.preferences.Settings.StringSetting;
import com.fsck.k9.preferences.Settings.V;
import kotlin.collections.SetsKt;
import com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo53;
import com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo54;
import com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo74;
import com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo80;
import com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo81;

import static com.fsck.k9.preferences.upgrader.AccountSettingsUpgraderTo53.FOLDER_NONE;


public class AccountSettingsDescriptions {
Expand All @@ -59,7 +64,7 @@ public class AccountSettingsDescriptions {
new V(13, new BooleanSetting(false))
));
s.put("archiveFolderName", Settings.versions(
new V(1, new StringSetting(SettingsUpgraderV53.FOLDER_NONE)),
new V(1, new StringSetting(FOLDER_NONE)),
new V(53, new StringSetting(null))
));
s.put("autoExpandFolderName", Settings.versions(
Expand All @@ -84,7 +89,7 @@ public class AccountSettingsDescriptions {
R.array.display_count_values))
));
s.put("draftsFolderName", Settings.versions(
new V(1, new StringSetting(SettingsUpgraderV53.FOLDER_NONE)),
new V(1, new StringSetting(FOLDER_NONE)),
new V(53, new StringSetting(null))
));
s.put("expungePolicy", Settings.versions(
Expand Down Expand Up @@ -174,7 +179,7 @@ public class AccountSettingsDescriptions {
new V(1, new EnumSetting<>(Searchable.class, Searchable.ALL))
));
s.put("sentFolderName", Settings.versions(
new V(1, new StringSetting(SettingsUpgraderV53.FOLDER_NONE)),
new V(1, new StringSetting(FOLDER_NONE)),
new V(53, new StringSetting(null))
));
s.put("sortTypeEnum", Settings.versions(
Expand All @@ -190,7 +195,7 @@ public class AccountSettingsDescriptions {
new V(1, new BooleanSetting(false))
));
s.put("spamFolderName", Settings.versions(
new V(1, new StringSetting(SettingsUpgraderV53.FOLDER_NONE)),
new V(1, new StringSetting(FOLDER_NONE)),
new V(53, new StringSetting(null))
));
s.put("stripSignature", Settings.versions(
Expand All @@ -203,7 +208,7 @@ public class AccountSettingsDescriptions {
new V(1, new BooleanSetting(true))
));
s.put("trashFolderName", Settings.versions(
new V(1, new StringSetting(SettingsUpgraderV53.FOLDER_NONE)),
new V(1, new StringSetting(FOLDER_NONE)),
new V(53, new StringSetting(null))
));
s.put("useCompression.MOBILE", Settings.versions(
Expand Down Expand Up @@ -285,11 +290,11 @@ public class AccountSettingsDescriptions {
SETTINGS = Collections.unmodifiableMap(s);

Map<Integer, SettingsUpgrader> u = new HashMap<>();
u.put(53, new SettingsUpgraderV53());
u.put(54, new SettingsUpgraderV54());
u.put(74, new SettingsUpgraderV74());
u.put(80, new SettingsUpgraderV80());
u.put(81, new SettingsUpgraderV81());
u.put(53, new AccountSettingsUpgraderTo53());
u.put(54, new AccountSettingsUpgraderTo54());
u.put(74, new AccountSettingsUpgraderTo74());
u.put(80, new AccountSettingsUpgraderTo80());
u.put(81, new AccountSettingsUpgraderTo81());

UPGRADERS = Collections.unmodifiableMap(u);
}
Expand Down Expand Up @@ -468,110 +473,4 @@ public Integer fromString(String value) throws InvalidSettingValueException {
}
}

/**
* Upgrades settings from version 52 to 53
*
* Replace folder entries of "-NONE-" with {@code null}.
*/
private static class SettingsUpgraderV53 implements SettingsUpgrader {
private static final String FOLDER_NONE = "-NONE-";

@Override
public Set<String> upgrade(Map<String, Object> settings) {
upgradeFolderEntry(settings, "archiveFolderName");
upgradeFolderEntry(settings, "autoExpandFolderName");
upgradeFolderEntry(settings, "draftsFolderName");
upgradeFolderEntry(settings, "sentFolderName");
upgradeFolderEntry(settings, "spamFolderName");
upgradeFolderEntry(settings, "trashFolderName");

return null;
}

private void upgradeFolderEntry(Map<String, Object> settings, String key) {
String archiveFolderName = (String) settings.get(key);
if (FOLDER_NONE.equals(archiveFolderName)) {
settings.put(key, null);
}
}
}

/**
* Upgrades settings from version 53 to 54
*
* Inserts folder selection entries with a value of "MANUAL"
*/
private static class SettingsUpgraderV54 implements SettingsUpgrader {
private static final String FOLDER_SELECTION_MANUAL = "MANUAL";

@Override
public Set<String> upgrade(Map<String, Object> settings) {
settings.put("archiveFolderSelection", FOLDER_SELECTION_MANUAL);
settings.put("draftsFolderSelection", FOLDER_SELECTION_MANUAL);
settings.put("sentFolderSelection", FOLDER_SELECTION_MANUAL);
settings.put("spamFolderSelection", FOLDER_SELECTION_MANUAL);
settings.put("trashFolderSelection", FOLDER_SELECTION_MANUAL);

return null;
}
}

/**
* Upgrades settings from version 73 to 74
*
* Rewrites 'idleRefreshMinutes' from '1' to '2' if necessary
*/
private static class SettingsUpgraderV74 implements SettingsUpgrader {
@Override
public Set<String> upgrade(Map<String, Object> settings) {
Integer idleRefreshMinutes = (Integer) settings.get("idleRefreshMinutes");
if (idleRefreshMinutes == 1) {
settings.put("idleRefreshMinutes", 2);
}

return null;
}
}

/**
* Upgrades settings from version 79 to 80
*
* Rewrites 'led' and 'lecColor' to 'notificationLight'.
*/
private static class SettingsUpgraderV80 implements SettingsUpgrader {
private final NotificationLightDecoder notificationLightDecoder = DI.get(NotificationLightDecoder.class);

@Override
public Set<String> upgrade(Map<String, Object> settings) {
Boolean isLedEnabled = (Boolean) settings.get("led");
Integer ledColor = (Integer) settings.get("ledColor");
Integer chipColor = (Integer) settings.get("chipColor");

if (isLedEnabled != null && ledColor != null) {
int accountColor = chipColor != null ? chipColor : 0;
NotificationLight light = notificationLightDecoder.decode(isLedEnabled, ledColor, accountColor);
settings.put("notificationLight", light.name());
}

return SetsKt.setOf("led", "ledColor");
}
}

/**
* Rewrite the per-network type IMAP compression settings to a single setting.
*/
private static class SettingsUpgraderV81 implements SettingsUpgrader {
@Override
public Set<String> upgrade(Map<String, Object> settings) {
Boolean useCompressionWifi = (Boolean) settings.get("useCompression.WIFI");
Boolean useCompressionMobile = (Boolean) settings.get("useCompression.MOBILE");
Boolean useCompressionOther = (Boolean) settings.get("useCompression.OTHER");

boolean useCompression = useCompressionWifi != null && useCompressionMobile != null &&
useCompressionOther != null && useCompressionWifi && useCompressionMobile && useCompressionOther;
settings.put("useCompression", useCompression);

return SetsKt.setOf("useCompression.WIFI", "useCompression.MOBILE", "useCompression.OTHER");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -33,6 +32,11 @@
import com.fsck.k9.preferences.Settings.SettingsUpgrader;
import com.fsck.k9.preferences.Settings.V;
import com.fsck.k9.preferences.Settings.WebFontSizeSetting;
import com.fsck.k9.preferences.upgrader.GeneralSettingsUpgraderTo24;
import com.fsck.k9.preferences.upgrader.GeneralSettingsUpgraderTo31;
import com.fsck.k9.preferences.upgrader.GeneralSettingsUpgraderTo58;
import com.fsck.k9.preferences.upgrader.GeneralSettingsUpgraderTo69;
import com.fsck.k9.preferences.upgrader.GeneralSettingsUpgraderTo79;

import static com.fsck.k9.K9.LockScreenNotificationVisibility;

Expand Down Expand Up @@ -285,11 +289,11 @@ public class GeneralSettingsDescriptions {
SETTINGS = Collections.unmodifiableMap(s);

Map<Integer, SettingsUpgrader> u = new HashMap<>();
u.put(24, new SettingsUpgraderV24());
u.put(31, new SettingsUpgraderV31());
u.put(58, new SettingsUpgraderV58());
u.put(69, new SettingsUpgraderV69());
u.put(79, new SettingsUpgraderV79());
u.put(24, new GeneralSettingsUpgraderTo24());
u.put(31, new GeneralSettingsUpgraderTo31());
u.put(58, new GeneralSettingsUpgraderTo58());
u.put(69, new GeneralSettingsUpgraderTo69());
u.put(79, new GeneralSettingsUpgraderTo79());

UPGRADERS = Collections.unmodifiableMap(u);
}
Expand Down Expand Up @@ -317,132 +321,6 @@ static Map<String, String> getGlobalSettings(Storage storage) {
return result;
}

/**
* Upgrades the settings from version 23 to 24.
*
* <p>
* Set <em>messageViewTheme</em> to {@link SubTheme#USE_GLOBAL} if <em>messageViewTheme</em> has
* the same value as <em>theme</em>.
* </p>
*/
private static class SettingsUpgraderV24 implements SettingsUpgrader {

@Override
public Set<String> upgrade(Map<String, Object> settings) {
SubTheme messageViewTheme = (SubTheme) settings.get("messageViewTheme");
AppTheme theme = (AppTheme) settings.get("theme");
if ((theme == AppTheme.LIGHT && messageViewTheme == SubTheme.LIGHT) ||
(theme == AppTheme.DARK && messageViewTheme == SubTheme.DARK)) {
settings.put("messageViewTheme", SubTheme.USE_GLOBAL);
}

return null;
}
}

/**
* Upgrades the settings from version 30 to 31.
*
* <p>
* Convert value from <em>fontSizeMessageViewContent</em> to
* <em>fontSizeMessageViewContentPercent</em>.
* </p>
*/
public static class SettingsUpgraderV31 implements SettingsUpgrader {

@Override
public Set<String> upgrade(Map<String, Object> settings) {
int oldSize = (Integer) settings.get("fontSizeMessageViewContent");

int newSize = convertFromOldSize(oldSize);

settings.put("fontSizeMessageViewContentPercent", newSize);

return new HashSet<>(Collections.singletonList("fontSizeMessageViewContent"));
}

public static int convertFromOldSize(int oldSize) {
switch (oldSize) {
case 1: {
return 40;
}
case 2: {
return 75;
}
case 4: {
return 175;
}
case 5: {
return 250;
}
case 3:
default: {
return 100;
}
}
}
}

/**
* Upgrades the settings from version 57 to 58.
*
* <p>
* Set <em>theme</em> to {@link AppTheme#FOLLOW_SYSTEM} if <em>theme</em> has the value {@link AppTheme#LIGHT}.
* </p>
*/
private static class SettingsUpgraderV58 implements SettingsUpgrader {

@Override
public Set<String> upgrade(Map<String, Object> settings) {
AppTheme theme = (AppTheme) settings.get("theme");
if (theme == AppTheme.LIGHT) {
settings.put("theme", AppTheme.FOLLOW_SYSTEM);
}

return null;
}
}

/**
* Upgrades the settings from version 68 to 69.
*
* <p>
* Renames {@code hideSpecialAccounts} to {@code showUnifiedInbox}.
* </p>
*/
private static class SettingsUpgraderV69 implements SettingsUpgrader {

@Override
public Set<String> upgrade(Map<String, Object> settings) {
Boolean hideSpecialAccounts = (Boolean) settings.get("hideSpecialAccounts");
boolean showUnifiedInbox = hideSpecialAccounts == null || !hideSpecialAccounts;
settings.put("showUnifiedInbox", showUnifiedInbox);

return new HashSet<>(Collections.singleton("hideSpecialAccounts"));
}
}

/**
* Upgrades the settings from version 78 to 79.
*
* <p>
* Change default value of {@code registeredNameColor} to have enough contrast in both the light and dark theme.
* </p>
*/
private static class SettingsUpgraderV79 implements SettingsUpgrader {

@Override
public Set<String> upgrade(Map<String, Object> settings) {
final Integer registeredNameColorValue = (Integer) settings.get("registeredNameColor");

if (registeredNameColorValue != null && registeredNameColorValue == 0xFF00008F) {
settings.put("registeredNameColor", 0xFF1093F5);
}

return null;
}
}

private static class LanguageSetting extends PseudoEnumSetting<String> {
private final Context context = DI.get(Context.class);
private final Map<String, String> mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public static class V {
*
* @see Settings#upgrade(int, Map, Map, Map)
*/
interface SettingsUpgrader {
public interface SettingsUpgrader {
/**
* Upgrade the provided settings.
*
Expand Down
Loading

0 comments on commit 288e0dd

Please sign in to comment.