Cleanup keyboard theme switching code

Change-Id: I023769b765d07237b2129d8f1b2a02ffd4c4f09d
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index 9e962ee..635b216 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -48,7 +48,7 @@
     <integer name="config_keyboard_grid_height">16</integer>
     <integer name="config_double_spaces_turn_into_period_timeout">1100</integer>
     <!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. -->
-    <string name="config_default_keyboard_theme_id" translatable="false">5</string>
+    <string name="config_default_keyboard_theme_index" translatable="false">5</string>
     <integer name="config_max_more_keys_column">5</integer>
     <!--
          Configuration for KeyboardView
diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml
index 325ee9a..2286659 100644
--- a/java/res/values/styles.xml
+++ b/java/res/values/styles.xml
@@ -17,6 +17,7 @@
 <resources>
     <!-- Theme "Basic" -->
     <style name="Keyboard">
+        <!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
         <item name="themeId">0</item>
         <item name="touchPositionCorrectionData">@array/touch_position_correction_data_empty</item>
         <item name="rowHeight">25%p</item>
@@ -141,6 +142,7 @@
         name="Keyboard.HighContrast"
         parent="Keyboard"
     >
+        <!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
         <item name="themeId">1</item>
     </style>
     <style
@@ -165,6 +167,7 @@
         name="Keyboard.Stone"
         parent="Keyboard"
     >
+        <!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
         <item name="themeId">6</item>
         <item name="keyboardHeight">@dimen/keyboardHeight_stone</item>
         <item name="keyboardTopPadding">@fraction/keyboard_top_padding_stone</item>
@@ -216,6 +219,7 @@
         name="Keyboard.Stone.Bold"
         parent="Keyboard.Stone"
     >
+        <!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
         <item name="themeId">7</item>
     </style>
     <style
@@ -239,6 +243,7 @@
         name="Keyboard.Gingerbread"
         parent="Keyboard"
     >
+        <!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
         <item name="themeId">8</item>
         <item name="touchPositionCorrectionData">@array/touch_position_correction_data_gingerbread</item>
         <item name="horizontalGap">@fraction/key_horizontal_gap_gb</item>
@@ -281,6 +286,7 @@
         name="Keyboard.IceCreamSandwich"
         parent="Keyboard"
     >
+        <!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
         <item name="themeId">5</item>
         <item name="keyboardTopPadding">@fraction/keyboard_top_padding_ics</item>
         <item name="keyboardBottomPadding">@fraction/keyboard_bottom_padding_ics</item>
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index 320b1be..689e322 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -388,21 +388,7 @@
         }
     }
 
-    // TODO: Move this method to KeyboardSwitcher.
-    public static String toThemeName(int themeId) {
-        // This should be aligned with theme-*.xml resource files' themeId attribute.
-        switch (themeId) {
-        case 0: return "Basic";
-        case 1: return "BasicHighContrast";
-        case 5: return "IceCreamSandwich";
-        case 6: return "Stone";
-        case 7: return "StoneBold";
-        case 8: return "GingerBread";
-        default: return null;
-        }
-    }
-
-    /**
+   /**
      * Keyboard Building helper.
      *
      * This class parses Keyboard XML file and eventually build a Keyboard.
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index efe5aa9..599adf2 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -43,13 +43,26 @@
     private static final String TAG = KeyboardSwitcher.class.getSimpleName();
 
     public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20110916";
-    private static final int[] KEYBOARD_THEMES = {
-        R.style.KeyboardTheme,
-        R.style.KeyboardTheme_HighContrast,
-        R.style.KeyboardTheme_Stone,
-        R.style.KeyboardTheme_Stone_Bold,
-        R.style.KeyboardTheme_Gingerbread,
-        R.style.KeyboardTheme_IceCreamSandwich,
+
+    static class KeyboardTheme {
+        public final String mName;
+        public final int mThemeId;
+        public final int mStyleId;
+
+        public KeyboardTheme(String name, int themeId, int styleId) {
+            mName = name;
+            mThemeId = themeId;
+            mStyleId = styleId;
+        }
+    }
+
+    private static final KeyboardTheme[] KEYBOARD_THEMES = {
+        new KeyboardTheme("Basic",            0, R.style.KeyboardTheme),
+        new KeyboardTheme("HighContrast",     1, R.style.KeyboardTheme_HighContrast),
+        new KeyboardTheme("Stone",            6, R.style.KeyboardTheme_Stone),
+        new KeyboardTheme("Stne.Bold",        7, R.style.KeyboardTheme_Stone_Bold),
+        new KeyboardTheme("GingerBread",      8, R.style.KeyboardTheme_Gingerbread),
+        new KeyboardTheme("IceCreamSandwich", 5, R.style.KeyboardTheme_IceCreamSandwich),
     };
 
     private SubtypeSwitcher mSubtypeSwitcher;
@@ -69,7 +82,7 @@
      * what user actually typed. */
     private boolean mIsAutoCorrectionActive;
 
-    private int mThemeIndex = -1;
+    private KeyboardTheme mKeyboardTheme = KEYBOARD_THEMES[0];
     private Context mThemeContext;
 
     private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();
@@ -92,29 +105,30 @@
         mPrefs = prefs;
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
         mState = new KeyboardState(this);
-        setContextThemeWrapper(ims, getKeyboardThemeIndex(ims, prefs));
+        setContextThemeWrapper(ims, getKeyboardTheme(ims, prefs));
         mForceNonDistinctMultitouch = prefs.getBoolean(
                 DebugSettings.FORCE_NON_DISTINCT_MULTITOUCH_KEY, false);
     }
 
-    private static int getKeyboardThemeIndex(Context context, SharedPreferences prefs) {
-        final String defaultThemeId = context.getString(R.string.config_default_keyboard_theme_id);
-        final String themeId = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultThemeId);
+    private static KeyboardTheme getKeyboardTheme(Context context, SharedPreferences prefs) {
+        final String defaultIndex = context.getString(R.string.config_default_keyboard_theme_index);
+        final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultIndex);
         try {
-            final int themeIndex = Integer.valueOf(themeId);
-            if (themeIndex >= 0 && themeIndex < KEYBOARD_THEMES.length)
-                return themeIndex;
+            final int index = Integer.valueOf(themeIndex);
+            if (index >= 0 && index < KEYBOARD_THEMES.length) {
+                return KEYBOARD_THEMES[index];
+            }
         } catch (NumberFormatException e) {
             // Format error, keyboard theme is default to 0.
         }
-        Log.w(TAG, "Illegal keyboard theme in preference: " + themeId + ", default to 0");
-        return 0;
+        Log.w(TAG, "Illegal keyboard theme in preference: " + themeIndex + ", default to 0");
+        return KEYBOARD_THEMES[0];
     }
 
-    private void setContextThemeWrapper(Context context, int themeIndex) {
-        if (mThemeIndex != themeIndex) {
-            mThemeIndex = themeIndex;
-            mThemeContext = new ContextThemeWrapper(context, KEYBOARD_THEMES[themeIndex]);
+    private void setContextThemeWrapper(Context context, KeyboardTheme keyboardTheme) {
+        if (mKeyboardTheme.mThemeId != keyboardTheme.mThemeId) {
+            mKeyboardTheme = keyboardTheme;
+            mThemeContext = new ContextThemeWrapper(context, keyboardTheme.mStyleId);
             KeyboardSet.clearKeyboardCache();
         }
     }
@@ -347,18 +361,16 @@
         boolean tryGC = true;
         for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) {
             try {
-                setContextThemeWrapper(mInputMethodService, mThemeIndex);
+                setContextThemeWrapper(mInputMethodService, mKeyboardTheme);
                 mCurrentInputView = (InputView)LayoutInflater.from(mThemeContext).inflate(
                         R.layout.input_view, null);
                 tryGC = false;
             } catch (OutOfMemoryError e) {
                 Log.w(TAG, "load keyboard failed: " + e);
-                tryGC = Utils.GCUtils.getInstance().tryGCOrWait(
-                        Keyboard.toThemeName(mThemeIndex), e);
+                tryGC = Utils.GCUtils.getInstance().tryGCOrWait(mKeyboardTheme.mName, e);
             } catch (InflateException e) {
                 Log.w(TAG, "load keyboard failed: " + e);
-                tryGC = Utils.GCUtils.getInstance().tryGCOrWait(
-                        Keyboard.toThemeName(mThemeIndex), e);
+                tryGC = Utils.GCUtils.getInstance().tryGCOrWait(mKeyboardTheme.mName, e);
             }
         }