From d584492a6df117baec2b1aaf20138b77fc8d5744 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 29 Mar 2012 06:33:01 +0200 Subject: [PATCH] Changed internal representation of the selected theme in the database --- src/com/fsck/k9/K9.java | 25 ++++++++++- src/com/fsck/k9/activity/K9Activity.java | 2 +- src/com/fsck/k9/activity/K9ListActivity.java | 2 +- src/com/fsck/k9/activity/setup/Prefs.java | 4 +- .../fsck/k9/preferences/GlobalSettings.java | 43 ++++++++++++------- .../fsck/k9/service/RemoteControlService.java | 2 +- 6 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/com/fsck/k9/K9.java b/src/com/fsck/k9/K9.java index 1da5b4d81..cc3065e4a 100644 --- a/src/com/fsck/k9/K9.java +++ b/src/com/fsck/k9/K9.java @@ -38,6 +38,9 @@ import com.fsck.k9.service.ShutdownReceiver; import com.fsck.k9.service.StorageGoneReceiver; public class K9 extends Application { + public static final int THEME_LIGHT = 0; + public static final int THEME_DARK = 1; + /** * Components that are interested in knowing when the K9 instance is * available and ready (Android invokes Application.onCreate() after other @@ -72,7 +75,7 @@ public class K9 extends Application { } private static String language = ""; - private static int theme = android.R.style.Theme_Light; + private static int theme = THEME_LIGHT; private static final FontSizes fontSizes = new FontSizes(); @@ -604,7 +607,17 @@ public class K9 extends Application { } K9.setK9Language(sprefs.getString("language", "")); - K9.setK9Theme(sprefs.getInt("theme", android.R.style.Theme_Light)); + + int theme = sprefs.getInt("theme", THEME_LIGHT); + + // We used to save the resource ID of the theme. So convert that to the new format if + // necessary. + if (theme == THEME_DARK || theme == android.R.style.Theme) { + theme = THEME_DARK; + } else { + theme = THEME_LIGHT; + } + K9.setK9Theme(theme); } private void maybeSetupStrictMode() { @@ -663,6 +676,14 @@ public class K9 extends Application { language = nlanguage; } + public static int getK9ThemeResourceId(int theme) { + return (theme == THEME_LIGHT) ? android.R.style.Theme_Light : android.R.style.Theme; + } + + public static int getK9ThemeResourceId() { + return getK9ThemeResourceId(theme); + } + public static int getK9Theme() { return theme; } diff --git a/src/com/fsck/k9/activity/K9Activity.java b/src/com/fsck/k9/activity/K9Activity.java index e9a68eccf..1c1a26eab 100644 --- a/src/com/fsck/k9/activity/K9Activity.java +++ b/src/com/fsck/k9/activity/K9Activity.java @@ -29,7 +29,7 @@ public class K9Activity extends Activity { public void onCreate(Bundle icicle, boolean useTheme) { setLanguage(this, K9.getK9Language()); if (useTheme) { - setTheme(K9.getK9Theme()); + setTheme(K9.getK9ThemeResourceId()); } super.onCreate(icicle); setupFormats(); diff --git a/src/com/fsck/k9/activity/K9ListActivity.java b/src/com/fsck/k9/activity/K9ListActivity.java index 204182be2..613b22119 100644 --- a/src/com/fsck/k9/activity/K9ListActivity.java +++ b/src/com/fsck/k9/activity/K9ListActivity.java @@ -13,7 +13,7 @@ public class K9ListActivity extends ListActivity { @Override public void onCreate(Bundle icicle) { K9Activity.setLanguage(this, K9.getK9Language()); - setTheme(K9.getK9Theme()); + setTheme(K9.getK9ThemeResourceId()); super.onCreate(icicle); setupFormats(); } diff --git a/src/com/fsck/k9/activity/setup/Prefs.java b/src/com/fsck/k9/activity/setup/Prefs.java index 1b359b808..abfa5391a 100644 --- a/src/com/fsck/k9/activity/setup/Prefs.java +++ b/src/com/fsck/k9/activity/setup/Prefs.java @@ -148,7 +148,7 @@ public class Prefs extends K9PreferenceActivity { entryVector.toArray(EMPTY_CHAR_SEQUENCE_ARRAY), entryValueVector.toArray(EMPTY_CHAR_SEQUENCE_ARRAY)); - final String theme = (K9.getK9Theme() == android.R.style.Theme) ? "dark" : "light"; + final String theme = (K9.getK9Theme() == K9.THEME_DARK) ? "dark" : "light"; mTheme = setupListPreference(PREFERENCE_THEME, theme); findPreference(PREFERENCE_FONT_SIZE).setOnPreferenceClickListener( @@ -348,7 +348,7 @@ public class Prefs extends K9PreferenceActivity { SharedPreferences preferences = Preferences.getPreferences(this).getPreferences(); K9.setK9Language(mLanguage.getValue()); - K9.setK9Theme(mTheme.getValue().equals("dark") ? android.R.style.Theme : android.R.style.Theme_Light); + K9.setK9Theme(mTheme.getValue().equals("dark") ? K9.THEME_DARK : K9.THEME_LIGHT); K9.setAnimations(mAnimations.isChecked()); K9.setGesturesEnabled(mGestures.isChecked()); K9.setCompactLayouts(compactLayouts.isChecked()); diff --git a/src/com/fsck/k9/preferences/GlobalSettings.java b/src/com/fsck/k9/preferences/GlobalSettings.java index 42ad9312d..d8087fbf4 100644 --- a/src/com/fsck/k9/preferences/GlobalSettings.java +++ b/src/com/fsck/k9/preferences/GlobalSettings.java @@ -181,7 +181,7 @@ public class GlobalSettings { new V(1, new BooleanSetting(false)) )); s.put("theme", Settings.versions( - new V(1, new ThemeSetting(android.R.style.Theme_Light)) + new V(1, new ThemeSetting(K9.THEME_LIGHT)) )); s.put("useGalleryBugWorkaround", Settings.versions( new V(1, new GalleryBugWorkaroundSetting()) @@ -291,34 +291,47 @@ public class GlobalSettings { /** * The theme setting. */ - public static class ThemeSetting extends PseudoEnumSetting { - private final Map mMapping; + public static class ThemeSetting extends SettingsDescription { + private static final String THEME_LIGHT = "light"; + private static final String THEME_DARK = "dark"; public ThemeSetting(int defaultValue) { super(defaultValue); - - Map mapping = new HashMap(); - mapping.put(android.R.style.Theme_Light, "light"); - mapping.put(android.R.style.Theme, "dark"); - mMapping = Collections.unmodifiableMap(mapping); - } - - @Override - protected Map getMapping() { - return mMapping; } @Override public Object fromString(String value) throws InvalidSettingValueException { try { Integer theme = Integer.parseInt(value); - if (mMapping.containsKey(theme)) { - return theme; + if (theme == K9.THEME_LIGHT || + // We used to store the resource ID of the theme in the preference storage, + // but don't use the database upgrade mechanism to update the values. So + // we have to deal with the old format here. + theme == android.R.style.Theme_Light) { + return K9.THEME_LIGHT; + } else if (theme == K9.THEME_DARK || theme == android.R.style.Theme) { + return K9.THEME_DARK; } } catch (NumberFormatException e) { /* do nothing */ } throw new InvalidSettingValueException(); } + + @Override + public Object fromPrettyString(String value) throws InvalidSettingValueException { + if (THEME_LIGHT.equals(value)) { + return K9.THEME_LIGHT; + } else if (THEME_DARK.equals(value)) { + return K9.THEME_DARK; + } + + throw new InvalidSettingValueException(); + } + + @Override + public String toPrettyString(Object value) { + return (((Integer)value).intValue() == K9.THEME_LIGHT) ? THEME_LIGHT : THEME_DARK; + } } /** diff --git a/src/com/fsck/k9/service/RemoteControlService.java b/src/com/fsck/k9/service/RemoteControlService.java index 3cfa52a74..bd21b2061 100644 --- a/src/com/fsck/k9/service/RemoteControlService.java +++ b/src/com/fsck/k9/service/RemoteControlService.java @@ -123,7 +123,7 @@ public class RemoteControlService extends CoreService { String theme = intent.getStringExtra(K9_THEME); if (theme != null) { - K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? android.R.style.Theme : android.R.style.Theme_Light); + K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? K9.THEME_DARK : K9.THEME_LIGHT); } SharedPreferences sPrefs = preferences.getPreferences();