SearchSettings.java revision bd92263c4f8df5e68e9d806167534e7067a5e5ed
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.AlertDialog;
20import android.app.Dialog;
21import android.app.SearchManager;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.SharedPreferences;
26import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.database.ContentObserver;
29import android.os.Bundle;
30import android.preference.CheckBoxPreference;
31import android.preference.Preference;
32import android.preference.PreferenceActivity;
33import android.preference.PreferenceGroup;
34import android.preference.PreferenceScreen;
35import android.preference.Preference.OnPreferenceChangeListener;
36import android.preference.Preference.OnPreferenceClickListener;
37import android.provider.Settings;
38import android.provider.Settings.System;
39import android.util.Log;
40
41import java.util.List;
42
43/**
44 * Activity for setting global search preferences. Changes to search preferences trigger a broadcast
45 * intent that causes all SuggestionSources objects to be updated.
46 */
47public class SearchSettings extends PreferenceActivity
48        implements OnPreferenceClickListener, OnPreferenceChangeListener {
49
50    private static final boolean DBG = false;
51    private static final String TAG = "SearchSettings";
52
53    // Name of the preferences file used to store search preference
54    public static final String PREFERENCES_NAME = "SearchSettings";
55
56    // Only used to find the preferences after inflating
57    private static final String CLEAR_SHORTCUTS_PREF = "clear_shortcuts";
58    private static final String SEARCH_ENGINE_SETTINGS_PREF = "search_engine_settings";
59    private static final String SEARCH_CORPORA_PREF = "search_corpora";
60
61    // Preifx of per-corpus enable preference
62    private static final String CORPUS_ENABLED_PREF_PREFIX = "enable_corpus_";
63
64    // References to the top-level preference objects
65    private Preference mClearShortcutsPreference;
66    private PreferenceScreen mSearchEngineSettingsPreference;
67    private PreferenceGroup mSourcePreferences;
68
69    // Dialog ids
70    private static final int CLEAR_SHORTCUTS_CONFIRM_DIALOG = 0;
71
72    @Override
73    protected void onCreate(Bundle savedInstanceState) {
74        super.onCreate(savedInstanceState);
75
76        getPreferenceManager().setSharedPreferencesName(PREFERENCES_NAME);
77
78        addPreferencesFromResource(R.xml.preferences);
79
80        PreferenceScreen preferenceScreen = getPreferenceScreen();
81        mClearShortcutsPreference = preferenceScreen.findPreference(CLEAR_SHORTCUTS_PREF);
82        mSearchEngineSettingsPreference = (PreferenceScreen) preferenceScreen.findPreference(
83                SEARCH_ENGINE_SETTINGS_PREF);
84        mSourcePreferences = (PreferenceGroup) getPreferenceScreen().findPreference(
85                SEARCH_CORPORA_PREF);
86
87        mClearShortcutsPreference.setOnPreferenceClickListener(this);
88
89        updateClearShortcutsPreference();
90        populateSourcePreference();
91        populateSearchEnginePreference();
92    }
93
94    /**
95     * Gets the preference key of the preference for whether the given corpus
96     * is enabled. The preference is stored in the {@link #PREFERENCES_NAME}
97     * preferences file.
98     */
99    public static String getCorpusEnabledPreference(Corpus corpus) {
100        return CORPUS_ENABLED_PREF_PREFIX + corpus.getName();
101    }
102
103    public static SharedPreferences getSearchPreferences(Context context) {
104        return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
105    }
106
107    private QsbApplication getQsbApplication() {
108        return (QsbApplication) getApplication();
109    }
110
111    private Corpora getCorpora() {
112        return getQsbApplication().getCorpora();
113    }
114
115    private ShortcutRepository getShortcuts() {
116        return getQsbApplication().getShortcutRepository();
117    }
118
119    /**
120     * Enables/disables the "Clear search shortcuts" preference depending
121     * on whether there is any search history.
122     */
123    private void updateClearShortcutsPreference() {
124        boolean hasHistory = getShortcuts().hasHistory();
125        if (DBG) Log.d(TAG, "hasHistory()=" + hasHistory);
126        mClearShortcutsPreference.setEnabled(hasHistory);
127    }
128
129    /**
130     * Populates the preference item for the web search engine, which links to further
131     * search settings.
132     */
133    private void populateSearchEnginePreference() {
134        Intent intent = new Intent(SearchManager.INTENT_ACTION_WEB_SEARCH_SETTINGS);
135        intent.setPackage(getPackageName());
136
137        CharSequence webSearchSettingsLabel = getActivityLabel(intent);
138        mSearchEngineSettingsPreference.setTitle(webSearchSettingsLabel);
139        mSearchEngineSettingsPreference.setIntent(intent);
140    }
141
142    private CharSequence getActivityLabel(Intent intent) {
143        PackageManager pm = getPackageManager();
144        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
145        if (resolveInfos.size() == 0) {
146            Log.e(TAG, "No web search settings activity");
147            return null;
148        }
149        if (resolveInfos.size() > 1) {
150            Log.e(TAG, "More than one web search settings activity");
151            return null;
152        }
153        return resolveInfos.get(0).activityInfo.loadLabel(pm);
154    }
155
156    /**
157     * Fills the suggestion source list.
158     */
159    private void populateSourcePreference() {
160        for (Corpus corpus : getCorpora().getAllCorpora()) {
161            Preference pref = createCorpusPreference(corpus);
162            if (pref != null) {
163                if (DBG) Log.d(TAG, "Adding corpus: " + corpus);
164                mSourcePreferences.addPreference(pref);
165            }
166        }
167    }
168
169    /**
170     * Adds a suggestion source to the list of suggestion source checkbox preferences.
171     */
172    private Preference createCorpusPreference(Corpus corpus) {
173        CheckBoxPreference sourcePref = new CheckBoxPreference(this);
174        sourcePref.setKey(getCorpusEnabledPreference(corpus));
175        sourcePref.setDefaultValue(getCorpora().isCorpusDefaultEnabled(corpus));
176        sourcePref.setOnPreferenceChangeListener(this);
177        CharSequence label = corpus.getLabel();
178        sourcePref.setTitle(label);
179        CharSequence description = corpus.getSettingsDescription();
180        sourcePref.setSummaryOn(description);
181        sourcePref.setSummaryOff(description);
182        return sourcePref;
183    }
184
185    /**
186     * Handles clicks on the "Clear search shortcuts" preference.
187     */
188    public synchronized boolean onPreferenceClick(Preference preference) {
189        if (preference == mClearShortcutsPreference) {
190            showDialog(CLEAR_SHORTCUTS_CONFIRM_DIALOG);
191            return true;
192        }
193        return false;
194    }
195
196    @Override
197    protected Dialog onCreateDialog(int id, Bundle args) {
198        switch (id) {
199            case CLEAR_SHORTCUTS_CONFIRM_DIALOG:
200                return new AlertDialog.Builder(this)
201                        .setTitle(R.string.clear_shortcuts)
202                        .setMessage(R.string.clear_shortcuts_prompt)
203                        .setPositiveButton(R.string.agree, new DialogInterface.OnClickListener() {
204                            public void onClick(DialogInterface dialog, int whichButton) {
205                                if (DBG) Log.d(TAG, "Clearing history...");
206                                getShortcuts().clearHistory();
207                                updateClearShortcutsPreference();
208                            }
209                        })
210                        .setNegativeButton(R.string.disagree, null).create();
211            default:
212                Log.e(TAG, "unknown dialog" + id);
213                return null;
214        }
215    }
216
217    /**
218     * Informs our listeners about the updated settings data.
219     */
220    private void broadcastSettingsChanged() {
221        // We use a message broadcast since the listeners could be in multiple processes.
222        Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCH_SETTINGS_CHANGED);
223        Log.i(TAG, "Broadcasting: " + intent);
224        sendBroadcast(intent);
225    }
226
227    public synchronized boolean onPreferenceChange(Preference preference, Object newValue) {
228        broadcastSettingsChanged();
229        return true;
230    }
231
232    public static boolean getShowWebSuggestions(Context context) {
233        return (Settings.System.getInt(context.getContentResolver(),
234                Settings.System.SHOW_WEB_SUGGESTIONS,
235                1 /* default on until user actually changes it */) == 1);
236    }
237
238    public static void setShowWebSuggestions(Context context, boolean showWebSuggestions) {
239        System.putInt(context.getContentResolver(), System.SHOW_WEB_SUGGESTIONS,
240            showWebSuggestions ? 1 : 0);
241    }
242
243    public static void registerShowWebSuggestionsSettingObserver(
244            Context context, ContentObserver observer) {
245        context.getContentResolver().registerContentObserver(
246                Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS),
247                false, observer);
248    }
249
250    public static void unregisterShowWebSuggestionsSettingObserver(
251            Context context, ContentObserver observer) {
252        context.getContentResolver().unregisterContentObserver(observer);
253    }
254}
255