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 17 package com.android.quicksearchbox; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.app.SearchManager; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.content.SharedPreferences; 26 import android.content.pm.PackageManager; 27 import android.content.pm.ResolveInfo; 28 import android.database.ContentObserver; 29 import android.os.Bundle; 30 import android.preference.Preference; 31 import android.preference.PreferenceActivity; 32 import android.preference.PreferenceScreen; 33 import android.preference.Preference.OnPreferenceClickListener; 34 import android.provider.Settings; 35 import android.provider.Settings.System; 36 import android.util.Log; 37 import android.view.Menu; 38 39 import java.util.List; 40 41 /** 42 * Activity for setting global search preferences. 43 */ 44 public 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 // Prefix 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 ShortcutRepository getShortcuts() { 113 return QsbApplication.get(this).getShortcutRepository(); 114 } 115 116 private Config getConfig() { 117 return QsbApplication.get(this).getConfig(); 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 public synchronized boolean onPreferenceClick(Preference preference) { 158 if (preference == mClearShortcutsPreference) { 159 showDialog(CLEAR_SHORTCUTS_CONFIRM_DIALOG); 160 return true; 161 } 162 return false; 163 } 164 165 @Override 166 protected Dialog onCreateDialog(int id, Bundle args) { 167 switch (id) { 168 case CLEAR_SHORTCUTS_CONFIRM_DIALOG: 169 return new AlertDialog.Builder(this) 170 .setTitle(R.string.clear_shortcuts) 171 .setMessage(R.string.clear_shortcuts_prompt) 172 .setPositiveButton(R.string.agree, new DialogInterface.OnClickListener() { 173 public void onClick(DialogInterface dialog, int whichButton) { 174 if (DBG) Log.d(TAG, "Clearing history..."); 175 getShortcuts().clearHistory(); 176 mClearShortcutsPreference.setEnabled(false); 177 } 178 }) 179 .setNegativeButton(R.string.disagree, null).create(); 180 default: 181 Log.e(TAG, "unknown dialog" + id); 182 return null; 183 } 184 } 185 186 /** 187 * Informs our listeners about the updated settings data. 188 */ 189 public static void broadcastSettingsChanged(Context context) { 190 // We use a message broadcast since the listeners could be in multiple processes. 191 Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCH_SETTINGS_CHANGED); 192 Log.i(TAG, "Broadcasting: " + intent); 193 context.sendBroadcast(intent); 194 } 195 196 public static boolean getShowWebSuggestions(Context context) { 197 return (Settings.System.getInt(context.getContentResolver(), 198 Settings.System.SHOW_WEB_SUGGESTIONS, 199 1 /* default on until user actually changes it */) == 1); 200 } 201 202 public static void setShowWebSuggestions(Context context, boolean showWebSuggestions) { 203 System.putInt(context.getContentResolver(), System.SHOW_WEB_SUGGESTIONS, 204 showWebSuggestions ? 1 : 0); 205 } 206 207 public static void registerShowWebSuggestionsSettingObserver( 208 Context context, ContentObserver observer) { 209 context.getContentResolver().registerContentObserver( 210 Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS), 211 false, observer); 212 } 213 214 public static void unregisterShowWebSuggestionsSettingObserver( 215 Context context, ContentObserver observer) { 216 context.getContentResolver().unregisterContentObserver(observer); 217 } 218 219 public static void addSearchSettingsMenuItem(Context context, Menu menu) { 220 Intent settings = new Intent(SearchManager.INTENT_ACTION_SEARCH_SETTINGS); 221 settings.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 222 // Don't show activity chooser if there are multiple search settings activities, 223 // e.g. from different QSB implementations. 224 settings.setPackage(context.getPackageName()); 225 menu.add(Menu.NONE, Menu.NONE, 0, R.string.menu_settings) 226 .setIcon(R.drawable.ic_menu_preferences).setAlphabeticShortcut('P') 227 .setIntent(settings); 228 } 229 } 230