Rename KeyboardSwitcher.setKeyboardMode to loadKeyboard

This change also eliminates KeyboardSwitcher.refreshKeyboardCache,
LatinIME.reloadKeyboards() and LanguageSwicther.getLocales().

Change-Id: I5fd4914660ea1c955ddfa0ca62b920f9bcd53e1b
main
Tadashi G. Takaoka 2010-11-10 23:03:19 -08:00
parent 4174655e23
commit 8b00bc4f32
4 changed files with 195 additions and 195 deletions

View File

@ -16,10 +16,12 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log;
import android.view.InflateException; import android.view.InflateException;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
@ -28,6 +30,8 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceChangeListener { public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "KeyboardSwitcher";
private static final boolean DEBUG = false;
public static final int MODE_TEXT = 0; public static final int MODE_TEXT = 0;
public static final int MODE_URL = 1; public static final int MODE_URL = 1;
@ -36,8 +40,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public static final int MODE_WEB = 4; public static final int MODE_WEB = 4;
public static final int MODE_PHONE = 5; public static final int MODE_PHONE = 5;
public static final int MODE_NONE = -1;
// Main keyboard layouts without the settings key // Main keyboard layouts without the settings key
private static final int KEYBOARDMODE_NORMAL = R.id.mode_normal; private static final int KEYBOARDMODE_NORMAL = R.id.mode_normal;
private static final int KEYBOARDMODE_URL = R.id.mode_url; private static final int KEYBOARDMODE_URL = R.id.mode_url;
@ -134,14 +136,16 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
private LatinKeyboardView mInputView; private LatinKeyboardView mInputView;
private final LatinIME mInputMethodService; private final LatinIME mInputMethodService;
private final LanguageSwitcher mLanguageSwitcher;
private KeyboardId mSymbolsId; private KeyboardId mSymbolsId;
private KeyboardId mSymbolsShiftedId; private KeyboardId mSymbolsShiftedId;
private KeyboardId mCurrentId; private KeyboardId mCurrentId;
private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboards; private final HashMap<KeyboardId, SoftReference<LatinKeyboard>> mKeyboardCache =
new HashMap<KeyboardId, SoftReference<LatinKeyboard>>();
private int mMode = MODE_NONE; /** One of the MODE_XXX values */ private int mMode = MODE_TEXT; /* default value */
private int mImeOptions; private int mImeOptions;
private boolean mIsSymbols; private boolean mIsSymbols;
/** mIsAutoCompletionActive indicates that auto completed word will be input instead of /** mIsAutoCompletionActive indicates that auto completed word will be input instead of
@ -161,65 +165,29 @@ 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 mLastDisplayWidth;
private LanguageSwitcher mLanguageSwitcher;
private Locale mInputLocale;
private int mLayoutId; private int mLayoutId;
public KeyboardSwitcher(LatinIME ims) { public KeyboardSwitcher(LatinIME ims, LanguageSwitcher languageSwitcher) {
mInputMethodService = ims; mInputMethodService = ims;
mLanguageSwitcher = languageSwitcher;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ims); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ims);
mLayoutId = Integer.valueOf(prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID)); mLayoutId = Integer.valueOf(prefs.getString(PREF_KEYBOARD_LAYOUT, DEFAULT_LAYOUT_ID));
updateSettingsKeyState(prefs);
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
mKeyboards = new HashMap<KeyboardId, SoftReference<LatinKeyboard>>();
mSymbolsId = makeSymbolsId(false);
mSymbolsShiftedId = makeSymbolsShiftedId(false);
}
/**
* Sets the input locale, when there are multiple locales for input.
* If no locale switching is required, then the locale should be set to null.
* @param locale the current input locale, or null for default locale with no locale
* button.
*/
public void setLanguageSwitcher(LanguageSwitcher languageSwitcher) {
mLanguageSwitcher = languageSwitcher;
mInputLocale = mLanguageSwitcher.getInputLocale();
}
private KeyboardId makeSymbolsId(boolean voiceButtonEnabled) {
final Configuration conf = mInputMethodService.getResources().getConfiguration();
final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode;
return new KeyboardId(mInputLocale, conf.orientation, SYMBOLS_KEYBOARD_MODES, mode,
KBD_SYMBOLS, getCharColorId(), mHasSettingsKey, voiceButtonEnabled, false);
}
private KeyboardId makeSymbolsShiftedId(boolean voiceButtonEnabled) {
final Configuration conf = mInputMethodService.getResources().getConfiguration();
final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode;
return new KeyboardId(mInputLocale, conf.orientation, SYMBOLS_KEYBOARD_MODES, mode,
KBD_SYMBOLS_SHIFT, getCharColorId(), mHasSettingsKey, voiceButtonEnabled, false);
} }
private void makeSymbolsKeyboardIds() { private void makeSymbolsKeyboardIds() {
mSymbolsId = makeSymbolsId(mVoiceButtonEnabled && !mVoiceButtonOnPrimary); final Locale locale = mLanguageSwitcher.getInputLocale();
mSymbolsShiftedId = makeSymbolsShiftedId(mVoiceButtonEnabled && !mVoiceButtonOnPrimary); final int orientation = mInputMethodService.getResources().getConfiguration().orientation;
} final int mode = mMode;
final int colorScheme = getCharColorId();
public void refreshKeyboardCache(boolean forceCreate) { final boolean hasSettingsKey = mHasSettingsKey;
makeSymbolsKeyboardIds(); final boolean hasVoiceKey = mVoiceButtonEnabled && !mVoiceButtonOnPrimary;
if (forceCreate) mKeyboards.clear(); final int imeOptions = mImeOptions;
// Configuration change is coming after the keyboard gets recreated. So don't rely on that. mSymbolsId = new KeyboardId(locale, orientation, SYMBOLS_KEYBOARD_MODES, mode,
// If keyboards have already been made, check if we have a screen width change and KBD_SYMBOLS, colorScheme, hasSettingsKey, hasVoiceKey, imeOptions, true);
// create the keyboard layouts again at the correct orientation mSymbolsShiftedId = new KeyboardId(locale, orientation, SYMBOLS_KEYBOARD_MODES, mode,
int displayWidth = mInputMethodService.getMaxWidth(); KBD_SYMBOLS_SHIFT, colorScheme, hasSettingsKey, hasVoiceKey, imeOptions, true);
if (displayWidth == mLastDisplayWidth) return;
mLastDisplayWidth = displayWidth;
if (!forceCreate) mKeyboards.clear();
} }
/** /**
@ -233,28 +201,38 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public final int mMode; public final int mMode;
public final int[] mXmlArray; public final int[] mXmlArray;
public final int mColorScheme; public final int mColorScheme;
public final boolean mHasSettings; public final boolean mHasSettingsKey;
public final boolean mVoiceButtonEnabled; public final boolean mHasVoiceKey;
public final int mImeOptions;
public final boolean mEnableShiftLock; public final boolean mEnableShiftLock;
private final int mHashCode; private final int mHashCode;
public KeyboardId(Locale locale, int orientation, int[][] keyboardModes, int mode, public KeyboardId(Locale locale, int orientation, int[][] keyboardModes, int mode,
int[] xmlArray, int colorScheme, boolean hasSettings, boolean voiceButtonEnabled, int[] xmlArray, int colorScheme, boolean hasSettingsKey, boolean hasVoiceKey,
boolean enableShiftLock) { int imeOptions, boolean enableShiftLock) {
this.mLocale = locale; this.mLocale = locale;
this.mOrientation = orientation; this.mOrientation = orientation;
this.mKeyboardModes = keyboardModes; this.mKeyboardModes = keyboardModes;
this.mMode = mode; this.mMode = mode;
this.mXmlArray = xmlArray; this.mXmlArray = xmlArray;
this.mColorScheme = colorScheme; this.mColorScheme = colorScheme;
this.mHasSettings = hasSettings; this.mHasSettingsKey = hasSettingsKey;
this.mVoiceButtonEnabled = voiceButtonEnabled; this.mHasVoiceKey = hasVoiceKey;
this.mImeOptions = imeOptions;
this.mEnableShiftLock = enableShiftLock; this.mEnableShiftLock = enableShiftLock;
this.mHashCode = Arrays.hashCode(new Object[] { this.mHashCode = Arrays.hashCode(new Object[] {
locale, orientation, keyboardModes, mode, xmlArray, colorScheme, hasSettings, locale,
voiceButtonEnabled, enableShiftLock, orientation,
keyboardModes,
mode,
xmlArray,
colorScheme,
hasSettingsKey,
hasVoiceKey,
imeOptions,
enableShiftLock,
}); });
} }
@ -270,8 +248,9 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
&& other.mMode == this.mMode && other.mMode == this.mMode
&& other.mXmlArray == this.mXmlArray && other.mXmlArray == this.mXmlArray
&& other.mColorScheme == this.mColorScheme && other.mColorScheme == this.mColorScheme
&& other.mHasSettings == this.mHasSettings && other.mHasSettingsKey == this.mHasSettingsKey
&& other.mVoiceButtonEnabled == this.mVoiceButtonEnabled && other.mHasVoiceKey == this.mHasVoiceKey
&& other.mImeOptions == this.mImeOptions
&& other.mEnableShiftLock == this.mEnableShiftLock; && other.mEnableShiftLock == this.mEnableShiftLock;
} }
@ -279,41 +258,74 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public int hashCode() { public int hashCode() {
return mHashCode; return mHashCode;
} }
@Override
public String toString() {
if (DEBUG) {
return String.format("[%s %s %6s %5s imeOptions=0x%08x xml=0x%08x %s%s%s%s]",
mLocale,
(mOrientation == 1 ? "port" : "land"),
(mKeyboardModes == QWERTY_KEYBOARD_MODES ? "alpha" : "symbol"),
modeName(mMode),
mImeOptions,
mXmlArray[0],
(mColorScheme == CHAR_THEME_COLOR_WHITE ? "white" : "black"),
(mHasSettingsKey ? " hasSettingsKey" : ""),
(mHasVoiceKey ? " hasVoiceKey" : ""),
(mEnableShiftLock ? " enableShiftLock" : ""));
} else {
return super.toString();
}
}
private static String modeName(int mode) {
if (DEBUG) {
switch (mode) {
case MODE_TEXT: return "text";
case MODE_URL: return "url";
case MODE_EMAIL: return "email";
case MODE_IM: return "im";
case MODE_WEB: return "web";
case MODE_PHONE: return "phone";
}
}
return null;
}
} }
private boolean isVoiceButtonEnabled(boolean isSymbols) { private boolean hasVoiceKey(boolean isSymbols) {
return mVoiceButtonEnabled && (isSymbols != mVoiceButtonOnPrimary); return mVoiceButtonEnabled && (isSymbols != mVoiceButtonOnPrimary);
} }
public void setKeyboardMode(int mode, int imeOptions, boolean voiceButtonEnabled, public void loadKeyboard(int mode, int imeOptions, boolean voiceButtonEnabled,
boolean voiceButtonOnPrimary) { boolean voiceButtonOnPrimary) {
mSymbolsModeState = SYMBOLS_MODE_STATE_NONE; mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
try { try {
setKeyboardModeInternal(mode, imeOptions, voiceButtonEnabled, voiceButtonOnPrimary, loadKeyboardInternal(mode, imeOptions, voiceButtonEnabled, voiceButtonOnPrimary,
false); false);
} catch (RuntimeException e) { } catch (RuntimeException e) {
LatinImeLogger.logOnException(mode + "," + imeOptions, e); LatinImeLogger.logOnException(mode + "," + imeOptions, e);
} }
} }
private void setKeyboardModeInternal(int mode, int imeOptions, boolean voiceButtonEnabled, private void loadKeyboardInternal(int mode, int imeOptions, boolean voiceButtonEnabled,
boolean voiceButtonOnPrimary, boolean isSymbols) { boolean voiceButtonOnPrimary, boolean isSymbols) {
if (mInputView == null) return; if (mInputView == null) return;
mInputView.setPreviewEnabled(mInputMethodService.getPopupOn());
mMode = mode; mMode = mode;
mImeOptions = imeOptions; mImeOptions = imeOptions;
makeSymbolsKeyboardIds(); mVoiceButtonEnabled = voiceButtonEnabled;
if (voiceButtonEnabled != mVoiceButtonEnabled mVoiceButtonOnPrimary = voiceButtonOnPrimary;
|| voiceButtonOnPrimary != mVoiceButtonOnPrimary) {
mKeyboards.clear();
mVoiceButtonEnabled = voiceButtonEnabled;
mVoiceButtonOnPrimary = voiceButtonOnPrimary;
}
mIsSymbols = isSymbols; mIsSymbols = isSymbols;
// Update the settings key state because number of enabled IMEs could have been changed
mHasSettingsKey = getSettingsKeyMode(
PreferenceManager.getDefaultSharedPreferences(mInputMethodService),
mInputMethodService);
makeSymbolsKeyboardIds();
mInputView.setPreviewEnabled(mInputMethodService.getPopupOn());
KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols); KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols);
LatinKeyboard keyboard = null; LatinKeyboard keyboard = getKeyboard(id);
keyboard = getKeyboard(id);
if (mode == MODE_PHONE) { if (mode == MODE_PHONE) {
mInputView.setPhoneKeyboard(keyboard); mInputView.setPhoneKeyboard(keyboard);
@ -321,53 +333,58 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
mCurrentId = id; mCurrentId = id;
mInputView.setKeyboard(keyboard); mInputView.setKeyboard(keyboard);
keyboard.setShifted(false);
keyboard.setShiftLocked(keyboard.isShiftLocked());
keyboard.setImeOptions(mInputMethodService.getResources(), mode, imeOptions);
keyboard.setColorOfSymbolIcons(mIsAutoCompletionActive, isBlackSym());
// Update the settings key state because number of enabled IMEs could have been changed
updateSettingsKeyState(PreferenceManager.getDefaultSharedPreferences(mInputMethodService));
} }
private LatinKeyboard getKeyboard(KeyboardId id) { private LatinKeyboard getKeyboard(KeyboardId id) {
SoftReference<LatinKeyboard> ref = mKeyboards.get(id); final SoftReference<LatinKeyboard> ref = mKeyboardCache.get(id);
LatinKeyboard keyboard = (ref == null) ? null : ref.get(); LatinKeyboard keyboard = (ref == null) ? null : ref.get();
if (keyboard == null) { if (keyboard == null) {
Resources res = mInputMethodService.getResources(); final Resources res = mInputMethodService.getResources();
Configuration conf = res.getConfiguration(); final Configuration conf = res.getConfiguration();
Locale saveLocale = conf.locale; final Locale saveLocale = conf.locale;
conf.locale = mInputLocale; conf.locale = mLanguageSwitcher.getInputLocale();
res.updateConfiguration(conf, null); res.updateConfiguration(conf, null);
final int keyboardMode = id.mKeyboardModes[id.mHasSettings ? 1 : 0][id.mMode];
final int keyboardMode = id.mKeyboardModes[id.mHasSettingsKey ? 1 : 0][id.mMode];
final int xml = id.mXmlArray[id.mColorScheme]; final int xml = id.mXmlArray[id.mColorScheme];
keyboard = new LatinKeyboard(mInputMethodService, xml, keyboardMode); keyboard = new LatinKeyboard(mInputMethodService, xml, keyboardMode, id.mColorScheme,
keyboard.setVoiceMode(isVoiceButtonEnabled(xml == R.xml.kbd_symbols id.mHasSettingsKey, id.mHasVoiceKey, id.mImeOptions);
|| xml == R.xml.kbd_symbols_black), mVoiceButtonEnabled); keyboard.setVoiceMode(
keyboard.setLanguageSwitcher(mLanguageSwitcher, mIsAutoCompletionActive, isBlackSym()); hasVoiceKey(xml == R.xml.kbd_symbols || xml == R.xml.kbd_symbols_black),
mVoiceButtonEnabled);
keyboard.setLanguageSwitcher(mLanguageSwitcher);
keyboard.setImeOptions(res, id.mMode, id.mImeOptions);
keyboard.setColorOfSymbolIcons(isBlackSym(id.mColorScheme));
if (id.mEnableShiftLock) { if (id.mEnableShiftLock) {
keyboard.enableShiftLock(); keyboard.enableShiftLock();
} }
mKeyboards.put(id, new SoftReference<LatinKeyboard>(keyboard));
mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard));
if (DEBUG)
Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": "
+ ((ref == null) ? "LOAD" : "GCed") + " id=" + id);
conf.locale = saveLocale; conf.locale = saveLocale;
res.updateConfiguration(conf, null); res.updateConfiguration(conf, null);
} else if (DEBUG) {
Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT id=" + id);
} }
keyboard.onAutoCompletionStateChanged(mIsAutoCompletionActive);
keyboard.setShifted(false);
// TODO: delete this?
keyboard.setShiftLocked(keyboard.isShiftLocked());
return keyboard; return keyboard;
} }
private KeyboardId getKeyboardId(int mode, int imeOptions, boolean isSymbols) { private KeyboardId getKeyboardId(int mode, int imeOptions, boolean isSymbols) {
final boolean voiceButtonEnabled = isVoiceButtonEnabled(isSymbols); final boolean hasVoiceKey = hasVoiceKey(isSymbols);
final int charColorId = getCharColorId(); final int charColorId = getCharColorId();
final int[] xmlArray; final int[] xmlArray;
final boolean enableShiftLock; final boolean enableShiftLock;
final int[][] keyboardModes; final int[][] keyboardModes;
if (mode == MODE_NONE) {
LatinImeLogger.logOnWarning(
"getKeyboardId:" + mode + "," + imeOptions + "," + isSymbols);
mode = MODE_TEXT;
}
if (isSymbols) { if (isSymbols) {
keyboardModes = SYMBOLS_KEYBOARD_MODES; keyboardModes = SYMBOLS_KEYBOARD_MODES;
xmlArray = mode == MODE_PHONE ? KBD_PHONE_SYMBOLS : KBD_SYMBOLS; xmlArray = mode == MODE_PHONE ? KBD_PHONE_SYMBOLS : KBD_SYMBOLS;
@ -377,9 +394,10 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
xmlArray = mode == MODE_PHONE ? KBD_PHONE : KBD_QWERTY; xmlArray = mode == MODE_PHONE ? KBD_PHONE : KBD_QWERTY;
enableShiftLock = mode == MODE_PHONE ? false : true; enableShiftLock = mode == MODE_PHONE ? false : true;
} }
final Configuration conf = mInputMethodService.getResources().getConfiguration(); final int orientation = mInputMethodService.getResources().getConfiguration().orientation;
return new KeyboardId(mInputLocale, conf.orientation, keyboardModes, mode, xmlArray, final Locale locale = mLanguageSwitcher.getInputLocale();
charColorId, mHasSettingsKey, voiceButtonEnabled, enableShiftLock); return new KeyboardId(locale, orientation, keyboardModes, mode, xmlArray,
charColorId, mHasSettingsKey, hasVoiceKey, imeOptions, enableShiftLock);
} }
public int getKeyboardMode() { public int getKeyboardMode() {
@ -407,32 +425,27 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public void toggleShift() { public void toggleShift() {
if (isAlphabetMode()) if (isAlphabetMode())
return; return;
final LatinKeyboard keyboard;
if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) { if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) {
LatinKeyboard symbolsShiftedKeyboard = getKeyboard(mSymbolsShiftedId);
mCurrentId = mSymbolsShiftedId; mCurrentId = mSymbolsShiftedId;
mInputView.setKeyboard(symbolsShiftedKeyboard); keyboard = getKeyboard(mCurrentId);
// Symbol shifted keyboard has an ALT key that has a caps lock style indicator. To // Symbol shifted keyboard has an ALT key that has a caps lock style indicator. To
// enable the indicator, we need to call enableShiftLock() and setShiftLocked(true). // enable the indicator, we need to call enableShiftLock() and setShiftLocked(true).
// Thus we can keep the ALT key's Key.on value true while LatinKey.onRelease() is // Thus we can keep the ALT key's Key.on value true while LatinKey.onRelease() is
// called. // called.
symbolsShiftedKeyboard.enableShiftLock(); keyboard.setShiftLocked(true);
symbolsShiftedKeyboard.setShiftLocked(true);
symbolsShiftedKeyboard.setImeOptions(mInputMethodService.getResources(),
mMode, mImeOptions);
} else { } else {
LatinKeyboard symbolsKeyboard = getKeyboard(mSymbolsId);
mCurrentId = mSymbolsId; mCurrentId = mSymbolsId;
mInputView.setKeyboard(symbolsKeyboard); keyboard = getKeyboard(mCurrentId);
// Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the // Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the
// indicator, we need to call enableShiftLock() and setShiftLocked(false). // indicator, we need to call enableShiftLock() and setShiftLocked(false).
symbolsKeyboard.enableShiftLock(); keyboard.setShifted(false);
symbolsKeyboard.setShifted(false);
symbolsKeyboard.setImeOptions(mInputMethodService.getResources(), mMode, mImeOptions);
} }
mInputView.setKeyboard(keyboard);
} }
public void toggleSymbols() { public void toggleSymbols() {
setKeyboardModeInternal(mMode, mImeOptions, mVoiceButtonEnabled, mVoiceButtonOnPrimary, loadKeyboardInternal(mMode, mImeOptions, mVoiceButtonEnabled, mVoiceButtonOnPrimary,
!mIsSymbols); !mIsSymbols);
if (mIsSymbols) { if (mIsSymbols) {
mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN; mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN;
@ -469,11 +482,11 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
return mInputView; return mInputView;
} }
public void recreateInputView() { public void loadKeyboardView() {
changeLatinKeyboardView(mLayoutId, true); loadKeyboardViewInternal(mLayoutId, true);
} }
private void changeLatinKeyboardView(int newLayout, boolean forceReset) { private void loadKeyboardViewInternal(int newLayout, boolean forceReset) {
if (mLayoutId != newLayout || mInputView == null || forceReset) { if (mLayoutId != newLayout || mInputView == null || forceReset) {
if (mInputView != null) { if (mInputView != null) {
mInputView.closing(); mInputView.closing();
@ -511,22 +524,27 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (PREF_KEYBOARD_LAYOUT.equals(key)) { if (PREF_KEYBOARD_LAYOUT.equals(key)) {
changeLatinKeyboardView( final int layoutId = Integer.valueOf(
Integer.valueOf(sharedPreferences.getString(key, DEFAULT_LAYOUT_ID)), false); sharedPreferences.getString(key, DEFAULT_LAYOUT_ID));
loadKeyboardViewInternal(layoutId, false);
} else if (LatinIMESettings.PREF_SETTINGS_KEY.equals(key)) { } else if (LatinIMESettings.PREF_SETTINGS_KEY.equals(key)) {
updateSettingsKeyState(sharedPreferences); mHasSettingsKey = getSettingsKeyMode(sharedPreferences, mInputMethodService);
recreateInputView(); loadKeyboardViewInternal(mLayoutId, true);
} }
} }
public boolean isBlackSym () { public boolean isBlackSym() {
if (mInputView != null && mInputView.getSymbolColorScheme() == 1) { if (mInputView != null && mInputView.getSymbolColorScheme() == 1) {
return true; return true;
} }
return false; return false;
} }
private int getCharColorId () { private boolean isBlackSym(int colorScheme) {
return colorScheme == CHAR_THEME_COLOR_BLACK;
}
private int getCharColorId() {
if (isBlackSym()) { if (isBlackSym()) {
return CHAR_THEME_COLOR_BLACK; return CHAR_THEME_COLOR_BLACK;
} else { } else {
@ -543,8 +561,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
} }
} }
private void updateSettingsKeyState(SharedPreferences prefs) { private static boolean getSettingsKeyMode(SharedPreferences prefs, Context context) {
Resources resources = mInputMethodService.getResources(); Resources resources = context.getResources();
final boolean showSettingsKeyOption = resources.getBoolean( final boolean showSettingsKeyOption = resources.getBoolean(
R.bool.config_enable_show_settings_key_option); R.bool.config_enable_show_settings_key_option);
if (showSettingsKeyOption) { if (showSettingsKeyOption) {
@ -554,11 +572,10 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
// 2) SETTINGS_KEY_MODE_AUTO and there are two or more enabled IMEs on the system // 2) SETTINGS_KEY_MODE_AUTO and there are two or more enabled IMEs on the system
if (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW)) if (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_ALWAYS_SHOW))
|| (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_AUTO)) || (settingsKeyMode.equals(resources.getString(SETTINGS_KEY_MODE_AUTO))
&& LatinIMEUtil.hasMultipleEnabledIMEs(mInputMethodService))) { && LatinIMEUtil.hasMultipleEnabledIMEs(context))) {
mHasSettingsKey = true; return true;
return;
} }
} }
mHasSettingsKey = false; return false;
} }
} }

View File

@ -16,21 +16,22 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import java.util.Locale;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Locale;
/** /**
* Keeps track of list of selected input languages and the current * Keeps track of list of selected input languages and the current
* input language that the user has selected. * input language that the user has selected.
*/ */
public class LanguageSwitcher { public class LanguageSwitcher {
private Locale[] mLocales; private final ArrayList<Locale> mLocales = new ArrayList<Locale>();
private LatinIME mIme; private final LatinIME mIme;
private String[] mSelectedLanguageArray; private String[] mSelectedLanguageArray;
private String mSelectedLanguages; private String mSelectedLanguages;
private int mCurrentIndex = 0; private int mCurrentIndex = 0;
@ -40,15 +41,10 @@ public class LanguageSwitcher {
public LanguageSwitcher(LatinIME ime) { public LanguageSwitcher(LatinIME ime) {
mIme = ime; mIme = ime;
mLocales = new Locale[0];
}
public Locale[] getLocales() {
return mLocales;
} }
public int getLocaleCount() { public int getLocaleCount() {
return mLocales.length; return mLocales.size();
} }
/** /**
@ -61,10 +57,10 @@ public class LanguageSwitcher {
String currentLanguage = sp.getString(LatinIME.PREF_INPUT_LANGUAGE, null); String currentLanguage = sp.getString(LatinIME.PREF_INPUT_LANGUAGE, null);
if (selectedLanguages == null || selectedLanguages.length() < 1) { if (selectedLanguages == null || selectedLanguages.length() < 1) {
loadDefaults(); loadDefaults();
if (mLocales.length == 0) { if (mLocales.size() == 0) {
return false; return false;
} }
mLocales = new Locale[0]; mLocales.clear();
return true; return true;
} }
if (selectedLanguages.equals(mSelectedLanguages)) { if (selectedLanguages.equals(mSelectedLanguages)) {
@ -77,7 +73,7 @@ public class LanguageSwitcher {
if (currentLanguage != null) { if (currentLanguage != null) {
// Find the index // Find the index
mCurrentIndex = 0; mCurrentIndex = 0;
for (int i = 0; i < mLocales.length; i++) { for (int i = 0; i < mLocales.size(); i++) {
if (mSelectedLanguageArray[i].equals(currentLanguage)) { if (mSelectedLanguageArray[i].equals(currentLanguage)) {
mCurrentIndex = i; mCurrentIndex = i;
break; break;
@ -96,11 +92,11 @@ public class LanguageSwitcher {
} }
private void constructLocales() { private void constructLocales() {
mLocales = new Locale[mSelectedLanguageArray.length]; mLocales.clear();
for (int i = 0; i < mLocales.length; i++) { for (final String lang : mSelectedLanguageArray) {
final String lang = mSelectedLanguageArray[i]; final Locale locale = new Locale(lang.substring(0, 2),
mLocales[i] = new Locale(lang.substring(0, 2),
lang.length() > 4 ? lang.substring(3, 5) : ""); lang.length() > 4 ? lang.substring(3, 5) : "");
mLocales.add(locale);
} }
} }
@ -129,7 +125,17 @@ public class LanguageSwitcher {
public Locale getInputLocale() { public Locale getInputLocale() {
if (getLocaleCount() == 0) return mDefaultInputLocale; if (getLocaleCount() == 0) return mDefaultInputLocale;
return mLocales[mCurrentIndex]; return mLocales.get(mCurrentIndex);
}
private int nextLocaleIndex() {
final int size = mLocales.size();
return (mCurrentIndex + 1) % size;
}
private int prevLocaleIndex() {
final int size = mLocales.size();
return (mCurrentIndex - 1 + size) % size;
} }
/** /**
@ -139,8 +145,7 @@ public class LanguageSwitcher {
*/ */
public Locale getNextInputLocale() { public Locale getNextInputLocale() {
if (getLocaleCount() == 0) return mDefaultInputLocale; if (getLocaleCount() == 0) return mDefaultInputLocale;
return mLocales.get(nextLocaleIndex());
return mLocales[(mCurrentIndex + 1) % mLocales.length];
} }
/** /**
@ -166,8 +171,7 @@ public class LanguageSwitcher {
*/ */
public Locale getPrevInputLocale() { public Locale getPrevInputLocale() {
if (getLocaleCount() == 0) return mDefaultInputLocale; if (getLocaleCount() == 0) return mDefaultInputLocale;
return mLocales.get(prevLocaleIndex());
return mLocales[(mCurrentIndex - 1 + mLocales.length) % mLocales.length];
} }
public void reset() { public void reset() {
@ -175,13 +179,11 @@ public class LanguageSwitcher {
} }
public void next() { public void next() {
mCurrentIndex++; mCurrentIndex = nextLocaleIndex();
if (mCurrentIndex >= mLocales.length) mCurrentIndex = 0; // Wrap around
} }
public void prev() { public void prev() {
mCurrentIndex--; mCurrentIndex = prevLocaleIndex();
if (mCurrentIndex < 0) mCurrentIndex = mLocales.length - 1; // Wrap around
} }
public void persist() { public void persist() {

View File

@ -348,8 +348,7 @@ public class LatinIME extends InputMethodService
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mLanguageSwitcher = new LanguageSwitcher(this); mLanguageSwitcher = new LanguageSwitcher(this);
mLanguageSwitcher.loadLocales(prefs); mLanguageSwitcher.loadLocales(prefs);
mKeyboardSwitcher = new KeyboardSwitcher(this); mKeyboardSwitcher = new KeyboardSwitcher(this, mLanguageSwitcher);
mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher);
mSystemLocale = conf.locale.toString(); mSystemLocale = conf.locale.toString();
mLanguageSwitcher.setSystemLocale(conf.locale); mLanguageSwitcher.setSystemLocale(conf.locale);
String inputLanguage = mLanguageSwitcher.getInputLanguage(); String inputLanguage = mLanguageSwitcher.getInputLanguage();
@ -501,14 +500,10 @@ public class LatinIME extends InputMethodService
final String systemLocale = conf.locale.toString(); final String systemLocale = conf.locale.toString();
if (!TextUtils.equals(systemLocale, mSystemLocale)) { if (!TextUtils.equals(systemLocale, mSystemLocale)) {
mSystemLocale = systemLocale; mSystemLocale = systemLocale;
if (mLanguageSwitcher != null) { mLanguageSwitcher.loadLocales(
mLanguageSwitcher.loadLocales( PreferenceManager.getDefaultSharedPreferences(this));
PreferenceManager.getDefaultSharedPreferences(this)); mLanguageSwitcher.setSystemLocale(conf.locale);
mLanguageSwitcher.setSystemLocale(conf.locale); toggleLanguage(true, true);
toggleLanguage(true, true);
} else {
reloadKeyboards();
}
} }
// If orientation changed while predicting, commit the change // If orientation changed while predicting, commit the change
if (conf.orientation != mOrientation) { if (conf.orientation != mOrientation) {
@ -516,8 +511,12 @@ public class LatinIME extends InputMethodService
commitTyped(ic); commitTyped(ic);
if (ic != null) ic.finishComposingText(); // For voice input if (ic != null) ic.finishComposingText(); // For voice input
mOrientation = conf.orientation; mOrientation = conf.orientation;
reloadKeyboards(); final int mode = mKeyboardSwitcher.getKeyboardMode();
final EditorInfo attribute = getCurrentInputEditorInfo();
mKeyboardSwitcher.loadKeyboard(mode, attribute.imeOptions, mVoiceButtonEnabled,
mVoiceButtonOnPrimary);
} }
mConfigurationChanging = true; mConfigurationChanging = true;
super.onConfigurationChanged(conf); super.onConfigurationChanged(conf);
if (mRecognizing) { if (mRecognizing) {
@ -528,14 +527,12 @@ public class LatinIME extends InputMethodService
@Override @Override
public View onCreateInputView() { public View onCreateInputView() {
mKeyboardSwitcher.recreateInputView(); mKeyboardSwitcher.loadKeyboardView();
mKeyboardSwitcher.refreshKeyboardCache(true);
return mKeyboardSwitcher.getInputView(); return mKeyboardSwitcher.getInputView();
} }
@Override @Override
public View onCreateCandidatesView() { public View onCreateCandidatesView() {
mKeyboardSwitcher.refreshKeyboardCache(true);
mCandidateViewContainer = (LinearLayout) getLayoutInflater().inflate( mCandidateViewContainer = (LinearLayout) getLayoutInflater().inflate(
R.layout.candidates, null); R.layout.candidates, null);
mCandidateView = (CandidateView) mCandidateViewContainer.findViewById(R.id.candidates); mCandidateView = (CandidateView) mCandidateViewContainer.findViewById(R.id.candidates);
@ -568,8 +565,6 @@ public class LatinIME extends InputMethodService
toggleLanguage(true, true); toggleLanguage(true, true);
} }
mKeyboardSwitcher.refreshKeyboardCache(false);
TextEntryState.newSession(this); TextEntryState.newSession(this);
// Most such things we decide below in the switch statement, but we need to know // Most such things we decide below in the switch statement, but we need to know
@ -663,7 +658,7 @@ public class LatinIME extends InputMethodService
mJustAddedAutoSpace = false; mJustAddedAutoSpace = false;
loadSettings(attribute); loadSettings(attribute);
mKeyboardSwitcher.setKeyboardMode(mode, attribute.imeOptions, mVoiceButtonEnabled, mKeyboardSwitcher.loadKeyboard(mode, attribute.imeOptions, mVoiceButtonEnabled,
mVoiceButtonOnPrimary); mVoiceButtonOnPrimary);
updateShiftKeyState(attribute); updateShiftKeyState(attribute);
@ -1024,12 +1019,6 @@ public class LatinIME extends InputMethodService
mVoiceInputHighlighted = false; mVoiceInputHighlighted = false;
} }
private void reloadKeyboards() {
mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher);
int mode = mKeyboardSwitcher.getKeyboardMode();
mKeyboardSwitcher.setKeyboardMode(mode, 0, mVoiceButtonEnabled, mVoiceButtonOnPrimary);
}
private void commitTyped(InputConnection inputConnection) { private void commitTyped(InputConnection inputConnection) {
if (mPredicting) { if (mPredicting) {
mPredicting = false; mPredicting = false;
@ -2282,10 +2271,9 @@ public class LatinIME extends InputMethodService
mLanguageSwitcher.prev(); mLanguageSwitcher.prev();
} }
} }
int currentKeyboardMode = mKeyboardSwitcher.getKeyboardMode(); final int mode = mKeyboardSwitcher.getKeyboardMode();
reloadKeyboards(); final EditorInfo attribute = getCurrentInputEditorInfo();
mKeyboardSwitcher.refreshKeyboardCache(true); mKeyboardSwitcher.loadKeyboard(mode, attribute.imeOptions, mVoiceButtonEnabled,
mKeyboardSwitcher.setKeyboardMode(currentKeyboardMode, 0, mVoiceButtonEnabled,
mVoiceButtonOnPrimary); mVoiceButtonOnPrimary);
initSuggest(mLanguageSwitcher.getInputLanguage()); initSuggest(mLanguageSwitcher.getInputLanguage());
mLanguageSwitcher.persist(); mLanguageSwitcher.persist();

View File

@ -113,11 +113,9 @@ public class LatinKeyboard extends BaseKeyboard {
private static int sSpacebarVerticalCorrection; private static int sSpacebarVerticalCorrection;
public LatinKeyboard(Context context, int xmlLayoutResId) { public LatinKeyboard(Context context, int xmlLayoutResId, int mode, int colorScheme,
this(context, xmlLayoutResId, 0); boolean hasSettingsKey, boolean hasVoiceKey, int imeOptions) {
} // TODO: to be used: colorScheme, hasSettingsKey, hasVoiceKey, imeOptions.
public LatinKeyboard(Context context, int xmlLayoutResId, int mode) {
super(context, xmlLayoutResId, mode); super(context, xmlLayoutResId, mode);
final Resources res = context.getResources(); final Resources res = context.getResources();
mContext = context; mContext = context;
@ -291,7 +289,7 @@ public class LatinKeyboard extends BaseKeyboard {
return mIsAlphaKeyboard; return mIsAlphaKeyboard;
} }
public void setColorOfSymbolIcons(boolean isAutoCompletion, boolean isBlack) { public void setColorOfSymbolIcons(boolean isBlack) {
mIsBlackSym = isBlack; mIsBlackSym = isBlack;
final Resources res = mRes; final Resources res = mRes;
if (isBlack) { if (isBlack) {
@ -306,9 +304,6 @@ public class LatinKeyboard extends BaseKeyboard {
m123MicIcon = res.getDrawable(R.drawable.sym_keyboard_123_mic); m123MicIcon = res.getDrawable(R.drawable.sym_keyboard_123_mic);
} }
updateDynamicKeys(); updateDynamicKeys();
if (mSpaceKey != null) {
updateSpaceBarForLocale(isAutoCompletion, isBlack);
}
} }
public void setVoiceMode(boolean hasVoiceButton, boolean hasVoice) { public void setVoiceMode(boolean hasVoiceButton, boolean hasVoice) {
@ -547,8 +542,7 @@ public class LatinKeyboard extends BaseKeyboard {
return mSpaceDragLastDiff > 0 ? 1 : -1; return mSpaceDragLastDiff > 0 ? 1 : -1;
} }
public void setLanguageSwitcher(LanguageSwitcher switcher, boolean isAutoCompletion, public void setLanguageSwitcher(LanguageSwitcher switcher) {
boolean isBlackSym) {
mLanguageSwitcher = switcher; mLanguageSwitcher = switcher;
Locale locale = mLanguageSwitcher.getLocaleCount() > 0 Locale locale = mLanguageSwitcher.getLocaleCount() > 0
? mLanguageSwitcher.getInputLocale() ? mLanguageSwitcher.getInputLocale()
@ -561,7 +555,6 @@ public class LatinKeyboard extends BaseKeyboard {
locale = null; locale = null;
} }
mLocale = locale; mLocale = locale;
setColorOfSymbolIcons(isAutoCompletion, isBlackSym);
} }
boolean isCurrentlyInSpace() { boolean isCurrentlyInSpace() {