mobile/android/base/preferences/SearchPreferenceCategory.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/preferences/SearchPreferenceCategory.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.preferences;
     1.9 +
    1.10 +import android.content.Context;
    1.11 +import android.preference.Preference;
    1.12 +import android.util.AttributeSet;
    1.13 +import android.util.Log;
    1.14 +
    1.15 +import org.json.JSONArray;
    1.16 +import org.json.JSONException;
    1.17 +import org.json.JSONObject;
    1.18 +
    1.19 +import org.mozilla.gecko.GeckoAppShell;
    1.20 +import org.mozilla.gecko.GeckoEvent;
    1.21 +import org.mozilla.gecko.util.GeckoEventListener;
    1.22 +import org.mozilla.gecko.util.ThreadUtils;
    1.23 +
    1.24 +public class SearchPreferenceCategory extends CustomListCategory implements GeckoEventListener {
    1.25 +    public static final String LOGTAG = "SearchPrefCategory";
    1.26 +
    1.27 +    public SearchPreferenceCategory(Context context) {
    1.28 +        super(context);
    1.29 +    }
    1.30 +
    1.31 +    public SearchPreferenceCategory(Context context, AttributeSet attrs) {
    1.32 +        super(context, attrs);
    1.33 +    }
    1.34 +
    1.35 +    public SearchPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
    1.36 +        super(context, attrs, defStyle);
    1.37 +    }
    1.38 +
    1.39 +    @Override
    1.40 +    protected void onAttachedToActivity() {
    1.41 +        super.onAttachedToActivity();
    1.42 +
    1.43 +        // Register for SearchEngines messages and request list of search engines from Gecko.
    1.44 +        GeckoAppShell.registerEventListener("SearchEngines:Data", this);
    1.45 +        GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:GetVisible", null));
    1.46 +    }
    1.47 +
    1.48 +    @Override
    1.49 +    protected void onPrepareForRemoval() {
    1.50 +        GeckoAppShell.unregisterEventListener("SearchEngines:Data", this);
    1.51 +    }
    1.52 +
    1.53 +    @Override
    1.54 +    public void setDefault(CustomListPreference item) {
    1.55 +        super.setDefault(item);
    1.56 +
    1.57 +        sendGeckoEngineEvent("SearchEngines:SetDefault", item.getTitle().toString());
    1.58 +    }
    1.59 +
    1.60 +    @Override
    1.61 +    public void uninstall(CustomListPreference item) {
    1.62 +        super.uninstall(item);
    1.63 +
    1.64 +        sendGeckoEngineEvent("SearchEngines:Remove", item.getTitle().toString());
    1.65 +    }
    1.66 +
    1.67 +    @Override
    1.68 +    public void handleMessage(String event, final JSONObject data) {
    1.69 +        if (event.equals("SearchEngines:Data")) {
    1.70 +            // Parse engines array from JSON.
    1.71 +            JSONArray engines;
    1.72 +            try {
    1.73 +                engines = data.getJSONArray("searchEngines");
    1.74 +            } catch (JSONException e) {
    1.75 +                Log.e(LOGTAG, "Unable to decode search engine data from Gecko.", e);
    1.76 +                return;
    1.77 +            }
    1.78 +
    1.79 +            // Clear the preferences category from this thread.
    1.80 +            this.removeAll();
    1.81 +
    1.82 +            // Create an element in this PreferenceCategory for each engine.
    1.83 +            for (int i = 0; i < engines.length(); i++) {
    1.84 +                try {
    1.85 +                    JSONObject engineJSON = engines.getJSONObject(i);
    1.86 +                    final String engineName = engineJSON.getString("name");
    1.87 +
    1.88 +                    final SearchEnginePreference enginePreference = new SearchEnginePreference(getContext(), this);
    1.89 +                    enginePreference.setSearchEngineFromJSON(engineJSON);
    1.90 +                    enginePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    1.91 +                        @Override
    1.92 +                        public boolean onPreferenceClick(Preference preference) {
    1.93 +                            SearchEnginePreference sPref = (SearchEnginePreference) preference;
    1.94 +                            // Display the configuration dialog associated with the tapped engine.
    1.95 +                            sPref.showDialog();
    1.96 +                            return true;
    1.97 +                        }
    1.98 +                    });
    1.99 +
   1.100 +                    addPreference(enginePreference);
   1.101 +
   1.102 +                    // The first element in the array is the default engine.
   1.103 +                    if (i == 0) {
   1.104 +                        // We set this here, not in setSearchEngineFromJSON, because it allows us to
   1.105 +                        // keep a reference  to the default engine to use when the AlertDialog
   1.106 +                        // callbacks are used.
   1.107 +                        ThreadUtils.postToUiThread(new Runnable() {
   1.108 +                            @Override
   1.109 +                            public void run() {
   1.110 +                                enginePreference.setIsDefault(true);
   1.111 +                            }
   1.112 +                        });
   1.113 +                        mDefaultReference = enginePreference;
   1.114 +                    }
   1.115 +                } catch (JSONException e) {
   1.116 +                    Log.e(LOGTAG, "JSONException parsing engine at index " + i, e);
   1.117 +                }
   1.118 +            }
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Helper method to send a particular event string to Gecko with an associated engine name.
   1.124 +     * @param event The type of event to send.
   1.125 +     * @param engine The engine to which the event relates.
   1.126 +     */
   1.127 +    private void sendGeckoEngineEvent(String event, String engineName) {
   1.128 +        JSONObject json = new JSONObject();
   1.129 +        try {
   1.130 +            json.put("engine", engineName);
   1.131 +        } catch (JSONException e) {
   1.132 +            Log.e(LOGTAG, "JSONException creating search engine configuration change message for Gecko.", e);
   1.133 +            return;
   1.134 +        }
   1.135 +        GeckoAppShell.notifyGeckoOfEvent(GeckoEvent.createBroadcastEvent(event, json.toString()));
   1.136 +    }
   1.137 +}

mercurial