blob: ecdaa15d9788c295a8bcb4afd6c2553717c4e695 [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
19import com.android.browser.search.SearchEngine;
20
21import android.app.SearchManager;
22import android.content.Context;
23import android.database.Cursor;
24import android.net.Uri;
John Reck35defff2010-11-11 14:06:45 -080025import android.os.AsyncTask;
Michael Kolb21ce4d22010-09-15 14:55:05 -070026import android.provider.BrowserContract;
Narayan Kamath5119edd2011-02-23 15:49:17 +000027import android.text.Html;
Michael Kolb21ce4d22010-09-15 14:55:05 -070028import android.text.TextUtils;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.View.OnClickListener;
32import android.view.ViewGroup;
33import android.widget.BaseAdapter;
34import android.widget.Filter;
35import android.widget.Filterable;
36import android.widget.ImageView;
Michael Kolb21ce4d22010-09-15 14:55:05 -070037import android.widget.TextView;
38
39import java.util.ArrayList;
40import java.util.List;
Michael Kolb21ce4d22010-09-15 14:55:05 -070041
42/**
43 * adapter to wrap multiple cursors for url/search completions
44 */
Michael Kolbbd2dd642011-01-13 13:01:30 -080045public class SuggestionsAdapter extends BaseAdapter implements Filterable,
46 OnClickListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -070047
Narayan Kamath5119edd2011-02-23 15:49:17 +000048 public static final int TYPE_BOOKMARK = 0;
49 public static final int TYPE_HISTORY = 1;
50 public static final int TYPE_SUGGEST_URL = 2;
51 public static final int TYPE_SEARCH = 3;
52 public static final int TYPE_SUGGEST = 4;
53 public static final int TYPE_VOICE_SEARCH = 5;
Michael Kolb21ce4d22010-09-15 14:55:05 -070054
55 private static final String[] COMBINED_PROJECTION =
56 {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
57 BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
58
Michael Kolb21ce4d22010-09-15 14:55:05 -070059 private static final String COMBINED_SELECTION =
60 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
61
Narayan Kamath5119edd2011-02-23 15:49:17 +000062 final Context mContext;
63 final Filter mFilter;
John Reck35defff2010-11-11 14:06:45 -080064 SuggestionResults mMixedResults;
65 List<SuggestItem> mSuggestResults, mFilterResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -070066 List<CursorSource> mSources;
67 boolean mLandscapeMode;
Narayan Kamath5119edd2011-02-23 15:49:17 +000068 final CompletionListener mListener;
69 final int mLinesPortrait;
70 final int mLinesLandscape;
71 final Object mResultsLock = new Object();
Michael Kolbcfa3af52010-12-14 10:36:11 -080072 List<String> mVoiceResults;
John Reck117f07d2011-01-24 09:39:03 -080073 boolean mIncognitoMode;
Michael Kolb21ce4d22010-09-15 14:55:05 -070074
75 interface CompletionListener {
76
77 public void onSearch(String txt);
78
Michael Kolbbd2dd642011-01-13 13:01:30 -080079 public void onSelect(String txt, int type, String extraData);
Michael Kolb21ce4d22010-09-15 14:55:05 -070080
81 }
82
83 public SuggestionsAdapter(Context ctx, CompletionListener listener) {
84 mContext = ctx;
85 mListener = listener;
86 mLinesPortrait = mContext.getResources().
87 getInteger(R.integer.max_suggest_lines_portrait);
88 mLinesLandscape = mContext.getResources().
89 getInteger(R.integer.max_suggest_lines_landscape);
Narayan Kamath5119edd2011-02-23 15:49:17 +000090
Michael Kolb21ce4d22010-09-15 14:55:05 -070091 mFilter = new SuggestFilter();
Michael Kolb21ce4d22010-09-15 14:55:05 -070092 addSource(new CombinedCursor());
93 }
94
Michael Kolbcfa3af52010-12-14 10:36:11 -080095 void setVoiceResults(List<String> voiceResults) {
96 mVoiceResults = voiceResults;
Michael Kolba50c4462010-12-15 10:49:12 -080097 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -080098 }
99
Michael Kolb21ce4d22010-09-15 14:55:05 -0700100 public void setLandscapeMode(boolean mode) {
101 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800102 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 }
104
Michael Kolb21ce4d22010-09-15 14:55:05 -0700105 public void addSource(CursorSource c) {
106 if (mSources == null) {
107 mSources = new ArrayList<CursorSource>(5);
108 }
109 mSources.add(c);
110 }
111
112 @Override
113 public void onClick(View v) {
John Reckad373302010-12-17 15:28:13 -0800114 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000115
Michael Kolb21ce4d22010-09-15 14:55:05 -0700116 if (R.id.icon2 == v.getId()) {
117 // replace input field text with suggestion text
Narayan Kamath5119edd2011-02-23 15:49:17 +0000118 mListener.onSearch(getSuggestionUrl(item));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700119 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000120 mListener.onSelect(getSuggestionUrl(item), item.type, item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700121 }
122 }
123
124 @Override
125 public Filter getFilter() {
126 return mFilter;
127 }
128
129 @Override
130 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800131 if (mVoiceResults != null) {
132 return mVoiceResults.size();
133 }
John Reck35defff2010-11-11 14:06:45 -0800134 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700135 }
136
137 @Override
138 public SuggestItem getItem(int position) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800139 if (mVoiceResults != null) {
Michael Kolbbd2dd642011-01-13 13:01:30 -0800140 SuggestItem item = new SuggestItem(mVoiceResults.get(position),
141 null, TYPE_VOICE_SEARCH);
142 item.extra = Integer.toString(position);
143 return item;
Michael Kolbcfa3af52010-12-14 10:36:11 -0800144 }
John Reck35defff2010-11-11 14:06:45 -0800145 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700146 return null;
147 }
John Reckad373302010-12-17 15:28:13 -0800148 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700149 }
150
151 @Override
152 public long getItemId(int position) {
John Reck1605bef2011-01-10 18:11:18 -0800153 return position;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700154 }
155
156 @Override
157 public View getView(int position, View convertView, ViewGroup parent) {
158 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800159 View view = convertView;
160 if (view == null) {
John Reckad373302010-12-17 15:28:13 -0800161 view = inflater.inflate(R.layout.suggestion_item, parent, false);
John Reck35defff2010-11-11 14:06:45 -0800162 }
John Reckad373302010-12-17 15:28:13 -0800163 bindView(view, getItem(position));
164 return view;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700165 }
166
167 private void bindView(View view, SuggestItem item) {
168 // store item for click handling
169 view.setTag(item);
170 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
171 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
172 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700173 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700174 View div = view.findViewById(R.id.divider);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000175 tv1.setText(Html.fromHtml(item.title));
John Reckad373302010-12-17 15:28:13 -0800176 if (TextUtils.isEmpty(item.url)) {
177 tv2.setVisibility(View.GONE);
178 } else {
179 tv2.setVisibility(View.VISIBLE);
180 tv2.setText(item.url);
181 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700182 int id = -1;
183 switch (item.type) {
184 case TYPE_SUGGEST:
185 case TYPE_SEARCH:
Michael Kolbbd2dd642011-01-13 13:01:30 -0800186 case TYPE_VOICE_SEARCH:
Michael Kolb21ce4d22010-09-15 14:55:05 -0700187 id = R.drawable.ic_search_category_suggest;
188 break;
189 case TYPE_BOOKMARK:
190 id = R.drawable.ic_search_category_bookmark;
191 break;
192 case TYPE_HISTORY:
193 id = R.drawable.ic_search_category_history;
194 break;
195 case TYPE_SUGGEST_URL:
196 id = R.drawable.ic_search_category_browser;
197 break;
198 default:
199 id = -1;
200 }
201 if (id != -1) {
202 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
203 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800204 ic2.setVisibility(((TYPE_SUGGEST == item.type)
205 || (TYPE_SEARCH == item.type)
206 || (TYPE_VOICE_SEARCH == item.type))
Michael Kolb21ce4d22010-09-15 14:55:05 -0700207 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700208 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700209 ic2.setOnClickListener(this);
John Reckad373302010-12-17 15:28:13 -0800210 view.findViewById(R.id.suggestion).setOnClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700211 }
212
John Reck35defff2010-11-11 14:06:45 -0800213 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700214
John Reck35defff2010-11-11 14:06:45 -0800215 @Override
216 protected List<SuggestItem> doInBackground(CharSequence... params) {
217 SuggestCursor cursor = new SuggestCursor();
218 cursor.runQuery(params[0]);
219 List<SuggestItem> results = new ArrayList<SuggestItem>();
220 int count = cursor.getCount();
221 for (int i = 0; i < count; i++) {
222 results.add(cursor.getItem());
223 cursor.moveToNext();
224 }
225 cursor.close();
226 return results;
227 }
228
229 @Override
230 protected void onPostExecute(List<SuggestItem> items) {
231 mSuggestResults = items;
232 mMixedResults = buildSuggestionResults();
233 notifyDataSetChanged();
John Reck35defff2010-11-11 14:06:45 -0800234 }
235 }
236
237 SuggestionResults buildSuggestionResults() {
238 SuggestionResults mixed = new SuggestionResults();
239 List<SuggestItem> filter, suggest;
240 synchronized (mResultsLock) {
241 filter = mFilterResults;
242 suggest = mSuggestResults;
243 }
244 if (filter != null) {
245 for (SuggestItem item : filter) {
246 mixed.addResult(item);
247 }
248 }
249 if (suggest != null) {
250 for (SuggestItem item : suggest) {
251 mixed.addResult(item);
252 }
253 }
254 return mixed;
255 }
256
257 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700258
259 @Override
260 public CharSequence convertResultToString(Object item) {
261 if (item == null) {
262 return "";
263 }
264 SuggestItem sitem = (SuggestItem) item;
265 if (sitem.title != null) {
266 return sitem.title;
267 } else {
268 return sitem.url;
269 }
270 }
271
John Reck35defff2010-11-11 14:06:45 -0800272 void startSuggestionsAsync(final CharSequence constraint) {
John Reck117f07d2011-01-24 09:39:03 -0800273 if (!mIncognitoMode) {
274 new SlowFilterTask().execute(constraint);
275 }
John Reck35defff2010-11-11 14:06:45 -0800276 }
277
Narayan Kamath5119edd2011-02-23 15:49:17 +0000278 private boolean shouldProcessEmptyQuery() {
279 final SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
280 return searchEngine.wantsEmptyQuery();
281 }
282
Michael Kolb21ce4d22010-09-15 14:55:05 -0700283 @Override
284 protected FilterResults performFiltering(CharSequence constraint) {
285 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800286 if (mVoiceResults == null) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000287 if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800288 res.count = 0;
289 res.values = null;
290 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700291 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800292 startSuggestionsAsync(constraint);
293 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
294 if (constraint != null) {
295 for (CursorSource sc : mSources) {
296 sc.runQuery(constraint);
297 }
298 mixResults(filterResults);
299 }
300 synchronized (mResultsLock) {
301 mFilterResults = filterResults;
302 }
303 SuggestionResults mixed = buildSuggestionResults();
304 res.count = mixed.getLineCount();
305 res.values = mixed;
306 } else {
307 res.count = mVoiceResults.size();
308 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700309 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700310 return res;
311 }
312
John Reck35defff2010-11-11 14:06:45 -0800313 void mixResults(List<SuggestItem> results) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000314 int maxLines = getMaxLines();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700315 for (int i = 0; i < mSources.size(); i++) {
316 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800317 int n = Math.min(s.getCount(), maxLines);
318 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700319 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700320 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800321 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700322 more = s.moveToNext();
323 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700324 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700325 }
326
327 @Override
328 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800329 if (fresults.values instanceof SuggestionResults) {
330 mMixedResults = (SuggestionResults) fresults.values;
John Recka005cd72011-02-01 11:46:53 -0800331 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800332 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700333 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000334 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700335
Narayan Kamath5119edd2011-02-23 15:49:17 +0000336 private int getMaxLines() {
337 int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
338 maxLines = (int) Math.ceil(maxLines / 2.0);
339 return maxLines;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700340 }
341
342 /**
343 * sorted list of results of a suggestion query
344 *
345 */
346 class SuggestionResults {
347
348 ArrayList<SuggestItem> items;
349 // count per type
350 int[] counts;
351
352 SuggestionResults() {
353 items = new ArrayList<SuggestItem>(24);
354 // n of types:
355 counts = new int[5];
356 }
357
358 int getTypeCount(int type) {
359 return counts[type];
360 }
361
362 void addResult(SuggestItem item) {
363 int ix = 0;
364 while ((ix < items.size()) && (item.type >= items.get(ix).type))
365 ix++;
366 items.add(ix, item);
367 counts[item.type]++;
368 }
369
370 int getLineCount() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000371 return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700372 }
373
John Reck35defff2010-11-11 14:06:45 -0800374 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700375 public String toString() {
376 if (items == null) return null;
377 if (items.size() == 0) return "[]";
378 StringBuilder sb = new StringBuilder();
379 for (int i = 0; i < items.size(); i++) {
380 SuggestItem item = items.get(i);
381 sb.append(item.type + ": " + item.title);
382 if (i < items.size() - 1) {
383 sb.append(", ");
384 }
385 }
386 return sb.toString();
387 }
388 }
389
390 /**
391 * data object to hold suggestion values
392 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000393 public class SuggestItem {
394 public String title;
395 public String url;
396 public int type;
397 public String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700398
399 public SuggestItem(String text, String u, int t) {
400 title = text;
401 url = u;
402 type = t;
403 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800404
Michael Kolb21ce4d22010-09-15 14:55:05 -0700405 }
406
407 abstract class CursorSource {
408
409 Cursor mCursor;
410
411 boolean moveToNext() {
412 return mCursor.moveToNext();
413 }
414
415 public abstract void runQuery(CharSequence constraint);
416
417 public abstract SuggestItem getItem();
418
419 public int getCount() {
420 return (mCursor != null) ? mCursor.getCount() : 0;
421 }
422
423 public void close() {
424 if (mCursor != null) {
425 mCursor.close();
426 }
427 }
428 }
429
430 /**
431 * combined bookmark & history source
432 */
433 class CombinedCursor extends CursorSource {
434
435 @Override
436 public SuggestItem getItem() {
437 if ((mCursor != null) && (!mCursor.isAfterLast())) {
438 String title = mCursor.getString(1);
439 String url = mCursor.getString(2);
440 boolean isBookmark = (mCursor.getInt(3) == 1);
441 return new SuggestItem(getTitle(title, url), getUrl(title, url),
442 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
443 }
444 return null;
445 }
446
447 @Override
448 public void runQuery(CharSequence constraint) {
449 // constraint != null
450 if (mCursor != null) {
451 mCursor.close();
452 }
453 String like = constraint + "%";
454 String[] args = null;
455 String selection = null;
456 if (like.startsWith("http") || like.startsWith("file")) {
457 args = new String[1];
458 args[0] = like;
459 selection = "url LIKE ?";
460 } else {
461 args = new String[5];
462 args[0] = "http://" + like;
463 args[1] = "http://www." + like;
464 args[2] = "https://" + like;
465 args[3] = "https://www." + like;
466 // To match against titles.
467 args[4] = like;
468 selection = COMBINED_SELECTION;
469 }
470 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700471 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
John Reckad373302010-12-17 15:28:13 -0800472 Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
John Reck3dff1ce2010-12-10 11:13:57 -0800473 BookmarkUtils.addAccountInfo(mContext, ub);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700474 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700475 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700476 selection,
477 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800478 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700479 BrowserContract.Combined.VISITS + " DESC, " +
480 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
481 if (mCursor != null) {
482 mCursor.moveToFirst();
483 }
484 }
485
486 /**
487 * Provides the title (text line 1) for a browser suggestion, which should be the
488 * webpage title. If the webpage title is empty, returns the stripped url instead.
489 *
490 * @return the title string to use
491 */
492 private String getTitle(String title, String url) {
493 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700494 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700495 }
496 return title;
497 }
498
499 /**
500 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
501 * webpage url. If the webpage title is empty, then the url should go in the title
502 * instead, and the subtitle should be empty, so this would return null.
503 *
504 * @return the subtitle string to use, or null if none
505 */
506 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700507 if (TextUtils.isEmpty(title)
508 || TextUtils.getTrimmedLength(title) == 0
509 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700510 return null;
511 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700512 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700513 }
514 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700515 }
516
Michael Kolb21ce4d22010-09-15 14:55:05 -0700517 class SuggestCursor extends CursorSource {
518
519 @Override
520 public SuggestItem getItem() {
521 if (mCursor != null) {
522 String title = mCursor.getString(
523 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
524 String text2 = mCursor.getString(
525 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
526 String url = mCursor.getString(
527 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
528 String uri = mCursor.getString(
529 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
530 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800531 SuggestItem item = new SuggestItem(title, url, type);
532 item.extra = mCursor.getString(
533 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
534 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700535 }
536 return null;
537 }
538
539 @Override
540 public void runQuery(CharSequence constraint) {
541 if (mCursor != null) {
542 mCursor.close();
543 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000544 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700545 if (!TextUtils.isEmpty(constraint)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700546 if (searchEngine != null && searchEngine.supportsSuggestions()) {
547 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
548 if (mCursor != null) {
549 mCursor.moveToFirst();
550 }
551 }
552 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000553 if (searchEngine.wantsEmptyQuery()) {
554 mCursor = searchEngine.getSuggestions(mContext, "");
555 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700556 mCursor = null;
557 }
558 }
559
560 }
561
Narayan Kamath5119edd2011-02-23 15:49:17 +0000562 private boolean useInstant() {
563 return BrowserSettings.getInstance().useInstant();
564 }
565
John Reck35defff2010-11-11 14:06:45 -0800566 public void clearCache() {
567 mFilterResults = null;
568 mSuggestResults = null;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000569 notifyDataSetInvalidated();
John Reck35defff2010-11-11 14:06:45 -0800570 }
571
John Reck117f07d2011-01-24 09:39:03 -0800572 public void setIncognitoMode(boolean incognito) {
573 mIncognitoMode = incognito;
574 clearCache();
575 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000576
577 static String getSuggestionTitle(SuggestItem item) {
578 // There must be a better way to strip HTML from things.
579 // This method is used in multiple places. It is also more
580 // expensive than a standard html escaper.
581 return (item.title != null) ? Html.fromHtml(item.title).toString() : null;
582 }
583
584 static String getSuggestionUrl(SuggestItem item) {
585 final String title = SuggestionsAdapter.getSuggestionTitle(item);
586
587 if (TextUtils.isEmpty(item.url)) {
588 return title;
589 }
590
591 return item.url;
592 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700593}