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