@Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   addPreferencesFromResource(R.xml.preferences);
   GeckoAppShell.registerGeckoEventListener("Preferences:Data", this);
   GeckoAppShell.registerGeckoEventListener("Sanitize:Finished", this);
 }
 public void handleMessage(String event, JSONObject message) {
   try {
     if (event.equals("Preferences:Data")) {
       JSONArray jsonPrefs = message.getJSONArray("preferences");
       refresh(jsonPrefs);
     } else if (event.equals("Sanitize:Finished")) {
       boolean success = message.getBoolean("success");
       final int stringRes = success ? R.string.private_data_success : R.string.private_data_fail;
       final Context context = this;
       GeckoAppShell.getMainHandler()
           .post(
               new Runnable() {
                 public void run() {
                   Toast.makeText(context, stringRes, Toast.LENGTH_SHORT).show();
                 }
               });
     }
   } catch (Exception e) {
     Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
   }
 }
  // send the Preferences:Set message to Gecko
  public static void setPreference(String pref, Object value) {
    if (pref == null || pref.length() == 0) return;

    try {
      JSONObject jsonPref = new JSONObject();
      jsonPref.put("name", pref);
      if (value instanceof Boolean) {
        jsonPref.put("type", "bool");
        jsonPref.put("value", ((Boolean) value).booleanValue());
      } else if (value instanceof Integer) {
        jsonPref.put("type", "int");
        jsonPref.put("value", ((Integer) value).intValue());
      } else {
        jsonPref.put("type", "string");
        jsonPref.put("value", String.valueOf(value));
      }

      GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Set", jsonPref.toString());
      GeckoAppShell.sendEventToGecko(event);
    } catch (JSONException e) {
      Log.e(LOGTAG, "JSON exception: ", e);
    }
  }
  // Initialize preferences by sending the "Preferences:Get" command to Gecko
  private void initValues() {
    JSONArray jsonPrefs = new JSONArray(mPreferencesList);

    GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Get", jsonPrefs.toString());
    GeckoAppShell.sendEventToGecko(event);
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   GeckoAppShell.unregisterGeckoEventListener("Preferences:Data", this);
   GeckoAppShell.unregisterGeckoEventListener("Sanitize:Finished", this);
 }
  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);
    }
  }