WebCorpus.java revision 5691f9062b2a558ba39c700d65bc522d397ebc75
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 CharSequence getHint() { 71 // The web corpus uses a drawable hint instead 72 return null; 73 } 74 75 private boolean isUrl(String query) { 76 return Patterns.WEB_URL.matcher(query).matches(); 77 } 78 79 public Intent createSearchIntent(String query, Bundle appData) { 80 return isUrl(query)? createBrowseIntent(query) : createWebSearchIntent(query, appData); 81 } 82 83 private static Intent createWebSearchIntent(String query, Bundle appData) { 84 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 85 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 86 // We need CLEAR_TOP to avoid reusing an old task that has other activities 87 // on top of the one we want. 88 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 89 intent.putExtra(SearchManager.USER_QUERY, query); 90 intent.putExtra(SearchManager.QUERY, query); 91 if (appData != null) { 92 intent.putExtra(SearchManager.APP_DATA, appData); 93 } 94 // TODO: Include something like this, to let the web search activity 95 // know how this query was started. 96 //intent.putExtra(SearchManager.SEARCH_MODE, SearchManager.MODE_GLOBAL_SEARCH_TYPED_QUERY); 97 return intent; 98 } 99 100 private static Intent createBrowseIntent(String query) { 101 Intent intent = new Intent(Intent.ACTION_VIEW); 102 intent.addCategory(Intent.CATEGORY_BROWSABLE); 103 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 104 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 105 String url = URLUtil.guessUrl(query); 106 intent.setData(Uri.parse(url)); 107 return intent; 108 } 109 110 public SuggestionData createSearchShortcut(String query) { 111 SuggestionData shortcut = new SuggestionData(mWebSearchSource); 112 if (isUrl(query)) { 113 shortcut.setIntentAction(Intent.ACTION_VIEW); 114 shortcut.setIcon1(String.valueOf(R.drawable.globe)); 115 shortcut.setText1(query); 116 // Set query so that trackball selection works 117 shortcut.setSuggestionQuery(query); 118 shortcut.setIntentData(URLUtil.guessUrl(query)); 119 } else { 120 shortcut.setIntentAction(Intent.ACTION_WEB_SEARCH); 121 shortcut.setIcon1(String.valueOf(R.drawable.magnifying_glass)); 122 shortcut.setText1(query); 123 shortcut.setSuggestionQuery(query); 124 } 125 return shortcut; 126 } 127 128 public Intent createVoiceSearchIntent(Bundle appData) { 129 return createVoiceWebSearchIntent(appData); 130 } 131 132 public static Intent createVoiceWebSearchIntent(Bundle appData) { 133 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); 134 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 135 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 136 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 137 if (appData != null) { 138 intent.putExtra(SearchManager.APP_DATA, appData); 139 } 140 return intent; 141 } 142 143 public Drawable getCorpusIcon() { 144 return getContext().getResources().getDrawable(R.drawable.corpus_icon_web); 145 } 146 147 public Uri getCorpusIconUri() { 148 int resourceId = R.drawable.corpus_icon_web; 149 return new Uri.Builder() 150 .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) 151 .authority(getContext().getPackageName()) 152 .appendEncodedPath(String.valueOf(resourceId)) 153 .build(); 154 } 155 156 public String getName() { 157 return WEB_CORPUS_NAME; 158 } 159 160 public int getQueryThreshold() { 161 return 0; 162 } 163 164 public boolean queryAfterZeroResults() { 165 return true; 166 } 167 168 public boolean voiceSearchEnabled() { 169 return true; 170 } 171 172 public boolean isWebCorpus() { 173 return true; 174 } 175 176 public Collection<Source> getSources() { 177 return mSources; 178 } 179 180 public CharSequence getSettingsDescription() { 181 return getContext().getText(R.string.corpus_description_web); 182 } 183 184 public CorpusResult getSuggestions(String query, int queryLimit) { 185 // TODO: Should run web and browser queries in parallel 186 SuggestionCursor webCursor = null; 187 if (SearchSettings.getShowWebSuggestions(getContext())) { 188 try { 189 webCursor = mWebSearchSource.getSuggestions(query, queryLimit); 190 } catch (RuntimeException ex) { 191 Log.e(TAG, "Error querying web search source", ex); 192 } 193 } 194 SuggestionCursor browserCursor = null; 195 try { 196 browserCursor = mBrowserSource.getSuggestions(query, queryLimit); 197 } catch (RuntimeException ex) { 198 Log.e(TAG, "Error querying browser search source", ex); 199 } 200 201 WebResult c = new WebResult(query, webCursor, browserCursor); 202 if (DBG) Log.d(TAG, "Returning " + c.getCount() + " suggestions"); 203 return c; 204 } 205 206 private class WebResult extends ListSuggestionCursor implements CorpusResult { 207 208 private SuggestionCursor mWebCursor; 209 210 private SuggestionCursor mBrowserCursor; 211 212 public WebResult(String userQuery, SuggestionCursor webCursor, 213 SuggestionCursor browserCursor) { 214 super(userQuery); 215 mWebCursor = webCursor; 216 mBrowserCursor = browserCursor; 217 218 if (mBrowserCursor != null && mBrowserCursor.getCount() > 0) { 219 if (DBG) Log.d(TAG, "Adding browser suggestion"); 220 add(new SuggestionPosition(mBrowserCursor, 0)); 221 } 222 223 if (mWebCursor != null) { 224 int count = mWebCursor.getCount(); 225 for (int i = 0; i < count; i++) { 226 if (DBG) Log.d(TAG, "Adding web suggestion"); 227 add(new SuggestionPosition(mWebCursor, i)); 228 } 229 } 230 } 231 232 public Corpus getCorpus() { 233 return WebCorpus.this; 234 } 235 236 @Override 237 public void close() { 238 super.close(); 239 if (mWebCursor != null) { 240 mWebCursor.close(); 241 } 242 if (mBrowserCursor != null) { 243 mBrowserCursor.close(); 244 } 245 } 246 } 247} 248