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; package com.android.inputmethod.compat;
import android.content.Context; import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
@ -50,9 +49,8 @@ public class InputMethodManagerCompatWrapper {
return sInstance; return sInstance;
} }
public static void init(InputMethodService service) { public static void init(Context context) {
sInstance.mImm = (InputMethodManager) service.getSystemService( sInstance.mImm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
Context.INPUT_METHOD_SERVICE);
} }
public InputMethodSubtype getCurrentInputMethodSubtype() { public InputMethodSubtype getCurrentInputMethodSubtype() {
@ -86,6 +84,11 @@ public class InputMethodManagerCompatWrapper {
onlyCurrentIme); onlyCurrentIme);
} }
public List<InputMethodInfo> getInputMethodList() {
if (mImm == null) return null;
return mImm.getInputMethodList();
}
public List<InputMethodInfo> getEnabledInputMethodList() { public List<InputMethodInfo> getEnabledInputMethodList() {
if (mImm == null) return null; if (mImm == null) return null;
return mImm.getEnabledInputMethodList(); 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.InputTypeUtils;
import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LatinIME;
import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.LocaleUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.StringUtils; import com.android.inputmethod.latin.StringUtils;
import com.android.inputmethod.latin.SubtypeLocale; 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_KEYBOARD_SET = "KeyboardLayoutSet";
private static final String TAG_ELEMENT = "Element"; 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 static final String KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX = "keyboard_layout_set_";
private final Context mContext; private final Context mContext;
@ -164,14 +161,13 @@ public class KeyboardLayoutSet {
} }
final KeyboardId id = getKeyboardId(keyboardLayoutSetElementId); final KeyboardId id = getKeyboardId(keyboardLayoutSetElementId);
try { try {
return getKeyboard(mContext, elementParams, id); return getKeyboard(elementParams, id);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new KeyboardLayoutSetException(e, id); throw new KeyboardLayoutSetException(e, id);
} }
} }
private Keyboard getKeyboard(Context context, ElementParams elementParams, private Keyboard getKeyboard(ElementParams elementParams, final KeyboardId id) {
final KeyboardId id) {
final SoftReference<Keyboard> ref = sKeyboardCache.get(id); final SoftReference<Keyboard> ref = sKeyboardCache.get(id);
Keyboard keyboard = (ref == null) ? null : ref.get(); Keyboard keyboard = (ref == null) ? null : ref.get();
if (keyboard == null) { if (keyboard == null) {
@ -215,30 +211,6 @@ public class KeyboardLayoutSet {
voiceKeyEnabled, hasShortcutKey, params.mLanguageSwitchKeyEnabled); 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 { public static class Builder {
private final Context mContext; private final Context mContext;
private final String mPackageName; private final String mPackageName;
@ -279,9 +251,9 @@ public class KeyboardLayoutSet {
final InputMethodSubtype keyboardSubtype = (forceAscii && !asciiCapable) final InputMethodSubtype keyboardSubtype = (forceAscii && !asciiCapable)
? SubtypeSwitcher.getInstance().getNoLanguageSubtype() ? SubtypeSwitcher.getInstance().getNoLanguageSubtype()
: subtype; : subtype;
mParams.mLocale = getKeyboardLayoutSetLocale(keyboardSubtype); mParams.mLocale = SubtypeLocale.getKeyboardLayoutSetLocale(keyboardSubtype);
mParams.mKeyboardLayoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX mParams.mKeyboardLayoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX
+ getKeyboardLayoutSetName(keyboardSubtype); + SubtypeLocale.getKeyboardLayoutSetName(keyboardSubtype);
return this; return this;
} }

View File

@ -18,6 +18,8 @@ package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.view.inputmethod.InputMethodSubtype;
import java.util.Locale; import java.util.Locale;
@ -33,6 +35,9 @@ public class SubtypeLocale {
private static String[] sExceptionKeys; private static String[] sExceptionKeys;
private static String[] sExceptionValues; private static String[] sExceptionValues;
private static final String DEFAULT_KEYBOARD_LAYOUT_SET = "qwerty";
private static final char KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER = ':';
private SubtypeLocale() { private SubtypeLocale() {
// Intentional empty constructor for utility class. // Intentional empty constructor for utility class.
} }
@ -72,7 +77,8 @@ public class SubtypeLocale {
return StringUtils.toTitleCase(locale.getDisplayName(locale), locale); return StringUtils.toTitleCase(locale.getDisplayName(locale), locale);
} }
if (value.indexOf("%s") >= 0) { 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 String.format(value, languageName);
} }
return value; return value;
@ -105,4 +111,28 @@ public class SubtypeLocale {
return StringUtils.toTitleCase(locale.getLanguage(), locale); 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 android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.keyboard.KeyboardLayoutSet;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import java.util.ArrayList; import java.util.ArrayList;
@ -105,8 +104,8 @@ public class SubtypeSwitcher {
mInputLocaleStr = null; mInputLocaleStr = null;
mCurrentSubtype = mImm.getCurrentInputMethodSubtype(); mCurrentSubtype = mImm.getCurrentInputMethodSubtype();
mAllEnabledSubtypesOfCurrentInputMethod = null; mAllEnabledSubtypesOfCurrentInputMethod = null;
mNoLanguageSubtype = SubtypeUtils.findSubtypeByKeyboardLayoutSetLocale( mNoLanguageSubtype = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
service, SubtypeLocale.LOCALE_NO_LANGUAGE_QWERTY); service, SubtypeLocale.LOCALE_NO_LANGUAGE_QWERTY, "qwerty");
final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
mIsNetworkConnected = (info != null && info.isConnected()); mIsNetworkConnected = (info != null && info.isConnected());
@ -129,14 +128,14 @@ public class SubtypeSwitcher {
// Reload enabledSubtypes from the framework. // Reload enabledSubtypes from the framework.
private void updateEnabledSubtypes() { private void updateEnabledSubtypes() {
final String currentMode = getCurrentSubtypeMode(); final String currentMode = mCurrentSubtype.getMode();
boolean foundCurrentSubtypeBecameDisabled = true; boolean foundCurrentSubtypeBecameDisabled = true;
mAllEnabledSubtypesOfCurrentInputMethod = mImm.getEnabledInputMethodSubtypeList( mAllEnabledSubtypesOfCurrentInputMethod = mImm.getEnabledInputMethodSubtypeList(
null, true); null, true);
mEnabledLanguagesOfCurrentInputMethod.clear(); mEnabledLanguagesOfCurrentInputMethod.clear();
mEnabledKeyboardSubtypesOfCurrentInputMethod.clear(); mEnabledKeyboardSubtypesOfCurrentInputMethod.clear();
for (InputMethodSubtype ims : mAllEnabledSubtypesOfCurrentInputMethod) { for (InputMethodSubtype ims : mAllEnabledSubtypesOfCurrentInputMethod) {
final String locale = KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(ims); final String locale = SubtypeLocale.getKeyboardLayoutSetLocaleString(ims);
final String mode = ims.getMode(); final String mode = ims.getMode();
mLocaleSplitter.setString(locale); mLocaleSplitter.setString(locale);
if (mLocaleSplitter.hasNext()) { if (mLocaleSplitter.hasNext()) {
@ -166,7 +165,7 @@ public class SubtypeSwitcher {
+ (mShortcutInputMethodInfo == null + (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", " ? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : ( + (mShortcutSubtype == null ? "<null>" : (
KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(mShortcutSubtype) SubtypeLocale.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
+ ", " + mShortcutSubtype.getMode()))); + ", " + mShortcutSubtype.getMode())));
} }
// TODO: Update an icon for shortcut IME // TODO: Update an icon for shortcut IME
@ -189,16 +188,16 @@ public class SubtypeSwitcher {
+ (mShortcutInputMethodInfo == null + (mShortcutInputMethodInfo == null
? "<null>" : mShortcutInputMethodInfo.getId()) + ", " ? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
+ (mShortcutSubtype == null ? "<null>" : ( + (mShortcutSubtype == null ? "<null>" : (
KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(mShortcutSubtype) SubtypeLocale.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
+ ", " + mShortcutSubtype.getMode()))); + ", " + mShortcutSubtype.getMode())));
} }
} }
// Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function. // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
public void updateSubtype(InputMethodSubtype newSubtype) { public void updateSubtype(InputMethodSubtype newSubtype) {
final String newLocale = KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(newSubtype); final String newLocale = SubtypeLocale.getKeyboardLayoutSetLocaleString(newSubtype);
final String newMode = newSubtype.getMode(); final String newMode = newSubtype.getMode();
final String oldMode = getCurrentSubtypeMode(); final String oldMode = mCurrentSubtype.getMode();
if (DBG) { if (DBG) {
Log.w(TAG, "Update subtype to:" + newLocale + "," + newMode Log.w(TAG, "Update subtype to:" + newLocale + "," + newMode
+ ", from: " + mInputLocaleStr + ", " + oldMode); + ", from: " + mInputLocaleStr + ", " + oldMode);
@ -218,7 +217,7 @@ public class SubtypeSwitcher {
} }
mCurrentSubtype = newSubtype; mCurrentSubtype = newSubtype;
if (isKeyboardMode()) { if (KEYBOARD_MODE.equals(mCurrentSubtype.getMode())) {
if (modeChanged || languageChanged) { if (modeChanged || languageChanged) {
updateShortcutIME(); updateShortcutIME();
mService.onRefreshKeyboard(); mService.onRefreshKeyboard();
@ -233,14 +232,12 @@ public class SubtypeSwitcher {
} }
Log.w(TAG, "Unknown subtype mode: " + newMode + "," + version + ", " + packageName Log.w(TAG, "Unknown subtype mode: " + newMode + "," + version + ", " + packageName
+ ". IME is already changed to other IME."); + ". IME is already changed to other IME.");
if (newSubtype != null) {
Log.w(TAG, "Subtype mode:" + newSubtype.getMode()); Log.w(TAG, "Subtype mode:" + newSubtype.getMode());
Log.w(TAG, "Subtype locale:" + newSubtype.getLocale()); Log.w(TAG, "Subtype locale:" + newSubtype.getLocale());
Log.w(TAG, "Subtype extra value:" + newSubtype.getExtraValue()); Log.w(TAG, "Subtype extra value:" + newSubtype.getExtraValue());
Log.w(TAG, "Subtype is auxiliary:" + newSubtype.isAuxiliary()); Log.w(TAG, "Subtype is auxiliary:" + newSubtype.isAuxiliary());
} }
} }
}
// Update the current input locale from Locale string. // Update the current input locale from Locale string.
private void updateInputLocale(String inputLocaleStr) { private void updateInputLocale(String inputLocaleStr) {
@ -282,10 +279,11 @@ public class SubtypeSwitcher {
if (token == null) { if (token == null) {
return; return;
} }
final InputMethodManagerCompatWrapper imm = mImm;
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
mImm.setInputMethodAndSubtype(token, imiId, subtype); imm.setInputMethodAndSubtype(token, imiId, subtype);
return null; return null;
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -385,22 +383,6 @@ public class SubtypeSwitcher {
return mIsDictionaryAvailable; 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() { public InputMethodSubtype getCurrentSubtype() {
return mCurrentSubtype; return mCurrentSubtype;
} }

View File

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

View File

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