Config.java revision 120040ef31d79c1d69138b13ca7f256841f3298e
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 static final boolean DISMISS_KEYBOARD_ON_SCROLL = true;
76
77    private final Context mContext;
78    private HashSet<String> mDefaultCorpora;
79    private HashSet<String> mHiddenCorpora;
80
81    /**
82     * Creates a new config that uses hard-coded default values.
83     */
84    public Config(Context context) {
85        mContext = context;
86    }
87
88    protected Context getContext() {
89        return mContext;
90    }
91
92    /**
93     * Releases any resources used by the configuration object.
94     *
95     * Default implementation does nothing.
96     */
97    public void close() {
98    }
99
100    private HashSet<String> loadResourceStringSet(int res) {
101        HashSet<String> defaultCorpora = new HashSet<String>();
102        try {
103            String[] corpora = mContext.getResources().getStringArray(res);
104            for (String corpus : corpora) {
105                defaultCorpora.add(corpus);
106            }
107            return defaultCorpora;
108        } catch (Resources.NotFoundException ex) {
109            Log.e(TAG, "Could not load resource string set", ex);
110            return defaultCorpora;
111        }
112    }
113
114    /**
115     * Checks if we trust the given source not to be spammy.
116     */
117    public synchronized boolean isCorpusEnabledByDefault(String corpusName) {
118        if (mDefaultCorpora == null) {
119            mDefaultCorpora = loadResourceStringSet(R.array.default_corpora);
120        }
121        return mDefaultCorpora.contains(corpusName);
122    }
123
124    /**
125     * Checks if the given corpus should be hidden from the corpus selection dialog.
126     */
127    public synchronized boolean isCorpusHidden(String corpusName) {
128        if (mHiddenCorpora == null) {
129            mHiddenCorpora = loadResourceStringSet(R.array.hidden_corpora);
130        }
131        return mHiddenCorpora.contains(corpusName);
132    }
133
134    /**
135     * The number of promoted sources.
136     */
137    public int getNumPromotedSources() {
138        return NUM_PROMOTED_SOURCES;
139    }
140
141    /**
142     * The number of suggestions visible above the onscreen keyboard.
143     */
144    public int getNumSuggestionsAboveKeyboard() {
145        try {
146            // Get the list of default corpora from a resource, which allows vendor overlays.
147            return mContext.getResources().getInteger(R.integer.num_suggestions_above_keyboard);
148        } catch (Resources.NotFoundException ex) {
149            Log.e(TAG, "Could not load num_suggestions_above_keyboard", ex);
150            return NUM_SUGGESTIONS_ABOVE_KEYBOARD;
151        }
152    }
153
154    /**
155     * The maximum number of suggestions to promote.
156     */
157    public int getMaxPromotedSuggestions() {
158        try {
159            return mContext.getResources().getInteger(R.integer.max_promoted_suggestions);
160        } catch (Resources.NotFoundException ex) {
161            Log.e(TAG, "Could not load max_promoted_suggestions", ex);
162            return MAX_PROMOTED_SUGGESTIONS;
163        }
164    }
165
166    /**
167     * The number of results to ask each source for.
168     */
169    public int getMaxResultsPerSource() {
170        return MAX_RESULTS_PER_SOURCE;
171    }
172
173    /**
174     * The maximum number of shortcuts to show for the web source in All mode.
175     */
176    public int getMaxShortcutsPerWebSource() {
177        return MAX_SHORTCUTS_PER_WEB_SOURCE;
178    }
179
180    /**
181     * The maximum number of shortcuts to show for each non-web source in All mode.
182     */
183    public int getMaxShortcutsPerNonWebSource() {
184        return MAX_SHORTCUTS_PER_NON_WEB_SOURCE;
185    }
186
187    /**
188     * The timeout for querying each source, in milliseconds.
189     */
190    public long getSourceTimeoutMillis() {
191        return SOURCE_TIMEOUT_MILLIS;
192    }
193
194    /**
195     * The priority of query threads.
196     *
197     * @return A thread priority, as defined in {@link Process}.
198     */
199    public int getQueryThreadPriority() {
200        return QUERY_THREAD_PRIORITY;
201    }
202
203    /**
204     * The maximum age of log data used for shortcuts.
205     */
206    public long getMaxStatAgeMillis(){
207        return MAX_STAT_AGE_MILLIS;
208    }
209
210    /**
211     * The minimum number of clicks needed to rank a source.
212     */
213    public int getMinClicksForSourceRanking(){
214        return MIN_CLICKS_FOR_SOURCE_RANKING;
215    }
216
217    public int getNumWebCorpusThreads() {
218        return NUM_WEB_CORPUS_THREADS;
219    }
220
221    /**
222     * How often query latency should be logged.
223     *
224     * @return An integer in the range 0-1000. 0 means that no latency events
225     *         should be logged. 1000 means that all latency events should be logged.
226     */
227    public int getLatencyLogFrequency() {
228        return LATENCY_LOG_FREQUENCY;
229    }
230
231    /**
232     * The delay in milliseconds before suggestions are updated while typing.
233     * If a new character is typed before this timeout expires, the timeout is reset.
234     */
235    public long getTypingUpdateSuggestionsDelayMillis() {
236        return TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS;
237    }
238
239    /**
240     * The delay in milliseconds before corpus results are published.
241     * If a new result arrives before this timeout expires, the timeout is reset.
242     */
243    public long getPublishResultDelayMillis() {
244        return PUBLISH_RESULT_DELAY_MILLIS;
245    }
246
247    public boolean allowVoiceSearchHints() {
248        return true;
249    }
250
251    /**
252     * The period of time for which after installing voice search we should consider showing voice
253     * search hints.
254     *
255     * @return The period in milliseconds.
256     */
257    public long getVoiceSearchHintActivePeriod() {
258        return VOICE_SEARCH_HINT_ACTIVE_PERIOD;
259    }
260
261    /**
262     * The time interval at which we should consider whether or not to show some voice search hints.
263     *
264     * @return The period in milliseconds.
265     */
266    public long getVoiceSearchHintUpdatePeriod() {
267        return VOICE_SEARCH_HINT_UPDATE_INTERVAL;
268    }
269
270    /**
271     * The time interval at which, on average, voice search hints are displayed.
272     *
273     * @return The period in milliseconds.
274     */
275    public long getVoiceSearchHintShowPeriod() {
276        return VOICE_SEARCH_HINT_SHOW_PERIOD_MILLIS;
277    }
278
279    /**
280     * The amount of time for which voice search hints are displayed in one go.
281     *
282     * @return The period in milliseconds.
283     */
284    public long getVoiceSearchHintVisibleTime() {
285        return VOICE_SEARCH_HINT_VISIBLE_PERIOD;
286    }
287
288    /**
289     * The period that we change voice search hints at while they're being displayed.
290     *
291     * @return The period in milliseconds.
292     */
293    public long getVoiceSearchHintChangePeriod() {
294        return VOICE_SEARCH_HINT_CHANGE_PERIOD;
295    }
296
297    public boolean isKeyboardDismissedOnScroll() {
298        try {
299            // Get the keyboard dismiss policy
300            return mContext.getResources().getBoolean(R.bool.dismiss_keyboard_on_scroll);
301        } catch (Resources.NotFoundException ex) {
302            Log.e(TAG, "Could not load dismiss_keyboard_on_scroll", ex);
303            return DISMISS_KEYBOARD_ON_SCROLL;
304        }
305    }
306}
307