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