GoogleSuggestClient.java revision c4e96e6c09422ab54984144ad618b2ecefb4b891
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            if (mSuggestUri == null) {
105                Locale l = Locale.getDefault();
106                String language = GoogleSearch.getLanguage(l);
107                mSuggestUri = getContext().getResources().getString(R.string.google_suggest_base,
108                                                                    language)
109                        + "json=true&q=";
110            }
111
112            String suggestUri = mSuggestUri + query;
113            if (DBG) Log.d(LOG_TAG, "Sending request: " + suggestUri);
114            HttpGet method = new HttpGet(suggestUri);
115            HttpResponse response = mHttpClient.execute(method);
116            if (response.getStatusLine().getStatusCode() == 200) {
117
118                /* Goto http://www.google.com/complete/search?json=true&q=foo
119                 * to see what the data format looks like. It's basically a json
120                 * array containing 4 other arrays. We only care about the middle
121                 * 2 which contain the suggestions and their popularity.
122                 */
123                JSONArray results = new JSONArray(EntityUtils.toString(response.getEntity()));
124                JSONArray suggestions = results.getJSONArray(1);
125                JSONArray popularity = results.getJSONArray(2);
126                if (DBG) Log.d(LOG_TAG, "Got " + suggestions.length() + " results");
127                return new GoogleSuggestCursor(this, query, suggestions, popularity);
128            } else {
129                if (DBG) Log.d(LOG_TAG, "Request failed " + response.getStatusLine());
130            }
131        } catch (UnsupportedEncodingException e) {
132            Log.w(LOG_TAG, "Error", e);
133        } catch (IOException e) {
134            Log.w(LOG_TAG, "Error", e);
135        } catch (JSONException e) {
136            Log.w(LOG_TAG, "Error", e);
137        }
138        return null;
139    }
140
141    @Override
142    public SuggestionCursor refreshShortcut(String shortcutId, String oldExtraData) {
143        return null;
144    }
145
146    private boolean isNetworkConnected() {
147        NetworkInfo networkInfo = getActiveNetworkInfo();
148        return networkInfo != null && networkInfo.isConnected();
149    }
150
151    private NetworkInfo getActiveNetworkInfo() {
152        ConnectivityManager connectivity =
153                (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
154        if (connectivity == null) {
155            return null;
156        }
157        return connectivity.getActiveNetworkInfo();
158    }
159
160    private static class GoogleSuggestCursor extends AbstractGoogleSourceResult {
161
162        /* Contains the actual suggestions */
163        private final JSONArray mSuggestions;
164
165        /* This contains the popularity of each suggestion
166         * i.e. 165,000 results. It's not related to sorting.
167         */
168        private final JSONArray mPopularity;
169
170        public GoogleSuggestCursor(Source source, String userQuery,
171                JSONArray suggestions, JSONArray popularity) {
172            super(source, userQuery);
173            mSuggestions = suggestions;
174            mPopularity = popularity;
175        }
176
177        @Override
178        public int getCount() {
179            return mSuggestions.length();
180        }
181
182        @Override
183        public String getSuggestionQuery() {
184            try {
185                return mSuggestions.getString(getPosition());
186            } catch (JSONException e) {
187                Log.w(LOG_TAG, "Error parsing response: " + e);
188                return null;
189            }
190        }
191
192        @Override
193        public String getSuggestionText2() {
194            try {
195                return mPopularity.getString(getPosition());
196            } catch (JSONException e) {
197                Log.w(LOG_TAG, "Error parsing response: " + e);
198                return null;
199            }
200        }
201    }
202}
203