From 8f3e61feabf030950502ce018041e3a1a666d5d3 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Wed, 6 Feb 2013 20:08:57 +0100 Subject: [PATCH] Convert theme setting value to an enum. --- src/com/fsck/k9/K9.java | 69 ++++++++++--------- .../k9/activity/K9PreferenceActivity.java | 2 +- src/com/fsck/k9/activity/MessageCompose.java | 4 +- src/com/fsck/k9/activity/MessageList.java | 8 +-- .../NotificationDeleteConfirmation.java | 2 +- src/com/fsck/k9/activity/setup/Prefs.java | 14 ++-- .../fsck/k9/fragment/MessageListFragment.java | 2 +- .../fsck/k9/preferences/GlobalSettings.java | 18 ++--- .../fsck/k9/service/RemoteControlService.java | 2 +- src/com/fsck/k9/view/MessageWebView.java | 4 +- 10 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/com/fsck/k9/K9.java b/src/com/fsck/k9/K9.java index 82dce7288..779949dbc 100644 --- a/src/com/fsck/k9/K9.java +++ b/src/com/fsck/k9/K9.java @@ -44,10 +44,6 @@ 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; - public static final int THEME_GLOBAL = 2; - /** * Components that are interested in knowing when the K9 instance is * available and ready (Android invokes Application.onCreate() after other @@ -99,9 +95,9 @@ public class K9 extends Application { } private static String language = ""; - private static int theme = THEME_LIGHT; - private static int messageViewTheme = THEME_GLOBAL; - private static int composerTheme = THEME_GLOBAL; + private static Theme theme = Theme.LIGHT; + private static Theme messageViewTheme = Theme.USE_GLOBAL; + private static Theme composerTheme = Theme.USE_GLOBAL; private static boolean useFixedMessageTheme = true; private static final FontSizes fontSizes = new FontSizes(); @@ -525,9 +521,9 @@ public class K9 extends Application { editor.putBoolean("batchButtonsUnselect", mBatchButtonsUnselect); editor.putString("language", language); - editor.putInt("theme", theme); - editor.putInt("messageViewTheme", messageViewTheme); - editor.putInt("messageComposeTheme", composerTheme); + editor.putInt("theme", theme.ordinal()); + editor.putInt("messageViewTheme", messageViewTheme.ordinal()); + editor.putInt("messageComposeTheme", composerTheme.ordinal()); editor.putBoolean("fixedMessageViewTheme", useFixedMessageTheme); editor.putBoolean("useGalleryBugWorkaround", useGalleryBugWorkaround); @@ -771,20 +767,20 @@ public class K9 extends Application { K9.setK9Language(sprefs.getString("language", "")); - int theme = sprefs.getInt("theme", THEME_LIGHT); - + int themeValue = sprefs.getInt("theme", Theme.LIGHT.ordinal()); // 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; + if (themeValue == Theme.DARK.ordinal() || themeValue == android.R.style.Theme) { + K9.setK9Theme(Theme.DARK); } else { - theme = THEME_LIGHT; + K9.setK9Theme(Theme.LIGHT); } - K9.setK9Theme(theme); - K9.setK9MessageViewThemeSetting(sprefs.getInt("messageViewTheme", THEME_GLOBAL)); + themeValue = sprefs.getInt("messageViewTheme", Theme.USE_GLOBAL.ordinal()); + K9.setK9MessageViewThemeSetting(Theme.values()[themeValue]); + themeValue = sprefs.getInt("messageComposeTheme", Theme.USE_GLOBAL.ordinal()); + K9.setK9ComposerThemeSetting(Theme.values()[themeValue]); K9.setUseFixedMessageViewTheme(sprefs.getBoolean("fixedMessageViewTheme", true)); - K9.setK9ComposerThemeSetting(sprefs.getInt("messageComposeTheme", THEME_GLOBAL)); } private void maybeSetupStrictMode() { @@ -843,40 +839,47 @@ public class K9 extends Application { language = nlanguage; } - public static int getK9ThemeResourceId(int themeId) { - return (themeId == THEME_LIGHT) ? R.style.Theme_K9_Light : R.style.Theme_K9_Dark; + public enum Theme { + LIGHT, + DARK, + USE_GLOBAL + } + + public static int getK9ThemeResourceId(Theme themeId) { + return (themeId == Theme.LIGHT) ? R.style.Theme_K9_Light : R.style.Theme_K9_Dark; } public static int getK9ThemeResourceId() { return getK9ThemeResourceId(theme); } - public static int getK9MessageViewTheme() { - return messageViewTheme == THEME_GLOBAL ? theme : messageViewTheme; + public static Theme getK9MessageViewTheme() { + return messageViewTheme == Theme.USE_GLOBAL ? theme : messageViewTheme; } - public static int getK9MessageViewThemeSetting() { + public static Theme getK9MessageViewThemeSetting() { return messageViewTheme; } - public static int getK9ComposerTheme() { - return composerTheme == THEME_GLOBAL ? theme : composerTheme; + public static Theme getK9ComposerTheme() { + return composerTheme == Theme.USE_GLOBAL ? theme : composerTheme; } - public static int getK9ComposerThemeSetting() { + public static Theme getK9ComposerThemeSetting() { return composerTheme; } - public static int getK9Theme() { + public static Theme getK9Theme() { return theme; } - public static void setK9Theme(int ntheme) { - assert ntheme != THEME_GLOBAL; - theme = ntheme; + public static void setK9Theme(Theme ntheme) { + if (ntheme != Theme.USE_GLOBAL) { + theme = ntheme; + } } - public static void setK9MessageViewThemeSetting(int nMessageViewTheme) { + public static void setK9MessageViewThemeSetting(Theme nMessageViewTheme) { messageViewTheme = nMessageViewTheme; } @@ -884,13 +887,13 @@ public class K9 extends Application { return useFixedMessageTheme; } - public static void setK9ComposerThemeSetting(int compTheme) { + public static void setK9ComposerThemeSetting(Theme compTheme) { composerTheme = compTheme; } public static void setUseFixedMessageViewTheme(boolean useFixed) { useFixedMessageTheme = useFixed; - if (!useFixedMessageTheme && messageViewTheme == THEME_GLOBAL) { + if (!useFixedMessageTheme && messageViewTheme == Theme.USE_GLOBAL) { messageViewTheme = theme; } } diff --git a/src/com/fsck/k9/activity/K9PreferenceActivity.java b/src/com/fsck/k9/activity/K9PreferenceActivity.java index d30cf9d63..f307e3c30 100644 --- a/src/com/fsck/k9/activity/K9PreferenceActivity.java +++ b/src/com/fsck/k9/activity/K9PreferenceActivity.java @@ -18,7 +18,7 @@ public class K9PreferenceActivity extends SherlockPreferenceActivity { // There's a display bug in all supported Android versions before 4.0 (SDK 14) which // causes PreferenceScreens to have a black background. // http://code.google.com/p/android/issues/detail?id=4611 - setTheme(K9.getK9ThemeResourceId(K9.THEME_DARK)); + setTheme(K9.getK9ThemeResourceId(K9.Theme.DARK)); } else { setTheme(K9.getK9ThemeResourceId()); } diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index fe647f1f8..bf22d4728 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -554,7 +554,7 @@ public class MessageCompose extends K9Activity implements OnClickListener { mMessageContentView = (EditText)findViewById(R.id.message_content); mMessageContentView.getInputExtras(true).putBoolean("allowEmoji", true); - if (K9.getK9ComposerThemeSetting() != K9.THEME_GLOBAL) { + if (K9.getK9ComposerThemeSetting() != K9.Theme.USE_GLOBAL) { ContextThemeWrapper wrapper = new ContextThemeWrapper(this, K9.getK9ThemeResourceId(K9.getK9ComposerTheme())); TypedValue outValue = new TypedValue(); @@ -2381,7 +2381,7 @@ public class MessageCompose extends K9Activity implements OnClickListener { .create(); case DIALOG_CHOOSE_IDENTITY: Context context = new ContextThemeWrapper(this, - (K9.getK9Theme() == K9.THEME_LIGHT) ? + (K9.getK9Theme() == K9.Theme.LIGHT) ? R.style.Theme_K9_Dialog_Light : R.style.Theme_K9_Dialog_Dark); Builder builder = new AlertDialog.Builder(context); diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index ed88cb6cc..50fd244cb 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -961,7 +961,7 @@ public class MessageList extends K9FragmentActivity implements MessageListFragme toggleTheme.setVisible(false); } else { // Set title of menu item to switch to dark/light theme - if (K9.getK9MessageViewTheme() == K9.THEME_DARK) { + if (K9.getK9MessageViewTheme() == K9.Theme.DARK) { toggleTheme.setTitle(R.string.message_view_theme_action_light); } else { toggleTheme.setTitle(R.string.message_view_theme_action_dark); @@ -1424,10 +1424,10 @@ public class MessageList extends K9FragmentActivity implements MessageListFragme } private void onToggleTheme() { - if (K9.getK9MessageViewTheme() == K9.THEME_DARK) { - K9.setK9MessageViewThemeSetting(K9.THEME_LIGHT); + if (K9.getK9MessageViewTheme() == K9.Theme.DARK) { + K9.setK9MessageViewThemeSetting(K9.Theme.LIGHT); } else { - K9.setK9MessageViewThemeSetting(K9.THEME_DARK); + K9.setK9MessageViewThemeSetting(K9.Theme.DARK); } new Thread(new Runnable() { diff --git a/src/com/fsck/k9/activity/NotificationDeleteConfirmation.java b/src/com/fsck/k9/activity/NotificationDeleteConfirmation.java index aeca92b8b..82a888dd6 100644 --- a/src/com/fsck/k9/activity/NotificationDeleteConfirmation.java +++ b/src/com/fsck/k9/activity/NotificationDeleteConfirmation.java @@ -38,7 +38,7 @@ public class NotificationDeleteConfirmation extends Activity { public void onCreate(Bundle icicle) { super.onCreate(icicle); - setTheme(K9.getK9Theme() == K9.THEME_LIGHT ? + setTheme(K9.getK9Theme() == K9.Theme.LIGHT ? R.style.Theme_K9_Dialog_Translucent_Light : R.style.Theme_K9_Dialog_Translucent_Dark); final Preferences preferences = Preferences.getPreferences(this); diff --git a/src/com/fsck/k9/activity/setup/Prefs.java b/src/com/fsck/k9/activity/setup/Prefs.java index 3bdfdb3a9..74343b39f 100644 --- a/src/com/fsck/k9/activity/setup/Prefs.java +++ b/src/com/fsck/k9/activity/setup/Prefs.java @@ -446,21 +446,21 @@ public class Prefs extends K9PreferenceActivity { mSplitViewMode.getEntries(), mSplitViewMode.getEntryValues()); } - private static String themeIdToName(int theme) { + private static String themeIdToName(K9.Theme theme) { switch (theme) { - case K9.THEME_DARK: return "dark"; - case K9.THEME_GLOBAL: return "global"; + case DARK: return "dark"; + case USE_GLOBAL: return "global"; default: return "light"; } } - private static int themeNameToId(String theme) { + private static K9.Theme themeNameToId(String theme) { if (TextUtils.equals(theme, "dark")) { - return K9.THEME_DARK; + return K9.Theme.DARK; } else if (TextUtils.equals(theme, "global")) { - return K9.THEME_GLOBAL; + return K9.Theme.USE_GLOBAL; } else { - return K9.THEME_LIGHT; + return K9.Theme.LIGHT; } } diff --git a/src/com/fsck/k9/fragment/MessageListFragment.java b/src/com/fsck/k9/fragment/MessageListFragment.java index 3334721fe..ba2123334 100644 --- a/src/com/fsck/k9/fragment/MessageListFragment.java +++ b/src/com/fsck/k9/fragment/MessageListFragment.java @@ -1880,7 +1880,7 @@ public class MessageListFragment extends SherlockFragment implements OnItemClick Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //TODO: make this part of the theme - int color = (K9.getK9Theme() == K9.THEME_LIGHT) ? + int color = (K9.getK9Theme() == K9.Theme.LIGHT) ? Color.rgb(105, 105, 105) : Color.rgb(160, 160, 160); diff --git a/src/com/fsck/k9/preferences/GlobalSettings.java b/src/com/fsck/k9/preferences/GlobalSettings.java index 2d663a47f..5afc8294a 100644 --- a/src/com/fsck/k9/preferences/GlobalSettings.java +++ b/src/com/fsck/k9/preferences/GlobalSettings.java @@ -185,10 +185,10 @@ public class GlobalSettings { new V(1, new BooleanSetting(false)) )); s.put("theme", Settings.versions( - new V(1, new ThemeSetting(K9.THEME_LIGHT)) + new V(1, new ThemeSetting(K9.Theme.LIGHT.ordinal())) )); s.put("messageViewTheme", Settings.versions( - new V(16, new ThemeSetting(K9.THEME_LIGHT)) + new V(16, new ThemeSetting(K9.Theme.LIGHT.ordinal())) )); s.put("useGalleryBugWorkaround", Settings.versions( new V(1, new GalleryBugWorkaroundSetting()) @@ -363,14 +363,14 @@ public class GlobalSettings { public Object fromString(String value) throws InvalidSettingValueException { try { Integer theme = Integer.parseInt(value); - if (theme == K9.THEME_LIGHT || + if (theme == K9.Theme.LIGHT.ordinal() || // 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; + return K9.Theme.LIGHT; + } else if (theme == K9.Theme.DARK.ordinal() || theme == android.R.style.Theme) { + return K9.Theme.DARK; } } catch (NumberFormatException e) { /* do nothing */ } @@ -380,9 +380,9 @@ public class GlobalSettings { @Override public Object fromPrettyString(String value) throws InvalidSettingValueException { if (THEME_LIGHT.equals(value)) { - return K9.THEME_LIGHT; + return K9.Theme.LIGHT; } else if (THEME_DARK.equals(value)) { - return K9.THEME_DARK; + return K9.Theme.DARK; } throw new InvalidSettingValueException(); @@ -390,7 +390,7 @@ public class GlobalSettings { @Override public String toPrettyString(Object value) { - return (((Integer)value).intValue() == K9.THEME_LIGHT) ? THEME_LIGHT : THEME_DARK; + return (((Integer)value).intValue() == K9.Theme.LIGHT.ordinal()) ? THEME_LIGHT : THEME_DARK; } } diff --git a/src/com/fsck/k9/service/RemoteControlService.java b/src/com/fsck/k9/service/RemoteControlService.java index bd21b2061..21f204fb3 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) ? K9.THEME_DARK : K9.THEME_LIGHT); + K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? K9.Theme.DARK : K9.Theme.LIGHT); } SharedPreferences sPrefs = preferences.getPreferences(); diff --git a/src/com/fsck/k9/view/MessageWebView.java b/src/com/fsck/k9/view/MessageWebView.java index 38b787534..26215646a 100644 --- a/src/com/fsck/k9/view/MessageWebView.java +++ b/src/com/fsck/k9/view/MessageWebView.java @@ -94,7 +94,7 @@ public class MessageWebView extends TitleBarWebView { this.setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY); this.setLongClickable(true); - if (K9.getK9MessageViewTheme() == K9.THEME_DARK) { + if (K9.getK9MessageViewTheme() == K9.Theme.DARK) { // Black theme should get a black webview background // we'll set the background of the messages on load this.setBackgroundColor(0xff000000); @@ -152,7 +152,7 @@ public class MessageWebView extends TitleBarWebView { public void setText(String text, String contentType) { String content = text; - if (K9.getK9MessageViewTheme() == K9.THEME_DARK) { + if (K9.getK9MessageViewTheme() == K9.Theme.DARK) { // It's a little wrong to just throw in the