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