mobile/android/base/preferences/SearchPreferenceCategory.java

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

mercurial