Config.java revision ced9f76b761536341d739e9a243c98a4bf90638c
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.content.Context; 20import android.content.res.Resources; 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 37 private static final long DAY_MILLIS = 86400000L; 38 39 private static final int NUM_SUGGESTIONS_ABOVE_KEYBOARD = 4; 40 private static final int NUM_PROMOTED_SOURCES = 3; 41 private static final int MAX_PROMOTED_SUGGESTIONS = 8; 42 private static final int MAX_RESULTS_PER_SOURCE = 50; 43 private static final long SOURCE_TIMEOUT_MILLIS = 10000; 44 45 private static final int QUERY_THREAD_PRIORITY = 46 Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE; 47 48 private static final long MAX_STAT_AGE_MILLIS = 30 * DAY_MILLIS; 49 private static final int MIN_CLICKS_FOR_SOURCE_RANKING = 3; 50 private static final int MAX_SHORTCUTS_RETURNED = MAX_PROMOTED_SUGGESTIONS; 51 52 private static final int NUM_WEB_CORPUS_THREADS = 2; 53 54 private static final int LATENCY_LOG_FREQUENCY = 1000; 55 56 private static final long TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS = 100; 57 private static final long PUBLISH_RESULT_DELAY_MILLIS = 200; 58 59 private final Context mContext; 60 private HashSet<String> mDefaultCorpora; 61 private HashSet<String> mHiddenCorpora; 62 63 /** 64 * Creates a new config that uses hard-coded default values. 65 */ 66 public Config(Context context) { 67 mContext = context; 68 } 69 70 protected Context getContext() { 71 return mContext; 72 } 73 74 /** 75 * Releases any resources used by the configuration object. 76 * 77 * Default implementation does nothing. 78 */ 79 public void close() { 80 } 81 82 private HashSet<String> loadResourceStringSet(int res) { 83 HashSet<String> defaultCorpora = new HashSet<String>(); 84 try { 85 String[] corpora = mContext.getResources().getStringArray(res); 86 for (String corpus : corpora) { 87 defaultCorpora.add(corpus); 88 } 89 return defaultCorpora; 90 } catch (Resources.NotFoundException ex) { 91 Log.e(TAG, "Could not load resource string set", ex); 92 return defaultCorpora; 93 } 94 } 95 96 /** 97 * Checks if we trust the given source not to be spammy. 98 */ 99 public synchronized boolean isCorpusEnabledByDefault(String corpusName) { 100 if (mDefaultCorpora == null) { 101 mDefaultCorpora = loadResourceStringSet(R.array.default_corpora); 102 } 103 return mDefaultCorpora.contains(corpusName); 104 } 105 106 /** 107 * Checks if the given corpus should be hidden from the corpus selection dialog. 108 */ 109 public synchronized boolean isCorpusHidden(String corpusName) { 110 if (mHiddenCorpora == null) { 111 mHiddenCorpora = loadResourceStringSet(R.array.hidden_corpora); 112 } 113 return mHiddenCorpora.contains(corpusName); 114 } 115 116 /** 117 * The number of promoted sources. 118 */ 119 public int getNumPromotedSources() { 120 return NUM_PROMOTED_SOURCES; 121 } 122 123 /** 124 * The number of suggestions visible above the onscreen keyboard. 125 */ 126 public int getNumSuggestionsAboveKeyboard() { 127 try { 128 // Get the list of default corpora from a resource, which allows vendor overlays. 129 return mContext.getResources().getInteger(R.integer.num_suggestions_above_keyboard); 130 } catch (Resources.NotFoundException ex) { 131 Log.e(TAG, "Could not load num_suggestions_above_keyboard", ex); 132 return NUM_SUGGESTIONS_ABOVE_KEYBOARD; 133 } 134 } 135 136 /** 137 * The maximum number of suggestions to promote. 138 */ 139 public int getMaxPromotedSuggestions() { 140 return MAX_PROMOTED_SUGGESTIONS; 141 } 142 143 /** 144 * The number of results to ask each source for. 145 */ 146 public int getMaxResultsPerSource() { 147 return MAX_RESULTS_PER_SOURCE; 148 } 149 150 /** 151 * The timeout for querying each source, in milliseconds. 152 */ 153 public long getSourceTimeoutMillis() { 154 return SOURCE_TIMEOUT_MILLIS; 155 } 156 157 /** 158 * The priority of query threads. 159 * 160 * @return A thread priority, as defined in {@link Process}. 161 */ 162 public int getQueryThreadPriority() { 163 return QUERY_THREAD_PRIORITY; 164 } 165 166 /** 167 * The maximum age of log data used for shortcuts. 168 */ 169 public long getMaxStatAgeMillis(){ 170 return MAX_STAT_AGE_MILLIS; 171 } 172 173 /** 174 * The minimum number of clicks needed to rank a source. 175 */ 176 public int getMinClicksForSourceRanking(){ 177 return MIN_CLICKS_FOR_SOURCE_RANKING; 178 } 179 180 /** 181 * The maximum number of shortcuts shown. 182 */ 183 public int getMaxShortcutsReturned(){ 184 return MAX_SHORTCUTS_RETURNED; 185 } 186 187 public int getNumWebCorpusThreads() { 188 return NUM_WEB_CORPUS_THREADS; 189 } 190 191 /** 192 * How often query latency should be logged. 193 * 194 * @return An integer in the range 0-1000. 0 means that no latency events 195 * should be logged. 1000 means that all latency events should be logged. 196 */ 197 public int getLatencyLogFrequency() { 198 return LATENCY_LOG_FREQUENCY; 199 } 200 201 /** 202 * The delay in milliseconds before suggestions are updated while typing. 203 * If a new character is typed before this timeout expires, the timeout is reset. 204 */ 205 public long getTypingUpdateSuggestionsDelayMillis() { 206 return TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS; 207 } 208 209 /** 210 * The delay in milliseconds before corpus results are published. 211 * If a new result arrives before this timeout expires, the timeout is reset. 212 */ 213 public long getPublishResultDelayMillis() { 214 return PUBLISH_RESULT_DELAY_MILLIS; 215 } 216 217} 218