@Override
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    String prefName = preference.getKey();
    if (prefName != null && prefName.equals("privacy.masterpassword.enabled")) {
      showDialog(
          (Boolean) newValue ? DIALOG_CREATE_MASTER_PASSWORD : DIALOG_REMOVE_MASTER_PASSWORD);
      return false;
    } else if (prefName != null && prefName.equals("browser.menu.showCharacterEncoding")) {
      setCharEncodingState(((String) newValue).equals("true"));
    }

    setPreference(prefName, newValue);
    if (preference instanceof ListPreference) {
      // We need to find the entry for the new value
      int newIndex = ((ListPreference) preference).findIndexOfValue((String) newValue);
      CharSequence newEntry = ((ListPreference) preference).getEntries()[newIndex];
      ((ListPreference) preference).setSummary(newEntry);
    } else if (preference instanceof LinkPreference) {
      finish();
    } else if (preference instanceof FontSizePreference) {
      final FontSizePreference fontSizePref = (FontSizePreference) preference;
      fontSizePref.setSummary(fontSizePref.getSavedFontSizeName());
    }
    return true;
  }
  private void initGroups(PreferenceGroup preferences) {
    final int count = preferences.getPreferenceCount();
    for (int i = 0; i < count; i++) {
      Preference pref = preferences.getPreference(i);
      if (pref instanceof PreferenceGroup) initGroups((PreferenceGroup) pref);
      else {
        pref.setOnPreferenceChangeListener(this);

        // Some Preference UI elements are not actually preferences,
        // but they require a key to work correctly. For example,
        // "Clear private data" requires a key for its state to be
        // saved when the orientation changes. It uses the
        // "android.not_a_preference.privacy.clear" key - which doesn't
        // exist in Gecko - to satisfy this requirement.
        String key = pref.getKey();
        if (key != null && !key.startsWith(NON_PREF_PREFIX)) {
          mPreferencesList.add(pref.getKey());
        }
      }
    }
  }