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