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 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 56 | private static final String COMBINED_SELECTION = |
| 57 | "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)"; |
| 58 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 59 | Context mContext; |
| 60 | Filter mFilter; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 61 | SuggestionResults mMixedResults; |
| 62 | List<SuggestItem> mSuggestResults, mFilterResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 63 | List<CursorSource> mSources; |
| 64 | boolean mLandscapeMode; |
| 65 | CompletionListener mListener; |
| 66 | int mLinesPortrait; |
| 67 | int mLinesLandscape; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 68 | Object mResultsLock = new Object(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 69 | List<String> mVoiceResults; |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame^] | 70 | boolean mReverseResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 71 | |
| 72 | interface CompletionListener { |
| 73 | |
| 74 | public void onSearch(String txt); |
| 75 | |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 76 | public void onSelect(String txt, String extraData); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 77 | |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 78 | public void onFilterComplete(int count); |
| 79 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 80 | } |
| 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 90 | addSource(new CombinedCursor()); |
| 91 | } |
| 92 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 93 | void setVoiceResults(List<String> voiceResults) { |
| 94 | mVoiceResults = voiceResults; |
Michael Kolb | a50c446 | 2010-12-15 10:49:12 -0800 | [diff] [blame] | 95 | notifyDataSetChanged(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 98 | public void setLandscapeMode(boolean mode) { |
| 99 | mLandscapeMode = mode; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 100 | notifyDataSetChanged(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 103 | 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 Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 112 | SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 113 | if (R.id.icon2 == v.getId()) { |
| 114 | // replace input field text with suggestion text |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 115 | mListener.onSearch(item.title); |
| 116 | } else { |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 117 | mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url), |
| 118 | item.extra); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Filter getFilter() { |
| 124 | return mFilter; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int getCount() { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 129 | if (mVoiceResults != null) { |
| 130 | return mVoiceResults.size(); |
| 131 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 132 | return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public SuggestItem getItem(int position) { |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame^] | 137 | if (mReverseResults) { |
| 138 | position = (getCount() - 1) - position; |
| 139 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 140 | if (mVoiceResults != null) { |
| 141 | return new SuggestItem(mVoiceResults.get(position), null, |
| 142 | TYPE_SEARCH); |
| 143 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 144 | if (mMixedResults == null) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 145 | return null; |
| 146 | } |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 147 | return mMixedResults.items.get(position); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 148 | } |
| 149 | |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame^] | 150 | public void setReverseResults(boolean reverse) { |
| 151 | mReverseResults = reverse; |
| 152 | } |
| 153 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public long getItemId(int position) { |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame^] | 156 | return position; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public View getView(int position, View convertView, ViewGroup parent) { |
| 161 | final LayoutInflater inflater = LayoutInflater.from(mContext); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 162 | View view = convertView; |
| 163 | if (view == null) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 164 | view = inflater.inflate(R.layout.suggestion_item, parent, false); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 165 | } |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 166 | bindView(view, getItem(position)); |
| 167 | return view; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 168 | } |
| 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 176 | View ic2 = view.findViewById(R.id.icon2); |
Michael Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 177 | View div = view.findViewById(R.id.divider); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 178 | tv1.setText(item.title); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 179 | if (TextUtils.isEmpty(item.url)) { |
| 180 | tv2.setVisibility(View.GONE); |
| 181 | } else { |
| 182 | tv2.setVisibility(View.VISIBLE); |
| 183 | tv2.setText(item.url); |
| 184 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 185 | 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 Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 208 | div.setVisibility(ic2.getVisibility()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 209 | ic2.setOnClickListener(this); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 210 | view.findViewById(R.id.suggestion).setOnClickListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 211 | } |
| 212 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 213 | class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 214 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 215 | @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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 259 | |
| 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 273 | void startSuggestionsAsync(final CharSequence constraint) { |
| 274 | new SlowFilterTask().execute(constraint); |
| 275 | } |
| 276 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 277 | @Override |
| 278 | protected FilterResults performFiltering(CharSequence constraint) { |
| 279 | FilterResults res = new FilterResults(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 280 | if (mVoiceResults == null) { |
| 281 | if (TextUtils.isEmpty(constraint)) { |
| 282 | res.count = 0; |
| 283 | res.values = null; |
| 284 | return res; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 285 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 286 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 303 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 304 | return res; |
| 305 | } |
| 306 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 307 | void mixResults(List<SuggestItem> results) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 308 | int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; |
| 309 | maxLines = (int) Math.ceil(maxLines / 2.0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 310 | for (int i = 0; i < mSources.size(); i++) { |
| 311 | CursorSource s = mSources.get(i); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 312 | int n = Math.min(s.getCount(), maxLines); |
| 313 | maxLines -= n; |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 314 | boolean more = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 315 | for (int j = 0; j < n; j++) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 316 | results.add(s.getItem()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 317 | more = s.moveToNext(); |
| 318 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 319 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
| 323 | protected void publishResults(CharSequence constraint, FilterResults fresults) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 324 | if (fresults.values instanceof SuggestionResults) { |
| 325 | mMixedResults = (SuggestionResults) fresults.values; |
| 326 | mListener.onFilterComplete(fresults.count); |
| 327 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 328 | 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 Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 363 | return Math.min(mLinesLandscape, items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 364 | } else { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 365 | return Math.min(mLinesPortrait, items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 369 | @Override |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 370 | 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 Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 392 | String extra; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 393 | |
| 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 Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 465 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 466 | Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 467 | BookmarkUtils.addAccountInfo(mContext, ub); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 468 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 469 | mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 470 | selection, |
| 471 | (constraint != null) ? args : null, |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 472 | BrowserContract.Combined.IS_BOOKMARK + " DESC, " + |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 473 | 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 Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 488 | title = UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 489 | } |
| 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 Reck | 7d132b1 | 2010-10-26 15:10:21 -0700 | [diff] [blame] | 501 | if (TextUtils.isEmpty(title) |
| 502 | || TextUtils.getTrimmedLength(title) == 0 |
| 503 | || title.equals(url)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 504 | return null; |
| 505 | } else { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 506 | return UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 511 | 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 Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 525 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 529 | } |
| 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 Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 553 | public void clearCache() { |
| 554 | mFilterResults = null; |
| 555 | mSuggestResults = null; |
| 556 | } |
| 557 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 558 | } |