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.net.Uri;
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    private static final boolean DBG = false;
38
39    protected static final long SECOND_MILLIS = 1000L;
40    protected static final long MINUTE_MILLIS = 60L * SECOND_MILLIS;
41    protected static final long DAY_MILLIS = 86400000L;
42
43    private static final int NUM_PROMOTED_SOURCES = 3;
44    private static final int MAX_RESULTS_PER_SOURCE = 50;
45    private static final long SOURCE_TIMEOUT_MILLIS = 10000;
46
47    private static final int QUERY_THREAD_PRIORITY =
48            Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE;
49
50    private static final long MAX_STAT_AGE_MILLIS = 30 * DAY_MILLIS;
51    private static final int MIN_CLICKS_FOR_SOURCE_RANKING = 3;
52
53    private static final int NUM_WEB_CORPUS_THREADS = 2;
54
55    private static final int LATENCY_LOG_FREQUENCY = 1000;
56
57    private static final long TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS = 100;
58    private static final long PUBLISH_RESULT_DELAY_MILLIS = 200;
59
60    private static final long VOICE_SEARCH_HINT_ACTIVE_PERIOD = 7L * DAY_MILLIS;
61
62    private static final long VOICE_SEARCH_HINT_UPDATE_INTERVAL
63            = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
64
65    private static final long VOICE_SEARCH_HINT_SHOW_PERIOD_MILLIS
66            = AlarmManager.INTERVAL_HOUR * 2;
67
68    private static final long VOICE_SEARCH_HINT_CHANGE_PERIOD = 2L * MINUTE_MILLIS;
69
70    private static final long VOICE_SEARCH_HINT_VISIBLE_PERIOD = 6L * MINUTE_MILLIS;
71
72    private static final int HTTP_CONNECT_TIMEOUT_MILLIS = 4000;
73    private static final int HTTP_READ_TIMEOUT_MILLIS = 4000;
74
75    private static final String USER_AGENT = "Android/1.0";
76
77    private final Context mContext;
78    private HashSet<String> mDefaultCorpora;
79    private HashSet<String> mHiddenCorpora;
80    private HashSet<String> mDefaultCorporaSuggestUris;
81
82    /**
83     * Creates a new config that uses hard-coded default values.
84     */
85    public Config(Context context) {
86        mContext = context;
87    }
88
89    protected Context getContext() {
90        return mContext;
91    }
92
93    /**
94     * Releases any resources used by the configuration object.
95     *
96     * Default implementation does nothing.
97     */
98    public void close() {
99    }
100
101    private HashSet<String> loadResourceStringSet(int res) {
102        HashSet<String> set = new HashSet<String>();
103        String[] items = mContext.getResources().getStringArray(res);
104        for (String item : items) {
105            set.add(item);
106        }
107        return set;
108    }
109
110    /**
111     * The number of promoted sources.
112     */
113    public int getNumPromotedSources() {
114        return NUM_PROMOTED_SOURCES;
115    }
116
117    /**
118     * The number of suggestions visible above the onscreen keyboard.
119     */
120    public int getNumSuggestionsAboveKeyboard() {
121        // Get the list of default corpora from a resource, which allows vendor overlays.
122        return mContext.getResources().getInteger(R.integer.num_suggestions_above_keyboard);
123    }
124
125    /**
126     * The maximum number of suggestions to promote.
127     */
128    public int getMaxPromotedSuggestions() {
129        return mContext.getResources().getInteger(R.integer.max_promoted_suggestions);
130    }
131
132    public int getMaxPromotedResults() {
133        return mContext.getResources().getInteger(R.integer.max_promoted_results);
134    }
135
136    /**
137     * The number of results to ask each source for.
138     */
139    public int getMaxResultsPerSource() {
140        return MAX_RESULTS_PER_SOURCE;
141    }
142
143    /**
144     * The maximum number of shortcuts to show for the web source in All mode.
145     */
146    public int getMaxShortcutsPerWebSource() {
147        return mContext.getResources().getInteger(R.integer.max_shortcuts_per_web_source);
148    }
149
150    /**
151     * The maximum number of shortcuts to show for each non-web source in All mode.
152     */
153    public int getMaxShortcutsPerNonWebSource() {
154        return mContext.getResources().getInteger(R.integer.max_shortcuts_per_non_web_source);
155    }
156
157    /**
158     * Gets the maximum number of shortcuts that will be shown from the given source.
159     */
160    public int getMaxShortcuts(String sourceName) {
161        return getMaxShortcutsPerNonWebSource();
162    }
163
164    /**
165     * The timeout for querying each source, in milliseconds.
166     */
167    public long getSourceTimeoutMillis() {
168        return SOURCE_TIMEOUT_MILLIS;
169    }
170
171    /**
172     * The priority of query threads.
173     *
174     * @return A thread priority, as defined in {@link Process}.
175     */
176    public int getQueryThreadPriority() {
177        return QUERY_THREAD_PRIORITY;
178    }
179
180    /**
181     * The maximum age of log data used for shortcuts.
182     */
183    public long getMaxStatAgeMillis(){
184        return MAX_STAT_AGE_MILLIS;
185    }
186
187    /**
188     * The minimum number of clicks needed to rank a source.
189     */
190    public int getMinClicksForSourceRanking(){
191        return MIN_CLICKS_FOR_SOURCE_RANKING;
192    }
193
194    public int getNumWebCorpusThreads() {
195        return NUM_WEB_CORPUS_THREADS;
196    }
197
198    /**
199     * How often query latency should be logged.
200     *
201     * @return An integer in the range 0-1000. 0 means that no latency events
202     *         should be logged. 1000 means that all latency events should be logged.
203     */
204    public int getLatencyLogFrequency() {
205        return LATENCY_LOG_FREQUENCY;
206    }
207
208    /**
209     * The delay in milliseconds before suggestions are updated while typing.
210     * If a new character is typed before this timeout expires, the timeout is reset.
211     */
212    public long getTypingUpdateSuggestionsDelayMillis() {
213        return TYPING_SUGGESTIONS_UPDATE_DELAY_MILLIS;
214    }
215
216    public boolean allowVoiceSearchHints() {
217        return true;
218    }
219
220    /**
221     * The period of time for which after installing voice search we should consider showing voice
222     * search hints.
223     *
224     * @return The period in milliseconds.
225     */
226    public long getVoiceSearchHintActivePeriod() {
227        return VOICE_SEARCH_HINT_ACTIVE_PERIOD;
228    }
229
230    /**
231     * The time interval at which we should consider whether or not to show some voice search hints.
232     *
233     * @return The period in milliseconds.
234     */
235    public long getVoiceSearchHintUpdatePeriod() {
236        return VOICE_SEARCH_HINT_UPDATE_INTERVAL;
237    }
238
239    /**
240     * The time interval at which, on average, voice search hints are displayed.
241     *
242     * @return The period in milliseconds.
243     */
244    public long getVoiceSearchHintShowPeriod() {
245        return VOICE_SEARCH_HINT_SHOW_PERIOD_MILLIS;
246    }
247
248    /**
249     * The amount of time for which voice search hints are displayed in one go.
250     *
251     * @return The period in milliseconds.
252     */
253    public long getVoiceSearchHintVisibleTime() {
254        return VOICE_SEARCH_HINT_VISIBLE_PERIOD;
255    }
256
257    /**
258     * The period that we change voice search hints at while they're being displayed.
259     *
260     * @return The period in milliseconds.
261     */
262    public long getVoiceSearchHintChangePeriod() {
263        return VOICE_SEARCH_HINT_CHANGE_PERIOD;
264    }
265
266    public boolean showSuggestionsForZeroQuery() {
267        // Get the list of default corpora from a resource, which allows vendor overlays.
268        return mContext.getResources().getBoolean(R.bool.show_zero_query_suggestions);
269    }
270
271    public boolean showShortcutsForZeroQuery() {
272        // Get the list of default corpora from a resource, which allows vendor overlays.
273        return mContext.getResources().getBoolean(R.bool.show_zero_query_shortcuts);
274    }
275
276    public boolean showScrollingSuggestions() {
277        return mContext.getResources().getBoolean(R.bool.show_scrolling_suggestions);
278    }
279
280    public boolean showScrollingResults() {
281        return mContext.getResources().getBoolean(R.bool.show_scrolling_results);
282    }
283
284    public Uri getHelpUrl(String activity) {
285        return null;
286    }
287
288    public int getHttpConnectTimeout() {
289        return HTTP_CONNECT_TIMEOUT_MILLIS;
290    }
291
292    public int getHttpReadTimeout() {
293        return HTTP_READ_TIMEOUT_MILLIS;
294    }
295
296    public String getUserAgent() {
297        return USER_AGENT;
298    }
299}
300