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 | */ |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 44 | public class SuggestionsAdapter extends BaseAdapter implements Filterable, |
| 45 | OnClickListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 46 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 47 | static final int TYPE_BOOKMARK = 0; |
| 48 | static final int TYPE_SUGGEST_URL = 1; |
| 49 | static final int TYPE_HISTORY = 2; |
| 50 | static final int TYPE_SEARCH = 3; |
| 51 | static final int TYPE_SUGGEST = 4; |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 52 | static final int TYPE_VOICE_SEARCH = 5; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 53 | |
| 54 | private static final String[] COMBINED_PROJECTION = |
| 55 | {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE, |
| 56 | BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK}; |
| 57 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 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; |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame] | 72 | boolean mReverseResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 73 | |
| 74 | interface CompletionListener { |
| 75 | |
| 76 | public void onSearch(String txt); |
| 77 | |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 78 | public void onSelect(String txt, int type, String extraData); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 79 | |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 80 | public void onFilterComplete(int count); |
| 81 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public SuggestionsAdapter(Context ctx, CompletionListener listener) { |
| 85 | mContext = ctx; |
| 86 | mListener = listener; |
| 87 | mLinesPortrait = mContext.getResources(). |
| 88 | getInteger(R.integer.max_suggest_lines_portrait); |
| 89 | mLinesLandscape = mContext.getResources(). |
| 90 | getInteger(R.integer.max_suggest_lines_landscape); |
| 91 | mFilter = new SuggestFilter(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 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 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 105 | public void addSource(CursorSource c) { |
| 106 | if (mSources == null) { |
| 107 | mSources = new ArrayList<CursorSource>(5); |
| 108 | } |
| 109 | mSources.add(c); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onClick(View v) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 114 | SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 115 | if (R.id.icon2 == v.getId()) { |
| 116 | // replace input field text with suggestion text |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 117 | mListener.onSearch(item.title); |
| 118 | } else { |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 119 | mListener.onSelect( |
| 120 | (TextUtils.isEmpty(item.url)? item.title : item.url), |
| 121 | item.type, item.extra); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Filter getFilter() { |
| 127 | return mFilter; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int getCount() { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 132 | if (mVoiceResults != null) { |
| 133 | return mVoiceResults.size(); |
| 134 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 135 | return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public SuggestItem getItem(int position) { |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame] | 140 | if (mReverseResults) { |
| 141 | position = (getCount() - 1) - position; |
| 142 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 143 | if (mVoiceResults != null) { |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 144 | SuggestItem item = new SuggestItem(mVoiceResults.get(position), |
| 145 | null, TYPE_VOICE_SEARCH); |
| 146 | item.extra = Integer.toString(position); |
| 147 | return item; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 148 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 149 | if (mMixedResults == null) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 150 | return null; |
| 151 | } |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 152 | return mMixedResults.items.get(position); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 153 | } |
| 154 | |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame] | 155 | public void setReverseResults(boolean reverse) { |
| 156 | mReverseResults = reverse; |
| 157 | } |
| 158 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public long getItemId(int position) { |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame] | 161 | return position; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public View getView(int position, View convertView, ViewGroup parent) { |
| 166 | final LayoutInflater inflater = LayoutInflater.from(mContext); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 167 | View view = convertView; |
| 168 | if (view == null) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 169 | view = inflater.inflate(R.layout.suggestion_item, parent, false); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 170 | } |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 171 | bindView(view, getItem(position)); |
| 172 | return view; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private void bindView(View view, SuggestItem item) { |
| 176 | // store item for click handling |
| 177 | view.setTag(item); |
| 178 | TextView tv1 = (TextView) view.findViewById(android.R.id.text1); |
| 179 | TextView tv2 = (TextView) view.findViewById(android.R.id.text2); |
| 180 | ImageView ic1 = (ImageView) view.findViewById(R.id.icon1); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 181 | View ic2 = view.findViewById(R.id.icon2); |
Michael Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 182 | View div = view.findViewById(R.id.divider); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 183 | tv1.setText(item.title); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 184 | if (TextUtils.isEmpty(item.url)) { |
| 185 | tv2.setVisibility(View.GONE); |
| 186 | } else { |
| 187 | tv2.setVisibility(View.VISIBLE); |
| 188 | tv2.setText(item.url); |
| 189 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 190 | int id = -1; |
| 191 | switch (item.type) { |
| 192 | case TYPE_SUGGEST: |
| 193 | case TYPE_SEARCH: |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 194 | case TYPE_VOICE_SEARCH: |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 195 | id = R.drawable.ic_search_category_suggest; |
| 196 | break; |
| 197 | case TYPE_BOOKMARK: |
| 198 | id = R.drawable.ic_search_category_bookmark; |
| 199 | break; |
| 200 | case TYPE_HISTORY: |
| 201 | id = R.drawable.ic_search_category_history; |
| 202 | break; |
| 203 | case TYPE_SUGGEST_URL: |
| 204 | id = R.drawable.ic_search_category_browser; |
| 205 | break; |
| 206 | default: |
| 207 | id = -1; |
| 208 | } |
| 209 | if (id != -1) { |
| 210 | ic1.setImageDrawable(mContext.getResources().getDrawable(id)); |
| 211 | } |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 212 | ic2.setVisibility(((TYPE_SUGGEST == item.type) |
| 213 | || (TYPE_SEARCH == item.type) |
| 214 | || (TYPE_VOICE_SEARCH == item.type)) |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 215 | ? View.VISIBLE : View.GONE); |
Michael Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 216 | div.setVisibility(ic2.getVisibility()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 217 | ic2.setOnClickListener(this); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 218 | view.findViewById(R.id.suggestion).setOnClickListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 219 | } |
| 220 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 221 | class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 222 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 223 | @Override |
| 224 | protected List<SuggestItem> doInBackground(CharSequence... params) { |
| 225 | SuggestCursor cursor = new SuggestCursor(); |
| 226 | cursor.runQuery(params[0]); |
| 227 | List<SuggestItem> results = new ArrayList<SuggestItem>(); |
| 228 | int count = cursor.getCount(); |
| 229 | for (int i = 0; i < count; i++) { |
| 230 | results.add(cursor.getItem()); |
| 231 | cursor.moveToNext(); |
| 232 | } |
| 233 | cursor.close(); |
| 234 | return results; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | protected void onPostExecute(List<SuggestItem> items) { |
| 239 | mSuggestResults = items; |
| 240 | mMixedResults = buildSuggestionResults(); |
| 241 | notifyDataSetChanged(); |
| 242 | mListener.onFilterComplete(mMixedResults.getLineCount()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | SuggestionResults buildSuggestionResults() { |
| 247 | SuggestionResults mixed = new SuggestionResults(); |
| 248 | List<SuggestItem> filter, suggest; |
| 249 | synchronized (mResultsLock) { |
| 250 | filter = mFilterResults; |
| 251 | suggest = mSuggestResults; |
| 252 | } |
| 253 | if (filter != null) { |
| 254 | for (SuggestItem item : filter) { |
| 255 | mixed.addResult(item); |
| 256 | } |
| 257 | } |
| 258 | if (suggest != null) { |
| 259 | for (SuggestItem item : suggest) { |
| 260 | mixed.addResult(item); |
| 261 | } |
| 262 | } |
| 263 | return mixed; |
| 264 | } |
| 265 | |
| 266 | class SuggestFilter extends Filter { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 267 | |
| 268 | @Override |
| 269 | public CharSequence convertResultToString(Object item) { |
| 270 | if (item == null) { |
| 271 | return ""; |
| 272 | } |
| 273 | SuggestItem sitem = (SuggestItem) item; |
| 274 | if (sitem.title != null) { |
| 275 | return sitem.title; |
| 276 | } else { |
| 277 | return sitem.url; |
| 278 | } |
| 279 | } |
| 280 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 281 | void startSuggestionsAsync(final CharSequence constraint) { |
| 282 | new SlowFilterTask().execute(constraint); |
| 283 | } |
| 284 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 285 | @Override |
| 286 | protected FilterResults performFiltering(CharSequence constraint) { |
| 287 | FilterResults res = new FilterResults(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 288 | if (mVoiceResults == null) { |
| 289 | if (TextUtils.isEmpty(constraint)) { |
| 290 | res.count = 0; |
| 291 | res.values = null; |
| 292 | return res; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 293 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 294 | startSuggestionsAsync(constraint); |
| 295 | List<SuggestItem> filterResults = new ArrayList<SuggestItem>(); |
| 296 | if (constraint != null) { |
| 297 | for (CursorSource sc : mSources) { |
| 298 | sc.runQuery(constraint); |
| 299 | } |
| 300 | mixResults(filterResults); |
| 301 | } |
| 302 | synchronized (mResultsLock) { |
| 303 | mFilterResults = filterResults; |
| 304 | } |
| 305 | SuggestionResults mixed = buildSuggestionResults(); |
| 306 | res.count = mixed.getLineCount(); |
| 307 | res.values = mixed; |
| 308 | } else { |
| 309 | res.count = mVoiceResults.size(); |
| 310 | res.values = mVoiceResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 311 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 312 | return res; |
| 313 | } |
| 314 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 315 | void mixResults(List<SuggestItem> results) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 316 | int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; |
| 317 | maxLines = (int) Math.ceil(maxLines / 2.0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 318 | for (int i = 0; i < mSources.size(); i++) { |
| 319 | CursorSource s = mSources.get(i); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 320 | int n = Math.min(s.getCount(), maxLines); |
| 321 | maxLines -= n; |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 322 | boolean more = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 323 | for (int j = 0; j < n; j++) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 324 | results.add(s.getItem()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 325 | more = s.moveToNext(); |
| 326 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 327 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | protected void publishResults(CharSequence constraint, FilterResults fresults) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 332 | if (fresults.values instanceof SuggestionResults) { |
| 333 | mMixedResults = (SuggestionResults) fresults.values; |
| 334 | mListener.onFilterComplete(fresults.count); |
| 335 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 336 | notifyDataSetChanged(); |
| 337 | } |
| 338 | |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * sorted list of results of a suggestion query |
| 343 | * |
| 344 | */ |
| 345 | class SuggestionResults { |
| 346 | |
| 347 | ArrayList<SuggestItem> items; |
| 348 | // count per type |
| 349 | int[] counts; |
| 350 | |
| 351 | SuggestionResults() { |
| 352 | items = new ArrayList<SuggestItem>(24); |
| 353 | // n of types: |
| 354 | counts = new int[5]; |
| 355 | } |
| 356 | |
| 357 | int getTypeCount(int type) { |
| 358 | return counts[type]; |
| 359 | } |
| 360 | |
| 361 | void addResult(SuggestItem item) { |
| 362 | int ix = 0; |
| 363 | while ((ix < items.size()) && (item.type >= items.get(ix).type)) |
| 364 | ix++; |
| 365 | items.add(ix, item); |
| 366 | counts[item.type]++; |
| 367 | } |
| 368 | |
| 369 | int getLineCount() { |
| 370 | if (mLandscapeMode) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 371 | return Math.min(mLinesLandscape, items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 372 | } else { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 373 | return Math.min(mLinesPortrait, items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 377 | @Override |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 378 | public String toString() { |
| 379 | if (items == null) return null; |
| 380 | if (items.size() == 0) return "[]"; |
| 381 | StringBuilder sb = new StringBuilder(); |
| 382 | for (int i = 0; i < items.size(); i++) { |
| 383 | SuggestItem item = items.get(i); |
| 384 | sb.append(item.type + ": " + item.title); |
| 385 | if (i < items.size() - 1) { |
| 386 | sb.append(", "); |
| 387 | } |
| 388 | } |
| 389 | return sb.toString(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * data object to hold suggestion values |
| 395 | */ |
| 396 | class SuggestItem { |
| 397 | String title; |
| 398 | String url; |
| 399 | int type; |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 400 | String extra; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 401 | |
| 402 | public SuggestItem(String text, String u, int t) { |
| 403 | title = text; |
| 404 | url = u; |
| 405 | type = t; |
| 406 | } |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 407 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | abstract class CursorSource { |
| 411 | |
| 412 | Cursor mCursor; |
| 413 | |
| 414 | boolean moveToNext() { |
| 415 | return mCursor.moveToNext(); |
| 416 | } |
| 417 | |
| 418 | public abstract void runQuery(CharSequence constraint); |
| 419 | |
| 420 | public abstract SuggestItem getItem(); |
| 421 | |
| 422 | public int getCount() { |
| 423 | return (mCursor != null) ? mCursor.getCount() : 0; |
| 424 | } |
| 425 | |
| 426 | public void close() { |
| 427 | if (mCursor != null) { |
| 428 | mCursor.close(); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * combined bookmark & history source |
| 435 | */ |
| 436 | class CombinedCursor extends CursorSource { |
| 437 | |
| 438 | @Override |
| 439 | public SuggestItem getItem() { |
| 440 | if ((mCursor != null) && (!mCursor.isAfterLast())) { |
| 441 | String title = mCursor.getString(1); |
| 442 | String url = mCursor.getString(2); |
| 443 | boolean isBookmark = (mCursor.getInt(3) == 1); |
| 444 | return new SuggestItem(getTitle(title, url), getUrl(title, url), |
| 445 | isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY); |
| 446 | } |
| 447 | return null; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void runQuery(CharSequence constraint) { |
| 452 | // constraint != null |
| 453 | if (mCursor != null) { |
| 454 | mCursor.close(); |
| 455 | } |
| 456 | String like = constraint + "%"; |
| 457 | String[] args = null; |
| 458 | String selection = null; |
| 459 | if (like.startsWith("http") || like.startsWith("file")) { |
| 460 | args = new String[1]; |
| 461 | args[0] = like; |
| 462 | selection = "url LIKE ?"; |
| 463 | } else { |
| 464 | args = new String[5]; |
| 465 | args[0] = "http://" + like; |
| 466 | args[1] = "http://www." + like; |
| 467 | args[2] = "https://" + like; |
| 468 | args[3] = "https://www." + like; |
| 469 | // To match against titles. |
| 470 | args[4] = like; |
| 471 | selection = COMBINED_SELECTION; |
| 472 | } |
| 473 | Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon(); |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 474 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 475 | Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 476 | BookmarkUtils.addAccountInfo(mContext, ub); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 477 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 478 | mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 479 | selection, |
| 480 | (constraint != null) ? args : null, |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 481 | BrowserContract.Combined.IS_BOOKMARK + " DESC, " + |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 482 | BrowserContract.Combined.VISITS + " DESC, " + |
| 483 | BrowserContract.Combined.DATE_LAST_VISITED + " DESC"); |
| 484 | if (mCursor != null) { |
| 485 | mCursor.moveToFirst(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Provides the title (text line 1) for a browser suggestion, which should be the |
| 491 | * webpage title. If the webpage title is empty, returns the stripped url instead. |
| 492 | * |
| 493 | * @return the title string to use |
| 494 | */ |
| 495 | private String getTitle(String title, String url) { |
| 496 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 497 | title = UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 498 | } |
| 499 | return title; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Provides the subtitle (text line 2) for a browser suggestion, which should be the |
| 504 | * webpage url. If the webpage title is empty, then the url should go in the title |
| 505 | * instead, and the subtitle should be empty, so this would return null. |
| 506 | * |
| 507 | * @return the subtitle string to use, or null if none |
| 508 | */ |
| 509 | private String getUrl(String title, String url) { |
John Reck | 7d132b1 | 2010-10-26 15:10:21 -0700 | [diff] [blame] | 510 | if (TextUtils.isEmpty(title) |
| 511 | || TextUtils.getTrimmedLength(title) == 0 |
| 512 | || title.equals(url)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 513 | return null; |
| 514 | } else { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 515 | return UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 520 | class SuggestCursor extends CursorSource { |
| 521 | |
| 522 | @Override |
| 523 | public SuggestItem getItem() { |
| 524 | if (mCursor != null) { |
| 525 | String title = mCursor.getString( |
| 526 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)); |
| 527 | String text2 = mCursor.getString( |
| 528 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2)); |
| 529 | String url = mCursor.getString( |
| 530 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL)); |
| 531 | String uri = mCursor.getString( |
| 532 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA)); |
| 533 | int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL; |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 534 | SuggestItem item = new SuggestItem(title, url, type); |
| 535 | item.extra = mCursor.getString( |
| 536 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA)); |
| 537 | return item; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 538 | } |
| 539 | return null; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public void runQuery(CharSequence constraint) { |
| 544 | if (mCursor != null) { |
| 545 | mCursor.close(); |
| 546 | } |
| 547 | if (!TextUtils.isEmpty(constraint)) { |
| 548 | SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine(); |
| 549 | if (searchEngine != null && searchEngine.supportsSuggestions()) { |
| 550 | mCursor = searchEngine.getSuggestions(mContext, constraint.toString()); |
| 551 | if (mCursor != null) { |
| 552 | mCursor.moveToFirst(); |
| 553 | } |
| 554 | } |
| 555 | } else { |
| 556 | mCursor = null; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | } |
| 561 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 562 | public void clearCache() { |
| 563 | mFilterResults = null; |
| 564 | mSuggestResults = null; |
| 565 | } |
| 566 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 567 | } |