@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 refresh(JSONArray jsonPrefs) {
    // enable all preferences once we have them from gecko
    GeckoAppShell.getMainHandler()
        .post(
            new Runnable() {
              public void run() {
                mPreferenceScreen.setEnabled(true);
              }
            });

    try {
      if (mPreferenceScreen == null) return;

      final int length = jsonPrefs.length();
      for (int i = 0; i < length; i++) {
        JSONObject jPref = jsonPrefs.getJSONObject(i);
        final String prefName = jPref.getString("name");
        final String prefType = jPref.getString("type");
        final Preference pref = mPreferenceScreen.findPreference(prefName);

        if (pref instanceof CheckBoxPreference && "bool".equals(prefType)) {
          final boolean value = jPref.getBoolean("value");
          GeckoAppShell.getMainHandler()
              .post(
                  new Runnable() {
                    public void run() {
                      if (((CheckBoxPreference) pref).isChecked() != value)
                        ((CheckBoxPreference) pref).setChecked(value);
                    }
                  });
        } else if (pref instanceof EditTextPreference && "string".equals(prefType)) {
          final String value = jPref.getString("value");
          GeckoAppShell.getMainHandler()
              .post(
                  new Runnable() {
                    public void run() {
                      ((EditTextPreference) pref).setText(value);
                    }
                  });
        } else if (pref instanceof ListPreference && "string".equals(prefType)) {
          final String value = jPref.getString("value");
          GeckoAppShell.getMainHandler()
              .post(
                  new Runnable() {
                    public void run() {
                      ((ListPreference) pref).setValue(value);
                      // Set the summary string to the current entry
                      CharSequence selectedEntry = ((ListPreference) pref).getEntry();
                      ((ListPreference) pref).setSummary(selectedEntry);
                    }
                  });
        } else if (pref instanceof FontSizePreference) {
          final FontSizePreference fontSizePref = (FontSizePreference) pref;
          final String twipValue = jPref.getString("value");
          fontSizePref.setSavedFontSize(twipValue);
          final String fontSizeName = fontSizePref.getSavedFontSizeName();
          GeckoAppShell.getMainHandler()
              .post(
                  new Runnable() {
                    public void run() {
                      fontSizePref.setSummary(fontSizeName); // Ex: "Small".
                    }
                  });
        }
      }
    } catch (JSONException e) {
      Log.e(LOGTAG, "Problem parsing preferences response: ", e);
    }
  }