WebCorpus.java revision 0be2eb803011b1ef8c2c8277c330fe5ad975316b
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 if (isUrl(query)) { 68 return createBrowseIntent(query); 69 } else if (mWebSearchSource != null){ 70 return mWebSearchSource.createSearchIntent(query, appData); 71 } else { 72 return null; 73 } 74 } 75 76 private Intent createBrowseIntent(String query) { 77 Intent intent = new Intent(Intent.ACTION_VIEW); 78 intent.addCategory(Intent.CATEGORY_BROWSABLE); 79 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 80 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 81 String url = URLUtil.guessUrl(query); 82 intent.setData(Uri.parse(url)); 83 return intent; 84 } 85 86 public SuggestionData createSearchShortcut(String query) { 87 SuggestionData shortcut = new SuggestionData(mWebSearchSource); 88 if (isUrl(query)) { 89 shortcut.setIntentAction(Intent.ACTION_VIEW); 90 shortcut.setIcon1(String.valueOf(R.drawable.globe)); 91 shortcut.setText1(query); 92 // Set query so that trackball selection works 93 shortcut.setSuggestionQuery(query); 94 shortcut.setIntentData(URLUtil.guessUrl(query)); 95 } else { 96 shortcut.setIntentAction(Intent.ACTION_WEB_SEARCH); 97 shortcut.setIcon1(String.valueOf(R.drawable.magnifying_glass)); 98 shortcut.setText1(query); 99 shortcut.setSuggestionQuery(query); 100 } 101 return shortcut; 102 } 103 104 public Intent createVoiceSearchIntent(Bundle appData) { 105 return new VoiceSearch(getContext()).createVoiceWebSearchIntent(appData); 106 } 107 108 private int getCorpusIconResource() { 109 return R.drawable.corpus_icon_web; 110 } 111 112 public Drawable getCorpusIcon() { 113 return getContext().getResources().getDrawable(getCorpusIconResource()); 114 } 115 116 public Uri getCorpusIconUri() { 117 return Util.getResourceUri(getContext(), getCorpusIconResource()); 118 } 119 120 public String getName() { 121 return WEB_CORPUS_NAME; 122 } 123 124 public int getQueryThreshold() { 125 return 0; 126 } 127 128 public boolean queryAfterZeroResults() { 129 return true; 130 } 131 132 public boolean voiceSearchEnabled() { 133 return true; 134 } 135 136 public boolean isWebCorpus() { 137 return true; 138 } 139 140 public CharSequence getSettingsDescription() { 141 return getContext().getText(R.string.corpus_description_web); 142 } 143 144 @Override 145 protected List<Source> getSourcesToQuery(String query) { 146 ArrayList<Source> sourcesToQuery = new ArrayList<Source>(2); 147 if (mWebSearchSource != null 148 && SearchSettings.getShowWebSuggestions(getContext())) { 149 sourcesToQuery.add(mWebSearchSource); 150 } 151 if (mBrowserSource != null && query.length() > 0) { 152 sourcesToQuery.add(mBrowserSource); 153 } 154 return sourcesToQuery; 155 } 156 157 @Override 158 protected Result createResult(String query, ArrayList<SourceResult> results, int latency) { 159 return new WebResult(query, results, latency); 160 } 161 162 protected class WebResult extends Result { 163 164 public WebResult(String query, ArrayList<SourceResult> results, int latency) { 165 super(query, results, latency); 166 } 167 168 @Override 169 public void fill() { 170 SourceResult webSearchResult = null; 171 SourceResult browserResult = null; 172 for (SourceResult result : getResults()) { 173 if (result.getSource().equals(mWebSearchSource)) { 174 webSearchResult = result; 175 } else { 176 browserResult = result; 177 } 178 } 179 if (browserResult != null && browserResult.getCount() > 0) { 180 add(new SuggestionPosition(browserResult, 0)); 181 } 182 if (webSearchResult != null) { 183 int count = webSearchResult.getCount(); 184 for (int i = 0; i < count; i++) { 185 add(new SuggestionPosition(webSearchResult, i)); 186 } 187 } 188 } 189 190 } 191} 192