WebCorpus.java revision cd1e3ba5f7c3f5242345ff6f674281e3d6366e24
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 return new VoiceSearch(getContext()).createVoiceWebSearchIntent(appData); 105 } 106 107 private int getCorpusIconResource() { 108 return R.drawable.corpus_icon_web; 109 } 110 111 public Drawable getCorpusIcon() { 112 return getContext().getResources().getDrawable(getCorpusIconResource()); 113 } 114 115 public Uri getCorpusIconUri() { 116 return Util.getResourceUri(getContext(), getCorpusIconResource()); 117 } 118 119 public String getName() { 120 return WEB_CORPUS_NAME; 121 } 122 123 @Override 124 public int getQueryThreshold() { 125 return 0; 126 } 127 128 @Override 129 public boolean queryAfterZeroResults() { 130 return true; 131 } 132 133 @Override 134 public boolean voiceSearchEnabled() { 135 return true; 136 } 137 138 public boolean isWebCorpus() { 139 return true; 140 } 141 142 public CharSequence getSettingsDescription() { 143 return getContext().getText(R.string.corpus_description_web); 144 } 145 146 @Override 147 protected List<Source> getSourcesToQuery(String query, boolean onlyCorpus) { 148 ArrayList<Source> sourcesToQuery = new ArrayList<Source>(2); 149 if (mWebSearchSource != null 150 && SearchSettings.getShowWebSuggestions(getContext())) { 151 sourcesToQuery.add(mWebSearchSource); 152 } 153 if (mBrowserSource != null && query.length() > 0) { 154 sourcesToQuery.add(mBrowserSource); 155 } 156 return sourcesToQuery; 157 } 158 159 @Override 160 protected Result createResult(String query, ArrayList<SourceResult> results, int latency) { 161 return new WebResult(query, results, latency); 162 } 163 164 protected class WebResult extends Result { 165 166 public WebResult(String query, ArrayList<SourceResult> results, int latency) { 167 super(query, results, latency); 168 } 169 170 @Override 171 public void fill() { 172 SourceResult webSearchResult = null; 173 SourceResult browserResult = null; 174 for (SourceResult result : getResults()) { 175 if (result.getSource().equals(mWebSearchSource)) { 176 webSearchResult = result; 177 } else { 178 browserResult = result; 179 } 180 } 181 if (browserResult != null && browserResult.getCount() > 0) { 182 add(new SuggestionPosition(browserResult, 0)); 183 } 184 if (webSearchResult != null) { 185 int count = webSearchResult.getCount(); 186 for (int i = 0; i < count; i++) { 187 add(new SuggestionPosition(webSearchResult, i)); 188 } 189 } 190 } 191 192 } 193} 194