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