SuggestionsAdapter.java revision ad373302b1e1a322144f818340fdce60f0eee403
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;
7021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    interface CompletionListener {
7221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void onSearch(String txt);
7421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7540f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck        public void onSelect(String txt, String extraData);
7621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
770506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb        public void onFilterComplete(int count);
780506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb
7921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
8021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
8121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public SuggestionsAdapter(Context ctx, CompletionListener listener) {
8221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mContext = ctx;
8321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mListener = listener;
8421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLinesPortrait = mContext.getResources().
8521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                getInteger(R.integer.max_suggest_lines_portrait);
8621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLinesLandscape = mContext.getResources().
8721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                getInteger(R.integer.max_suggest_lines_landscape);
8821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mFilter = new SuggestFilter();
8921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        addSource(new CombinedCursor());
9021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
9121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
92cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    void setVoiceResults(List<String> voiceResults) {
93cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        mVoiceResults = voiceResults;
94a50c446037eecaa2dcd83dab133bd9dc390ede69Michael Kolb        notifyDataSetChanged();
95cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    }
96cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb
9721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void setLandscapeMode(boolean mode) {
9821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLandscapeMode = mode;
9935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        notifyDataSetChanged();
10021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
10121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
10221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void addSource(CursorSource c) {
10321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (mSources == null) {
10421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mSources = new ArrayList<CursorSource>(5);
10521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
10621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mSources.add(c);
10721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
10821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
10921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
11021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void onClick(View v) {
111ad373302b1e1a322144f818340fdce60f0eee403John Reck        SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
11221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (R.id.icon2 == v.getId()) {
11321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // replace input field text with suggestion text
11421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mListener.onSearch(item.title);
11521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        } else {
11640f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck            mListener.onSelect((TextUtils.isEmpty(item.url)? item.title : item.url),
11740f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                    item.extra);
11821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
11921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
12021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
12121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
12221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public Filter getFilter() {
12321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        return mFilter;
12421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
12521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
12621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
12721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public int getCount() {
128cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        if (mVoiceResults != null) {
129cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            return mVoiceResults.size();
130cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        }
13135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
13221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
13321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
13421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
13521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public SuggestItem getItem(int position) {
136cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        if (mVoiceResults != null) {
137cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            return new SuggestItem(mVoiceResults.get(position), null,
138cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    TYPE_SEARCH);
139cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        }
14035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (mMixedResults == null) {
14121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
14221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
143ad373302b1e1a322144f818340fdce60f0eee403John Reck        return mMixedResults.items.get(position);
14421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
14521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
14621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
14721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public long getItemId(int position) {
14821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        return 0;
14921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
15021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
15121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
15221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public View getView(int position, View convertView, ViewGroup parent) {
15321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        final LayoutInflater inflater = LayoutInflater.from(mContext);
15435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        View view = convertView;
15535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (view == null) {
156ad373302b1e1a322144f818340fdce60f0eee403John Reck            view = inflater.inflate(R.layout.suggestion_item, parent, false);
15721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
158ad373302b1e1a322144f818340fdce60f0eee403John Reck        bindView(view, getItem(position));
159ad373302b1e1a322144f818340fdce60f0eee403John Reck        return view;
16021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
16121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
16221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private void bindView(View view, SuggestItem item) {
16321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        // store item for click handling
16421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        view.setTag(item);
16521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
16621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
16721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
16821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        View ic2 = view.findViewById(R.id.icon2);
1697b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb        View div = view.findViewById(R.id.divider);
17021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        tv1.setText(item.title);
171ad373302b1e1a322144f818340fdce60f0eee403John Reck        if (TextUtils.isEmpty(item.url)) {
172ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setVisibility(View.GONE);
173ad373302b1e1a322144f818340fdce60f0eee403John Reck        } else {
174ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setVisibility(View.VISIBLE);
175ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setText(item.url);
176ad373302b1e1a322144f818340fdce60f0eee403John Reck        }
17721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int id = -1;
17821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        switch (item.type) {
17921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SUGGEST:
18021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SEARCH:
18121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_suggest;
18221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
18321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_BOOKMARK:
18421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_bookmark;
18521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
18621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_HISTORY:
18721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_history;
18821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
18921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SUGGEST_URL:
19021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_browser;
19121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
19221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            default:
19321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = -1;
19421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
19521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (id != -1) {
19621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            ic1.setImageDrawable(mContext.getResources().getDrawable(id));
19721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
19821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ic2.setVisibility(((TYPE_SUGGEST == item.type) || (TYPE_SEARCH == item.type))
19921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                ? View.VISIBLE : View.GONE);
2007b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb        div.setVisibility(ic2.getVisibility());
20121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ic2.setOnClickListener(this);
202ad373302b1e1a322144f818340fdce60f0eee403John Reck        view.findViewById(R.id.suggestion).setOnClickListener(this);
20321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
20421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
20535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
20635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
20735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
20835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        protected List<SuggestItem> doInBackground(CharSequence... params) {
20935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            SuggestCursor cursor = new SuggestCursor();
21035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            cursor.runQuery(params[0]);
21135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            List<SuggestItem> results = new ArrayList<SuggestItem>();
21235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            int count = cursor.getCount();
21335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (int i = 0; i < count; i++) {
21435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                results.add(cursor.getItem());
21535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                cursor.moveToNext();
21635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
21735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            cursor.close();
21835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            return results;
21935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
22035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
22135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
22235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        protected void onPostExecute(List<SuggestItem> items) {
22335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mSuggestResults = items;
22435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mMixedResults = buildSuggestionResults();
22535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            notifyDataSetChanged();
22635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mListener.onFilterComplete(mMixedResults.getLineCount());
22735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
22835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
22935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
23035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    SuggestionResults buildSuggestionResults() {
23135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        SuggestionResults mixed = new SuggestionResults();
23235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        List<SuggestItem> filter, suggest;
23335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        synchronized (mResultsLock) {
23435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            filter = mFilterResults;
23535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            suggest = mSuggestResults;
23635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
23735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (filter != null) {
23835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (SuggestItem item : filter) {
23935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                mixed.addResult(item);
24035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
24135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
24235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (suggest != null) {
24335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (SuggestItem item : suggest) {
24435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                mixed.addResult(item);
24535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
24635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
24735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        return mixed;
24835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
24921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
25035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    class SuggestFilter extends Filter {
25121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
25221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
25321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public CharSequence convertResultToString(Object item) {
25421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (item == null) {
25521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return "";
25621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
25721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            SuggestItem sitem = (SuggestItem) item;
25821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (sitem.title != null) {
25921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return sitem.title;
26021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
26121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return sitem.url;
26221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
26321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
26421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
26535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        void startSuggestionsAsync(final CharSequence constraint) {
26635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            new SlowFilterTask().execute(constraint);
26735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
26835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
26921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
27021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        protected FilterResults performFiltering(CharSequence constraint) {
27121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            FilterResults res = new FilterResults();
272cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            if (mVoiceResults == null) {
273cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                if (TextUtils.isEmpty(constraint)) {
274cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    res.count = 0;
275cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    res.values = null;
276cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    return res;
27721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
278cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                startSuggestionsAsync(constraint);
279cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
280cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                if (constraint != null) {
281cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    for (CursorSource sc : mSources) {
282cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                        sc.runQuery(constraint);
283cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    }
284cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    mixResults(filterResults);
285cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                }
286cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                synchronized (mResultsLock) {
287cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    mFilterResults = filterResults;
288cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                }
289cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                SuggestionResults mixed = buildSuggestionResults();
290cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.count = mixed.getLineCount();
291cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.values = mixed;
292cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            } else {
293cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.count = mVoiceResults.size();
294cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.values = mVoiceResults;
29521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
29621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return res;
29721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
29821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
29935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        void mixResults(List<SuggestItem> results) {
300ad373302b1e1a322144f818340fdce60f0eee403John Reck            int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
301ad373302b1e1a322144f818340fdce60f0eee403John Reck            maxLines = (int) Math.ceil(maxLines / 2.0);
30221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            for (int i = 0; i < mSources.size(); i++) {
30321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                CursorSource s = mSources.get(i);
30435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                int n = Math.min(s.getCount(), maxLines);
30535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                maxLines -= n;
3060506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb                boolean more = false;
30721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                for (int j = 0; j < n; j++) {
30835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                    results.add(s.getItem());
30921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    more = s.moveToNext();
31021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
31121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
31221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
31321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
31421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
31521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        protected void publishResults(CharSequence constraint, FilterResults fresults) {
316cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            if (fresults.values instanceof SuggestionResults) {
317cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                mMixedResults = (SuggestionResults) fresults.values;
318cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                mListener.onFilterComplete(fresults.count);
319cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            }
32021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            notifyDataSetChanged();
32121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
32221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
32321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
32421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
32521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
32621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * sorted list of results of a suggestion query
32721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     *
32821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
32921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestionResults {
33021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ArrayList<SuggestItem> items;
33221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        // count per type
33321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int[] counts;
33421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        SuggestionResults() {
33621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            items = new ArrayList<SuggestItem>(24);
33721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // n of types:
33821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            counts = new int[5];
33921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
34021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
34121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int getTypeCount(int type) {
34221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return counts[type];
34321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
34421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
34521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        void addResult(SuggestItem item) {
34621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            int ix = 0;
34721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            while ((ix < items.size()) && (item.type >= items.get(ix).type))
34821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                ix++;
34921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            items.add(ix, item);
35021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            counts[item.type]++;
35121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
35221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
35321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int getLineCount() {
35421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mLandscapeMode) {
355ad373302b1e1a322144f818340fdce60f0eee403John Reck                return Math.min(mLinesLandscape, items.size());
35621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
357ad373302b1e1a322144f818340fdce60f0eee403John Reck                return Math.min(mLinesPortrait, items.size());
35821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
35921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
36021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
36135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
36221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public String toString() {
36321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (items == null) return null;
36421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (items.size() == 0) return "[]";
36521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            StringBuilder sb = new StringBuilder();
36621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            for (int i = 0; i < items.size(); i++) {
36721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                SuggestItem item = items.get(i);
36821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                sb.append(item.type + ": " + item.title);
36921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                if (i < items.size() - 1) {
37021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    sb.append(", ");
37121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
37221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
37321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return sb.toString();
37421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
37521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
37621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
37721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
37821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * data object to hold suggestion values
37921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
38021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestItem {
38121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        String title;
38221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        String url;
38321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int type;
38440f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck        String extra;
38521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
38621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem(String text, String u, int t) {
38721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            title = text;
38821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            url = u;
38921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            type = t;
39021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
39121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
39221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
39321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    abstract class CursorSource {
39421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
39521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        Cursor mCursor;
39621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
39721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        boolean moveToNext() {
39821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return mCursor.moveToNext();
39921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
40021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public abstract void runQuery(CharSequence constraint);
40221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public abstract SuggestItem getItem();
40421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public int getCount() {
40621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return (mCursor != null) ? mCursor.getCount() : 0;
40721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
40821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void close() {
41021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
41121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
41221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
41321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
41421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
41521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
41721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * combined bookmark & history source
41821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
41921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class CombinedCursor extends CursorSource {
42021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
42121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
42221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem getItem() {
42321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if ((mCursor != null) && (!mCursor.isAfterLast())) {
42421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String title = mCursor.getString(1);
42521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String url = mCursor.getString(2);
42621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                boolean isBookmark = (mCursor.getInt(3) == 1);
42721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return new SuggestItem(getTitle(title, url), getUrl(title, url),
42821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
42921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
43021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
43121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
43221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
43321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
43421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void runQuery(CharSequence constraint) {
43521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // constraint != null
43621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
43721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
43821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
43921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String like = constraint + "%";
44021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String[] args = null;
44121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String selection = null;
44221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (like.startsWith("http") || like.startsWith("file")) {
44321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args = new String[1];
44421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[0] = like;
44521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                selection = "url LIKE ?";
44621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
44721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args = new String[5];
44821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[0] = "http://" + like;
44921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[1] = "http://www." + like;
45021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[2] = "https://" + like;
45121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[3] = "https://www." + like;
45221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                // To match against titles.
45321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[4] = like;
45421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                selection = COMBINED_SELECTION;
45521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
45621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
4570506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb            ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
458ad373302b1e1a322144f818340fdce60f0eee403John Reck                    Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
4593dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck            BookmarkUtils.addAccountInfo(mContext, ub);
46021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mCursor =
4610506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb                    mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
46221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            selection,
46321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            (constraint != null) ? args : null,
4643dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck                            BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
46521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            BrowserContract.Combined.VISITS + " DESC, " +
46621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
46721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
46821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.moveToFirst();
46921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
47021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
47121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
47221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        /**
47321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * Provides the title (text line 1) for a browser suggestion, which should be the
47421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * webpage title. If the webpage title is empty, returns the stripped url instead.
47521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         *
47621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * @return the title string to use
47721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         */
47821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        private String getTitle(String title, String url) {
47921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
480fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck                title = UrlUtils.stripUrl(url);
48121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
48221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return title;
48321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
48421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
48521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        /**
48621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * Provides the subtitle (text line 2) for a browser suggestion, which should be the
48721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * webpage url. If the webpage title is empty, then the url should go in the title
48821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * instead, and the subtitle should be empty, so this would return null.
48921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         *
49021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * @return the subtitle string to use, or null if none
49121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         */
49221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        private String getUrl(String title, String url) {
4937d132b17c9dd1a816c68bf8505be2028691aaee3John Reck            if (TextUtils.isEmpty(title)
4947d132b17c9dd1a816c68bf8505be2028691aaee3John Reck                    || TextUtils.getTrimmedLength(title) == 0
4957d132b17c9dd1a816c68bf8505be2028691aaee3John Reck                    || title.equals(url)) {
49621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return null;
49721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
498fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck                return UrlUtils.stripUrl(url);
49921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
50021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
50121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
50221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
50321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestCursor extends CursorSource {
50421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
50521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
50621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem getItem() {
50721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
50821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String title = mCursor.getString(
50921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
51021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String text2 = mCursor.getString(
51121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
51221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String url = mCursor.getString(
51321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
51421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String uri = mCursor.getString(
51521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
51621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
51740f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                SuggestItem item = new SuggestItem(title, url, type);
51840f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                item.extra = mCursor.getString(
51940f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
52040f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                return item;
52121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
52221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
52321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
52421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
52521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
52621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void runQuery(CharSequence constraint) {
52721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
52821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
52921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
53021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (!TextUtils.isEmpty(constraint)) {
53121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
53221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                if (searchEngine != null && searchEngine.supportsSuggestions()) {
53321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
53421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    if (mCursor != null) {
53521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.moveToFirst();
53621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    }
53721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
53821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
53921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor = null;
54021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
54121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
54221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
54321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
54421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
54535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    public void clearCache() {
54635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        mFilterResults = null;
54735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        mSuggestResults = null;
54835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
54935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
55021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb}
551