blob: a06c075492094ed4cdf3fb21c4a89dc0305e9431 [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,
130 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131
132 private static final String LOG_TAG = "Settings";
133
134 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700135 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
137 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700138 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700139 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800140
141 /**
142 * When starting this activity, the invoking Intent can contain this extra
143 * string to specify which fragment should be initially displayed.
144 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
145 * will call isValidFragment() to confirm that the fragment class name is valid for this
146 * activity.
147 */
148 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
149
150 /**
151 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
152 * this extra can also be specified to supply a Bundle of arguments to pass
153 * to that fragment when it is instantiated during the initial creation
154 * of the activity.
155 */
156 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
157
158 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700159 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
160 */
161 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
162
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800163 public static final String BACK_STACK_PREFS = ":settings:prefs";
164
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165 // extras that allow any preference activity to be launched as part of a wizard
166
167 // show Back and Next buttons? takes boolean parameter
168 // Back will then return RESULT_CANCELED and Next RESULT_OK
169 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
170
171 // add a Skip button?
172 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
173
174 // specify custom text for the Back or Next buttons, or cause a button to not appear
175 // at all by setting it to null
176 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
177 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
178
179 /**
180 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700181 * 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 -0800182 * that fragment.
183 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700184 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700186 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800187
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188 private static final String META_DATA_KEY_FRAGMENT_CLASS =
189 "com.android.settings.FRAGMENT_CLASS";
190
191 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
192
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700193 private static final String EMPTY_QUERY = "";
194
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 private static boolean sShowNoHomeNotice = false;
196
197 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800199 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700200 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 // Show only these settings for restricted users
203 private int[] SETTINGS_FOR_RESTRICTED = {
204 R.id.wireless_section,
205 R.id.wifi_settings,
206 R.id.bluetooth_settings,
207 R.id.data_usage_settings,
208 R.id.wireless_settings,
209 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400210 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 R.id.display_settings,
212 R.id.storage_settings,
213 R.id.application_settings,
214 R.id.battery_settings,
215 R.id.personal_section,
216 R.id.location_settings,
217 R.id.security_settings,
218 R.id.language_settings,
219 R.id.user_settings,
220 R.id.account_settings,
221 R.id.account_add,
222 R.id.system_section,
223 R.id.date_time_settings,
224 R.id.about_settings,
225 R.id.accessibility_settings,
226 R.id.print_settings,
227 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800228 R.id.home_settings,
229 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800230 };
231
232 private static final String[] ENTRY_FRAGMENTS = {
233 WirelessSettings.class.getName(),
234 WifiSettings.class.getName(),
235 AdvancedWifiSettings.class.getName(),
236 BluetoothSettings.class.getName(),
237 TetherSettings.class.getName(),
238 WifiP2pSettings.class.getName(),
239 VpnSettings.class.getName(),
240 DateTimeSettings.class.getName(),
241 LocalePicker.class.getName(),
242 InputMethodAndLanguageSettings.class.getName(),
243 SpellCheckersSettings.class.getName(),
244 UserDictionaryList.class.getName(),
245 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 DisplaySettings.class.getName(),
247 DeviceInfoSettings.class.getName(),
248 ManageApplications.class.getName(),
249 ProcessStatsUi.class.getName(),
250 NotificationStation.class.getName(),
251 LocationSettings.class.getName(),
252 SecuritySettings.class.getName(),
253 PrivacySettings.class.getName(),
254 DeviceAdminSettings.class.getName(),
255 AccessibilitySettings.class.getName(),
256 CaptionPropertiesFragment.class.getName(),
257 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
259 TextToSpeechSettings.class.getName(),
260 Memory.class.getName(),
261 DevelopmentSettings.class.getName(),
262 UsbSettings.class.getName(),
263 AndroidBeam.class.getName(),
264 WifiDisplaySettings.class.getName(),
265 PowerUsageSummary.class.getName(),
266 AccountSyncSettings.class.getName(),
267 CryptKeeperSettings.class.getName(),
268 DataUsageSummary.class.getName(),
269 DreamSettings.class.getName(),
270 UserSettings.class.getName(),
271 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400272 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 ManageAccountsSettings.class.getName(),
274 PrintSettingsFragment.class.getName(),
275 PrintJobSettingsFragment.class.getName(),
276 TrustedCredentialsSettings.class.getName(),
277 PaymentSettings.class.getName(),
278 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700279 ZenModeSettings.class.getName(),
280 NotificationSettings.class.getName(),
281 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
282 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400283 InstalledAppDetails.class.getName(),
284 BatterySaverSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 };
286
287 private SharedPreferences mDevelopmentPreferences;
288 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
289
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 private AuthenticatorHelper mAuthenticatorHelper;
291 private boolean mListeningToAccountUpdates;
292
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 private boolean mBatteryPresent = true;
294 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
295
296 @Override
297 public void onReceive(Context context, Intent intent) {
298 String action = intent.getAction();
299 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
300 boolean batteryPresent = Utils.isBatteryPresent(intent);
301
302 if (mBatteryPresent != batteryPresent) {
303 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700304 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305 }
306 }
307 }
308 };
309
Svetoslav990159a2014-04-14 17:14:59 -0700310 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
311 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700312
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700313 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700314 private SwitchBar mSwitchBar;
315
316 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700317
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700318 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700319 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700320
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700321 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700322 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700323
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700324 private ViewGroup mContent;
325
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700326 private SearchView mSearchView;
327 private MenuItem mSearchMenuItem;
328 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700329 private SearchResultsSummary mSearchResultsFragment;
330 private String mSearchQuery;
331
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700332 // Categories
333 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
334 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800335
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700336 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800337 private Handler mHandler = new Handler() {
338 @Override
339 public void handleMessage(Message msg) {
340 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700341 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700342 if(mNeedToRebuildCategories) {
343 buildDashboardCategories(mCategories);
344 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 } break;
346 }
347 }
348 };
349
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700350 private boolean mNeedToRevertToInitialFragment = false;
351
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700352 public SwitchBar getSwitchBar() {
353 return mSwitchBar;
354 }
355
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700356 public AuthenticatorHelper getAuthenticatorHelper() {
357 return mAuthenticatorHelper;
358 }
359
360 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700361 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700362 buildDashboardCategories(mCategories);
363 mNeedToRebuildCategories = false;
364 }
365 return mCategories;
366 }
367
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 @Override
369 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
370 // Override the fragment title for Wallpaper settings
371 int titleRes = pref.getTitleRes();
372 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
373 titleRes = R.string.wallpaper_settings_fragment_title;
374 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
375 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
376 if (UserManager.get(this).isLinkedUser()) {
377 titleRes = R.string.profile_info_settings_title;
378 } else {
379 titleRes = R.string.user_info_settings_title;
380 }
381 }
382 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
383 null, 0);
384 return true;
385 }
386
387 @Override
388 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
389 return false;
390 }
391
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700392 private void invalidateCategories() {
393 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
394 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800395 }
396 }
397
398 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800399 public void onConfigurationChanged(Configuration newConfig) {
400 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800401 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402 }
403
404 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700405 protected void onStart() {
406 super.onStart();
407
408 if (mNeedToRevertToInitialFragment) {
409 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 }
412
413 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700414 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700415 if (!mDisplaySearch) {
416 return false;
417 }
418
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700419 MenuInflater inflater = getMenuInflater();
420 inflater.inflate(R.menu.options_menu, menu);
421
422 // Cache the search query (can be overriden by the OnQueryTextListener)
423 final String query = mSearchQuery;
424
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700425 mSearchMenuItem = menu.findItem(R.id.search);
426 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700427
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700428 if (mSearchMenuItem == null || mSearchView == null) {
429 return false;
430 }
431
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700432 if (mSearchResultsFragment != null) {
433 mSearchResultsFragment.setSearchView(mSearchView);
434 }
435
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700436 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700437 mSearchView.setOnQueryTextListener(this);
438 mSearchView.setOnCloseListener(this);
439
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700440 if (mSearchMenuItemExpanded) {
441 mSearchMenuItem.expandActionView();
442 }
443 mSearchView.setQuery(query, true /* submit */);
444
445 return true;
446 }
447
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700448 private static boolean isShortCutIntent(final Intent intent) {
449 Set<String> categories = intent.getCategories();
450 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
451 }
452
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700453 private static boolean isAppInfoIntent(final Intent intent) {
454 String action = intent.getAction();
455 return (action != null) && action.equals("android.settings.APPLICATION_DETAILS_SETTINGS");
456 }
457
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700458 @Override
459 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700460 super.onCreate(savedState);
461
462 // Should happen before any call to getIntent()
463 getMetaData();
464
465 final Intent intent = getIntent();
466 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
467 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800468 }
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700469
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800470 mAuthenticatorHelper = new AuthenticatorHelper();
471 mAuthenticatorHelper.updateAuthDescriptions(this);
472 mAuthenticatorHelper.onAccountsUpdated(this, null);
473
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
475 Context.MODE_PRIVATE);
476
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700477 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700478 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700479
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700480 mIsShortcut = isShortCutIntent(intent) || isAppInfoIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700481 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
482
483 mIsShowingDashboard = (initialFragmentName == null) && !mIsShortcut;
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700484
485 final ComponentName cn = getIntent().getComponent();
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700486 final boolean isSubSettings = cn.getClassName().equals(SubSettings.class.getName());
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700487
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700488 // If this is a sub settings or not the main Dashboard and not a Shortcut then apply the
489 // correct theme for the ActionBar content inset
490 if (isSubSettings || (!mIsShowingDashboard && !mIsShortcut)) {
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700491 setTheme(R.style.Theme_SubSettings);
492 }
493
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800494 setContentView(R.layout.settings_main);
495
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700496 mContent = (ViewGroup) findViewById(R.id.prefs);
497
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800498 getFragmentManager().addOnBackStackChangedListener(this);
499
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700500 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700501 mDisplaySearch = true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800502
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700503 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700504 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700505 }
506
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700507 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700508 // We are restarting from a previous saved state; used that to initialize, instead
509 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700510 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
511 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700513 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800514
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700515 ArrayList<DashboardCategory> categories =
516 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
517 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700518 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700519 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700520 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800521 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700522
523 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700524 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800525 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700526 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700527 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
528 // only if it is a sub settings
529 if (mIsShortcut) {
530 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700531 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700532 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700533 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700534
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700535 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700536 switchToFragment(initialFragmentName, initialArguments, true, false,
537 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000538 } else {
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700539 // No UP if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700540 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700541 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700542 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700543 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800544 }
545 }
546
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700547 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700548 if (mActionBar != null) {
549 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
550 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
551 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700552 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
553
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800554 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800555 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
556
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700557 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 if (buttonBar != null) {
559 buttonBar.setVisibility(View.VISIBLE);
560
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700561 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 backButton.setOnClickListener(new OnClickListener() {
563 public void onClick(View v) {
564 setResult(RESULT_CANCELED);
565 finish();
566 }
567 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700568 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800569 skipButton.setOnClickListener(new OnClickListener() {
570 public void onClick(View v) {
571 setResult(RESULT_OK);
572 finish();
573 }
574 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700575 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 mNextButton.setOnClickListener(new OnClickListener() {
577 public void onClick(View v) {
578 setResult(RESULT_OK);
579 finish();
580 }
581 });
582
583 // set our various button parameters
584 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
585 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
586 if (TextUtils.isEmpty(buttonText)) {
587 mNextButton.setVisibility(View.GONE);
588 }
589 else {
590 mNextButton.setText(buttonText);
591 }
592 }
593 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
594 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
595 if (TextUtils.isEmpty(buttonText)) {
596 backButton.setVisibility(View.GONE);
597 }
598 else {
599 backButton.setText(buttonText);
600 }
601 }
602 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
603 skipButton.setVisibility(View.VISIBLE);
604 }
605 }
606 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800607 }
608
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700609 private void setTitleFromIntent(Intent intent) {
610 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
611 if (initialTitleResId > 0) {
612 mInitialTitle = null;
613 mInitialTitleResId = initialTitleResId;
614 setTitle(mInitialTitleResId);
615 } else {
616 mInitialTitleResId = -1;
617 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
618 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
619 setTitle(mInitialTitle);
620 }
621 }
622
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800623 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800624 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700625 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800626 }
627
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700628 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800629 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700630
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800631 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700632 if (mInitialTitleResId > 0) {
633 setTitle(mInitialTitleResId);
634 } else {
635 setTitle(mInitialTitle);
636 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700637 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800638 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700639
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800640 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
641 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700642
643 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800644 }
645
646 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
647 final CharSequence title;
648 final int titleRes = bse.getBreadCrumbTitleRes();
649 if (titleRes > 0) {
650 title = getText(titleRes);
651 } else {
652 title = bse.getBreadCrumbTitle();
653 }
654 if (title != null) {
655 setTitle(title);
656 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800657 }
658
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 @Override
660 protected void onSaveInstanceState(Bundle outState) {
661 super.onSaveInstanceState(outState);
662
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700663 if (mCategories.size() > 0) {
664 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800665 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700666
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700667 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700668 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700669
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700670 if (mDisplaySearch) {
671 // The option menus are created if the ActionBar is visible and they are also created
672 // asynchronously. If you launch Settings with an Intent action like
673 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
674 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
675 // menu item and search view are null.
676 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
677 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700678
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700679 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
680 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
681 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800682 }
683
684 @Override
685 public void onResume() {
686 super.onResume();
687
688 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
689 @Override
690 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700691 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700692 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 }
694 };
695 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
696 mDevelopmentPreferencesListener);
697
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800698 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700699
Svetoslav990159a2014-04-14 17:14:59 -0700700 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700701
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700702 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700703 onQueryTextSubmit(mSearchQuery);
704 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800705 }
706
707 @Override
708 public void onPause() {
709 super.onPause();
710
711 unregisterReceiver(mBatteryInfoReceiver);
712
Svetoslav990159a2014-04-14 17:14:59 -0700713 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800714 }
715
716 @Override
717 public void onDestroy() {
718 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700719
720 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
721 mDevelopmentPreferencesListener);
722 mDevelopmentPreferencesListener = null;
723
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800724 if (mListeningToAccountUpdates) {
725 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
726 }
727 }
728
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800729 protected boolean isValidFragment(String fragmentName) {
730 // Almost all fragments are wrapped in this,
731 // except for a few that have their own activities.
732 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
733 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
734 }
735 return false;
736 }
737
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800738 @Override
739 public Intent getIntent() {
740 Intent superIntent = super.getIntent();
741 String startingFragment = getStartingFragmentClass(superIntent);
742 // This is called from super.onCreate, isMultiPane() is not yet reliable
743 // Do not use onIsHidingHeaders either, which relies itself on this method
744 if (startingFragment != null) {
745 Intent modIntent = new Intent(superIntent);
746 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
747 Bundle args = superIntent.getExtras();
748 if (args != null) {
749 args = new Bundle(args);
750 } else {
751 args = new Bundle();
752 }
753 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100754 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800755 return modIntent;
756 }
757 return superIntent;
758 }
759
760 /**
761 * Checks if the component name in the intent is different from the Settings class and
762 * returns the class name to load as a fragment.
763 */
764 private String getStartingFragmentClass(Intent intent) {
765 if (mFragmentClass != null) return mFragmentClass;
766
767 String intentClass = intent.getComponent().getClassName();
768 if (intentClass.equals(getClass().getName())) return null;
769
770 if ("com.android.settings.ManageApplications".equals(intentClass)
771 || "com.android.settings.RunningServices".equals(intentClass)
772 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
773 // Old names of manage apps.
774 intentClass = com.android.settings.applications.ManageApplications.class.getName();
775 }
776
777 return intentClass;
778 }
779
780 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000781 * Start a new fragment containing a preference panel. If the preferences
782 * are being displayed in multi-pane mode, the given fragment class will
783 * be instantiated and placed in the appropriate pane. If running in
784 * single-pane mode, a new activity will be launched in which to show the
785 * fragment.
786 *
787 * @param fragmentClass Full name of the class implementing the fragment.
788 * @param args Any desired arguments to supply to the fragment.
789 * @param titleRes Optional resource identifier of the title of this
790 * fragment.
791 * @param titleText Optional text of the title of this fragment.
792 * @param resultTo Optional fragment that result data should be sent to.
793 * If non-null, resultTo.onActivityResult() will be called when this
794 * preference panel is done. The launched panel must use
795 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
796 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700797 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000798 */
799 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700800 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700801 String title = null;
802 if (titleRes < 0) {
803 if (titleText != null) {
804 title = titleText.toString();
805 } else {
806 // There not much we can do in that case
807 title = "";
808 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700809 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700810 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700811 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000812 }
813
814 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800815 * Called by a preference panel fragment to finish itself.
816 *
817 * @param caller The fragment that is asking to be finished.
818 * @param resultCode Optional result code to send back to the original
819 * launching fragment.
820 * @param resultData Optional result data to send back to the original
821 * launching fragment.
822 */
823 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
824 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700825 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800826 }
827
828 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000829 * Start a new fragment.
830 *
831 * @param fragment The fragment to start
832 * @param push If true, the current fragment will be pushed onto the back stack. If false,
833 * the current fragment will be replaced.
834 */
835 public void startPreferenceFragment(Fragment fragment, boolean push) {
836 FragmentTransaction transaction = getFragmentManager().beginTransaction();
837 transaction.replace(R.id.prefs, fragment);
838 if (push) {
839 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
840 transaction.addToBackStack(BACK_STACK_PREFS);
841 } else {
842 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
843 }
844 transaction.commitAllowingStateLoss();
845 }
846
847 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700848 * Switch to a specific Fragment with taking care of validation, Title and BackStack
849 */
850 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700851 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700852 if (validate && !isValidFragment(fragmentName)) {
853 throw new IllegalArgumentException("Invalid fragment for this activity: "
854 + fragmentName);
855 }
856 Fragment f = Fragment.instantiate(this, fragmentName, args);
857 FragmentTransaction transaction = getFragmentManager().beginTransaction();
858 transaction.replace(R.id.prefs, f);
859 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700860 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700861 }
862 if (addToBackStack) {
863 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
864 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700865 if (titleResId > 0) {
866 transaction.setBreadCrumbTitle(titleResId);
867 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700868 transaction.setBreadCrumbTitle(title);
869 }
870 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700871 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700872 return f;
873 }
874
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700875 public void setNeedToRebuildCategories(boolean need) {
876 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700877 }
878
879 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700880 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700881 *
882 * @param categories The list in which to place the tiles categories.
883 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700884 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700885 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700886 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
887 updateTilesList(categories);
888 }
889
890 /**
891 * Parse the given XML file as a categories description, adding each
892 * parsed categories and tiles into the target list.
893 *
894 * @param resid The XML resource to load and parse.
895 * @param target The list in which the parsed categories and tiles should be placed.
896 */
897 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
898 XmlResourceParser parser = null;
899 try {
900 parser = getResources().getXml(resid);
901 AttributeSet attrs = Xml.asAttributeSet(parser);
902
903 int type;
904 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
905 && type != XmlPullParser.START_TAG) {
906 // Parse next until start tag is found
907 }
908
909 String nodeName = parser.getName();
910 if (!"dashboard-categories".equals(nodeName)) {
911 throw new RuntimeException(
912 "XML document must start with <preference-categories> tag; found"
913 + nodeName + " at " + parser.getPositionDescription());
914 }
915
916 Bundle curBundle = null;
917
918 final int outerDepth = parser.getDepth();
919 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
920 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
921 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
922 continue;
923 }
924
925 nodeName = parser.getName();
926 if ("dashboard-category".equals(nodeName)) {
927 DashboardCategory category = new DashboardCategory();
928
929 TypedArray sa = obtainStyledAttributes(
930 attrs, com.android.internal.R.styleable.PreferenceHeader);
931 category.id = sa.getResourceId(
932 com.android.internal.R.styleable.PreferenceHeader_id,
933 (int)DashboardCategory.CAT_ID_UNDEFINED);
934
935 TypedValue tv = sa.peekValue(
936 com.android.internal.R.styleable.PreferenceHeader_title);
937 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
938 if (tv.resourceId != 0) {
939 category.titleRes = tv.resourceId;
940 } else {
941 category.title = tv.string;
942 }
943 }
944 sa.recycle();
945
946 final int innerDepth = parser.getDepth();
947 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
948 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
949 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
950 continue;
951 }
952
953 String innerNodeName = parser.getName();
954 if (innerNodeName.equals("dashboard-tile")) {
955 DashboardTile tile = new DashboardTile();
956
957 sa = obtainStyledAttributes(
958 attrs, com.android.internal.R.styleable.PreferenceHeader);
959 tile.id = sa.getResourceId(
960 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700961 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700962 tv = sa.peekValue(
963 com.android.internal.R.styleable.PreferenceHeader_title);
964 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
965 if (tv.resourceId != 0) {
966 tile.titleRes = tv.resourceId;
967 } else {
968 tile.title = tv.string;
969 }
970 }
971 tv = sa.peekValue(
972 com.android.internal.R.styleable.PreferenceHeader_summary);
973 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
974 if (tv.resourceId != 0) {
975 tile.summaryRes = tv.resourceId;
976 } else {
977 tile.summary = tv.string;
978 }
979 }
980 tile.iconRes = sa.getResourceId(
981 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
982 tile.fragment = sa.getString(
983 com.android.internal.R.styleable.PreferenceHeader_fragment);
984 sa.recycle();
985
986 if (curBundle == null) {
987 curBundle = new Bundle();
988 }
989
990 final int innerDepth2 = parser.getDepth();
991 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
992 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
993 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
994 continue;
995 }
996
997 String innerNodeName2 = parser.getName();
998 if (innerNodeName2.equals("extra")) {
999 getResources().parseBundleExtra("extra", attrs, curBundle);
1000 XmlUtils.skipCurrentTag(parser);
1001
1002 } else if (innerNodeName2.equals("intent")) {
1003 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1004
1005 } else {
1006 XmlUtils.skipCurrentTag(parser);
1007 }
1008 }
1009
1010 if (curBundle.size() > 0) {
1011 tile.fragmentArguments = curBundle;
1012 curBundle = null;
1013 }
1014
1015 category.addTile(tile);
1016
1017 } else {
1018 XmlUtils.skipCurrentTag(parser);
1019 }
1020 }
1021
1022 target.add(category);
1023 } else {
1024 XmlUtils.skipCurrentTag(parser);
1025 }
1026 }
1027
1028 } catch (XmlPullParserException e) {
1029 throw new RuntimeException("Error parsing categories", e);
1030 } catch (IOException e) {
1031 throw new RuntimeException("Error parsing categories", e);
1032 } finally {
1033 if (parser != null) parser.close();
1034 }
1035 }
1036
1037 private void updateTilesList(List<DashboardCategory> target) {
1038 final boolean showDev = mDevelopmentPreferences.getBoolean(
1039 DevelopmentSettings.PREF_SHOW,
1040 android.os.Build.TYPE.equals("eng"));
1041
1042 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1043
1044 final int size = target.size();
1045 for (int i = 0; i < size; i++) {
1046
1047 DashboardCategory category = target.get(i);
1048
1049 // Ids are integers, so downcasting is ok
1050 int id = (int) category.id;
1051 if (id == R.id.account_settings) {
1052 insertAccountsTiles(category);
1053 continue;
1054 }
1055 int n = category.getTilesCount() - 1;
1056 while (n >= 0) {
1057
1058 DashboardTile tile = category.getTile(n);
1059
1060 id = (int) tile.id;
1061 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1062 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1063 } else if (id == R.id.wifi_settings) {
1064 // Remove WiFi Settings if WiFi service is not available.
1065 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1066 category.removeTile(n);
1067 }
1068 } else if (id == R.id.bluetooth_settings) {
1069 // Remove Bluetooth Settings if Bluetooth service is not available.
1070 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1071 category.removeTile(n);
1072 }
1073 } else if (id == R.id.data_usage_settings) {
1074 // Remove data usage when kernel module not enabled
1075 final INetworkManagementService netManager = INetworkManagementService.Stub
1076 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1077 try {
1078 if (!netManager.isBandwidthControlEnabled()) {
1079 category.removeTile(n);
1080 }
1081 } catch (RemoteException e) {
1082 // ignored
1083 }
1084 } else if (id == R.id.battery_settings) {
1085 // Remove battery settings when battery is not available. (e.g. TV)
1086
1087 if (!mBatteryPresent) {
1088 category.removeTile(n);
1089 }
1090 } else if (id == R.id.home_settings) {
1091 if (!updateHomeSettingTiles(tile)) {
1092 category.removeTile(n);
1093 }
1094 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001095 boolean hasMultipleUsers =
1096 ((UserManager) getSystemService(Context.USER_SERVICE))
1097 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001098 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001099 || (!UserManager.supportsMultipleUsers()
1100 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001101 || Utils.isMonkeyRunning()) {
1102 category.removeTile(n);
1103 }
1104 } else if (id == R.id.nfc_payment_settings) {
1105 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1106 category.removeTile(n);
1107 } else {
1108 // Only show if NFC is on and we have the HCE feature
1109 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1110 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1111 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1112 category.removeTile(n);
1113 }
1114 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001115 } else if (id == R.id.print_settings) {
1116 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1117 PackageManager.FEATURE_PRINTING);
1118 if (!hasPrintingSupport) {
1119 category.removeTile(n);
1120 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001121 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001122 if (!showDev || um.hasUserRestriction(
1123 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001124 category.removeTile(n);
1125 }
1126 } else if (id == R.id.account_add) {
1127 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1128 category.removeTile(n);
1129 }
1130 }
1131
1132 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001133 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1134 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001135 category.removeTile(n);
1136 }
1137
1138 n--;
1139 }
1140 }
1141 }
1142
1143 private boolean updateHomeSettingTiles(DashboardTile tile) {
1144 // Once we decide to show Home settings, keep showing it forever
1145 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1146 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1147 return true;
1148 }
1149
1150 try {
1151 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1152 getPackageManager().getHomeActivities(homeApps);
1153 if (homeApps.size() < 2) {
1154 // When there's only one available home app, omit this settings
1155 // category entirely at the top level UI. If the user just
1156 // uninstalled the penultimate home app candidiate, we also
1157 // now tell them about why they aren't seeing 'Home' in the list.
1158 if (sShowNoHomeNotice) {
1159 sShowNoHomeNotice = false;
1160 NoHomeDialogFragment.show(this);
1161 }
1162 return false;
1163 } else {
1164 // Okay, we're allowing the Home settings category. Tell it, when
1165 // invoked via this front door, that we'll need to be told about the
1166 // case when the user uninstalls all but one home app.
1167 if (tile.fragmentArguments == null) {
1168 tile.fragmentArguments = new Bundle();
1169 }
1170 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1171 }
1172 } catch (Exception e) {
1173 // Can't look up the home activity; bail on configuring the icon
1174 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1175 }
1176
1177 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1178 return true;
1179 }
1180
1181 private void insertAccountsTiles(DashboardCategory target) {
1182 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1183 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1184 for (String accountType : accountTypes) {
1185 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1186 if (label == null) {
1187 continue;
1188 }
1189
1190 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1191 boolean skipToAccount = accounts.length == 1
1192 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1193 DashboardTile accountTile = new DashboardTile();
1194 accountTile.title = label;
1195 if (accountTile.extras == null) {
1196 accountTile.extras = new Bundle();
1197 }
1198 if (skipToAccount) {
1199 accountTile.fragment = AccountSyncSettings.class.getName();
1200 accountTile.fragmentArguments = new Bundle();
1201 // Need this for the icon
1202 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1203 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1204 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1205 accounts[0]);
1206 } else {
1207 accountTile.fragment = ManageAccountsSettings.class.getName();
1208 accountTile.fragmentArguments = new Bundle();
1209 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1210 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1211 accountType);
1212 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1213 label.toString());
1214 }
1215 dashboardTiles.add(accountTile);
1216 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1217 }
1218
1219 // Sort by label
1220 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1221 @Override
1222 public int compare(DashboardTile t1, DashboardTile t2) {
1223 return t1.title.toString().compareTo(t2.title.toString());
1224 }
1225 });
1226 int index = 0;
1227 for (DashboardTile tile : dashboardTiles) {
1228 target.addTile(index, tile);
1229 index++;
1230 }
1231 if (!mListeningToAccountUpdates) {
1232 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1233 mListeningToAccountUpdates = true;
1234 }
1235 }
1236
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001237 private void getMetaData() {
1238 try {
1239 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1240 PackageManager.GET_META_DATA);
1241 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001242 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1243 } catch (NameNotFoundException nnfe) {
1244 // No recovery
1245 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1246 }
1247 }
1248
1249 // give subclasses access to the Next button
1250 public boolean hasNextButton() {
1251 return mNextButton != null;
1252 }
1253
1254 public Button getNextButton() {
1255 return mNextButton;
1256 }
1257
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001258 @Override
1259 public boolean shouldUpRecreateTask(Intent targetIntent) {
1260 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1261 }
1262
1263 @Override
1264 public void onAccountsUpdated(Account[] accounts) {
1265 // TODO: watch for package upgrades to invalidate cache; see 7206643
1266 mAuthenticatorHelper.updateAuthDescriptions(this);
1267 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001268 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001269 }
1270
1271 public static void requestHomeNotice() {
1272 sShowNoHomeNotice = true;
1273 }
1274
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001275 @Override
1276 public boolean onQueryTextSubmit(String query) {
1277 switchToSearchResultsFragmentIfNeeded();
1278 mSearchQuery = query;
1279 return mSearchResultsFragment.onQueryTextSubmit(query);
1280 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001281
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001282 @Override
1283 public boolean onQueryTextChange(String newText) {
1284 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001285 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001286 return false;
1287 }
1288 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001289 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001290
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001291 @Override
1292 public boolean onClose() {
1293 return false;
1294 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001295
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001296 @Override
1297 public boolean onMenuItemActionExpand(MenuItem item) {
1298 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001299 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001300 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001301 return true;
1302 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001303
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001304 @Override
1305 public boolean onMenuItemActionCollapse(MenuItem item) {
1306 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001307 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001308 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001309 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001310 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001311 return true;
1312 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001313
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001314 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001315 if (mSearchResultsFragment != null) {
1316 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001317 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001318 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1319 if (current != null && current instanceof SearchResultsSummary) {
1320 mSearchResultsFragment = (SearchResultsSummary) current;
1321 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001322 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001323 SearchResultsSummary.class.getName(), null, false, true,
1324 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001325 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001326 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001327 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001328 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001329
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001330 public void needToRevertToInitialFragment() {
1331 mNeedToRevertToInitialFragment = true;
1332 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001333
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001334 private void revertToInitialFragment() {
1335 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001336 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001337 mSearchMenuItemExpanded = false;
1338 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1339 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001340 if (mSearchMenuItem != null) {
1341 mSearchMenuItem.collapseActionView();
1342 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001343 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001344}