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