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