Config.java revision 5eee22a4ec6fc45deb9706ba535039ccae51b90a
1/* 2 * Copyright (C) 2009 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 android.app.AlarmManager; 20import android.content.Context; 21import android.os.Process; 22import android.util.Log; 23 24import java.util.HashSet; 25 26/** 27 * Provides values for configurable parameters in all of QSB. 28 * 29 * All the methods in this class return fixed default values. Subclasses may 30 * make these values server-side settable. 31 * 32 */ 33public class Config { 34 35 private static final String TAG = "QSB.Config"; 36 private static final boolean DBG = false; 37 38 protected static final long SECOND_MILLIS = 1000L; 39 protected static final long MINUTE_MILLIS = 60L * SECOND_MILLIS; 40 protected static final long DAY_MILLIS = 86400000L; 41 42 private static final int NUM_PROMOTED_SOURCES = 3; 43 private static final int MAX_PROMOTED_SUGGESTIONS = 8; 44 private static final int MAX_RESULTS_PER_SOURCE = 50; 45 private static final long SOURCE_TIMEOUT_MILLIS = 10000; 46 47 private static final int QUERY_THREAD_PRIORITY = 48 Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE; 49 50 private static final long MAX_STAT_AGE_MILLIS = 30 * DAY_MILLIS; 51 private static final int MIN_CLICKS_FOR_SOURCE_RANKING = 3; 52 53 private static final int NUM_WEB_CORPUS_THREADS = 2; 54 55 private static final int LATENCY_LOG_FREQUENCY = 1000; 56 57 private static final long TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS = 100; 58 private static final long PUBLISH_RESULT_DELAY_MILLIS = 200; 59 60 private static final long VOICE_SEARCH_HINT_ACTIVE_PERIOD = 7L * DAY_MILLIS; 61 62 private static final long VOICE_SEARCH_HINT_UPDATE_INTERVAL 63 = AlarmManager.INTERVAL_FIFTEEN_MINUTES; 64 65 private static final long VOICE_SEARCH_HINT_SHOW_PERIOD_MILLIS 66 = AlarmManager.INTERVAL_HOUR * 2; 67 68 private static final long VOICE_SEARCH_HINT_CHANGE_PERIOD = 2L * MINUTE_MILLIS; 69 70 private static final long VOICE_SEARCH_HINT_VISIBLE_PERIOD = 6L * MINUTE_MILLIS; 71 72 private final Context mContext; 73 private HashSet<String> mDefaultCorpora; 74 private HashSet<String> mHiddenCorpora; 75 private HashSet<String> mDefaultCorporaSuggestUris; 76 77 /** 78 * Creates a new config that uses hard-coded default values. 79 */ 80 public Config(Context context) { 81 mContext = context; 82 } 83 84 protected Context getContext() { 85 return mContext; 86 } 87 88 /** 89 * Releases any resources used by the configuration object. 90 * 91 * Default implementation does nothing. 92 */ 93 public void close() { 94 } 95 96 private HashSet<String> loadResourceStringSet(int res) { 97 HashSet<String> defaultCorpora = new HashSet<String>(); 98 String[] corpora = mContext.getResources().getStringArray(res); 99 for (String corpus : corpora) { 100 if (DBG) Log.d(TAG, "Default corpus: " + corpus); 101 defaultCorpora.add(corpus); 102 } 103 return defaultCorpora; 104 } 105 106 /** 107 * Checks if we trust the given source not to be spammy. 108 */ 109 public synchronized boolean isCorpusEnabledByDefault(Corpus corpus) { 110 if (DBG) Log.d(TAG, "isCorpusEnabledByDefault(" + corpus.getName() + ")"); 111 if (mDefaultCorpora == null) { 112 mDefaultCorpora = loadResourceStringSet(R.array.default_corpora); 113 } 114 if (mDefaultCorpora.contains(corpus.getName())) { 115 if (DBG) Log.d(TAG, "Corpus " + corpus.getName() + " IS default"); 116 return true; 117 } 118 119 if (mDefaultCorporaSuggestUris == null) { 120 mDefaultCorporaSuggestUris = loadResourceStringSet( 121 R.array.default_corpora_suggest_uris); 122 } 123 124 for (Source s : corpus.getSources()) { 125 String uri = s.getSuggestUri(); 126 if (DBG) Log.d(TAG, "Suggest URI for " + corpus.getName() + ": " + uri); 127 if (mDefaultCorporaSuggestUris.contains(uri)) { 128 if (DBG) Log.d(TAG, "Corpus " + corpus.getName() + " IS default"); 129 return true; 130 } 131 } 132 if (DBG) Log.d(TAG, "Corpus " + corpus.getName() + " is NOT default"); 133 return false; 134 } 135 136 /** 137 * Checks if the given corpus should be hidden from the corpus selection dialog. 138 */ 139 public synchronized boolean isCorpusHidden(String corpusName) { 140 if (mHiddenCorpora == null) { 141 mHiddenCorpora = loadResourceStringSet(R.array.hidden_corpora); 142 } 143 return mHiddenCorpora.contains(corpusName); 144 } 145 146 /** 147 * The number of promoted sources. 148 */ 149 public int getNumPromotedSources() { 150 return NUM_PROMOTED_SOURCES; 151 } 152 153 /** 154 * The number of suggestions visible above the onscreen keyboard. 155 */ 156 public int getNumSuggestionsAboveKeyboard() { 157 // Get the list of default corpora from a resource, which allows vendor overlays. 158 return mContext.getResources().getInteger(R.integer.num_suggestions_above_keyboard); 159 } 160 161 /** 162 * The maximum number of suggestions to promote. 163 */ 164 public int getMaxPromotedSuggestions() { 165 return mContext.getResources().getInteger(R.integer.max_promoted_suggestions); 166 } 167 168 public int getMaxPromotedResults() { 169 return mContext.getResources().getInteger(R.integer.max_promoted_results); 170 } 171 172 /** 173 * The number of results to ask each source for. 174 */ 175 public int getMaxResultsPerSource() { 176 return MAX_RESULTS_PER_SOURCE; 177 } 178 179 /** 180 * The maximum number of shortcuts to show for the web source in All mode. 181 */ 182 public int getMaxShortcutsPerWebSource() { 183 return mContext.getResources().getInteger(R.integer.max_shortcuts_per_web_source); 184 } 185 186 /** 187 * The maximum number of shortcuts to show for each non-web source in All mode. 188 */ 189 public int getMaxShortcutsPerNonWebSource() { 190 return mContext.getResources().getInteger(R.integer.max_shortcuts_per_non_web_source); 191 } 192 193 /** 194 * Gets the maximum number of shortcuts that will be shown from the given source. 195 */ 196 public int getMaxShortcuts(String sourceName) { 197 return getMaxShortcutsPerNonWebSource(); 198 } 199 200 /** 201 * The timeout for querying each source, in milliseconds. 202 */ 203 public long getSourceTimeoutMillis() { 204 return SOURCE_TIMEOUT_MILLIS; 205 } 206 207 /** 208 * The priority of query threads. 209 * 210 * @return A thread priority, as defined in {@link Process}. 211 */ 212 public int getQueryThreadPriority() { 213 return QUERY_THREAD_PRIORITY; 214 } 215 216 /** 217 * The maximum age of log data used for shortcuts. 218 */ 219 public long getMaxStatAgeMillis(){ 220 return MAX_STAT_AGE_MILLIS; 221 } 222 223 /** 224 * The minimum number of clicks needed to rank a source. 225 */ 226 public int getMinClicksForSourceRanking(){ 227 return MIN_CLICKS_FOR_SOURCE_RANKING; 228 } 229 230 public int getNumWebCorpusThreads() { 231 return NUM_WEB_CORPUS_THREADS; 232 } 233 234 /** 235 * How often query latency should be logged. 236 * 237 * @return An integer in the range 0-1000. 0 means that no latency events 238 * should be logged. 1000 means that all latency events should be logged. 239 */ 240 public int getLatencyLogFrequency() { 241 return LATENCY_LOG_FREQUENCY; 242 } 243 244 /** 245 * The delay in milliseconds before suggestions are updated while typing. 246 * If a new character is typed before this timeout expires, the timeout is reset. 247 */ 248 public long getTypingUpdateSuggestionsDelayMillis() { 249 return TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS; 250 } 251 252 /** 253 * The delay in milliseconds before corpus results are published. 254 * If a new result arrives before this timeout expires, the timeout is reset. 255 */ 256 public long getPublishResultDelayMillis() { 257 return PUBLISH_RESULT_DELAY_MILLIS; 258 } 259 260 public boolean allowVoiceSearchHints() { 261 return true; 262 } 263 264 /** 265 * The period of time for which after installing voice search we should consider showing voice 266 * search hints. 267 * 268 * @return The period in milliseconds. 269 */ 270 public long getVoiceSearchHintActivePeriod() { 271 return VOICE_SEARCH_HINT_ACTIVE_PERIOD; 272 } 273 274 /** 275 * The time interval at which we should consider whether or not to show some voice search hints. 276 * 277 * @return The period in milliseconds. 278 */ 279 public long getVoiceSearchHintUpdatePeriod() { 280 return VOICE_SEARCH_HINT_UPDATE_INTERVAL; 281 } 282 283 /** 284 * The time interval at which, on average, voice search hints are displayed. 285 * 286 * @return The period in milliseconds. 287 */ 288 public long getVoiceSearchHintShowPeriod() { 289 return VOICE_SEARCH_HINT_SHOW_PERIOD_MILLIS; 290 } 291 292 /** 293 * The amount of time for which voice search hints are displayed in one go. 294 * 295 * @return The period in milliseconds. 296 */ 297 public long getVoiceSearchHintVisibleTime() { 298 return VOICE_SEARCH_HINT_VISIBLE_PERIOD; 299 } 300 301 /** 302 * The period that we change voice search hints at while they're being displayed. 303 * 304 * @return The period in milliseconds. 305 */ 306 public long getVoiceSearchHintChangePeriod() { 307 return VOICE_SEARCH_HINT_CHANGE_PERIOD; 308 } 309 310 public boolean showSuggestionsForZeroQuery() { 311 // Get the list of default corpora from a resource, which allows vendor overlays. 312 return mContext.getResources().getBoolean(R.bool.show_zero_query_suggestions); 313 } 314 315 public boolean showShortcutsForZeroQuery() { 316 // Get the list of default corpora from a resource, which allows vendor overlays. 317 return mContext.getResources().getBoolean(R.bool.show_zero_query_shortcuts); 318 } 319 320 public boolean showScrollingSuggestions() { 321 return mContext.getResources().getBoolean(R.bool.show_scrolling_suggestions); 322 } 323 324 public boolean showScrollingResults() { 325 return mContext.getResources().getBoolean(R.bool.show_scrolling_results); 326 } 327 328 public String getHelpUrl() { 329 return getContext().getString(R.string.help_url); 330 } 331 332} 333