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