Config.java revision 72f9b08ce84d0e13daf2d1c112d4e6d1d3ada045
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 = 12;
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 final Context mContext;
60    private HashSet<String> mDefaultCorpora;
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> loadDefaultCorpora() {
82        HashSet<String> defaultCorpora = new HashSet<String>();
83        try {
84            // Get the list of default corpora from a resource, which allows vendor overlays.
85            String[] corpora = mContext.getResources().getStringArray(R.array.default_corpora);
86            for (String corpus : corpora) {
87                defaultCorpora.add(corpus);
88            }
89            return defaultCorpora;
90        } catch (Resources.NotFoundException ex) {
91            Log.e(TAG, "Could not load default corpora", ex);
92            return defaultCorpora;
93        }
94    }
95
96    /**
97     * Checks if we trust the given source not to be spammy.
98     */
99    public synchronized boolean isCorpusEnabledByDefault(String corpusName) {
100        if (mDefaultCorpora == null) {
101            mDefaultCorpora = loadDefaultCorpora();
102        }
103        return mDefaultCorpora.contains(corpusName);
104    }
105
106    /**
107     * The number of promoted sources.
108     */
109    public int getNumPromotedSources() {
110        return NUM_PROMOTED_SOURCES;
111    }
112
113    /**
114     * The maximum number of suggestions to promote.
115     */
116    public int getMaxPromotedSuggestions() {
117        return MAX_PROMOTED_SUGGESTIONS;
118    }
119
120    /**
121     * The number of results to ask each source for.
122     */
123    public int getMaxResultsPerSource() {
124        return MAX_RESULTS_PER_SOURCE;
125    }
126
127    /**
128     * The timeout for querying each source, in milliseconds.
129     */
130    public long getSourceTimeoutMillis() {
131        return SOURCE_TIMEOUT_MILLIS;
132    }
133
134    /**
135     * The maximum thread pool size for the query thread pool.
136     */
137    public int getQueryThreadMaxPoolSize(){
138        return QUERY_THREAD_MAX_POOL_SIZE;
139    }
140
141    /**
142     * The keep-alive time for the query thread pool, in millisseconds.
143     */
144    public long getQueryThreadKeepAliveMillis() {
145        return QUERY_THREAD_KEEPALIVE_MILLIS;
146    }
147
148    /**
149     * The priority of query threads.
150     *
151     * @return A thread priority, as defined in {@link Process}.
152     */
153    public int getQueryThreadPriority() {
154        return QUERY_THREAD_PRIORITY;
155    }
156
157    /**
158     * The maximum age of log data used for shortcuts.
159     */
160    public long getMaxStatAgeMillis(){
161        return MAX_STAT_AGE_MILLIS;
162    }
163
164    /**
165     * The maximum age of log data used for source ranking.
166     */
167    public long getMaxSourceEventAgeMillis(){
168        return MAX_SOURCE_EVENT_AGE_MILLIS;
169    }
170
171    /**
172     * The minimum number of impressions needed to rank a source.
173     */
174    public int getMinImpressionsForSourceRanking(){
175        return MIN_IMPRESSIONS_FOR_SOURCE_RANKING;
176    }
177
178    /**
179     * The minimum number of clicks needed to rank a source.
180     */
181    public int getMinClicksForSourceRanking(){
182        return MIN_CLICKS_FOR_SOURCE_RANKING;
183    }
184
185    /**
186     * The maximum number of shortcuts shown.
187     */
188    public int getMaxShortcutsReturned(){
189        return MAX_SHORTCUTS_RETURNED;
190    }
191
192    /**
193     * The maximum time to delay starting a search query thread after the user types a character.
194     */
195    public long getThreadStartDelayMillis() {
196        return THREAD_START_DELAY_MILLIS;
197    }
198
199    public int getNumWebCorpusThreads() {
200        return NUM_WEB_CORPUS_THREADS;
201    }
202
203}
204