mobile/android/base/preferences/SearchPreferenceCategory.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.preferences;
michael@0 6
michael@0 7 import android.content.Context;
michael@0 8 import android.preference.Preference;
michael@0 9 import android.util.AttributeSet;
michael@0 10 import android.util.Log;
michael@0 11
michael@0 12 import org.json.JSONArray;
michael@0 13 import org.json.JSONException;
michael@0 14 import org.json.JSONObject;
michael@0 15
michael@0 16 import org.mozilla.gecko.GeckoAppShell;
michael@0 17 import org.mozilla.gecko.GeckoEvent;
michael@0 18 import org.mozilla.gecko.util.GeckoEventListener;
michael@0 19 import org.mozilla.gecko.util.ThreadUtils;
michael@0 20
michael@0 21 public class SearchPreferenceCategory extends CustomListCategory implements GeckoEventListener {
michael@0 22 public static final String LOGTAG = "SearchPrefCategory";
michael@0 23
michael@0 24 public SearchPreferenceCategory(Context context) {
michael@0 25 super(context);
michael@0 26 }
michael@0 27
michael@0 28 public SearchPreferenceCategory(Context context, AttributeSet attrs) {
michael@0 29 super(context, attrs);
michael@0 30 }
michael@0 31
michael@0 32 public SearchPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
michael@0 33 super(context, attrs, defStyle);
michael@0 34 }
michael@0 35
michael@0 36 @Override
michael@0 37 protected void onAttachedToActivity() {
michael@0 38 super.onAttachedToActivity();
michael@0 39
michael@0 40 // Register for SearchEngines messages and request list of search engines from Gecko.
michael@0 41 GeckoAppShell.registerEventListener("SearchEngines:Data", this);
michael@0 42 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:GetVisible", null));
michael@0 43 }
michael@0 44
michael@0 45 @Override
michael@0 46 protected void onPrepareForRemoval() {
michael@0 47 GeckoAppShell.unregisterEventListener("SearchEngines:Data", this);
michael@0 48 }
michael@0 49
michael@0 50 @Override
michael@0 51 public void setDefault(CustomListPreference item) {
michael@0 52 super.setDefault(item);
michael@0 53
michael@0 54 sendGeckoEngineEvent("SearchEngines:SetDefault", item.getTitle().toString());
michael@0 55 }
michael@0 56
michael@0 57 @Override
michael@0 58 public void uninstall(CustomListPreference item) {
michael@0 59 super.uninstall(item);
michael@0 60
michael@0 61 sendGeckoEngineEvent("SearchEngines:Remove", item.getTitle().toString());
michael@0 62 }
michael@0 63
michael@0 64 @Override
michael@0 65 public void handleMessage(String event, final JSONObject data) {
michael@0 66 if (event.equals("SearchEngines:Data")) {
michael@0 67 // Parse engines array from JSON.
michael@0 68 JSONArray engines;
michael@0 69 try {
michael@0 70 engines = data.getJSONArray("searchEngines");
michael@0 71 } catch (JSONException e) {
michael@0 72 Log.e(LOGTAG, "Unable to decode search engine data from Gecko.", e);
michael@0 73 return;
michael@0 74 }
michael@0 75
michael@0 76 // Clear the preferences category from this thread.
michael@0 77 this.removeAll();
michael@0 78
michael@0 79 // Create an element in this PreferenceCategory for each engine.
michael@0 80 for (int i = 0; i < engines.length(); i++) {
michael@0 81 try {
michael@0 82 JSONObject engineJSON = engines.getJSONObject(i);
michael@0 83 final String engineName = engineJSON.getString("name");
michael@0 84
michael@0 85 final SearchEnginePreference enginePreference = new SearchEnginePreference(getContext(), this);
michael@0 86 enginePreference.setSearchEngineFromJSON(engineJSON);
michael@0 87 enginePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
michael@0 88 @Override
michael@0 89 public boolean onPreferenceClick(Preference preference) {
michael@0 90 SearchEnginePreference sPref = (SearchEnginePreference) preference;
michael@0 91 // Display the configuration dialog associated with the tapped engine.
michael@0 92 sPref.showDialog();
michael@0 93 return true;
michael@0 94 }
michael@0 95 });
michael@0 96
michael@0 97 addPreference(enginePreference);
michael@0 98
michael@0 99 // The first element in the array is the default engine.
michael@0 100 if (i == 0) {
michael@0 101 // We set this here, not in setSearchEngineFromJSON, because it allows us to
michael@0 102 // keep a reference to the default engine to use when the AlertDialog
michael@0 103 // callbacks are used.
michael@0 104 ThreadUtils.postToUiThread(new Runnable() {
michael@0 105 @Override
michael@0 106 public void run() {
michael@0 107 enginePreference.setIsDefault(true);
michael@0 108 }
michael@0 109 });
michael@0 110 mDefaultReference = enginePreference;
michael@0 111 }
michael@0 112 } catch (JSONException e) {
michael@0 113 Log.e(LOGTAG, "JSONException parsing engine at index " + i, e);
michael@0 114 }
michael@0 115 }
michael@0 116 }
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Helper method to send a particular event string to Gecko with an associated engine name.
michael@0 121 * @param event The type of event to send.
michael@0 122 * @param engine The engine to which the event relates.
michael@0 123 */
michael@0 124 private void sendGeckoEngineEvent(String event, String engineName) {
michael@0 125 JSONObject json = new JSONObject();
michael@0 126 try {
michael@0 127 json.put("engine", engineName);
michael@0 128 } catch (JSONException e) {
michael@0 129 Log.e(LOGTAG, "JSONException creating search engine configuration change message for Gecko.", e);
michael@0 130 return;
michael@0 131 }
michael@0 132 GeckoAppShell.notifyGeckoOfEvent(GeckoEvent.createBroadcastEvent(event, json.toString()));
michael@0 133 }
michael@0 134 }

mercurial