@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;
 }
示例#3
0
 public static InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(
     Context context, String localeString, String keyboardLayoutSetName) {
   final InputMethodInfo imi = getInputMethodInfoOfThisIme(context);
   final int count = imi.getSubtypeCount();
   for (int i = 0; i < count; i++) {
     final InputMethodSubtype subtype = imi.getSubtypeAt(i);
     final String layoutName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
     if (localeString.equals(subtype.getLocale()) && keyboardLayoutSetName.equals(layoutName)) {
       return subtype;
     }
   }
   return null;
 }
    public SubtypeLocaleAdapter(Context context) {
      super(context, android.R.layout.simple_spinner_item);
      setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

      final TreeSet<SubtypeLocaleItem> items = new TreeSet<SubtypeLocaleItem>();
      final InputMethodInfo imi = ImfUtils.getInputMethodInfoOfThisIme(context);
      final int count = imi.getSubtypeCount();
      for (int i = 0; i < count; i++) {
        final InputMethodSubtype subtype = imi.getSubtypeAt(i);
        if (subtype.containsExtraValueKey(ASCII_CAPABLE)) {
          items.add(createItem(context, subtype.getLocale()));
        }
      }
      // TODO: Should filter out already existing combinations of locale and layout.
      addAll(items);
    }
 public void setSubtype(InputMethodSubtype subtype) {
   mPreviousSubtype = mSubtype;
   mSubtype = subtype;
   if (isIncomplete()) {
     setTitle(null);
     setDialogTitle(R.string.add_style);
     setKey(KEY_NEW_SUBTYPE);
   } else {
     final String displayName =
         SubtypeLocale.getSubtypeDisplayName(subtype, getContext().getResources());
     setTitle(displayName);
     setDialogTitle(displayName);
     setKey(
         KEY_PREFIX
             + subtype.getLocale()
             + "_"
             + SubtypeLocale.getKeyboardLayoutSetName(subtype));
   }
 }
 @Override
 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
   final Context context = builder.getContext();
   builder.setCancelable(true).setOnCancelListener(this);
   if (isIncomplete()) {
     builder
         .setPositiveButton(R.string.add, this)
         .setNegativeButton(android.R.string.cancel, this);
   } else {
     builder
         .setPositiveButton(R.string.save, this)
         .setNeutralButton(android.R.string.cancel, this)
         .setNegativeButton(R.string.remove, this);
     final SubtypeLocaleItem localeItem =
         SubtypeLocaleAdapter.createItem(context, mSubtype.getLocale());
     final KeyboardLayoutSetItem layoutItem = new KeyboardLayoutSetItem(mSubtype);
     setSpinnerPosition(mSubtypeLocaleSpinner, localeItem);
     setSpinnerPosition(mKeyboardLayoutSetSpinner, layoutItem);
   }
 }
 private InputMethodSubtype findDuplicatedSubtype(InputMethodSubtype subtype) {
   final String localeString = subtype.getLocale();
   final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
   return ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
       getActivity(), localeString, keyboardLayoutSetName);
 }