GoogleSuggestClient.java revision 93bd2e70b8b08da1ec37fd0e990dac05551d2e90
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.quicksearchbox.google;
18
19import com.android.quicksearchbox.R;
20import com.android.quicksearchbox.Source;
21import com.android.quicksearchbox.SourceResult;
22import com.android.quicksearchbox.SuggestionCursor;
23
24import org.apache.http.HttpResponse;
25import org.apache.http.client.HttpClient;
26import org.apache.http.client.methods.HttpGet;
27import org.apache.http.impl.client.DefaultHttpClient;
28import org.apache.http.params.HttpParams;
29import org.apache.http.params.HttpProtocolParams;
30import org.apache.http.util.EntityUtils;
31import org.json.JSONArray;
32import org.json.JSONException;
33
34import android.content.ComponentName;
35import android.content.Context;
36import android.net.ConnectivityManager;
37import android.net.NetworkInfo;
38import android.text.TextUtils;
39import android.util.Log;
40
41import java.io.IOException;
42import java.io.UnsupportedEncodingException;
43import java.net.URLEncoder;
44import java.util.Locale;
45
46/**
47 * Use network-based Google Suggests to provide search suggestions.
48 */
49public class GoogleSuggestClient implements GoogleClient {
50
51    private static final boolean DBG = false;
52    private static final String LOG_TAG = "GoogleSearch";
53
54    private static final String USER_AGENT = "Android/1.0";
55    private String mSuggestUri;
56    private static final int HTTP_TIMEOUT_MS = 1000;
57
58    // TODO: this should be defined somewhere
59    private static final String HTTP_TIMEOUT = "http.connection-manager.timeout";
60
61    private final Context mContext;
62    private final Source mSource;
63    private final HttpClient mHttpClient;
64
65    public GoogleSuggestClient(Context context, Source source) {
66        mContext = context;
67        mSource = source;
68        mHttpClient = new DefaultHttpClient();
69        HttpParams params = mHttpClient.getParams();
70        HttpProtocolParams.setUserAgent(params, USER_AGENT);
71        params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);
72
73        // NOTE:  Do not look up the resource here;  Localization changes may not have completed
74        // yet (e.g. we may still be reading the SIM card).
75        mSuggestUri = null;
76    }
77
78    protected Context getContext() {
79        return mContext;
80    }
81
82    public ComponentName getIntentComponent() {
83        return new ComponentName(getContext(), GoogleSearch.class);
84    }
85
86    public boolean isLocationAware() {
87        return false;
88    }
89
90    /**
91     * Queries for a given search term and returns a cursor containing
92     * suggestions ordered by best match.
93     */
94    public SourceResult query(String query) {
95        if (TextUtils.isEmpty(query)) {
96            return null;
97        }
98        if (!isNetworkConnected()) {
99            Log.i(LOG_TAG, "Not connected to network.");
100            return null;
101        }
102        try {
103            query = URLEncoder.encode(query, "UTF-8");
104            // NOTE:  This code uses resources to optionally select the search Uri, based on the
105            // MCC value from the SIM.  iThe default string will most likely be fine.  It is
106            // paramerterized to accept info from the Locale, the language code is the first
107            // parameter (%1$s) and the country code is the second (%2$s).  This code *must*
108            // function in the same way as a similar lookup in
109            // com.android.browser.BrowserActivity#onCreate().  If you change
110            // either of these functions, change them both.  (The same is true for the underlying
111            // resource strings, which are stored in mcc-specific xml files.)
112            if (mSuggestUri == null) {
113                Locale l = Locale.getDefault();
114                String language = l.getLanguage();
115                String country = l.getCountry().toLowerCase();
116                // Chinese and Portuguese have two langauge variants.
117                if ("zh".equals(language)) {
118                    if ("cn".equals(country)) {
119                        language = "zh-CN";
120                    } else if ("tw".equals(country)) {
121                        language = "zh-TW";
122                    }
123                } else if ("pt".equals(language)) {
124                    if ("br".equals(country)) {
125                        language = "pt-BR";
126                    } else if ("pt".equals(country)) {
127                        language = "pt-PT";
128                    }
129                }
130                mSuggestUri = getContext().getResources().getString(R.string.google_suggest_base,
131                                                                    language,
132                                                                    country)
133                        + "json=true&q=";
134            }
135
136            String suggestUri = mSuggestUri + query;
137            if (DBG) Log.d(LOG_TAG, "Sending request: " + suggestUri);
138            HttpGet method = new HttpGet(suggestUri);
139            HttpResponse response = mHttpClient.execute(method);
140            if (response.getStatusLine().getStatusCode() == 200) {
141
142                /* Goto http://www.google.com/complete/search?json=true&q=foo
143                 * to see what the data format looks like. It's basically a json
144                 * array containing 4 other arrays. We only care about the middle
145                 * 2 which contain the suggestions and their popularity.
146                 */
147                JSONArray results = new JSONArray(EntityUtils.toString(response.getEntity()));
148                JSONArray suggestions = results.getJSONArray(1);
149                JSONArray popularity = results.getJSONArray(2);
150                if (DBG) Log.d(LOG_TAG, "Got " + suggestions.length() + " results");
151                return new GoogleSuggestCursor(mSource, query, suggestions, popularity);
152            } else {
153                if (DBG) Log.d(LOG_TAG, "Request failed " + response.getStatusLine());
154            }
155        } catch (UnsupportedEncodingException e) {
156            Log.w(LOG_TAG, "Error", e);
157        } catch (IOException e) {
158            Log.w(LOG_TAG, "Error", e);
159        } catch (JSONException e) {
160            Log.w(LOG_TAG, "Error", e);
161        }
162        return null;
163    }
164
165    public SuggestionCursor refreshShortcut(String shortcutId, String oldExtraData) {
166        return null;
167    }
168
169    private boolean isNetworkConnected() {
170        NetworkInfo networkInfo = getActiveNetworkInfo();
171        return networkInfo != null && networkInfo.isConnected();
172    }
173
174    private NetworkInfo getActiveNetworkInfo() {
175        ConnectivityManager connectivity =
176                (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
177        if (connectivity == null) {
178            return null;
179        }
180        return connectivity.getActiveNetworkInfo();
181    }
182
183    private static class GoogleSuggestCursor extends AbstractGoogleSourceResult {
184
185        /* Contains the actual suggestions */
186        private final JSONArray mSuggestions;
187
188        /* This contains the popularity of each suggestion
189         * i.e. 165,000 results. It's not related to sorting.
190         */
191        private final JSONArray mPopularity;
192
193        public GoogleSuggestCursor(Source source, String userQuery,
194                JSONArray suggestions, JSONArray popularity) {
195            super(source, userQuery);
196            mSuggestions = suggestions;
197            mPopularity = popularity;
198        }
199
200        @Override
201        public int getCount() {
202            return mSuggestions.length();
203        }
204
205        @Override
206        public String getSuggestionQuery() {
207            try {
208                return mSuggestions.getString(getPosition());
209            } catch (JSONException e) {
210                Log.w(LOG_TAG, "Error parsing response: " + e);
211                return null;
212            }
213        }
214
215        @Override
216        public String getSuggestionText2() {
217            try {
218                return mPopularity.getString(getPosition());
219            } catch (JSONException e) {
220                Log.w(LOG_TAG, "Error parsing response: " + e);
221                return null;
222            }
223        }
224    }
225}
226