Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | package com.android.browser; |
| 17 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 18 | import com.android.browser.Controller; |
| 19 | import com.android.browser.R; |
| 20 | import com.android.browser.UI.DropdownChangeListener; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 21 | import com.android.browser.search.SearchEngine; |
| 22 | |
| 23 | import android.app.SearchManager; |
| 24 | import android.content.Context; |
| 25 | import android.database.AbstractCursor; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.text.TextUtils; |
| 30 | import android.util.Log; |
| 31 | import android.util.LruCache; |
| 32 | import android.webkit.SearchBox; |
| 33 | import android.webkit.WebView; |
| 34 | |
| 35 | import java.util.Collections; |
| 36 | import java.util.List; |
| 37 | |
| 38 | public class InstantSearchEngine implements SearchEngine, DropdownChangeListener { |
| 39 | private static final String TAG = "Browser.InstantSearchEngine"; |
| 40 | private static final boolean DBG = false; |
| 41 | |
| 42 | private Controller mController; |
| 43 | private SearchBox mSearchBox; |
| 44 | private final BrowserSearchboxListener mListener = new BrowserSearchboxListener(); |
| 45 | private int mHeight; |
| 46 | |
| 47 | private String mInstantBaseUrl; |
| 48 | private final Context mContext; |
| 49 | // Used for startSearch( ) calls if for some reason instant |
| 50 | // is off, or no searchbox is present. |
| 51 | private final SearchEngine mWrapped; |
| 52 | |
| 53 | public InstantSearchEngine(Context context, SearchEngine wrapped) { |
| 54 | mContext = context; |
| 55 | mWrapped = wrapped; |
| 56 | } |
| 57 | |
| 58 | public void setController(Controller controller) { |
| 59 | mController = controller; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String getName() { |
| 64 | return SearchEngine.GOOGLE; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public CharSequence getLabel() { |
| 69 | return mContext.getResources().getString(R.string.instant_search_label); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void startSearch(Context context, String query, Bundle appData, String extraData) { |
| 74 | if (DBG) Log.d(TAG, "startSearch(" + query + ")"); |
| 75 | |
| 76 | switchSearchboxIfNeeded(); |
| 77 | |
| 78 | // If for some reason we are in a bad state, ensure that the |
| 79 | // user gets default search results at the very least. |
Narayan Kamath | 41feb05 | 2011-03-04 18:57:30 +0000 | [diff] [blame] | 80 | if (mSearchBox == null || !isInstantPage()) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 81 | mWrapped.startSearch(context, query, appData, extraData); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | mSearchBox.setQuery(query); |
| 86 | mSearchBox.setVerbatim(true); |
| 87 | mSearchBox.onsubmit(); |
| 88 | } |
| 89 | |
| 90 | private final class BrowserSearchboxListener implements SearchBox.SearchBoxListener { |
| 91 | /* |
| 92 | * The maximum number of out of order suggestions we accept |
| 93 | * before giving up the wait. |
| 94 | */ |
| 95 | private static final int MAX_OUT_OF_ORDER = 5; |
| 96 | |
| 97 | /* |
| 98 | * We wait for suggestions in increments of 600ms. This is primarily to |
| 99 | * guard against suggestions arriving out of order. |
| 100 | */ |
| 101 | private static final int WAIT_INCREMENT_MS = 600; |
| 102 | |
| 103 | /* |
| 104 | * A cache of suggestions received, keyed by the queries they were |
| 105 | * received for. |
| 106 | */ |
| 107 | private final LruCache<String, List<String>> mSuggestions = |
| 108 | new LruCache<String, List<String>>(20); |
| 109 | |
| 110 | /* |
| 111 | * The last set of suggestions received. We use this reduce UI flicker |
| 112 | * in case there is a delay in recieving suggestions. |
| 113 | */ |
| 114 | private List<String> mLatestSuggestion = Collections.emptyList(); |
| 115 | |
| 116 | @Override |
| 117 | public synchronized void onSuggestionsReceived(String query, List<String> suggestions) { |
| 118 | if (DBG) Log.d(TAG, "onSuggestionsReceived(" + query + ")"); |
| 119 | |
| 120 | if (!TextUtils.isEmpty(query)) { |
| 121 | mSuggestions.put(query, suggestions); |
| 122 | mLatestSuggestion = suggestions; |
| 123 | } |
| 124 | |
| 125 | notifyAll(); |
| 126 | } |
| 127 | |
| 128 | public synchronized List<String> tryWaitForSuggestions(String query) { |
| 129 | if (DBG) Log.d(TAG, "tryWait(" + query + ")"); |
| 130 | |
| 131 | int numWaitReturns = 0; |
| 132 | |
| 133 | // This slightly unusual waiting construct is used to safeguard |
| 134 | // to some extent against suggestions arriving out of order. We |
| 135 | // wait for upto 5 notifyAll( ) calls to check if we received |
| 136 | // suggestions for a given query. |
| 137 | while (mSuggestions.get(query) == null) { |
| 138 | try { |
| 139 | wait(WAIT_INCREMENT_MS); |
| 140 | ++numWaitReturns; |
| 141 | if (numWaitReturns > MAX_OUT_OF_ORDER) { |
| 142 | // We've waited too long for suggestions to be returned. |
| 143 | // return the last available suggestion. |
| 144 | break; |
| 145 | } |
| 146 | } catch (InterruptedException e) { |
| 147 | return Collections.emptyList(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | List<String> suggestions = mSuggestions.get(query); |
| 152 | if (suggestions == null) { |
| 153 | return mLatestSuggestion; |
| 154 | } |
| 155 | |
| 156 | return suggestions; |
| 157 | } |
| 158 | |
| 159 | public synchronized void clear() { |
| 160 | mSuggestions.evictAll(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | private WebView getCurrentWebview() { |
| 165 | if (mController != null) { |
| 166 | return mController.getTabControl().getCurrentTopWebView(); |
| 167 | } |
| 168 | |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Attaches the searchbox to the right browser page, i.e, the currently |
| 174 | * visible tab. |
| 175 | */ |
| 176 | private void switchSearchboxIfNeeded() { |
| 177 | final SearchBox searchBox = getCurrentWebview().getSearchBox(); |
| 178 | if (searchBox != mSearchBox) { |
| 179 | if (mSearchBox != null) { |
| 180 | mSearchBox.removeSearchBoxListener(mListener); |
| 181 | mListener.clear(); |
| 182 | } |
| 183 | mSearchBox = searchBox; |
Michael Kolb | 00a2275 | 2011-03-03 17:13:30 -0800 | [diff] [blame] | 184 | if (mSearchBox != null) { |
| 185 | mSearchBox.addSearchBoxListener(mListener); |
| 186 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | private boolean isInstantPage() { |
| 191 | String currentUrl = getCurrentWebview().getUrl(); |
| 192 | |
| 193 | if (currentUrl != null) { |
| 194 | Uri uri = Uri.parse(currentUrl); |
| 195 | final String host = uri.getHost(); |
| 196 | final String path = uri.getPath(); |
| 197 | |
| 198 | // Is there a utility class that does this ? |
| 199 | if (path != null && host != null) { |
| 200 | return host.startsWith("www.google.") && |
| 201 | (path.startsWith("/search") || path.startsWith("/webhp")); |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | private void loadInstantPage() { |
| 210 | mController.getActivity().runOnUiThread(new Runnable() { |
| 211 | @Override |
| 212 | public void run() { |
| 213 | getCurrentWebview().loadUrl(getInstantBaseUrl()); |
| 214 | } |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Queries for a given search term and returns a cursor containing |
| 220 | * suggestions ordered by best match. |
| 221 | */ |
| 222 | @Override |
| 223 | public Cursor getSuggestions(Context context, String query) { |
| 224 | if (DBG) Log.d(TAG, "getSuggestions(" + query + ")"); |
| 225 | if (query == null) { |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | if (!isInstantPage()) { |
| 230 | loadInstantPage(); |
| 231 | } |
| 232 | |
| 233 | switchSearchboxIfNeeded(); |
| 234 | |
| 235 | mController.registerDropdownChangeListener(this); |
| 236 | |
Narayan Kamath | 41feb05 | 2011-03-04 18:57:30 +0000 | [diff] [blame] | 237 | if (mSearchBox == null) { |
| 238 | return mWrapped.getSuggestions(context, query); |
| 239 | } |
| 240 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 241 | mSearchBox.setDimensions(0, 0, 0, mHeight); |
| 242 | mSearchBox.onresize(); |
| 243 | |
| 244 | if (TextUtils.isEmpty(query)) { |
| 245 | // To force the SRP to render an empty (no results) page. |
| 246 | mSearchBox.setVerbatim(true); |
| 247 | } else { |
| 248 | mSearchBox.setVerbatim(false); |
| 249 | } |
| 250 | mSearchBox.setQuery(query); |
| 251 | mSearchBox.onchange(); |
| 252 | |
| 253 | // Don't bother waiting for suggestions for an empty query. We still |
| 254 | // set the query so that the SRP clears itself. |
| 255 | if (TextUtils.isEmpty(query)) { |
| 256 | return new SuggestionsCursor(Collections.<String>emptyList()); |
| 257 | } else { |
| 258 | return new SuggestionsCursor(mListener.tryWaitForSuggestions(query)); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public boolean supportsSuggestions() { |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void close() { |
| 269 | if (mController != null) { |
| 270 | mController.registerDropdownChangeListener(null); |
| 271 | } |
| 272 | if (mSearchBox != null) { |
| 273 | mSearchBox.removeSearchBoxListener(mListener); |
| 274 | } |
| 275 | mListener.clear(); |
| 276 | mWrapped.close(); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean supportsVoiceSearch() { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public String toString() { |
| 286 | return "InstantSearchEngine {" + hashCode() + "}"; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public boolean wantsEmptyQuery() { |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | private int rescaleHeight(int height) { |
| 295 | final float scale = getCurrentWebview().getScale(); |
| 296 | if (scale != 0) { |
| 297 | return (int) (height / scale); |
| 298 | } |
| 299 | |
| 300 | return height; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void onNewDropdownDimensions(int height) { |
| 305 | final int rescaledHeight = rescaleHeight(height); |
| 306 | |
| 307 | if (rescaledHeight != mHeight) { |
| 308 | mHeight = rescaledHeight; |
| 309 | mSearchBox.setDimensions(0, 0, 0, rescaledHeight); |
| 310 | mSearchBox.onresize(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | private String getInstantBaseUrl() { |
| 315 | if (mInstantBaseUrl == null) { |
| 316 | String url = mContext.getResources().getString(R.string.instant_base); |
| 317 | if (url.indexOf("{CID}") != -1) { |
| 318 | url = url.replace("{CID}", |
| 319 | BrowserProvider.getClientId(mContext.getContentResolver())); |
| 320 | } |
| 321 | mInstantBaseUrl = url; |
| 322 | } |
| 323 | |
| 324 | return mInstantBaseUrl; |
| 325 | } |
| 326 | |
| 327 | // Indices of the columns in the below arrays. |
| 328 | private static final int COLUMN_INDEX_ID = 0; |
| 329 | private static final int COLUMN_INDEX_QUERY = 1; |
| 330 | private static final int COLUMN_INDEX_ICON = 2; |
| 331 | private static final int COLUMN_INDEX_TEXT_1 = 3; |
| 332 | |
| 333 | private static final String[] COLUMNS_WITHOUT_DESCRIPTION = new String[] { |
| 334 | "_id", |
| 335 | SearchManager.SUGGEST_COLUMN_QUERY, |
| 336 | SearchManager.SUGGEST_COLUMN_ICON_1, |
| 337 | SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 338 | }; |
| 339 | |
| 340 | private static class SuggestionsCursor extends AbstractCursor { |
| 341 | private final List<String> mSuggestions; |
| 342 | |
| 343 | public SuggestionsCursor(List<String> suggestions) { |
| 344 | mSuggestions = suggestions; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public int getCount() { |
| 349 | return mSuggestions.size(); |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public String[] getColumnNames() { |
| 354 | return COLUMNS_WITHOUT_DESCRIPTION; |
| 355 | } |
| 356 | |
| 357 | private String format(String suggestion) { |
| 358 | if (TextUtils.isEmpty(suggestion)) { |
| 359 | return ""; |
| 360 | } |
| 361 | return suggestion; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public String getString(int column) { |
| 366 | if (mPos >= 0 && mPos < mSuggestions.size()) { |
| 367 | if ((column == COLUMN_INDEX_QUERY) || (column == COLUMN_INDEX_TEXT_1)) { |
| 368 | return format(mSuggestions.get(mPos)); |
| 369 | } else if (column == COLUMN_INDEX_ICON) { |
| 370 | return String.valueOf(R.drawable.magnifying_glass); |
| 371 | } |
| 372 | } |
| 373 | return null; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public double getDouble(int column) { |
| 378 | throw new UnsupportedOperationException(); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public float getFloat(int column) { |
| 383 | throw new UnsupportedOperationException(); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public int getInt(int column) { |
| 388 | if (column == COLUMN_INDEX_ID) { |
| 389 | return mPos; |
| 390 | } |
| 391 | throw new UnsupportedOperationException(); |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public long getLong(int column) { |
| 396 | throw new UnsupportedOperationException(); |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public short getShort(int column) { |
| 401 | throw new UnsupportedOperationException(); |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public boolean isNull(int column) { |
| 406 | throw new UnsupportedOperationException(); |
| 407 | } |
| 408 | } |
| 409 | } |