blob: 1b2a48c48173ca04031030101fccd239dca59484 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 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.settings;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.accounts.OnAccountsUpdateListener;
22import android.app.ActionBar;
23import android.app.Activity;
24import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080027import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070028import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080029import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.content.SharedPreferences;
33import android.content.pm.ActivityInfo;
34import android.content.pm.PackageManager;
35import android.content.pm.PackageManager.NameNotFoundException;
36import android.content.pm.ResolveInfo;
37import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.content.res.TypedArray;
39import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.nfc.NfcAdapter;
41import android.os.Bundle;
42import android.os.Handler;
43import android.os.INetworkManagementService;
44import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.UserHandle;
48import android.os.UserManager;
49import android.preference.Preference;
50import android.preference.PreferenceFragment;
51import android.preference.PreferenceManager;
52import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070054import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.AttributeSet;
56import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080057import android.util.TypedValue;
58import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070059import android.view.Menu;
60import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080061import android.view.MenuItem;
62import android.view.View;
63import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070064import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080065import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070066import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040067
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080068import com.android.internal.util.ArrayUtils;
69import com.android.internal.util.XmlUtils;
70import com.android.settings.accessibility.AccessibilitySettings;
71import com.android.settings.accessibility.CaptionPropertiesFragment;
72import com.android.settings.accounts.AccountSyncSettings;
73import com.android.settings.accounts.AuthenticatorHelper;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.accounts.ManageAccountsSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070075import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.applications.ManageApplications;
77import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070079import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070082import com.android.settings.dashboard.NoHomeDialogFragment;
83import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084import com.android.settings.deviceinfo.Memory;
85import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040086import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087import com.android.settings.fuelgauge.PowerUsageSummary;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070088import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070089import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
91import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
92import com.android.settings.inputmethod.SpellCheckersSettings;
93import com.android.settings.inputmethod.UserDictionaryList;
94import com.android.settings.location.LocationSettings;
95import com.android.settings.nfc.AndroidBeam;
96import com.android.settings.nfc.PaymentSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040097import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
101import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800102import com.android.settings.print.PrintJobSettingsFragment;
103import com.android.settings.print.PrintSettingsFragment;
104import com.android.settings.tts.TextToSpeechSettings;
105import com.android.settings.users.UserSettings;
106import com.android.settings.vpn2.VpnSettings;
107import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700108import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.wifi.AdvancedWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.wifi.WifiSettings;
111import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400112
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import org.xmlpull.v1.XmlPullParser;
114import org.xmlpull.v1.XmlPullParserException;
115
116import java.io.IOException;
117import java.util.ArrayList;
118import java.util.Collections;
119import java.util.Comparator;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700121import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700123import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700124
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125public class SettingsActivity extends Activity
126 implements PreferenceManager.OnPreferenceTreeClickListener,
127 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700128 ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
129 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100130 MenuItem.OnActionExpandListener,
131 AuthenticatorHelper.OnAccountsUpdateListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133 private static final String LOG_TAG = "Settings";
134
135 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700136 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
138 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700139 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700140 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 /**
143 * When starting this activity, the invoking Intent can contain this extra
144 * string to specify which fragment should be initially displayed.
145 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
146 * will call isValidFragment() to confirm that the fragment class name is valid for this
147 * activity.
148 */
149 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
150
151 /**
152 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
153 * this extra can also be specified to supply a Bundle of arguments to pass
154 * to that fragment when it is instantiated during the initial creation
155 * of the activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
158
159 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700160 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
161 */
162 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
163
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800164 public static final String BACK_STACK_PREFS = ":settings:prefs";
165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 // extras that allow any preference activity to be launched as part of a wizard
167
168 // show Back and Next buttons? takes boolean parameter
169 // Back will then return RESULT_CANCELED and Next RESULT_OK
170 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
171
172 // add a Skip button?
173 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
174
175 // specify custom text for the Back or Next buttons, or cause a button to not appear
176 // at all by setting it to null
177 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
178 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
179
180 /**
181 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700182 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800183 * that fragment.
184 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700187 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800189 private static final String META_DATA_KEY_FRAGMENT_CLASS =
190 "com.android.settings.FRAGMENT_CLASS";
191
192 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
193
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700194 private static final String EMPTY_QUERY = "";
195
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196 private static boolean sShowNoHomeNotice = false;
197
198 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800199
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800200 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700201 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800202
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203 // Show only these settings for restricted users
204 private int[] SETTINGS_FOR_RESTRICTED = {
205 R.id.wireless_section,
206 R.id.wifi_settings,
207 R.id.bluetooth_settings,
208 R.id.data_usage_settings,
209 R.id.wireless_settings,
210 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400211 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 R.id.display_settings,
213 R.id.storage_settings,
214 R.id.application_settings,
215 R.id.battery_settings,
216 R.id.personal_section,
217 R.id.location_settings,
218 R.id.security_settings,
219 R.id.language_settings,
220 R.id.user_settings,
221 R.id.account_settings,
222 R.id.account_add,
223 R.id.system_section,
224 R.id.date_time_settings,
225 R.id.about_settings,
226 R.id.accessibility_settings,
227 R.id.print_settings,
228 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800229 R.id.home_settings,
230 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800231 };
232
233 private static final String[] ENTRY_FRAGMENTS = {
234 WirelessSettings.class.getName(),
235 WifiSettings.class.getName(),
236 AdvancedWifiSettings.class.getName(),
237 BluetoothSettings.class.getName(),
238 TetherSettings.class.getName(),
239 WifiP2pSettings.class.getName(),
240 VpnSettings.class.getName(),
241 DateTimeSettings.class.getName(),
242 LocalePicker.class.getName(),
243 InputMethodAndLanguageSettings.class.getName(),
244 SpellCheckersSettings.class.getName(),
245 UserDictionaryList.class.getName(),
246 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 DisplaySettings.class.getName(),
248 DeviceInfoSettings.class.getName(),
249 ManageApplications.class.getName(),
250 ProcessStatsUi.class.getName(),
251 NotificationStation.class.getName(),
252 LocationSettings.class.getName(),
253 SecuritySettings.class.getName(),
254 PrivacySettings.class.getName(),
255 DeviceAdminSettings.class.getName(),
256 AccessibilitySettings.class.getName(),
257 CaptionPropertiesFragment.class.getName(),
258 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
260 TextToSpeechSettings.class.getName(),
261 Memory.class.getName(),
262 DevelopmentSettings.class.getName(),
263 UsbSettings.class.getName(),
264 AndroidBeam.class.getName(),
265 WifiDisplaySettings.class.getName(),
266 PowerUsageSummary.class.getName(),
267 AccountSyncSettings.class.getName(),
268 CryptKeeperSettings.class.getName(),
269 DataUsageSummary.class.getName(),
270 DreamSettings.class.getName(),
271 UserSettings.class.getName(),
272 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400273 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 ManageAccountsSettings.class.getName(),
275 PrintSettingsFragment.class.getName(),
276 PrintJobSettingsFragment.class.getName(),
277 TrustedCredentialsSettings.class.getName(),
278 PaymentSettings.class.getName(),
279 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700280 ZenModeSettings.class.getName(),
281 NotificationSettings.class.getName(),
282 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
283 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400284 InstalledAppDetails.class.getName(),
285 BatterySaverSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 };
287
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700288
289 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
290 "android.settings.APPLICATION_DETAILS_SETTINGS"
291 };
292
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 private SharedPreferences mDevelopmentPreferences;
294 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
295
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 private AuthenticatorHelper mAuthenticatorHelper;
297 private boolean mListeningToAccountUpdates;
298
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 private boolean mBatteryPresent = true;
300 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
301
302 @Override
303 public void onReceive(Context context, Intent intent) {
304 String action = intent.getAction();
305 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
306 boolean batteryPresent = Utils.isBatteryPresent(intent);
307
308 if (mBatteryPresent != batteryPresent) {
309 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700310 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 }
312 }
313 }
314 };
315
Svetoslav990159a2014-04-14 17:14:59 -0700316 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
317 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700318
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700319 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700320 private SwitchBar mSwitchBar;
321
322 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700323
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700324 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700325 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700326
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700327 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700328 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700329
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700330 private ViewGroup mContent;
331
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700332 private SearchView mSearchView;
333 private MenuItem mSearchMenuItem;
334 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700335 private SearchResultsSummary mSearchResultsFragment;
336 private String mSearchQuery;
337
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700338 // Categories
339 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
340 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700342 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343 private Handler mHandler = new Handler() {
344 @Override
345 public void handleMessage(Message msg) {
346 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700347 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700348 if(mNeedToRebuildCategories) {
349 buildDashboardCategories(mCategories);
350 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351 } break;
352 }
353 }
354 };
355
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700356 private boolean mNeedToRevertToInitialFragment = false;
357
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700358 public SwitchBar getSwitchBar() {
359 return mSwitchBar;
360 }
361
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700362 public AuthenticatorHelper getAuthenticatorHelper() {
363 return mAuthenticatorHelper;
364 }
365
366 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700367 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700368 buildDashboardCategories(mCategories);
369 mNeedToRebuildCategories = false;
370 }
371 return mCategories;
372 }
373
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800374 @Override
375 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
376 // Override the fragment title for Wallpaper settings
377 int titleRes = pref.getTitleRes();
378 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
379 titleRes = R.string.wallpaper_settings_fragment_title;
380 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
381 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
382 if (UserManager.get(this).isLinkedUser()) {
383 titleRes = R.string.profile_info_settings_title;
384 } else {
385 titleRes = R.string.user_info_settings_title;
386 }
387 }
388 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
389 null, 0);
390 return true;
391 }
392
393 @Override
394 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
395 return false;
396 }
397
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700398 private void invalidateCategories() {
399 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
400 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401 }
402 }
403
404 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405 public void onConfigurationChanged(Configuration newConfig) {
406 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800407 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800408 }
409
410 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700411 protected void onStart() {
412 super.onStart();
413
414 if (mNeedToRevertToInitialFragment) {
415 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800416 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800417 }
418
419 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700420 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700421 if (!mDisplaySearch) {
422 return false;
423 }
424
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700425 MenuInflater inflater = getMenuInflater();
426 inflater.inflate(R.menu.options_menu, menu);
427
428 // Cache the search query (can be overriden by the OnQueryTextListener)
429 final String query = mSearchQuery;
430
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700431 mSearchMenuItem = menu.findItem(R.id.search);
432 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700433
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700434 if (mSearchMenuItem == null || mSearchView == null) {
435 return false;
436 }
437
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700438 if (mSearchResultsFragment != null) {
439 mSearchResultsFragment.setSearchView(mSearchView);
440 }
441
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700442 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700443 mSearchView.setOnQueryTextListener(this);
444 mSearchView.setOnCloseListener(this);
445
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700446 if (mSearchMenuItemExpanded) {
447 mSearchMenuItem.expandActionView();
448 }
449 mSearchView.setQuery(query, true /* submit */);
450
451 return true;
452 }
453
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700454 private static boolean isShortCutIntent(final Intent intent) {
455 Set<String> categories = intent.getCategories();
456 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
457 }
458
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700459 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700460 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700461 if (action == null) {
462 return false;
463 }
464 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
465 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
466 }
467 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700468 }
469
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700470 @Override
471 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700472 super.onCreate(savedState);
473
474 // Should happen before any call to getIntent()
475 getMetaData();
476
477 final Intent intent = getIntent();
478 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
479 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800480 }
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100481 // TODO: Delete accounts tile once we have the new screen working
482 // See: http://b/15815948
483 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
484 mAuthenticatorHelper = new AuthenticatorHelper(
485 this, UserHandle.getCallingUserHandle(), um, this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800486 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100487 mAuthenticatorHelper.onAccountsUpdated(null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800489 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
490 Context.MODE_PRIVATE);
491
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700492 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700493 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700494
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700495 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700496 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
497
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700498 final ComponentName cn = getIntent().getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700499 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700500
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700501 mIsShowingDashboard = className.equals(Settings.class.getName());
502 final boolean isSubSettings = className.equals(SubSettings.class.getName());
503
504 // If this is a sub settings or not the main Dashboard and not a Shortcut and not initial
505 // Fragment then apply the correct theme for the ActionBar content inset
506 if (isSubSettings ||
507 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName == null))) {
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700508 setTheme(R.style.Theme_SubSettings);
509 }
510
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800511 setContentView(R.layout.settings_main);
512
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700513 mContent = (ViewGroup) findViewById(R.id.prefs);
514
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515 getFragmentManager().addOnBackStackChangedListener(this);
516
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700517 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700518 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700519 }
520
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700521 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700522 // We are restarting from a previous saved state; used that to initialize, instead
523 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700524 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
525 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700527 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800528
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700529 ArrayList<DashboardCategory> categories =
530 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
531 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700532 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700533 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700534 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700536
537 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700538 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700540 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700541 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
542 // only if it is a sub settings
543 if (mIsShortcut) {
544 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700545 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700546 } else if (isSubSettings) {
547 mDisplayHomeAsUpEnabled = true;
548 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700549 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700550 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700551
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700552 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700553 switchToFragment(initialFragmentName, initialArguments, true, false,
554 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000555 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700556 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700557 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700558 // Show Search affordance
559 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700560 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700561 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700562 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800563 }
564 }
565
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700566 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700567 if (mActionBar != null) {
568 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
569 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
570 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700571 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
572
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
575
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700576 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 if (buttonBar != null) {
578 buttonBar.setVisibility(View.VISIBLE);
579
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700580 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 backButton.setOnClickListener(new OnClickListener() {
582 public void onClick(View v) {
583 setResult(RESULT_CANCELED);
584 finish();
585 }
586 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700587 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 skipButton.setOnClickListener(new OnClickListener() {
589 public void onClick(View v) {
590 setResult(RESULT_OK);
591 finish();
592 }
593 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700594 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 mNextButton.setOnClickListener(new OnClickListener() {
596 public void onClick(View v) {
597 setResult(RESULT_OK);
598 finish();
599 }
600 });
601
602 // set our various button parameters
603 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
604 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
605 if (TextUtils.isEmpty(buttonText)) {
606 mNextButton.setVisibility(View.GONE);
607 }
608 else {
609 mNextButton.setText(buttonText);
610 }
611 }
612 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
613 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
614 if (TextUtils.isEmpty(buttonText)) {
615 backButton.setVisibility(View.GONE);
616 }
617 else {
618 backButton.setText(buttonText);
619 }
620 }
621 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
622 skipButton.setVisibility(View.VISIBLE);
623 }
624 }
625 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800626 }
627
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 private void setTitleFromIntent(Intent intent) {
629 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
630 if (initialTitleResId > 0) {
631 mInitialTitle = null;
632 mInitialTitleResId = initialTitleResId;
633 setTitle(mInitialTitleResId);
634 } else {
635 mInitialTitleResId = -1;
636 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
637 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
638 setTitle(mInitialTitle);
639 }
640 }
641
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800642 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800643 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700644 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 }
646
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700647 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800648 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700649
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800650 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700651 if (mInitialTitleResId > 0) {
652 setTitle(mInitialTitleResId);
653 } else {
654 setTitle(mInitialTitle);
655 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700656 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800657 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700658
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800659 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
660 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700661
662 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800663 }
664
665 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
666 final CharSequence title;
667 final int titleRes = bse.getBreadCrumbTitleRes();
668 if (titleRes > 0) {
669 title = getText(titleRes);
670 } else {
671 title = bse.getBreadCrumbTitle();
672 }
673 if (title != null) {
674 setTitle(title);
675 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800676 }
677
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800678 @Override
679 protected void onSaveInstanceState(Bundle outState) {
680 super.onSaveInstanceState(outState);
681
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700682 if (mCategories.size() > 0) {
683 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800684 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700685
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700686 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700687 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700688
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700689 if (mDisplaySearch) {
690 // The option menus are created if the ActionBar is visible and they are also created
691 // asynchronously. If you launch Settings with an Intent action like
692 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
693 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
694 // menu item and search view are null.
695 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
696 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700697
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700698 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
699 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
700 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800701 }
702
703 @Override
704 public void onResume() {
705 super.onResume();
706
707 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
708 @Override
709 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700710 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700711 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800712 }
713 };
714 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
715 mDevelopmentPreferencesListener);
716
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800717 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700718
Svetoslav990159a2014-04-14 17:14:59 -0700719 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700720
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700721 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700722 onQueryTextSubmit(mSearchQuery);
723 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800724 }
725
726 @Override
727 public void onPause() {
728 super.onPause();
729
730 unregisterReceiver(mBatteryInfoReceiver);
731
Svetoslav990159a2014-04-14 17:14:59 -0700732 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 }
734
735 @Override
736 public void onDestroy() {
737 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700738
739 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
740 mDevelopmentPreferencesListener);
741 mDevelopmentPreferencesListener = null;
742
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800743 if (mListeningToAccountUpdates) {
744 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
745 }
746 }
747
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800748 protected boolean isValidFragment(String fragmentName) {
749 // Almost all fragments are wrapped in this,
750 // except for a few that have their own activities.
751 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
752 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
753 }
754 return false;
755 }
756
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800757 @Override
758 public Intent getIntent() {
759 Intent superIntent = super.getIntent();
760 String startingFragment = getStartingFragmentClass(superIntent);
761 // This is called from super.onCreate, isMultiPane() is not yet reliable
762 // Do not use onIsHidingHeaders either, which relies itself on this method
763 if (startingFragment != null) {
764 Intent modIntent = new Intent(superIntent);
765 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
766 Bundle args = superIntent.getExtras();
767 if (args != null) {
768 args = new Bundle(args);
769 } else {
770 args = new Bundle();
771 }
772 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100773 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 return modIntent;
775 }
776 return superIntent;
777 }
778
779 /**
780 * Checks if the component name in the intent is different from the Settings class and
781 * returns the class name to load as a fragment.
782 */
783 private String getStartingFragmentClass(Intent intent) {
784 if (mFragmentClass != null) return mFragmentClass;
785
786 String intentClass = intent.getComponent().getClassName();
787 if (intentClass.equals(getClass().getName())) return null;
788
789 if ("com.android.settings.ManageApplications".equals(intentClass)
790 || "com.android.settings.RunningServices".equals(intentClass)
791 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
792 // Old names of manage apps.
793 intentClass = com.android.settings.applications.ManageApplications.class.getName();
794 }
795
796 return intentClass;
797 }
798
799 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000800 * Start a new fragment containing a preference panel. If the preferences
801 * are being displayed in multi-pane mode, the given fragment class will
802 * be instantiated and placed in the appropriate pane. If running in
803 * single-pane mode, a new activity will be launched in which to show the
804 * fragment.
805 *
806 * @param fragmentClass Full name of the class implementing the fragment.
807 * @param args Any desired arguments to supply to the fragment.
808 * @param titleRes Optional resource identifier of the title of this
809 * fragment.
810 * @param titleText Optional text of the title of this fragment.
811 * @param resultTo Optional fragment that result data should be sent to.
812 * If non-null, resultTo.onActivityResult() will be called when this
813 * preference panel is done. The launched panel must use
814 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
815 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700816 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000817 */
818 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700819 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700820 String title = null;
821 if (titleRes < 0) {
822 if (titleText != null) {
823 title = titleText.toString();
824 } else {
825 // There not much we can do in that case
826 title = "";
827 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700828 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700829 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700830 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000831 }
832
833 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800834 * Called by a preference panel fragment to finish itself.
835 *
836 * @param caller The fragment that is asking to be finished.
837 * @param resultCode Optional result code to send back to the original
838 * launching fragment.
839 * @param resultData Optional result data to send back to the original
840 * launching fragment.
841 */
842 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
843 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700844 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800845 }
846
847 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000848 * Start a new fragment.
849 *
850 * @param fragment The fragment to start
851 * @param push If true, the current fragment will be pushed onto the back stack. If false,
852 * the current fragment will be replaced.
853 */
854 public void startPreferenceFragment(Fragment fragment, boolean push) {
855 FragmentTransaction transaction = getFragmentManager().beginTransaction();
856 transaction.replace(R.id.prefs, fragment);
857 if (push) {
858 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
859 transaction.addToBackStack(BACK_STACK_PREFS);
860 } else {
861 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
862 }
863 transaction.commitAllowingStateLoss();
864 }
865
866 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700867 * Switch to a specific Fragment with taking care of validation, Title and BackStack
868 */
869 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700870 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700871 if (validate && !isValidFragment(fragmentName)) {
872 throw new IllegalArgumentException("Invalid fragment for this activity: "
873 + fragmentName);
874 }
875 Fragment f = Fragment.instantiate(this, fragmentName, args);
876 FragmentTransaction transaction = getFragmentManager().beginTransaction();
877 transaction.replace(R.id.prefs, f);
878 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700879 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700880 }
881 if (addToBackStack) {
882 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
883 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700884 if (titleResId > 0) {
885 transaction.setBreadCrumbTitle(titleResId);
886 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700887 transaction.setBreadCrumbTitle(title);
888 }
889 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700890 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700891 return f;
892 }
893
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700894 public void setNeedToRebuildCategories(boolean need) {
895 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700896 }
897
898 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700899 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700900 *
901 * @param categories The list in which to place the tiles categories.
902 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700903 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700904 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700905 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
906 updateTilesList(categories);
907 }
908
909 /**
910 * Parse the given XML file as a categories description, adding each
911 * parsed categories and tiles into the target list.
912 *
913 * @param resid The XML resource to load and parse.
914 * @param target The list in which the parsed categories and tiles should be placed.
915 */
916 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
917 XmlResourceParser parser = null;
918 try {
919 parser = getResources().getXml(resid);
920 AttributeSet attrs = Xml.asAttributeSet(parser);
921
922 int type;
923 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
924 && type != XmlPullParser.START_TAG) {
925 // Parse next until start tag is found
926 }
927
928 String nodeName = parser.getName();
929 if (!"dashboard-categories".equals(nodeName)) {
930 throw new RuntimeException(
931 "XML document must start with <preference-categories> tag; found"
932 + nodeName + " at " + parser.getPositionDescription());
933 }
934
935 Bundle curBundle = null;
936
937 final int outerDepth = parser.getDepth();
938 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
939 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
940 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
941 continue;
942 }
943
944 nodeName = parser.getName();
945 if ("dashboard-category".equals(nodeName)) {
946 DashboardCategory category = new DashboardCategory();
947
948 TypedArray sa = obtainStyledAttributes(
949 attrs, com.android.internal.R.styleable.PreferenceHeader);
950 category.id = sa.getResourceId(
951 com.android.internal.R.styleable.PreferenceHeader_id,
952 (int)DashboardCategory.CAT_ID_UNDEFINED);
953
954 TypedValue tv = sa.peekValue(
955 com.android.internal.R.styleable.PreferenceHeader_title);
956 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
957 if (tv.resourceId != 0) {
958 category.titleRes = tv.resourceId;
959 } else {
960 category.title = tv.string;
961 }
962 }
963 sa.recycle();
964
965 final int innerDepth = parser.getDepth();
966 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
967 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
968 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
969 continue;
970 }
971
972 String innerNodeName = parser.getName();
973 if (innerNodeName.equals("dashboard-tile")) {
974 DashboardTile tile = new DashboardTile();
975
976 sa = obtainStyledAttributes(
977 attrs, com.android.internal.R.styleable.PreferenceHeader);
978 tile.id = sa.getResourceId(
979 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700980 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700981 tv = sa.peekValue(
982 com.android.internal.R.styleable.PreferenceHeader_title);
983 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
984 if (tv.resourceId != 0) {
985 tile.titleRes = tv.resourceId;
986 } else {
987 tile.title = tv.string;
988 }
989 }
990 tv = sa.peekValue(
991 com.android.internal.R.styleable.PreferenceHeader_summary);
992 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
993 if (tv.resourceId != 0) {
994 tile.summaryRes = tv.resourceId;
995 } else {
996 tile.summary = tv.string;
997 }
998 }
999 tile.iconRes = sa.getResourceId(
1000 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1001 tile.fragment = sa.getString(
1002 com.android.internal.R.styleable.PreferenceHeader_fragment);
1003 sa.recycle();
1004
1005 if (curBundle == null) {
1006 curBundle = new Bundle();
1007 }
1008
1009 final int innerDepth2 = parser.getDepth();
1010 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1011 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1012 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1013 continue;
1014 }
1015
1016 String innerNodeName2 = parser.getName();
1017 if (innerNodeName2.equals("extra")) {
1018 getResources().parseBundleExtra("extra", attrs, curBundle);
1019 XmlUtils.skipCurrentTag(parser);
1020
1021 } else if (innerNodeName2.equals("intent")) {
1022 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1023
1024 } else {
1025 XmlUtils.skipCurrentTag(parser);
1026 }
1027 }
1028
1029 if (curBundle.size() > 0) {
1030 tile.fragmentArguments = curBundle;
1031 curBundle = null;
1032 }
1033
1034 category.addTile(tile);
1035
1036 } else {
1037 XmlUtils.skipCurrentTag(parser);
1038 }
1039 }
1040
1041 target.add(category);
1042 } else {
1043 XmlUtils.skipCurrentTag(parser);
1044 }
1045 }
1046
1047 } catch (XmlPullParserException e) {
1048 throw new RuntimeException("Error parsing categories", e);
1049 } catch (IOException e) {
1050 throw new RuntimeException("Error parsing categories", e);
1051 } finally {
1052 if (parser != null) parser.close();
1053 }
1054 }
1055
1056 private void updateTilesList(List<DashboardCategory> target) {
1057 final boolean showDev = mDevelopmentPreferences.getBoolean(
1058 DevelopmentSettings.PREF_SHOW,
1059 android.os.Build.TYPE.equals("eng"));
1060
1061 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1062
1063 final int size = target.size();
1064 for (int i = 0; i < size; i++) {
1065
1066 DashboardCategory category = target.get(i);
1067
1068 // Ids are integers, so downcasting is ok
1069 int id = (int) category.id;
1070 if (id == R.id.account_settings) {
1071 insertAccountsTiles(category);
1072 continue;
1073 }
1074 int n = category.getTilesCount() - 1;
1075 while (n >= 0) {
1076
1077 DashboardTile tile = category.getTile(n);
1078
1079 id = (int) tile.id;
1080 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1081 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1082 } else if (id == R.id.wifi_settings) {
1083 // Remove WiFi Settings if WiFi service is not available.
1084 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1085 category.removeTile(n);
1086 }
1087 } else if (id == R.id.bluetooth_settings) {
1088 // Remove Bluetooth Settings if Bluetooth service is not available.
1089 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1090 category.removeTile(n);
1091 }
1092 } else if (id == R.id.data_usage_settings) {
1093 // Remove data usage when kernel module not enabled
1094 final INetworkManagementService netManager = INetworkManagementService.Stub
1095 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1096 try {
1097 if (!netManager.isBandwidthControlEnabled()) {
1098 category.removeTile(n);
1099 }
1100 } catch (RemoteException e) {
1101 // ignored
1102 }
1103 } else if (id == R.id.battery_settings) {
1104 // Remove battery settings when battery is not available. (e.g. TV)
1105
1106 if (!mBatteryPresent) {
1107 category.removeTile(n);
1108 }
1109 } else if (id == R.id.home_settings) {
1110 if (!updateHomeSettingTiles(tile)) {
1111 category.removeTile(n);
1112 }
1113 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001114 boolean hasMultipleUsers =
1115 ((UserManager) getSystemService(Context.USER_SERVICE))
1116 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001117 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001118 || (!UserManager.supportsMultipleUsers()
1119 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001120 || Utils.isMonkeyRunning()) {
1121 category.removeTile(n);
1122 }
1123 } else if (id == R.id.nfc_payment_settings) {
1124 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1125 category.removeTile(n);
1126 } else {
1127 // Only show if NFC is on and we have the HCE feature
1128 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1129 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1130 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1131 category.removeTile(n);
1132 }
1133 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001134 } else if (id == R.id.print_settings) {
1135 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1136 PackageManager.FEATURE_PRINTING);
1137 if (!hasPrintingSupport) {
1138 category.removeTile(n);
1139 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001140 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001141 if (!showDev || um.hasUserRestriction(
1142 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001143 category.removeTile(n);
1144 }
1145 } else if (id == R.id.account_add) {
1146 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1147 category.removeTile(n);
1148 }
1149 }
1150
1151 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001152 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1153 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001154 category.removeTile(n);
1155 }
1156
1157 n--;
1158 }
1159 }
1160 }
1161
1162 private boolean updateHomeSettingTiles(DashboardTile tile) {
1163 // Once we decide to show Home settings, keep showing it forever
1164 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1165 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1166 return true;
1167 }
1168
1169 try {
1170 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1171 getPackageManager().getHomeActivities(homeApps);
1172 if (homeApps.size() < 2) {
1173 // When there's only one available home app, omit this settings
1174 // category entirely at the top level UI. If the user just
1175 // uninstalled the penultimate home app candidiate, we also
1176 // now tell them about why they aren't seeing 'Home' in the list.
1177 if (sShowNoHomeNotice) {
1178 sShowNoHomeNotice = false;
1179 NoHomeDialogFragment.show(this);
1180 }
1181 return false;
1182 } else {
1183 // Okay, we're allowing the Home settings category. Tell it, when
1184 // invoked via this front door, that we'll need to be told about the
1185 // case when the user uninstalls all but one home app.
1186 if (tile.fragmentArguments == null) {
1187 tile.fragmentArguments = new Bundle();
1188 }
1189 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1190 }
1191 } catch (Exception e) {
1192 // Can't look up the home activity; bail on configuring the icon
1193 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1194 }
1195
1196 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1197 return true;
1198 }
1199
1200 private void insertAccountsTiles(DashboardCategory target) {
1201 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1202 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1203 for (String accountType : accountTypes) {
1204 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1205 if (label == null) {
1206 continue;
1207 }
1208
1209 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1210 boolean skipToAccount = accounts.length == 1
1211 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1212 DashboardTile accountTile = new DashboardTile();
1213 accountTile.title = label;
1214 if (accountTile.extras == null) {
1215 accountTile.extras = new Bundle();
1216 }
1217 if (skipToAccount) {
1218 accountTile.fragment = AccountSyncSettings.class.getName();
1219 accountTile.fragmentArguments = new Bundle();
1220 // Need this for the icon
1221 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1222 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1223 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1224 accounts[0]);
1225 } else {
1226 accountTile.fragment = ManageAccountsSettings.class.getName();
1227 accountTile.fragmentArguments = new Bundle();
1228 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1229 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1230 accountType);
1231 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1232 label.toString());
1233 }
1234 dashboardTiles.add(accountTile);
1235 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1236 }
1237
1238 // Sort by label
1239 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1240 @Override
1241 public int compare(DashboardTile t1, DashboardTile t2) {
1242 return t1.title.toString().compareTo(t2.title.toString());
1243 }
1244 });
1245 int index = 0;
1246 for (DashboardTile tile : dashboardTiles) {
1247 target.addTile(index, tile);
1248 index++;
1249 }
1250 if (!mListeningToAccountUpdates) {
1251 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1252 mListeningToAccountUpdates = true;
1253 }
1254 }
1255
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001256 private void getMetaData() {
1257 try {
1258 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1259 PackageManager.GET_META_DATA);
1260 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001261 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1262 } catch (NameNotFoundException nnfe) {
1263 // No recovery
1264 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1265 }
1266 }
1267
1268 // give subclasses access to the Next button
1269 public boolean hasNextButton() {
1270 return mNextButton != null;
1271 }
1272
1273 public Button getNextButton() {
1274 return mNextButton;
1275 }
1276
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001277 @Override
1278 public boolean shouldUpRecreateTask(Intent targetIntent) {
1279 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1280 }
1281
1282 @Override
1283 public void onAccountsUpdated(Account[] accounts) {
1284 // TODO: watch for package upgrades to invalidate cache; see 7206643
1285 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +01001286 invalidateCategories();
1287 }
1288
1289 @Override
1290 public void onAccountsUpdate(UserHandle userHandle) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001291 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001292 }
1293
1294 public static void requestHomeNotice() {
1295 sShowNoHomeNotice = true;
1296 }
1297
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001298 @Override
1299 public boolean onQueryTextSubmit(String query) {
1300 switchToSearchResultsFragmentIfNeeded();
1301 mSearchQuery = query;
1302 return mSearchResultsFragment.onQueryTextSubmit(query);
1303 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001304
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001305 @Override
1306 public boolean onQueryTextChange(String newText) {
1307 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001308 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001309 return false;
1310 }
1311 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001312 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001313
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001314 @Override
1315 public boolean onClose() {
1316 return false;
1317 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001318
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001319 @Override
1320 public boolean onMenuItemActionExpand(MenuItem item) {
1321 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001322 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001323 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001324 return true;
1325 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001326
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001327 @Override
1328 public boolean onMenuItemActionCollapse(MenuItem item) {
1329 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001330 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001331 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001332 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001333 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001334 return true;
1335 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001336
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001337 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001338 if (mSearchResultsFragment != null) {
1339 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001340 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001341 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1342 if (current != null && current instanceof SearchResultsSummary) {
1343 mSearchResultsFragment = (SearchResultsSummary) current;
1344 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001345 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001346 SearchResultsSummary.class.getName(), null, false, true,
1347 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001348 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001349 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001350 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001351 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001352
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001353 public void needToRevertToInitialFragment() {
1354 mNeedToRevertToInitialFragment = true;
1355 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001356
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001357 private void revertToInitialFragment() {
1358 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001359 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001360 mSearchMenuItemExpanded = false;
1361 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1362 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001363 if (mSearchMenuItem != null) {
1364 mSearchMenuItem.collapseActionView();
1365 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001366 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001367}