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 | |
John Reck | 7ffb895 | 2011-06-27 15:42:39 -0700 | [diff] [blame] | 19 | import com.android.browser.provider.BrowserProvider2; |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame] | 20 | import com.android.browser.provider.BrowserProvider2.OmniboxSuggestions; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 21 | import com.android.browser.search.SearchEngine; |
| 22 | |
| 23 | import android.app.SearchManager; |
| 24 | import android.content.Context; |
| 25 | import android.database.Cursor; |
| 26 | import android.net.Uri; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 27 | import android.os.AsyncTask; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 28 | import android.provider.BrowserContract; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 29 | import android.text.Html; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
| 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.view.View.OnClickListener; |
| 34 | import android.view.ViewGroup; |
| 35 | import android.widget.BaseAdapter; |
| 36 | import android.widget.Filter; |
| 37 | import android.widget.Filterable; |
| 38 | import android.widget.ImageView; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
| 40 | |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * adapter to wrap multiple cursors for url/search completions |
| 46 | */ |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 47 | public class SuggestionsAdapter extends BaseAdapter implements Filterable, |
| 48 | OnClickListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 49 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 50 | public static final int TYPE_BOOKMARK = 0; |
| 51 | public static final int TYPE_HISTORY = 1; |
| 52 | public static final int TYPE_SUGGEST_URL = 2; |
| 53 | public static final int TYPE_SEARCH = 3; |
| 54 | public static final int TYPE_SUGGEST = 4; |
| 55 | public static final int TYPE_VOICE_SEARCH = 5; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 56 | |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame] | 57 | private static final String[] COMBINED_PROJECTION = { |
| 58 | OmniboxSuggestions._ID, |
| 59 | OmniboxSuggestions.TITLE, |
| 60 | OmniboxSuggestions.URL, |
| 61 | OmniboxSuggestions.IS_BOOKMARK |
| 62 | }; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 63 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 64 | private static final String COMBINED_SELECTION = |
| 65 | "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)"; |
| 66 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 67 | final Context mContext; |
| 68 | final Filter mFilter; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 69 | SuggestionResults mMixedResults; |
| 70 | List<SuggestItem> mSuggestResults, mFilterResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 71 | List<CursorSource> mSources; |
| 72 | boolean mLandscapeMode; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 73 | final CompletionListener mListener; |
| 74 | final int mLinesPortrait; |
| 75 | final int mLinesLandscape; |
| 76 | final Object mResultsLock = new Object(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 77 | List<String> mVoiceResults; |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 78 | boolean mIncognitoMode; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 79 | BrowserSettings mSettings; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 80 | |
| 81 | interface CompletionListener { |
| 82 | |
| 83 | public void onSearch(String txt); |
| 84 | |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 85 | public void onSelect(String txt, int type, String extraData); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 86 | |
| 87 | } |
| 88 | |
| 89 | public SuggestionsAdapter(Context ctx, CompletionListener listener) { |
| 90 | mContext = ctx; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 91 | mSettings = BrowserSettings.getInstance(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 92 | mListener = listener; |
| 93 | mLinesPortrait = mContext.getResources(). |
| 94 | getInteger(R.integer.max_suggest_lines_portrait); |
| 95 | mLinesLandscape = mContext.getResources(). |
| 96 | getInteger(R.integer.max_suggest_lines_landscape); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 97 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 98 | mFilter = new SuggestFilter(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 99 | addSource(new CombinedCursor()); |
| 100 | } |
| 101 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 102 | void setVoiceResults(List<String> voiceResults) { |
| 103 | mVoiceResults = voiceResults; |
Michael Kolb | a50c446 | 2010-12-15 10:49:12 -0800 | [diff] [blame] | 104 | notifyDataSetChanged(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 107 | public void setLandscapeMode(boolean mode) { |
| 108 | mLandscapeMode = mode; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 109 | notifyDataSetChanged(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 112 | public void addSource(CursorSource c) { |
| 113 | if (mSources == null) { |
| 114 | mSources = new ArrayList<CursorSource>(5); |
| 115 | } |
| 116 | mSources.add(c); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onClick(View v) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 121 | SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 122 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 123 | if (R.id.icon2 == v.getId()) { |
| 124 | // replace input field text with suggestion text |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 125 | mListener.onSearch(getSuggestionUrl(item)); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 126 | } else { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 127 | mListener.onSelect(getSuggestionUrl(item), item.type, item.extra); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Filter getFilter() { |
| 133 | return mFilter; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public int getCount() { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 138 | if (mVoiceResults != null) { |
| 139 | return mVoiceResults.size(); |
| 140 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 141 | return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public SuggestItem getItem(int position) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 146 | if (mVoiceResults != null) { |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 147 | SuggestItem item = new SuggestItem(mVoiceResults.get(position), |
| 148 | null, TYPE_VOICE_SEARCH); |
| 149 | item.extra = Integer.toString(position); |
| 150 | return item; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 151 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 152 | if (mMixedResults == null) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 153 | return null; |
| 154 | } |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 155 | return mMixedResults.items.get(position); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 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); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 182 | tv1.setText(Html.fromHtml(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); |
John Reck | 6a9afa9 | 2011-07-29 10:18:41 -0700 | [diff] [blame^] | 185 | tv1.setMaxLines(2); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 186 | } else { |
| 187 | tv2.setVisibility(View.VISIBLE); |
| 188 | tv2.setText(item.url); |
John Reck | 6a9afa9 | 2011-07-29 10:18:41 -0700 | [diff] [blame^] | 189 | tv1.setMaxLines(1); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 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(); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 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) { |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 282 | if (!mIncognitoMode) { |
| 283 | new SlowFilterTask().execute(constraint); |
| 284 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 287 | private boolean shouldProcessEmptyQuery() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 288 | final SearchEngine searchEngine = mSettings.getSearchEngine(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 289 | return searchEngine.wantsEmptyQuery(); |
| 290 | } |
| 291 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 292 | @Override |
| 293 | protected FilterResults performFiltering(CharSequence constraint) { |
| 294 | FilterResults res = new FilterResults(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 295 | if (mVoiceResults == null) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 296 | if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 297 | res.count = 0; |
| 298 | res.values = null; |
| 299 | return res; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 300 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 301 | startSuggestionsAsync(constraint); |
| 302 | List<SuggestItem> filterResults = new ArrayList<SuggestItem>(); |
| 303 | if (constraint != null) { |
| 304 | for (CursorSource sc : mSources) { |
| 305 | sc.runQuery(constraint); |
| 306 | } |
| 307 | mixResults(filterResults); |
| 308 | } |
| 309 | synchronized (mResultsLock) { |
| 310 | mFilterResults = filterResults; |
| 311 | } |
| 312 | SuggestionResults mixed = buildSuggestionResults(); |
| 313 | res.count = mixed.getLineCount(); |
| 314 | res.values = mixed; |
| 315 | } else { |
| 316 | res.count = mVoiceResults.size(); |
| 317 | res.values = mVoiceResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 318 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 319 | return res; |
| 320 | } |
| 321 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 322 | void mixResults(List<SuggestItem> results) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 323 | int maxLines = getMaxLines(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 324 | for (int i = 0; i < mSources.size(); i++) { |
| 325 | CursorSource s = mSources.get(i); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 326 | int n = Math.min(s.getCount(), maxLines); |
| 327 | maxLines -= n; |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 328 | boolean more = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 329 | for (int j = 0; j < n; j++) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 330 | results.add(s.getItem()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 331 | more = s.moveToNext(); |
| 332 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 333 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
| 337 | protected void publishResults(CharSequence constraint, FilterResults fresults) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 338 | if (fresults.values instanceof SuggestionResults) { |
| 339 | mMixedResults = (SuggestionResults) fresults.values; |
John Reck | a005cd7 | 2011-02-01 11:46:53 -0800 | [diff] [blame] | 340 | notifyDataSetChanged(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 341 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 342 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 343 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 344 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 345 | private int getMaxLines() { |
| 346 | int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; |
| 347 | maxLines = (int) Math.ceil(maxLines / 2.0); |
| 348 | return maxLines; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * sorted list of results of a suggestion query |
| 353 | * |
| 354 | */ |
| 355 | class SuggestionResults { |
| 356 | |
| 357 | ArrayList<SuggestItem> items; |
| 358 | // count per type |
| 359 | int[] counts; |
| 360 | |
| 361 | SuggestionResults() { |
| 362 | items = new ArrayList<SuggestItem>(24); |
| 363 | // n of types: |
| 364 | counts = new int[5]; |
| 365 | } |
| 366 | |
| 367 | int getTypeCount(int type) { |
| 368 | return counts[type]; |
| 369 | } |
| 370 | |
| 371 | void addResult(SuggestItem item) { |
| 372 | int ix = 0; |
| 373 | while ((ix < items.size()) && (item.type >= items.get(ix).type)) |
| 374 | ix++; |
| 375 | items.add(ix, item); |
| 376 | counts[item.type]++; |
| 377 | } |
| 378 | |
| 379 | int getLineCount() { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 380 | return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 381 | } |
| 382 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 383 | @Override |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 384 | public String toString() { |
| 385 | if (items == null) return null; |
| 386 | if (items.size() == 0) return "[]"; |
| 387 | StringBuilder sb = new StringBuilder(); |
| 388 | for (int i = 0; i < items.size(); i++) { |
| 389 | SuggestItem item = items.get(i); |
| 390 | sb.append(item.type + ": " + item.title); |
| 391 | if (i < items.size() - 1) { |
| 392 | sb.append(", "); |
| 393 | } |
| 394 | } |
| 395 | return sb.toString(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * data object to hold suggestion values |
| 401 | */ |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 402 | public class SuggestItem { |
| 403 | public String title; |
| 404 | public String url; |
| 405 | public int type; |
| 406 | public String extra; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 407 | |
| 408 | public SuggestItem(String text, String u, int t) { |
| 409 | title = text; |
| 410 | url = u; |
| 411 | type = t; |
| 412 | } |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 413 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | abstract class CursorSource { |
| 417 | |
| 418 | Cursor mCursor; |
| 419 | |
| 420 | boolean moveToNext() { |
| 421 | return mCursor.moveToNext(); |
| 422 | } |
| 423 | |
| 424 | public abstract void runQuery(CharSequence constraint); |
| 425 | |
| 426 | public abstract SuggestItem getItem(); |
| 427 | |
| 428 | public int getCount() { |
| 429 | return (mCursor != null) ? mCursor.getCount() : 0; |
| 430 | } |
| 431 | |
| 432 | public void close() { |
| 433 | if (mCursor != null) { |
| 434 | mCursor.close(); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * combined bookmark & history source |
| 441 | */ |
| 442 | class CombinedCursor extends CursorSource { |
| 443 | |
| 444 | @Override |
| 445 | public SuggestItem getItem() { |
| 446 | if ((mCursor != null) && (!mCursor.isAfterLast())) { |
| 447 | String title = mCursor.getString(1); |
| 448 | String url = mCursor.getString(2); |
| 449 | boolean isBookmark = (mCursor.getInt(3) == 1); |
| 450 | return new SuggestItem(getTitle(title, url), getUrl(title, url), |
| 451 | isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY); |
| 452 | } |
| 453 | return null; |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public void runQuery(CharSequence constraint) { |
| 458 | // constraint != null |
| 459 | if (mCursor != null) { |
| 460 | mCursor.close(); |
| 461 | } |
| 462 | String like = constraint + "%"; |
| 463 | String[] args = null; |
| 464 | String selection = null; |
| 465 | if (like.startsWith("http") || like.startsWith("file")) { |
| 466 | args = new String[1]; |
| 467 | args[0] = like; |
| 468 | selection = "url LIKE ?"; |
| 469 | } else { |
| 470 | args = new String[5]; |
| 471 | args[0] = "http://" + like; |
| 472 | args[1] = "http://www." + like; |
| 473 | args[2] = "https://" + like; |
| 474 | args[3] = "https://www." + like; |
| 475 | // To match against titles. |
| 476 | args[4] = like; |
| 477 | selection = COMBINED_SELECTION; |
| 478 | } |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame] | 479 | Uri.Builder ub = OmniboxSuggestions.CONTENT_URI.buildUpon(); |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 480 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 481 | Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); |
John Reck | 7ffb895 | 2011-06-27 15:42:39 -0700 | [diff] [blame] | 482 | ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY, |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame] | 483 | OmniboxSuggestions.URL); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 484 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 485 | mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame] | 486 | selection, (constraint != null) ? args : null, null); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 487 | if (mCursor != null) { |
| 488 | mCursor.moveToFirst(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Provides the title (text line 1) for a browser suggestion, which should be the |
| 494 | * webpage title. If the webpage title is empty, returns the stripped url instead. |
| 495 | * |
| 496 | * @return the title string to use |
| 497 | */ |
| 498 | private String getTitle(String title, String url) { |
| 499 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 500 | title = UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 501 | } |
| 502 | return title; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Provides the subtitle (text line 2) for a browser suggestion, which should be the |
| 507 | * webpage url. If the webpage title is empty, then the url should go in the title |
| 508 | * instead, and the subtitle should be empty, so this would return null. |
| 509 | * |
| 510 | * @return the subtitle string to use, or null if none |
| 511 | */ |
| 512 | private String getUrl(String title, String url) { |
John Reck | 7d132b1 | 2010-10-26 15:10:21 -0700 | [diff] [blame] | 513 | if (TextUtils.isEmpty(title) |
| 514 | || TextUtils.getTrimmedLength(title) == 0 |
| 515 | || title.equals(url)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 516 | return null; |
| 517 | } else { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 518 | return UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 523 | class SuggestCursor extends CursorSource { |
| 524 | |
| 525 | @Override |
| 526 | public SuggestItem getItem() { |
| 527 | if (mCursor != null) { |
| 528 | String title = mCursor.getString( |
| 529 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)); |
| 530 | String text2 = mCursor.getString( |
| 531 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2)); |
| 532 | String url = mCursor.getString( |
| 533 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL)); |
| 534 | String uri = mCursor.getString( |
| 535 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA)); |
| 536 | int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL; |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 537 | SuggestItem item = new SuggestItem(title, url, type); |
| 538 | item.extra = mCursor.getString( |
| 539 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA)); |
| 540 | return item; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 541 | } |
| 542 | return null; |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void runQuery(CharSequence constraint) { |
| 547 | if (mCursor != null) { |
| 548 | mCursor.close(); |
| 549 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 550 | SearchEngine searchEngine = mSettings.getSearchEngine(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 551 | if (!TextUtils.isEmpty(constraint)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 552 | if (searchEngine != null && searchEngine.supportsSuggestions()) { |
| 553 | mCursor = searchEngine.getSuggestions(mContext, constraint.toString()); |
| 554 | if (mCursor != null) { |
| 555 | mCursor.moveToFirst(); |
| 556 | } |
| 557 | } |
| 558 | } else { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 559 | if (searchEngine.wantsEmptyQuery()) { |
| 560 | mCursor = searchEngine.getSuggestions(mContext, ""); |
| 561 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 562 | mCursor = null; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | } |
| 567 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 568 | private boolean useInstant() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 569 | return mSettings.useInstantSearch(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 570 | } |
| 571 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 572 | public void clearCache() { |
| 573 | mFilterResults = null; |
| 574 | mSuggestResults = null; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 575 | notifyDataSetInvalidated(); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 576 | } |
| 577 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 578 | public void setIncognitoMode(boolean incognito) { |
| 579 | mIncognitoMode = incognito; |
| 580 | clearCache(); |
| 581 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 582 | |
| 583 | static String getSuggestionTitle(SuggestItem item) { |
| 584 | // There must be a better way to strip HTML from things. |
| 585 | // This method is used in multiple places. It is also more |
| 586 | // expensive than a standard html escaper. |
| 587 | return (item.title != null) ? Html.fromHtml(item.title).toString() : null; |
| 588 | } |
| 589 | |
| 590 | static String getSuggestionUrl(SuggestItem item) { |
| 591 | final String title = SuggestionsAdapter.getSuggestionTitle(item); |
| 592 | |
| 593 | if (TextUtils.isEmpty(item.url)) { |
| 594 | return title; |
| 595 | } |
| 596 | |
| 597 | return item.url; |
| 598 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 599 | } |