WebCorpus.java revision dbdffd8316e75bc2813dbbcbef13d357970e8c84
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.quicksearchbox;
18
19import com.android.common.Patterns;
20
21import android.app.SearchManager;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.graphics.drawable.Drawable;
26import android.net.Uri;
27import android.os.Bundle;
28import android.speech.RecognizerIntent;
29import android.util.Log;
30import android.webkit.URLUtil;
31
32import java.util.ArrayList;
33import java.util.Collection;
34
35/**
36 * The web search source.
37 */
38public class WebCorpus extends AbstractCorpus {
39
40    private static final boolean DBG = false;
41    private static final String TAG = "QSB.WebCorpus";
42
43    private static final String WEB_CORPUS_NAME = "web";
44
45    private final Context mContext;
46
47    private final Source mWebSearchSource;
48    private final Source mBrowserSource;
49
50    private final ArrayList<Source> mSources;
51
52    public WebCorpus(Context context, Source webSearchSource, Source browserSource) {
53        mContext = context;
54        mWebSearchSource = webSearchSource;
55        mBrowserSource = browserSource;
56
57        mSources = new ArrayList<Source>();
58        mSources.add(mWebSearchSource);
59        mSources.add(mBrowserSource);
60    }
61
62    protected Context getContext() {
63        return mContext;
64    }
65
66    public CharSequence getLabel() {
67        return getContext().getText(R.string.corpus_label_web);
68    }
69
70    public Intent createSearchIntent(String query, Bundle appData) {
71        return createWebIntent(query, appData);
72    }
73
74    public static Intent createWebIntent(String query, Bundle appData) {
75        return Patterns.WEB_URL.matcher(query).matches()
76                ? createBrowseIntent(query)
77                : createWebSearchIntent(query, appData);
78    }
79
80    private static Intent createWebSearchIntent(String query, Bundle appData) {
81        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
82        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83        // We need CLEAR_TOP to avoid reusing an old task that has other activities
84        // on top of the one we want.
85        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
86        intent.putExtra(SearchManager.USER_QUERY, query);
87        intent.putExtra(SearchManager.QUERY, query);
88        if (appData != null) {
89            intent.putExtra(SearchManager.APP_DATA, appData);
90        }
91        // TODO: Include something like this, to let the web search activity
92        // know how this query was started.
93        //intent.putExtra(SearchManager.SEARCH_MODE, SearchManager.MODE_GLOBAL_SEARCH_TYPED_QUERY);
94        return intent;
95    }
96
97    private static Intent createBrowseIntent(String url) {
98        Intent intent = new Intent(Intent.ACTION_VIEW);
99        intent.addCategory(Intent.CATEGORY_BROWSABLE);
100        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
101        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
102        url = URLUtil.guessUrl(url);
103        intent.setData(Uri.parse(url));
104        return intent;
105    }
106
107    public Intent createVoiceSearchIntent(Bundle appData) {
108        return createVoiceWebSearchIntent(appData);
109    }
110
111    public static Intent createVoiceWebSearchIntent(Bundle appData) {
112        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
113        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
114        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
115                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
116        if (appData != null) {
117            intent.putExtra(SearchManager.APP_DATA, appData);
118        }
119        return intent;
120    }
121
122    public Drawable getCorpusIcon() {
123        return getContext().getResources().getDrawable(R.drawable.corpus_icon_web);
124    }
125
126    public Uri getCorpusIconUri() {
127        int resourceId = R.drawable.corpus_icon_web;
128        return new Uri.Builder()
129                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
130                .authority(getContext().getPackageName())
131                .appendEncodedPath(String.valueOf(resourceId))
132                .build();
133    }
134
135    public String getName() {
136        return WEB_CORPUS_NAME;
137    }
138
139    public int getQueryThreshold() {
140        return 0;
141    }
142
143    public boolean queryAfterZeroResults() {
144        return true;
145    }
146
147    public boolean voiceSearchEnabled() {
148        return true;
149    }
150
151    public boolean isWebCorpus() {
152        return true;
153    }
154
155    public Collection<Source> getSources() {
156        return mSources;
157    }
158
159    public CharSequence getSettingsDescription() {
160        return getContext().getText(R.string.corpus_description_web);
161    }
162
163    public CorpusResult getSuggestions(String query, int queryLimit) {
164        // TODO: Should run web and browser queries in parallel
165        SuggestionCursor webCursor = null;
166        if (SearchSettings.getShowWebSuggestions(getContext())) {
167            try {
168                webCursor = mWebSearchSource.getSuggestions(query, queryLimit);
169            } catch (RuntimeException ex) {
170                Log.e(TAG, "Error querying web search source", ex);
171            }
172        }
173        SuggestionCursor browserCursor = null;
174        try {
175            browserCursor = mBrowserSource.getSuggestions(query, queryLimit);
176        } catch (RuntimeException ex) {
177            Log.e(TAG, "Error querying browser search source", ex);
178        }
179
180        WebResult c = new WebResult(query, webCursor, browserCursor);
181        if (DBG) Log.d(TAG, "Returning " + c.getCount() + " suggestions");
182        return c;
183    }
184
185    private class WebResult extends ListSuggestionCursor implements CorpusResult {
186
187        private SuggestionCursor mWebCursor;
188
189        private SuggestionCursor mBrowserCursor;
190
191        public WebResult(String userQuery, SuggestionCursor webCursor,
192                SuggestionCursor browserCursor) {
193            super(userQuery);
194            mWebCursor = webCursor;
195            mBrowserCursor = browserCursor;
196
197            if (mBrowserCursor != null && mBrowserCursor.getCount() > 0) {
198                if (DBG) Log.d(TAG, "Adding browser suggestion");
199                add(new SuggestionPosition(mBrowserCursor, 0));
200            }
201
202            if (mWebCursor != null) {
203                int count = mWebCursor.getCount();
204                for (int i = 0; i < count; i++) {
205                    if (DBG) Log.d(TAG, "Adding web suggestion");
206                    add(new SuggestionPosition(mWebCursor, i));
207                }
208            }
209        }
210
211        public Corpus getCorpus() {
212            return WebCorpus.this;
213        }
214
215        @Override
216        public void close() {
217            super.close();
218            if (mWebCursor != null) {
219                mWebCursor.close();
220            }
221            if (mBrowserCursor != null) {
222                mBrowserCursor.close();
223            }
224        }
225    }
226}
227