@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);
    }
  }
 private Drawable getSubtypeIcon(InputMethodInfo imi, InputMethodSubtype subtype) {
   if (imi != null) {
     if (DEBUG) {
       Log.d(TAG, "Update icons of IME: " + imi.getPackageName());
       if (subtype != null) {
         Log.d(TAG, "subtype =" + subtype.getLocale() + "," + subtype.getMode());
       }
     }
     if (subtype != null) {
       return mPackageManager.getDrawable(
           imi.getPackageName(), subtype.getIconResId(), imi.getServiceInfo().applicationInfo);
     } else if (imi.getSubtypeCount() > 0) {
       return mPackageManager.getDrawable(
           imi.getPackageName(),
           imi.getSubtypeAt(0).getIconResId(),
           imi.getServiceInfo().applicationInfo);
     } else {
       try {
         return mPackageManager
             .getApplicationInfo(imi.getPackageName(), 0)
             .loadIcon(mPackageManager);
       } catch (PackageManager.NameNotFoundException e) {
         Log.w(TAG, "IME can't be found: " + imi.getPackageName());
       }
     }
   }
   return null;
 }
Example #3
0
  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;
  }