@Override
 public void onRemovePressed(SubtypePreference subtypePref) {
   mIsAddingNewSubtype = false;
   final PreferenceGroup group = getPreferenceScreen();
   group.removePreference(subtypePref);
   ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), getSubtypes());
 }
    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);
    }
        @Override
        public void onAddPressed(SubtypePreference subtypePref) {
          mIsAddingNewSubtype = false;
          final InputMethodSubtype subtype = subtypePref.getSubtype();
          if (findDuplicatedSubtype(subtype) == null) {
            ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), getSubtypes());
            mSubtypePreferenceKeyForSubtypeEnabler = subtypePref.getKey();
            mSubtypeEnablerNotificationDialog = createDialog(subtypePref);
            mSubtypeEnablerNotificationDialog.show();
            return;
          }

          // Newly added subtype is duplicated.
          final PreferenceGroup group = getPreferenceScreen();
          group.removePreference(subtypePref);
          showSubtypeAlreadyExistsToast(subtype);
        }
  @Override
  public void onPause() {
    super.onPause();
    final String oldSubtypes = SettingsValues.getPrefAdditionalSubtypes(mPrefs, getResources());
    final InputMethodSubtype[] subtypes = getSubtypes();
    final String prefSubtypes = getPrefSubtypes(subtypes);
    if (prefSubtypes.equals(oldSubtypes)) {
      return;
    }

    final SharedPreferences.Editor editor = mPrefs.edit();
    try {
      editor.putString(Settings.PREF_CUSTOM_INPUT_STYLES, prefSubtypes);
    } finally {
      editor.apply();
    }
    ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), subtypes);
  }
        @Override
        public void onSavePressed(SubtypePreference subtypePref) {
          final InputMethodSubtype subtype = subtypePref.getSubtype();
          if (!subtypePref.hasBeenModified()) {
            return;
          }
          if (findDuplicatedSubtype(subtype) == null) {
            ImfUtils.setAdditionalInputMethodSubtypes(getActivity(), getSubtypes());
            return;
          }

          // Saved subtype is duplicated.
          final PreferenceGroup group = getPreferenceScreen();
          group.removePreference(subtypePref);
          subtypePref.revert();
          group.addPreference(subtypePref);
          showSubtypeAlreadyExistsToast(subtype);
        }
 private InputMethodSubtype findDuplicatedSubtype(InputMethodSubtype subtype) {
   final String localeString = subtype.getLocale();
   final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
   return ImfUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
       getActivity(), localeString, keyboardLayoutSetName);
 }