WebCorpus.java revision 81a0897ff9685f3313c58294bf7973700c468b2b
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
19
20import com.android.quicksearchbox.util.Util;
21
22import android.app.SearchManager;
23import android.content.Context;
24import android.content.Intent;
25import android.graphics.drawable.Drawable;
26import android.net.Uri;
27import android.os.Bundle;
28import android.util.Patterns;
29import android.webkit.URLUtil;
30
31import java.util.ArrayList;
32import java.util.List;
33import java.util.concurrent.Executor;
34
35/**
36 * The web search source.
37 */
38public class WebCorpus extends MultiSourceCorpus {
39
40    private static final String WEB_CORPUS_NAME = "web";
41
42    private final Source mWebSearchSource;
43
44    private final Source mBrowserSource;
45
46    public WebCorpus(Context context, Executor executor,
47            Source webSearchSource, Source browserSource) {
48        super(context, executor, webSearchSource, browserSource);
49        mWebSearchSource = webSearchSource;
50        mBrowserSource = browserSource;
51    }
52
53    public CharSequence getLabel() {
54        return getContext().getText(R.string.corpus_label_web);
55    }
56
57    public CharSequence getHint() {
58        // The web corpus uses a drawable hint instead
59        return null;
60    }
61
62    private boolean isUrl(String query) {
63       return Patterns.WEB_URL.matcher(query).matches();
64    }
65
66    public Intent createSearchIntent(String query, Bundle appData) {
67        return isUrl(query)? createBrowseIntent(query) : createWebSearchIntent(query, appData);
68    }
69
70    private static Intent createWebSearchIntent(String query, Bundle appData) {
71        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
72        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
73        // We need CLEAR_TOP to avoid reusing an old task that has other activities
74        // on top of the one we want.
75        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
76        intent.putExtra(SearchManager.USER_QUERY, query);
77        intent.putExtra(SearchManager.QUERY, query);
78        if (appData != null) {
79            intent.putExtra(SearchManager.APP_DATA, appData);
80        }
81        // TODO: Include something like this, to let the web search activity
82        // know how this query was started.
83        //intent.putExtra(SearchManager.SEARCH_MODE, SearchManager.MODE_GLOBAL_SEARCH_TYPED_QUERY);
84        return intent;
85    }
86
87    private static Intent createBrowseIntent(String query) {
88        Intent intent = new Intent(Intent.ACTION_VIEW);
89        intent.addCategory(Intent.CATEGORY_BROWSABLE);
90        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
91        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
92        String url = URLUtil.guessUrl(query);
93        intent.setData(Uri.parse(url));
94        return intent;
95    }
96
97    public SuggestionData createSearchShortcut(String query) {
98        SuggestionData shortcut = new SuggestionData(mWebSearchSource);
99        if (isUrl(query)) {
100            shortcut.setIntentAction(Intent.ACTION_VIEW);
101            shortcut.setIcon1(String.valueOf(R.drawable.globe));
102            shortcut.setText1(query);
103            // Set query so that trackball selection works
104            shortcut.setSuggestionQuery(query);
105            shortcut.setIntentData(URLUtil.guessUrl(query));
106        } else {
107            shortcut.setIntentAction(Intent.ACTION_WEB_SEARCH);
108            shortcut.setIcon1(String.valueOf(R.drawable.magnifying_glass));
109            shortcut.setText1(query);
110            shortcut.setSuggestionQuery(query);
111        }
112        return shortcut;
113    }
114
115    public Intent createVoiceSearchIntent(Bundle appData) {
116        return new VoiceSearch(getContext()).createVoiceWebSearchIntent(appData);
117    }
118
119    private int getCorpusIconResource() {
120        return R.drawable.corpus_icon_web;
121    }
122
123    public Drawable getCorpusIcon() {
124        return getContext().getResources().getDrawable(getCorpusIconResource());
125    }
126
127    public Uri getCorpusIconUri() {
128        return Util.getResourceUri(getContext(), getCorpusIconResource());
129    }
130
131    public String getName() {
132        return WEB_CORPUS_NAME;
133    }
134
135    public int getQueryThreshold() {
136        return 0;
137    }
138
139    public boolean queryAfterZeroResults() {
140        return true;
141    }
142
143    public boolean voiceSearchEnabled() {
144        return true;
145    }
146
147    public boolean isWebCorpus() {
148        return true;
149    }
150
151    public CharSequence getSettingsDescription() {
152        return getContext().getText(R.string.corpus_description_web);
153    }
154
155    @Override
156    protected List<Source> getSourcesToQuery(String query) {
157        ArrayList<Source> sourcesToQuery = new ArrayList<Source>(2);
158        if (mWebSearchSource != null
159                && SearchSettings.getShowWebSuggestions(getContext())) {
160            sourcesToQuery.add(mWebSearchSource);
161        }
162        if (mBrowserSource != null && query.length() > 0) {
163            sourcesToQuery.add(mBrowserSource);
164        }
165        return sourcesToQuery;
166    }
167
168    @Override
169    protected Result createResult(String query, ArrayList<SourceResult> results, int latency) {
170        return new WebResult(query, results, latency);
171    }
172
173    protected class WebResult extends Result {
174
175        public WebResult(String query, ArrayList<SourceResult> results, int latency) {
176            super(query, results, latency);
177        }
178
179        @Override
180        public void fill() {
181            SourceResult webSearchResult = null;
182            SourceResult browserResult = null;
183            for (SourceResult result : getResults()) {
184                if (result.getSource().equals(mWebSearchSource)) {
185                    webSearchResult = result;
186                } else {
187                    browserResult = result;
188                }
189            }
190            if (browserResult != null && browserResult.getCount() > 0) {
191                add(new SuggestionPosition(browserResult, 0));
192            }
193            if (webSearchResult != null) {
194                int count = webSearchResult.getCount();
195                for (int i = 0; i < count; i++) {
196                    add(new SuggestionPosition(webSearchResult, i));
197                }
198            }
199        }
200
201    }
202}
203