SuggestionsAdapter.java revision bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331
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 com.android.browser.search.SearchEngine; 2021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 2121ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.app.SearchManager; 2221ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.content.Context; 2321ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.database.Cursor; 2421ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.net.Uri; 2535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reckimport android.os.AsyncTask; 2621ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.provider.BrowserContract; 2721ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.text.TextUtils; 28bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolbimport android.util.Log; 2921ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.LayoutInflater; 3021ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.View; 3121ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.View.OnClickListener; 3221ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.ViewGroup; 3321ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.BaseAdapter; 3421ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.Filter; 3521ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.Filterable; 3621ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.ImageView; 3721ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.TextView; 3821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 3921ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport java.util.ArrayList; 4021ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport java.util.List; 4121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 4221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb/** 4321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * adapter to wrap multiple cursors for url/search completions 4421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 45bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolbpublic class SuggestionsAdapter extends BaseAdapter implements Filterable, 46bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb OnClickListener { 4721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 4835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck static final int TYPE_BOOKMARK = 0; 4935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck static final int TYPE_SUGGEST_URL = 1; 5035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck static final int TYPE_HISTORY = 2; 5135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck static final int TYPE_SEARCH = 3; 5235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck static final int TYPE_SUGGEST = 4; 53bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb static final int TYPE_VOICE_SEARCH = 5; 5421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 5521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private static final String[] COMBINED_PROJECTION = 5621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE, 5721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK}; 5821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 5921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private static final String COMBINED_SELECTION = 6021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)"; 6121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 6221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb Context mContext; 6321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb Filter mFilter; 6435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestionResults mMixedResults; 6535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck List<SuggestItem> mSuggestResults, mFilterResults; 6621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb List<CursorSource> mSources; 6721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb boolean mLandscapeMode; 6821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb CompletionListener mListener; 6921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int mLinesPortrait; 7021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int mLinesLandscape; 7135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck Object mResultsLock = new Object(); 72cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb List<String> mVoiceResults; 731605bef4e1f99805a801308f97ade622b907dc7aJohn Reck boolean mReverseResults; 7421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 7521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb interface CompletionListener { 7621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 7721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void onSearch(String txt); 7821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 79bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb public void onSelect(String txt, int type, String extraData); 8021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 810506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb public void onFilterComplete(int count); 820506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb 8321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 8421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 8521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestionsAdapter(Context ctx, CompletionListener listener) { 8621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mContext = ctx; 8721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mListener = listener; 8821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mLinesPortrait = mContext.getResources(). 8921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb getInteger(R.integer.max_suggest_lines_portrait); 9021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mLinesLandscape = mContext.getResources(). 9121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb getInteger(R.integer.max_suggest_lines_landscape); 9221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mFilter = new SuggestFilter(); 9321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb addSource(new CombinedCursor()); 9421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 9521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 96cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb void setVoiceResults(List<String> voiceResults) { 97cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb mVoiceResults = voiceResults; 98a50c446037eecaa2dcd83dab133bd9dc390ede69Michael Kolb notifyDataSetChanged(); 99cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 100cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb 10121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void setLandscapeMode(boolean mode) { 10221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mLandscapeMode = mode; 10335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck notifyDataSetChanged(); 10421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 10521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 10621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void addSource(CursorSource c) { 10721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mSources == null) { 10821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mSources = new ArrayList<CursorSource>(5); 10921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 11021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mSources.add(c); 11121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 11221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 11321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 11421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void onClick(View v) { 115ad373302b1e1a322144f818340fdce60f0eee403John Reck SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag(); 11621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (R.id.icon2 == v.getId()) { 11721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // replace input field text with suggestion text 11821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mListener.onSearch(item.title); 11921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 120bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb mListener.onSelect( 121bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb (TextUtils.isEmpty(item.url)? item.title : item.url), 122bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb item.type, item.extra); 12321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 12421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 12521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 12621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 12721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public Filter getFilter() { 12821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return mFilter; 12921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 13021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 13121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 13221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public int getCount() { 133cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (mVoiceResults != null) { 134cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb return mVoiceResults.size(); 135cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 13635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck return (mMixedResults == null) ? 0 : mMixedResults.getLineCount(); 13721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 13821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 13921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 14021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem getItem(int position) { 1411605bef4e1f99805a801308f97ade622b907dc7aJohn Reck if (mReverseResults) { 1421605bef4e1f99805a801308f97ade622b907dc7aJohn Reck position = (getCount() - 1) - position; 1431605bef4e1f99805a801308f97ade622b907dc7aJohn Reck } 144cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (mVoiceResults != null) { 145bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb SuggestItem item = new SuggestItem(mVoiceResults.get(position), 146bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb null, TYPE_VOICE_SEARCH); 147bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb item.extra = Integer.toString(position); 148bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb return item; 149cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 15035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (mMixedResults == null) { 15121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 15221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 153ad373302b1e1a322144f818340fdce60f0eee403John Reck return mMixedResults.items.get(position); 15421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 15521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 1561605bef4e1f99805a801308f97ade622b907dc7aJohn Reck public void setReverseResults(boolean reverse) { 1571605bef4e1f99805a801308f97ade622b907dc7aJohn Reck mReverseResults = reverse; 1581605bef4e1f99805a801308f97ade622b907dc7aJohn Reck } 1591605bef4e1f99805a801308f97ade622b907dc7aJohn Reck 16021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 16121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public long getItemId(int position) { 1621605bef4e1f99805a801308f97ade622b907dc7aJohn Reck return position; 16321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 16421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 16521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 16621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public View getView(int position, View convertView, ViewGroup parent) { 16721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb final LayoutInflater inflater = LayoutInflater.from(mContext); 16835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck View view = convertView; 16935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (view == null) { 170ad373302b1e1a322144f818340fdce60f0eee403John Reck view = inflater.inflate(R.layout.suggestion_item, parent, false); 17121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 172ad373302b1e1a322144f818340fdce60f0eee403John Reck bindView(view, getItem(position)); 173ad373302b1e1a322144f818340fdce60f0eee403John Reck return view; 17421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 17521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 17621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private void bindView(View view, SuggestItem item) { 17721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // store item for click handling 17821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb view.setTag(item); 17921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb TextView tv1 = (TextView) view.findViewById(android.R.id.text1); 18021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb TextView tv2 = (TextView) view.findViewById(android.R.id.text2); 18121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ImageView ic1 = (ImageView) view.findViewById(R.id.icon1); 18221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb View ic2 = view.findViewById(R.id.icon2); 1837b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb View div = view.findViewById(R.id.divider); 18421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb tv1.setText(item.title); 185ad373302b1e1a322144f818340fdce60f0eee403John Reck if (TextUtils.isEmpty(item.url)) { 186ad373302b1e1a322144f818340fdce60f0eee403John Reck tv2.setVisibility(View.GONE); 187ad373302b1e1a322144f818340fdce60f0eee403John Reck } else { 188ad373302b1e1a322144f818340fdce60f0eee403John Reck tv2.setVisibility(View.VISIBLE); 189ad373302b1e1a322144f818340fdce60f0eee403John Reck tv2.setText(item.url); 190ad373302b1e1a322144f818340fdce60f0eee403John Reck } 19121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int id = -1; 19221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb switch (item.type) { 19321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_SUGGEST: 19421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_SEARCH: 195bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb case TYPE_VOICE_SEARCH: 19621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_suggest; 19721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 19821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_BOOKMARK: 19921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_bookmark; 20021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 20121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_HISTORY: 20221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_history; 20321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 20421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb case TYPE_SUGGEST_URL: 20521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = R.drawable.ic_search_category_browser; 20621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb break; 20721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb default: 20821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb id = -1; 20921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 21021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (id != -1) { 21121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ic1.setImageDrawable(mContext.getResources().getDrawable(id)); 21221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 213bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb ic2.setVisibility(((TYPE_SUGGEST == item.type) 214bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb || (TYPE_SEARCH == item.type) 215bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb || (TYPE_VOICE_SEARCH == item.type)) 21621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ? View.VISIBLE : View.GONE); 2177b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb div.setVisibility(ic2.getVisibility()); 21821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ic2.setOnClickListener(this); 219ad373302b1e1a322144f818340fdce60f0eee403John Reck view.findViewById(R.id.suggestion).setOnClickListener(this); 22021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 22121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 22235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> { 22335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 22435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck @Override 22535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck protected List<SuggestItem> doInBackground(CharSequence... params) { 22635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestCursor cursor = new SuggestCursor(); 22735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck cursor.runQuery(params[0]); 22835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck List<SuggestItem> results = new ArrayList<SuggestItem>(); 22935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck int count = cursor.getCount(); 23035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck for (int i = 0; i < count; i++) { 23135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck results.add(cursor.getItem()); 23235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck cursor.moveToNext(); 23335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 23435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck cursor.close(); 23535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck return results; 23635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 23735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 23835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck @Override 23935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck protected void onPostExecute(List<SuggestItem> items) { 24035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mSuggestResults = items; 24135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mMixedResults = buildSuggestionResults(); 24235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck notifyDataSetChanged(); 24335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mListener.onFilterComplete(mMixedResults.getLineCount()); 24435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 24535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 24635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 24735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestionResults buildSuggestionResults() { 24835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck SuggestionResults mixed = new SuggestionResults(); 24935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck List<SuggestItem> filter, suggest; 25035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck synchronized (mResultsLock) { 25135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck filter = mFilterResults; 25235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck suggest = mSuggestResults; 25335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 25435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (filter != null) { 25535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck for (SuggestItem item : filter) { 25635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mixed.addResult(item); 25735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 25835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 25935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck if (suggest != null) { 26035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck for (SuggestItem item : suggest) { 26135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mixed.addResult(item); 26235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 26335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 26435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck return mixed; 26535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 26621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 26735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck class SuggestFilter extends Filter { 26821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 26921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 27021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public CharSequence convertResultToString(Object item) { 27121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (item == null) { 27221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return ""; 27321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 27421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SuggestItem sitem = (SuggestItem) item; 27521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (sitem.title != null) { 27621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return sitem.title; 27721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 27821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return sitem.url; 27921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 28021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 28121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 28235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck void startSuggestionsAsync(final CharSequence constraint) { 28335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck new SlowFilterTask().execute(constraint); 28435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 28535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 28621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 28721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb protected FilterResults performFiltering(CharSequence constraint) { 28821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb FilterResults res = new FilterResults(); 289cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (mVoiceResults == null) { 290cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (TextUtils.isEmpty(constraint)) { 291cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb res.count = 0; 292cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb res.values = null; 293cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb return res; 29421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 295cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb startSuggestionsAsync(constraint); 296cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb List<SuggestItem> filterResults = new ArrayList<SuggestItem>(); 297cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (constraint != null) { 298cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb for (CursorSource sc : mSources) { 299cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb sc.runQuery(constraint); 300cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 301cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb mixResults(filterResults); 302cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 303cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb synchronized (mResultsLock) { 304cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb mFilterResults = filterResults; 305cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 306cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb SuggestionResults mixed = buildSuggestionResults(); 307cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb res.count = mixed.getLineCount(); 308cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb res.values = mixed; 309cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } else { 310bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb Log.i("voice", "using voice results"); 311cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb res.count = mVoiceResults.size(); 312cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb res.values = mVoiceResults; 31321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 31421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return res; 31521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 31621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 31735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck void mixResults(List<SuggestItem> results) { 318ad373302b1e1a322144f818340fdce60f0eee403John Reck int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait; 319ad373302b1e1a322144f818340fdce60f0eee403John Reck maxLines = (int) Math.ceil(maxLines / 2.0); 32021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb for (int i = 0; i < mSources.size(); i++) { 32121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb CursorSource s = mSources.get(i); 32235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck int n = Math.min(s.getCount(), maxLines); 32335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck maxLines -= n; 3240506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb boolean more = false; 32521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb for (int j = 0; j < n; j++) { 32635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck results.add(s.getItem()); 32721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb more = s.moveToNext(); 32821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 32921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 33021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 33121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 33221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 33321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb protected void publishResults(CharSequence constraint, FilterResults fresults) { 334cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb if (fresults.values instanceof SuggestionResults) { 335cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb mMixedResults = (SuggestionResults) fresults.values; 336cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb mListener.onFilterComplete(fresults.count); 337cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb } 33821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb notifyDataSetChanged(); 33921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 34021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 34121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 34221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 34321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 34421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * sorted list of results of a suggestion query 34521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 34621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 34721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class SuggestionResults { 34821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 34921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ArrayList<SuggestItem> items; 35021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // count per type 35121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int[] counts; 35221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 35321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SuggestionResults() { 35421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb items = new ArrayList<SuggestItem>(24); 35521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // n of types: 35621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb counts = new int[5]; 35721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 35821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 35921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int getTypeCount(int type) { 36021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return counts[type]; 36121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 36221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 36321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb void addResult(SuggestItem item) { 36421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int ix = 0; 36521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb while ((ix < items.size()) && (item.type >= items.get(ix).type)) 36621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb ix++; 36721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb items.add(ix, item); 36821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb counts[item.type]++; 36921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 37121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int getLineCount() { 37221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mLandscapeMode) { 373ad373302b1e1a322144f818340fdce60f0eee403John Reck return Math.min(mLinesLandscape, items.size()); 37421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 375ad373302b1e1a322144f818340fdce60f0eee403John Reck return Math.min(mLinesPortrait, items.size()); 37621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 37821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 37935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck @Override 38021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public String toString() { 38121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (items == null) return null; 38221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (items.size() == 0) return "[]"; 38321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb StringBuilder sb = new StringBuilder(); 38421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb for (int i = 0; i < items.size(); i++) { 38521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SuggestItem item = items.get(i); 38621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb sb.append(item.type + ": " + item.title); 38721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (i < items.size() - 1) { 38821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb sb.append(", "); 38921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 39021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 39121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return sb.toString(); 39221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 39321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 39421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 39521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 39621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * data object to hold suggestion values 39721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 39821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class SuggestItem { 39921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String title; 40021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String url; 40121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int type; 40240f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck String extra; 40321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 40421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem(String text, String u, int t) { 40521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb title = text; 40621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb url = u; 40721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb type = t; 40821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 409bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb 41021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 41121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 41221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb abstract class CursorSource { 41321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 41421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb Cursor mCursor; 41521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 41621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb boolean moveToNext() { 41721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return mCursor.moveToNext(); 41821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 41921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 42021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public abstract void runQuery(CharSequence constraint); 42121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 42221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public abstract SuggestItem getItem(); 42321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 42421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public int getCount() { 42521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return (mCursor != null) ? mCursor.getCount() : 0; 42621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 42721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 42821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void close() { 42921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 43021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.close(); 43121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 43221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 43321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 43421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 43521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 43621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * combined bookmark & history source 43721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 43821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class CombinedCursor extends CursorSource { 43921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 44021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 44121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem getItem() { 44221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if ((mCursor != null) && (!mCursor.isAfterLast())) { 44321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String title = mCursor.getString(1); 44421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String url = mCursor.getString(2); 44521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb boolean isBookmark = (mCursor.getInt(3) == 1); 44621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return new SuggestItem(getTitle(title, url), getUrl(title, url), 44721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY); 44821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 44921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 45021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 45121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 45221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 45321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void runQuery(CharSequence constraint) { 45421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // constraint != null 45521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 45621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.close(); 45721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 45821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String like = constraint + "%"; 45921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String[] args = null; 46021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String selection = null; 46121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (like.startsWith("http") || like.startsWith("file")) { 46221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args = new String[1]; 46321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[0] = like; 46421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb selection = "url LIKE ?"; 46521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 46621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args = new String[5]; 46721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[0] = "http://" + like; 46821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[1] = "http://www." + like; 46921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[2] = "https://" + like; 47021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[3] = "https://www." + like; 47121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb // To match against titles. 47221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb args[4] = like; 47321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb selection = COMBINED_SELECTION; 47421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 47521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon(); 4760506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb ub.appendQueryParameter(BrowserContract.PARAM_LIMIT, 477ad373302b1e1a322144f818340fdce60f0eee403John Reck Integer.toString(Math.max(mLinesLandscape, mLinesPortrait))); 4783dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck BookmarkUtils.addAccountInfo(mContext, ub); 47921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor = 4800506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION, 48121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb selection, 48221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb (constraint != null) ? args : null, 4833dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck BrowserContract.Combined.IS_BOOKMARK + " DESC, " + 48421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb BrowserContract.Combined.VISITS + " DESC, " + 48521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb BrowserContract.Combined.DATE_LAST_VISITED + " DESC"); 48621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 48721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.moveToFirst(); 48821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 48921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 49021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 49121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 49221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Provides the title (text line 1) for a browser suggestion, which should be the 49321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * webpage title. If the webpage title is empty, returns the stripped url instead. 49421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 49521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * @return the title string to use 49621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 49721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private String getTitle(String title, String url) { 49821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { 499fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck title = UrlUtils.stripUrl(url); 50021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 50121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return title; 50221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 50321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 50421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb /** 50521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * Provides the subtitle (text line 2) for a browser suggestion, which should be the 50621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * webpage url. If the webpage title is empty, then the url should go in the title 50721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * instead, and the subtitle should be empty, so this would return null. 50821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * 50921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * @return the subtitle string to use, or null if none 51021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */ 51121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb private String getUrl(String title, String url) { 5127d132b17c9dd1a816c68bf8505be2028691aaee3John Reck if (TextUtils.isEmpty(title) 5137d132b17c9dd1a816c68bf8505be2028691aaee3John Reck || TextUtils.getTrimmedLength(title) == 0 5147d132b17c9dd1a816c68bf8505be2028691aaee3John Reck || title.equals(url)) { 51521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 51621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 517fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck return UrlUtils.stripUrl(url); 51821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 51921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 52021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 52121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 52221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb class SuggestCursor extends CursorSource { 52321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 52421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 52521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public SuggestItem getItem() { 52621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 52721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String title = mCursor.getString( 52821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)); 52921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String text2 = mCursor.getString( 53021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2)); 53121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String url = mCursor.getString( 53221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL)); 53321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb String uri = mCursor.getString( 53421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA)); 53521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL; 53640f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck SuggestItem item = new SuggestItem(title, url, type); 53740f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck item.extra = mCursor.getString( 53840f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA)); 53940f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck return item; 54021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 54121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb return null; 54221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 54321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 54421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb @Override 54521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb public void runQuery(CharSequence constraint) { 54621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 54721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.close(); 54821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 54921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (!TextUtils.isEmpty(constraint)) { 55021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine(); 55121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (searchEngine != null && searchEngine.supportsSuggestions()) { 55221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor = searchEngine.getSuggestions(mContext, constraint.toString()); 55321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb if (mCursor != null) { 55421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor.moveToFirst(); 55521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 55621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 55721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } else { 55821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb mCursor = null; 55921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 56021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 56121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 56221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb } 56321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb 56435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck public void clearCache() { 56535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mFilterResults = null; 56635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck mSuggestResults = null; 56735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck } 56835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck 56921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb} 570