Cleanup InputMethodManagerCompatWrapper

Change-Id: Id3b84ee19bb504ed8fbb398e260cc663a5b5ae0d
This commit is contained in:
Tadashi G. Takaoka 2012-04-18 17:39:57 +09:00
parent feea31e9ce
commit 55d28fd1b2
7 changed files with 79 additions and 104 deletions

View File

@ -18,14 +18,12 @@ package com.android.inputmethod.compat;
import android.content.Context; import android.content.Context;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
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.latin.ImfUtils;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
// TODO: Override this class with the concrete implementation if we need to take care of the // TODO: Override this class with the concrete implementation if we need to take care of the
// performance. // performance.
@ -44,41 +42,20 @@ public class InputMethodManagerCompatWrapper {
} }
public static InputMethodManagerCompatWrapper getInstance() { public static InputMethodManagerCompatWrapper getInstance() {
if (sInstance.mImm == null) if (sInstance.mImm == null) {
Log.w(TAG, "getInstance() is called before initialization"); throw new RuntimeException(TAG + ".getInstance() is called before initialization");
}
return sInstance; return sInstance;
} }
public static void init(Context context) { public static void init(Context context) {
sInstance.mImm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); sInstance.mImm = ImfUtils.getInputMethodManager(context);
}
public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
mImm.setAdditionalInputMethodSubtypes(imiId, subtypes);
}
public InputMethodSubtype getCurrentInputMethodSubtype() {
return mImm.getCurrentInputMethodSubtype();
} }
public InputMethodSubtype getLastInputMethodSubtype() { public InputMethodSubtype getLastInputMethodSubtype() {
return mImm.getLastInputMethodSubtype(); return mImm.getLastInputMethodSubtype();
} }
public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(
InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes) {
return mImm.getEnabledInputMethodSubtypeList(imi, allowsImplicitlySelectedSubtypes);
}
public Map<InputMethodInfo, List<InputMethodSubtype>> getShortcutInputMethodsAndSubtypes() {
return mImm.getShortcutInputMethodsAndSubtypes();
}
// We don't call this method when we switch between subtypes within this IME.
public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
mImm.setInputMethodAndSubtype(token, id, subtype);
}
public boolean switchToLastInputMethod(IBinder token) { public boolean switchToLastInputMethod(IBinder token) {
return mImm.switchToLastInputMethod(token); return mImm.switchToLastInputMethod(token);
} }
@ -88,18 +65,7 @@ public class InputMethodManagerCompatWrapper {
onlyCurrentIme); 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();
}
public void showInputMethodPicker() { public void showInputMethodPicker() {
if (mImm == null) return;
mImm.showInputMethodPicker(); mImm.showInputMethodPicker();
} }
} }

View File

@ -42,13 +42,13 @@ import com.android.inputmethod.accessibility.AccessibilityUtils;
import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy; import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy;
import com.android.inputmethod.keyboard.PointerTracker.TimerProxy; import com.android.inputmethod.keyboard.PointerTracker.TimerProxy;
import com.android.inputmethod.latin.ImfUtils;
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.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResearchLogger; import com.android.inputmethod.latin.ResearchLogger;
import com.android.inputmethod.latin.StaticInnerHandlerWrapper; import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
import com.android.inputmethod.latin.SubtypeLocale; import com.android.inputmethod.latin.SubtypeLocale;
import com.android.inputmethod.latin.SubtypeUtils;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.Utils.UsabilityStudyLogUtils; import com.android.inputmethod.latin.Utils.UsabilityStudyLogUtils;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
@ -877,13 +877,14 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
// Whether space key needs to show the "..." popup hint for special purposes // Whether space key needs to show the "..." popup hint for special purposes
if (mIsSpacebarTriggeringPopupByLongPress if (mIsSpacebarTriggeringPopupByLongPress
&& SubtypeUtils.hasMultipleEnabledIMEsOrSubtypes( && ImfUtils.hasMultipleEnabledIMEsOrSubtypes(
true /* include aux subtypes */)) { getContext(), true /* include aux subtypes */)) {
drawKeyPopupHint(key, canvas, paint, params); drawKeyPopupHint(key, canvas, paint, params);
} }
} else if (key.mCode == Keyboard.CODE_LANGUAGE_SWITCH) { } else if (key.mCode == Keyboard.CODE_LANGUAGE_SWITCH) {
super.onDrawKeyTopVisuals(key, canvas, paint, params); super.onDrawKeyTopVisuals(key, canvas, paint, params);
if (SubtypeUtils.hasMultipleEnabledIMEsOrSubtypes(true /* include aux subtypes */)) { if (ImfUtils.hasMultipleEnabledIMEsOrSubtypes(
getContext(), true /* include aux subtypes */)) {
drawKeyPopupHint(key, canvas, paint, params); drawKeyPopupHint(key, canvas, paint, params);
} }
} else { } else {

View File

@ -18,24 +18,52 @@ package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class SubtypeUtils { /**
private SubtypeUtils() { * Utility class for Input Method Framework
*/
public class ImfUtils {
private ImfUtils() {
// This utility class is not publicly instantiable. // This utility class is not publicly instantiable.
} }
// TODO: Cache my InputMethodInfo and/or InputMethodSubtype list. private static InputMethodManager sInputMethodManager;
public static boolean checkIfSubtypeBelongsToThisIme(Context context, InputMethodSubtype ims) {
final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance();
if (imm == null) return false;
final InputMethodInfo myImi = getInputMethodInfo(context.getPackageName()); public static InputMethodManager getInputMethodManager(Context context) {
if (sInputMethodManager == null) {
sInputMethodManager = (InputMethodManager)context.getSystemService(
Context.INPUT_METHOD_SERVICE);
}
return sInputMethodManager;
}
private static InputMethodInfo sInputMethodInfoOfThisIme;
public static InputMethodInfo getInputMethodInfoOfThisIme(Context context) {
if (sInputMethodInfoOfThisIme == null) {
final InputMethodManager imm = getInputMethodManager(context);
final String packageName = context.getPackageName();
for (final InputMethodInfo imi : imm.getInputMethodList()) {
if (imi.getPackageName().equals(packageName))
return imi;
}
throw new RuntimeException("Can not find input method id for " + packageName);
}
return sInputMethodInfoOfThisIme;
}
public static String getInputMethodIdOfThisIme(Context context) {
return getInputMethodInfoOfThisIme(context).getId();
}
public static boolean checkIfSubtypeBelongsToThisIme(Context context, InputMethodSubtype ims) {
final InputMethodInfo myImi = getInputMethodInfoOfThisIme(context);
final InputMethodManager imm = getInputMethodManager(context);
final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(myImi, true); final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(myImi, true);
for (final InputMethodSubtype subtype : subtypes) { for (final InputMethodSubtype subtype : subtypes) {
if (subtype.equals(ims)) { if (subtype.equals(ims)) {
@ -45,26 +73,23 @@ public class SubtypeUtils {
return false; return false;
} }
public static boolean hasMultipleEnabledIMEsOrSubtypes( public static boolean hasMultipleEnabledIMEsOrSubtypes(Context context,
final boolean shouldIncludeAuxiliarySubtypes) { final boolean shouldIncludeAuxiliarySubtypes) {
final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); final InputMethodManager imm = getInputMethodManager(context);
if (imm == null) return false;
final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, enabledImis); return hasMultipleEnabledSubtypes(context, shouldIncludeAuxiliarySubtypes, enabledImis);
} }
public static boolean hasMultipleEnabledSubtypesInThisIme(Context context, public static boolean hasMultipleEnabledSubtypesInThisIme(Context context,
final boolean shouldIncludeAuxiliarySubtypes) { final boolean shouldIncludeAuxiliarySubtypes) {
final InputMethodInfo myImi = getInputMethodInfo(context.getPackageName()); final InputMethodInfo myImi = getInputMethodInfoOfThisIme(context);
final List<InputMethodInfo> imiList = Collections.singletonList(myImi); final List<InputMethodInfo> imiList = Collections.singletonList(myImi);
return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, imiList); return hasMultipleEnabledSubtypes(context, shouldIncludeAuxiliarySubtypes, imiList);
} }
private static boolean hasMultipleEnabledSubtypes(final boolean shouldIncludeAuxiliarySubtypes, private static boolean hasMultipleEnabledSubtypes(Context context,
List<InputMethodInfo> imiList) { final boolean shouldIncludeAuxiliarySubtypes, List<InputMethodInfo> imiList) {
final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); final InputMethodManager imm = getInputMethodManager(context);
if (imm == null) return false;
// Number of the filtered IMEs // Number of the filtered IMEs
int filteredImisCount = 0; int filteredImisCount = 0;
@ -113,26 +138,9 @@ public class SubtypeUtils {
return keyboardCount > 1; return keyboardCount > 1;
} }
public static String getInputMethodId(String packageName) {
return getInputMethodInfo(packageName).getId();
}
public static InputMethodInfo getInputMethodInfo(String packageName) {
final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance();
if (imm == null) {
throw new RuntimeException("Input method manager not found");
}
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 findSubtypeByLocaleAndKeyboardLayoutSet( public static InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(
Context context, String localeString, String keyboardLayoutSetName) { Context context, String localeString, String keyboardLayoutSetName) {
final InputMethodInfo imi = getInputMethodInfo(context.getPackageName()); final InputMethodInfo imi = getInputMethodInfoOfThisIme(context);
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);
@ -148,11 +156,8 @@ public class SubtypeUtils {
public static void setAdditionalInputMethodSubtypes(Context context, public static void setAdditionalInputMethodSubtypes(Context context,
InputMethodSubtype[] subtypes) { InputMethodSubtype[] subtypes) {
final InputMethodManagerCompatWrapper imm = InputMethodManagerCompatWrapper.getInstance(); final InputMethodManager imm = getInputMethodManager(context);
if (imm == null) { final String imiId = getInputMethodIdOfThisIme(context);
throw new RuntimeException("Input method manager not found");
}
final String imiId = getInputMethodId(context.getPackageName());
imm.setAdditionalInputMethodSubtypes(imiId, subtypes); imm.setAdditionalInputMethodSubtypes(imiId, subtypes);
} }
} }

View File

@ -441,7 +441,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
loadSettings(); loadSettings();
SubtypeUtils.setAdditionalInputMethodSubtypes( ImfUtils.setAdditionalInputMethodSubtypes(
this, mSettingsValues.getPrefefinedAdditionalSubtypes()); this, mSettingsValues.getPrefefinedAdditionalSubtypes());
// TODO: remove the following when it's not needed by updateCorrectionMode() any more // TODO: remove the following when it's not needed by updateCorrectionMode() any more
@ -1134,7 +1134,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (isShowingOptionDialog()) return false; if (isShowingOptionDialog()) return false;
switch (requestCode) { switch (requestCode) {
case CODE_SHOW_INPUT_METHOD_PICKER: case CODE_SHOW_INPUT_METHOD_PICKER:
if (SubtypeUtils.hasMultipleEnabledIMEsOrSubtypes(true /* include aux subtypes */)) { if (ImfUtils.hasMultipleEnabledIMEsOrSubtypes(
this, true /* include aux subtypes */)) {
mImm.showInputMethodPicker(); mImm.showInputMethodPicker();
return true; return true;
} }
@ -1169,7 +1170,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final IBinder token = getWindow().getWindow().getAttributes().token; final IBinder token = getWindow().getWindow().getAttributes().token;
if (mShouldSwitchToLastSubtype) { if (mShouldSwitchToLastSubtype) {
final InputMethodSubtype lastSubtype = mImm.getLastInputMethodSubtype(); final InputMethodSubtype lastSubtype = mImm.getLastInputMethodSubtype();
final boolean lastSubtypeBelongsToThisIme = SubtypeUtils.checkIfSubtypeBelongsToThisIme( final boolean lastSubtypeBelongsToThisIme = ImfUtils.checkIfSubtypeBelongsToThisIme(
this, lastSubtype); this, lastSubtype);
if ((includesOtherImes || lastSubtypeBelongsToThisIme) if ((includesOtherImes || lastSubtypeBelongsToThisIme)
&& mImm.switchToLastInputMethod(token)) { && mImm.switchToLastInputMethod(token)) {
@ -2299,6 +2300,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
getString(R.string.language_selection_title), getString(R.string.language_selection_title),
getString(R.string.english_ime_settings), getString(R.string.english_ime_settings),
}; };
final Context context = this;
final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface di, int position) { public void onClick(DialogInterface di, int position) {
@ -2306,7 +2308,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
switch (position) { switch (position) {
case 0: case 0:
Intent intent = CompatUtils.getInputLanguageSelectionIntent( Intent intent = CompatUtils.getInputLanguageSelectionIntent(
SubtypeUtils.getInputMethodId(getPackageName()), ImfUtils.getInputMethodIdOfThisIme(context),
Intent.FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
| Intent.FLAG_ACTIVITY_CLEAR_TOP); | Intent.FLAG_ACTIVITY_CLEAR_TOP);

View File

@ -306,9 +306,10 @@ public class SettingsValues {
return false; return false;
} }
if (mIncludesOtherImesInLanguageSwitchList) { if (mIncludesOtherImesInLanguageSwitchList) {
return SubtypeUtils.hasMultipleEnabledIMEsOrSubtypes(/* include aux subtypes */false); return ImfUtils.hasMultipleEnabledIMEsOrSubtypes(
context, /* include aux subtypes */false);
} else { } else {
return SubtypeUtils.hasMultipleEnabledSubtypesInThisIme( return ImfUtils.hasMultipleEnabledSubtypesInThisIme(
context, /* include aux subtypes */false); context, /* include aux subtypes */false);
} }
} }

View File

@ -28,9 +28,9 @@ import android.os.IBinder;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,7 +52,7 @@ public class SubtypeSwitcher {
private static final SubtypeSwitcher sInstance = new SubtypeSwitcher(); private static final SubtypeSwitcher sInstance = new SubtypeSwitcher();
private /* final */ LatinIME mService; private /* final */ LatinIME mService;
private /* final */ InputMethodManagerCompatWrapper mImm; private /* final */ InputMethodManager mImm;
private /* final */ Resources mResources; private /* final */ Resources mResources;
private /* final */ ConnectivityManager mConnectivityManager; private /* final */ ConnectivityManager mConnectivityManager;
private final ArrayList<InputMethodSubtype> mEnabledKeyboardSubtypesOfCurrentInputMethod = private final ArrayList<InputMethodSubtype> mEnabledKeyboardSubtypesOfCurrentInputMethod =
@ -94,7 +94,7 @@ public class SubtypeSwitcher {
private void initialize(LatinIME service) { private void initialize(LatinIME service) {
mService = service; mService = service;
mResources = service.getResources(); mResources = service.getResources();
mImm = InputMethodManagerCompatWrapper.getInstance(); mImm = ImfUtils.getInputMethodManager(service);
mConnectivityManager = (ConnectivityManager) service.getSystemService( mConnectivityManager = (ConnectivityManager) service.getSystemService(
Context.CONNECTIVITY_SERVICE); Context.CONNECTIVITY_SERVICE);
mEnabledKeyboardSubtypesOfCurrentInputMethod.clear(); mEnabledKeyboardSubtypesOfCurrentInputMethod.clear();
@ -104,7 +104,7 @@ public class SubtypeSwitcher {
mInputLocaleStr = null; mInputLocaleStr = null;
mCurrentSubtype = mImm.getCurrentInputMethodSubtype(); mCurrentSubtype = mImm.getCurrentInputMethodSubtype();
mAllEnabledSubtypesOfCurrentInputMethod = null; mAllEnabledSubtypesOfCurrentInputMethod = null;
mNoLanguageSubtype = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( mNoLanguageSubtype = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
service, SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.QWERTY); service, SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.QWERTY);
final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
@ -277,7 +277,7 @@ public class SubtypeSwitcher {
if (token == null) { if (token == null) {
return; return;
} }
final InputMethodManagerCompatWrapper imm = mImm; final InputMethodManager imm = mImm;
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {

View File

@ -114,17 +114,17 @@ public class SubtypeLocaleTests extends AndroidTestCase {
public void testSampleSubtypes() { public void testSampleSubtypes() {
final Context context = getContext(); final Context context = getContext();
final InputMethodSubtype EN_US = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype EN_US = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.US.toString(), AdditionalSubtype.QWERTY); context, Locale.US.toString(), AdditionalSubtype.QWERTY);
final InputMethodSubtype EN_GB = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype EN_GB = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.UK.toString(), AdditionalSubtype.QWERTY); context, Locale.UK.toString(), AdditionalSubtype.QWERTY);
final InputMethodSubtype FR = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype FR = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.FRENCH.toString(), AdditionalSubtype.AZERTY); context, Locale.FRENCH.toString(), AdditionalSubtype.AZERTY);
final InputMethodSubtype FR_CA = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype FR_CA = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.CANADA_FRENCH.toString(), AdditionalSubtype.QWERTY); context, Locale.CANADA_FRENCH.toString(), AdditionalSubtype.QWERTY);
final InputMethodSubtype DE = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype DE = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, Locale.GERMAN.toString(), AdditionalSubtype.QWERTZ); context, Locale.GERMAN.toString(), AdditionalSubtype.QWERTZ);
final InputMethodSubtype ZZ = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet( final InputMethodSubtype ZZ = ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
context, SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.QWERTY); context, SubtypeLocale.NO_LANGUAGE, AdditionalSubtype.QWERTY);
assertFalse(AdditionalSubtype.isAdditionalSubtype(EN_US)); assertFalse(AdditionalSubtype.isAdditionalSubtype(EN_US));