blob: 4c0217c3f89655460236bafa4fc63722b4a574d0 [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;
27import android.text.TextUtils;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.View.OnClickListener;
31import android.view.ViewGroup;
32import android.widget.BaseAdapter;
33import android.widget.Filter;
34import android.widget.Filterable;
35import android.widget.ImageView;
Michael Kolb21ce4d22010-09-15 14:55:05 -070036import android.widget.TextView;
37
38import java.util.ArrayList;
39import java.util.List;
Michael Kolb21ce4d22010-09-15 14:55:05 -070040
41/**
42 * adapter to wrap multiple cursors for url/search completions
43 */
44public class SuggestionsAdapter extends BaseAdapter implements Filterable, OnClickListener {
45
John Reck35defff2010-11-11 14:06:45 -080046 static final int TYPE_BOOKMARK = 0;
47 static final int TYPE_SUGGEST_URL = 1;
48 static final int TYPE_HISTORY = 2;
49 static final int TYPE_SEARCH = 3;
50 static final int TYPE_SUGGEST = 4;
Michael Kolb21ce4d22010-09-15 14:55:05 -070051
52 private static final String[] COMBINED_PROJECTION =
53 {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
54 BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
55
56 private static final String[] SEARCHES_PROJECTION = {BrowserContract.Searches.SEARCH};
57
58 private static final String COMBINED_SELECTION =
59 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
60
Michael Kolb21ce4d22010-09-15 14:55:05 -070061 Context mContext;
62 Filter mFilter;
John Reck35defff2010-11-11 14:06:45 -080063 SuggestionResults mMixedResults;
64 List<SuggestItem> mSuggestResults, mFilterResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -070065 List<CursorSource> mSources;
66 boolean mLandscapeMode;
67 CompletionListener mListener;
68 int mLinesPortrait;
69 int mLinesLandscape;
John Reck35defff2010-11-11 14:06:45 -080070 Object mResultsLock = new Object();
Michael Kolbcfa3af52010-12-14 10:36:11 -080071 List<String> mVoiceResults;
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
Michael Kolbcfa3af52010-12-14 10:36:11 -080095 void setVoiceResults(List<String> voiceResults) {
96 mVoiceResults = voiceResults;
97 notifyDataSetInvalidated();
98
99 }
100
Michael Kolb21ce4d22010-09-15 14:55:05 -0700101 public void setLandscapeMode(boolean mode) {
102 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800103 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700104 }
105
106 public int getLeftCount() {
John Reck35defff2010-11-11 14:06:45 -0800107 return mMixedResults.getLeftCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700108 }
109
110 public int getRightCount() {
John Reck35defff2010-11-11 14:06:45 -0800111 return mMixedResults.getRightCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700112 }
113
114 public void addSource(CursorSource c) {
115 if (mSources == null) {
116 mSources = new ArrayList<CursorSource>(5);
117 }
118 mSources.add(c);
119 }
120
121 @Override
122 public void onClick(View v) {
123 if (R.id.icon2 == v.getId()) {
124 // replace input field text with suggestion text
125 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
126 mListener.onSearch(item.title);
127 } else {
128 SuggestItem item = (SuggestItem) v.getTag();
John Reck40f720e2010-11-10 11:57:04 -0800129 mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url),
130 item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700131 }
132 }
133
134 @Override
135 public Filter getFilter() {
136 return mFilter;
137 }
138
139 @Override
140 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800141 if (mVoiceResults != null) {
142 return mVoiceResults.size();
143 }
John Reck35defff2010-11-11 14:06:45 -0800144 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700145 }
146
147 @Override
148 public SuggestItem getItem(int position) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800149 if (mVoiceResults != null) {
150 return new SuggestItem(mVoiceResults.get(position), null,
151 TYPE_SEARCH);
152 }
John Reck35defff2010-11-11 14:06:45 -0800153 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700154 return null;
155 }
156 if (mLandscapeMode) {
John Reck35defff2010-11-11 14:06:45 -0800157 if (position >= mMixedResults.getLineCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700158 // right column
John Reck35defff2010-11-11 14:06:45 -0800159 position = position - mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700160 // index in column
John Reck35defff2010-11-11 14:06:45 -0800161 if (position >= mMixedResults.getRightCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700162 return null;
163 }
John Reck35defff2010-11-11 14:06:45 -0800164 return mMixedResults.items.get(position + mMixedResults.getLeftCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700165 } else {
166 // left column
John Reck35defff2010-11-11 14:06:45 -0800167 if (position >= mMixedResults.getLeftCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700168 return null;
169 }
John Reck35defff2010-11-11 14:06:45 -0800170 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700171 }
172 } else {
John Reck35defff2010-11-11 14:06:45 -0800173 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700174 }
175 }
176
177 @Override
178 public long getItemId(int position) {
179 return 0;
180 }
181
182 @Override
183 public View getView(int position, View convertView, ViewGroup parent) {
184 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800185 View view = convertView;
186 if (view == null) {
187 view = inflater.inflate(R.layout.suggestion_two_column, parent, false);
188 }
189 View s1 = view.findViewById(R.id.suggest1);
190 View s2 = view.findViewById(R.id.suggest2);
191 View div = view.findViewById(R.id.suggestion_divider);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800192 if (mLandscapeMode && (mVoiceResults == null)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700193 SuggestItem item = getItem(position);
John Reck35defff2010-11-11 14:06:45 -0800194 div.setVisibility(View.VISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700195 if (item != null) {
John Reck35defff2010-11-11 14:06:45 -0800196 s1.setVisibility(View.VISIBLE);
197 bindView(s1, item);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700198 } else {
John Reck35defff2010-11-11 14:06:45 -0800199 s1.setVisibility(View.INVISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700200 }
John Reck35defff2010-11-11 14:06:45 -0800201 item = getItem(position + mMixedResults.getLineCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700202 if (item != null) {
John Reck35defff2010-11-11 14:06:45 -0800203 s2.setVisibility(View.VISIBLE);
204 bindView(s2, item);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700205 } else {
John Reck35defff2010-11-11 14:06:45 -0800206 s2.setVisibility(View.INVISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700207 }
208 return view;
209 } else {
John Reck35defff2010-11-11 14:06:45 -0800210 s1.setVisibility(View.VISIBLE);
211 div.setVisibility(View.GONE);
212 s2.setVisibility(View.GONE);
213 bindView(s1, getItem(position));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700214 return view;
215 }
216 }
217
218 private void bindView(View view, SuggestItem item) {
219 // store item for click handling
220 view.setTag(item);
221 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
222 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
223 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
224 View spacer = view.findViewById(R.id.spacer);
225 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700226 View div = view.findViewById(R.id.divider);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700227 tv1.setText(item.title);
228 tv2.setText(item.url);
229 int id = -1;
230 switch (item.type) {
231 case TYPE_SUGGEST:
232 case TYPE_SEARCH:
233 id = R.drawable.ic_search_category_suggest;
234 break;
235 case TYPE_BOOKMARK:
236 id = R.drawable.ic_search_category_bookmark;
237 break;
238 case TYPE_HISTORY:
239 id = R.drawable.ic_search_category_history;
240 break;
241 case TYPE_SUGGEST_URL:
242 id = R.drawable.ic_search_category_browser;
243 break;
244 default:
245 id = -1;
246 }
247 if (id != -1) {
248 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
249 }
250 ic2.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
251 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700252 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700253 spacer.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
254 ? View.GONE : View.INVISIBLE);
255 view.setOnClickListener(this);
256 ic2.setOnClickListener(this);
257 }
258
John Reck35defff2010-11-11 14:06:45 -0800259 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700260
John Reck35defff2010-11-11 14:06:45 -0800261 @Override
262 protected List<SuggestItem> doInBackground(CharSequence... params) {
263 SuggestCursor cursor = new SuggestCursor();
264 cursor.runQuery(params[0]);
265 List<SuggestItem> results = new ArrayList<SuggestItem>();
266 int count = cursor.getCount();
267 for (int i = 0; i < count; i++) {
268 results.add(cursor.getItem());
269 cursor.moveToNext();
270 }
271 cursor.close();
272 return results;
273 }
274
275 @Override
276 protected void onPostExecute(List<SuggestItem> items) {
277 mSuggestResults = items;
278 mMixedResults = buildSuggestionResults();
279 notifyDataSetChanged();
280 mListener.onFilterComplete(mMixedResults.getLineCount());
281 }
282 }
283
284 SuggestionResults buildSuggestionResults() {
285 SuggestionResults mixed = new SuggestionResults();
286 List<SuggestItem> filter, suggest;
287 synchronized (mResultsLock) {
288 filter = mFilterResults;
289 suggest = mSuggestResults;
290 }
291 if (filter != null) {
292 for (SuggestItem item : filter) {
293 mixed.addResult(item);
294 }
295 }
296 if (suggest != null) {
297 for (SuggestItem item : suggest) {
298 mixed.addResult(item);
299 }
300 }
301 return mixed;
302 }
303
304 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700305
306 @Override
307 public CharSequence convertResultToString(Object item) {
308 if (item == null) {
309 return "";
310 }
311 SuggestItem sitem = (SuggestItem) item;
312 if (sitem.title != null) {
313 return sitem.title;
314 } else {
315 return sitem.url;
316 }
317 }
318
John Reck35defff2010-11-11 14:06:45 -0800319 void startSuggestionsAsync(final CharSequence constraint) {
320 new SlowFilterTask().execute(constraint);
321 }
322
Michael Kolb21ce4d22010-09-15 14:55:05 -0700323 @Override
324 protected FilterResults performFiltering(CharSequence constraint) {
325 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800326 if (mVoiceResults == null) {
327 if (TextUtils.isEmpty(constraint)) {
328 res.count = 0;
329 res.values = null;
330 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700331 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800332 startSuggestionsAsync(constraint);
333 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
334 if (constraint != null) {
335 for (CursorSource sc : mSources) {
336 sc.runQuery(constraint);
337 }
338 mixResults(filterResults);
339 }
340 synchronized (mResultsLock) {
341 mFilterResults = filterResults;
342 }
343 SuggestionResults mixed = buildSuggestionResults();
344 res.count = mixed.getLineCount();
345 res.values = mixed;
346 } else {
347 res.count = mVoiceResults.size();
348 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700349 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700350 return res;
351 }
352
John Reck35defff2010-11-11 14:06:45 -0800353 void mixResults(List<SuggestItem> results) {
354 int maxLines = mLandscapeMode ? mLinesLandscape : (mLinesPortrait / 2);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700355 for (int i = 0; i < mSources.size(); i++) {
356 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800357 int n = Math.min(s.getCount(), maxLines);
358 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700359 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700360 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800361 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700362 more = s.moveToNext();
363 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700364 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700365 }
366
367 @Override
368 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800369 if (fresults.values instanceof SuggestionResults) {
370 mMixedResults = (SuggestionResults) fresults.values;
371 mListener.onFilterComplete(fresults.count);
372 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700373 notifyDataSetChanged();
374 }
375
376 }
377
378 /**
379 * sorted list of results of a suggestion query
380 *
381 */
382 class SuggestionResults {
383
384 ArrayList<SuggestItem> items;
385 // count per type
386 int[] counts;
387
388 SuggestionResults() {
389 items = new ArrayList<SuggestItem>(24);
390 // n of types:
391 counts = new int[5];
392 }
393
394 int getTypeCount(int type) {
395 return counts[type];
396 }
397
398 void addResult(SuggestItem item) {
399 int ix = 0;
400 while ((ix < items.size()) && (item.type >= items.get(ix).type))
401 ix++;
402 items.add(ix, item);
403 counts[item.type]++;
404 }
405
406 int getLineCount() {
407 if (mLandscapeMode) {
John Reck35defff2010-11-11 14:06:45 -0800408 return Math.min(mLinesLandscape,
409 Math.max(getLeftCount(), getRightCount()));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700410 } else {
John Reck35defff2010-11-11 14:06:45 -0800411 return Math.min(mLinesPortrait, getLeftCount() + getRightCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700412 }
413 }
414
415 int getLeftCount() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700416 return counts[TYPE_BOOKMARK] + counts[TYPE_HISTORY] + counts[TYPE_SUGGEST_URL];
417 }
418
John Reck35defff2010-11-11 14:06:45 -0800419 int getRightCount() {
420 return counts[TYPE_SEARCH] + counts[TYPE_SUGGEST];
421 }
422
423 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700424 public String toString() {
425 if (items == null) return null;
426 if (items.size() == 0) return "[]";
427 StringBuilder sb = new StringBuilder();
428 for (int i = 0; i < items.size(); i++) {
429 SuggestItem item = items.get(i);
430 sb.append(item.type + ": " + item.title);
431 if (i < items.size() - 1) {
432 sb.append(", ");
433 }
434 }
435 return sb.toString();
436 }
437 }
438
439 /**
440 * data object to hold suggestion values
441 */
442 class SuggestItem {
443 String title;
444 String url;
445 int type;
John Reck40f720e2010-11-10 11:57:04 -0800446 String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700447
448 public SuggestItem(String text, String u, int t) {
449 title = text;
450 url = u;
451 type = t;
452 }
453 }
454
455 abstract class CursorSource {
456
457 Cursor mCursor;
458
459 boolean moveToNext() {
460 return mCursor.moveToNext();
461 }
462
463 public abstract void runQuery(CharSequence constraint);
464
465 public abstract SuggestItem getItem();
466
467 public int getCount() {
468 return (mCursor != null) ? mCursor.getCount() : 0;
469 }
470
471 public void close() {
472 if (mCursor != null) {
473 mCursor.close();
474 }
475 }
476 }
477
478 /**
479 * combined bookmark & history source
480 */
481 class CombinedCursor extends CursorSource {
482
483 @Override
484 public SuggestItem getItem() {
485 if ((mCursor != null) && (!mCursor.isAfterLast())) {
486 String title = mCursor.getString(1);
487 String url = mCursor.getString(2);
488 boolean isBookmark = (mCursor.getInt(3) == 1);
489 return new SuggestItem(getTitle(title, url), getUrl(title, url),
490 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
491 }
492 return null;
493 }
494
495 @Override
496 public void runQuery(CharSequence constraint) {
497 // constraint != null
498 if (mCursor != null) {
499 mCursor.close();
500 }
501 String like = constraint + "%";
502 String[] args = null;
503 String selection = null;
504 if (like.startsWith("http") || like.startsWith("file")) {
505 args = new String[1];
506 args[0] = like;
507 selection = "url LIKE ?";
508 } else {
509 args = new String[5];
510 args[0] = "http://" + like;
511 args[1] = "http://www." + like;
512 args[2] = "https://" + like;
513 args[3] = "https://www." + like;
514 // To match against titles.
515 args[4] = like;
516 selection = COMBINED_SELECTION;
517 }
518 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700519 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700520 Integer.toString(mLinesPortrait));
John Reck3dff1ce2010-12-10 11:13:57 -0800521 BookmarkUtils.addAccountInfo(mContext, ub);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700522 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700523 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700524 selection,
525 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800526 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700527 BrowserContract.Combined.VISITS + " DESC, " +
528 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
529 if (mCursor != null) {
530 mCursor.moveToFirst();
531 }
532 }
533
534 /**
535 * Provides the title (text line 1) for a browser suggestion, which should be the
536 * webpage title. If the webpage title is empty, returns the stripped url instead.
537 *
538 * @return the title string to use
539 */
540 private String getTitle(String title, String url) {
541 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700542 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700543 }
544 return title;
545 }
546
547 /**
548 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
549 * webpage url. If the webpage title is empty, then the url should go in the title
550 * instead, and the subtitle should be empty, so this would return null.
551 *
552 * @return the subtitle string to use, or null if none
553 */
554 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700555 if (TextUtils.isEmpty(title)
556 || TextUtils.getTrimmedLength(title) == 0
557 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700558 return null;
559 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700560 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700561 }
562 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700563 }
564
565 class SearchesCursor extends CursorSource {
566
567 @Override
568 public SuggestItem getItem() {
569 if ((mCursor != null) && (!mCursor.isAfterLast())) {
570 return new SuggestItem(mCursor.getString(0), null, TYPE_SEARCH);
571 }
572 return null;
573 }
574
575 @Override
576 public void runQuery(CharSequence constraint) {
577 // constraint != null
578 if (mCursor != null) {
579 mCursor.close();
580 }
581 String like = constraint + "%";
John Reck04e77442010-11-23 12:58:13 -0800582 String[] args = new String[] {like};
Michael Kolb21ce4d22010-09-15 14:55:05 -0700583 String selection = BrowserContract.Searches.SEARCH + " LIKE ?";
584 Uri.Builder ub = BrowserContract.Searches.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700585 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700586 Integer.toString(mLinesPortrait));
587 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700588 mContext.getContentResolver().query(ub.build(), SEARCHES_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700589 selection,
590 args, BrowserContract.Searches.DATE + " DESC");
591 if (mCursor != null) {
592 mCursor.moveToFirst();
593 }
594 }
595
596 }
597
598 class SuggestCursor extends CursorSource {
599
600 @Override
601 public SuggestItem getItem() {
602 if (mCursor != null) {
603 String title = mCursor.getString(
604 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
605 String text2 = mCursor.getString(
606 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
607 String url = mCursor.getString(
608 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
609 String uri = mCursor.getString(
610 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
611 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800612 SuggestItem item = new SuggestItem(title, url, type);
613 item.extra = mCursor.getString(
614 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
615 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700616 }
617 return null;
618 }
619
620 @Override
621 public void runQuery(CharSequence constraint) {
622 if (mCursor != null) {
623 mCursor.close();
624 }
625 if (!TextUtils.isEmpty(constraint)) {
626 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
627 if (searchEngine != null && searchEngine.supportsSuggestions()) {
628 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
629 if (mCursor != null) {
630 mCursor.moveToFirst();
631 }
632 }
633 } else {
634 mCursor = null;
635 }
636 }
637
638 }
639
John Reck35defff2010-11-11 14:06:45 -0800640 public void clearCache() {
641 mFilterResults = null;
642 mSuggestResults = null;
643 }
644
Michael Kolb21ce4d22010-09-15 14:55:05 -0700645}