blob: 30b4738c3821584d092b059922459012907b6a20 [file] [log] [blame]
Michael Kolb21ce4d22010-09-15 14:55:05 -07001/*
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.browser;
18
John Reck7ffb8952011-06-27 15:42:39 -070019import com.android.browser.provider.BrowserProvider2;
John Reck5e8466f2011-07-28 17:48:58 -070020import com.android.browser.provider.BrowserProvider2.OmniboxSuggestions;
Michael Kolb21ce4d22010-09-15 14:55:05 -070021import com.android.browser.search.SearchEngine;
22
23import android.app.SearchManager;
24import android.content.Context;
25import android.database.Cursor;
26import android.net.Uri;
John Reck35defff2010-11-11 14:06:45 -080027import android.os.AsyncTask;
Michael Kolb21ce4d22010-09-15 14:55:05 -070028import android.provider.BrowserContract;
Narayan Kamath5119edd2011-02-23 15:49:17 +000029import android.text.Html;
Michael Kolb21ce4d22010-09-15 14:55:05 -070030import android.text.TextUtils;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.View.OnClickListener;
34import android.view.ViewGroup;
35import android.widget.BaseAdapter;
36import android.widget.Filter;
37import android.widget.Filterable;
38import android.widget.ImageView;
Michael Kolb21ce4d22010-09-15 14:55:05 -070039import android.widget.TextView;
40
41import java.util.ArrayList;
42import java.util.List;
Michael Kolb21ce4d22010-09-15 14:55:05 -070043
44/**
45 * adapter to wrap multiple cursors for url/search completions
46 */
Michael Kolbbd2dd642011-01-13 13:01:30 -080047public class SuggestionsAdapter extends BaseAdapter implements Filterable,
48 OnClickListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -070049
Narayan Kamath5119edd2011-02-23 15:49:17 +000050 public static final int TYPE_BOOKMARK = 0;
51 public static final int TYPE_HISTORY = 1;
52 public static final int TYPE_SUGGEST_URL = 2;
53 public static final int TYPE_SEARCH = 3;
54 public static final int TYPE_SUGGEST = 4;
55 public static final int TYPE_VOICE_SEARCH = 5;
Michael Kolb21ce4d22010-09-15 14:55:05 -070056
John Reck5e8466f2011-07-28 17:48:58 -070057 private static final String[] COMBINED_PROJECTION = {
58 OmniboxSuggestions._ID,
59 OmniboxSuggestions.TITLE,
60 OmniboxSuggestions.URL,
61 OmniboxSuggestions.IS_BOOKMARK
62 };
Michael Kolb21ce4d22010-09-15 14:55:05 -070063
Michael Kolb21ce4d22010-09-15 14:55:05 -070064 private static final String COMBINED_SELECTION =
65 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
66
Narayan Kamath5119edd2011-02-23 15:49:17 +000067 final Context mContext;
68 final Filter mFilter;
John Reck35defff2010-11-11 14:06:45 -080069 SuggestionResults mMixedResults;
70 List<SuggestItem> mSuggestResults, mFilterResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -070071 List<CursorSource> mSources;
72 boolean mLandscapeMode;
Narayan Kamath5119edd2011-02-23 15:49:17 +000073 final CompletionListener mListener;
74 final int mLinesPortrait;
75 final int mLinesLandscape;
76 final Object mResultsLock = new Object();
Michael Kolbcfa3af52010-12-14 10:36:11 -080077 List<String> mVoiceResults;
John Reck117f07d2011-01-24 09:39:03 -080078 boolean mIncognitoMode;
John Reck35e9dd62011-04-25 09:01:54 -070079 BrowserSettings mSettings;
Michael Kolb21ce4d22010-09-15 14:55:05 -070080
81 interface CompletionListener {
82
83 public void onSearch(String txt);
84
Michael Kolbbd2dd642011-01-13 13:01:30 -080085 public void onSelect(String txt, int type, String extraData);
Michael Kolb21ce4d22010-09-15 14:55:05 -070086
87 }
88
89 public SuggestionsAdapter(Context ctx, CompletionListener listener) {
90 mContext = ctx;
John Reck35e9dd62011-04-25 09:01:54 -070091 mSettings = BrowserSettings.getInstance();
Michael Kolb21ce4d22010-09-15 14:55:05 -070092 mListener = listener;
93 mLinesPortrait = mContext.getResources().
94 getInteger(R.integer.max_suggest_lines_portrait);
95 mLinesLandscape = mContext.getResources().
96 getInteger(R.integer.max_suggest_lines_landscape);
Narayan Kamath5119edd2011-02-23 15:49:17 +000097
Michael Kolb21ce4d22010-09-15 14:55:05 -070098 mFilter = new SuggestFilter();
Michael Kolb21ce4d22010-09-15 14:55:05 -070099 addSource(new CombinedCursor());
100 }
101
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 void setVoiceResults(List<String> voiceResults) {
103 mVoiceResults = voiceResults;
Michael Kolba50c4462010-12-15 10:49:12 -0800104 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105 }
106
Michael Kolb21ce4d22010-09-15 14:55:05 -0700107 public void setLandscapeMode(boolean mode) {
108 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800109 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700110 }
111
Michael Kolb21ce4d22010-09-15 14:55:05 -0700112 public void addSource(CursorSource c) {
113 if (mSources == null) {
114 mSources = new ArrayList<CursorSource>(5);
115 }
116 mSources.add(c);
117 }
118
119 @Override
120 public void onClick(View v) {
John Reckad373302010-12-17 15:28:13 -0800121 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000122
Michael Kolb21ce4d22010-09-15 14:55:05 -0700123 if (R.id.icon2 == v.getId()) {
124 // replace input field text with suggestion text
Narayan Kamath5119edd2011-02-23 15:49:17 +0000125 mListener.onSearch(getSuggestionUrl(item));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700126 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000127 mListener.onSelect(getSuggestionUrl(item), item.type, item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700128 }
129 }
130
131 @Override
132 public Filter getFilter() {
133 return mFilter;
134 }
135
136 @Override
137 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800138 if (mVoiceResults != null) {
139 return mVoiceResults.size();
140 }
John Reck35defff2010-11-11 14:06:45 -0800141 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700142 }
143
144 @Override
145 public SuggestItem getItem(int position) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800146 if (mVoiceResults != null) {
Michael Kolbbd2dd642011-01-13 13:01:30 -0800147 SuggestItem item = new SuggestItem(mVoiceResults.get(position),
148 null, TYPE_VOICE_SEARCH);
149 item.extra = Integer.toString(position);
150 return item;
Michael Kolbcfa3af52010-12-14 10:36:11 -0800151 }
John Reck35defff2010-11-11 14:06:45 -0800152 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700153 return null;
154 }
John Reckad373302010-12-17 15:28:13 -0800155 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700156 }
157
158 @Override
159 public long getItemId(int position) {
John Reck1605bef2011-01-10 18:11:18 -0800160 return position;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700161 }
162
163 @Override
164 public View getView(int position, View convertView, ViewGroup parent) {
165 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800166 View view = convertView;
167 if (view == null) {
John Reckad373302010-12-17 15:28:13 -0800168 view = inflater.inflate(R.layout.suggestion_item, parent, false);
John Reck35defff2010-11-11 14:06:45 -0800169 }
John Reckad373302010-12-17 15:28:13 -0800170 bindView(view, getItem(position));
171 return view;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700172 }
173
174 private void bindView(View view, SuggestItem item) {
175 // store item for click handling
176 view.setTag(item);
177 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
178 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
179 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700180 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700181 View div = view.findViewById(R.id.divider);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000182 tv1.setText(Html.fromHtml(item.title));
John Reckad373302010-12-17 15:28:13 -0800183 if (TextUtils.isEmpty(item.url)) {
184 tv2.setVisibility(View.GONE);
185 } else {
186 tv2.setVisibility(View.VISIBLE);
187 tv2.setText(item.url);
188 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700189 int id = -1;
190 switch (item.type) {
191 case TYPE_SUGGEST:
192 case TYPE_SEARCH:
Michael Kolbbd2dd642011-01-13 13:01:30 -0800193 case TYPE_VOICE_SEARCH:
Michael Kolb21ce4d22010-09-15 14:55:05 -0700194 id = R.drawable.ic_search_category_suggest;
195 break;
196 case TYPE_BOOKMARK:
197 id = R.drawable.ic_search_category_bookmark;
198 break;
199 case TYPE_HISTORY:
200 id = R.drawable.ic_search_category_history;
201 break;
202 case TYPE_SUGGEST_URL:
203 id = R.drawable.ic_search_category_browser;
204 break;
205 default:
206 id = -1;
207 }
208 if (id != -1) {
209 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
210 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800211 ic2.setVisibility(((TYPE_SUGGEST == item.type)
212 || (TYPE_SEARCH == item.type)
213 || (TYPE_VOICE_SEARCH == item.type))
Michael Kolb21ce4d22010-09-15 14:55:05 -0700214 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700215 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700216 ic2.setOnClickListener(this);
John Reckad373302010-12-17 15:28:13 -0800217 view.findViewById(R.id.suggestion).setOnClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700218 }
219
John Reck35defff2010-11-11 14:06:45 -0800220 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700221
John Reck35defff2010-11-11 14:06:45 -0800222 @Override
223 protected List<SuggestItem> doInBackground(CharSequence... params) {
224 SuggestCursor cursor = new SuggestCursor();
225 cursor.runQuery(params[0]);
226 List<SuggestItem> results = new ArrayList<SuggestItem>();
227 int count = cursor.getCount();
228 for (int i = 0; i < count; i++) {
229 results.add(cursor.getItem());
230 cursor.moveToNext();
231 }
232 cursor.close();
233 return results;
234 }
235
236 @Override
237 protected void onPostExecute(List<SuggestItem> items) {
238 mSuggestResults = items;
239 mMixedResults = buildSuggestionResults();
240 notifyDataSetChanged();
John Reck35defff2010-11-11 14:06:45 -0800241 }
242 }
243
244 SuggestionResults buildSuggestionResults() {
245 SuggestionResults mixed = new SuggestionResults();
246 List<SuggestItem> filter, suggest;
247 synchronized (mResultsLock) {
248 filter = mFilterResults;
249 suggest = mSuggestResults;
250 }
251 if (filter != null) {
252 for (SuggestItem item : filter) {
253 mixed.addResult(item);
254 }
255 }
256 if (suggest != null) {
257 for (SuggestItem item : suggest) {
258 mixed.addResult(item);
259 }
260 }
261 return mixed;
262 }
263
264 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700265
266 @Override
267 public CharSequence convertResultToString(Object item) {
268 if (item == null) {
269 return "";
270 }
271 SuggestItem sitem = (SuggestItem) item;
272 if (sitem.title != null) {
273 return sitem.title;
274 } else {
275 return sitem.url;
276 }
277 }
278
John Reck35defff2010-11-11 14:06:45 -0800279 void startSuggestionsAsync(final CharSequence constraint) {
John Reck117f07d2011-01-24 09:39:03 -0800280 if (!mIncognitoMode) {
281 new SlowFilterTask().execute(constraint);
282 }
John Reck35defff2010-11-11 14:06:45 -0800283 }
284
Narayan Kamath5119edd2011-02-23 15:49:17 +0000285 private boolean shouldProcessEmptyQuery() {
John Reck35e9dd62011-04-25 09:01:54 -0700286 final SearchEngine searchEngine = mSettings.getSearchEngine();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000287 return searchEngine.wantsEmptyQuery();
288 }
289
Michael Kolb21ce4d22010-09-15 14:55:05 -0700290 @Override
291 protected FilterResults performFiltering(CharSequence constraint) {
292 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800293 if (mVoiceResults == null) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000294 if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800295 res.count = 0;
296 res.values = null;
297 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700298 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800299 startSuggestionsAsync(constraint);
300 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
301 if (constraint != null) {
302 for (CursorSource sc : mSources) {
303 sc.runQuery(constraint);
304 }
305 mixResults(filterResults);
306 }
307 synchronized (mResultsLock) {
308 mFilterResults = filterResults;
309 }
310 SuggestionResults mixed = buildSuggestionResults();
311 res.count = mixed.getLineCount();
312 res.values = mixed;
313 } else {
314 res.count = mVoiceResults.size();
315 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700316 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700317 return res;
318 }
319
John Reck35defff2010-11-11 14:06:45 -0800320 void mixResults(List<SuggestItem> results) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000321 int maxLines = getMaxLines();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700322 for (int i = 0; i < mSources.size(); i++) {
323 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800324 int n = Math.min(s.getCount(), maxLines);
325 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700326 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700327 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800328 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700329 more = s.moveToNext();
330 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700331 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700332 }
333
334 @Override
335 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800336 if (fresults.values instanceof SuggestionResults) {
337 mMixedResults = (SuggestionResults) fresults.values;
John Recka005cd72011-02-01 11:46:53 -0800338 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800339 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700340 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000341 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700342
Narayan Kamath5119edd2011-02-23 15:49:17 +0000343 private int getMaxLines() {
344 int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
345 maxLines = (int) Math.ceil(maxLines / 2.0);
346 return maxLines;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700347 }
348
349 /**
350 * sorted list of results of a suggestion query
351 *
352 */
353 class SuggestionResults {
354
355 ArrayList<SuggestItem> items;
356 // count per type
357 int[] counts;
358
359 SuggestionResults() {
360 items = new ArrayList<SuggestItem>(24);
361 // n of types:
362 counts = new int[5];
363 }
364
365 int getTypeCount(int type) {
366 return counts[type];
367 }
368
369 void addResult(SuggestItem item) {
370 int ix = 0;
371 while ((ix < items.size()) && (item.type >= items.get(ix).type))
372 ix++;
373 items.add(ix, item);
374 counts[item.type]++;
375 }
376
377 int getLineCount() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000378 return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700379 }
380
John Reck35defff2010-11-11 14:06:45 -0800381 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700382 public String toString() {
383 if (items == null) return null;
384 if (items.size() == 0) return "[]";
385 StringBuilder sb = new StringBuilder();
386 for (int i = 0; i < items.size(); i++) {
387 SuggestItem item = items.get(i);
388 sb.append(item.type + ": " + item.title);
389 if (i < items.size() - 1) {
390 sb.append(", ");
391 }
392 }
393 return sb.toString();
394 }
395 }
396
397 /**
398 * data object to hold suggestion values
399 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000400 public class SuggestItem {
401 public String title;
402 public String url;
403 public int type;
404 public String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700405
406 public SuggestItem(String text, String u, int t) {
407 title = text;
408 url = u;
409 type = t;
410 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800411
Michael Kolb21ce4d22010-09-15 14:55:05 -0700412 }
413
414 abstract class CursorSource {
415
416 Cursor mCursor;
417
418 boolean moveToNext() {
419 return mCursor.moveToNext();
420 }
421
422 public abstract void runQuery(CharSequence constraint);
423
424 public abstract SuggestItem getItem();
425
426 public int getCount() {
427 return (mCursor != null) ? mCursor.getCount() : 0;
428 }
429
430 public void close() {
431 if (mCursor != null) {
432 mCursor.close();
433 }
434 }
435 }
436
437 /**
438 * combined bookmark & history source
439 */
440 class CombinedCursor extends CursorSource {
441
442 @Override
443 public SuggestItem getItem() {
444 if ((mCursor != null) && (!mCursor.isAfterLast())) {
445 String title = mCursor.getString(1);
446 String url = mCursor.getString(2);
447 boolean isBookmark = (mCursor.getInt(3) == 1);
448 return new SuggestItem(getTitle(title, url), getUrl(title, url),
449 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
450 }
451 return null;
452 }
453
454 @Override
455 public void runQuery(CharSequence constraint) {
456 // constraint != null
457 if (mCursor != null) {
458 mCursor.close();
459 }
460 String like = constraint + "%";
461 String[] args = null;
462 String selection = null;
463 if (like.startsWith("http") || like.startsWith("file")) {
464 args = new String[1];
465 args[0] = like;
466 selection = "url LIKE ?";
467 } else {
468 args = new String[5];
469 args[0] = "http://" + like;
470 args[1] = "http://www." + like;
471 args[2] = "https://" + like;
472 args[3] = "https://www." + like;
473 // To match against titles.
474 args[4] = like;
475 selection = COMBINED_SELECTION;
476 }
John Reck5e8466f2011-07-28 17:48:58 -0700477 Uri.Builder ub = OmniboxSuggestions.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700478 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
John Reckad373302010-12-17 15:28:13 -0800479 Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
John Reck7ffb8952011-06-27 15:42:39 -0700480 ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY,
John Reck5e8466f2011-07-28 17:48:58 -0700481 OmniboxSuggestions.URL);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700482 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700483 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
John Reck5e8466f2011-07-28 17:48:58 -0700484 selection, (constraint != null) ? args : null, null);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700485 if (mCursor != null) {
486 mCursor.moveToFirst();
487 }
488 }
489
490 /**
491 * Provides the title (text line 1) for a browser suggestion, which should be the
492 * webpage title. If the webpage title is empty, returns the stripped url instead.
493 *
494 * @return the title string to use
495 */
496 private String getTitle(String title, String url) {
497 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700498 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700499 }
500 return title;
501 }
502
503 /**
504 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
505 * webpage url. If the webpage title is empty, then the url should go in the title
506 * instead, and the subtitle should be empty, so this would return null.
507 *
508 * @return the subtitle string to use, or null if none
509 */
510 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700511 if (TextUtils.isEmpty(title)
512 || TextUtils.getTrimmedLength(title) == 0
513 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700514 return null;
515 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700516 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700517 }
518 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700519 }
520
Michael Kolb21ce4d22010-09-15 14:55:05 -0700521 class SuggestCursor extends CursorSource {
522
523 @Override
524 public SuggestItem getItem() {
525 if (mCursor != null) {
526 String title = mCursor.getString(
527 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
528 String text2 = mCursor.getString(
529 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
530 String url = mCursor.getString(
531 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
532 String uri = mCursor.getString(
533 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
534 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800535 SuggestItem item = new SuggestItem(title, url, type);
536 item.extra = mCursor.getString(
537 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
538 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700539 }
540 return null;
541 }
542
543 @Override
544 public void runQuery(CharSequence constraint) {
545 if (mCursor != null) {
546 mCursor.close();
547 }
John Reck35e9dd62011-04-25 09:01:54 -0700548 SearchEngine searchEngine = mSettings.getSearchEngine();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700549 if (!TextUtils.isEmpty(constraint)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700550 if (searchEngine != null && searchEngine.supportsSuggestions()) {
551 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
552 if (mCursor != null) {
553 mCursor.moveToFirst();
554 }
555 }
556 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000557 if (searchEngine.wantsEmptyQuery()) {
558 mCursor = searchEngine.getSuggestions(mContext, "");
559 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700560 mCursor = null;
561 }
562 }
563
564 }
565
Narayan Kamath5119edd2011-02-23 15:49:17 +0000566 private boolean useInstant() {
John Reck35e9dd62011-04-25 09:01:54 -0700567 return mSettings.useInstantSearch();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000568 }
569
John Reck35defff2010-11-11 14:06:45 -0800570 public void clearCache() {
571 mFilterResults = null;
572 mSuggestResults = null;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000573 notifyDataSetInvalidated();
John Reck35defff2010-11-11 14:06:45 -0800574 }
575
John Reck117f07d2011-01-24 09:39:03 -0800576 public void setIncognitoMode(boolean incognito) {
577 mIncognitoMode = incognito;
578 clearCache();
579 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000580
581 static String getSuggestionTitle(SuggestItem item) {
582 // There must be a better way to strip HTML from things.
583 // This method is used in multiple places. It is also more
584 // expensive than a standard html escaper.
585 return (item.title != null) ? Html.fromHtml(item.title).toString() : null;
586 }
587
588 static String getSuggestionUrl(SuggestItem item) {
589 final String title = SuggestionsAdapter.getSuggestionTitle(item);
590
591 if (TextUtils.isEmpty(item.url)) {
592 return title;
593 }
594
595 return item.url;
596 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700597}