private void removeAll() {
   mProgressCategory.removeAll();
   getPreferenceScreen().removeAll();
   mProgressCategory.setProgress(false);
   mItemPreferences.clear();
   mItemKeys.clear();
 }
 public boolean onPreferenceChange(Preference preference, Object newValue) {
   if (preference != null && preference instanceof SecurityItemPreference) {
     Xlog.d(TAG, "onPreferenceChange, select " + preference.getKey() + " active");
     Settings.Global.putString(
         getContentResolver(), Settings.Global.NFC_MULTISE_ACTIVE, preference.getKey());
     mProgressCategory.setProgress(true);
     mSwitchBar.setEnabled(false);
     for (SecurityItemPreference pref : mItemPreferences) {
       pref.setEnabled(false);
     }
     return true;
   }
   return false;
 }
  private void addItemPreference() {
    String[] list = getCardEmulationList();
    if (list != null) {
      for (String key : list) {
        SecurityItemPreference pref = new SecurityItemPreference(getActivity());
        pref.setTitle(key);
        pref.setKey(key);
        pref.setOnPreferenceChangeListener(this);
        mProgressCategory.addPreference(pref);

        mItemPreferences.add(pref);
        mItemKeys.add(key);
      }
    }
  }
 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
   if (preference != null && preference instanceof SecurityItemPreference) {
     Xlog.d(TAG, "onPreferenceTreeClick " + preference.getKey());
     String activeMode =
         Settings.Global.getString(getContentResolver(), Settings.Global.NFC_MULTISE_ACTIVE);
     String prefKey = preference.getKey();
     if (prefKey != null && !(prefKey.equals(activeMode))) {
       Settings.Global.putString(
           getContentResolver(), Settings.Global.NFC_MULTISE_ACTIVE, preference.getKey());
       mProgressCategory.setProgress(true);
       mSwitchBar.setEnabled(false);
       for (SecurityItemPreference pref : mItemPreferences) {
         pref.setEnabled(false);
       }
       return true;
     }
   }
   return false;
 }
  /** update the preference according to the status of NfcAdapter settings */
  private void updatePreferences() {

    removeAll();

    String activeMode =
        Settings.Global.getString(getContentResolver(), Settings.Global.NFC_MULTISE_ACTIVE);
    String previousMode =
        Settings.Global.getString(getContentResolver(), Settings.Global.NFC_MULTISE_PREVIOUS);
    int transactionStatus =
        Settings.Global.getInt(getContentResolver(), Settings.Global.NFC_MULTISE_IN_TRANSACTION, 0);
    int switchingStatus =
        Settings.Global.getInt(getContentResolver(), Settings.Global.NFC_MULTISE_IN_SWITCHING, 0);
    Xlog.d(
        TAG,
        "updatePreferences(),EMULATION_OFF "
            + EMULATION_OFF
            + ", active mode: "
            + activeMode
            + ", previous mode is "
            + previousMode);
    Xlog.d(
        TAG,
        "updatePreferences, transactionStatus is "
            + transactionStatus
            + " switchingStatus is "
            + switchingStatus);

    if (EMULATION_OFF.equals(activeMode)) {
      mUpdateStatusOnly = true;
      mSwitchBar.setChecked(false);
      mUpdateStatusOnly = false;
      if (getCardEmulationList().length == 0) {
        Xlog.d(TAG, "no available security elment found and the active mode is off");
        mEmptyView.setText(R.string.card_emulation_settings_no_element_found);
      } else {
        if (switchingStatus == 0) {
          mEmptyView.setText(R.string.card_emulation_settings_off_text);
        } else {
          mEmptyView.setText(R.string.card_emulation_turning_off_text);
        }
      }
      mSwitchBar.setEnabled(transactionStatus == 0 && switchingStatus == 0);
    } else {
      mUpdateStatusOnly = true;
      mSwitchBar.setChecked(true);
      mUpdateStatusOnly = false;
      if (switchingStatus == 1 && EMULATION_OFF.equals(previousMode)) {
        mSwitchBar.setEnabled(false);
        mEmptyView.setText(R.string.card_emulation_turning_on_text);
      } else {
        mSwitchBar.setEnabled(transactionStatus == 0 && switchingStatus == 0);
        addItemPreference();
        int prefCount = mProgressCategory.getPreferenceCount();
        getPreferenceScreen().addPreference(mProgressCategory);
        SecurityItemPreference itemPref = (SecurityItemPreference) findPreference(activeMode);
        if (itemPref != null) {
          itemPref.setChecked(true);
          mActivePref = itemPref;
        } else {
          Xlog.d(TAG, "Activie mode is " + activeMode + ", can not find it on screen");
        }
        mProgressCategory.setProgress(switchingStatus == 1);
        mProgressCategory.setEnabled(transactionStatus == 0 && switchingStatus == 0);
      }
    }
  }