@Override
 public void onCreatePreferences(Bundle bundle, String s) {
   Activity activity = getActivity();
   PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(activity);
   screen.setTitle(activity.getString(R.string.available_virtual_keyboard_category));
   setPreferenceScreen(screen);
   mInputMethodSettingValues = InputMethodSettingValuesWrapper.getInstance(activity);
   mImm = activity.getSystemService(InputMethodManager.class);
   mDpm = activity.getSystemService(DevicePolicyManager.class);
 }
 @Override
 public void onResume() {
   super.onResume();
   // Refresh internal states in mInputMethodSettingValues to keep the latest
   // "InputMethodInfo"s and "InputMethodSubtype"s
   mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
   updateInputMethodPreferenceViews();
 }
 @Override
 public void onSaveInputMethodPreference(final InputMethodPreference pref) {
   final boolean hasHardwareKeyboard =
       getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY;
   InputMethodAndSubtypeUtil.saveInputMethodSubtypeList(
       this, getContentResolver(), mImm.getInputMethodList(), hasHardwareKeyboard);
   // Update input method settings and preference list.
   mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
   for (final InputMethodPreference p : mInputMethodPreferenceList) {
     p.updatePreferenceViews();
   }
 }
 private void updateInputMethodPreferenceViews() {
   mInputMethodSettingValues.refreshAllInputMethodAndSubtypes();
   // Clear existing "InputMethodPreference"s
   mInputMethodPreferenceList.clear();
   List<String> permittedList = mDpm.getPermittedInputMethodsForCurrentUser();
   final Context context = getPrefContext();
   final PackageManager packageManager = getActivity().getPackageManager();
   final List<InputMethodInfo> imis = mInputMethodSettingValues.getInputMethodList();
   final int N = (imis == null ? 0 : imis.size());
   for (int i = 0; i < N; ++i) {
     final InputMethodInfo imi = imis.get(i);
     final boolean isAllowedByOrganization =
         permittedList == null || permittedList.contains(imi.getPackageName());
     final InputMethodPreference pref =
         new InputMethodPreference(context, imi, true, isAllowedByOrganization, this);
     pref.setIcon(getInputMethodIcon(packageManager, imi));
     mInputMethodPreferenceList.add(pref);
   }
   final Collator collator = Collator.getInstance();
   Collections.sort(
       mInputMethodPreferenceList,
       new Comparator<InputMethodPreference>() {
         @Override
         public int compare(InputMethodPreference lhs, InputMethodPreference rhs) {
           return lhs.compareTo(rhs, collator);
         }
       });
   getPreferenceScreen().removeAll();
   for (int i = 0; i < N; ++i) {
     final InputMethodPreference pref = mInputMethodPreferenceList.get(i);
     pref.setOrder(i);
     getPreferenceScreen().addPreference(pref);
     InputMethodAndSubtypeUtil.removeUnnecessaryNonPersistentPreference(pref);
     pref.updatePreferenceViews();
   }
 }