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