SearchSettings.java revision 44dca900d1d5e1a2e1a13da1691be94474b4a79a
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.PreferenceScreen;
34import android.preference.Preference.OnPreferenceClickListener;
35import android.provider.Settings;
36import android.provider.Settings.System;
37import android.util.Log;
38import android.view.Menu;
39
40import java.util.List;
41
42/**
43 * Activity for setting global search preferences.
44 */
45public class SearchSettings extends PreferenceActivity
46        implements OnPreferenceClickListener {
47
48    private static final boolean DBG = false;
49    private static final String TAG = "SearchSettings";
50
51    // Name of the preferences file used to store search preference
52    public static final String PREFERENCES_NAME = "SearchSettings";
53
54    // Intent action that opens the "Searchable Items" preference
55    public static final String ACTION_SEARCHABLE_ITEMS =
56            "com.android.quicksearchbox.action.SEARCHABLE_ITEMS";
57
58    // Only used to find the preferences after inflating
59    private static final String CLEAR_SHORTCUTS_PREF = "clear_shortcuts";
60    private static final String SEARCH_ENGINE_SETTINGS_PREF = "search_engine_settings";
61    private static final String SEARCH_CORPORA_PREF = "search_corpora";
62    private static final String SEARCH_WIDGET_CATEGORY = "search_widget_settings_category";
63
64    // Prefix of per-corpus enable preference
65    private static final String CORPUS_ENABLED_PREF_PREFIX = "enable_corpus_";
66    private static final String SEARCH_WIDGET_HINTS_ENABLED_PREF = "search_widget_hints_enabled";
67
68    // References to the top-level preference objects
69    private Preference mClearShortcutsPreference;
70    private PreferenceScreen mSearchEngineSettingsPreference;
71    private CheckBoxPreference mVoiceSearchHintsPreference;
72
73    // Dialog ids
74    private static final int CLEAR_SHORTCUTS_CONFIRM_DIALOG = 0;
75
76    @Override
77    protected void onCreate(Bundle savedInstanceState) {
78        super.onCreate(savedInstanceState);
79
80        getPreferenceManager().setSharedPreferencesName(PREFERENCES_NAME);
81
82        addPreferencesFromResource(R.xml.preferences);
83
84        PreferenceScreen preferenceScreen = getPreferenceScreen();
85        mClearShortcutsPreference = preferenceScreen.findPreference(CLEAR_SHORTCUTS_PREF);
86        mSearchEngineSettingsPreference = (PreferenceScreen) preferenceScreen.findPreference(
87                SEARCH_ENGINE_SETTINGS_PREF);
88        mVoiceSearchHintsPreference = (CheckBoxPreference)
89                preferenceScreen.findPreference(SEARCH_WIDGET_HINTS_ENABLED_PREF);
90        Preference corporaPreference = preferenceScreen.findPreference(SEARCH_CORPORA_PREF);
91        corporaPreference.setIntent(getSearchableItemsIntent(this));
92
93        mClearShortcutsPreference.setOnPreferenceClickListener(this);
94
95        if (getConfig().allowVoiceSearchHints()) {
96            mVoiceSearchHintsPreference.setOnPreferenceClickListener(this);
97        } else {
98            preferenceScreen.removePreference(
99                    preferenceScreen.findPreference(SEARCH_WIDGET_CATEGORY));
100            mVoiceSearchHintsPreference = null;
101        }
102
103        updateClearShortcutsPreference();
104        populateSearchEnginePreference();
105    }
106
107    public static Intent getSearchableItemsIntent(Context context) {
108        Intent intent = new Intent(SearchSettings.ACTION_SEARCHABLE_ITEMS);
109        intent.setPackage(context.getPackageName());
110        return intent;
111    }
112
113    /**
114     * Gets the preference key of the preference for whether the given corpus
115     * is enabled. The preference is stored in the {@link #PREFERENCES_NAME}
116     * preferences file.
117     */
118    public static String getCorpusEnabledPreference(Corpus corpus) {
119        return CORPUS_ENABLED_PREF_PREFIX + corpus.getName();
120    }
121
122    public static boolean areVoiceSearchHintsEnabled(Context context) {
123        return getSearchPreferences(context).getBoolean(SEARCH_WIDGET_HINTS_ENABLED_PREF, true);
124    }
125
126    public static void setVoiceSearchHintsEnabled(Context context, boolean enabled) {
127        getSearchPreferences(context)
128                .edit().putBoolean(SEARCH_WIDGET_HINTS_ENABLED_PREF, enabled).commit();
129        SearchWidgetProvider.updateSearchWidgets(context);
130    }
131
132    public static SharedPreferences getSearchPreferences(Context context) {
133        return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
134    }
135
136    private ShortcutRepository getShortcuts() {
137        return QsbApplication.get(this).getShortcutRepository();
138    }
139
140    private Config getConfig() {
141        return QsbApplication.get(this).getConfig();
142    }
143
144    /**
145     * Enables/disables the "Clear search shortcuts" preference depending
146     * on whether there is any search history.
147     */
148    private void updateClearShortcutsPreference() {
149        boolean hasHistory = getShortcuts().hasHistory();
150        if (DBG) Log.d(TAG, "hasHistory()=" + hasHistory);
151        mClearShortcutsPreference.setEnabled(hasHistory);
152    }
153
154    /**
155     * Populates the preference item for the web search engine, which links to further
156     * search settings.
157     */
158    private void populateSearchEnginePreference() {
159        Intent intent = new Intent(SearchManager.INTENT_ACTION_WEB_SEARCH_SETTINGS);
160        intent.setPackage(getPackageName());
161
162        CharSequence webSearchSettingsLabel = getActivityLabel(intent);
163        mSearchEngineSettingsPreference.setTitle(webSearchSettingsLabel);
164        mSearchEngineSettingsPreference.setIntent(intent);
165    }
166
167    private CharSequence getActivityLabel(Intent intent) {
168        PackageManager pm = getPackageManager();
169        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
170        if (resolveInfos.size() == 0) {
171            Log.e(TAG, "No web search settings activity");
172            return null;
173        }
174        if (resolveInfos.size() > 1) {
175            Log.e(TAG, "More than one web search settings activity");
176            return null;
177        }
178        return resolveInfos.get(0).activityInfo.loadLabel(pm);
179    }
180
181    public synchronized boolean onPreferenceClick(Preference preference) {
182        if (preference == mClearShortcutsPreference) {
183            showDialog(CLEAR_SHORTCUTS_CONFIRM_DIALOG);
184            return true;
185        } else if (preference == mVoiceSearchHintsPreference) {
186            SearchWidgetProvider.updateSearchWidgets(this);
187            return true;
188        }
189        return false;
190    }
191
192    @Override
193    protected Dialog onCreateDialog(int id, Bundle args) {
194        switch (id) {
195            case CLEAR_SHORTCUTS_CONFIRM_DIALOG:
196                return new AlertDialog.Builder(this)
197                        .setTitle(R.string.clear_shortcuts)
198                        .setMessage(R.string.clear_shortcuts_prompt)
199                        .setPositiveButton(R.string.agree, new DialogInterface.OnClickListener() {
200                            public void onClick(DialogInterface dialog, int whichButton) {
201                                if (DBG) Log.d(TAG, "Clearing history...");
202                                getShortcuts().clearHistory();
203                                mClearShortcutsPreference.setEnabled(false);
204                            }
205                        })
206                        .setNegativeButton(R.string.disagree, null).create();
207            default:
208                Log.e(TAG, "unknown dialog" + id);
209                return null;
210        }
211    }
212
213    /**
214     * Informs our listeners about the updated settings data.
215     */
216    public static void broadcastSettingsChanged(Context context) {
217        // We use a message broadcast since the listeners could be in multiple processes.
218        Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCH_SETTINGS_CHANGED);
219        Log.i(TAG, "Broadcasting: " + intent);
220        context.sendBroadcast(intent);
221    }
222
223    public static boolean getShowWebSuggestions(Context context) {
224        return (Settings.System.getInt(context.getContentResolver(),
225                Settings.System.SHOW_WEB_SUGGESTIONS,
226                1 /* default on until user actually changes it */) == 1);
227    }
228
229    public static void setShowWebSuggestions(Context context, boolean showWebSuggestions) {
230        System.putInt(context.getContentResolver(), System.SHOW_WEB_SUGGESTIONS,
231            showWebSuggestions ? 1 : 0);
232    }
233
234    public static void registerShowWebSuggestionsSettingObserver(
235            Context context, ContentObserver observer) {
236        context.getContentResolver().registerContentObserver(
237                Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS),
238                false, observer);
239    }
240
241    public static void unregisterShowWebSuggestionsSettingObserver(
242            Context context, ContentObserver observer) {
243        context.getContentResolver().unregisterContentObserver(observer);
244    }
245
246    public static void addSearchSettingsMenuItem(Context context, Menu menu) {
247        Intent settings = new Intent(SearchManager.INTENT_ACTION_SEARCH_SETTINGS);
248        settings.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
249        // Don't show activity chooser if there are multiple search settings activities,
250        // e.g. from different QSB implementations.
251        settings.setPackage(context.getPackageName());
252        menu.add(Menu.NONE, Menu.NONE, 0, R.string.menu_settings)
253                .setIcon(R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
254                .setIntent(settings);
255    }
256}
257