Refatcor InputMethodSubtype related stuff a bit

Change-Id: Iaded72331660afbaeddda085f2b633b681d4b6df
main
Tadashi G. Takaoka 2012-04-13 13:07:28 +09:00
parent 35b5a7babb
commit 8abde7db6b
6 changed files with 66 additions and 81 deletions

View File

@ -17,7 +17,6 @@
package com.android.inputmethod.compat;
import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.os.IBinder;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
@ -50,9 +49,8 @@ public class InputMethodManagerCompatWrapper {
return sInstance;
}
public static void init(InputMethodService service) {
sInstance.mImm = (InputMethodManager) service.getSystemService(
Context.INPUT_METHOD_SERVICE);
public static void init(Context context) {
sInstance.mImm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
}
public InputMethodSubtype getCurrentInputMethodSubtype() {
@ -86,6 +84,11 @@ public class InputMethodManagerCompatWrapper {
onlyCurrentIme);
}
public List<InputMethodInfo> getInputMethodList() {
if (mImm == null) return null;
return mImm.getInputMethodList();
}
public List<InputMethodInfo> getEnabledInputMethodList() {
if (mImm == null) return null;
return mImm.getEnabledInputMethodList();

View File

@ -32,7 +32,6 @@ import com.android.inputmethod.keyboard.KeyboardLayoutSet.Params.ElementParams;
import com.android.inputmethod.latin.InputTypeUtils;
import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SubtypeLocale;
@ -61,8 +60,6 @@ public class KeyboardLayoutSet {
private static final String TAG_KEYBOARD_SET = "KeyboardLayoutSet";
private static final String TAG_ELEMENT = "Element";
private static final String DEFAULT_KEYBOARD_LAYOUT_SET = "qwerty";
private static final char KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER = ':';
private static final String KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX = "keyboard_layout_set_";
private final Context mContext;
@ -164,14 +161,13 @@ public class KeyboardLayoutSet {
}
final KeyboardId id = getKeyboardId(keyboardLayoutSetElementId);
try {
return getKeyboard(mContext, elementParams, id);
return getKeyboard(elementParams, id);
} catch (RuntimeException e) {
throw new KeyboardLayoutSetException(e, id);
}
}
private Keyboard getKeyboard(Context context, ElementParams elementParams,
final KeyboardId id) {
private Keyboard getKeyboard(ElementParams elementParams, final KeyboardId id) {
final SoftReference<Keyboard> ref = sKeyboardCache.get(id);
Keyboard keyboard = (ref == null) ? null : ref.get();
if (keyboard == null) {
@ -215,30 +211,6 @@ public class KeyboardLayoutSet {
voiceKeyEnabled, hasShortcutKey, params.mLanguageSwitchKeyEnabled);
}
private static String getKeyboardLayoutSetName(InputMethodSubtype subtype) {
final String keyboardLayoutSet = subtype.getExtraValueOf(
LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
// TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
// fixed.
if (keyboardLayoutSet == null) return DEFAULT_KEYBOARD_LAYOUT_SET;
final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
return (pos > 0) ? keyboardLayoutSet.substring(0, pos) : keyboardLayoutSet;
}
public static String getKeyboardLayoutSetLocaleString(InputMethodSubtype subtype) {
final String keyboardLayoutSet = subtype.getExtraValueOf(
LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
// TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
// fixed.
if (keyboardLayoutSet == null) return subtype.getLocale();
final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
return (pos > 0) ? keyboardLayoutSet.substring(pos + 1) : subtype.getLocale();
}
public static Locale getKeyboardLayoutSetLocale(InputMethodSubtype subtype) {
return LocaleUtils.constructLocaleFromString(getKeyboardLayoutSetLocaleString(subtype));
}
public static class Builder {
private final Context mContext;
private final String mPackageName;
@ -279,9 +251,9 @@ public class KeyboardLayoutSet {
final InputMethodSubtype keyboardSubtype = (forceAscii && !asciiCapable)
? SubtypeSwitcher.getInstance().getNoLanguageSubtype()
: subtype;
mParams.mLocale = getKeyboardLayoutSetLocale(keyboardSubtype);
mParams.mLocale = SubtypeLocale.getKeyboardLayoutSetLocale(keyboardSubtype);
mParams.mKeyboardLayoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX
+ getKeyboardLayoutSetName(keyboardSubtype);
+ SubtypeLocale.getKeyboardLayoutSetName(keyboardSubtype);
return this;
}

View File

@ -18,6 +18,8 @@ package com.android.inputmethod.latin;
import android.content.Context;
import android.content.res.Resources;
import android.view.inputmethod.InputMethodSubtype;
import java.util.Locale;
@ -33,6 +35,9 @@ public class SubtypeLocale {
private static String[] sExceptionKeys;
private static String[] sExceptionValues;
private static final String DEFAULT_KEYBOARD_LAYOUT_SET = "qwerty";
private static final char KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER = ':';
private SubtypeLocale() {
// Intentional empty constructor for utility class.
}
@ -72,7 +77,8 @@ public class SubtypeLocale {
return StringUtils.toTitleCase(locale.getDisplayName(locale), locale);
}
if (value.indexOf("%s") >= 0) {
final String languageName = StringUtils.toTitleCase(locale.getDisplayLanguage(locale), locale);
final String languageName = StringUtils.toTitleCase(
locale.getDisplayLanguage(locale), locale);
return String.format(value, languageName);
}
return value;
@ -105,4 +111,28 @@ public class SubtypeLocale {
return StringUtils.toTitleCase(locale.getLanguage(), locale);
}
}
public static String getKeyboardLayoutSetName(InputMethodSubtype subtype) {
final String keyboardLayoutSet = subtype.getExtraValueOf(
LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
// TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
// fixed.
if (keyboardLayoutSet == null) return DEFAULT_KEYBOARD_LAYOUT_SET;
final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
return (pos > 0) ? keyboardLayoutSet.substring(0, pos) : keyboardLayoutSet;
}
public static String getKeyboardLayoutSetLocaleString(InputMethodSubtype subtype) {
final String keyboardLayoutSet = subtype.getExtraValueOf(
LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
// TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
// fixed.
if (keyboardLayoutSet == null) return subtype.getLocale();
final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
return (pos > 0) ? keyboardLayoutSet.substring(pos + 1) : subtype.getLocale();
}
public static Locale getKeyboardLayoutSetLocale(InputMethodSubtype subtype) {
return LocaleUtils.constructLocaleFromString(getKeyboardLayoutSetLocaleString(subtype));
}
}

View File

@ -31,7 +31,6 @@ import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.keyboard.KeyboardLayoutSet;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import java.util.ArrayList;
@ -105,8 +104,8 @@ public class SubtypeSwitcher {
mInputLocaleStr = null;
mCurrentSubtype = mImm.getCurrentInputMethodSubtype();
mAllEnabledSubtypesOfCurrentInputMethod = null;
mNoLanguageSubtype = SubtypeUtils.findSubtypeByKeyboardLayoutSetLocale(
service, SubtypeLocale.LOCALE_NO_LANGUAGE_QWERTY);
mNoLanguageSubtype = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
service, SubtypeLocale.LOCALE_NO_LANGUAGE_QWERTY, "qwerty");
final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
mIsNetworkConnected = (info != null && info.isConnected());
@ -129,14 +128,14 @@ public class SubtypeSwitcher {
// Reload enabledSubtypes from the framework.
private void updateEnabledSubtypes() {
final String currentMode = getCurrentSubtypeMode();
final String currentMode = mCurrentSubtype.getMode();
boolean foundCurrentSubtypeBecameDisabled = true;
mAllEnabledSubtypesOfCurrentInputMethod = mImm.getEnabledInputMethodSubtypeList(
null, true);
mEnabledLanguagesOfCurrentInputMethod.clear();
mEnabledKeyboardSubtypesOfCurrentInputMethod.clear();
for (InputMethodSubtype ims : mAllEnabledSubtypesOfCurrentInputMethod) {
final String locale = KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(ims);
final String locale = SubtypeLocale.getKeyboardLayoutSetLocaleString(ims);
final String mode = ims.getMode();
mLocaleSplitter.setString(locale);
if (mLocaleSplitter.hasNext()) {
@ -166,7 +165,7 @@ public class SubtypeSwitcher {
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
SubtypeLocale.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
+ ", " + mShortcutSubtype.getMode())));
}
// TODO: Update an icon for shortcut IME
@ -189,16 +188,16 @@ public class SubtypeSwitcher {
+ (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : (
KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
SubtypeLocale.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
+ ", " + mShortcutSubtype.getMode())));
}
}
// Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
public void updateSubtype(InputMethodSubtype newSubtype) {
final String newLocale = KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(newSubtype);
final String newLocale = SubtypeLocale.getKeyboardLayoutSetLocaleString(newSubtype);
final String newMode = newSubtype.getMode();
final String oldMode = getCurrentSubtypeMode();
final String oldMode = mCurrentSubtype.getMode();
if (DBG) {
Log.w(TAG, "Update subtype to:" + newLocale + "," + newMode
+ ", from: " + mInputLocaleStr + ", " + oldMode);
@ -218,7 +217,7 @@ public class SubtypeSwitcher {
}
mCurrentSubtype = newSubtype;
if (isKeyboardMode()) {
if (KEYBOARD_MODE.equals(mCurrentSubtype.getMode())) {
if (modeChanged || languageChanged) {
updateShortcutIME();
mService.onRefreshKeyboard();
@ -233,12 +232,10 @@ public class SubtypeSwitcher {
}
Log.w(TAG, "Unknown subtype mode: " + newMode + "," + version + ", " + packageName
+ ". IME is already changed to other IME.");
if (newSubtype != null) {
Log.w(TAG, "Subtype mode:" + newSubtype.getMode());
Log.w(TAG, "Subtype locale:" + newSubtype.getLocale());
Log.w(TAG, "Subtype extra value:" + newSubtype.getExtraValue());
Log.w(TAG, "Subtype is auxiliary:" + newSubtype.isAuxiliary());
}
Log.w(TAG, "Subtype mode:" + newSubtype.getMode());
Log.w(TAG, "Subtype locale:" + newSubtype.getLocale());
Log.w(TAG, "Subtype extra value:" + newSubtype.getExtraValue());
Log.w(TAG, "Subtype is auxiliary:" + newSubtype.isAuxiliary());
}
}
@ -282,10 +279,11 @@ public class SubtypeSwitcher {
if (token == null) {
return;
}
final InputMethodManagerCompatWrapper imm = mImm;
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
mImm.setInputMethodAndSubtype(token, imiId, subtype);
imm.setInputMethodAndSubtype(token, imiId, subtype);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -385,22 +383,6 @@ public class SubtypeSwitcher {
return mIsDictionaryAvailable;
}
// TODO: Remove this method
private boolean isKeyboardMode() {
return KEYBOARD_MODE.equals(getCurrentSubtypeMode());
}
// TODO: Remove this method
private String getCurrentSubtypeMode() {
return mCurrentSubtype.getMode();
}
// TODO: Remove this method
public boolean currentSubtypeContainsExtraValueKey(String key) {
// If null, return what an empty ExtraValue would return : false.
return mCurrentSubtype.containsExtraValueKey(key);
}
public InputMethodSubtype getCurrentSubtype() {
return mCurrentSubtype;
}

View File

@ -21,7 +21,6 @@ import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.keyboard.KeyboardLayoutSet;
import java.util.Collections;
import java.util.List;
@ -125,21 +124,22 @@ public class SubtypeUtils {
throw new RuntimeException("Input method manager not found");
}
for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
for (final InputMethodInfo imi : imm.getInputMethodList()) {
if (imi.getPackageName().equals(packageName))
return imi;
}
throw new RuntimeException("Can not find input method id for " + packageName);
}
public static InputMethodSubtype findSubtypeByKeyboardLayoutSetLocale(
Context context, Locale locale) {
public static InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(
Context context, Locale locale, String keyoardLayoutSet) {
final String localeString = locale.toString();
final InputMethodInfo imi = SubtypeUtils.getInputMethodInfo(context.getPackageName());
final InputMethodInfo imi = getInputMethodInfo(context.getPackageName());
final int count = imi.getSubtypeCount();
for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = imi.getSubtypeAt(i);
if (localeString.equals(KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(subtype))) {
final String layout = SubtypeLocale.getKeyboardLayoutSetName(subtype);
if (localeString.equals(subtype.getLocale()) && keyoardLayoutSet.equals(layout)) {
return subtype;
}
}

View File

@ -22,8 +22,6 @@ import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.keyboard.KeyboardLayoutSet;
import java.util.ArrayList;
import java.util.Locale;
@ -64,7 +62,7 @@ public class SubtypeLocaleTests extends AndroidTestCase {
final StringBuilder messages = new StringBuilder();
int failedCount = 0;
for (final InputMethodSubtype subtype : mSubtypesList) {
final Locale locale = KeyboardLayoutSet.getKeyboardLayoutSetLocale(subtype);
final Locale locale = SubtypeLocale.getKeyboardLayoutSetLocale(subtype);
if (locale.getLanguage().equals(SubtypeLocale.NO_LANGUAGE)) {
// This is special language name for language agnostic usage.
continue;
@ -94,7 +92,7 @@ public class SubtypeLocaleTests extends AndroidTestCase {
final StringBuilder messages = new StringBuilder();
int failedCount = 0;
for (final InputMethodSubtype subtype : mSubtypesList) {
final Locale locale = KeyboardLayoutSet.getKeyboardLayoutSetLocale(subtype);
final Locale locale = SubtypeLocale.getKeyboardLayoutSetLocale(subtype);
if (locale.getLanguage().equals(SubtypeLocale.NO_LANGUAGE)) {
// This is special language name for language agnostic usage.
continue;
@ -121,7 +119,7 @@ public class SubtypeLocaleTests extends AndroidTestCase {
final StringBuilder messages = new StringBuilder();
int failedCount = 0;
for (final InputMethodSubtype subtype : mSubtypesList) {
final Locale locale = KeyboardLayoutSet.getKeyboardLayoutSetLocale(subtype);
final Locale locale = SubtypeLocale.getKeyboardLayoutSetLocale(subtype);
if (locale.getCountry().equals(SubtypeLocale.QWERTY)) {
// This is special country code for QWERTY keyboard.
continue;