blob: 9a099cb3fa81e7af6a7dfc665fbb64ef361d63c0 [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;
Michael Kolb21ce4d22010-09-15 14:55:05 -070020import com.android.browser.search.SearchEngine;
21
22import android.app.SearchManager;
23import android.content.Context;
24import android.database.Cursor;
25import android.net.Uri;
John Reck35defff2010-11-11 14:06:45 -080026import android.os.AsyncTask;
Michael Kolb21ce4d22010-09-15 14:55:05 -070027import android.provider.BrowserContract;
Narayan Kamath5119edd2011-02-23 15:49:17 +000028import android.text.Html;
Michael Kolb21ce4d22010-09-15 14:55:05 -070029import android.text.TextUtils;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.View.OnClickListener;
33import android.view.ViewGroup;
34import android.widget.BaseAdapter;
35import android.widget.Filter;
36import android.widget.Filterable;
37import android.widget.ImageView;
Michael Kolb21ce4d22010-09-15 14:55:05 -070038import android.widget.TextView;
39
40import java.util.ArrayList;
41import java.util.List;
Michael Kolb21ce4d22010-09-15 14:55:05 -070042
43/**
44 * adapter to wrap multiple cursors for url/search completions
45 */
Michael Kolbbd2dd642011-01-13 13:01:30 -080046public class SuggestionsAdapter extends BaseAdapter implements Filterable,
47 OnClickListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -070048
Narayan Kamath5119edd2011-02-23 15:49:17 +000049 public static final int TYPE_BOOKMARK = 0;
50 public static final int TYPE_HISTORY = 1;
51 public static final int TYPE_SUGGEST_URL = 2;
52 public static final int TYPE_SEARCH = 3;
53 public static final int TYPE_SUGGEST = 4;
54 public static final int TYPE_VOICE_SEARCH = 5;
Michael Kolb21ce4d22010-09-15 14:55:05 -070055
56 private static final String[] COMBINED_PROJECTION =
57 {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
58 BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
59
Michael Kolb21ce4d22010-09-15 14:55:05 -070060 private static final String COMBINED_SELECTION =
61 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
62
Narayan Kamath5119edd2011-02-23 15:49:17 +000063 final Context mContext;
64 final Filter mFilter;
John Reck35defff2010-11-11 14:06:45 -080065 SuggestionResults mMixedResults;
66 List<SuggestItem> mSuggestResults, mFilterResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -070067 List<CursorSource> mSources;
68 boolean mLandscapeMode;
Narayan Kamath5119edd2011-02-23 15:49:17 +000069 final CompletionListener mListener;
70 final int mLinesPortrait;
71 final int mLinesLandscape;
72 final Object mResultsLock = new Object();
Michael Kolbcfa3af52010-12-14 10:36:11 -080073 List<String> mVoiceResults;
John Reck117f07d2011-01-24 09:39:03 -080074 boolean mIncognitoMode;
John Reck35e9dd62011-04-25 09:01:54 -070075 BrowserSettings mSettings;
Michael Kolb21ce4d22010-09-15 14:55:05 -070076
77 interface CompletionListener {
78
79 public void onSearch(String txt);
80
Michael Kolbbd2dd642011-01-13 13:01:30 -080081 public void onSelect(String txt, int type, String extraData);
Michael Kolb21ce4d22010-09-15 14:55:05 -070082
83 }
84
85 public SuggestionsAdapter(Context ctx, CompletionListener listener) {
86 mContext = ctx;
John Reck35e9dd62011-04-25 09:01:54 -070087 mSettings = BrowserSettings.getInstance();
Michael Kolb21ce4d22010-09-15 14:55:05 -070088 mListener = listener;
89 mLinesPortrait = mContext.getResources().
90 getInteger(R.integer.max_suggest_lines_portrait);
91 mLinesLandscape = mContext.getResources().
92 getInteger(R.integer.max_suggest_lines_landscape);
Narayan Kamath5119edd2011-02-23 15:49:17 +000093
Michael Kolb21ce4d22010-09-15 14:55:05 -070094 mFilter = new SuggestFilter();
Michael Kolb21ce4d22010-09-15 14:55:05 -070095 addSource(new CombinedCursor());
96 }
97
Michael Kolbcfa3af52010-12-14 10:36:11 -080098 void setVoiceResults(List<String> voiceResults) {
99 mVoiceResults = voiceResults;
Michael Kolba50c4462010-12-15 10:49:12 -0800100 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101 }
102
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 public void setLandscapeMode(boolean mode) {
104 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800105 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 }
107
Michael Kolb21ce4d22010-09-15 14:55:05 -0700108 public void addSource(CursorSource c) {
109 if (mSources == null) {
110 mSources = new ArrayList<CursorSource>(5);
111 }
112 mSources.add(c);
113 }
114
115 @Override
116 public void onClick(View v) {
John Reckad373302010-12-17 15:28:13 -0800117 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000118
Michael Kolb21ce4d22010-09-15 14:55:05 -0700119 if (R.id.icon2 == v.getId()) {
120 // replace input field text with suggestion text
Narayan Kamath5119edd2011-02-23 15:49:17 +0000121 mListener.onSearch(getSuggestionUrl(item));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700122 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000123 mListener.onSelect(getSuggestionUrl(item), item.type, item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700124 }
125 }
126
127 @Override
128 public Filter getFilter() {
129 return mFilter;
130 }
131
132 @Override
133 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800134 if (mVoiceResults != null) {
135 return mVoiceResults.size();
136 }
John Reck35defff2010-11-11 14:06:45 -0800137 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700138 }
139
140 @Override
141 public SuggestItem getItem(int position) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800142 if (mVoiceResults != null) {
Michael Kolbbd2dd642011-01-13 13:01:30 -0800143 SuggestItem item = new SuggestItem(mVoiceResults.get(position),
144 null, TYPE_VOICE_SEARCH);
145 item.extra = Integer.toString(position);
146 return item;
Michael Kolbcfa3af52010-12-14 10:36:11 -0800147 }
John Reck35defff2010-11-11 14:06:45 -0800148 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700149 return null;
150 }
John Reckad373302010-12-17 15:28:13 -0800151 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700152 }
153
154 @Override
155 public long getItemId(int position) {
John Reck1605bef2011-01-10 18:11:18 -0800156 return position;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700157 }
158
159 @Override
160 public View getView(int position, View convertView, ViewGroup parent) {
161 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800162 View view = convertView;
163 if (view == null) {
John Reckad373302010-12-17 15:28:13 -0800164 view = inflater.inflate(R.layout.suggestion_item, parent, false);
John Reck35defff2010-11-11 14:06:45 -0800165 }
John Reckad373302010-12-17 15:28:13 -0800166 bindView(view, getItem(position));
167 return view;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700168 }
169
170 private void bindView(View view, SuggestItem item) {
171 // store item for click handling
172 view.setTag(item);
173 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
174 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
175 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700176 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700177 View div = view.findViewById(R.id.divider);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000178 tv1.setText(Html.fromHtml(item.title));
John Reckad373302010-12-17 15:28:13 -0800179 if (TextUtils.isEmpty(item.url)) {
180 tv2.setVisibility(View.GONE);
181 } else {
182 tv2.setVisibility(View.VISIBLE);
183 tv2.setText(item.url);
184 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700185 int id = -1;
186 switch (item.type) {
187 case TYPE_SUGGEST:
188 case TYPE_SEARCH:
Michael Kolbbd2dd642011-01-13 13:01:30 -0800189 case TYPE_VOICE_SEARCH:
Michael Kolb21ce4d22010-09-15 14:55:05 -0700190 id = R.drawable.ic_search_category_suggest;
191 break;
192 case TYPE_BOOKMARK:
193 id = R.drawable.ic_search_category_bookmark;
194 break;
195 case TYPE_HISTORY:
196 id = R.drawable.ic_search_category_history;
197 break;
198 case TYPE_SUGGEST_URL:
199 id = R.drawable.ic_search_category_browser;
200 break;
201 default:
202 id = -1;
203 }
204 if (id != -1) {
205 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
206 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800207 ic2.setVisibility(((TYPE_SUGGEST == item.type)
208 || (TYPE_SEARCH == item.type)
209 || (TYPE_VOICE_SEARCH == item.type))
Michael Kolb21ce4d22010-09-15 14:55:05 -0700210 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700211 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700212 ic2.setOnClickListener(this);
John Reckad373302010-12-17 15:28:13 -0800213 view.findViewById(R.id.suggestion).setOnClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700214 }
215
John Reck35defff2010-11-11 14:06:45 -0800216 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700217
John Reck35defff2010-11-11 14:06:45 -0800218 @Override
219 protected List<SuggestItem> doInBackground(CharSequence... params) {
220 SuggestCursor cursor = new SuggestCursor();
221 cursor.runQuery(params[0]);
222 List<SuggestItem> results = new ArrayList<SuggestItem>();
223 int count = cursor.getCount();
224 for (int i = 0; i < count; i++) {
225 results.add(cursor.getItem());
226 cursor.moveToNext();
227 }
228 cursor.close();
229 return results;
230 }
231
232 @Override
233 protected void onPostExecute(List<SuggestItem> items) {
234 mSuggestResults = items;
235 mMixedResults = buildSuggestionResults();
236 notifyDataSetChanged();
John Reck35defff2010-11-11 14:06:45 -0800237 }
238 }
239
240 SuggestionResults buildSuggestionResults() {
241 SuggestionResults mixed = new SuggestionResults();
242 List<SuggestItem> filter, suggest;
243 synchronized (mResultsLock) {
244 filter = mFilterResults;
245 suggest = mSuggestResults;
246 }
247 if (filter != null) {
248 for (SuggestItem item : filter) {
249 mixed.addResult(item);
250 }
251 }
252 if (suggest != null) {
253 for (SuggestItem item : suggest) {
254 mixed.addResult(item);
255 }
256 }
257 return mixed;
258 }
259
260 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700261
262 @Override
263 public CharSequence convertResultToString(Object item) {
264 if (item == null) {
265 return "";
266 }
267 SuggestItem sitem = (SuggestItem) item;
268 if (sitem.title != null) {
269 return sitem.title;
270 } else {
271 return sitem.url;
272 }
273 }
274
John Reck35defff2010-11-11 14:06:45 -0800275 void startSuggestionsAsync(final CharSequence constraint) {
John Reck117f07d2011-01-24 09:39:03 -0800276 if (!mIncognitoMode) {
277 new SlowFilterTask().execute(constraint);
278 }
John Reck35defff2010-11-11 14:06:45 -0800279 }
280
Narayan Kamath5119edd2011-02-23 15:49:17 +0000281 private boolean shouldProcessEmptyQuery() {
John Reck35e9dd62011-04-25 09:01:54 -0700282 final SearchEngine searchEngine = mSettings.getSearchEngine();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000283 return searchEngine.wantsEmptyQuery();
284 }
285
Michael Kolb21ce4d22010-09-15 14:55:05 -0700286 @Override
287 protected FilterResults performFiltering(CharSequence constraint) {
288 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800289 if (mVoiceResults == null) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000290 if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800291 res.count = 0;
292 res.values = null;
293 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700294 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800295 startSuggestionsAsync(constraint);
296 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
297 if (constraint != null) {
298 for (CursorSource sc : mSources) {
299 sc.runQuery(constraint);
300 }
301 mixResults(filterResults);
302 }
303 synchronized (mResultsLock) {
304 mFilterResults = filterResults;
305 }
306 SuggestionResults mixed = buildSuggestionResults();
307 res.count = mixed.getLineCount();
308 res.values = mixed;
309 } else {
310 res.count = mVoiceResults.size();
311 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700312 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700313 return res;
314 }
315
John Reck35defff2010-11-11 14:06:45 -0800316 void mixResults(List<SuggestItem> results) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000317 int maxLines = getMaxLines();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700318 for (int i = 0; i < mSources.size(); i++) {
319 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800320 int n = Math.min(s.getCount(), maxLines);
321 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700322 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700323 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800324 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700325 more = s.moveToNext();
326 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700327 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700328 }
329
330 @Override
331 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800332 if (fresults.values instanceof SuggestionResults) {
333 mMixedResults = (SuggestionResults) fresults.values;
John Recka005cd72011-02-01 11:46:53 -0800334 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800335 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700336 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000337 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700338
Narayan Kamath5119edd2011-02-23 15:49:17 +0000339 private int getMaxLines() {
340 int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
341 maxLines = (int) Math.ceil(maxLines / 2.0);
342 return maxLines;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700343 }
344
345 /**
346 * sorted list of results of a suggestion query
347 *
348 */
349 class SuggestionResults {
350
351 ArrayList<SuggestItem> items;
352 // count per type
353 int[] counts;
354
355 SuggestionResults() {
356 items = new ArrayList<SuggestItem>(24);
357 // n of types:
358 counts = new int[5];
359 }
360
361 int getTypeCount(int type) {
362 return counts[type];
363 }
364
365 void addResult(SuggestItem item) {
366 int ix = 0;
367 while ((ix < items.size()) && (item.type >= items.get(ix).type))
368 ix++;
369 items.add(ix, item);
370 counts[item.type]++;
371 }
372
373 int getLineCount() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000374 return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700375 }
376
John Reck35defff2010-11-11 14:06:45 -0800377 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700378 public String toString() {
379 if (items == null) return null;
380 if (items.size() == 0) return "[]";
381 StringBuilder sb = new StringBuilder();
382 for (int i = 0; i < items.size(); i++) {
383 SuggestItem item = items.get(i);
384 sb.append(item.type + ": " + item.title);
385 if (i < items.size() - 1) {
386 sb.append(", ");
387 }
388 }
389 return sb.toString();
390 }
391 }
392
393 /**
394 * data object to hold suggestion values
395 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000396 public class SuggestItem {
397 public String title;
398 public String url;
399 public int type;
400 public String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700401
402 public SuggestItem(String text, String u, int t) {
403 title = text;
404 url = u;
405 type = t;
406 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800407
Michael Kolb21ce4d22010-09-15 14:55:05 -0700408 }
409
410 abstract class CursorSource {
411
412 Cursor mCursor;
413
414 boolean moveToNext() {
415 return mCursor.moveToNext();
416 }
417
418 public abstract void runQuery(CharSequence constraint);
419
420 public abstract SuggestItem getItem();
421
422 public int getCount() {
423 return (mCursor != null) ? mCursor.getCount() : 0;
424 }
425
426 public void close() {
427 if (mCursor != null) {
428 mCursor.close();
429 }
430 }
431 }
432
433 /**
434 * combined bookmark & history source
435 */
436 class CombinedCursor extends CursorSource {
437
438 @Override
439 public SuggestItem getItem() {
440 if ((mCursor != null) && (!mCursor.isAfterLast())) {
441 String title = mCursor.getString(1);
442 String url = mCursor.getString(2);
443 boolean isBookmark = (mCursor.getInt(3) == 1);
444 return new SuggestItem(getTitle(title, url), getUrl(title, url),
445 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
446 }
447 return null;
448 }
449
450 @Override
451 public void runQuery(CharSequence constraint) {
452 // constraint != null
453 if (mCursor != null) {
454 mCursor.close();
455 }
456 String like = constraint + "%";
457 String[] args = null;
458 String selection = null;
459 if (like.startsWith("http") || like.startsWith("file")) {
460 args = new String[1];
461 args[0] = like;
462 selection = "url LIKE ?";
463 } else {
464 args = new String[5];
465 args[0] = "http://" + like;
466 args[1] = "http://www." + like;
467 args[2] = "https://" + like;
468 args[3] = "https://www." + like;
469 // To match against titles.
470 args[4] = like;
471 selection = COMBINED_SELECTION;
472 }
473 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700474 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
John Reckad373302010-12-17 15:28:13 -0800475 Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
John Reck7ffb8952011-06-27 15:42:39 -0700476 ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY,
477 BrowserContract.Combined.URL);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700478 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700479 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700480 selection,
481 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800482 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700483 BrowserContract.Combined.VISITS + " DESC, " +
484 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
485 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}