Merge "Fix IllegalStateException when setting input view"

This commit is contained in:
Tadashi G. Takaoka 2011-05-24 23:49:40 -07:00 committed by Android (Google) Code Review
commit d3ab465c5b

View file

@ -56,7 +56,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
private SubtypeSwitcher mSubtypeSwitcher; private SubtypeSwitcher mSubtypeSwitcher;
private SharedPreferences mPrefs; private SharedPreferences mPrefs;
private View mInputView;
private LatinKeyboardView mKeyboardView; private LatinKeyboardView mKeyboardView;
private LatinIME mInputMethodService; private LatinIME mInputMethodService;
@ -101,7 +100,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
// Default is SETTINGS_KEY_MODE_AUTO. // Default is SETTINGS_KEY_MODE_AUTO.
private static final int DEFAULT_SETTINGS_KEY_MODE = SETTINGS_KEY_MODE_AUTO; private static final int DEFAULT_SETTINGS_KEY_MODE = SETTINGS_KEY_MODE_AUTO;
private int mLayoutId; private int mThemeIndex;
private int mKeyboardWidth; private int mKeyboardWidth;
private static final KeyboardSwitcher sInstance = new KeyboardSwitcher(); private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();
@ -122,11 +121,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
try { try {
sConfigDefaultKeyboardThemeId = ims.getString( sConfigDefaultKeyboardThemeId = ims.getString(
R.string.config_default_keyboard_theme_id); R.string.config_default_keyboard_theme_id);
sInstance.mLayoutId = Integer.valueOf( sInstance.mThemeIndex = Integer.valueOf(
prefs.getString(PREF_KEYBOARD_LAYOUT, sConfigDefaultKeyboardThemeId)); prefs.getString(PREF_KEYBOARD_LAYOUT, sConfigDefaultKeyboardThemeId));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
sConfigDefaultKeyboardThemeId = "0"; sConfigDefaultKeyboardThemeId = "0";
sInstance.mLayoutId = 0; sInstance.mThemeIndex = 0;
} }
prefs.registerOnSharedPreferenceChangeListener(sInstance); prefs.registerOnSharedPreferenceChangeListener(sInstance);
} }
@ -711,49 +710,51 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
} }
public View onCreateInputView() { public View onCreateInputView() {
createInputViewInternal(mLayoutId, true); return createInputView(mThemeIndex, true);
return mInputView;
} }
private void createInputViewInternal(int newLayout, boolean forceReset) { // Instance variable only for {@link #createInputView(int, boolean)}.
int layoutId = newLayout; private View mCurrentInputView;
if (mLayoutId != layoutId || mKeyboardView == null || forceReset) {
if (mKeyboardView != null) {
mKeyboardView.closing();
}
if (KEYBOARD_THEMES.length <= layoutId) {
layoutId = Integer.valueOf(sConfigDefaultKeyboardThemeId);
}
Utils.GCUtils.getInstance().reset(); private View createInputView(final int newThemeIndex, final boolean forceRecreate) {
boolean tryGC = true; if (mCurrentInputView != null && mThemeIndex == newThemeIndex && !forceRecreate)
for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) { return mCurrentInputView;
try {
mInputView = LayoutInflater.from(mInputMethodService).inflate( if (mKeyboardView != null) {
KEYBOARD_THEMES[layoutId], null); mKeyboardView.closing();
tryGC = false;
} catch (OutOfMemoryError e) {
Log.w(TAG, "load keyboard failed: " + e);
tryGC = Utils.GCUtils.getInstance().tryGCOrWait(
mLayoutId + "," + layoutId, e);
} catch (InflateException e) {
Log.w(TAG, "load keyboard failed: " + e);
tryGC = Utils.GCUtils.getInstance().tryGCOrWait(
mLayoutId + "," + layoutId, e);
}
}
mKeyboardView = (LatinKeyboardView)mInputView.findViewById(R.id.latin_keyboard_view);
mKeyboardView.setOnKeyboardActionListener(mInputMethodService);
mLayoutId = layoutId;
} }
final int themeIndex = (newThemeIndex < KEYBOARD_THEMES.length) ? newThemeIndex
: Integer.valueOf(sConfigDefaultKeyboardThemeId);
Utils.GCUtils.getInstance().reset();
boolean tryGC = true;
for (int i = 0; i < Utils.GCUtils.GC_TRY_LOOP_MAX && tryGC; ++i) {
try {
mCurrentInputView = LayoutInflater.from(mInputMethodService).inflate(
KEYBOARD_THEMES[themeIndex], null);
tryGC = false;
} catch (OutOfMemoryError e) {
Log.w(TAG, "load keyboard failed: " + e);
tryGC = Utils.GCUtils.getInstance().tryGCOrWait(mThemeIndex + "," + themeIndex, e);
} catch (InflateException e) {
Log.w(TAG, "load keyboard failed: " + e);
tryGC = Utils.GCUtils.getInstance().tryGCOrWait(mThemeIndex + "," + themeIndex, e);
}
}
mKeyboardView = (LatinKeyboardView) mCurrentInputView.findViewById(
R.id.latin_keyboard_view);
mKeyboardView.setOnKeyboardActionListener(mInputMethodService);
mThemeIndex = themeIndex;
return mCurrentInputView;
} }
private void postSetInputView() { private void postSetInputView(final View newInputView) {
mInputMethodService.mHandler.post(new Runnable() { mInputMethodService.mHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
if (mKeyboardView != null) { if (newInputView != null) {
mInputMethodService.setInputView(mKeyboardView); mInputMethodService.setInputView(newInputView);
} }
mInputMethodService.updateInputViewShown(); mInputMethodService.updateInputViewShown();
} }
@ -765,13 +766,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
if (PREF_KEYBOARD_LAYOUT.equals(key)) { if (PREF_KEYBOARD_LAYOUT.equals(key)) {
final int layoutId = Integer.valueOf( final int layoutId = Integer.valueOf(
sharedPreferences.getString(key, sConfigDefaultKeyboardThemeId)); sharedPreferences.getString(key, sConfigDefaultKeyboardThemeId));
createInputViewInternal(layoutId, false); postSetInputView(createInputView(layoutId, false));
postSetInputView();
} else if (Settings.PREF_SETTINGS_KEY.equals(key)) { } else if (Settings.PREF_SETTINGS_KEY.equals(key)) {
mSettingsKeyEnabledInSettings = getSettingsKeyMode(sharedPreferences, mSettingsKeyEnabledInSettings = getSettingsKeyMode(sharedPreferences,
mInputMethodService); mInputMethodService);
createInputViewInternal(mLayoutId, true); postSetInputView(createInputView(mThemeIndex, true));
postSetInputView();
} }
} }