WebCorpus.java revision bd92263c4f8df5e68e9d806167534e7067a5e5ed
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
32/**
33 * The web search source.
34 */
35public class WebCorpus extends AbstractCorpus {
36
37    private static final boolean DBG = false;
38    private static final String TAG = "QSB.WebCorpus";
39
40    private static final String WEB_CORPUS_NAME = "web";
41
42    private final Context mContext;
43
44    private final Source mWebSearchSource;
45    private final Source mBrowserSource;
46
47    public WebCorpus(Context context, Source webSearchSource, Source browserSource) {
48        mContext = context;
49        mWebSearchSource = webSearchSource;
50        mBrowserSource = browserSource;
51    }
52
53    protected Context getContext() {
54        return mContext;
55    }
56
57    public CharSequence getLabel() {
58        return getContext().getText(R.string.corpus_label_web);
59    }
60
61    public Intent createSearchIntent(String query, Bundle appData) {
62        return createWebIntent(query, appData);
63    }
64
65    public static Intent createWebIntent(String query, Bundle appData) {
66        return Patterns.WEB_URL.matcher(query).matches()
67                ? createBrowseIntent(query)
68                : createWebSearchIntent(query, appData);
69    }
70
71    private static Intent createWebSearchIntent(String query, Bundle appData) {
72        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
73        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74        // We need CLEAR_TOP to avoid reusing an old task that has other activities
75        // on top of the one we want.
76        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
77        intent.putExtra(SearchManager.USER_QUERY, query);
78        intent.putExtra(SearchManager.QUERY, query);
79        if (appData != null) {
80            intent.putExtra(SearchManager.APP_DATA, appData);
81        }
82        // TODO: Include something like this, to let the web search activity
83        // know how this query was started.
84        //intent.putExtra(SearchManager.SEARCH_MODE, SearchManager.MODE_GLOBAL_SEARCH_TYPED_QUERY);
85        return intent;
86    }
87
88    private static Intent createBrowseIntent(String url) {
89        Intent intent = new Intent(Intent.ACTION_VIEW);
90        intent.addCategory(Intent.CATEGORY_BROWSABLE);
91        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
92        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
93        url = URLUtil.guessUrl(url);
94        intent.setData(Uri.parse(url));
95        return intent;
96    }
97
98    public Intent createVoiceSearchIntent(Bundle appData) {
99        return createVoiceWebSearchIntent(appData);
100    }
101
102    public static Intent createVoiceWebSearchIntent(Bundle appData) {
103        Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
104        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
105        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
106                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
107        if (appData != null) {
108            intent.putExtra(SearchManager.APP_DATA, appData);
109        }
110        return intent;
111    }
112
113    public Drawable getCorpusIcon() {
114        return getContext().getResources().getDrawable(R.drawable.corpus_icon_web);
115    }
116
117    public Uri getCorpusIconUri() {
118        int resourceId = R.drawable.corpus_icon_web;
119        return new Uri.Builder()
120                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
121                .authority(getContext().getPackageName())
122                .appendEncodedPath(String.valueOf(resourceId))
123                .build();
124    }
125
126    public String getName() {
127        return WEB_CORPUS_NAME;
128    }
129
130    public int getQueryThreshold() {
131        return 0;
132    }
133
134    public boolean queryAfterZeroResults() {
135        return true;
136    }
137
138    public boolean voiceSearchEnabled() {
139        return true;
140    }
141
142    public boolean isWebCorpus() {
143        return true;
144    }
145
146    public CharSequence getSettingsDescription() {
147        return getContext().getText(R.string.corpus_description_web);
148    }
149
150    public CorpusResult getSuggestions(String query, int queryLimit) {
151        // TODO: Should run web and browser queries in parallel
152        SuggestionCursor webCursor = null;
153        if (SearchSettings.getShowWebSuggestions(getContext())) {
154            try {
155                webCursor = mWebSearchSource.getSuggestions(query, queryLimit);
156            } catch (RuntimeException ex) {
157                Log.e(TAG, "Error querying web search source", ex);
158            }
159        }
160        SuggestionCursor browserCursor = null;
161        try {
162            browserCursor = mBrowserSource.getSuggestions(query, queryLimit);
163        } catch (RuntimeException ex) {
164            Log.e(TAG, "Error querying browser search source", ex);
165        }
166
167        WebResult c = new WebResult(query, webCursor, browserCursor);
168        if (DBG) Log.d(TAG, "Returning " + c.getCount() + " suggestions");
169        return c;
170    }
171
172    private class WebResult extends ListSuggestionCursor implements CorpusResult {
173
174        private SuggestionCursor mWebCursor;
175
176        private SuggestionCursor mBrowserCursor;
177
178        public WebResult(String userQuery, SuggestionCursor webCursor,
179                SuggestionCursor browserCursor) {
180            super(userQuery);
181            mWebCursor = webCursor;
182            mBrowserCursor = browserCursor;
183
184            if (mBrowserCursor != null && mBrowserCursor.getCount() > 0) {
185                if (DBG) Log.d(TAG, "Adding browser suggestion");
186                add(new SuggestionPosition(mBrowserCursor, 0));
187            }
188
189            if (mWebCursor != null) {
190                int count = mWebCursor.getCount();
191                for (int i = 0; i < count; i++) {
192                    if (DBG) Log.d(TAG, "Adding web suggestion");
193                    add(new SuggestionPosition(mWebCursor, i));
194                }
195            }
196        }
197
198        public Corpus getCorpus() {
199            return WebCorpus.this;
200        }
201
202        @Override
203        public void close() {
204            super.close();
205            if (mWebCursor != null) {
206                mWebCursor.close();
207            }
208            if (mBrowserCursor != null) {
209                mBrowserCursor.close();
210            }
211        }
212    }
213}
214