@Override
 public boolean onOptionsItemSelected(MenuItem item) {
   final int itemId = item.getItemId();
   if (itemId == MENU_ADD_SUBTYPE) {
     final SubtypePreference newSubtype =
         SubtypePreference.newIncompleteSubtypePreference(getActivity(), mSubtypeProxy);
     getPreferenceScreen().addPreference(newSubtype);
     newSubtype.show();
     mIsAddingNewSubtype = true;
     return true;
   }
   return super.onOptionsItemSelected(item);
 }
 private InputMethodSubtype[] getSubtypes() {
   final PreferenceGroup group = getPreferenceScreen();
   final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
   final int count = group.getPreferenceCount();
   for (int i = 0; i < count; i++) {
     final Preference pref = group.getPreference(i);
     if (pref instanceof SubtypePreference) {
       final SubtypePreference subtypePref = (SubtypePreference) pref;
       // We should not save newly adding subtype to preference because it is incomplete.
       if (subtypePref.isIncomplete()) continue;
       subtypes.add(subtypePref.getSubtype());
     }
   }
   return subtypes.toArray(new InputMethodSubtype[subtypes.size()]);
 }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    final Context context = getActivity();
    mSubtypeLocaleAdapter = new SubtypeLocaleAdapter(context);
    mKeyboardLayoutSetAdapter = new KeyboardLayoutSetAdapter(context);

    final String prefSubtypes = SettingsValues.getPrefAdditionalSubtypes(mPrefs, getResources());
    setPrefSubtypes(prefSubtypes, context);

    mIsAddingNewSubtype =
        (savedInstanceState != null) && savedInstanceState.containsKey(KEY_IS_ADDING_NEW_SUBTYPE);
    if (mIsAddingNewSubtype) {
      getPreferenceScreen()
          .addPreference(SubtypePreference.newIncompleteSubtypePreference(context, mSubtypeProxy));
    }

    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null
        && savedInstanceState.containsKey(KEY_IS_SUBTYPE_ENABLER_NOTIFICATION_DIALOG_OPEN)) {
      mSubtypePreferenceKeyForSubtypeEnabler =
          savedInstanceState.getString(KEY_SUBTYPE_FOR_SUBTYPE_ENABLER);
      final SubtypePreference subtypePref =
          (SubtypePreference) findPreference(mSubtypePreferenceKeyForSubtypeEnabler);
      mSubtypeEnablerNotificationDialog = createDialog(subtypePref);
      mSubtypeEnablerNotificationDialog.show();
    }
  }
        @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 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);
        }