WebCorpus.java revision 96fec862c3d494aebcb4e1d93589a241385a2ba7
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.Log; 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 private static final String TAG = "QSB.WebCorpus"; 40 private static final boolean DBG = false; 41 42 private static final String WEB_CORPUS_NAME = "web"; 43 44 private final SearchSettings mSettings; 45 46 private Source mWebSearchSource; 47 48 private final Source mBrowserSource; 49 50 public WebCorpus(Context context, Config config, SearchSettings settings, Executor executor, 51 Source webSearchSource, Source browserSource) { 52 super(context, config, executor, webSearchSource, browserSource); 53 if (DBG) { 54 Log.d(TAG, "init webSource=" + webSearchSource + "; browser source = " + browserSource); 55 } 56 mSettings = settings; 57 mWebSearchSource = webSearchSource; 58 mBrowserSource = browserSource; 59 } 60 61 protected SearchSettings getSettings() { 62 return mSettings; 63 } 64 65 public void setWebSource(Source web) { 66 if (DBG) Log.d(TAG, "setWebSource(" + web + ")"); 67 mWebSearchSource = web; 68 } 69 70 public CharSequence getLabel() { 71 return getContext().getText(R.string.corpus_label_web); 72 } 73 74 public CharSequence getHint() { 75 // The web corpus uses a drawable hint instead 76 return null; 77 } 78 79 private boolean isUrl(String query) { 80 return Patterns.WEB_URL.matcher(query).matches(); 81 } 82 83 public Intent createSearchIntent(String query, Bundle appData) { 84 if (isUrl(query)) { 85 return createBrowseIntent(query); 86 } else if (mWebSearchSource != null){ 87 return mWebSearchSource.createSearchIntent(query, appData); 88 } else { 89 return null; 90 } 91 } 92 93 private Intent createBrowseIntent(String query) { 94 Intent intent = new Intent(Intent.ACTION_VIEW); 95 intent.addCategory(Intent.CATEGORY_BROWSABLE); 96 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 97 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 98 String url = URLUtil.guessUrl(query); 99 intent.setData(Uri.parse(url)); 100 return intent; 101 } 102 103 public SuggestionData createSearchShortcut(String query) { 104 SuggestionData shortcut = new SuggestionData(mWebSearchSource); 105 if (isUrl(query)) { 106 shortcut.setIntentAction(Intent.ACTION_VIEW); 107 shortcut.setIcon1(String.valueOf(R.drawable.globe)); 108 shortcut.setText1(query); 109 // Set query so that trackball selection works 110 shortcut.setSuggestionQuery(query); 111 shortcut.setIntentData(URLUtil.guessUrl(query)); 112 } else { 113 shortcut.setIntentAction(Intent.ACTION_WEB_SEARCH); 114 shortcut.setIcon1(String.valueOf(R.drawable.magnifying_glass)); 115 shortcut.setText1(query); 116 shortcut.setSuggestionQuery(query); 117 } 118 return shortcut; 119 } 120 121 public Intent createVoiceSearchIntent(Bundle appData) { 122 // TODO in 2-pane mode, mWebSearchSource may be NULL 123 // this functionality should be moved elsewhere. 124 if (mWebSearchSource != null){ 125 return mWebSearchSource.createVoiceSearchIntent(appData); 126 } else { 127 return null; 128 } 129 } 130 131 private int getCorpusIconResource() { 132 return R.drawable.corpus_icon_web; 133 } 134 135 public Drawable getCorpusIcon() { 136 return getContext().getResources().getDrawable(getCorpusIconResource()); 137 } 138 139 public Uri getCorpusIconUri() { 140 return Util.getResourceUri(getContext(), getCorpusIconResource()); 141 } 142 143 public String getName() { 144 return WEB_CORPUS_NAME; 145 } 146 147 @Override 148 public int getQueryThreshold() { 149 return 0; 150 } 151 152 @Override 153 public boolean queryAfterZeroResults() { 154 return true; 155 } 156 157 @Override 158 public boolean voiceSearchEnabled() { 159 return true; 160 } 161 162 public boolean isWebCorpus() { 163 return true; 164 } 165 166 public CharSequence getSettingsDescription() { 167 return getContext().getText(R.string.corpus_description_web); 168 } 169 170 @Override 171 protected List<Source> getSourcesToQuery(String query, boolean onlyCorpus) { 172 ArrayList<Source> sourcesToQuery = new ArrayList<Source>(2); 173 if (getSettings().getShowWebSuggestions()) { 174 if (mWebSearchSource != null) sourcesToQuery.add(mWebSearchSource); 175 } 176 if (mBrowserSource != null && query.length() > 0) { 177 sourcesToQuery.add(mBrowserSource); 178 } 179 if (DBG) Log.d(TAG, "getSourcesToQuery sourcesToQuery=" + sourcesToQuery); 180 return sourcesToQuery; 181 } 182 183 @Override 184 protected Result createResult(String query, ArrayList<SourceResult> results, int latency) { 185 return new WebResult(query, results, latency); 186 } 187 188 protected class WebResult extends Result { 189 190 public WebResult(String query, ArrayList<SourceResult> results, int latency) { 191 super(query, results, latency); 192 } 193 194 @Override 195 public void fill() { 196 SourceResult webSearchResult = null; 197 SourceResult browserResult = null; 198 for (SourceResult result : getResults()) { 199 if (result.getSource().equals(mWebSearchSource)) { 200 webSearchResult = result; 201 } else { 202 browserResult = result; 203 } 204 } 205 if (browserResult != null && browserResult.getCount() > 0) { 206 add(new SuggestionPosition(browserResult, 0)); 207 } 208 if (webSearchResult != null) { 209 int count = webSearchResult.getCount(); 210 for (int i = 0; i < count; i++) { 211 add(new SuggestionPosition(webSearchResult, i)); 212 } 213 } 214 } 215 216 } 217} 218