SuggestionsAdapter.java revision 1605bef4e1f99805a801308f97ade622b907dc7a
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;
2821ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.LayoutInflater;
2921ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.View;
3021ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.View.OnClickListener;
3121ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.view.ViewGroup;
3221ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.BaseAdapter;
3321ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.Filter;
3421ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.Filterable;
3521ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.ImageView;
3621ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport android.widget.TextView;
3721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
3821ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport java.util.ArrayList;
3921ce4d295802db811873b46e7821abfa0540dab2Michael Kolbimport java.util.List;
4021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
4121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb/**
4221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb * adapter to wrap multiple cursors for url/search completions
4321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb */
4421ce4d295802db811873b46e7821abfa0540dab2Michael Kolbpublic class SuggestionsAdapter extends BaseAdapter implements Filterable, OnClickListener {
4521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
4635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_BOOKMARK = 0;
4735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_SUGGEST_URL = 1;
4835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_HISTORY = 2;
4935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_SEARCH = 3;
5035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_SUGGEST = 4;
5121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
5221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private static final String[] COMBINED_PROJECTION =
5321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
5421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
5521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
5621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private static final String COMBINED_SELECTION =
5721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
5821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
5921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    Context mContext;
6021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    Filter mFilter;
6135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    SuggestionResults mMixedResults;
6235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    List<SuggestItem> mSuggestResults, mFilterResults;
6321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    List<CursorSource> mSources;
6421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    boolean mLandscapeMode;
6521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    CompletionListener mListener;
6621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    int mLinesPortrait;
6721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    int mLinesLandscape;
6835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    Object mResultsLock = new Object();
69cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    List<String> mVoiceResults;
701605bef4e1f99805a801308f97ade622b907dc7aJohn Reck    boolean mReverseResults;
7121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    interface CompletionListener {
7321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void onSearch(String txt);
7521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7640f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck        public void onSelect(String txt, String extraData);
7721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
780506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb        public void onFilterComplete(int count);
790506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb
8021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
8121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
8221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public SuggestionsAdapter(Context ctx, CompletionListener listener) {
8321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mContext = ctx;
8421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mListener = listener;
8521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLinesPortrait = mContext.getResources().
8621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                getInteger(R.integer.max_suggest_lines_portrait);
8721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLinesLandscape = mContext.getResources().
8821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                getInteger(R.integer.max_suggest_lines_landscape);
8921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mFilter = new SuggestFilter();
9021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        addSource(new CombinedCursor());
9121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
9221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
93cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    void setVoiceResults(List<String> voiceResults) {
94cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        mVoiceResults = voiceResults;
95a50c446037eecaa2dcd83dab133bd9dc390ede69Michael Kolb        notifyDataSetChanged();
96cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    }
97cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb
9821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void setLandscapeMode(boolean mode) {
9921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLandscapeMode = mode;
10035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        notifyDataSetChanged();
10121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
10221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
10321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void addSource(CursorSource c) {
10421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (mSources == null) {
10521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mSources = new ArrayList<CursorSource>(5);
10621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
10721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mSources.add(c);
10821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
10921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
11021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
11121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void onClick(View v) {
112ad373302b1e1a322144f818340fdce60f0eee403John Reck        SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
11321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (R.id.icon2 == v.getId()) {
11421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // replace input field text with suggestion text
11521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mListener.onSearch(item.title);
11621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        } else {
11740f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck            mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url),
11840f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                    item.extra);
11921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
12021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
12121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
12221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
12321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public Filter getFilter() {
12421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        return mFilter;
12521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
12621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
12721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
12821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public int getCount() {
129cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        if (mVoiceResults != null) {
130cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            return mVoiceResults.size();
131cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        }
13235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
13321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
13421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
13521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
13621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public SuggestItem getItem(int position) {
1371605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        if (mReverseResults) {
1381605bef4e1f99805a801308f97ade622b907dc7aJohn Reck            position = (getCount() - 1) - position;
1391605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        }
140cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        if (mVoiceResults != null) {
141cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            return new SuggestItem(mVoiceResults.get(position), null,
142cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    TYPE_SEARCH);
143cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        }
14435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (mMixedResults == null) {
14521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
14621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
147ad373302b1e1a322144f818340fdce60f0eee403John Reck        return mMixedResults.items.get(position);
14821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
14921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
1501605bef4e1f99805a801308f97ade622b907dc7aJohn Reck    public void setReverseResults(boolean reverse) {
1511605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        mReverseResults = reverse;
1521605bef4e1f99805a801308f97ade622b907dc7aJohn Reck    }
1531605bef4e1f99805a801308f97ade622b907dc7aJohn Reck
15421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
15521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public long getItemId(int position) {
1561605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        return position;
15721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
15821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
15921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
16021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public View getView(int position, View convertView, ViewGroup parent) {
16121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        final LayoutInflater inflater = LayoutInflater.from(mContext);
16235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        View view = convertView;
16335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (view == null) {
164ad373302b1e1a322144f818340fdce60f0eee403John Reck            view = inflater.inflate(R.layout.suggestion_item, parent, false);
16521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
166ad373302b1e1a322144f818340fdce60f0eee403John Reck        bindView(view, getItem(position));
167ad373302b1e1a322144f818340fdce60f0eee403John Reck        return view;
16821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
16921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
17021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private void bindView(View view, SuggestItem item) {
17121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        // store item for click handling
17221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        view.setTag(item);
17321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
17421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
17521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
17621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        View ic2 = view.findViewById(R.id.icon2);
1777b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb        View div = view.findViewById(R.id.divider);
17821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        tv1.setText(item.title);
179ad373302b1e1a322144f818340fdce60f0eee403John Reck        if (TextUtils.isEmpty(item.url)) {
180ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setVisibility(View.GONE);
181ad373302b1e1a322144f818340fdce60f0eee403John Reck        } else {
182ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setVisibility(View.VISIBLE);
183ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setText(item.url);
184ad373302b1e1a322144f818340fdce60f0eee403John Reck        }
18521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int id = -1;
18621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        switch (item.type) {
18721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SUGGEST:
18821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SEARCH:
18921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_suggest;
19021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
19121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_BOOKMARK:
19221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_bookmark;
19321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
19421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_HISTORY:
19521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_history;
19621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
19721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SUGGEST_URL:
19821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_browser;
19921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
20021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            default:
20121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = -1;
20221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
20321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (id != -1) {
20421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            ic1.setImageDrawable(mContext.getResources().getDrawable(id));
20521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
20621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ic2.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
20721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                ? View.VISIBLE : View.GONE);
2087b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb        div.setVisibility(ic2.getVisibility());
20921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ic2.setOnClickListener(this);
210ad373302b1e1a322144f818340fdce60f0eee403John Reck        view.findViewById(R.id.suggestion).setOnClickListener(this);
21121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
21221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
21335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
21435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
21535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
21635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        protected List<SuggestItem> doInBackground(CharSequence... params) {
21735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            SuggestCursor cursor = new SuggestCursor();
21835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            cursor.runQuery(params[0]);
21935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            List<SuggestItem> results = new ArrayList<SuggestItem>();
22035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            int count = cursor.getCount();
22135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (int i = 0; i < count; i++) {
22235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                results.add(cursor.getItem());
22335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                cursor.moveToNext();
22435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
22535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            cursor.close();
22635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            return results;
22735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
22835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
22935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
23035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        protected void onPostExecute(List<SuggestItem> items) {
23135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mSuggestResults = items;
23235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mMixedResults = buildSuggestionResults();
23335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            notifyDataSetChanged();
23435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mListener.onFilterComplete(mMixedResults.getLineCount());
23535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
23635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
23735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
23835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    SuggestionResults buildSuggestionResults() {
23935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        SuggestionResults mixed = new SuggestionResults();
24035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        List<SuggestItem> filter, suggest;
24135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        synchronized (mResultsLock) {
24235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            filter = mFilterResults;
24335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            suggest = mSuggestResults;
24435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
24535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (filter != null) {
24635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (SuggestItem item : filter) {
24735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                mixed.addResult(item);
24835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
24935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
25035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (suggest != null) {
25135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (SuggestItem item : suggest) {
25235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                mixed.addResult(item);
25335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
25435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
25535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        return mixed;
25635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
25721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
25835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    class SuggestFilter extends Filter {
25921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
26021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
26121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public CharSequence convertResultToString(Object item) {
26221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (item == null) {
26321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return "";
26421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
26521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            SuggestItem sitem = (SuggestItem) item;
26621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (sitem.title != null) {
26721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return sitem.title;
26821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
26921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return sitem.url;
27021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
27121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
27221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
27335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        void startSuggestionsAsync(final CharSequence constraint) {
27435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            new SlowFilterTask().execute(constraint);
27535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
27635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
27721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
27821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        protected FilterResults performFiltering(CharSequence constraint) {
27921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            FilterResults res = new FilterResults();
280cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            if (mVoiceResults == null) {
281cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                if (TextUtils.isEmpty(constraint)) {
282cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    res.count = 0;
283cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    res.values = null;
284cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    return res;
28521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
286cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                startSuggestionsAsync(constraint);
287cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
288cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                if (constraint != null) {
289cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    for (CursorSource sc : mSources) {
290cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                        sc.runQuery(constraint);
291cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    }
292cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    mixResults(filterResults);
293cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                }
294cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                synchronized (mResultsLock) {
295cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    mFilterResults = filterResults;
296cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                }
297cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                SuggestionResults mixed = buildSuggestionResults();
298cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.count = mixed.getLineCount();
299cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.values = mixed;
300cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            } else {
301cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.count = mVoiceResults.size();
302cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.values = mVoiceResults;
30321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
30421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return res;
30521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
30621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
30735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        void mixResults(List<SuggestItem> results) {
308ad373302b1e1a322144f818340fdce60f0eee403John Reck            int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
309ad373302b1e1a322144f818340fdce60f0eee403John Reck            maxLines = (int) Math.ceil(maxLines / 2.0);
31021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            for (int i = 0; i < mSources.size(); i++) {
31121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                CursorSource s = mSources.get(i);
31235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                int n = Math.min(s.getCount(), maxLines);
31335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                maxLines -= n;
3140506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb                boolean more = false;
31521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                for (int j = 0; j < n; j++) {
31635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                    results.add(s.getItem());
31721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    more = s.moveToNext();
31821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
31921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
32021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
32121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
32221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
32321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        protected void publishResults(CharSequence constraint, FilterResults fresults) {
324cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            if (fresults.values instanceof SuggestionResults) {
325cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                mMixedResults = (SuggestionResults) fresults.values;
326cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                mListener.onFilterComplete(fresults.count);
327cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            }
32821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            notifyDataSetChanged();
32921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
33021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
33221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
33421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * sorted list of results of a suggestion query
33521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     *
33621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
33721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestionResults {
33821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ArrayList<SuggestItem> items;
34021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        // count per type
34121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int[] counts;
34221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
34321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        SuggestionResults() {
34421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            items = new ArrayList<SuggestItem>(24);
34521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // n of types:
34621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            counts = new int[5];
34721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
34821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
34921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int getTypeCount(int type) {
35021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return counts[type];
35121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
35221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
35321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        void addResult(SuggestItem item) {
35421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            int ix = 0;
35521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            while ((ix < items.size()) && (item.type >= items.get(ix).type))
35621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                ix++;
35721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            items.add(ix, item);
35821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            counts[item.type]++;
35921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
36021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
36121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int getLineCount() {
36221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mLandscapeMode) {
363ad373302b1e1a322144f818340fdce60f0eee403John Reck                return Math.min(mLinesLandscape, items.size());
36421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
365ad373302b1e1a322144f818340fdce60f0eee403John Reck                return Math.min(mLinesPortrait, items.size());
36621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
36721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
36821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
36935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
37021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public String toString() {
37121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (items == null) return null;
37221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (items.size() == 0) return "[]";
37321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            StringBuilder sb = new StringBuilder();
37421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            for (int i = 0; i < items.size(); i++) {
37521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                SuggestItem item = items.get(i);
37621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                sb.append(item.type + ": " + item.title);
37721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                if (i < items.size() - 1) {
37821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    sb.append(", ");
37921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
38021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
38121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return sb.toString();
38221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
38321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
38421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
38521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
38621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * data object to hold suggestion values
38721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
38821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestItem {
38921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        String title;
39021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        String url;
39121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int type;
39240f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck        String extra;
39321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
39421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem(String text, String u, int t) {
39521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            title = text;
39621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            url = u;
39721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            type = t;
39821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
39921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
40021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    abstract class CursorSource {
40221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        Cursor mCursor;
40421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        boolean moveToNext() {
40621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return mCursor.moveToNext();
40721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
40821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public abstract void runQuery(CharSequence constraint);
41021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public abstract SuggestItem getItem();
41221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public int getCount() {
41421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return (mCursor != null) ? mCursor.getCount() : 0;
41521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
41621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void close() {
41821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
41921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
42021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
42121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
42221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
42321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
42421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
42521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * combined bookmark & history source
42621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
42721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class CombinedCursor extends CursorSource {
42821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
42921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
43021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem getItem() {
43121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if ((mCursor != null) && (!mCursor.isAfterLast())) {
43221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String title = mCursor.getString(1);
43321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String url = mCursor.getString(2);
43421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                boolean isBookmark = (mCursor.getInt(3) == 1);
43521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return new SuggestItem(getTitle(title, url), getUrl(title, url),
43621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
43721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
43821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
43921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
44021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
44121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
44221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void runQuery(CharSequence constraint) {
44321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // constraint != null
44421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
44521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
44621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
44721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String like = constraint + "%";
44821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String[] args = null;
44921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String selection = null;
45021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (like.startsWith("http") || like.startsWith("file")) {
45121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args = new String[1];
45221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[0] = like;
45321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                selection = "url LIKE ?";
45421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
45521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args = new String[5];
45621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[0] = "http://" + like;
45721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[1] = "http://www." + like;
45821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[2] = "https://" + like;
45921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[3] = "https://www." + like;
46021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                // To match against titles.
46121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[4] = like;
46221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                selection = COMBINED_SELECTION;
46321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
46421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
4650506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb            ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
466ad373302b1e1a322144f818340fdce60f0eee403John Reck                    Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
4673dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck            BookmarkUtils.addAccountInfo(mContext, ub);
46821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mCursor =
4690506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb                    mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
47021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            selection,
47121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            (constraint != null) ? args : null,
4723dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck                            BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
47321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            BrowserContract.Combined.VISITS + " DESC, " +
47421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
47521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
47621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.moveToFirst();
47721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
47821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
47921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
48021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        /**
48121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * Provides the title (text line 1) for a browser suggestion, which should be the
48221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * webpage title. If the webpage title is empty, returns the stripped url instead.
48321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         *
48421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * @return the title string to use
48521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         */
48621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        private String getTitle(String title, String url) {
48721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
488fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck                title = UrlUtils.stripUrl(url);
48921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
49021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return title;
49121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
49221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
49321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        /**
49421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * Provides the subtitle (text line 2) for a browser suggestion, which should be the
49521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * webpage url. If the webpage title is empty, then the url should go in the title
49621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * instead, and the subtitle should be empty, so this would return null.
49721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         *
49821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * @return the subtitle string to use, or null if none
49921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         */
50021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        private String getUrl(String title, String url) {
5017d132b17c9dd1a816c68bf8505be2028691aaee3John Reck            if (TextUtils.isEmpty(title)
5027d132b17c9dd1a816c68bf8505be2028691aaee3John Reck                    || TextUtils.getTrimmedLength(title) == 0
5037d132b17c9dd1a816c68bf8505be2028691aaee3John Reck                    || title.equals(url)) {
50421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return null;
50521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
506fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck                return UrlUtils.stripUrl(url);
50721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
50821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
50921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
51021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
51121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestCursor extends CursorSource {
51221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
51321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
51421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem getItem() {
51521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
51621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String title = mCursor.getString(
51721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
51821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String text2 = mCursor.getString(
51921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
52021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String url = mCursor.getString(
52121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
52221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String uri = mCursor.getString(
52321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
52421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
52540f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                SuggestItem item = new SuggestItem(title, url, type);
52640f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                item.extra = mCursor.getString(
52740f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
52840f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                return item;
52921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
53021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
53121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
53221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
53321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
53421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void runQuery(CharSequence constraint) {
53521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
53621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
53721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
53821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (!TextUtils.isEmpty(constraint)) {
53921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
54021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                if (searchEngine != null && searchEngine.supportsSuggestions()) {
54121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
54221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    if (mCursor != null) {
54321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.moveToFirst();
54421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    }
54521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
54621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
54721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor = null;
54821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
54921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
55021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
55121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
55221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
55335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    public void clearCache() {
55435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        mFilterResults = null;
55535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        mSuggestResults = null;
55635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
55735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
55821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb}
559