public void deletePref(String key) {
   if (preferenceFile == null) {
     return;
   }
   preferenceFile.removeValue(key);
   PreferenceFile.saveFast(preferenceFile, mFullPath, mPackageName);
   ((PreferenceAdapter) gridView.getAdapter()).notifyDataSetChanged();
 }
 public void addPrefKeyValue(String previousKey, String newKey, Object value, boolean editMode) {
   if (preferenceFile == null) {
     return;
   }
   preferenceFile.add(previousKey, newKey, value, editMode);
   PreferenceFile.saveFast(preferenceFile, mFullPath, mPackageName);
   ((PreferenceAdapter) gridView.getAdapter()).notifyDataSetChanged();
 }
 @Override
 protected PreferenceFile doInBackground(Void... params) {
   long start = System.currentTimeMillis();
   Log.d(Utils.TAG, "Start reading " + mFile);
   Data data = App.getRoot().file.read(mFile);
   Log.d(
       Utils.TAG,
       "End reading " + mFile + " --> " + (System.currentTimeMillis() - start) + " ms");
   return PreferenceFile.fromXml(data == null ? null : data.toString());
 }
 @SuppressWarnings("ConstantConditions")
 @Override
 public void onPrepareOptionsMenu(Menu menu) {
   menu.findItem(R.id.action_add)
       .setEnabled(preferenceFile != null && preferenceFile.isValidPreferenceFile());
   menu.findItem(R.id.action_add)
       .setIcon(
           preferenceFile != null && preferenceFile.isValidPreferenceFile()
               ? R.drawable.ic_action_add
               : R.drawable.ic_action_add_disabled);
   menu.findItem(R.id.action_sort_alpha)
       .setChecked(PreferencesActivity.preferenceSortType == PreferenceSortType.ALPHANUMERIC);
   menu.findItem(R.id.action_sort_type)
       .setChecked(
           PreferencesActivity.preferenceSortType == PreferenceSortType.TYPE_AND_ALPHANUMERIC);
   menu.findItem(R.id.action_restore_file)
       .setVisible(mListener != null && mListener.canRestoreFile(mFullPath));
   super.onPrepareOptionsMenu(menu);
 }
  private void setSortType(PreferenceSortType type) {
    if (PreferencesActivity.preferenceSortType != type) {
      PreferencesActivity.preferenceSortType = type;
      if (getActivity() != null) {
        getActivity().invalidateOptionsMenu();
        PreferenceManager.getDefaultSharedPreferences(getActivity())
            .edit()
            .putInt(PreferencesActivity.KEY_SORT_TYPE, type.ordinal())
            .commit();
      }

      if (gridView.getAdapter() != null && preferenceFile != null) {
        preferenceFile.updateSort();
        ((PreferenceAdapter) gridView.getAdapter()).notifyDataSetChanged();
      }
    }
  }
  void updateListView(PreferenceFile p, boolean animate) {
    if (getActivity() == null || getActivity().isFinishing()) {
      return;
    }
    if (p == null) {
      if (getActivity() != null) {
        getActivity().finish();
      }
      return;
    }
    preferenceFile = p;
    emptyViewText.setText(
        preferenceFile.isValidPreferenceFile()
            ? R.string.empty_preference_file_valid
            : R.string.empty_preference_file_invalid);
    loadingView.setVisibility(View.GONE);
    gridView.setVisibility(View.VISIBLE);

    if (animate) {
      if (getActivity() != null) {
        Animation fadeOut = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out);
        if (fadeOut != null) {
          loadingView.startAnimation(fadeOut);
        }
        Animation fadeIn = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
        if (fadeIn != null) {
          gridView.startAnimation(fadeIn);
        }
      }
    }

    gridView.setAdapter(new PreferenceAdapter(getActivity(), this));
    gridView.setEmptyView(emptyView);
    gridView.setOnItemClickListener(
        new OnItemClickListener() {
          @SuppressWarnings("unchecked")
          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            Entry<String, Object> item =
                (Entry<String, Object>) gridView.getAdapter().getItem(arg2);
            PreferenceType type = PreferenceType.fromObject(item.getValue());
            if (type == PreferenceType.UNSUPPORTED) {
              Toast.makeText(getActivity(), R.string.preference_unsupported, Toast.LENGTH_SHORT)
                  .show();
            } else {
              showPrefDialog(type, true, item.getKey(), item.getValue());
            }
          }
        });
    gridView.setMultiChoiceModeListener(
        new MultiChoiceModeListener() {

          @Override
          public void onItemCheckedStateChanged(
              ActionMode mode, int position, long id, boolean checked) {
            ((PreferenceAdapter) gridView.getAdapter()).itemCheckedStateChanged(position, checked);
            mode.setTitle(Html.fromHtml("<b>" + gridView.getCheckedItemCount() + "</b>"));
          }

          @Override
          public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
              case R.id.action_delete:
                ((PreferenceAdapter) gridView.getAdapter()).deleteSelection();
                PreferenceFile.saveFast(preferenceFile, mFullPath, mPackageName);
                ((PreferenceAdapter) gridView.getAdapter()).notifyDataSetChanged();
                mode.finish();
                return true;
              case R.id.action_select_all:
                boolean check = gridView.getCheckedItemCount() != gridView.getCount();
                for (int i = 0; i < gridView.getCount(); i++) {
                  gridView.setItemChecked(i, check);
                }
                return true;
              default:
                return false;
            }
          }

          @Override
          public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            if (inflater != null) {
              inflater.inflate(R.menu.cab, menu);
            }
            return true;
          }

          @Override
          public void onDestroyActionMode(ActionMode mode) {
            ((PreferenceAdapter) gridView.getAdapter()).resetSelection();
            getActivity().invalidateOptionsMenu();
          }

          @Override
          public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
          }
        });
    getActivity().invalidateOptionsMenu();
  }