下面列出了怎么用android.view.inputmethod.InputMethodInfo的API类实例代码及写法,或者点击链接到github查看源代码。
protected ComponentName retrieveActiveInputMethod() {
if (null == mContext) return null;
final String id = Settings.Secure.getString(
mContext.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD
);
if (TextUtils.isEmpty(id)) return null;
List<InputMethodInfo> mInputMethodProperties = mInputMethodManager.getEnabledInputMethodList();
for (InputMethodInfo mInputMethod : mInputMethodProperties) {
if (id.equals(mInputMethod.getId())) {
return mInputMethod.getComponent();
}
}
return null;
}
public boolean isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes() {
final Locale systemLocale = mContext.getResources().getConfiguration().locale;
final Set<InputMethodSubtype> enabledSubtypesOfEnabledImes = new HashSet<>();
final InputMethodManager inputMethodManager = getInputMethodManager();
final List<InputMethodInfo> enabledInputMethodInfoList =
inputMethodManager.getEnabledInputMethodList();
for (final InputMethodInfo info : enabledInputMethodInfoList) {
final List<InputMethodSubtype> enabledSubtypes =
inputMethodManager.getEnabledInputMethodSubtypeList(
info, true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// An IME with no subtypes is found.
return false;
}
enabledSubtypesOfEnabledImes.addAll(enabledSubtypes);
}
for (final InputMethodSubtype subtype : enabledSubtypesOfEnabledImes) {
if (!subtype.isAuxiliary() && !subtype.getLocale().isEmpty()
&& !systemLocale.equals(SubtypeLocaleUtils.getSubtypeLocale(subtype))) {
return false;
}
}
return true;
}
public SubtypeLocaleAdapter(final Context context) {
super(context, android.R.layout.simple_spinner_item);
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final TreeSet<SubtypeLocaleItem> items = new TreeSet<>();
final InputMethodInfo imi = RichInputMethodManager.getInstance()
.getInputMethodInfoOfThisIme();
final int count = imi.getSubtypeCount();
for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = imi.getSubtypeAt(i);
if (DEBUG_SUBTYPE_ID) {
Log.d(TAG_SUBTYPE, String.format("%-6s 0x%08x %11d %s",
subtype.getLocale(), subtype.hashCode(), subtype.hashCode(),
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)));
}
if (subtype.isAsciiCapable()) {
items.add(new SubtypeLocaleItem(subtype));
}
}
// TODO: Should filter out already existing combinations of locale and layout.
addAll(items);
}
public SubtypeLocaleAdapter(final Context context) {
super(context, android.R.layout.simple_spinner_item);
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final TreeSet<SubtypeLocaleItem> items = new TreeSet<>();
final InputMethodInfo imi = RichInputMethodManager.getInstance()
.getInputMethodInfoOfThisIme();
final int count = imi.getSubtypeCount();
for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = imi.getSubtypeAt(i);
if (DEBUG_SUBTYPE_ID) {
Log.d(TAG_SUBTYPE, String.format("%-6s 0x%08x %11d %s",
subtype.getLocale(), subtype.hashCode(), subtype.hashCode(),
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)));
}
if (InputMethodSubtypeCompatUtils.isAsciiCapable(subtype)) {
items.add(new SubtypeLocaleItem(subtype));
}
}
// TODO: Should filter out already existing combinations of locale and layout.
addAll(items);
}
private static String getEnabledSubtypesLabel(
Context context, InputMethodManager imm, InputMethodInfo imi) {
if (context == null || imm == null || imi == null) return null;
final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
final StringBuilder sb = new StringBuilder();
final int N = subtypes.size();
for (int i = 0; i < N; ++i) {
final InputMethodSubtype subtype = subtypes.get(i);
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(subtype.getDisplayName(context, imi.getPackageName(),
imi.getServiceInfo().applicationInfo));
}
return sb.toString();
}
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final List<InputMethodInfo> enabledImis = mImmService.getEnabledInputMethodList();
final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
+ getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi,
true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// The next IME has no subtype.
mImmService.setInputMethod(token, nextImi.getId());
return true;
}
final InputMethodSubtype firstSubtype = enabledSubtypes.get(0);
mImmService.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype);
return true;
}
public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
if (methodMap == null) {
throw new NullPointerException("methodMap is null");
}
mMethodMap = methodMap;
final File systemDir = userId == UserHandle.USER_OWNER
? new File(Environment.getDataDirectory(), SYSTEM_PATH)
: Environment.getUserSystemDirectory(userId);
final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
if (!inputMethodDir.mkdirs()) {
Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
}
final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
if (!subtypeFile.exists()) {
// If "subtypes.xml" doesn't exist, create a blank file.
writeAdditionalInputMethodSubtypes(
mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
} else {
readAdditionalInputMethodSubtypes(
mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
}
}
private Set<String> getSystemInputMethods() {
// InputMethodManager is final so it cannot be mocked.
// So, we're using IInputMethodManager directly because it can be mocked.
List<InputMethodInfo> inputMethods = null;
try {
inputMethods = mIInputMethodManager.getInputMethodList();
} catch (RemoteException e) {
ProvisionLogger.loge("Could not communicate with IInputMethodManager", e);
return Collections.<String>emptySet();
}
Set<String> systemInputMethods = new HashSet<String>();
for (InputMethodInfo inputMethodInfo : inputMethods) {
ApplicationInfo applicationInfo = inputMethodInfo.getServiceInfo().applicationInfo;
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
systemInputMethods.add(inputMethodInfo.getPackageName());
}
}
return systemInputMethods;
}
private static String getEnabledSubtypesLabel(
Context context, InputMethodManager imm, InputMethodInfo imi) {
if (context == null || imm == null || imi == null) return null;
final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
final StringBuilder sb = new StringBuilder();
final int N = subtypes.size();
for (int i = 0; i < N; ++i) {
final InputMethodSubtype subtype = subtypes.get(i);
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(subtype.getDisplayName(context, imi.getPackageName(),
imi.getServiceInfo().applicationInfo));
}
return sb.toString();
}
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final List<InputMethodInfo> enabledImis = mImmService.getEnabledInputMethodList();
final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
+ getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi,
true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// The next IME has no subtype.
mImmService.setInputMethod(token, nextImi.getId());
return true;
}
final InputMethodSubtype firstSubtype = enabledSubtypes.get(0);
mImmService.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype);
return true;
}
/**
* Gets the set of locales supported by the current enabled Input Methods.
* @param context A {@link Context} instance.
* @return A possibly-empty {@link Set} of locale strings.
*/
public static Set<String> getIMELocales(Context context) {
LinkedHashSet<String> locales = new LinkedHashSet<String>();
InputMethodManager imManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> enabledMethods = imManager.getEnabledInputMethodList();
for (int i = 0; i < enabledMethods.size(); i++) {
List<InputMethodSubtype> subtypes =
imManager.getEnabledInputMethodSubtypeList(enabledMethods.get(i), true);
if (subtypes == null) continue;
for (int j = 0; j < subtypes.size(); j++) {
String locale = ApiCompatibilityUtils.getLocale(subtypes.get(j));
if (!TextUtils.isEmpty(locale)) locales.add(locale);
}
}
return locales;
}
public SubtypeLocaleAdapter(final Context context) {
super(context, android.R.layout.simple_spinner_item);
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final TreeSet<SubtypeLocaleItem> items = new TreeSet<>();
final InputMethodInfo imi = RichInputMethodManager.getInstance()
.getInputMethodInfoOfThisIme();
final int count = imi.getSubtypeCount();
for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = imi.getSubtypeAt(i);
if (DEBUG_SUBTYPE_ID) {
Log.d(TAG_SUBTYPE, String.format("%-6s 0x%08x %11d %s",
subtype.getLocale(), subtype.hashCode(), subtype.hashCode(),
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype)));
}
if (subtype.isAsciiCapable()) {
items.add(new SubtypeLocaleItem(subtype));
}
}
// TODO: Should filter out already existing combinations of locale and layout.
addAll(items);
}
private static String getEnabledSubtypesLabel(
Context context, InputMethodManager imm, InputMethodInfo imi) {
if (context == null || imm == null || imi == null) return null;
final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
final StringBuilder sb = new StringBuilder();
final int N = subtypes.size();
for (int i = 0; i < N; ++i) {
final InputMethodSubtype subtype = subtypes.get(i);
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(subtype.getDisplayName(context, imi.getPackageName(),
imi.getServiceInfo().applicationInfo));
}
return sb.toString();
}
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final InputMethodManager imm = mImmWrapper.mImm;
final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
+ getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
final List<InputMethodSubtype> enabledSubtypes = getEnabledInputMethodSubtypeList(nextImi,
true /* allowsImplicitlySelectedSubtypes */);
if (enabledSubtypes.isEmpty()) {
// The next IME has no subtype.
imm.setInputMethod(token, nextImi.getId());
return true;
}
final InputMethodSubtype firstSubtype = enabledSubtypes.get(0);
imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype);
return true;
}
private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
InputMethodInfo imi = mMethodMap.get(newDefaultIme);
int lastSubtypeId = NOT_A_SUBTYPE_ID;
// newDefaultIme is empty when there is no candidate for the selected IME.
if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
if (subtypeHashCode != null) {
try {
lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
imi, Integer.valueOf(subtypeHashCode));
} catch (NumberFormatException e) {
Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
}
}
}
setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
}
private static String getEnabledSubtypesLabel(
Context context, InputMethodManager imm, InputMethodInfo imi) {
if (context == null || imm == null || imi == null) return null;
final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true);
final StringBuilder sb = new StringBuilder();
final int N = subtypes.size();
for (int i = 0; i < N; ++i) {
final InputMethodSubtype subtype = subtypes.get(i);
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(subtype.getDisplayName(context, imi.getPackageName(),
imi.getServiceInfo().applicationInfo));
}
return sb.toString();
}
private static int getImiIndexInList(final InputMethodInfo inputMethodInfo,
final List<InputMethodInfo> imiList) {
final int count = imiList.size();
for (int index = 0; index < count; index++) {
final InputMethodInfo imi = imiList.get(index);
if (imi.equals(inputMethodInfo)) {
return index;
}
}
return INDEX_NOT_FOUND;
}
private static InputMethodInfo getNextNonAuxiliaryIme(final int currentIndex,
final List<InputMethodInfo> imiList) {
final int count = imiList.size();
for (int i = 1; i < count; i++) {
final int nextIndex = (currentIndex + i) % count;
final InputMethodInfo nextImi = imiList.get(nextIndex);
if (!isAuxiliaryIme(nextImi)) {
return nextImi;
}
}
return imiList.get(currentIndex);
}
public synchronized InputMethodInfo getInputMethodOfThisIme() {
if (mCachedThisImeInfo != null) {
return mCachedThisImeInfo;
}
for (final InputMethodInfo imi : mImm.getInputMethodList()) {
if (imi.getPackageName().equals(mImePackageName)) {
mCachedThisImeInfo = imi;
return imi;
}
}
throw new RuntimeException("Input method id for " + mImePackageName + " not found.");
}
public synchronized InputMethodInfo getInputMethodOfThisIme() {
if (mCachedThisImeInfo != null) {
return mCachedThisImeInfo;
}
for (final InputMethodInfo imi : mImm.getInputMethodList()) {
if (imi.getPackageName().equals(mImePackageName)) {
mCachedThisImeInfo = imi;
return imi;
}
}
throw new RuntimeException("Input method id for " + mImePackageName + " not found.");
}
public synchronized List<InputMethodSubtype> getEnabledInputMethodSubtypeList(
final InputMethodInfo imi, final boolean allowsImplicitlySelectedSubtypes) {
final HashMap<InputMethodInfo, List<InputMethodSubtype>> cache =
allowsImplicitlySelectedSubtypes
? mCachedSubtypeListWithImplicitlySelected
: mCachedSubtypeListOnlyExplicitlySelected;
final List<InputMethodSubtype> cachedList = cache.get(imi);
if (cachedList != null) {
return cachedList;
}
final List<InputMethodSubtype> result = mImm.getEnabledInputMethodSubtypeList(
imi, allowsImplicitlySelectedSubtypes);
cache.put(imi, result);
return result;
}
public InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(final String localeString,
final String keyboardLayoutSetName) {
final InputMethodInfo myImi = getInputMethodInfoOfThisIme();
final int count = myImi.getSubtypeCount();
for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = myImi.getSubtypeAt(i);
final String layoutName = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype);
if (localeString.equals(subtype.getLocale())
&& keyboardLayoutSetName.equals(layoutName)) {
return subtype;
}
}
return null;
}
void invokeSubtypeEnablerOfThisIme() {
final InputMethodInfo imi =
UncachedInputMethodManagerUtils.getInputMethodInfoOf(getPackageName(), mImm);
if (imi == null) {
return;
}
final Intent intent = new Intent();
intent.setAction(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, imi.getId());
startActivity(intent);
}
/**
* Check if the IME specified by the context is enabled.
* CAVEAT: This may cause a round trip IPC.
*
* @param context package context of the IME to be checked.
* @param imm the {@link InputMethodManager}.
* @return true if this IME is enabled.
*/
public static boolean isThisImeEnabled(final Context context,
final InputMethodManager imm) {
final String packageName = context.getPackageName();
for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
if (packageName.equals(imi.getPackageName())) {
return true;
}
}
return false;
}
@Override
protected void setUp() throws Exception {
super.setUp();
final Context context = getContext();
final Resources res = context.getResources();
RichInputMethodManager.init(context);
mRichImm = RichInputMethodManager.getInstance();
// Save and reset additional subtypes preference.
mSavedAdditionalSubtypes = mRichImm.getAdditionalSubtypes();
final InputMethodSubtype[] predefinedAdditionalSubtypes =
AdditionalSubtypeUtils.createAdditionalSubtypesArray(
AdditionalSubtypeUtils.createPrefSubtypes(
res.getStringArray(R.array.predefined_subtypes)));
mRichImm.setAdditionalInputMethodSubtypes(predefinedAdditionalSubtypes);
final KeyboardTheme keyboardTheme = KeyboardTheme.searchKeyboardThemeById(
getKeyboardThemeForTests(), KeyboardTheme.KEYBOARD_THEMES);
setContext(new ContextThemeWrapper(getContext(), keyboardTheme.mStyleId));
KeyboardLayoutSet.onKeyboardThemeChanged();
mScreenMetrics = Settings.readScreenMetrics(res);
final InputMethodInfo imi = mRichImm.getInputMethodInfoOfThisIme();
final int subtypeCount = imi.getSubtypeCount();
for (int index = 0; index < subtypeCount; index++) {
mAllSubtypesList.add(imi.getSubtypeAt(index));
}
}
private static boolean isAuxiliaryIme(final InputMethodInfo imi) {
final int count = imi.getSubtypeCount();
if (count == 0) {
return false;
}
for (int index = 0; index < count; index++) {
final InputMethodSubtype subtype = imi.getSubtypeAt(index);
if (!subtype.isAuxiliary()) {
return false;
}
}
return true;
}
@Override
public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
if (!isChangingPackagesOfCurrentUser()) {
return false;
}
synchronized (mMethodMap) {
String curInputMethodId = mSettings.getSelectedInputMethod();
final int N = mMethodList.size();
if (curInputMethodId != null) {
for (int i=0; i<N; i++) {
InputMethodInfo imi = mMethodList.get(i);
if (imi.getId().equals(curInputMethodId)) {
for (String pkg : packages) {
if (imi.getPackageName().equals(pkg)) {
if (!doit) {
return true;
}
resetSelectedInputMethodAndSubtypeLocked("");
chooseNewDefaultIMELocked();
return true;
}
}
}
}
}
}
return false;
}
public synchronized boolean isInputMethodOfThisImeEnabled() {
for (final InputMethodInfo imi : mImm.getEnabledInputMethodList()) {
if (imi.getPackageName().equals(mImePackageName)) {
return true;
}
}
return false;
}
public synchronized InputMethodInfo getInputMethodOfThisIme() {
if (mCachedThisImeInfo != null) {
return mCachedThisImeInfo;
}
for (final InputMethodInfo imi : mImm.getInputMethodList()) {
if (imi.getPackageName().equals(mImePackageName)) {
mCachedThisImeInfo = imi;
return imi;
}
}
throw new RuntimeException("Input method id for " + mImePackageName + " not found.");
}
public synchronized List<InputMethodSubtype> getEnabledInputMethodSubtypeList(
final InputMethodInfo imi, final boolean allowsImplicitlySelectedSubtypes) {
final HashMap<InputMethodInfo, List<InputMethodSubtype>> cache =
allowsImplicitlySelectedSubtypes
? mCachedSubtypeListWithImplicitlySelected
: mCachedSubtypeListOnlyExplicitlySelected;
final List<InputMethodSubtype> cachedList = cache.get(imi);
if (cachedList != null) {
return cachedList;
}
final List<InputMethodSubtype> result = mImm.getEnabledInputMethodSubtypeList(
imi, allowsImplicitlySelectedSubtypes);
cache.put(imi, result);
return result;
}