blob: 6473dfd297a709fc0a9382959263a12380c00bda [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;
26import android.os.Handler;
Michael Kolb21ce4d22010-09-15 14:55:05 -070027import android.provider.BrowserContract;
28import 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 */
45public class SuggestionsAdapter extends BaseAdapter implements Filterable, OnClickListener {
46
John Reck35defff2010-11-11 14:06:45 -080047 static final int TYPE_BOOKMARK = 0;
48 static final int TYPE_SUGGEST_URL = 1;
49 static final int TYPE_HISTORY = 2;
50 static final int TYPE_SEARCH = 3;
51 static final int TYPE_SUGGEST = 4;
Michael Kolb21ce4d22010-09-15 14:55:05 -070052
53 private static final String[] COMBINED_PROJECTION =
54 {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
55 BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
56
57 private static final String[] SEARCHES_PROJECTION = {BrowserContract.Searches.SEARCH};
58
59 private static final String COMBINED_SELECTION =
60 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
61
Michael Kolb21ce4d22010-09-15 14:55:05 -070062 Context mContext;
63 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;
68 CompletionListener mListener;
69 int mLinesPortrait;
70 int mLinesLandscape;
John Reck35defff2010-11-11 14:06:45 -080071 Object mResultsLock = new Object();
Michael Kolb21ce4d22010-09-15 14:55:05 -070072
73 interface CompletionListener {
74
75 public void onSearch(String txt);
76
John Reck40f720e2010-11-10 11:57:04 -080077 public void onSelect(String txt, String extraData);
Michael Kolb21ce4d22010-09-15 14:55:05 -070078
Michael Kolb0506f2d2010-10-14 16:20:16 -070079 public void onFilterComplete(int count);
80
Michael Kolb21ce4d22010-09-15 14:55:05 -070081 }
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);
90 mFilter = new SuggestFilter();
Michael Kolb21ce4d22010-09-15 14:55:05 -070091 addSource(new SearchesCursor());
92 addSource(new CombinedCursor());
93 }
94
95 public void setLandscapeMode(boolean mode) {
96 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -080097 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -070098 }
99
100 public int getLeftCount() {
John Reck35defff2010-11-11 14:06:45 -0800101 return mMixedResults.getLeftCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700102 }
103
104 public int getRightCount() {
John Reck35defff2010-11-11 14:06:45 -0800105 return mMixedResults.getRightCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 }
107
108 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) {
117 if (R.id.icon2 == v.getId()) {
118 // replace input field text with suggestion text
119 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
120 mListener.onSearch(item.title);
121 } else {
122 SuggestItem item = (SuggestItem) v.getTag();
John Reck40f720e2010-11-10 11:57:04 -0800123 mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url),
124 item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700125 }
126 }
127
128 @Override
129 public Filter getFilter() {
130 return mFilter;
131 }
132
133 @Override
134 public int getCount() {
John Reck35defff2010-11-11 14:06:45 -0800135 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700136 }
137
138 @Override
139 public SuggestItem getItem(int position) {
John Reck35defff2010-11-11 14:06:45 -0800140 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700141 return null;
142 }
143 if (mLandscapeMode) {
John Reck35defff2010-11-11 14:06:45 -0800144 if (position >= mMixedResults.getLineCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700145 // right column
John Reck35defff2010-11-11 14:06:45 -0800146 position = position - mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700147 // index in column
John Reck35defff2010-11-11 14:06:45 -0800148 if (position >= mMixedResults.getRightCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700149 return null;
150 }
John Reck35defff2010-11-11 14:06:45 -0800151 return mMixedResults.items.get(position + mMixedResults.getLeftCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700152 } else {
153 // left column
John Reck35defff2010-11-11 14:06:45 -0800154 if (position >= mMixedResults.getLeftCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700155 return null;
156 }
John Reck35defff2010-11-11 14:06:45 -0800157 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700158 }
159 } else {
John Reck35defff2010-11-11 14:06:45 -0800160 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700161 }
162 }
163
164 @Override
165 public long getItemId(int position) {
166 return 0;
167 }
168
169 @Override
170 public View getView(int position, View convertView, ViewGroup parent) {
171 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800172 View view = convertView;
173 if (view == null) {
174 view = inflater.inflate(R.layout.suggestion_two_column, parent, false);
175 }
176 View s1 = view.findViewById(R.id.suggest1);
177 View s2 = view.findViewById(R.id.suggest2);
178 View div = view.findViewById(R.id.suggestion_divider);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700179 if (mLandscapeMode) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700180 SuggestItem item = getItem(position);
John Reck35defff2010-11-11 14:06:45 -0800181 div.setVisibility(View.VISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700182 if (item != null) {
John Reck35defff2010-11-11 14:06:45 -0800183 s1.setVisibility(View.VISIBLE);
184 bindView(s1, item);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700185 } else {
John Reck35defff2010-11-11 14:06:45 -0800186 s1.setVisibility(View.INVISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700187 }
John Reck35defff2010-11-11 14:06:45 -0800188 item = getItem(position + mMixedResults.getLineCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700189 if (item != null) {
John Reck35defff2010-11-11 14:06:45 -0800190 s2.setVisibility(View.VISIBLE);
191 bindView(s2, item);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700192 } else {
John Reck35defff2010-11-11 14:06:45 -0800193 s2.setVisibility(View.INVISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700194 }
195 return view;
196 } else {
John Reck35defff2010-11-11 14:06:45 -0800197 s1.setVisibility(View.VISIBLE);
198 div.setVisibility(View.GONE);
199 s2.setVisibility(View.GONE);
200 bindView(s1, getItem(position));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700201 return view;
202 }
203 }
204
205 private void bindView(View view, SuggestItem item) {
206 // store item for click handling
207 view.setTag(item);
208 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
209 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
210 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
211 View spacer = view.findViewById(R.id.spacer);
212 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700213 View div = view.findViewById(R.id.divider);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700214 tv1.setText(item.title);
215 tv2.setText(item.url);
216 int id = -1;
217 switch (item.type) {
218 case TYPE_SUGGEST:
219 case TYPE_SEARCH:
220 id = R.drawable.ic_search_category_suggest;
221 break;
222 case TYPE_BOOKMARK:
223 id = R.drawable.ic_search_category_bookmark;
224 break;
225 case TYPE_HISTORY:
226 id = R.drawable.ic_search_category_history;
227 break;
228 case TYPE_SUGGEST_URL:
229 id = R.drawable.ic_search_category_browser;
230 break;
231 default:
232 id = -1;
233 }
234 if (id != -1) {
235 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
236 }
237 ic2.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
238 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700239 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700240 spacer.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
241 ? View.GONE : View.INVISIBLE);
242 view.setOnClickListener(this);
243 ic2.setOnClickListener(this);
244 }
245
John Reck35defff2010-11-11 14:06:45 -0800246 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700247
John Reck35defff2010-11-11 14:06:45 -0800248 @Override
249 protected List<SuggestItem> doInBackground(CharSequence... params) {
250 SuggestCursor cursor = new SuggestCursor();
251 cursor.runQuery(params[0]);
252 List<SuggestItem> results = new ArrayList<SuggestItem>();
253 int count = cursor.getCount();
254 for (int i = 0; i < count; i++) {
255 results.add(cursor.getItem());
256 cursor.moveToNext();
257 }
258 cursor.close();
259 return results;
260 }
261
262 @Override
263 protected void onPostExecute(List<SuggestItem> items) {
264 mSuggestResults = items;
265 mMixedResults = buildSuggestionResults();
266 notifyDataSetChanged();
267 mListener.onFilterComplete(mMixedResults.getLineCount());
268 }
269 }
270
271 SuggestionResults buildSuggestionResults() {
272 SuggestionResults mixed = new SuggestionResults();
273 List<SuggestItem> filter, suggest;
274 synchronized (mResultsLock) {
275 filter = mFilterResults;
276 suggest = mSuggestResults;
277 }
278 if (filter != null) {
279 for (SuggestItem item : filter) {
280 mixed.addResult(item);
281 }
282 }
283 if (suggest != null) {
284 for (SuggestItem item : suggest) {
285 mixed.addResult(item);
286 }
287 }
288 return mixed;
289 }
290
291 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700292
293 @Override
294 public CharSequence convertResultToString(Object item) {
295 if (item == null) {
296 return "";
297 }
298 SuggestItem sitem = (SuggestItem) item;
299 if (sitem.title != null) {
300 return sitem.title;
301 } else {
302 return sitem.url;
303 }
304 }
305
John Reck35defff2010-11-11 14:06:45 -0800306 void startSuggestionsAsync(final CharSequence constraint) {
307 new SlowFilterTask().execute(constraint);
308 }
309
Michael Kolb21ce4d22010-09-15 14:55:05 -0700310 @Override
311 protected FilterResults performFiltering(CharSequence constraint) {
312 FilterResults res = new FilterResults();
313 if (TextUtils.isEmpty(constraint)) {
314 res.count = 0;
315 res.values = null;
316 return res;
317 }
John Reck35defff2010-11-11 14:06:45 -0800318 startSuggestionsAsync(constraint);
319 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700320 if (constraint != null) {
321 for (CursorSource sc : mSources) {
322 sc.runQuery(constraint);
323 }
John Reck35defff2010-11-11 14:06:45 -0800324 mixResults(filterResults);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700325 }
John Reck35defff2010-11-11 14:06:45 -0800326 synchronized (mResultsLock) {
327 mFilterResults = filterResults;
328 }
329 SuggestionResults mixed = buildSuggestionResults();
330 res.count = mixed.getLineCount();
331 res.values = mixed;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700332 return res;
333 }
334
John Reck35defff2010-11-11 14:06:45 -0800335 void mixResults(List<SuggestItem> results) {
336 int maxLines = mLandscapeMode ? mLinesLandscape : (mLinesPortrait / 2);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700337 for (int i = 0; i < mSources.size(); i++) {
338 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800339 int n = Math.min(s.getCount(), maxLines);
340 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700341 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700342 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800343 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700344 more = s.moveToNext();
345 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700346 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700347 }
348
349 @Override
350 protected void publishResults(CharSequence constraint, FilterResults fresults) {
John Reck35defff2010-11-11 14:06:45 -0800351 mMixedResults = (SuggestionResults) fresults.values;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700352 mListener.onFilterComplete(fresults.count);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700353 notifyDataSetChanged();
354 }
355
356 }
357
358 /**
359 * sorted list of results of a suggestion query
360 *
361 */
362 class SuggestionResults {
363
364 ArrayList<SuggestItem> items;
365 // count per type
366 int[] counts;
367
368 SuggestionResults() {
369 items = new ArrayList<SuggestItem>(24);
370 // n of types:
371 counts = new int[5];
372 }
373
374 int getTypeCount(int type) {
375 return counts[type];
376 }
377
378 void addResult(SuggestItem item) {
379 int ix = 0;
380 while ((ix < items.size()) && (item.type >= items.get(ix).type))
381 ix++;
382 items.add(ix, item);
383 counts[item.type]++;
384 }
385
386 int getLineCount() {
387 if (mLandscapeMode) {
John Reck35defff2010-11-11 14:06:45 -0800388 return Math.min(mLinesLandscape,
389 Math.max(getLeftCount(), getRightCount()));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700390 } else {
John Reck35defff2010-11-11 14:06:45 -0800391 return Math.min(mLinesPortrait, getLeftCount() + getRightCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700392 }
393 }
394
395 int getLeftCount() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700396 return counts[TYPE_BOOKMARK] + counts[TYPE_HISTORY] + counts[TYPE_SUGGEST_URL];
397 }
398
John Reck35defff2010-11-11 14:06:45 -0800399 int getRightCount() {
400 return counts[TYPE_SEARCH] + counts[TYPE_SUGGEST];
401 }
402
403 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700404 public String toString() {
405 if (items == null) return null;
406 if (items.size() == 0) return "[]";
407 StringBuilder sb = new StringBuilder();
408 for (int i = 0; i < items.size(); i++) {
409 SuggestItem item = items.get(i);
410 sb.append(item.type + ": " + item.title);
411 if (i < items.size() - 1) {
412 sb.append(", ");
413 }
414 }
415 return sb.toString();
416 }
417 }
418
419 /**
420 * data object to hold suggestion values
421 */
422 class SuggestItem {
423 String title;
424 String url;
425 int type;
John Reck40f720e2010-11-10 11:57:04 -0800426 String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700427
428 public SuggestItem(String text, String u, int t) {
429 title = text;
430 url = u;
431 type = t;
432 }
433 }
434
435 abstract class CursorSource {
436
437 Cursor mCursor;
438
439 boolean moveToNext() {
440 return mCursor.moveToNext();
441 }
442
443 public abstract void runQuery(CharSequence constraint);
444
445 public abstract SuggestItem getItem();
446
447 public int getCount() {
448 return (mCursor != null) ? mCursor.getCount() : 0;
449 }
450
451 public void close() {
452 if (mCursor != null) {
453 mCursor.close();
454 }
455 }
456 }
457
458 /**
459 * combined bookmark & history source
460 */
461 class CombinedCursor extends CursorSource {
462
463 @Override
464 public SuggestItem getItem() {
465 if ((mCursor != null) && (!mCursor.isAfterLast())) {
466 String title = mCursor.getString(1);
467 String url = mCursor.getString(2);
468 boolean isBookmark = (mCursor.getInt(3) == 1);
469 return new SuggestItem(getTitle(title, url), getUrl(title, url),
470 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
471 }
472 return null;
473 }
474
475 @Override
476 public void runQuery(CharSequence constraint) {
477 // constraint != null
478 if (mCursor != null) {
479 mCursor.close();
480 }
481 String like = constraint + "%";
482 String[] args = null;
483 String selection = null;
484 if (like.startsWith("http") || like.startsWith("file")) {
485 args = new String[1];
486 args[0] = like;
487 selection = "url LIKE ?";
488 } else {
489 args = new String[5];
490 args[0] = "http://" + like;
491 args[1] = "http://www." + like;
492 args[2] = "https://" + like;
493 args[3] = "https://www." + like;
494 // To match against titles.
495 args[4] = like;
496 selection = COMBINED_SELECTION;
497 }
498 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700499 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700500 Integer.toString(mLinesPortrait));
John Reck3dff1ce2010-12-10 11:13:57 -0800501 BookmarkUtils.addAccountInfo(mContext, ub);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700502 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700503 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700504 selection,
505 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800506 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700507 BrowserContract.Combined.VISITS + " DESC, " +
508 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
509 if (mCursor != null) {
510 mCursor.moveToFirst();
511 }
512 }
513
514 /**
515 * Provides the title (text line 1) for a browser suggestion, which should be the
516 * webpage title. If the webpage title is empty, returns the stripped url instead.
517 *
518 * @return the title string to use
519 */
520 private String getTitle(String title, String url) {
521 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700522 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700523 }
524 return title;
525 }
526
527 /**
528 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
529 * webpage url. If the webpage title is empty, then the url should go in the title
530 * instead, and the subtitle should be empty, so this would return null.
531 *
532 * @return the subtitle string to use, or null if none
533 */
534 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700535 if (TextUtils.isEmpty(title)
536 || TextUtils.getTrimmedLength(title) == 0
537 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700538 return null;
539 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700540 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700541 }
542 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700543 }
544
545 class SearchesCursor extends CursorSource {
546
547 @Override
548 public SuggestItem getItem() {
549 if ((mCursor != null) && (!mCursor.isAfterLast())) {
550 return new SuggestItem(mCursor.getString(0), null, TYPE_SEARCH);
551 }
552 return null;
553 }
554
555 @Override
556 public void runQuery(CharSequence constraint) {
557 // constraint != null
558 if (mCursor != null) {
559 mCursor.close();
560 }
561 String like = constraint + "%";
John Reck04e77442010-11-23 12:58:13 -0800562 String[] args = new String[] {like};
Michael Kolb21ce4d22010-09-15 14:55:05 -0700563 String selection = BrowserContract.Searches.SEARCH + " LIKE ?";
564 Uri.Builder ub = BrowserContract.Searches.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700565 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700566 Integer.toString(mLinesPortrait));
567 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700568 mContext.getContentResolver().query(ub.build(), SEARCHES_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700569 selection,
570 args, BrowserContract.Searches.DATE + " DESC");
571 if (mCursor != null) {
572 mCursor.moveToFirst();
573 }
574 }
575
576 }
577
578 class SuggestCursor extends CursorSource {
579
580 @Override
581 public SuggestItem getItem() {
582 if (mCursor != null) {
583 String title = mCursor.getString(
584 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
585 String text2 = mCursor.getString(
586 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
587 String url = mCursor.getString(
588 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
589 String uri = mCursor.getString(
590 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
591 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800592 SuggestItem item = new SuggestItem(title, url, type);
593 item.extra = mCursor.getString(
594 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
595 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700596 }
597 return null;
598 }
599
600 @Override
601 public void runQuery(CharSequence constraint) {
602 if (mCursor != null) {
603 mCursor.close();
604 }
605 if (!TextUtils.isEmpty(constraint)) {
606 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
607 if (searchEngine != null && searchEngine.supportsSuggestions()) {
608 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
609 if (mCursor != null) {
610 mCursor.moveToFirst();
611 }
612 }
613 } else {
614 mCursor = null;
615 }
616 }
617
618 }
619
John Reck35defff2010-11-11 14:06:45 -0800620 public void clearCache() {
621 mFilterResults = null;
622 mSuggestResults = null;
623 }
624
Michael Kolb21ce4d22010-09-15 14:55:05 -0700625}