summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/InstantSearchEngine.java
blob: e2e9c8ae4f3bf8fe35452703364419f9981896bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.browser;

import com.android.browser.Controller;
import com.android.browser.R;
import com.android.browser.UI.DropdownChangeListener;
import com.android.browser.provider.BrowserProvider;
import com.android.browser.search.SearchEngine;

import android.app.SearchManager;
import android.content.Context;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.LruCache;
import android.webkit.SearchBox;
import android.webkit.WebView;

import java.util.Collections;
import java.util.List;

public class InstantSearchEngine implements SearchEngine, DropdownChangeListener {
    private static final String TAG = "Browser.InstantSearchEngine";
    private static final boolean DBG = false;

    private Controller mController;
    private SearchBox mSearchBox;
    private final BrowserSearchboxListener mListener = new BrowserSearchboxListener();
    private int mHeight;

    private String mInstantBaseUrl;
    private final Context mContext;
    // Used for startSearch( ) calls if for some reason instant
    // is off, or no searchbox is present.
    private final SearchEngine mWrapped;

    public InstantSearchEngine(Context context, SearchEngine wrapped) {
        mContext = context;
        mWrapped = wrapped;
    }

    public void setController(Controller controller) {
        mController = controller;
    }

    @Override
    public String getName() {
        return SearchEngine.GOOGLE;
    }

    @Override
    public CharSequence getLabel() {
        return mContext.getResources().getString(R.string.instant_search_label);
    }

    @Override
    public void startSearch(Context context, String query, Bundle appData, String extraData) {
        if (DBG) Log.d(TAG, "startSearch(" + query + ")");

        switchSearchboxIfNeeded();

        // If for some reason we are in a bad state, ensure that the
        // user gets default search results at the very least.
        if (mSearchBox == null || !isInstantPage()) {
            mWrapped.startSearch(context, query, appData, extraData);
            return;
        }

        mSearchBox.setQuery(query);
        mSearchBox.setVerbatim(true);
        mSearchBox.onsubmit();
    }

    private final class BrowserSearchboxListener implements SearchBox.SearchBoxListener {
        /*
         * The maximum number of out of order suggestions we accept
         * before giving up the wait.
         */
        private static final int MAX_OUT_OF_ORDER = 5;

        /*
         * We wait for suggestions in increments of 600ms. This is primarily to
         * guard against suggestions arriving out of order.
         */
        private static final int WAIT_INCREMENT_MS = 600;

        /*
         * A cache of suggestions received, keyed by the queries they were
         * received for.
         */
        private final LruCache<String, List<String>> mSuggestions =
                new LruCache<String, List<String>>(20);

        /*
         * The last set of suggestions received. We use this reduce UI flicker
         * in case there is a delay in recieving suggestions.
         */
        private List<String> mLatestSuggestion = Collections.emptyList();

        @Override
        public synchronized void onSuggestionsReceived(String query, List<String> suggestions) {
            if (DBG) Log.d(TAG, "onSuggestionsReceived(" + query + ")");

            if (!TextUtils.isEmpty(query)) {
                mSuggestions.put(query, suggestions);
                mLatestSuggestion = suggestions;
            }

            notifyAll();
        }

        public synchronized List<String> tryWaitForSuggestions(String query) {
            if (DBG) Log.d(TAG, "tryWait(" + query + ")");

            int numWaitReturns = 0;

            // This slightly unusual waiting construct is used to safeguard
            // to some extent against suggestions arriving out of order. We
            // wait for upto 5 notifyAll( ) calls to check if we received
            // suggestions for a given query.
            while (mSuggestions.get(query) == null)  {
                try {
                    wait(WAIT_INCREMENT_MS);
                    ++numWaitReturns;
                    if (numWaitReturns > MAX_OUT_OF_ORDER) {
                        // We've waited too long for suggestions to be returned.
                        // return the last available suggestion.
                        break;
                    }
                } catch (InterruptedException e) {
                    return Collections.emptyList();
                }
            }

            List<String> suggestions = mSuggestions.get(query);
            if (suggestions == null) {
                return mLatestSuggestion;
            }

            return suggestions;
        }

        public synchronized void clear() {
            mSuggestions.evictAll();
        }
    }

    private WebView getCurrentWebview() {
        if (mController != null) {
            return mController.getTabControl().getCurrentTopWebView();
        }

        return null;
    }

    /**
     * Attaches the searchbox to the right browser page, i.e, the currently
     * visible tab.
     */
    private void switchSearchboxIfNeeded() {
        final WebView current = getCurrentWebview();
        if (current == null) {
            return;
        }

        final SearchBox searchBox = current.getSearchBox();
        if (searchBox != mSearchBox) {
            if (mSearchBox != null) {
                mSearchBox.removeSearchBoxListener(mListener);
                mListener.clear();
            }
            mSearchBox = searchBox;
            if (mSearchBox != null) {
                mSearchBox.addSearchBoxListener(mListener);
            }
        }
    }

    private boolean isInstantPage() {
        final WebView current = getCurrentWebview();
        if (current == null) {
            return false;
        }

        final String currentUrl = current.getUrl();

        if (currentUrl != null) {
            Uri uri = Uri.parse(currentUrl);
            final String host = uri.getHost();
            final String path = uri.getPath();

            // Is there a utility class that does this ?
            if (path != null && host != null) {
                return host.startsWith("www.google.") &&
                        (path.startsWith("/search") || path.startsWith("/webhp"));
            }
            return false;
        }

        return false;
    }

    private void loadInstantPage() {
        mController.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final WebView current = getCurrentWebview();
                if (current != null) {
                    current.loadUrl(getInstantBaseUrl());
                }
            }
        });
    }

    /**
     * Queries for a given search term and returns a cursor containing
     * suggestions ordered by best match.
     */
    @Override
    public Cursor getSuggestions(Context context, String query) {
        if (DBG) Log.d(TAG, "getSuggestions(" + query + ")");
        if (query == null) {
            return null;
        }

        if (!isInstantPage()) {
            loadInstantPage();
        }

        switchSearchboxIfNeeded();

        mController.registerDropdownChangeListener(this);

        if (mSearchBox == null) {
            return mWrapped.getSuggestions(context, query);
        }

        mSearchBox.setDimensions(0, 0, 0, mHeight);
        mSearchBox.onresize();

        if (TextUtils.isEmpty(query)) {
            // To force the SRP to render an empty (no results) page.
            mSearchBox.setVerbatim(true);
        } else {
            mSearchBox.setVerbatim(false);
        }
        mSearchBox.setQuery(query);
        mSearchBox.onchange();

        // Don't bother waiting for suggestions for an empty query. We still
        // set the query so that the SRP clears itself.
        if (TextUtils.isEmpty(query)) {
            return new SuggestionsCursor(Collections.<String>emptyList());
        } else {
            return new SuggestionsCursor(mListener.tryWaitForSuggestions(query));
        }
    }

    @Override
    public boolean supportsSuggestions() {
        return true;
    }

    @Override
    public void close() {
        if (mController != null) {
            mController.registerDropdownChangeListener(null);
        }
        if (mSearchBox != null) {
            mSearchBox.removeSearchBoxListener(mListener);
        }
        mListener.clear();
        mWrapped.close();
    }

    @Override
    public boolean supportsVoiceSearch() {
        return false;
    }

    @Override
    public String toString() {
        return "InstantSearchEngine {" + hashCode() + "}";
    }

    @Override
    public boolean wantsEmptyQuery() {
        return true;
    }

    private int rescaleHeight(int height) {
        final WebView current = getCurrentWebview();
        if (current == null) {
            return 0;
        }

        final float scale = current.getScale();
        if (scale != 0) {
            return (int) (height / scale);
        }

        return height;
    }

    @Override
    public void onNewDropdownDimensions(int height) {
        final int rescaledHeight = rescaleHeight(height);

        if (rescaledHeight != mHeight) {
            mHeight = rescaledHeight;
            if (mSearchBox != null) {
                mSearchBox.setDimensions(0, 0, 0, rescaledHeight);
                mSearchBox.onresize();
            }
        }
    }

    private String getInstantBaseUrl() {
        if (mInstantBaseUrl == null) {
            String url = mContext.getResources().getString(R.string.instant_base);
            if (url.indexOf("{CID}") != -1) {
                url = url.replace("{CID}",
                        BrowserProvider.getClientId(mContext.getContentResolver()));
            }
            mInstantBaseUrl = url;
        }

        return mInstantBaseUrl;
    }

    // Indices of the columns in the below arrays.
    private static final int COLUMN_INDEX_ID = 0;
    private static final int COLUMN_INDEX_QUERY = 1;
    private static final int COLUMN_INDEX_ICON = 2;
    private static final int COLUMN_INDEX_TEXT_1 = 3;

    private static final String[] COLUMNS_WITHOUT_DESCRIPTION = new String[] {
        "_id",
        SearchManager.SUGGEST_COLUMN_QUERY,
        SearchManager.SUGGEST_COLUMN_ICON_1,
        SearchManager.SUGGEST_COLUMN_TEXT_1,
    };

    private static class SuggestionsCursor extends AbstractCursor {
        private final List<String> mSuggestions;

        public SuggestionsCursor(List<String> suggestions) {
            mSuggestions = suggestions;
        }

        @Override
        public int getCount() {
            return mSuggestions.size();
        }

        @Override
        public String[] getColumnNames() {
            return COLUMNS_WITHOUT_DESCRIPTION;
        }

        private String format(String suggestion) {
            if (TextUtils.isEmpty(suggestion)) {
                return "";
            }
            return suggestion;
        }

        @Override
        public String getString(int column) {
            if (mPos >= 0 && mPos < mSuggestions.size()) {
              if ((column == COLUMN_INDEX_QUERY) || (column == COLUMN_INDEX_TEXT_1)) {
                  return format(mSuggestions.get(mPos));
              } else if (column == COLUMN_INDEX_ICON) {
                  return String.valueOf(R.drawable.magnifying_glass);
              }
            }
            return null;
        }

        @Override
        public double getDouble(int column) {
            throw new UnsupportedOperationException();
        }

        @Override
        public float getFloat(int column) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int getInt(int column) {
            if (column == COLUMN_INDEX_ID) {
                return mPos;
            }
            throw new UnsupportedOperationException();
        }

        @Override
        public long getLong(int column) {
            throw new UnsupportedOperationException();
        }

        @Override
        public short getShort(int column) {
          throw new UnsupportedOperationException();
        }

        @Override
        public boolean isNull(int column) {
            throw new UnsupportedOperationException();
        }
    }
}