SuggestionsAdapter.java revision a005cd7e4091513b189a6573b4fa96d762399c7e
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 */
44bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolbpublic class SuggestionsAdapter extends BaseAdapter implements Filterable,
45bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb        OnClickListener {
4621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
4735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_BOOKMARK = 0;
48dd7d751f480a8554e5b318adaa866517453f3fceJohn Reck    static final int TYPE_HISTORY = 1;
49dd7d751f480a8554e5b318adaa866517453f3fceJohn Reck    static final int TYPE_SUGGEST_URL = 2;
5035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_SEARCH = 3;
5135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    static final int TYPE_SUGGEST = 4;
52bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb    static final int TYPE_VOICE_SEARCH = 5;
5321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
5421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private static final String[] COMBINED_PROJECTION =
5521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
5621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
5721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
5821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private static final String COMBINED_SELECTION =
5921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
6021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
6121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    Context mContext;
6221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    Filter mFilter;
6335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    SuggestionResults mMixedResults;
6435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    List<SuggestItem> mSuggestResults, mFilterResults;
6521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    List<CursorSource> mSources;
6621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    boolean mLandscapeMode;
6721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    CompletionListener mListener;
6821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    int mLinesPortrait;
6921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    int mLinesLandscape;
7035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    Object mResultsLock = new Object();
71cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    List<String> mVoiceResults;
721605bef4e1f99805a801308f97ade622b907dc7aJohn Reck    boolean mReverseResults;
73117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck    boolean mIncognitoMode;
7421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    interface CompletionListener {
7621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
7721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void onSearch(String txt);
7821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
79bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb        public void onSelect(String txt, int type, String extraData);
8021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
8121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
8221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
8321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public SuggestionsAdapter(Context ctx, CompletionListener listener) {
8421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mContext = ctx;
8521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mListener = listener;
8621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLinesPortrait = mContext.getResources().
8721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                getInteger(R.integer.max_suggest_lines_portrait);
8821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLinesLandscape = mContext.getResources().
8921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                getInteger(R.integer.max_suggest_lines_landscape);
9021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mFilter = new SuggestFilter();
9121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        addSource(new CombinedCursor());
9221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
9321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
94cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    void setVoiceResults(List<String> voiceResults) {
95cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        mVoiceResults = voiceResults;
96a50c446037eecaa2dcd83dab133bd9dc390ede69Michael Kolb        notifyDataSetChanged();
97cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb    }
98cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb
9921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void setLandscapeMode(boolean mode) {
10021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mLandscapeMode = mode;
10135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        notifyDataSetChanged();
10221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
10321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
10421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void addSource(CursorSource c) {
10521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (mSources == null) {
10621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mSources = new ArrayList<CursorSource>(5);
10721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
10821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        mSources.add(c);
10921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
11021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
11121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
11221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public void onClick(View v) {
113ad373302b1e1a322144f818340fdce60f0eee403John Reck        SuggestItem item = (SuggestItem) ((View) v.getParent()).getTag();
11421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (R.id.icon2 == v.getId()) {
11521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // replace input field text with suggestion text
11621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mListener.onSearch(item.title);
11721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        } else {
118bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb            mListener.onSelect(
119bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb                    (TextUtils.isEmpty(item.url)? item.title : item.url),
120bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb                    item.type, item.extra);
12121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
12221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
12321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
12421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
12521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public Filter getFilter() {
12621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        return mFilter;
12721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
12821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
12921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
13021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public int getCount() {
131cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        if (mVoiceResults != null) {
132cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            return mVoiceResults.size();
133cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        }
13435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        return (mMixedResults == null) ? 0 : mMixedResults.getLineCount();
13521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
13621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
13721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
13821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public SuggestItem getItem(int position) {
1391605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        if (mReverseResults) {
1401605bef4e1f99805a801308f97ade622b907dc7aJohn Reck            position = (getCount() - 1) - position;
1411605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        }
142cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        if (mVoiceResults != null) {
143bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb            SuggestItem item = new SuggestItem(mVoiceResults.get(position),
144bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb                    null, TYPE_VOICE_SEARCH);
145bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb            item.extra = Integer.toString(position);
146bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb            return item;
147cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb        }
14835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (mMixedResults == null) {
14921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
15021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
151ad373302b1e1a322144f818340fdce60f0eee403John Reck        return mMixedResults.items.get(position);
15221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
15321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
1541605bef4e1f99805a801308f97ade622b907dc7aJohn Reck    public void setReverseResults(boolean reverse) {
1551605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        mReverseResults = reverse;
1561605bef4e1f99805a801308f97ade622b907dc7aJohn Reck    }
1571605bef4e1f99805a801308f97ade622b907dc7aJohn Reck
15821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
15921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public long getItemId(int position) {
1601605bef4e1f99805a801308f97ade622b907dc7aJohn Reck        return position;
16121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
16221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
16321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    @Override
16421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    public View getView(int position, View convertView, ViewGroup parent) {
16521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        final LayoutInflater inflater = LayoutInflater.from(mContext);
16635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        View view = convertView;
16735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (view == null) {
168ad373302b1e1a322144f818340fdce60f0eee403John Reck            view = inflater.inflate(R.layout.suggestion_item, parent, false);
16921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
170ad373302b1e1a322144f818340fdce60f0eee403John Reck        bindView(view, getItem(position));
171ad373302b1e1a322144f818340fdce60f0eee403John Reck        return view;
17221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
17321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
17421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    private void bindView(View view, SuggestItem item) {
17521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        // store item for click handling
17621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        view.setTag(item);
17721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        TextView tv1 = (TextView) view.findViewById(android.R.id.text1);
17821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        TextView tv2 = (TextView) view.findViewById(android.R.id.text2);
17921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ImageView ic1 = (ImageView) view.findViewById(R.id.icon1);
18021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        View ic2 = view.findViewById(R.id.icon2);
1817b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb        View div = view.findViewById(R.id.divider);
18221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        tv1.setText(item.title);
183ad373302b1e1a322144f818340fdce60f0eee403John Reck        if (TextUtils.isEmpty(item.url)) {
184ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setVisibility(View.GONE);
185ad373302b1e1a322144f818340fdce60f0eee403John Reck        } else {
186ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setVisibility(View.VISIBLE);
187ad373302b1e1a322144f818340fdce60f0eee403John Reck            tv2.setText(item.url);
188ad373302b1e1a322144f818340fdce60f0eee403John Reck        }
18921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int id = -1;
19021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        switch (item.type) {
19121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SUGGEST:
19221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SEARCH:
193bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb            case TYPE_VOICE_SEARCH:
19421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_suggest;
19521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
19621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_BOOKMARK:
19721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_bookmark;
19821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
19921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_HISTORY:
20021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_history;
20121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
20221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            case TYPE_SUGGEST_URL:
20321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = R.drawable.ic_search_category_browser;
20421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                break;
20521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            default:
20621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                id = -1;
20721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
20821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        if (id != -1) {
20921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            ic1.setImageDrawable(mContext.getResources().getDrawable(id));
21021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
211bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb        ic2.setVisibility(((TYPE_SUGGEST == item.type)
212bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb                || (TYPE_SEARCH == item.type)
213bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb                || (TYPE_VOICE_SEARCH == item.type))
21421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                ? View.VISIBLE : View.GONE);
2157b20ddd4f03d59cca8fdd4aee790784421570aabMichael Kolb        div.setVisibility(ic2.getVisibility());
21621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ic2.setOnClickListener(this);
217ad373302b1e1a322144f818340fdce60f0eee403John Reck        view.findViewById(R.id.suggestion).setOnClickListener(this);
21821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
21921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
22035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    class SlowFilterTask extends AsyncTask<CharSequence, Void, List<SuggestItem>> {
22135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
22235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
22335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        protected List<SuggestItem> doInBackground(CharSequence... params) {
22435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            SuggestCursor cursor = new SuggestCursor();
22535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            cursor.runQuery(params[0]);
22635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            List<SuggestItem> results = new ArrayList<SuggestItem>();
22735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            int count = cursor.getCount();
22835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (int i = 0; i < count; i++) {
22935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                results.add(cursor.getItem());
23035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                cursor.moveToNext();
23135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
23235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            cursor.close();
23335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            return results;
23435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
23535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
23635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
23735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        protected void onPostExecute(List<SuggestItem> items) {
23835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mSuggestResults = items;
23935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            mMixedResults = buildSuggestionResults();
24035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            notifyDataSetChanged();
24135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
24235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
24335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
24435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    SuggestionResults buildSuggestionResults() {
24535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        SuggestionResults mixed = new SuggestionResults();
24635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        List<SuggestItem> filter, suggest;
24735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        synchronized (mResultsLock) {
24835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            filter = mFilterResults;
24935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            suggest = mSuggestResults;
25035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
25135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (filter != null) {
25235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (SuggestItem item : filter) {
25335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                mixed.addResult(item);
25435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
25535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
25635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        if (suggest != null) {
25735defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            for (SuggestItem item : suggest) {
25835defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                mixed.addResult(item);
25935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck            }
26035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
26135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        return mixed;
26235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
26321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
26435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    class SuggestFilter extends Filter {
26521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
26621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
26721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public CharSequence convertResultToString(Object item) {
26821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (item == null) {
26921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return "";
27021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
27121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            SuggestItem sitem = (SuggestItem) item;
27221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (sitem.title != null) {
27321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return sitem.title;
27421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
27521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return sitem.url;
27621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
27721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
27821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
27935defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        void startSuggestionsAsync(final CharSequence constraint) {
280117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck            if (!mIncognitoMode) {
281117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck                new SlowFilterTask().execute(constraint);
282117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck            }
28335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        }
28435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
28521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
28621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        protected FilterResults performFiltering(CharSequence constraint) {
28721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            FilterResults res = new FilterResults();
288cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            if (mVoiceResults == null) {
289cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                if (TextUtils.isEmpty(constraint)) {
290cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    res.count = 0;
291cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    res.values = null;
292cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    return res;
29321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
294cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                startSuggestionsAsync(constraint);
295cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                List<SuggestItem> filterResults = new ArrayList<SuggestItem>();
296cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                if (constraint != null) {
297cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    for (CursorSource sc : mSources) {
298cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                        sc.runQuery(constraint);
299cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    }
300cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    mixResults(filterResults);
301cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                }
302cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                synchronized (mResultsLock) {
303cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                    mFilterResults = filterResults;
304cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                }
305cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                SuggestionResults mixed = buildSuggestionResults();
306cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.count = mixed.getLineCount();
307cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.values = mixed;
308cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            } else {
309cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.count = mVoiceResults.size();
310cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                res.values = mVoiceResults;
31121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
31221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return res;
31321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
31421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
31535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        void mixResults(List<SuggestItem> results) {
316ad373302b1e1a322144f818340fdce60f0eee403John Reck            int maxLines = mLandscapeMode ? mLinesLandscape : mLinesPortrait;
317ad373302b1e1a322144f818340fdce60f0eee403John Reck            maxLines = (int) Math.ceil(maxLines / 2.0);
31821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            for (int i = 0; i < mSources.size(); i++) {
31921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                CursorSource s = mSources.get(i);
32035defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                int n = Math.min(s.getCount(), maxLines);
32135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                maxLines -= n;
3220506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb                boolean more = false;
32321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                for (int j = 0; j < n; j++) {
32435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck                    results.add(s.getItem());
32521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    more = s.moveToNext();
32621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
32721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
32821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
32921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
33121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        protected void publishResults(CharSequence constraint, FilterResults fresults) {
332cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            if (fresults.values instanceof SuggestionResults) {
333cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb                mMixedResults = (SuggestionResults) fresults.values;
334a005cd7e4091513b189a6573b4fa96d762399c7eJohn Reck                notifyDataSetChanged();
335cfa3af5c59abb38c895416a80ef16da0ec1b5287Michael Kolb            }
33621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
33721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
33821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
33921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
34021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
34121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * sorted list of results of a suggestion query
34221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     *
34321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
34421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestionResults {
34521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
34621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        ArrayList<SuggestItem> items;
34721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        // count per type
34821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int[] counts;
34921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
35021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        SuggestionResults() {
35121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            items = new ArrayList<SuggestItem>(24);
35221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // n of types:
35321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            counts = new int[5];
35421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
35521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
35621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int getTypeCount(int type) {
35721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return counts[type];
35821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
35921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
36021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        void addResult(SuggestItem item) {
36121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            int ix = 0;
36221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            while ((ix < items.size()) && (item.type >= items.get(ix).type))
36321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                ix++;
36421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            items.add(ix, item);
36521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            counts[item.type]++;
36621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
36721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
36821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int getLineCount() {
36921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mLandscapeMode) {
370ad373302b1e1a322144f818340fdce60f0eee403John Reck                return Math.min(mLinesLandscape, items.size());
37121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
372ad373302b1e1a322144f818340fdce60f0eee403John Reck                return Math.min(mLinesPortrait, items.size());
37321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
37421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
37521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
37635defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        @Override
37721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public String toString() {
37821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (items == null) return null;
37921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (items.size() == 0) return "[]";
38021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            StringBuilder sb = new StringBuilder();
38121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            for (int i = 0; i < items.size(); i++) {
38221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                SuggestItem item = items.get(i);
38321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                sb.append(item.type + ": " + item.title);
38421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                if (i < items.size() - 1) {
38521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    sb.append(", ");
38621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
38721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
38821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return sb.toString();
38921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
39021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
39121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
39221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
39321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * data object to hold suggestion values
39421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
39521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestItem {
39621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        String title;
39721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        String url;
39821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        int type;
39940f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck        String extra;
40021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem(String text, String u, int t) {
40221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            title = text;
40321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            url = u;
40421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            type = t;
40521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
406bd2dd64dda88ca8a7c2b148d88c5a44e73e1c331Michael Kolb
40721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
40821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
40921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    abstract class CursorSource {
41021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        Cursor mCursor;
41221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        boolean moveToNext() {
41421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return mCursor.moveToNext();
41521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
41621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public abstract void runQuery(CharSequence constraint);
41821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
41921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public abstract SuggestItem getItem();
42021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
42121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public int getCount() {
42221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return (mCursor != null) ? mCursor.getCount() : 0;
42321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
42421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
42521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void close() {
42621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
42721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
42821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
42921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
43021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
43121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
43221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    /**
43321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     * combined bookmark & history source
43421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb     */
43521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class CombinedCursor extends CursorSource {
43621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
43721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
43821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem getItem() {
43921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if ((mCursor != null) && (!mCursor.isAfterLast())) {
44021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String title = mCursor.getString(1);
44121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String url = mCursor.getString(2);
44221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                boolean isBookmark = (mCursor.getInt(3) == 1);
44321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return new SuggestItem(getTitle(title, url), getUrl(title, url),
44421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        isBookmark ? TYPE_BOOKMARK : TYPE_HISTORY);
44521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
44621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
44721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
44821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
44921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
45021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void runQuery(CharSequence constraint) {
45121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            // constraint != null
45221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
45321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
45421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
45521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String like = constraint + "%";
45621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String[] args = null;
45721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            String selection = null;
45821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (like.startsWith("http") || like.startsWith("file")) {
45921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args = new String[1];
46021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[0] = like;
46121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                selection = "url LIKE ?";
46221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
46321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args = new String[5];
46421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[0] = "http://" + like;
46521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[1] = "http://www." + like;
46621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[2] = "https://" + like;
46721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[3] = "https://www." + like;
46821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                // To match against titles.
46921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                args[4] = like;
47021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                selection = COMBINED_SELECTION;
47121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
47221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
4730506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb            ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
474ad373302b1e1a322144f818340fdce60f0eee403John Reck                    Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
4753dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck            BookmarkUtils.addAccountInfo(mContext, ub);
47621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            mCursor =
4770506f2dd9239e23099244e101f7d32d5fc189668Michael Kolb                    mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
47821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            selection,
47921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            (constraint != null) ? args : null,
4803dff1cedc22620ab56fbd39f3703f19bb552ca34John Reck                            BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
48121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            BrowserContract.Combined.VISITS + " DESC, " +
48221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                            BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
48321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
48421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.moveToFirst();
48521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
48621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
48721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
48821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        /**
48921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * Provides the title (text line 1) for a browser suggestion, which should be the
49021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * webpage title. If the webpage title is empty, returns the stripped url instead.
49121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         *
49221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * @return the title string to use
49321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         */
49421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        private String getTitle(String title, String url) {
49521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
496fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck                title = UrlUtils.stripUrl(url);
49721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
49821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return title;
49921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
50021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
50121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        /**
50221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * Provides the subtitle (text line 2) for a browser suggestion, which should be the
50321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * webpage url. If the webpage title is empty, then the url should go in the title
50421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * instead, and the subtitle should be empty, so this would return null.
50521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         *
50621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         * @return the subtitle string to use, or null if none
50721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb         */
50821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        private String getUrl(String title, String url) {
5097d132b17c9dd1a816c68bf8505be2028691aaee3John Reck            if (TextUtils.isEmpty(title)
5107d132b17c9dd1a816c68bf8505be2028691aaee3John Reck                    || TextUtils.getTrimmedLength(title) == 0
5117d132b17c9dd1a816c68bf8505be2028691aaee3John Reck                    || title.equals(url)) {
51221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                return null;
51321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
514fb3017ffd8aa3f2342380270cf468e3a68914e69John Reck                return UrlUtils.stripUrl(url);
51521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
51621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
51721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
51821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
51921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    class SuggestCursor extends CursorSource {
52021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
52121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
52221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public SuggestItem getItem() {
52321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
52421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String title = mCursor.getString(
52521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
52621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String text2 = mCursor.getString(
52721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2));
52821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String url = mCursor.getString(
52921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL));
53021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                String uri = mCursor.getString(
53121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_DATA));
53221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                int type = (TextUtils.isEmpty(url)) ? TYPE_SUGGEST : TYPE_SUGGEST_URL;
53340f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                SuggestItem item = new SuggestItem(title, url, type);
53440f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                item.extra = mCursor.getString(
53540f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                        mCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA));
53640f720ecfd4ef7ebb657f0fc1906a9982b3bafbdJohn Reck                return item;
53721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
53821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            return null;
53921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
54021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
54121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        @Override
54221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        public void runQuery(CharSequence constraint) {
54321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (mCursor != null) {
54421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor.close();
54521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
54621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            if (!TextUtils.isEmpty(constraint)) {
54721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                SearchEngine searchEngine = BrowserSettings.getInstance().getSearchEngine();
54821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                if (searchEngine != null && searchEngine.supportsSuggestions()) {
54921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    mCursor = searchEngine.getSuggestions(mContext, constraint.toString());
55021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    if (mCursor != null) {
55121ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                        mCursor.moveToFirst();
55221ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                    }
55321ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                }
55421ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            } else {
55521ce4d295802db811873b46e7821abfa0540dab2Michael Kolb                mCursor = null;
55621ce4d295802db811873b46e7821abfa0540dab2Michael Kolb            }
55721ce4d295802db811873b46e7821abfa0540dab2Michael Kolb        }
55821ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
55921ce4d295802db811873b46e7821abfa0540dab2Michael Kolb    }
56021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb
56135defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    public void clearCache() {
56235defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        mFilterResults = null;
56335defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck        mSuggestResults = null;
56435defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck    }
56535defffe93d70e9d5b37ff550968debdcd5d3d8bJohn Reck
566117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck    public void setIncognitoMode(boolean incognito) {
567117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck        mIncognitoMode = incognito;
568117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck        clearCache();
569117f07d08e3e25e3c920a1dabe31dcd7643bacb2John Reck    }
57021ce4d295802db811873b46e7821abfa0540dab2Michael Kolb}
571