blob: 242e17099c7905d0b73ac036097195d1ae082098 [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;
John Reck35e9dd62011-04-25 09:01:54 -070074 BrowserSettings mSettings;
Michael Kolb21ce4d22010-09-15 14:55:05 -070075
76 interface CompletionListener {
77
78 public void onSearch(String txt);
79
Michael Kolbbd2dd642011-01-13 13:01:30 -080080 public void onSelect(String txt, int type, String extraData);
Michael Kolb21ce4d22010-09-15 14:55:05 -070081
82 }
83
84 public SuggestionsAdapter(Context ctx, CompletionListener listener) {
85 mContext = ctx;
John Reck35e9dd62011-04-25 09:01:54 -070086 mSettings = BrowserSettings.getInstance();
Michael Kolb21ce4d22010-09-15 14:55:05 -070087 mListener = listener;
88 mLinesPortrait = mContext.getResources().
89 getInteger(R.integer.max_suggest_lines_portrait);
90 mLinesLandscape = mContext.getResources().
91 getInteger(R.integer.max_suggest_lines_landscape);
Narayan Kamath5119edd2011-02-23 15:49:17 +000092
Michael Kolb21ce4d22010-09-15 14:55:05 -070093 mFilter = new SuggestFilter();
Michael Kolb21ce4d22010-09-15 14:55:05 -070094 addSource(new CombinedCursor());
95 }
96
Michael Kolbcfa3af52010-12-14 10:36:11 -080097 void setVoiceResults(List<String> voiceResults) {
98 mVoiceResults = voiceResults;
Michael Kolba50c4462010-12-15 10:49:12 -080099 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800100 }
101
Michael Kolb21ce4d22010-09-15 14:55:05 -0700102 public void setLandscapeMode(boolean mode) {
103 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800104 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700105 }
106
Michael Kolb21ce4d22010-09-15 14:55:05 -0700107 public void addSource(CursorSource c) {
108 if (mSources == null) {
109 mSources = new ArrayList<CursorSource>(5);
110 }
111 mSources.add(c);
112 }
113
114 @Override
115 public void onClick(View v) {
John Reckad373302010-12-17 15:28:13 -0800116 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000117
Michael Kolb21ce4d22010-09-15 14:55:05 -0700118 if (R.id.icon2 == v.getId()) {
119 // replace input field text with suggestion text
Narayan Kamath5119edd2011-02-23 15:49:17 +0000120 mListener.onSearch(getSuggestionUrl(item));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700121 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000122 mListener.onSelect(getSuggestionUrl(item), item.type, item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700123 }
124 }
125
126 @Override
127 public Filter getFilter() {
128 return mFilter;
129 }
130
131 @Override
132 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800133 if (mVoiceResults != null) {
134 return mVoiceResults.size();
135 }
John Reck35defff2010-11-11 14:06:45 -0800136 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700137 }
138
139 @Override
140 public SuggestItem getItem(int position) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800141 if (mVoiceResults != null) {
Michael Kolbbd2dd642011-01-13 13:01:30 -0800142 SuggestItem item = new SuggestItem(mVoiceResults.get(position),
143 null, TYPE_VOICE_SEARCH);
144 item.extra = Integer.toString(position);
145 return item;
Michael Kolbcfa3af52010-12-14 10:36:11 -0800146 }
John Reck35defff2010-11-11 14:06:45 -0800147 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700148 return null;
149 }
John Reckad373302010-12-17 15:28:13 -0800150 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700151 }
152
153 @Override
154 public long getItemId(int position) {
John Reck1605bef2011-01-10 18:11:18 -0800155 return position;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700156 }
157
158 @Override
159 public View getView(int position, View convertView, ViewGroup parent) {
160 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800161 View view = convertView;
162 if (view == null) {
John Reckad373302010-12-17 15:28:13 -0800163 view = inflater.inflate(R.layout.suggestion_item, parent, false);
John Reck35defff2010-11-11 14:06:45 -0800164 }
John Reckad373302010-12-17 15:28:13 -0800165 bindView(view, getItem(position));
166 return view;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700167 }
168
169 private void bindView(View view, SuggestItem item) {
170 // store item for click handling
171 view.setTag(item);
172 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
173 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
174 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700175 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700176 View div = view.findViewById(R.id.divider);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000177 tv1.setText(Html.fromHtml(item.title));
John Reckad373302010-12-17 15:28:13 -0800178 if (TextUtils.isEmpty(item.url)) {
179 tv2.setVisibility(View.GONE);
180 } else {
181 tv2.setVisibility(View.VISIBLE);
182 tv2.setText(item.url);
183 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700184 int id = -1;
185 switch (item.type) {
186 case TYPE_SUGGEST:
187 case TYPE_SEARCH:
Michael Kolbbd2dd642011-01-13 13:01:30 -0800188 case TYPE_VOICE_SEARCH:
Michael Kolb21ce4d22010-09-15 14:55:05 -0700189 id = R.drawable.ic_search_category_suggest;
190 break;
191 case TYPE_BOOKMARK:
192 id = R.drawable.ic_search_category_bookmark;
193 break;
194 case TYPE_HISTORY:
195 id = R.drawable.ic_search_category_history;
196 break;
197 case TYPE_SUGGEST_URL:
198 id = R.drawable.ic_search_category_browser;
199 break;
200 default:
201 id = -1;
202 }
203 if (id != -1) {
204 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
205 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800206 ic2.setVisibility(((TYPE_SUGGEST == item.type)
207 || (TYPE_SEARCH == item.type)
208 || (TYPE_VOICE_SEARCH == item.type))
Michael Kolb21ce4d22010-09-15 14:55:05 -0700209 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700210 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700211 ic2.setOnClickListener(this);
John Reckad373302010-12-17 15:28:13 -0800212 view.findViewById(R.id.suggestion).setOnClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700213 }
214
John Reck35defff2010-11-11 14:06:45 -0800215 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700216
John Reck35defff2010-11-11 14:06:45 -0800217 @Override
218 protected List<SuggestItem> doInBackground(CharSequence... params) {
219 SuggestCursor cursor = new SuggestCursor();
220 cursor.runQuery(params[0]);
221 List<SuggestItem> results = new ArrayList<SuggestItem>();
222 int count = cursor.getCount();
223 for (int i = 0; i < count; i++) {
224 results.add(cursor.getItem());
225 cursor.moveToNext();
226 }
227 cursor.close();
228 return results;
229 }
230
231 @Override
232 protected void onPostExecute(List<SuggestItem> items) {
233 mSuggestResults = items;
234 mMixedResults = buildSuggestionResults();
235 notifyDataSetChanged();
John Reck35defff2010-11-11 14:06:45 -0800236 }
237 }
238
239 SuggestionResults buildSuggestionResults() {
240 SuggestionResults mixed = new SuggestionResults();
241 List<SuggestItem> filter, suggest;
242 synchronized (mResultsLock) {
243 filter = mFilterResults;
244 suggest = mSuggestResults;
245 }
246 if (filter != null) {
247 for (SuggestItem item : filter) {
248 mixed.addResult(item);
249 }
250 }
251 if (suggest != null) {
252 for (SuggestItem item : suggest) {
253 mixed.addResult(item);
254 }
255 }
256 return mixed;
257 }
258
259 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700260
261 @Override
262 public CharSequence convertResultToString(Object item) {
263 if (item == null) {
264 return "";
265 }
266 SuggestItem sitem = (SuggestItem) item;
267 if (sitem.title != null) {
268 return sitem.title;
269 } else {
270 return sitem.url;
271 }
272 }
273
John Reck35defff2010-11-11 14:06:45 -0800274 void startSuggestionsAsync(final CharSequence constraint) {
John Reck117f07d2011-01-24 09:39:03 -0800275 if (!mIncognitoMode) {
276 new SlowFilterTask().execute(constraint);
277 }
John Reck35defff2010-11-11 14:06:45 -0800278 }
279
Narayan Kamath5119edd2011-02-23 15:49:17 +0000280 private boolean shouldProcessEmptyQuery() {
John Reck35e9dd62011-04-25 09:01:54 -0700281 final SearchEngine searchEngine = mSettings.getSearchEngine();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000282 return searchEngine.wantsEmptyQuery();
283 }
284
Michael Kolb21ce4d22010-09-15 14:55:05 -0700285 @Override
286 protected FilterResults performFiltering(CharSequence constraint) {
287 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800288 if (mVoiceResults == null) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000289 if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800290 res.count = 0;
291 res.values = null;
292 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700293 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800294 startSuggestionsAsync(constraint);
295 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
296 if (constraint != null) {
297 for (CursorSource sc : mSources) {
298 sc.runQuery(constraint);
299 }
300 mixResults(filterResults);
301 }
302 synchronized (mResultsLock) {
303 mFilterResults = filterResults;
304 }
305 SuggestionResults mixed = buildSuggestionResults();
306 res.count = mixed.getLineCount();
307 res.values = mixed;
308 } else {
309 res.count = mVoiceResults.size();
310 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700311 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700312 return res;
313 }
314
John Reck35defff2010-11-11 14:06:45 -0800315 void mixResults(List<SuggestItem> results) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000316 int maxLines = getMaxLines();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700317 for (int i = 0; i < mSources.size(); i++) {
318 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800319 int n = Math.min(s.getCount(), maxLines);
320 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700321 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700322 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800323 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700324 more = s.moveToNext();
325 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700326 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700327 }
328
329 @Override
330 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800331 if (fresults.values instanceof SuggestionResults) {
332 mMixedResults = (SuggestionResults) fresults.values;
John Recka005cd72011-02-01 11:46:53 -0800333 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800334 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700335 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000336 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700337
Narayan Kamath5119edd2011-02-23 15:49:17 +0000338 private int getMaxLines() {
339 int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
340 maxLines = (int) Math.ceil(maxLines / 2.0);
341 return maxLines;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700342 }
343
344 /**
345 * sorted list of results of a suggestion query
346 *
347 */
348 class SuggestionResults {
349
350 ArrayList<SuggestItem> items;
351 // count per type
352 int[] counts;
353
354 SuggestionResults() {
355 items = new ArrayList<SuggestItem>(24);
356 // n of types:
357 counts = new int[5];
358 }
359
360 int getTypeCount(int type) {
361 return counts[type];
362 }
363
364 void addResult(SuggestItem item) {
365 int ix = 0;
366 while ((ix < items.size()) && (item.type >= items.get(ix).type))
367 ix++;
368 items.add(ix, item);
369 counts[item.type]++;
370 }
371
372 int getLineCount() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000373 return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700374 }
375
John Reck35defff2010-11-11 14:06:45 -0800376 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700377 public String toString() {
378 if (items == null) return null;
379 if (items.size() == 0) return "[]";
380 StringBuilder sb = new StringBuilder();
381 for (int i = 0; i < items.size(); i++) {
382 SuggestItem item = items.get(i);
383 sb.append(item.type + ": " + item.title);
384 if (i < items.size() - 1) {
385 sb.append(", ");
386 }
387 }
388 return sb.toString();
389 }
390 }
391
392 /**
393 * data object to hold suggestion values
394 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000395 public class SuggestItem {
396 public String title;
397 public String url;
398 public int type;
399 public String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700400
401 public SuggestItem(String text, String u, int t) {
402 title = text;
403 url = u;
404 type = t;
405 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800406
Michael Kolb21ce4d22010-09-15 14:55:05 -0700407 }
408
409 abstract class CursorSource {
410
411 Cursor mCursor;
412
413 boolean moveToNext() {
414 return mCursor.moveToNext();
415 }
416
417 public abstract void runQuery(CharSequence constraint);
418
419 public abstract SuggestItem getItem();
420
421 public int getCount() {
422 return (mCursor != null) ? mCursor.getCount() : 0;
423 }
424
425 public void close() {
426 if (mCursor != null) {
427 mCursor.close();
428 }
429 }
430 }
431
432 /**
433 * combined bookmark & history source
434 */
435 class CombinedCursor extends CursorSource {
436
437 @Override
438 public SuggestItem getItem() {
439 if ((mCursor != null) && (!mCursor.isAfterLast())) {
440 String title = mCursor.getString(1);
441 String url = mCursor.getString(2);
442 boolean isBookmark = (mCursor.getInt(3) == 1);
443 return new SuggestItem(getTitle(title, url), getUrl(title, url),
444 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
445 }
446 return null;
447 }
448
449 @Override
450 public void runQuery(CharSequence constraint) {
451 // constraint != null
452 if (mCursor != null) {
453 mCursor.close();
454 }
455 String like = constraint + "%";
456 String[] args = null;
457 String selection = null;
458 if (like.startsWith("http") || like.startsWith("file")) {
459 args = new String[1];
460 args[0] = like;
461 selection = "url LIKE ?";
462 } else {
463 args = new String[5];
464 args[0] = "http://" + like;
465 args[1] = "http://www." + like;
466 args[2] = "https://" + like;
467 args[3] = "https://www." + like;
468 // To match against titles.
469 args[4] = like;
470 selection = COMBINED_SELECTION;
471 }
472 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700473 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
John Reckad373302010-12-17 15:28:13 -0800474 Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700475 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700476 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700477 selection,
478 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800479 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700480 BrowserContract.Combined.VISITS + " DESC, " +
481 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
482 if (mCursor != null) {
483 mCursor.moveToFirst();
484 }
485 }
486
487 /**
488 * Provides the title (text line 1) for a browser suggestion, which should be the
489 * webpage title. If the webpage title is empty, returns the stripped url instead.
490 *
491 * @return the title string to use
492 */
493 private String getTitle(String title, String url) {
494 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700495 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700496 }
497 return title;
498 }
499
500 /**
501 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
502 * webpage url. If the webpage title is empty, then the url should go in the title
503 * instead, and the subtitle should be empty, so this would return null.
504 *
505 * @return the subtitle string to use, or null if none
506 */
507 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700508 if (TextUtils.isEmpty(title)
509 || TextUtils.getTrimmedLength(title) == 0
510 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700511 return null;
512 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700513 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700514 }
515 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700516 }
517
Michael Kolb21ce4d22010-09-15 14:55:05 -0700518 class SuggestCursor extends CursorSource {
519
520 @Override
521 public SuggestItem getItem() {
522 if (mCursor != null) {
523 String title = mCursor.getString(
524 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
525 String text2 = mCursor.getString(
526 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
527 String url = mCursor.getString(
528 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
529 String uri = mCursor.getString(
530 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
531 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800532 SuggestItem item = new SuggestItem(title, url, type);
533 item.extra = mCursor.getString(
534 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
535 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700536 }
537 return null;
538 }
539
540 @Override
541 public void runQuery(CharSequence constraint) {
542 if (mCursor != null) {
543 mCursor.close();
544 }
John Reck35e9dd62011-04-25 09:01:54 -0700545 SearchEngine searchEngine = mSettings.getSearchEngine();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700546 if (!TextUtils.isEmpty(constraint)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700547 if (searchEngine != null && searchEngine.supportsSuggestions()) {
548 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
549 if (mCursor != null) {
550 mCursor.moveToFirst();
551 }
552 }
553 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000554 if (searchEngine.wantsEmptyQuery()) {
555 mCursor = searchEngine.getSuggestions(mContext, "");
556 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700557 mCursor = null;
558 }
559 }
560
561 }
562
Narayan Kamath5119edd2011-02-23 15:49:17 +0000563 private boolean useInstant() {
John Reck35e9dd62011-04-25 09:01:54 -0700564 return mSettings.useInstantSearch();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000565 }
566
John Reck35defff2010-11-11 14:06:45 -0800567 public void clearCache() {
568 mFilterResults = null;
569 mSuggestResults = null;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000570 notifyDataSetInvalidated();
John Reck35defff2010-11-11 14:06:45 -0800571 }
572
John Reck117f07d2011-01-24 09:39:03 -0800573 public void setIncognitoMode(boolean incognito) {
574 mIncognitoMode = incognito;
575 clearCache();
576 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000577
578 static String getSuggestionTitle(SuggestItem item) {
579 // There must be a better way to strip HTML from things.
580 // This method is used in multiple places. It is also more
581 // expensive than a standard html escaper.
582 return (item.title != null) ? Html.fromHtml(item.title).toString() : null;
583 }
584
585 static String getSuggestionUrl(SuggestItem item) {
586 final String title = SuggestionsAdapter.getSuggestionTitle(item);
587
588 if (TextUtils.isEmpty(item.url)) {
589 return title;
590 }
591
592 return item.url;
593 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700594}