blob: e2d938636bdcada5638c6f4e4b683d7812312a5c [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
Michael Kolb21ce4d22010-09-15 14:55:05 -070056 private static final String COMBINED_SELECTION =
57 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
58
Michael Kolb21ce4d22010-09-15 14:55:05 -070059 Context mContext;
60 Filter mFilter;
John Reck35defff2010-11-11 14:06:45 -080061 SuggestionResults mMixedResults;
62 List<SuggestItem> mSuggestResults, mFilterResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -070063 List<CursorSource> mSources;
64 boolean mLandscapeMode;
65 CompletionListener mListener;
66 int mLinesPortrait;
67 int mLinesLandscape;
John Reck35defff2010-11-11 14:06:45 -080068 Object mResultsLock = new Object();
Michael Kolbcfa3af52010-12-14 10:36:11 -080069 List<String> mVoiceResults;
John Reck1605bef2011-01-10 18:11:18 -080070 boolean mReverseResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -070071
72 interface CompletionListener {
73
74 public void onSearch(String txt);
75
John Reck40f720e2010-11-10 11:57:04 -080076 public void onSelect(String txt, String extraData);
Michael Kolb21ce4d22010-09-15 14:55:05 -070077
Michael Kolb0506f2d2010-10-14 16:20:16 -070078 public void onFilterComplete(int count);
79
Michael Kolb21ce4d22010-09-15 14:55:05 -070080 }
81
82 public SuggestionsAdapter(Context ctx, CompletionListener listener) {
83 mContext = ctx;
84 mListener = listener;
85 mLinesPortrait = mContext.getResources().
86 getInteger(R.integer.max_suggest_lines_portrait);
87 mLinesLandscape = mContext.getResources().
88 getInteger(R.integer.max_suggest_lines_landscape);
89 mFilter = new SuggestFilter();
Michael Kolb21ce4d22010-09-15 14:55:05 -070090 addSource(new CombinedCursor());
91 }
92
Michael Kolbcfa3af52010-12-14 10:36:11 -080093 void setVoiceResults(List<String> voiceResults) {
94 mVoiceResults = voiceResults;
Michael Kolba50c4462010-12-15 10:49:12 -080095 notifyDataSetChanged();
Michael Kolbcfa3af52010-12-14 10:36:11 -080096 }
97
Michael Kolb21ce4d22010-09-15 14:55:05 -070098 public void setLandscapeMode(boolean mode) {
99 mLandscapeMode = mode;
John Reck35defff2010-11-11 14:06:45 -0800100 notifyDataSetChanged();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700101 }
102
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 public void addSource(CursorSource c) {
104 if (mSources == null) {
105 mSources = new ArrayList<CursorSource>(5);
106 }
107 mSources.add(c);
108 }
109
110 @Override
111 public void onClick(View v) {
John Reckad373302010-12-17 15:28:13 -0800112 SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700113 if (R.id.icon2 == v.getId()) {
114 // replace input field text with suggestion text
Michael Kolb21ce4d22010-09-15 14:55:05 -0700115 mListener.onSearch(item.title);
116 } else {
John Reck40f720e2010-11-10 11:57:04 -0800117 mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url),
118 item.extra);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700119 }
120 }
121
122 @Override
123 public Filter getFilter() {
124 return mFilter;
125 }
126
127 @Override
128 public int getCount() {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800129 if (mVoiceResults != null) {
130 return mVoiceResults.size();
131 }
John Reck35defff2010-11-11 14:06:45 -0800132 return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700133 }
134
135 @Override
136 public SuggestItem getItem(int position) {
John Reck1605bef2011-01-10 18:11:18 -0800137 if (mReverseResults) {
138 position = (getCount() - 1) - position;
139 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800140 if (mVoiceResults != null) {
141 return new SuggestItem(mVoiceResults.get(position), null,
142 TYPE_SEARCH);
143 }
John Reck35defff2010-11-11 14:06:45 -0800144 if (mMixedResults == null) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700145 return null;
146 }
John Reckad373302010-12-17 15:28:13 -0800147 return mMixedResults.items.get(position);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700148 }
149
John Reck1605bef2011-01-10 18:11:18 -0800150 public void setReverseResults(boolean reverse) {
151 mReverseResults = reverse;
152 }
153
Michael Kolb21ce4d22010-09-15 14:55:05 -0700154 @Override
155 public long getItemId(int position) {
John Reck1605bef2011-01-10 18:11:18 -0800156 return position;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700157 }
158
159 @Override
160 public View getView(int position, View convertView, ViewGroup parent) {
161 final LayoutInflater inflater = LayoutInflater.from(mContext);
John Reck35defff2010-11-11 14:06:45 -0800162 View view = convertView;
163 if (view == null) {
John Reckad373302010-12-17 15:28:13 -0800164 view = inflater.inflate(R.layout.suggestion_item, parent, false);
John Reck35defff2010-11-11 14:06:45 -0800165 }
John Reckad373302010-12-17 15:28:13 -0800166 bindView(view, getItem(position));
167 return view;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700168 }
169
170 private void bindView(View view, SuggestItem item) {
171 // store item for click handling
172 view.setTag(item);
173 TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
174 TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
175 ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700176 View ic2 = view.findViewById(R.id.icon2);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700177 View div = view.findViewById(R.id.divider);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700178 tv1.setText(item.title);
John Reckad373302010-12-17 15:28:13 -0800179 if (TextUtils.isEmpty(item.url)) {
180 tv2.setVisibility(View.GONE);
181 } else {
182 tv2.setVisibility(View.VISIBLE);
183 tv2.setText(item.url);
184 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700185 int id = -1;
186 switch (item.type) {
187 case TYPE_SUGGEST:
188 case TYPE_SEARCH:
189 id = R.drawable.ic_search_category_suggest;
190 break;
191 case TYPE_BOOKMARK:
192 id = R.drawable.ic_search_category_bookmark;
193 break;
194 case TYPE_HISTORY:
195 id = R.drawable.ic_search_category_history;
196 break;
197 case TYPE_SUGGEST_URL:
198 id = R.drawable.ic_search_category_browser;
199 break;
200 default:
201 id = -1;
202 }
203 if (id != -1) {
204 ic1.setImageDrawable(mContext.getResources().getDrawable(id));
205 }
206 ic2.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
207 ? View.VISIBLE : View.GONE);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700208 div.setVisibility(ic2.getVisibility());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700209 ic2.setOnClickListener(this);
John Reckad373302010-12-17 15:28:13 -0800210 view.findViewById(R.id.suggestion).setOnClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700211 }
212
John Reck35defff2010-11-11 14:06:45 -0800213 class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700214
John Reck35defff2010-11-11 14:06:45 -0800215 @Override
216 protected List<SuggestItem> doInBackground(CharSequence... params) {
217 SuggestCursor cursor = new SuggestCursor();
218 cursor.runQuery(params[0]);
219 List<SuggestItem> results = new ArrayList<SuggestItem>();
220 int count = cursor.getCount();
221 for (int i = 0; i < count; i++) {
222 results.add(cursor.getItem());
223 cursor.moveToNext();
224 }
225 cursor.close();
226 return results;
227 }
228
229 @Override
230 protected void onPostExecute(List<SuggestItem> items) {
231 mSuggestResults = items;
232 mMixedResults = buildSuggestionResults();
233 notifyDataSetChanged();
234 mListener.onFilterComplete(mMixedResults.getLineCount());
235 }
236 }
237
238 SuggestionResults buildSuggestionResults() {
239 SuggestionResults mixed = new SuggestionResults();
240 List<SuggestItem> filter, suggest;
241 synchronized (mResultsLock) {
242 filter = mFilterResults;
243 suggest = mSuggestResults;
244 }
245 if (filter != null) {
246 for (SuggestItem item : filter) {
247 mixed.addResult(item);
248 }
249 }
250 if (suggest != null) {
251 for (SuggestItem item : suggest) {
252 mixed.addResult(item);
253 }
254 }
255 return mixed;
256 }
257
258 class SuggestFilter extends Filter {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700259
260 @Override
261 public CharSequence convertResultToString(Object item) {
262 if (item == null) {
263 return "";
264 }
265 SuggestItem sitem = (SuggestItem) item;
266 if (sitem.title != null) {
267 return sitem.title;
268 } else {
269 return sitem.url;
270 }
271 }
272
John Reck35defff2010-11-11 14:06:45 -0800273 void startSuggestionsAsync(final CharSequence constraint) {
274 new SlowFilterTask().execute(constraint);
275 }
276
Michael Kolb21ce4d22010-09-15 14:55:05 -0700277 @Override
278 protected FilterResults performFiltering(CharSequence constraint) {
279 FilterResults res = new FilterResults();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800280 if (mVoiceResults == null) {
281 if (TextUtils.isEmpty(constraint)) {
282 res.count = 0;
283 res.values = null;
284 return res;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700285 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800286 startSuggestionsAsync(constraint);
287 List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
288 if (constraint != null) {
289 for (CursorSource sc : mSources) {
290 sc.runQuery(constraint);
291 }
292 mixResults(filterResults);
293 }
294 synchronized (mResultsLock) {
295 mFilterResults = filterResults;
296 }
297 SuggestionResults mixed = buildSuggestionResults();
298 res.count = mixed.getLineCount();
299 res.values = mixed;
300 } else {
301 res.count = mVoiceResults.size();
302 res.values = mVoiceResults;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700303 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700304 return res;
305 }
306
John Reck35defff2010-11-11 14:06:45 -0800307 void mixResults(List<SuggestItem> results) {
John Reckad373302010-12-17 15:28:13 -0800308 int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
309 maxLines = (int) Math.ceil(maxLines / 2.0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700310 for (int i = 0; i < mSources.size(); i++) {
311 CursorSource s = mSources.get(i);
John Reck35defff2010-11-11 14:06:45 -0800312 int n = Math.min(s.getCount(), maxLines);
313 maxLines -= n;
Michael Kolb0506f2d2010-10-14 16:20:16 -0700314 boolean more = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700315 for (int j = 0; j < n; j++) {
John Reck35defff2010-11-11 14:06:45 -0800316 results.add(s.getItem());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700317 more = s.moveToNext();
318 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700319 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700320 }
321
322 @Override
323 protected void publishResults(CharSequence constraint, FilterResults fresults) {
Michael Kolbcfa3af52010-12-14 10:36:11 -0800324 if (fresults.values instanceof SuggestionResults) {
325 mMixedResults = (SuggestionResults) fresults.values;
326 mListener.onFilterComplete(fresults.count);
327 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700328 notifyDataSetChanged();
329 }
330
331 }
332
333 /**
334 * sorted list of results of a suggestion query
335 *
336 */
337 class SuggestionResults {
338
339 ArrayList<SuggestItem> items;
340 // count per type
341 int[] counts;
342
343 SuggestionResults() {
344 items = new ArrayList<SuggestItem>(24);
345 // n of types:
346 counts = new int[5];
347 }
348
349 int getTypeCount(int type) {
350 return counts[type];
351 }
352
353 void addResult(SuggestItem item) {
354 int ix = 0;
355 while ((ix < items.size()) && (item.type >= items.get(ix).type))
356 ix++;
357 items.add(ix, item);
358 counts[item.type]++;
359 }
360
361 int getLineCount() {
362 if (mLandscapeMode) {
John Reckad373302010-12-17 15:28:13 -0800363 return Math.min(mLinesLandscape, items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700364 } else {
John Reckad373302010-12-17 15:28:13 -0800365 return Math.min(mLinesPortrait, items.size());
Michael Kolb21ce4d22010-09-15 14:55:05 -0700366 }
367 }
368
John Reck35defff2010-11-11 14:06:45 -0800369 @Override
Michael Kolb21ce4d22010-09-15 14:55:05 -0700370 public String toString() {
371 if (items == null) return null;
372 if (items.size() == 0) return "[]";
373 StringBuilder sb = new StringBuilder();
374 for (int i = 0; i < items.size(); i++) {
375 SuggestItem item = items.get(i);
376 sb.append(item.type + ": " + item.title);
377 if (i < items.size() - 1) {
378 sb.append(", ");
379 }
380 }
381 return sb.toString();
382 }
383 }
384
385 /**
386 * data object to hold suggestion values
387 */
388 class SuggestItem {
389 String title;
390 String url;
391 int type;
John Reck40f720e2010-11-10 11:57:04 -0800392 String extra;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700393
394 public SuggestItem(String text, String u, int t) {
395 title = text;
396 url = u;
397 type = t;
398 }
399 }
400
401 abstract class CursorSource {
402
403 Cursor mCursor;
404
405 boolean moveToNext() {
406 return mCursor.moveToNext();
407 }
408
409 public abstract void runQuery(CharSequence constraint);
410
411 public abstract SuggestItem getItem();
412
413 public int getCount() {
414 return (mCursor != null) ? mCursor.getCount() : 0;
415 }
416
417 public void close() {
418 if (mCursor != null) {
419 mCursor.close();
420 }
421 }
422 }
423
424 /**
425 * combined bookmark & history source
426 */
427 class CombinedCursor extends CursorSource {
428
429 @Override
430 public SuggestItem getItem() {
431 if ((mCursor != null) && (!mCursor.isAfterLast())) {
432 String title = mCursor.getString(1);
433 String url = mCursor.getString(2);
434 boolean isBookmark = (mCursor.getInt(3) == 1);
435 return new SuggestItem(getTitle(title, url), getUrl(title, url),
436 isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
437 }
438 return null;
439 }
440
441 @Override
442 public void runQuery(CharSequence constraint) {
443 // constraint != null
444 if (mCursor != null) {
445 mCursor.close();
446 }
447 String like = constraint + "%";
448 String[] args = null;
449 String selection = null;
450 if (like.startsWith("http") || like.startsWith("file")) {
451 args = new String[1];
452 args[0] = like;
453 selection = "url LIKE ?";
454 } else {
455 args = new String[5];
456 args[0] = "http://" + like;
457 args[1] = "http://www." + like;
458 args[2] = "https://" + like;
459 args[3] = "https://www." + like;
460 // To match against titles.
461 args[4] = like;
462 selection = COMBINED_SELECTION;
463 }
464 Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
Michael Kolb0506f2d2010-10-14 16:20:16 -0700465 ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
John Reckad373302010-12-17 15:28:13 -0800466 Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
John Reck3dff1ce2010-12-10 11:13:57 -0800467 BookmarkUtils.addAccountInfo(mContext, ub);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700468 mCursor =
Michael Kolb0506f2d2010-10-14 16:20:16 -0700469 mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
Michael Kolb21ce4d22010-09-15 14:55:05 -0700470 selection,
471 (constraint != null) ? args : null,
John Reck3dff1ce2010-12-10 11:13:57 -0800472 BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
Michael Kolb21ce4d22010-09-15 14:55:05 -0700473 BrowserContract.Combined.VISITS + " DESC, " +
474 BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
475 if (mCursor != null) {
476 mCursor.moveToFirst();
477 }
478 }
479
480 /**
481 * Provides the title (text line 1) for a browser suggestion, which should be the
482 * webpage title. If the webpage title is empty, returns the stripped url instead.
483 *
484 * @return the title string to use
485 */
486 private String getTitle(String title, String url) {
487 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
John Reckfb3017f2010-10-26 19:01:24 -0700488 title = UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700489 }
490 return title;
491 }
492
493 /**
494 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
495 * webpage url. If the webpage title is empty, then the url should go in the title
496 * instead, and the subtitle should be empty, so this would return null.
497 *
498 * @return the subtitle string to use, or null if none
499 */
500 private String getUrl(String title, String url) {
John Reck7d132b12010-10-26 15:10:21 -0700501 if (TextUtils.isEmpty(title)
502 || TextUtils.getTrimmedLength(title) == 0
503 || title.equals(url)) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700504 return null;
505 } else {
John Reckfb3017f2010-10-26 19:01:24 -0700506 return UrlUtils.stripUrl(url);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700507 }
508 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700509 }
510
Michael Kolb21ce4d22010-09-15 14:55:05 -0700511 class SuggestCursor extends CursorSource {
512
513 @Override
514 public SuggestItem getItem() {
515 if (mCursor != null) {
516 String title = mCursor.getString(
517 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
518 String text2 = mCursor.getString(
519 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
520 String url = mCursor.getString(
521 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
522 String uri = mCursor.getString(
523 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
524 int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
John Reck40f720e2010-11-10 11:57:04 -0800525 SuggestItem item = new SuggestItem(title, url, type);
526 item.extra = mCursor.getString(
527 mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
528 return item;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700529 }
530 return null;
531 }
532
533 @Override
534 public void runQuery(CharSequence constraint) {
535 if (mCursor != null) {
536 mCursor.close();
537 }
538 if (!TextUtils.isEmpty(constraint)) {
539 SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
540 if (searchEngine != null && searchEngine.supportsSuggestions()) {
541 mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
542 if (mCursor != null) {
543 mCursor.moveToFirst();
544 }
545 }
546 } else {
547 mCursor = null;
548 }
549 }
550
551 }
552
John Reck35defff2010-11-11 14:06:45 -0800553 public void clearCache() {
554 mFilterResults = null;
555 mSuggestResults = null;
556 }
557
Michael Kolb21ce4d22010-09-15 14:55:05 -0700558}