blob: 260da6e997416ff870fedfcb800edf0f4576e99a [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;
Michael Kolba50c4462010-12-15 10:49:12 -080097 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -080098 }
99
Michael Kolb21ce4d22010-09-15 14:55:05 -0700100 public void setLandscapeMode(boolean mode) {
101 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800102 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 }
104
105 public int getLeftCount() {
John Reck35defff2010-11-11 14:06:45 -0800106 return mMixedResults.getLeftCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700107 }
108
109 public int getRightCount() {
John Reck35defff2010-11-11 14:06:45 -0800110 return mMixedResults.getRightCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700111 }
112
113 public void addSource(CursorSource c) {
114 if (mSources == null) {
115 mSources = new ArrayList<CursorSource>(5);
116 }
117 mSources.add(c);
118 }
119
120 @Override
121 public void onClick(View v) {
122 if (R.id.icon2 == v.getId()) {
123 // replace input field text with suggestion text
124 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
125 mListener.onSearch(item.title);
126 } else {
127 SuggestItem item = (SuggestItem) v.getTag();
John Reck40f720e2010-11-10 11:57:04 -0800128 mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url),
129 item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700130 }
131 }
132
133 @Override
134 public Filter getFilter() {
135 return mFilter;
136 }
137
138 @Override
139 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800140 if (mVoiceResults != null) {
141 return mVoiceResults.size();
142 }
John Reck35defff2010-11-11 14:06:45 -0800143 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700144 }
145
146 @Override
147 public SuggestItem getItem(int position) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800148 if (mVoiceResults != null) {
149 return new SuggestItem(mVoiceResults.get(position), null,
150 TYPE_SEARCH);
151 }
John Reck35defff2010-11-11 14:06:45 -0800152 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700153 return null;
154 }
155 if (mLandscapeMode) {
John Reck35defff2010-11-11 14:06:45 -0800156 if (position >= mMixedResults.getLineCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700157 // right column
John Reck35defff2010-11-11 14:06:45 -0800158 position = position - mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700159 // index in column
John Reck35defff2010-11-11 14:06:45 -0800160 if (position >= mMixedResults.getRightCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700161 return null;
162 }
John Reck35defff2010-11-11 14:06:45 -0800163 return mMixedResults.items.get(position + mMixedResults.getLeftCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700164 } else {
165 // left column
John Reck35defff2010-11-11 14:06:45 -0800166 if (position >= mMixedResults.getLeftCount()) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700167 return null;
168 }
John Reck35defff2010-11-11 14:06:45 -0800169 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700170 }
171 } else {
John Reck35defff2010-11-11 14:06:45 -0800172 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700173 }
174 }
175
176 @Override
177 public long getItemId(int position) {
178 return 0;
179 }
180
181 @Override
182 public View getView(int position, View convertView, ViewGroup parent) {
183 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800184 View view = convertView;
185 if (view == null) {
186 view = inflater.inflate(R.layout.suggestion_two_column, parent, false);
187 }
188 View s1 = view.findViewById(R.id.suggest1);
189 View s2 = view.findViewById(R.id.suggest2);
190 View div = view.findViewById(R.id.suggestion_divider);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800191 if (mLandscapeMode && (mVoiceResults == null)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700192 SuggestItem item = getItem(position);
John Reck35defff2010-11-11 14:06:45 -0800193 div.setVisibility(View.VISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700194 if (item != null) {
John Reck35defff2010-11-11 14:06:45 -0800195 s1.setVisibility(View.VISIBLE);
196 bindView(s1, item);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700197 } else {
John Reck35defff2010-11-11 14:06:45 -0800198 s1.setVisibility(View.INVISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700199 }
John Reck35defff2010-11-11 14:06:45 -0800200 item = getItem(position + mMixedResults.getLineCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700201 if (item != null) {
John Reck35defff2010-11-11 14:06:45 -0800202 s2.setVisibility(View.VISIBLE);
203 bindView(s2, item);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700204 } else {
John Reck35defff2010-11-11 14:06:45 -0800205 s2.setVisibility(View.INVISIBLE);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700206 }
207 return view;
208 } else {
John Reck35defff2010-11-11 14:06:45 -0800209 s1.setVisibility(View.VISIBLE);
210 div.setVisibility(View.GONE);
211 s2.setVisibility(View.GONE);
212 bindView(s1, getItem(position));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700213 return view;
214 }
215 }
216
217 private void bindView(View view, SuggestItem item) {
218 // store item for click handling
219 view.setTag(item);
220 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
221 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
222 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
223 View spacer = view.findViewById(R.id.spacer);
224 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700225 View div = view.findViewById(R.id.divider);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700226 tv1.setText(item.title);
227 tv2.setText(item.url);
228 int id = -1;
229 switch (item.type) {
230 case TYPE_SUGGEST:
231 case TYPE_SEARCH:
232 id = R.drawable.ic_search_category_suggest;
233 break;
234 case TYPE_BOOKMARK:
235 id = R.drawable.ic_search_category_bookmark;
236 break;
237 case TYPE_HISTORY:
238 id = R.drawable.ic_search_category_history;
239 break;
240 case TYPE_SUGGEST_URL:
241 id = R.drawable.ic_search_category_browser;
242 break;
243 default:
244 id = -1;
245 }
246 if (id != -1) {
247 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
248 }
249 ic2.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
250 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700251 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700252 spacer.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
253 ? View.GONE : View.INVISIBLE);
254 view.setOnClickListener(this);
255 ic2.setOnClickListener(this);
256 }
257
John Reck35defff2010-11-11 14:06:45 -0800258 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700259
John Reck35defff2010-11-11 14:06:45 -0800260 @Override
261 protected List<SuggestItem> doInBackground(CharSequence... params) {
262 SuggestCursor cursor = new SuggestCursor();
263 cursor.runQuery(params[0]);
264 List<SuggestItem> results = new ArrayList<SuggestItem>();
265 int count = cursor.getCount();
266 for (int i = 0; i < count; i++) {
267 results.add(cursor.getItem());
268 cursor.moveToNext();
269 }
270 cursor.close();
271 return results;
272 }
273
274 @Override
275 protected void onPostExecute(List<SuggestItem> items) {
276 mSuggestResults = items;
277 mMixedResults = buildSuggestionResults();
278 notifyDataSetChanged();
279 mListener.onFilterComplete(mMixedResults.getLineCount());
280 }
281 }
282
283 SuggestionResults buildSuggestionResults() {
284 SuggestionResults mixed = new SuggestionResults();
285 List<SuggestItem> filter, suggest;
286 synchronized (mResultsLock) {
287 filter = mFilterResults;
288 suggest = mSuggestResults;
289 }
290 if (filter != null) {
291 for (SuggestItem item : filter) {
292 mixed.addResult(item);
293 }
294 }
295 if (suggest != null) {
296 for (SuggestItem item : suggest) {
297 mixed.addResult(item);
298 }
299 }
300 return mixed;
301 }
302
303 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700304
305 @Override
306 public CharSequence convertResultToString(Object item) {
307 if (item == null) {
308 return "";
309 }
310 SuggestItem sitem = (SuggestItem) item;
311 if (sitem.title != null) {
312 return sitem.title;
313 } else {
314 return sitem.url;
315 }
316 }
317
John Reck35defff2010-11-11 14:06:45 -0800318 void startSuggestionsAsync(final CharSequence constraint) {
319 new SlowFilterTask().execute(constraint);
320 }
321
Michael Kolb21ce4d22010-09-15 14:55:05 -0700322 @Override
323 protected FilterResults performFiltering(CharSequence constraint) {
324 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800325 if (mVoiceResults == null) {
326 if (TextUtils.isEmpty(constraint)) {
327 res.count = 0;
328 res.values = null;
329 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700330 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800331 startSuggestionsAsync(constraint);
332 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
333 if (constraint != null) {
334 for (CursorSource sc : mSources) {
335 sc.runQuery(constraint);
336 }
337 mixResults(filterResults);
338 }
339 synchronized (mResultsLock) {
340 mFilterResults = filterResults;
341 }
342 SuggestionResults mixed = buildSuggestionResults();
343 res.count = mixed.getLineCount();
344 res.values = mixed;
345 } else {
346 res.count = mVoiceResults.size();
347 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700348 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700349 return res;
350 }
351
John Reck35defff2010-11-11 14:06:45 -0800352 void mixResults(List<SuggestItem> results) {
353 int maxLines = mLandscapeMode ? mLinesLandscape : (mLinesPortrait / 2);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700354 for (int i = 0; i < mSources.size(); i++) {
355 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800356 int n = Math.min(s.getCount(), maxLines);
357 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700358 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700359 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800360 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700361 more = s.moveToNext();
362 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700363 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700364 }
365
366 @Override
367 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800368 if (fresults.values instanceof SuggestionResults) {
369 mMixedResults = (SuggestionResults) fresults.values;
370 mListener.onFilterComplete(fresults.count);
371 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700372 notifyDataSetChanged();
373 }
374
375 }
376
377 /**
378 * sorted list of results of a suggestion query
379 *
380 */
381 class SuggestionResults {
382
383 ArrayList<SuggestItem> items;
384 // count per type
385 int[] counts;
386
387 SuggestionResults() {
388 items = new ArrayList<SuggestItem>(24);
389 // n of types:
390 counts = new int[5];
391 }
392
393 int getTypeCount(int type) {
394 return counts[type];
395 }
396
397 void addResult(SuggestItem item) {
398 int ix = 0;
399 while ((ix < items.size()) && (item.type >= items.get(ix).type))
400 ix++;
401 items.add(ix, item);
402 counts[item.type]++;
403 }
404
405 int getLineCount() {
406 if (mLandscapeMode) {
John Reck35defff2010-11-11 14:06:45 -0800407 return Math.min(mLinesLandscape,
408 Math.max(getLeftCount(), getRightCount()));
Michael Kolb21ce4d22010-09-15 14:55:05 -0700409 } else {
John Reck35defff2010-11-11 14:06:45 -0800410 return Math.min(mLinesPortrait, getLeftCount() + getRightCount());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700411 }
412 }
413
414 int getLeftCount() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700415 return counts[TYPE_BOOKMARK] + counts[TYPE_HISTORY] + counts[TYPE_SUGGEST_URL];
416 }
417
John Reck35defff2010-11-11 14:06:45 -0800418 int getRightCount() {
419 return counts[TYPE_SEARCH] + counts[TYPE_SUGGEST];
420 }
421
422 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700423 public String toString() {
424 if (items == null) return null;
425 if (items.size() == 0) return "[]";
426 StringBuilder sb = new StringBuilder();
427 for (int i = 0; i < items.size(); i++) {
428 SuggestItem item = items.get(i);
429 sb.append(item.type + ": " + item.title);
430 if (i < items.size() - 1) {
431 sb.append(", ");
432 }
433 }
434 return sb.toString();
435 }
436 }
437
438 /**
439 * data object to hold suggestion values
440 */
441 class SuggestItem {
442 String title;
443 String url;
444 int type;
John Reck40f720e2010-11-10 11:57:04 -0800445 String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700446
447 public SuggestItem(String text, String u, int t) {
448 title = text;
449 url = u;
450 type = t;
451 }
452 }
453
454 abstract class CursorSource {
455
456 Cursor mCursor;
457
458 boolean moveToNext() {
459 return mCursor.moveToNext();
460 }
461
462 public abstract void runQuery(CharSequence constraint);
463
464 public abstract SuggestItem getItem();
465
466 public int getCount() {
467 return (mCursor != null) ? mCursor.getCount() : 0;
468 }
469
470 public void close() {
471 if (mCursor != null) {
472 mCursor.close();
473 }
474 }
475 }
476
477 /**
478 * combined bookmark & history source
479 */
480 class CombinedCursor extends CursorSource {
481
482 @Override
483 public SuggestItem getItem() {
484 if ((mCursor != null) && (!mCursor.isAfterLast())) {
485 String title = mCursor.getString(1);
486 String url = mCursor.getString(2);
487 boolean isBookmark = (mCursor.getInt(3) == 1);
488 return new SuggestItem(getTitle(title, url), getUrl(title, url),
489 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
490 }
491 return null;
492 }
493
494 @Override
495 public void runQuery(CharSequence constraint) {
496 // constraint != null
497 if (mCursor != null) {
498 mCursor.close();
499 }
500 String like = constraint + "%";
501 String[] args = null;
502 String selection = null;
503 if (like.startsWith("http") || like.startsWith("file")) {
504 args = new String[1];
505 args[0] = like;
506 selection = "url LIKE ?";
507 } else {
508 args = new String[5];
509 args[0] = "http://" + like;
510 args[1] = "http://www." + like;
511 args[2] = "https://" + like;
512 args[3] = "https://www." + like;
513 // To match against titles.
514 args[4] = like;
515 selection = COMBINED_SELECTION;
516 }
517 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700518 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700519 Integer.toString(mLinesPortrait));
John Reck3dff1ce2010-12-10 11:13:57 -0800520 BookmarkUtils.addAccountInfo(mContext, ub);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700521 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700522 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700523 selection,
524 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800525 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700526 BrowserContract.Combined.VISITS + " DESC, " +
527 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
528 if (mCursor != null) {
529 mCursor.moveToFirst();
530 }
531 }
532
533 /**
534 * Provides the title (text line 1) for a browser suggestion, which should be the
535 * webpage title. If the webpage title is empty, returns the stripped url instead.
536 *
537 * @return the title string to use
538 */
539 private String getTitle(String title, String url) {
540 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700541 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700542 }
543 return title;
544 }
545
546 /**
547 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
548 * webpage url. If the webpage title is empty, then the url should go in the title
549 * instead, and the subtitle should be empty, so this would return null.
550 *
551 * @return the subtitle string to use, or null if none
552 */
553 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700554 if (TextUtils.isEmpty(title)
555 || TextUtils.getTrimmedLength(title) == 0
556 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700557 return null;
558 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700559 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700560 }
561 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700562 }
563
564 class SearchesCursor extends CursorSource {
565
566 @Override
567 public SuggestItem getItem() {
568 if ((mCursor != null) && (!mCursor.isAfterLast())) {
569 return new SuggestItem(mCursor.getString(0), null, TYPE_SEARCH);
570 }
571 return null;
572 }
573
574 @Override
575 public void runQuery(CharSequence constraint) {
576 // constraint != null
577 if (mCursor != null) {
578 mCursor.close();
579 }
580 String like = constraint + "%";
John Reck04e77442010-11-23 12:58:13 -0800581 String[] args = new String[] {like};
Michael Kolb21ce4d22010-09-15 14:55:05 -0700582 String selection = BrowserContract.Searches.SEARCH + " LIKE ?";
583 Uri.Builder ub = BrowserContract.Searches.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700584 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700585 Integer.toString(mLinesPortrait));
586 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700587 mContext.getContentResolver().query(ub.build(), SEARCHES_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700588 selection,
589 args, BrowserContract.Searches.DATE + " DESC");
590 if (mCursor != null) {
591 mCursor.moveToFirst();
592 }
593 }
594
595 }
596
597 class SuggestCursor extends CursorSource {
598
599 @Override
600 public SuggestItem getItem() {
601 if (mCursor != null) {
602 String title = mCursor.getString(
603 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
604 String text2 = mCursor.getString(
605 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
606 String url = mCursor.getString(
607 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
608 String uri = mCursor.getString(
609 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
610 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800611 SuggestItem item = new SuggestItem(title, url, type);
612 item.extra = mCursor.getString(
613 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
614 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700615 }
616 return null;
617 }
618
619 @Override
620 public void runQuery(CharSequence constraint) {
621 if (mCursor != null) {
622 mCursor.close();
623 }
624 if (!TextUtils.isEmpty(constraint)) {
625 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
626 if (searchEngine != null && searchEngine.supportsSuggestions()) {
627 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
628 if (mCursor != null) {
629 mCursor.moveToFirst();
630 }
631 }
632 } else {
633 mCursor = null;
634 }
635 }
636
637 }
638
John Reck35defff2010-11-11 14:06:45 -0800639 public void clearCache() {
640 mFilterResults = null;
641 mSuggestResults = null;
642 }
643
Michael Kolb21ce4d22010-09-15 14:55:05 -0700644}