Config.java revision 3e44ff1f2a204db3f479698cf0b3eab3d451dec2
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 MAX_PROMOTED_SUGGESTIONS = 8;
40    private static final int MAX_RESULTS_PER_SOURCE = 50;
41    private static final long SOURCE_TIMEOUT_MILLIS = 10000;
42
43    private static final int QUERY_THREAD_MAX_POOL_SIZE = 4;
44    private static final long QUERY_THREAD_KEEPALIVE_MILLIS = 30000;
45    private static final int QUERY_THREAD_PRIORITY =
46            Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE;
47
48    private static final long SOURCE_RESULT_PUBLISH_DELAY_MILLIS = 200;
49
50    private static final long MAX_STAT_AGE_MILLIS = 7 * DAY_MILLIS;
51    private static final long MAX_SOURCE_EVENT_AGE_MILLIS = 30 * DAY_MILLIS;
52    private static final int MIN_IMPRESSIONS_FOR_SOURCE_RANKING = 5;
53    private static final int MIN_CLICKS_FOR_SOURCE_RANKING = 3;
54    private static final int MAX_SHORTCUTS_RETURNED = 12;
55
56    private static final long THREAD_START_DELAY_MILLIS = 100;
57
58    private Context mContext;
59    private HashSet<String> mTrustedPackages;
60
61    /**
62     * Creates a new config that uses hard-coded default values.
63     */
64    public Config(Context context) {
65        mContext = context;
66    }
67
68    private HashSet<String> loadTrustedPackages() {
69        HashSet<String> trusted = new HashSet<String>();
70
71        try {
72            // Get the list of trusted packages from a resource, which allows vendor overlays.
73            String[] trustedPackages = mContext.getResources().getStringArray(
74                    R.array.trusted_search_providers);
75            if (trustedPackages == null) {
76                Log.w(TAG, "Could not load list of trusted search providers, trusting none");
77                return trusted;
78            }
79            for (String trustedPackage : trustedPackages) {
80                trusted.add(trustedPackage);
81            }
82            return trusted;
83        } catch (Resources.NotFoundException ex) {
84            Log.w(TAG, "Could not load list of trusted search providers, trusting none");
85            return trusted;
86        }
87    }
88
89    /**
90     * Checks if we trust the given source not to be spammy.
91     */
92    public synchronized boolean isTrustedSource(String packageName) {
93        if (mTrustedPackages == null) {
94            mTrustedPackages = loadTrustedPackages();
95        }
96        return mTrustedPackages.contains(packageName);
97    }
98
99    /**
100     * The maximum number of suggestions to promote.
101     */
102    public int getMaxPromotedSuggestions() {
103        return MAX_PROMOTED_SUGGESTIONS;
104    }
105
106    /**
107     * The number of results to ask each source for.
108     */
109    public int getMaxResultsPerSource() {
110        return MAX_RESULTS_PER_SOURCE;
111    }
112
113    /**
114     * The timeout for querying each source, in milliseconds.
115     */
116    public long getSourceTimeoutMillis() {
117        return SOURCE_TIMEOUT_MILLIS;
118    }
119
120    /**
121     * The maximum thread pool size for the query thread pool.
122     */
123    public int getQueryThreadMaxPoolSize(){
124        return QUERY_THREAD_MAX_POOL_SIZE;
125    }
126
127    /**
128     * The keep-alive time for the query thread pool, in millisseconds.
129     */
130    public long getQueryThreadKeepAliveMillis() {
131        return QUERY_THREAD_KEEPALIVE_MILLIS;
132    }
133
134    /**
135     * The priority of query threads.
136     *
137     * @return A thread priority, as defined in {@link Process}.
138     */
139    public int getQueryThreadPriority() {
140        return QUERY_THREAD_PRIORITY;
141    }
142
143    /**
144     * The maximum delay with which a new source result is published, in milliseconds.
145     */
146    public long getSourceResultPublishDelayMillis() {
147        return SOURCE_RESULT_PUBLISH_DELAY_MILLIS;
148    }
149
150
151    public long getMaxStatAgeMillis(){
152        return MAX_STAT_AGE_MILLIS;
153    }
154
155    public long getMaxSourceEventAgeMillis(){
156        return MAX_SOURCE_EVENT_AGE_MILLIS;
157    }
158
159    public int getMinImpressionsForSourceRanking(){
160        return MIN_IMPRESSIONS_FOR_SOURCE_RANKING;
161    }
162
163    public int getMinClicksForSourceRanking(){
164        return MIN_CLICKS_FOR_SOURCE_RANKING;
165    }
166
167    public int getMaxShortcutsReturned(){
168        return MAX_SHORTCUTS_RETURNED;
169    }
170
171    public long getThreadStartDelayMillis() {
172        return THREAD_START_DELAY_MILLIS;
173    }
174
175}
176