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