Config.java revision 13b4f2dc4b339790c2b9b0220be47c8e77fd61ea
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.app.AlarmManager;
20import android.content.Context;
21import android.content.res.Resources;
22import android.os.Process;
23import android.util.Log;
24
25import 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 */
34public 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        try {
157            return mContext.getResources().getInteger(R.integer.max_promoted_suggestions);
158        } catch (Resources.NotFoundException ex) {
159            Log.e(TAG, "Could not load max_promoted_suggestions", ex);
160            return MAX_PROMOTED_SUGGESTIONS;
161        }
162    }
163
164    /**
165     * The number of results to ask each source for.
166     */
167    public int getMaxResultsPerSource() {
168        return MAX_RESULTS_PER_SOURCE;
169    }
170
171    /**
172     * The maximum number of shortcuts to show for the web source in All mode.
173     */
174    public int getMaxShortcutsPerWebSource() {
175        return MAX_SHORTCUTS_PER_WEB_SOURCE;
176    }
177
178    /**
179     * The maximum number of shortcuts to show for each non-web source in All mode.
180     */
181    public int getMaxShortcutsPerNonWebSource() {
182        return MAX_SHORTCUTS_PER_NON_WEB_SOURCE;
183    }
184
185    /**
186     * The timeout for querying each source, in milliseconds.
187     */
188    public long getSourceTimeoutMillis() {
189        return SOURCE_TIMEOUT_MILLIS;
190    }
191
192    /**
193     * The priority of query threads.
194     *
195     * @return A thread priority, as defined in {@link Process}.
196     */
197    public int getQueryThreadPriority() {
198        return QUERY_THREAD_PRIORITY;
199    }
200
201    /**
202     * The maximum age of log data used for shortcuts.
203     */
204    public long getMaxStatAgeMillis(){
205        return MAX_STAT_AGE_MILLIS;
206    }
207
208    /**
209     * The minimum number of clicks needed to rank a source.
210     */
211    public int getMinClicksForSourceRanking(){
212        return MIN_CLICKS_FOR_SOURCE_RANKING;
213    }
214
215    public int getNumWebCorpusThreads() {
216        return NUM_WEB_CORPUS_THREADS;
217    }
218
219    /**
220     * How often query latency should be logged.
221     *
222     * @return An integer in the range 0-1000. 0 means that no latency events
223     *         should be logged. 1000 means that all latency events should be logged.
224     */
225    public int getLatencyLogFrequency() {
226        return LATENCY_LOG_FREQUENCY;
227    }
228
229    /**
230     * The delay in milliseconds before suggestions are updated while typing.
231     * If a new character is typed before this timeout expires, the timeout is reset.
232     */
233    public long getTypingUpdateSuggestionsDelayMillis() {
234        return TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS;
235    }
236
237    /**
238     * The delay in milliseconds before corpus results are published.
239     * If a new result arrives before this timeout expires, the timeout is reset.
240     */
241    public long getPublishResultDelayMillis() {
242        return PUBLISH_RESULT_DELAY_MILLIS;
243    }
244
245    public boolean allowVoiceSearchHints() {
246        return true;
247    }
248
249    /**
250     * The period of time for which after installing voice search we should consider showing voice
251     * search hints.
252     *
253     * @return The period in milliseconds.
254     */
255    public long getVoiceSearchHintActivePeriod() {
256        return VOICE_SEARCH_HINT_ACTIVE_PERIOD;
257    }
258
259    /**
260     * The time interval at which we should consider whether or not to show some voice search hints.
261     *
262     * @return The period in milliseconds.
263     */
264    public long getVoiceSearchHintUpdatePeriod() {
265        return VOICE_SEARCH_HINT_UPDATE_INTERVAL;
266    }
267
268    /**
269     * The time interval at which, on average, voice search hints are displayed.
270     *
271     * @return The period in milliseconds.
272     */
273    public long getVoiceSearchHintShowPeriod() {
274        return VOICE_SEARCH_HINT_SHOW_PERIOD_MILLIS;
275    }
276
277    /**
278     * The amount of time for which voice search hints are displayed in one go.
279     *
280     * @return The period in milliseconds.
281     */
282    public long getVoiceSearchHintVisibleTime() {
283        return VOICE_SEARCH_HINT_VISIBLE_PERIOD;
284    }
285
286    /**
287     * The period that we change voice search hints at while they're being displayed.
288     *
289     * @return The period in milliseconds.
290     */
291    public long getVoiceSearchHintChangePeriod() {
292        return VOICE_SEARCH_HINT_CHANGE_PERIOD;
293    }
294
295}
296