Cleanup keyboard theme switching code

Change-Id: I023769b765d07237b2129d8f1b2a02ffd4c4f09d
This commit is contained in:
Tadashi G. Takaoka 2012-03-08 16:20:22 +09:00
parent 3e2d385810
commit 411749a4ba
4 changed files with 46 additions and 42 deletions

View file

@ -48,7 +48,7 @@
<integer name="config_keyboard_grid_height">16</integer> <integer name="config_keyboard_grid_height">16</integer>
<integer name="config_double_spaces_turn_into_period_timeout">1100</integer> <integer name="config_double_spaces_turn_into_period_timeout">1100</integer>
<!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. --> <!-- 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> <integer name="config_max_more_keys_column">5</integer>
<!-- <!--
Configuration for KeyboardView Configuration for KeyboardView

View file

@ -17,6 +17,7 @@
<resources> <resources>
<!-- Theme "Basic" --> <!-- Theme "Basic" -->
<style name="Keyboard"> <style name="Keyboard">
<!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
<item name="themeId">0</item> <item name="themeId">0</item>
<item name="touchPositionCorrectionData">@array/touch_position_correction_data_empty</item> <item name="touchPositionCorrectionData">@array/touch_position_correction_data_empty</item>
<item name="rowHeight">25%p</item> <item name="rowHeight">25%p</item>
@ -141,6 +142,7 @@
name="Keyboard.HighContrast" name="Keyboard.HighContrast"
parent="Keyboard" parent="Keyboard"
> >
<!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
<item name="themeId">1</item> <item name="themeId">1</item>
</style> </style>
<style <style
@ -165,6 +167,7 @@
name="Keyboard.Stone" name="Keyboard.Stone"
parent="Keyboard" parent="Keyboard"
> >
<!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
<item name="themeId">6</item> <item name="themeId">6</item>
<item name="keyboardHeight">@dimen/keyboardHeight_stone</item> <item name="keyboardHeight">@dimen/keyboardHeight_stone</item>
<item name="keyboardTopPadding">@fraction/keyboard_top_padding_stone</item> <item name="keyboardTopPadding">@fraction/keyboard_top_padding_stone</item>
@ -216,6 +219,7 @@
name="Keyboard.Stone.Bold" name="Keyboard.Stone.Bold"
parent="Keyboard.Stone" parent="Keyboard.Stone"
> >
<!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
<item name="themeId">7</item> <item name="themeId">7</item>
</style> </style>
<style <style
@ -239,6 +243,7 @@
name="Keyboard.Gingerbread" name="Keyboard.Gingerbread"
parent="Keyboard" parent="Keyboard"
> >
<!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
<item name="themeId">8</item> <item name="themeId">8</item>
<item name="touchPositionCorrectionData">@array/touch_position_correction_data_gingerbread</item> <item name="touchPositionCorrectionData">@array/touch_position_correction_data_gingerbread</item>
<item name="horizontalGap">@fraction/key_horizontal_gap_gb</item> <item name="horizontalGap">@fraction/key_horizontal_gap_gb</item>
@ -281,6 +286,7 @@
name="Keyboard.IceCreamSandwich" name="Keyboard.IceCreamSandwich"
parent="Keyboard" parent="Keyboard"
> >
<!-- This should be aligned with KeyboardSwitcher.KEYBOARD_THEMES[] -->
<item name="themeId">5</item> <item name="themeId">5</item>
<item name="keyboardTopPadding">@fraction/keyboard_top_padding_ics</item> <item name="keyboardTopPadding">@fraction/keyboard_top_padding_ics</item>
<item name="keyboardBottomPadding">@fraction/keyboard_bottom_padding_ics</item> <item name="keyboardBottomPadding">@fraction/keyboard_bottom_padding_ics</item>

View file

@ -388,21 +388,7 @@ public class Keyboard {
} }
} }
// 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. * Keyboard Building helper.
* *
* This class parses Keyboard XML file and eventually build a Keyboard. * This class parses Keyboard XML file and eventually build a Keyboard.

View file

@ -43,13 +43,26 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions {
private static final String TAG = KeyboardSwitcher.class.getSimpleName(); private static final String TAG = KeyboardSwitcher.class.getSimpleName();
public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20110916"; public static final String PREF_KEYBOARD_LAYOUT = "pref_keyboard_layout_20110916";
private static final int[] KEYBOARD_THEMES = {
R.style.KeyboardTheme, static class KeyboardTheme {
R.style.KeyboardTheme_HighContrast, public final String mName;
R.style.KeyboardTheme_Stone, public final int mThemeId;
R.style.KeyboardTheme_Stone_Bold, public final int mStyleId;
R.style.KeyboardTheme_Gingerbread,
R.style.KeyboardTheme_IceCreamSandwich, 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; private SubtypeSwitcher mSubtypeSwitcher;
@ -69,7 +82,7 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions {
* what user actually typed. */ * what user actually typed. */
private boolean mIsAutoCorrectionActive; private boolean mIsAutoCorrectionActive;
private int mThemeIndex = -1; private KeyboardTheme mKeyboardTheme = KEYBOARD_THEMES[0];
private Context mThemeContext; private Context mThemeContext;
private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();
@ -92,29 +105,30 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions {
mPrefs = prefs; mPrefs = prefs;
mSubtypeSwitcher = SubtypeSwitcher.getInstance(); mSubtypeSwitcher = SubtypeSwitcher.getInstance();
mState = new KeyboardState(this); mState = new KeyboardState(this);
setContextThemeWrapper(ims, getKeyboardThemeIndex(ims, prefs)); setContextThemeWrapper(ims, getKeyboardTheme(ims, prefs));
mForceNonDistinctMultitouch = prefs.getBoolean( mForceNonDistinctMultitouch = prefs.getBoolean(
DebugSettings.FORCE_NON_DISTINCT_MULTITOUCH_KEY, false); DebugSettings.FORCE_NON_DISTINCT_MULTITOUCH_KEY, false);
} }
private static int getKeyboardThemeIndex(Context context, SharedPreferences prefs) { private static KeyboardTheme getKeyboardTheme(Context context, SharedPreferences prefs) {
final String defaultThemeId = context.getString(R.string.config_default_keyboard_theme_id); final String defaultIndex = context.getString(R.string.config_default_keyboard_theme_index);
final String themeId = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultThemeId); final String themeIndex = prefs.getString(PREF_KEYBOARD_LAYOUT, defaultIndex);
try { try {
final int themeIndex = Integer.valueOf(themeId); final int index = Integer.valueOf(themeIndex);
if (themeIndex >= 0 && themeIndex < KEYBOARD_THEMES.length) if (index >= 0 && index < KEYBOARD_THEMES.length) {
return themeIndex; return KEYBOARD_THEMES[index];
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Format error, keyboard theme is default to 0. // Format error, keyboard theme is default to 0.
} }
Log.w(TAG, "Illegal keyboard theme in preference: " + themeId + ", default to 0"); Log.w(TAG, "Illegal keyboard theme in preference: " + themeIndex + ", default to 0");
return 0; return KEYBOARD_THEMES[0];
} }
private void setContextThemeWrapper(Context context, int themeIndex) { private void setContextThemeWrapper(Context context, KeyboardTheme keyboardTheme) {
if (mThemeIndex != themeIndex) { if (mKeyboardTheme.mThemeId != keyboardTheme.mThemeId) {
mThemeIndex = themeIndex; mKeyboardTheme = keyboardTheme;
mThemeContext = new ContextThemeWrapper(context, KEYBOARD_THEMES[themeIndex]); mThemeContext = new ContextThemeWrapper(context, keyboardTheme.mStyleId);
KeyboardSet.clearKeyboardCache(); KeyboardSet.clearKeyboardCache();
} }
} }
@ -347,18 +361,16 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions {
boolean tryGC = true; boolean tryGC = true;
for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) { for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) {
try { try {
setContextThemeWrapper(mInputMethodService, mThemeIndex); setContextThemeWrapper(mInputMethodService, mKeyboardTheme);
mCurrentInputView = (InputView)LayoutInflater.from(mThemeContext).inflate( mCurrentInputView = (InputView)LayoutInflater.from(mThemeContext).inflate(
R.layout.input_view, null); R.layout.input_view, null);
tryGC = false; tryGC = false;
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
Log.w(TAG, "load keyboard failed: " + e); Log.w(TAG, "load keyboard failed: " + e);
tryGC = Utils.GCUtils.getInstance().tryGCOrWait( tryGC = Utils.GCUtils.getInstance().tryGCOrWait(mKeyboardTheme.mName, e);
Keyboard.toThemeName(mThemeIndex), e);
} catch (InflateException e) { } catch (InflateException e) {
Log.w(TAG, "load keyboard failed: " + e); Log.w(TAG, "load keyboard failed: " + e);
tryGC = Utils.GCUtils.getInstance().tryGCOrWait( tryGC = Utils.GCUtils.getInstance().tryGCOrWait(mKeyboardTheme.mName, e);
Keyboard.toThemeName(mThemeIndex), e);
} }
} }