Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import com.android.browser.search.SearchEngine; |
| 20 | |
| 21 | import android.app.SearchManager; |
| 22 | import android.content.Context; |
| 23 | import android.database.Cursor; |
| 24 | import android.net.Uri; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 25 | import android.os.AsyncTask; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 26 | import android.provider.BrowserContract; |
| 27 | import android.text.TextUtils; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.View.OnClickListener; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.widget.BaseAdapter; |
| 33 | import android.widget.Filter; |
| 34 | import android.widget.Filterable; |
| 35 | import android.widget.ImageView; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 36 | import android.widget.TextView; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.List; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * adapter to wrap multiple cursors for url/search completions |
| 43 | */ |
| 44 | public class SuggestionsAdapter extends BaseAdapter implements Filterable, OnClickListener { |
| 45 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 46 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 51 | |
| 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 61 | Context mContext; |
| 62 | Filter mFilter; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 63 | SuggestionResults mMixedResults; |
| 64 | List<SuggestItem> mSuggestResults, mFilterResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 65 | List<CursorSource> mSources; |
| 66 | boolean mLandscapeMode; |
| 67 | CompletionListener mListener; |
| 68 | int mLinesPortrait; |
| 69 | int mLinesLandscape; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 70 | Object mResultsLock = new Object(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 71 | List<String> mVoiceResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 72 | |
| 73 | interface CompletionListener { |
| 74 | |
| 75 | public void onSearch(String txt); |
| 76 | |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 77 | public void onSelect(String txt, String extraData); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 78 | |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 79 | public void onFilterComplete(int count); |
| 80 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 81 | } |
| 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 91 | addSource(new SearchesCursor()); |
| 92 | addSource(new CombinedCursor()); |
| 93 | } |
| 94 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 95 | void setVoiceResults(List<String> voiceResults) { |
| 96 | mVoiceResults = voiceResults; |
Michael Kolb | a50c446 | 2010-12-15 10:49:12 -0800 | [diff] [blame] | 97 | notifyDataSetChanged(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 100 | public void setLandscapeMode(boolean mode) { |
| 101 | mLandscapeMode = mode; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 102 | notifyDataSetChanged(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public int getLeftCount() { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 106 | return mMixedResults.getLeftCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public int getRightCount() { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 110 | return mMixedResults.getRightCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 111 | } |
| 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 Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 128 | mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url), |
| 129 | item.extra); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Filter getFilter() { |
| 135 | return mFilter; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int getCount() { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 140 | if (mVoiceResults != null) { |
| 141 | return mVoiceResults.size(); |
| 142 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 143 | return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public SuggestItem getItem(int position) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 148 | if (mVoiceResults != null) { |
| 149 | return new SuggestItem(mVoiceResults.get(position), null, |
| 150 | TYPE_SEARCH); |
| 151 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 152 | if (mMixedResults == null) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 153 | return null; |
| 154 | } |
| 155 | if (mLandscapeMode) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 156 | if (position >= mMixedResults.getLineCount()) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 157 | // right column |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 158 | position = position - mMixedResults.getLineCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 159 | // index in column |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 160 | if (position >= mMixedResults.getRightCount()) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 161 | return null; |
| 162 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 163 | return mMixedResults.items.get(position + mMixedResults.getLeftCount()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 164 | } else { |
| 165 | // left column |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 166 | if (position >= mMixedResults.getLeftCount()) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 167 | return null; |
| 168 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 169 | return mMixedResults.items.get(position); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 170 | } |
| 171 | } else { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 172 | return mMixedResults.items.get(position); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 173 | } |
| 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 184 | 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 Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 191 | if (mLandscapeMode && (mVoiceResults == null)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 192 | SuggestItem item = getItem(position); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 193 | div.setVisibility(View.VISIBLE); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 194 | if (item != null) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 195 | s1.setVisibility(View.VISIBLE); |
| 196 | bindView(s1, item); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 197 | } else { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 198 | s1.setVisibility(View.INVISIBLE); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 199 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 200 | item = getItem(position + mMixedResults.getLineCount()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 201 | if (item != null) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 202 | s2.setVisibility(View.VISIBLE); |
| 203 | bindView(s2, item); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 204 | } else { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 205 | s2.setVisibility(View.INVISIBLE); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 206 | } |
| 207 | return view; |
| 208 | } else { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 209 | s1.setVisibility(View.VISIBLE); |
| 210 | div.setVisibility(View.GONE); |
| 211 | s2.setVisibility(View.GONE); |
| 212 | bindView(s1, getItem(position)); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 213 | 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 Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 225 | View div = view.findViewById(R.id.divider); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 226 | 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 Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 251 | div.setVisibility(ic2.getVisibility()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 252 | 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 258 | class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 259 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 260 | @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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 304 | |
| 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 318 | void startSuggestionsAsync(final CharSequence constraint) { |
| 319 | new SlowFilterTask().execute(constraint); |
| 320 | } |
| 321 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 322 | @Override |
| 323 | protected FilterResults performFiltering(CharSequence constraint) { |
| 324 | FilterResults res = new FilterResults(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 325 | if (mVoiceResults == null) { |
| 326 | if (TextUtils.isEmpty(constraint)) { |
| 327 | res.count = 0; |
| 328 | res.values = null; |
| 329 | return res; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 330 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 331 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 348 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 349 | return res; |
| 350 | } |
| 351 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 352 | void mixResults(List<SuggestItem> results) { |
| 353 | int maxLines = mLandscapeMode ? mLinesLandscape : (mLinesPortrait / 2); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 354 | for (int i = 0; i < mSources.size(); i++) { |
| 355 | CursorSource s = mSources.get(i); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 356 | int n = Math.min(s.getCount(), maxLines); |
| 357 | maxLines -= n; |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 358 | boolean more = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 359 | for (int j = 0; j < n; j++) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 360 | results.add(s.getItem()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 361 | more = s.moveToNext(); |
| 362 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 363 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
| 367 | protected void publishResults(CharSequence constraint, FilterResults fresults) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 368 | if (fresults.values instanceof SuggestionResults) { |
| 369 | mMixedResults = (SuggestionResults) fresults.values; |
| 370 | mListener.onFilterComplete(fresults.count); |
| 371 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 372 | 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 407 | return Math.min(mLinesLandscape, |
| 408 | Math.max(getLeftCount(), getRightCount())); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 409 | } else { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 410 | return Math.min(mLinesPortrait, getLeftCount() + getRightCount()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | int getLeftCount() { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 415 | return counts[TYPE_BOOKMARK] + counts[TYPE_HISTORY] + counts[TYPE_SUGGEST_URL]; |
| 416 | } |
| 417 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 418 | int getRightCount() { |
| 419 | return counts[TYPE_SEARCH] + counts[TYPE_SUGGEST]; |
| 420 | } |
| 421 | |
| 422 | @Override |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 423 | 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 Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 445 | String extra; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 446 | |
| 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 Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 518 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 519 | Integer.toString(mLinesPortrait)); |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 520 | BookmarkUtils.addAccountInfo(mContext, ub); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 521 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 522 | mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 523 | selection, |
| 524 | (constraint != null) ? args : null, |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 525 | BrowserContract.Combined.IS_BOOKMARK + " DESC, " + |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 526 | 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 Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 541 | title = UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 542 | } |
| 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 Reck | 7d132b1 | 2010-10-26 15:10:21 -0700 | [diff] [blame] | 554 | if (TextUtils.isEmpty(title) |
| 555 | || TextUtils.getTrimmedLength(title) == 0 |
| 556 | || title.equals(url)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 557 | return null; |
| 558 | } else { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 559 | return UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 562 | } |
| 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 Reck | 04e7744 | 2010-11-23 12:58:13 -0800 | [diff] [blame] | 581 | String[] args = new String[] {like}; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 582 | String selection = BrowserContract.Searches.SEARCH + " LIKE ?"; |
| 583 | Uri.Builder ub = BrowserContract.Searches.CONTENT_URI.buildUpon(); |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 584 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 585 | Integer.toString(mLinesPortrait)); |
| 586 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 587 | mContext.getContentResolver().query(ub.build(), SEARCHES_PROJECTION, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 588 | 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 Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 611 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 615 | } |
| 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 639 | public void clearCache() { |
| 640 | mFilterResults = null; |
| 641 | mSuggestResults = null; |
| 642 | } |
| 643 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 644 | } |