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