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); |
| 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 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 285 | private boolean shouldProcessEmptyQuery() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 286 | final SearchEngine searchEngine = mSettings.getSearchEngine(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 287 | return searchEngine.wantsEmptyQuery(); |
| 288 | } |
| 289 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 290 | @Override |
| 291 | protected FilterResults performFiltering(CharSequence constraint) { |
| 292 | FilterResults res = new FilterResults(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 293 | if (mVoiceResults == null) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 294 | if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 295 | res.count = 0; |
| 296 | res.values = null; |
| 297 | return res; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 298 | } |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 299 | startSuggestionsAsync(constraint); |
| 300 | List<SuggestItem> filterResults = new ArrayList<SuggestItem>(); |
| 301 | if (constraint != null) { |
| 302 | for (CursorSource sc : mSources) { |
| 303 | sc.runQuery(constraint); |
| 304 | } |
| 305 | mixResults(filterResults); |
| 306 | } |
| 307 | synchronized (mResultsLock) { |
| 308 | mFilterResults = filterResults; |
| 309 | } |
| 310 | SuggestionResults mixed = buildSuggestionResults(); |
| 311 | res.count = mixed.getLineCount(); |
| 312 | res.values = mixed; |
| 313 | } else { |
| 314 | res.count = mVoiceResults.size(); |
| 315 | res.values = mVoiceResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 316 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 317 | return res; |
| 318 | } |
| 319 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 320 | void mixResults(List<SuggestItem> results) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 321 | int maxLines = getMaxLines(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 322 | for (int i = 0; i < mSources.size(); i++) { |
| 323 | CursorSource s = mSources.get(i); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 324 | int n = Math.min(s.getCount(), maxLines); |
| 325 | maxLines -= n; |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 326 | boolean more = false; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 327 | for (int j = 0; j < n; j++) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 328 | results.add(s.getItem()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 329 | more = s.moveToNext(); |
| 330 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 331 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Override |
| 335 | protected void publishResults(CharSequence constraint, FilterResults fresults) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 336 | if (fresults.values instanceof SuggestionResults) { |
| 337 | mMixedResults = (SuggestionResults) fresults.values; |
John Reck | a005cd7 | 2011-02-01 11:46:53 -0800 | [diff] [blame] | 338 | notifyDataSetChanged(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 339 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 340 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [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 | private int getMaxLines() { |
| 344 | int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; |
| 345 | maxLines = (int) Math.ceil(maxLines / 2.0); |
| 346 | return maxLines; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
| 350 | * sorted list of results of a suggestion query |
| 351 | * |
| 352 | */ |
| 353 | class SuggestionResults { |
| 354 | |
| 355 | ArrayList<SuggestItem> items; |
| 356 | // count per type |
| 357 | int[] counts; |
| 358 | |
| 359 | SuggestionResults() { |
| 360 | items = new ArrayList<SuggestItem>(24); |
| 361 | // n of types: |
| 362 | counts = new int[5]; |
| 363 | } |
| 364 | |
| 365 | int getTypeCount(int type) { |
| 366 | return counts[type]; |
| 367 | } |
| 368 | |
| 369 | void addResult(SuggestItem item) { |
| 370 | int ix = 0; |
| 371 | while ((ix < items.size()) && (item.type >= items.get(ix).type)) |
| 372 | ix++; |
| 373 | items.add(ix, item); |
| 374 | counts[item.type]++; |
| 375 | } |
| 376 | |
| 377 | int getLineCount() { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 378 | return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 379 | } |
| 380 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 381 | @Override |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 382 | public String toString() { |
| 383 | if (items == null) return null; |
| 384 | if (items.size() == 0) return "[]"; |
| 385 | StringBuilder sb = new StringBuilder(); |
| 386 | for (int i = 0; i < items.size(); i++) { |
| 387 | SuggestItem item = items.get(i); |
| 388 | sb.append(item.type + ": " + item.title); |
| 389 | if (i < items.size() - 1) { |
| 390 | sb.append(", "); |
| 391 | } |
| 392 | } |
| 393 | return sb.toString(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * data object to hold suggestion values |
| 399 | */ |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 400 | public class SuggestItem { |
| 401 | public String title; |
| 402 | public String url; |
| 403 | public int type; |
| 404 | public String extra; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 405 | |
| 406 | public SuggestItem(String text, String u, int t) { |
| 407 | title = text; |
| 408 | url = u; |
| 409 | type = t; |
| 410 | } |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 411 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | abstract class CursorSource { |
| 415 | |
| 416 | Cursor mCursor; |
| 417 | |
| 418 | boolean moveToNext() { |
| 419 | return mCursor.moveToNext(); |
| 420 | } |
| 421 | |
| 422 | public abstract void runQuery(CharSequence constraint); |
| 423 | |
| 424 | public abstract SuggestItem getItem(); |
| 425 | |
| 426 | public int getCount() { |
| 427 | return (mCursor != null) ? mCursor.getCount() : 0; |
| 428 | } |
| 429 | |
| 430 | public void close() { |
| 431 | if (mCursor != null) { |
| 432 | mCursor.close(); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * combined bookmark & history source |
| 439 | */ |
| 440 | class CombinedCursor extends CursorSource { |
| 441 | |
| 442 | @Override |
| 443 | public SuggestItem getItem() { |
| 444 | if ((mCursor != null) && (!mCursor.isAfterLast())) { |
| 445 | String title = mCursor.getString(1); |
| 446 | String url = mCursor.getString(2); |
| 447 | boolean isBookmark = (mCursor.getInt(3) == 1); |
| 448 | return new SuggestItem(getTitle(title, url), getUrl(title, url), |
| 449 | isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY); |
| 450 | } |
| 451 | return null; |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void runQuery(CharSequence constraint) { |
| 456 | // constraint != null |
| 457 | if (mCursor != null) { |
| 458 | mCursor.close(); |
| 459 | } |
| 460 | String like = constraint + "%"; |
| 461 | String[] args = null; |
| 462 | String selection = null; |
| 463 | if (like.startsWith("http") || like.startsWith("file")) { |
| 464 | args = new String[1]; |
| 465 | args[0] = like; |
| 466 | selection = "url LIKE ?"; |
| 467 | } else { |
| 468 | args = new String[5]; |
| 469 | args[0] = "http://" + like; |
| 470 | args[1] = "http://www." + like; |
| 471 | args[2] = "https://" + like; |
| 472 | args[3] = "https://www." + like; |
| 473 | // To match against titles. |
| 474 | args[4] = like; |
| 475 | selection = COMBINED_SELECTION; |
| 476 | } |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame^] | 477 | Uri.Builder ub = OmniboxSuggestions.CONTENT_URI.buildUpon(); |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 478 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 479 | Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); |
John Reck | 7ffb895 | 2011-06-27 15:42:39 -0700 | [diff] [blame] | 480 | ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY, |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame^] | 481 | OmniboxSuggestions.URL); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 482 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 483 | mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, |
John Reck | 5e8466f | 2011-07-28 17:48:58 -0700 | [diff] [blame^] | 484 | selection, (constraint != null) ? args : null, null); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 485 | if (mCursor != null) { |
| 486 | mCursor.moveToFirst(); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Provides the title (text line 1) for a browser suggestion, which should be the |
| 492 | * webpage title. If the webpage title is empty, returns the stripped url instead. |
| 493 | * |
| 494 | * @return the title string to use |
| 495 | */ |
| 496 | private String getTitle(String title, String url) { |
| 497 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 498 | title = UrlUtils.stripUrl(url); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 499 | } |
| 500 | return title; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Provides the subtitle (text line 2) for a browser suggestion, which should be the |
| 505 | * webpage url. If the webpage title is empty, then the url should go in the title |
| 506 | * instead, and the subtitle should be empty, so this would return null. |
| 507 | * |
| 508 | * @return the subtitle string to use, or null if none |
| 509 | */ |
| 510 | private String getUrl(String title, String url) { |
John Reck | 7d132b1 | 2010-10-26 15:10:21 -0700 | [diff] [blame] | 511 | if (TextUtils.isEmpty(title) |
| 512 | || TextUtils.getTrimmedLength(title) == 0 |
| 513 | || title.equals(url)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 514 | return null; |
| 515 | } else { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 516 | return UrlUtils.stripUrl(url); |
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 | } |
| 520 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 521 | class SuggestCursor extends CursorSource { |
| 522 | |
| 523 | @Override |
| 524 | public SuggestItem getItem() { |
| 525 | if (mCursor != null) { |
| 526 | String title = mCursor.getString( |
| 527 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)); |
| 528 | String text2 = mCursor.getString( |
| 529 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2)); |
| 530 | String url = mCursor.getString( |
| 531 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL)); |
| 532 | String uri = mCursor.getString( |
| 533 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA)); |
| 534 | int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL; |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 535 | SuggestItem item = new SuggestItem(title, url, type); |
| 536 | item.extra = mCursor.getString( |
| 537 | mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA)); |
| 538 | return item; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 539 | } |
| 540 | return null; |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void runQuery(CharSequence constraint) { |
| 545 | if (mCursor != null) { |
| 546 | mCursor.close(); |
| 547 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 548 | SearchEngine searchEngine = mSettings.getSearchEngine(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 549 | if (!TextUtils.isEmpty(constraint)) { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 550 | if (searchEngine != null && searchEngine.supportsSuggestions()) { |
| 551 | mCursor = searchEngine.getSuggestions(mContext, constraint.toString()); |
| 552 | if (mCursor != null) { |
| 553 | mCursor.moveToFirst(); |
| 554 | } |
| 555 | } |
| 556 | } else { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 557 | if (searchEngine.wantsEmptyQuery()) { |
| 558 | mCursor = searchEngine.getSuggestions(mContext, ""); |
| 559 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 560 | mCursor = null; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | } |
| 565 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 566 | private boolean useInstant() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 567 | return mSettings.useInstantSearch(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 568 | } |
| 569 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 570 | public void clearCache() { |
| 571 | mFilterResults = null; |
| 572 | mSuggestResults = null; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 573 | notifyDataSetInvalidated(); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 574 | } |
| 575 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 576 | public void setIncognitoMode(boolean incognito) { |
| 577 | mIncognitoMode = incognito; |
| 578 | clearCache(); |
| 579 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 580 | |
| 581 | static String getSuggestionTitle(SuggestItem item) { |
| 582 | // There must be a better way to strip HTML from things. |
| 583 | // This method is used in multiple places. It is also more |
| 584 | // expensive than a standard html escaper. |
| 585 | return (item.title != null) ? Html.fromHtml(item.title).toString() : null; |
| 586 | } |
| 587 | |
| 588 | static String getSuggestionUrl(SuggestItem item) { |
| 589 | final String title = SuggestionsAdapter.getSuggestionTitle(item); |
| 590 | |
| 591 | if (TextUtils.isEmpty(item.url)) { |
| 592 | return title; |
| 593 | } |
| 594 | |
| 595 | return item.url; |
| 596 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 597 | } |