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