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