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; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 20 | import com.android.browser.search.SearchEngine; |
| 21 | |
| 22 | import android.app.SearchManager; |
| 23 | import android.content.Context; |
| 24 | import android.database.Cursor; |
| 25 | import android.net.Uri; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 27 | import android.provider.BrowserContract; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 28 | import android.text.Html; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
| 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
| 32 | import android.view.View.OnClickListener; |
| 33 | import android.view.ViewGroup; |
| 34 | import android.widget.BaseAdapter; |
| 35 | import android.widget.Filter; |
| 36 | import android.widget.Filterable; |
| 37 | import android.widget.ImageView; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 38 | import android.widget.TextView; |
| 39 | |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * adapter to wrap multiple cursors for url/search completions |
| 45 | */ |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 46 | public class SuggestionsAdapter extends BaseAdapter implements Filterable, |
| 47 | OnClickListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 48 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 49 | public static final int TYPE_BOOKMARK = 0; |
| 50 | public static final int TYPE_HISTORY = 1; |
| 51 | public static final int TYPE_SUGGEST_URL = 2; |
| 52 | public static final int TYPE_SEARCH = 3; |
| 53 | public static final int TYPE_SUGGEST = 4; |
| 54 | public static final int TYPE_VOICE_SEARCH = 5; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 55 | |
| 56 | private static final String[] COMBINED_PROJECTION = |
| 57 | {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE, |
| 58 | BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK}; |
| 59 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 60 | private static final String COMBINED_SELECTION = |
| 61 | "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)"; |
| 62 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 63 | final Context mContext; |
| 64 | final Filter mFilter; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 65 | SuggestionResults mMixedResults; |
| 66 | List<SuggestItem> mSuggestResults, mFilterResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 67 | List<CursorSource> mSources; |
| 68 | boolean mLandscapeMode; |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 69 | final CompletionListener mListener; |
| 70 | final int mLinesPortrait; |
| 71 | final int mLinesLandscape; |
| 72 | final Object mResultsLock = new Object(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 73 | List<String> mVoiceResults; |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 74 | boolean mIncognitoMode; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 75 | BrowserSettings mSettings; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 76 | |
| 77 | interface CompletionListener { |
| 78 | |
| 79 | public void onSearch(String txt); |
| 80 | |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 81 | public void onSelect(String txt, int type, String extraData); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 82 | |
| 83 | } |
| 84 | |
| 85 | public SuggestionsAdapter(Context ctx, CompletionListener listener) { |
| 86 | mContext = ctx; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 87 | mSettings = BrowserSettings.getInstance(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 88 | mListener = listener; |
| 89 | mLinesPortrait = mContext.getResources(). |
| 90 | getInteger(R.integer.max_suggest_lines_portrait); |
| 91 | mLinesLandscape = mContext.getResources(). |
| 92 | getInteger(R.integer.max_suggest_lines_landscape); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 93 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 94 | mFilter = new SuggestFilter(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 95 | addSource(new CombinedCursor()); |
| 96 | } |
| 97 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 98 | void setVoiceResults(List<String> voiceResults) { |
| 99 | mVoiceResults = voiceResults; |
Michael Kolb | a50c446 | 2010-12-15 10:49:12 -0800 | [diff] [blame] | 100 | notifyDataSetChanged(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 103 | public void setLandscapeMode(boolean mode) { |
| 104 | mLandscapeMode = mode; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 105 | notifyDataSetChanged(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 108 | public void addSource(CursorSource c) { |
| 109 | if (mSources == null) { |
| 110 | mSources = new ArrayList<CursorSource>(5); |
| 111 | } |
| 112 | mSources.add(c); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void onClick(View v) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 117 | SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 118 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 119 | if (R.id.icon2 == v.getId()) { |
| 120 | // replace input field text with suggestion text |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 121 | mListener.onSearch(getSuggestionUrl(item)); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 122 | } else { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 123 | mListener.onSelect(getSuggestionUrl(item), item.type, item.extra); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Filter getFilter() { |
| 129 | return mFilter; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public int getCount() { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 134 | if (mVoiceResults != null) { |
| 135 | return mVoiceResults.size(); |
| 136 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 137 | return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public SuggestItem getItem(int position) { |
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 | |
| 154 | @Override |
| 155 | public long getItemId(int position) { |
John Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame] | 156 | return position; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public View getView(int position, View convertView, ViewGroup parent) { |
| 161 | final LayoutInflater inflater = LayoutInflater.from(mContext); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 162 | View view = convertView; |
| 163 | if (view == null) { |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 164 | view = inflater.inflate(R.layout.suggestion_item, parent, false); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 165 | } |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 166 | bindView(view, getItem(position)); |
| 167 | return view; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private void bindView(View view, SuggestItem item) { |
| 171 | // store item for click handling |
| 172 | view.setTag(item); |
| 173 | TextView tv1 = (TextView) view.findViewById(android.R.id.text1); |
| 174 | TextView tv2 = (TextView) view.findViewById(android.R.id.text2); |
| 175 | ImageView ic1 = (ImageView) view.findViewById(R.id.icon1); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 176 | View ic2 = view.findViewById(R.id.icon2); |
Michael Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 177 | View div = view.findViewById(R.id.divider); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 178 | tv1.setText(Html.fromHtml(item.title)); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 179 | if (TextUtils.isEmpty(item.url)) { |
| 180 | tv2.setVisibility(View.GONE); |
| 181 | } else { |
| 182 | tv2.setVisibility(View.VISIBLE); |
| 183 | tv2.setText(item.url); |
| 184 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 185 | int id = -1; |
| 186 | switch (item.type) { |
| 187 | case TYPE_SUGGEST: |
| 188 | case TYPE_SEARCH: |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 189 | case TYPE_VOICE_SEARCH: |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 190 | id = R.drawable.ic_search_category_suggest; |
| 191 | break; |
| 192 | case TYPE_BOOKMARK: |
| 193 | id = R.drawable.ic_search_category_bookmark; |
| 194 | break; |
| 195 | case TYPE_HISTORY: |
| 196 | id = R.drawable.ic_search_category_history; |
| 197 | break; |
| 198 | case TYPE_SUGGEST_URL: |
| 199 | id = R.drawable.ic_search_category_browser; |
| 200 | break; |
| 201 | default: |
| 202 | id = -1; |
| 203 | } |
| 204 | if (id != -1) { |
| 205 | ic1.setImageDrawable(mContext.getResources().getDrawable(id)); |
| 206 | } |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 207 | ic2.setVisibility(((TYPE_SUGGEST == item.type) |
| 208 | || (TYPE_SEARCH == item.type) |
| 209 | || (TYPE_VOICE_SEARCH == item.type)) |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 210 | ? View.VISIBLE : View.GONE); |
Michael Kolb | 7b20ddd | 2010-10-14 15:03:28 -0700 | [diff] [blame] | 211 | div.setVisibility(ic2.getVisibility()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 212 | ic2.setOnClickListener(this); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 213 | view.findViewById(R.id.suggestion).setOnClickListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 214 | } |
| 215 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 216 | class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 217 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 218 | @Override |
| 219 | protected List<SuggestItem> doInBackground(CharSequence... params) { |
| 220 | SuggestCursor cursor = new SuggestCursor(); |
| 221 | cursor.runQuery(params[0]); |
| 222 | List<SuggestItem> results = new ArrayList<SuggestItem>(); |
| 223 | int count = cursor.getCount(); |
| 224 | for (int i = 0; i < count; i++) { |
| 225 | results.add(cursor.getItem()); |
| 226 | cursor.moveToNext(); |
| 227 | } |
| 228 | cursor.close(); |
| 229 | return results; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | protected void onPostExecute(List<SuggestItem> items) { |
| 234 | mSuggestResults = items; |
| 235 | mMixedResults = buildSuggestionResults(); |
| 236 | notifyDataSetChanged(); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | SuggestionResults buildSuggestionResults() { |
| 241 | SuggestionResults mixed = new SuggestionResults(); |
| 242 | List<SuggestItem> filter, suggest; |
| 243 | synchronized (mResultsLock) { |
| 244 | filter = mFilterResults; |
| 245 | suggest = mSuggestResults; |
| 246 | } |
| 247 | if (filter != null) { |
| 248 | for (SuggestItem item : filter) { |
| 249 | mixed.addResult(item); |
| 250 | } |
| 251 | } |
| 252 | if (suggest != null) { |
| 253 | for (SuggestItem item : suggest) { |
| 254 | mixed.addResult(item); |
| 255 | } |
| 256 | } |
| 257 | return mixed; |
| 258 | } |
| 259 | |
| 260 | class SuggestFilter extends Filter { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 261 | |
| 262 | @Override |
| 263 | public CharSequence convertResultToString(Object item) { |
| 264 | if (item == null) { |
| 265 | return ""; |
| 266 | } |
| 267 | SuggestItem sitem = (SuggestItem) item; |
| 268 | if (sitem.title != null) { |
| 269 | return sitem.title; |
| 270 | } else { |
| 271 | return sitem.url; |
| 272 | } |
| 273 | } |
| 274 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 275 | void startSuggestionsAsync(final CharSequence constraint) { |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 276 | if (!mIncognitoMode) { |
| 277 | new SlowFilterTask().execute(constraint); |
| 278 | } |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 281 | private boolean shouldProcessEmptyQuery() { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 282 | final SearchEngine searchEngine = mSettings.getSearchEngine(); |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 283 | return searchEngine.wantsEmptyQuery(); |
| 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) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 290 | if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) { |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 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 { |
| 310 | res.count = mVoiceResults.size(); |
| 311 | res.values = mVoiceResults; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 312 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 313 | return res; |
| 314 | } |
| 315 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 316 | void mixResults(List<SuggestItem> results) { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 317 | int maxLines = getMaxLines(); |
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 | } |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 337 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 338 | |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 339 | private int getMaxLines() { |
| 340 | int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; |
| 341 | maxLines = (int) Math.ceil(maxLines / 2.0); |
| 342 | return maxLines; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
| 346 | * sorted list of results of a suggestion query |
| 347 | * |
| 348 | */ |
| 349 | class SuggestionResults { |
| 350 | |
| 351 | ArrayList<SuggestItem> items; |
| 352 | // count per type |
| 353 | int[] counts; |
| 354 | |
| 355 | SuggestionResults() { |
| 356 | items = new ArrayList<SuggestItem>(24); |
| 357 | // n of types: |
| 358 | counts = new int[5]; |
| 359 | } |
| 360 | |
| 361 | int getTypeCount(int type) { |
| 362 | return counts[type]; |
| 363 | } |
| 364 | |
| 365 | void addResult(SuggestItem item) { |
| 366 | int ix = 0; |
| 367 | while ((ix < items.size()) && (item.type >= items.get(ix).type)) |
| 368 | ix++; |
| 369 | items.add(ix, item); |
| 370 | counts[item.type]++; |
| 371 | } |
| 372 | |
| 373 | int getLineCount() { |
Narayan Kamath | 5119edd | 2011-02-23 15:49:17 +0000 | [diff] [blame] | 374 | return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size()); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 375 | } |
| 376 | |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 377 | @Override |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 378 | public String toString() { |
| 379 | if (items == null) return null; |
| 380 | if (items.size() == 0) return "[]"; |
| 381 | StringBuilder sb = new StringBuilder(); |
| 382 | for (int i = 0; i < items.size(); i++) { |
| 383 | SuggestItem item = items.get(i); |
| 384 | sb.append(item.type + ": " + item.title); |
| 385 | if (i < items.size() - 1) { |
| 386 | sb.append(", "); |
| 387 | } |
| 388 | } |
| 389 | return sb.toString(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * data object to hold suggestion values |
| 395 | */ |
Narayan Kamath | 80aad8d | 2011-02-23 12:01:13 +0000 | [diff] [blame] | 396 | public class SuggestItem { |
| 397 | public String title; |
| 398 | public String url; |
| 399 | public int type; |
| 400 | public String extra; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 401 | |
| 402 | public SuggestItem(String text, String u, int t) { |
| 403 | title = text; |
| 404 | url = u; |
| 405 | type = t; |
| 406 | } |
Michael Kolb | bd2dd64 | 2011-01-13 13:01:30 -0800 | [diff] [blame] | 407 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | abstract class CursorSource { |
| 411 | |
| 412 | Cursor mCursor; |
| 413 | |
| 414 | boolean moveToNext() { |
| 415 | return mCursor.moveToNext(); |
| 416 | } |
| 417 | |
| 418 | public abstract void runQuery(CharSequence constraint); |
| 419 | |
| 420 | public abstract SuggestItem getItem(); |
| 421 | |
| 422 | public int getCount() { |
| 423 | return (mCursor != null) ? mCursor.getCount() : 0; |
| 424 | } |
| 425 | |
| 426 | public void close() { |
| 427 | if (mCursor != null) { |
| 428 | mCursor.close(); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * combined bookmark & history source |
| 435 | */ |
| 436 | class CombinedCursor extends CursorSource { |
| 437 | |
| 438 | @Override |
| 439 | public SuggestItem getItem() { |
| 440 | if ((mCursor != null) && (!mCursor.isAfterLast())) { |
| 441 | String title = mCursor.getString(1); |
| 442 | String url = mCursor.getString(2); |
| 443 | boolean isBookmark = (mCursor.getInt(3) == 1); |
| 444 | return new SuggestItem(getTitle(title, url), getUrl(title, url), |
| 445 | isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY); |
| 446 | } |
| 447 | return null; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void runQuery(CharSequence constraint) { |
| 452 | // constraint != null |
| 453 | if (mCursor != null) { |
| 454 | mCursor.close(); |
| 455 | } |
| 456 | String like = constraint + "%"; |
| 457 | String[] args = null; |
| 458 | String selection = null; |
| 459 | if (like.startsWith("http") || like.startsWith("file")) { |
| 460 | args = new String[1]; |
| 461 | args[0] = like; |
| 462 | selection = "url LIKE ?"; |
| 463 | } else { |
| 464 | args = new String[5]; |
| 465 | args[0] = "http://" + like; |
| 466 | args[1] = "http://www." + like; |
| 467 | args[2] = "https://" + like; |
| 468 | args[3] = "https://www." + like; |
| 469 | // To match against titles. |
| 470 | args[4] = like; |
| 471 | selection = COMBINED_SELECTION; |
| 472 | } |
| 473 | Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon(); |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 474 | ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 475 | Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); |
John Reck | 7ffb895 | 2011-06-27 15:42:39 -0700 | [diff] [blame] | 476 | ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY, |
| 477 | BrowserContract.Combined.URL); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 478 | mCursor = |
Michael Kolb | 0506f2d | 2010-10-14 16:20:16 -0700 | [diff] [blame] | 479 | mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 480 | selection, |
| 481 | (constraint != null) ? args : null, |
John Reck | 3dff1ce | 2010-12-10 11:13:57 -0800 | [diff] [blame] | 482 | BrowserContract.Combined.IS_BOOKMARK + " DESC, " + |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 483 | BrowserContract.Combined.VISITS + " DESC, " + |
| 484 | BrowserContract.Combined.DATE_LAST_VISITED + " DESC"); |
| 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 | } |