michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.preferences; michael@0: michael@0: import android.content.Context; michael@0: import android.preference.Preference; michael@0: import android.util.AttributeSet; michael@0: import android.util.Log; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.GeckoEvent; michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: public class SearchPreferenceCategory extends CustomListCategory implements GeckoEventListener { michael@0: public static final String LOGTAG = "SearchPrefCategory"; michael@0: michael@0: public SearchPreferenceCategory(Context context) { michael@0: super(context); michael@0: } michael@0: michael@0: public SearchPreferenceCategory(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: } michael@0: michael@0: public SearchPreferenceCategory(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: } michael@0: michael@0: @Override michael@0: protected void onAttachedToActivity() { michael@0: super.onAttachedToActivity(); michael@0: michael@0: // Register for SearchEngines messages and request list of search engines from Gecko. michael@0: GeckoAppShell.registerEventListener("SearchEngines:Data", this); michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:GetVisible", null)); michael@0: } michael@0: michael@0: @Override michael@0: protected void onPrepareForRemoval() { michael@0: GeckoAppShell.unregisterEventListener("SearchEngines:Data", this); michael@0: } michael@0: michael@0: @Override michael@0: public void setDefault(CustomListPreference item) { michael@0: super.setDefault(item); michael@0: michael@0: sendGeckoEngineEvent("SearchEngines:SetDefault", item.getTitle().toString()); michael@0: } michael@0: michael@0: @Override michael@0: public void uninstall(CustomListPreference item) { michael@0: super.uninstall(item); michael@0: michael@0: sendGeckoEngineEvent("SearchEngines:Remove", item.getTitle().toString()); michael@0: } michael@0: michael@0: @Override michael@0: public void handleMessage(String event, final JSONObject data) { michael@0: if (event.equals("SearchEngines:Data")) { michael@0: // Parse engines array from JSON. michael@0: JSONArray engines; michael@0: try { michael@0: engines = data.getJSONArray("searchEngines"); michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "Unable to decode search engine data from Gecko.", e); michael@0: return; michael@0: } michael@0: michael@0: // Clear the preferences category from this thread. michael@0: this.removeAll(); michael@0: michael@0: // Create an element in this PreferenceCategory for each engine. michael@0: for (int i = 0; i < engines.length(); i++) { michael@0: try { michael@0: JSONObject engineJSON = engines.getJSONObject(i); michael@0: final String engineName = engineJSON.getString("name"); michael@0: michael@0: final SearchEnginePreference enginePreference = new SearchEnginePreference(getContext(), this); michael@0: enginePreference.setSearchEngineFromJSON(engineJSON); michael@0: enginePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { michael@0: @Override michael@0: public boolean onPreferenceClick(Preference preference) { michael@0: SearchEnginePreference sPref = (SearchEnginePreference) preference; michael@0: // Display the configuration dialog associated with the tapped engine. michael@0: sPref.showDialog(); michael@0: return true; michael@0: } michael@0: }); michael@0: michael@0: addPreference(enginePreference); michael@0: michael@0: // The first element in the array is the default engine. michael@0: if (i == 0) { michael@0: // We set this here, not in setSearchEngineFromJSON, because it allows us to michael@0: // keep a reference to the default engine to use when the AlertDialog michael@0: // callbacks are used. michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: enginePreference.setIsDefault(true); michael@0: } michael@0: }); michael@0: mDefaultReference = enginePreference; michael@0: } michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "JSONException parsing engine at index " + i, e); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Helper method to send a particular event string to Gecko with an associated engine name. michael@0: * @param event The type of event to send. michael@0: * @param engine The engine to which the event relates. michael@0: */ michael@0: private void sendGeckoEngineEvent(String event, String engineName) { michael@0: JSONObject json = new JSONObject(); michael@0: try { michael@0: json.put("engine", engineName); michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "JSONException creating search engine configuration change message for Gecko.", e); michael@0: return; michael@0: } michael@0: GeckoAppShell.notifyGeckoOfEvent(GeckoEvent.createBroadcastEvent(event, json.toString())); michael@0: } michael@0: }