121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb/* 221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Copyright (C) 2010 The Android Open Source Project 321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Licensed under the Apache License, Version 2.0 (the "License"); 521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * you may not use this file except in compliance with the License. 621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * You may obtain a copy of the License at 721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * http://www.apache.org/licenses/LICENSE-2.0 921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 1021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Unless required by applicable law or agreed to in writing, software 1121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * distributed under the License is distributed on an "AS IS" BASIS, 1221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * See the License for the specific language governing permissions and 1421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * limitations under the License. 1521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 1621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 1721ce4d295802db811873b46e7821abfa0540dab2Michael Kolbpackage com.android.browser; 1821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 1921ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.app.SearchManager; 2021ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.content.Context; 2121ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.database.Cursor; 2221ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.net.Uri; 2335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reckimport android.os.AsyncTask; 2421ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.provider.BrowserContract; 255119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamathimport android.text.Html; 2621ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.text.TextUtils; 2721ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.LayoutInflater; 2821ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.View; 2921ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.View.OnClickListener; 3021ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.ViewGroup; 3121ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.BaseAdapter; 3221ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.Filter; 3321ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.Filterable; 3421ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.ImageView; 3521ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.TextView; 3621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 375ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolbimport com.android.browser.provider.BrowserProvider2.OmniboxSuggestions; 385ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolbimport com.android.browser.search.SearchEngine; 395ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb 4021ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport java.util.ArrayList; 4121ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport java.util.List; 4221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 4321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb/** 4421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * adapter to wrap multiple cursors for url/search completions 4521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 46bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolbpublic class SuggestionsAdapter extends BaseAdapter implements Filterable, 47bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb OnClickListener { 4821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 495119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath public static final int TYPE_BOOKMARK = 0; 505119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath public static final int TYPE_HISTORY = 1; 515119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath public static final int TYPE_SUGGEST_URL = 2; 525119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath public static final int TYPE_SEARCH = 3; 535119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath public static final int TYPE_SUGGEST = 4; 5421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 555e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck private static final String[] COMBINED_PROJECTION = { 565e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck OmniboxSuggestions._ID, 575e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck OmniboxSuggestions.TITLE, 585e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck OmniboxSuggestions.URL, 595e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck OmniboxSuggestions.IS_BOOKMARK 605e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck }; 6121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 6221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private static final String COMBINED_SELECTION = 6321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)"; 6421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 655119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final Context mContext; 665119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final Filter mFilter; 6735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestionResults mMixedResults; 6835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck List<SuggestItem> mSuggestResults, mFilterResults; 6921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb List<CursorSource> mSources; 7021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb boolean mLandscapeMode; 715119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final CompletionListener mListener; 725119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final int mLinesPortrait; 735119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final int mLinesLandscape; 745119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final Object mResultsLock = new Object(); 75117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck boolean mIncognitoMode; 7635e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck BrowserSettings mSettings; 7721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 7821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb interface CompletionListener { 7921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 8021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void onSearch(String txt); 8121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 82bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb public void onSelect(String txt, int type, String extraData); 8321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 8421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 8521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 8621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestionsAdapter(Context ctx, CompletionListener listener) { 8721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mContext = ctx; 8835e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck mSettings = BrowserSettings.getInstance(); 8921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mListener = listener; 9021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mLinesPortrait = mContext.getResources(). 9121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb getInteger(R.integer.max_suggest_lines_portrait); 9221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mLinesLandscape = mContext.getResources(). 9321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb getInteger(R.integer.max_suggest_lines_landscape); 945119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 9521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mFilter = new SuggestFilter(); 9621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb addSource(new CombinedCursor()); 9721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 9821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 9921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void setLandscapeMode(boolean mode) { 10021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mLandscapeMode = mode; 10135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck notifyDataSetChanged(); 10221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 10321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 10421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void addSource(CursorSource c) { 10521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mSources == null) { 10621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mSources = new ArrayList<CursorSource>(5); 10721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 10821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mSources.add(c); 10921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 11021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 11121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 11221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void onClick(View v) { 113ad373302b1e1a322144f818340fdce60f0eee403John Reck SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag(); 1145119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 11521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (R.id.icon2 == v.getId()) { 11621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // replace input field text with suggestion text 1175119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath mListener.onSearch(getSuggestionUrl(item)); 11821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 1195119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath mListener.onSelect(getSuggestionUrl(item), item.type, item.extra); 12021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 12121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 12221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 12321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 12421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public Filter getFilter() { 12521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return mFilter; 12621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 12721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 12821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 12921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public int getCount() { 13035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); 13121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 13221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 13321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 13421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem getItem(int position) { 13535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (mMixedResults == null) { 13621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 13721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 138ad373302b1e1a322144f818340fdce60f0eee403John Reck return mMixedResults.items.get(position); 13921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 14021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 14121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 14221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public long getItemId(int position) { 1431605bef4e1f99805a801308f97ade622b907dc7aJohn Reck return position; 14421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 14521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 14621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 14721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public View getView(int position, View convertView, ViewGroup parent) { 14821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb final LayoutInflater inflater = LayoutInflater.from(mContext); 14935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck View view = convertView; 15035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (view == null) { 151ad373302b1e1a322144f818340fdce60f0eee403John Reck view = inflater.inflate(R.layout.suggestion_item, parent, false); 15221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 153ad373302b1e1a322144f818340fdce60f0eee403John Reck bindView(view, getItem(position)); 154ad373302b1e1a322144f818340fdce60f0eee403John Reck return view; 15521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 15621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 15721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private void bindView(View view, SuggestItem item) { 15821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // store item for click handling 15921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb view.setTag(item); 16021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb TextView tv1 = (TextView) view.findViewById(android.R.id.text1); 16121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb TextView tv2 = (TextView) view.findViewById(android.R.id.text2); 16221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ImageView ic1 = (ImageView) view.findViewById(R.id.icon1); 16321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb View ic2 = view.findViewById(R.id.icon2); 1647b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb View div = view.findViewById(R.id.divider); 1655119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath tv1.setText(Html.fromHtml(item.title)); 166ad373302b1e1a322144f818340fdce60f0eee403John Reck if (TextUtils.isEmpty(item.url)) { 167ad373302b1e1a322144f818340fdce60f0eee403John Reck tv2.setVisibility(View.GONE); 1686a9afa94e282efa9f6f44b02be54e384765d853cJohn Reck tv1.setMaxLines(2); 169ad373302b1e1a322144f818340fdce60f0eee403John Reck } else { 170ad373302b1e1a322144f818340fdce60f0eee403John Reck tv2.setVisibility(View.VISIBLE); 171ad373302b1e1a322144f818340fdce60f0eee403John Reck tv2.setText(item.url); 1726a9afa94e282efa9f6f44b02be54e384765d853cJohn Reck tv1.setMaxLines(1); 173ad373302b1e1a322144f818340fdce60f0eee403John Reck } 17421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int id = -1; 17521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb switch (item.type) { 17621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_SUGGEST: 17721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_SEARCH: 17821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_suggest; 17921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 18021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_BOOKMARK: 18121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_bookmark; 18221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 18321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_HISTORY: 18421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_history; 18521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 18621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_SUGGEST_URL: 18721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_browser; 18821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 18921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb default: 19021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = -1; 19121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 19221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (id != -1) { 19321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ic1.setImageDrawable(mContext.getResources().getDrawable(id)); 19421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 195bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb ic2.setVisibility(((TYPE_SUGGEST == item.type) 1965ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb || (TYPE_SEARCH == item.type)) 19721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ? View.VISIBLE : View.GONE); 1987b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb div.setVisibility(ic2.getVisibility()); 19921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ic2.setOnClickListener(this); 200ad373302b1e1a322144f818340fdce60f0eee403John Reck view.findViewById(R.id.suggestion).setOnClickListener(this); 20121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 20221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 20335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> { 20435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 20535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck @Override 20635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck protected List<SuggestItem> doInBackground(CharSequence... params) { 20735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestCursor cursor = new SuggestCursor(); 20835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck cursor.runQuery(params[0]); 20935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck List<SuggestItem> results = new ArrayList<SuggestItem>(); 21035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck int count = cursor.getCount(); 21135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck for (int i = 0; i < count; i++) { 21235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck results.add(cursor.getItem()); 21335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck cursor.moveToNext(); 21435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 21535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck cursor.close(); 21635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck return results; 21735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 21835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 21935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck @Override 22035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck protected void onPostExecute(List<SuggestItem> items) { 22135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mSuggestResults = items; 22235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mMixedResults = buildSuggestionResults(); 22335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck notifyDataSetChanged(); 22435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 22535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 22635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 22735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestionResults buildSuggestionResults() { 22835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestionResults mixed = new SuggestionResults(); 22935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck List<SuggestItem> filter, suggest; 23035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck synchronized (mResultsLock) { 23135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck filter = mFilterResults; 23235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck suggest = mSuggestResults; 23335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 23435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (filter != null) { 23535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck for (SuggestItem item : filter) { 23635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mixed.addResult(item); 23735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 23835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 23935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (suggest != null) { 24035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck for (SuggestItem item : suggest) { 24135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mixed.addResult(item); 24235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 24335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 24435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck return mixed; 24535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 24621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 24735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck class SuggestFilter extends Filter { 24821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 24921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 25021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public CharSequence convertResultToString(Object item) { 25121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (item == null) { 25221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return ""; 25321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 25421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SuggestItem sitem = (SuggestItem) item; 25521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (sitem.title != null) { 25621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return sitem.title; 25721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 25821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return sitem.url; 25921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 26021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 26121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 26235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck void startSuggestionsAsync(final CharSequence constraint) { 263117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck if (!mIncognitoMode) { 264117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck new SlowFilterTask().execute(constraint); 265117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck } 26635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 26735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 2685119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath private boolean shouldProcessEmptyQuery() { 26935e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck final SearchEngine searchEngine = mSettings.getSearchEngine(); 2705119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath return searchEngine.wantsEmptyQuery(); 2715119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath } 2725119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 27321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 27421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb protected FilterResults performFiltering(CharSequence constraint) { 27521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb FilterResults res = new FilterResults(); 2765ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb if (TextUtils.isEmpty(constraint) && !shouldProcessEmptyQuery()) { 2775ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb res.count = 0; 2785ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb res.values = null; 2795ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb return res; 2805ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb } 2815ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb startSuggestionsAsync(constraint); 2825ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb List<SuggestItem> filterResults = new ArrayList<SuggestItem>(); 2835ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb if (constraint != null) { 2845ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb for (CursorSource sc : mSources) { 2855ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb sc.runQuery(constraint); 286cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 2875ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb mixResults(filterResults); 2885ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb } 2895ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb synchronized (mResultsLock) { 2905ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb mFilterResults = filterResults; 29121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 2925ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb SuggestionResults mixed = buildSuggestionResults(); 2935ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb res.count = mixed.getLineCount(); 2945ff5c8b88968fa794eab4b7a263cae25f05bd4d3Michael Kolb res.values = mixed; 29521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return res; 29621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 29721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 29835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck void mixResults(List<SuggestItem> results) { 2995119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath int maxLines = getMaxLines(); 30021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb for (int i = 0; i < mSources.size(); i++) { 30121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb CursorSource s = mSources.get(i); 30235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck int n = Math.min(s.getCount(), maxLines); 30335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck maxLines -= n; 3040506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb boolean more = false; 30521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb for (int j = 0; j < n; j++) { 30635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck results.add(s.getItem()); 30721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb more = s.moveToNext(); 30821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 30921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 31021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 31121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 31221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 31321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb protected void publishResults(CharSequence constraint, FilterResults fresults) { 314cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (fresults.values instanceof SuggestionResults) { 315cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb mMixedResults = (SuggestionResults) fresults.values; 316a005cd7e4091513b189a6573b4fa96d762399c7eJohn Reck notifyDataSetChanged(); 317cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 31821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 3195119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath } 32021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 3215119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath private int getMaxLines() { 3225119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; 3235119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath maxLines = (int) Math.ceil(maxLines / 2.0); 3245119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath return maxLines; 32521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 32621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 32721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 32821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * sorted list of results of a suggestion query 32921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 33021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 33121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class SuggestionResults { 33221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 33321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ArrayList<SuggestItem> items; 33421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // count per type 33521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int[] counts; 33621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 33721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SuggestionResults() { 33821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb items = new ArrayList<SuggestItem>(24); 33921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // n of types: 34021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb counts = new int[5]; 34121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 34221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 34321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int getTypeCount(int type) { 34421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return counts[type]; 34521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 34621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 34721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb void addResult(SuggestItem item) { 34821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int ix = 0; 34921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb while ((ix < items.size()) && (item.type >= items.get(ix).type)) 35021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ix++; 35121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb items.add(ix, item); 35221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb counts[item.type]++; 35321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 35421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 35521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int getLineCount() { 3565119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath return Math.min((mLandscapeMode ? mLinesLandscape : mLinesPortrait), items.size()); 35721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 35821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 35935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck @Override 36021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public String toString() { 36121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (items == null) return null; 36221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (items.size() == 0) return "[]"; 36321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb StringBuilder sb = new StringBuilder(); 36421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb for (int i = 0; i < items.size(); i++) { 36521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SuggestItem item = items.get(i); 36621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb sb.append(item.type + ": " + item.title); 36721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (i < items.size() - 1) { 36821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb sb.append(", "); 36921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return sb.toString(); 37221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 37521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 37621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * data object to hold suggestion values 37721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 37880aad8d851601d39f73214c198111ca49e25f654Narayan Kamath public class SuggestItem { 37980aad8d851601d39f73214c198111ca49e25f654Narayan Kamath public String title; 38080aad8d851601d39f73214c198111ca49e25f654Narayan Kamath public String url; 38180aad8d851601d39f73214c198111ca49e25f654Narayan Kamath public int type; 38280aad8d851601d39f73214c198111ca49e25f654Narayan Kamath public String extra; 38321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 38421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem(String text, String u, int t) { 38521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb title = text; 38621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb url = u; 38721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb type = t; 38821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 389bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb 39021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 39121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 39221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb abstract class CursorSource { 39321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 39421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb Cursor mCursor; 39521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 39621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb boolean moveToNext() { 39721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return mCursor.moveToNext(); 39821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 39921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 40021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public abstract void runQuery(CharSequence constraint); 40121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 40221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public abstract SuggestItem getItem(); 40321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 40421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public int getCount() { 40521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return (mCursor != null) ? mCursor.getCount() : 0; 40621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 40721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 40821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void close() { 40921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 41021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.close(); 41121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 41221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 41321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 41421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 41521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 41621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * combined bookmark & history source 41721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 41821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class CombinedCursor extends CursorSource { 41921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 42021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 42121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem getItem() { 42221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if ((mCursor != null) && (!mCursor.isAfterLast())) { 42321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String title = mCursor.getString(1); 42421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String url = mCursor.getString(2); 42521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb boolean isBookmark = (mCursor.getInt(3) == 1); 42621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return new SuggestItem(getTitle(title, url), getUrl(title, url), 42721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY); 42821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 42921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 43021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 43121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 43221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 43321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void runQuery(CharSequence constraint) { 43421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // constraint != null 43521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 43621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.close(); 43721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 43821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String like = constraint + "%"; 43921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String[] args = null; 44021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String selection = null; 44121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (like.startsWith("http") || like.startsWith("file")) { 44221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args = new String[1]; 44321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[0] = like; 44421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb selection = "url LIKE ?"; 44521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 44621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args = new String[5]; 44721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[0] = "http://" + like; 44821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[1] = "http://www." + like; 44921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[2] = "https://" + like; 45021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[3] = "https://www." + like; 45121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // To match against titles. 45221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[4] = like; 45321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb selection = COMBINED_SELECTION; 45421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 4555e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck Uri.Builder ub = OmniboxSuggestions.CONTENT_URI.buildUpon(); 4560506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, 457ad373302b1e1a322144f818340fdce60f0eee403John Reck Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); 45821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor = 4590506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, 4605e8466f11f7acd78b83e0c388796d5ec0ab06c96John Reck selection, (constraint != null) ? args : null, null); 46121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 46221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.moveToFirst(); 46321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 46421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 46521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 46621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 46721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Provides the title (text line 1) for a browser suggestion, which should be the 46821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * webpage title. If the webpage title is empty, returns the stripped url instead. 46921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 47021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * @return the title string to use 47121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 47221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private String getTitle(String title, String url) { 47321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { 474fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck title = UrlUtils.stripUrl(url); 47521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 47621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return title; 47721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 47821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 47921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 48021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Provides the subtitle (text line 2) for a browser suggestion, which should be the 48121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * webpage url. If the webpage title is empty, then the url should go in the title 48221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * instead, and the subtitle should be empty, so this would return null. 48321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 48421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * @return the subtitle string to use, or null if none 48521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 48621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private String getUrl(String title, String url) { 4877d132b17c9dd1a816c68bf8505be2028691aaee3John Reck if (TextUtils.isEmpty(title) 4887d132b17c9dd1a816c68bf8505be2028691aaee3John Reck || TextUtils.getTrimmedLength(title) == 0 4897d132b17c9dd1a816c68bf8505be2028691aaee3John Reck || title.equals(url)) { 49021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 49121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 492fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck return UrlUtils.stripUrl(url); 49321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 49421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 49521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 49621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 49721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class SuggestCursor extends CursorSource { 49821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 49921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 50021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem getItem() { 50121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 50221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String title = mCursor.getString( 50321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)); 50421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String text2 = mCursor.getString( 50521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2)); 50621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String url = mCursor.getString( 50721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL)); 50821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String uri = mCursor.getString( 50921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA)); 51021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL; 51140f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck SuggestItem item = new SuggestItem(title, url, type); 51240f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck item.extra = mCursor.getString( 51340f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA)); 51440f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck return item; 51521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 51621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 51721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 51821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 51921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 52021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void runQuery(CharSequence constraint) { 52121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 52221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.close(); 52321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 52435e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck SearchEngine searchEngine = mSettings.getSearchEngine(); 52521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (!TextUtils.isEmpty(constraint)) { 52621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (searchEngine != null && searchEngine.supportsSuggestions()) { 52721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor = searchEngine.getSuggestions(mContext, constraint.toString()); 52821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 52921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.moveToFirst(); 53021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 53121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 53221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 5335119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath if (searchEngine.wantsEmptyQuery()) { 5345119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath mCursor = searchEngine.getSuggestions(mContext, ""); 5355119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath } 53621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor = null; 53721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 53821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 53921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 54021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 54121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 54235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck public void clearCache() { 54335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mFilterResults = null; 54435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mSuggestResults = null; 5455119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath notifyDataSetInvalidated(); 54635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 54735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 548117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck public void setIncognitoMode(boolean incognito) { 549117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck mIncognitoMode = incognito; 550117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck clearCache(); 551117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck } 5525119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 5535119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath static String getSuggestionTitle(SuggestItem item) { 5545119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath // There must be a better way to strip HTML from things. 5555119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath // This method is used in multiple places. It is also more 5565119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath // expensive than a standard html escaper. 5575119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath return (item.title != null) ? Html.fromHtml(item.title).toString() : null; 5585119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath } 5595119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 5605119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath static String getSuggestionUrl(SuggestItem item) { 5615119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath final String title = SuggestionsAdapter.getSuggestionTitle(item); 5625119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 5635119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath if (TextUtils.isEmpty(item.url)) { 5645119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath return title; 5655119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath } 5665119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath 5675119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath return item.url; 5685119edd5744cfc6d3a8ed480a8853586c737bed4Narayan Kamath } 56921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb} 570