private static boolean hasMultipleEnabledSubtypes( Context context, final boolean shouldIncludeAuxiliarySubtypes, List<InputMethodInfo> imiList) { final InputMethodManager imm = getInputMethodManager(context); // Number of the filtered IMEs int filteredImisCount = 0; for (InputMethodInfo imi : imiList) { // We can return true immediately after we find two or more filtered IMEs. if (filteredImisCount > 1) return true; final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true); // IMEs that have no subtypes should be counted. if (subtypes.isEmpty()) { ++filteredImisCount; continue; } int auxCount = 0; for (InputMethodSubtype subtype : subtypes) { if (subtype.isAuxiliary()) { ++auxCount; } } final int nonAuxCount = subtypes.size() - auxCount; // IMEs that have one or more non-auxiliary subtypes should be counted. // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary // subtypes should be counted as well. if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { ++filteredImisCount; continue; } } if (filteredImisCount > 1) { return true; } final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(null, true); int keyboardCount = 0; // imm.getEnabledInputMethodSubtypeList(null, true) will return the current IME's // both explicitly and implicitly enabled input method subtype. // (The current IME should be LatinIME.) for (InputMethodSubtype subtype : subtypes) { if (KEYBOARD_MODE.equals(subtype.getMode())) { ++keyboardCount; } } return keyboardCount > 1; }
@SuppressWarnings("deprecation") // InputMethodSubtype.getLocale() deprecated in API 24 private void recordKeyboardLocaleUma() { InputMethodManager imm = (InputMethodManager) mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE); List<InputMethodInfo> ims = imm.getEnabledInputMethodList(); ArrayList<String> uniqueLanguages = new ArrayList<>(); for (InputMethodInfo method : ims) { List<InputMethodSubtype> submethods = imm.getEnabledInputMethodSubtypeList(method, true); for (InputMethodSubtype submethod : submethods) { if (submethod.getMode().equals("keyboard")) { String language = submethod.getLocale().split("_")[0]; if (!uniqueLanguages.contains(language)) { uniqueLanguages.add(language); } } } } RecordHistogram.recordCountHistogram("InputMethod.ActiveCount", uniqueLanguages.size()); InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype(); Locale systemLocale = Locale.getDefault(); if (currentSubtype != null && currentSubtype.getLocale() != null && systemLocale != null) { String keyboardLanguage = currentSubtype.getLocale().split("_")[0]; boolean match = systemLocale.getLanguage().equalsIgnoreCase(keyboardLanguage); RecordHistogram.recordBooleanHistogram("InputMethod.MatchesSystemLanguage", match); } }
/** * Method adapted from com.android.inputmethod.latin.Utils * * @param imm The input method manager * @param shouldIncludeAuxiliarySubtypes * @return true if we have multiple IMEs to choose from */ private boolean hasMultipleEnabledIMEsOrSubtypes( InputMethodManager imm, final boolean shouldIncludeAuxiliarySubtypes) { final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); // Number of the filtered IMEs int filteredImisCount = 0; for (InputMethodInfo imi : enabledImis) { // We can return true immediately after we find two or more filtered IMEs. if (filteredImisCount > 1) return true; final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(imi, true); // IMEs that have no subtypes should be counted. if (subtypes.isEmpty()) { ++filteredImisCount; continue; } int auxCount = 0; for (InputMethodSubtype subtype : subtypes) { if (subtype.isAuxiliary()) { ++auxCount; } } final int nonAuxCount = subtypes.size() - auxCount; // IMEs that have one or more non-auxiliary subtypes should be counted. // If shouldIncludeAuxiliarySubtypes is true, IMEs that have two or more auxiliary // subtypes should be counted as well. if (nonAuxCount > 0 || (shouldIncludeAuxiliarySubtypes && auxCount > 1)) { ++filteredImisCount; continue; } } return filteredImisCount > 1 // imm.getEnabledInputMethodSubtypeList(null, false) will return the current IME's enabled // input method subtype (The current IME should be LatinIME.) || imm.getEnabledInputMethodSubtypeList(null, false).size() > 1; }
public static boolean checkIfSubtypeBelongsToThisImeAndEnabled( Context context, InputMethodSubtype ims) { final InputMethodInfo myImi = getInputMethodInfoOfThisIme(context); final InputMethodManager imm = getInputMethodManager(context); // TODO: Cache all subtypes of this IME for optimization final List<InputMethodSubtype> subtypes = imm.getEnabledInputMethodSubtypeList(myImi, true); for (final InputMethodSubtype subtype : subtypes) { if (subtype.equals(ims)) { return true; } } return 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 TreeMap<InputMethodInfo, List<InputMethodSubtype>> getEnabledInputMethodAndSubtypeList() { String newEnabledIMIs = Settings.Secure.getString( mContext.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS); String currentSystemLocaleString = mContext.getResources().getConfiguration().locale.toString(); if (!TextUtils.equals(mEnabledInputMethodAndSubtypesCacheStr, newEnabledIMIs) || !TextUtils.equals(mLastSystemLocaleString, currentSystemLocaleString) || mPackageChanged) { mEnabledInputMethodAndSubtypesCache.clear(); final List<InputMethodInfo> imis = mImm.getEnabledInputMethodList(); for (InputMethodInfo imi : imis) { mEnabledInputMethodAndSubtypesCache.put( imi, mImm.getEnabledInputMethodSubtypeList(imi, true)); } mEnabledInputMethodAndSubtypesCacheStr = newEnabledIMIs; mPackageChanged = false; mLastSystemLocaleString = currentSystemLocaleString; } return mEnabledInputMethodAndSubtypesCache; }