blob: e1511b9d632a0f351e97984a909badf3fe475b75 [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);
John Reck6a9afa92011-07-29 10:18:41 -0700185 tv1.setMaxLines(2);
John Reckad373302010-12-17 15:28:13 -0800186 } else {
187 tv2.setVisibility(View.VISIBLE);
188 tv2.setText(item.url);
John Reck6a9afa92011-07-29 10:18:41 -0700189 tv1.setMaxLines(1);
John Reckad373302010-12-17 15:28:13 -0800190 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700191 int id = -1;
192 switch (item.type) {
193 case TYPE_SUGGEST:
194 case TYPE_SEARCH:
Michael Kolbbd2dd642011-01-13 13:01:30 -0800195 case TYPE_VOICE_SEARCH:
Michael Kolb21ce4d22010-09-15 14:55:05 -0700196 id = R.drawable.ic_search_category_suggest;
197 break;
198 case TYPE_BOOKMARK:
199 id = R.drawable.ic_search_category_bookmark;
200 break;
201 case TYPE_HISTORY:
202 id = R.drawable.ic_search_category_history;
203 break;
204 case TYPE_SUGGEST_URL:
205 id = R.drawable.ic_search_category_browser;
206 break;
207 default:
208 id = -1;
209 }
210 if (id != -1) {
211 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
212 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800213 ic2.setVisibility(((TYPE_SUGGEST == item.type)
214 || (TYPE_SEARCH == item.type)
215 || (TYPE_VOICE_SEARCH == item.type))
Michael Kolb21ce4d22010-09-15 14:55:05 -0700216 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700217 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700218 ic2.setOnClickListener(this);
John Reckad373302010-12-17 15:28:13 -0800219 view.findViewById(R.id.suggestion).setOnClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700220 }
221
John Reck35defff2010-11-11 14:06:45 -0800222 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700223
John Reck35defff2010-11-11 14:06:45 -0800224 @Override
225 protected List<SuggestItem> doInBackground(CharSequence... params) {
226 SuggestCursor cursor = new SuggestCursor();
227 cursor.runQuery(params[0]);
228 List<SuggestItem> results = new ArrayList<SuggestItem>();
229 int count = cursor.getCount();
230 for (int i = 0; i < count; i++) {
231 results.add(cursor.getItem());
232 cursor.moveToNext();
233 }
234 cursor.close();
235 return results;
236 }
237
238 @Override
239 protected void onPostExecute(List<SuggestItem> items) {
240 mSuggestResults = items;
241 mMixedResults = buildSuggestionResults();
242 notifyDataSetChanged();
John Reck35defff2010-11-11 14:06:45 -0800243 }
244 }
245
246 SuggestionResults buildSuggestionResults() {
247 SuggestionResults mixed = new SuggestionResults();
248 List<SuggestItem> filter, suggest;
249 synchronized (mResultsLock) {
250 filter = mFilterResults;
251 suggest = mSuggestResults;
252 }
253 if (filter != null) {
254 for (SuggestItem item : filter) {
255 mixed.addResult(item);
256 }
257 }
258 if (suggest != null) {
259 for (SuggestItem item : suggest) {
260 mixed.addResult(item);
261 }
262 }
263 return mixed;
264 }
265
266 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700267
268 @Override
269 public CharSequence convertResultToString(Object item) {
270 if (item == null) {
271 return "";
272 }
273 SuggestItem sitem = (SuggestItem) item;
274 if (sitem.title != null) {
275 return sitem.title;
276 } else {
277 return sitem.url;
278 }
279 }
280
John Reck35defff2010-11-11 14:06:45 -0800281 void startSuggestionsAsync(final CharSequence constraint) {
John Reck117f07d2011-01-24 09:39:03 -0800282 if (!mIncognitoMode) {
283 new SlowFilterTask().execute(constraint);
284 }
John Reck35defff2010-11-11 14:06:45 -0800285 }
286
Narayan Kamath5119edd2011-02-23 15:49:17 +0000287 private boolean shouldProcessEmptyQuery() {
John Reck35e9dd62011-04-25 09:01:54 -0700288 final SearchEngine searchEngine = mSettings.getSearchEngine();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000289 return searchEngine.wantsEmptyQuery();
290 }
291
Michael Kolb21ce4d22010-09-15 14:55:05 -0700292 @Override
293 protected FilterResults performFiltering(CharSequence constraint) {
294 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800295 if (mVoiceResults == null) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000296 if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800297 res.count = 0;
298 res.values = null;
299 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700300 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800301 startSuggestionsAsync(constraint);
302 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
303 if (constraint != null) {
304 for (CursorSource sc : mSources) {
305 sc.runQuery(constraint);
306 }
307 mixResults(filterResults);
308 }
309 synchronized (mResultsLock) {
310 mFilterResults = filterResults;
311 }
312 SuggestionResults mixed = buildSuggestionResults();
313 res.count = mixed.getLineCount();
314 res.values = mixed;
315 } else {
316 res.count = mVoiceResults.size();
317 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700318 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700319 return res;
320 }
321
John Reck35defff2010-11-11 14:06:45 -0800322 void mixResults(List<SuggestItem> results) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000323 int maxLines = getMaxLines();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700324 for (int i = 0; i < mSources.size(); i++) {
325 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800326 int n = Math.min(s.getCount(), maxLines);
327 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700328 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700329 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800330 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700331 more = s.moveToNext();
332 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700333 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700334 }
335
336 @Override
337 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800338 if (fresults.values instanceof SuggestionResults) {
339 mMixedResults = (SuggestionResults) fresults.values;
John Recka005cd72011-02-01 11:46:53 -0800340 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800341 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700342 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000343 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700344
Narayan Kamath5119edd2011-02-23 15:49:17 +0000345 private int getMaxLines() {
346 int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
347 maxLines = (int) Math.ceil(maxLines / 2.0);
348 return maxLines;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700349 }
350
351 /**
352 * sorted list of results of a suggestion query
353 *
354 */
355 class SuggestionResults {
356
357 ArrayList<SuggestItem> items;
358 // count per type
359 int[] counts;
360
361 SuggestionResults() {
362 items = new ArrayList<SuggestItem>(24);
363 // n of types:
364 counts = new int[5];
365 }
366
367 int getTypeCount(int type) {
368 return counts[type];
369 }
370
371 void addResult(SuggestItem item) {
372 int ix = 0;
373 while ((ix < items.size()) && (item.type >= items.get(ix).type))
374 ix++;
375 items.add(ix, item);
376 counts[item.type]++;
377 }
378
379 int getLineCount() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000380 return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700381 }
382
John Reck35defff2010-11-11 14:06:45 -0800383 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700384 public String toString() {
385 if (items == null) return null;
386 if (items.size() == 0) return "[]";
387 StringBuilder sb = new StringBuilder();
388 for (int i = 0; i < items.size(); i++) {
389 SuggestItem item = items.get(i);
390 sb.append(item.type + ": " + item.title);
391 if (i < items.size() - 1) {
392 sb.append(", ");
393 }
394 }
395 return sb.toString();
396 }
397 }
398
399 /**
400 * data object to hold suggestion values
401 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000402 public class SuggestItem {
403 public String title;
404 public String url;
405 public int type;
406 public String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700407
408 public SuggestItem(String text, String u, int t) {
409 title = text;
410 url = u;
411 type = t;
412 }
Michael Kolbbd2dd642011-01-13 13:01:30 -0800413
Michael Kolb21ce4d22010-09-15 14:55:05 -0700414 }
415
416 abstract class CursorSource {
417
418 Cursor mCursor;
419
420 boolean moveToNext() {
421 return mCursor.moveToNext();
422 }
423
424 public abstract void runQuery(CharSequence constraint);
425
426 public abstract SuggestItem getItem();
427
428 public int getCount() {
429 return (mCursor != null) ? mCursor.getCount() : 0;
430 }
431
432 public void close() {
433 if (mCursor != null) {
434 mCursor.close();
435 }
436 }
437 }
438
439 /**
440 * combined bookmark & history source
441 */
442 class CombinedCursor extends CursorSource {
443
444 @Override
445 public SuggestItem getItem() {
446 if ((mCursor != null) && (!mCursor.isAfterLast())) {
447 String title = mCursor.getString(1);
448 String url = mCursor.getString(2);
449 boolean isBookmark = (mCursor.getInt(3) == 1);
450 return new SuggestItem(getTitle(title, url), getUrl(title, url),
451 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
452 }
453 return null;
454 }
455
456 @Override
457 public void runQuery(CharSequence constraint) {
458 // constraint != null
459 if (mCursor != null) {
460 mCursor.close();
461 }
462 String like = constraint + "%";
463 String[] args = null;
464 String selection = null;
465 if (like.startsWith("http") || like.startsWith("file")) {
466 args = new String[1];
467 args[0] = like;
468 selection = "url LIKE ?";
469 } else {
470 args = new String[5];
471 args[0] = "http://" + like;
472 args[1] = "http://www." + like;
473 args[2] = "https://" + like;
474 args[3] = "https://www." + like;
475 // To match against titles.
476 args[4] = like;
477 selection = COMBINED_SELECTION;
478 }
John Reck5e8466f2011-07-28 17:48:58 -0700479 Uri.Builder ub = OmniboxSuggestions.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700480 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
John Reckad373302010-12-17 15:28:13 -0800481 Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
John Reck7ffb8952011-06-27 15:42:39 -0700482 ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY,
John Reck5e8466f2011-07-28 17:48:58 -0700483 OmniboxSuggestions.URL);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700484 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700485 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
John Reck5e8466f2011-07-28 17:48:58 -0700486 selection, (constraint != null) ? args : null, null);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700487 if (mCursor != null) {
488 mCursor.moveToFirst();
489 }
490 }
491
492 /**
493 * Provides the title (text line 1) for a browser suggestion, which should be the
494 * webpage title. If the webpage title is empty, returns the stripped url instead.
495 *
496 * @return the title string to use
497 */
498 private String getTitle(String title, String url) {
499 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700500 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700501 }
502 return title;
503 }
504
505 /**
506 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
507 * webpage url. If the webpage title is empty, then the url should go in the title
508 * instead, and the subtitle should be empty, so this would return null.
509 *
510 * @return the subtitle string to use, or null if none
511 */
512 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700513 if (TextUtils.isEmpty(title)
514 || TextUtils.getTrimmedLength(title) == 0
515 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700516 return null;
517 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700518 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700519 }
520 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700521 }
522
Michael Kolb21ce4d22010-09-15 14:55:05 -0700523 class SuggestCursor extends CursorSource {
524
525 @Override
526 public SuggestItem getItem() {
527 if (mCursor != null) {
528 String title = mCursor.getString(
529 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
530 String text2 = mCursor.getString(
531 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
532 String url = mCursor.getString(
533 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
534 String uri = mCursor.getString(
535 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
536 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800537 SuggestItem item = new SuggestItem(title, url, type);
538 item.extra = mCursor.getString(
539 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
540 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700541 }
542 return null;
543 }
544
545 @Override
546 public void runQuery(CharSequence constraint) {
547 if (mCursor != null) {
548 mCursor.close();
549 }
John Reck35e9dd62011-04-25 09:01:54 -0700550 SearchEngine searchEngine = mSettings.getSearchEngine();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700551 if (!TextUtils.isEmpty(constraint)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700552 if (searchEngine != null && searchEngine.supportsSuggestions()) {
553 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
554 if (mCursor != null) {
555 mCursor.moveToFirst();
556 }
557 }
558 } else {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000559 if (searchEngine.wantsEmptyQuery()) {
560 mCursor = searchEngine.getSuggestions(mContext, "");
561 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700562 mCursor = null;
563 }
564 }
565
566 }
567
Narayan Kamath5119edd2011-02-23 15:49:17 +0000568 private boolean useInstant() {
John Reck35e9dd62011-04-25 09:01:54 -0700569 return mSettings.useInstantSearch();
Narayan Kamath5119edd2011-02-23 15:49:17 +0000570 }
571
John Reck35defff2010-11-11 14:06:45 -0800572 public void clearCache() {
573 mFilterResults = null;
574 mSuggestResults = null;
Narayan Kamath5119edd2011-02-23 15:49:17 +0000575 notifyDataSetInvalidated();
John Reck35defff2010-11-11 14:06:45 -0800576 }
577
John Reck117f07d2011-01-24 09:39:03 -0800578 public void setIncognitoMode(boolean incognito) {
579 mIncognitoMode = incognito;
580 clearCache();
581 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000582
583 static String getSuggestionTitle(SuggestItem item) {
584 // There must be a better way to strip HTML from things.
585 // This method is used in multiple places. It is also more
586 // expensive than a standard html escaper.
587 return (item.title != null) ? Html.fromHtml(item.title).toString() : null;
588 }
589
590 static String getSuggestionUrl(SuggestItem item) {
591 final String title = SuggestionsAdapter.getSuggestionTitle(item);
592
593 if (TextUtils.isEmpty(item.url)) {
594 return title;
595 }
596
597 return item.url;
598 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700599}