GoogleSuggestClient.java revision f3f70e5ae88f06ff6dabdec9e7c71a19ca1e7108
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 extends AbstractGoogleSource {
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 HttpClient mHttpClient;
62
63    public GoogleSuggestClient(Context context) {
64        super(context);
65        mHttpClient = new DefaultHttpClient();
66        HttpParams params = mHttpClient.getParams();
67        HttpProtocolParams.setUserAgent(params, USER_AGENT);
68        params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);
69
70        // NOTE:  Do not look up the resource here;  Localization changes may not have completed
71        // yet (e.g. we may still be reading the SIM card).
72        mSuggestUri = null;
73    }
74
75    @Override
76    public ComponentName getIntentComponent() {
77        return new ComponentName(getContext(), GoogleSearch.class);
78    }
79
80    @Override
81    public SourceResult queryInternal(String query) {
82        return query(query);
83    }
84
85    @Override
86    public SourceResult queryExternal(String query) {
87        return query(query);
88    }
89
90    /**
91     * Queries for a given search term and returns a cursor containing
92     * suggestions ordered by best match.
93     */
94    private 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(this, 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    @Override
166    public SuggestionCursor refreshShortcut(String shortcutId, String oldExtraData) {
167        return null;
168    }
169
170    private boolean isNetworkConnected() {
171        NetworkInfo networkInfo = getActiveNetworkInfo();
172        return networkInfo != null && networkInfo.isConnected();
173    }
174
175    private NetworkInfo getActiveNetworkInfo() {
176        ConnectivityManager connectivity =
177                (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
178        if (connectivity == null) {
179            return null;
180        }
181        return connectivity.getActiveNetworkInfo();
182    }
183
184    private static class GoogleSuggestCursor extends AbstractGoogleSourceResult {
185
186        /* Contains the actual suggestions */
187        private final JSONArray mSuggestions;
188
189        /* This contains the popularity of each suggestion
190         * i.e. 165,000 results. It's not related to sorting.
191         */
192        private final JSONArray mPopularity;
193
194        public GoogleSuggestCursor(Source source, String userQuery,
195                JSONArray suggestions, JSONArray popularity) {
196            super(source, userQuery);
197            mSuggestions = suggestions;
198            mPopularity = popularity;
199        }
200
201        @Override
202        public int getCount() {
203            return mSuggestions.length();
204        }
205
206        @Override
207        public String getSuggestionQuery() {
208            try {
209                return mSuggestions.getString(getPosition());
210            } catch (JSONException e) {
211                Log.w(LOG_TAG, "Error parsing response: " + e);
212                return null;
213            }
214        }
215
216        @Override
217        public String getSuggestionText2() {
218            try {
219                return mPopularity.getString(getPosition());
220            } catch (JSONException e) {
221                Log.w(LOG_TAG, "Error parsing response: " + e);
222                return null;
223            }
224        }
225    }
226}
227