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