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