blob: 89231df5e52be3bf453ec713e978723ae9aa8151 [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
Jason Monk2ebc8a02015-02-13 15:23:19 -050019import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
20
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.ActionBar;
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.nfc.NfcAdapter;
40import android.os.Bundle;
41import android.os.Handler;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080043import android.os.UserHandle;
44import android.os.UserManager;
45import android.preference.Preference;
46import android.preference.PreferenceFragment;
47import android.preference.PreferenceManager;
48import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050051import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.AttributeSet;
53import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.TypedValue;
56import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070057import android.view.Menu;
58import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080059import android.view.MenuItem;
60import android.view.View;
61import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070062import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080063import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070064import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040065
Chris Wren8a963ba2015-03-20 10:29:14 -040066import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080067import com.android.internal.util.ArrayUtils;
68import com.android.internal.util.XmlUtils;
69import com.android.settings.accessibility.AccessibilitySettings;
70import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000071import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070073import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.applications.ManageApplications;
Xiyuan Xia86a55402015-06-02 14:55:32 -070075import com.android.settings.applications.ManageAssist;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040077import com.android.settings.applications.UsageAccessDetails;
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;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070084import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070085import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070086import com.android.settings.deviceinfo.PublicVolumeSettings;
87import com.android.settings.deviceinfo.StorageSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040088import com.android.settings.fuelgauge.BatterySaverSettings;
Jason Monk1eb54eb2015-04-29 12:46:42 -040089import com.android.settings.fuelgauge.PowerUsageDetail;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.fuelgauge.PowerUsageSummary;
91import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
92import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
93import com.android.settings.inputmethod.SpellCheckersSettings;
94import com.android.settings.inputmethod.UserDictionaryList;
95import com.android.settings.location.LocationSettings;
96import com.android.settings.nfc.AndroidBeam;
97import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040098import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -040099import com.android.settings.notification.NotificationAccessSettings;
100import com.android.settings.notification.NotificationSettings;
101import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500102import com.android.settings.notification.OtherSoundSettings;
John Spurlock08531a82015-05-07 17:45:43 -0400103import com.android.settings.notification.ZenAccessSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400104import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400105import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400106import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400107import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400108import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.print.PrintJobSettingsFragment;
110import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500111import com.android.settings.search.DynamicIndexableContentMonitor;
112import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700113import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import com.android.settings.tts.TextToSpeechSettings;
115import com.android.settings.users.UserSettings;
116import com.android.settings.vpn2.VpnSettings;
117import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700118import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700120import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import com.android.settings.wifi.WifiSettings;
122import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500123
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124import org.xmlpull.v1.XmlPullParser;
125import org.xmlpull.v1.XmlPullParserException;
126
127import java.io.IOException;
128import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800129import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500130import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700131import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133public class SettingsActivity extends Activity
134 implements PreferenceManager.OnPreferenceTreeClickListener,
135 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100136 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100138 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800139
140 private static final String LOG_TAG = "Settings";
141
142 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700143 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700144 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
145 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700146 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700147 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700148 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149
150 /**
151 * When starting this activity, the invoking Intent can contain this extra
152 * string to specify which fragment should be initially displayed.
153 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
154 * will call isValidFragment() to confirm that the fragment class name is valid for this
155 * activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
158
159 /**
160 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
161 * this extra can also be specified to supply a Bundle of arguments to pass
162 * to that fragment when it is instantiated during the initial creation
163 * of the activity.
164 */
165 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
166
167 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700168 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
169 */
170 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
171
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800172 public static final String BACK_STACK_PREFS = ":settings:prefs";
173
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174 // extras that allow any preference activity to be launched as part of a wizard
175
176 // show Back and Next buttons? takes boolean parameter
177 // Back will then return RESULT_CANCELED and Next RESULT_OK
178 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
179
180 // add a Skip button?
181 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
182
183 // specify custom text for the Back or Next buttons, or cause a button to not appear
184 // at all by setting it to null
185 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
186 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
187
188 /**
189 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700190 * 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 -0800191 * that fragment.
192 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700193 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100194 /**
195 * The package name used to resolve the title resource id.
196 */
197 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
198 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700199 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
200 ":settings:show_fragment_title_resid";
201 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
202 ":settings:show_fragment_as_shortcut";
203
204 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
205 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207 private static final String META_DATA_KEY_FRAGMENT_CLASS =
208 "com.android.settings.FRAGMENT_CLASS";
209
210 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
211
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700212 private static final String EMPTY_QUERY = "";
213
Jason Monk2ebc8a02015-02-13 15:23:19 -0500214 /**
215 * Settings will search for system activities of this action and add them as a top level
216 * settings tile using the following parameters.
217 *
218 * <p>A category must be specified in the meta-data for the activity named
219 * {@link #EXTRA_CATEGORY_KEY}
220 *
221 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
222 * otherwise the label for the activity will be used.
223 *
224 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
225 * otherwise the icon for the activity will be used.
226 *
227 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
228 */
229 private static final String EXTRA_SETTINGS_ACTION =
230 "com.android.settings.action.EXTRA_SETTINGS";
231
232 /**
233 * The key used to get the category from metadata of activities of action
234 * {@link #EXTRA_SETTINGS_ACTION}
235 * The value must be one of:
236 * <li>com.android.settings.category.wireless</li>
237 * <li>com.android.settings.category.device</li>
238 * <li>com.android.settings.category.personal</li>
239 * <li>com.android.settings.category.system</li>
240 */
241 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
242
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243 private static boolean sShowNoHomeNotice = false;
244
245 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800247 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700248 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800249
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 // Show only these settings for restricted users
251 private int[] SETTINGS_FOR_RESTRICTED = {
252 R.id.wireless_section,
253 R.id.wifi_settings,
254 R.id.bluetooth_settings,
255 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700256 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 R.id.wireless_settings,
258 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400259 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 R.id.display_settings,
261 R.id.storage_settings,
262 R.id.application_settings,
263 R.id.battery_settings,
264 R.id.personal_section,
265 R.id.location_settings,
266 R.id.security_settings,
267 R.id.language_settings,
268 R.id.user_settings,
269 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270 R.id.system_section,
271 R.id.date_time_settings,
272 R.id.about_settings,
273 R.id.accessibility_settings,
274 R.id.print_settings,
275 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800276 R.id.home_settings,
277 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 };
279
280 private static final String[] ENTRY_FRAGMENTS = {
281 WirelessSettings.class.getName(),
282 WifiSettings.class.getName(),
283 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700284 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700286 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 TetherSettings.class.getName(),
288 WifiP2pSettings.class.getName(),
289 VpnSettings.class.getName(),
290 DateTimeSettings.class.getName(),
291 LocalePicker.class.getName(),
292 InputMethodAndLanguageSettings.class.getName(),
293 SpellCheckersSettings.class.getName(),
294 UserDictionaryList.class.getName(),
295 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700296 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 DisplaySettings.class.getName(),
298 DeviceInfoSettings.class.getName(),
299 ManageApplications.class.getName(),
Xiyuan Xia86a55402015-06-02 14:55:32 -0700300 ManageAssist.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 ProcessStatsUi.class.getName(),
302 NotificationStation.class.getName(),
303 LocationSettings.class.getName(),
304 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400305 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 PrivacySettings.class.getName(),
307 DeviceAdminSettings.class.getName(),
308 AccessibilitySettings.class.getName(),
309 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
311 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700312 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700313 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700314 PrivateVolumeSettings.class.getName(),
315 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800317 AndroidBeam.class.getName(),
318 WifiDisplaySettings.class.getName(),
319 PowerUsageSummary.class.getName(),
320 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000321 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 CryptKeeperSettings.class.getName(),
323 DataUsageSummary.class.getName(),
324 DreamSettings.class.getName(),
325 UserSettings.class.getName(),
326 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400327 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800328 PrintSettingsFragment.class.getName(),
329 PrintJobSettingsFragment.class.getName(),
330 TrustedCredentialsSettings.class.getName(),
331 PaymentSettings.class.getName(),
332 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700333 ZenModeSettings.class.getName(),
334 NotificationSettings.class.getName(),
335 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
336 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400337 InstalledAppDetails.class.getName(),
338 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700339 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700340 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000341 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400342 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400343 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400344 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400345 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400346 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400347 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400348 PowerUsageDetail.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 };
350
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700351
352 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
353 "android.settings.APPLICATION_DETAILS_SETTINGS"
354 };
355
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 private SharedPreferences mDevelopmentPreferences;
357 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
358
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 private boolean mBatteryPresent = true;
360 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 @Override
362 public void onReceive(Context context, Intent intent) {
363 String action = intent.getAction();
364 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
365 boolean batteryPresent = Utils.isBatteryPresent(intent);
366
367 if (mBatteryPresent != batteryPresent) {
368 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700369 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 }
371 }
372 }
373 };
374
Svetoslav990159a2014-04-14 17:14:59 -0700375 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
376 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700377
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700378 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700379 private SwitchBar mSwitchBar;
380
381 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700382
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700383 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700384 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700385
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700386 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700387 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700388
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700389 private ViewGroup mContent;
390
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700391 private SearchView mSearchView;
392 private MenuItem mSearchMenuItem;
393 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700394 private SearchResultsSummary mSearchResultsFragment;
395 private String mSearchQuery;
396
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700397 // Categories
398 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800399
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700400 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700401 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402 private Handler mHandler = new Handler() {
403 @Override
404 public void handleMessage(Message msg) {
405 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700406 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700407 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
408 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700409 buildDashboardCategories(mCategories);
410 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 } break;
412 }
413 }
414 };
415
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700416 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700417 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700418
Jim Miller0698a212014-10-16 19:49:07 -0700419 private Intent mResultIntentData;
420
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700421 public SwitchBar getSwitchBar() {
422 return mSwitchBar;
423 }
424
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700425 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
426 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700427 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700428 }
429 return mCategories;
430 }
431
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800432 @Override
433 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
434 // Override the fragment title for Wallpaper settings
435 int titleRes = pref.getTitleRes();
436 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
437 titleRes = R.string.wallpaper_settings_fragment_title;
438 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
439 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
440 if (UserManager.get(this).isLinkedUser()) {
441 titleRes = R.string.profile_info_settings_title;
442 } else {
443 titleRes = R.string.user_info_settings_title;
444 }
445 }
446 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
447 null, 0);
448 return true;
449 }
450
451 @Override
452 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
453 return false;
454 }
455
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700456 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700457 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700458 Message msg = new Message();
459 msg.what = MSG_BUILD_CATEGORIES;
460 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800461 }
462 }
463
464 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465 public void onConfigurationChanged(Configuration newConfig) {
466 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800467 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800468 }
469
470 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700471 protected void onStart() {
472 super.onStart();
473
474 if (mNeedToRevertToInitialFragment) {
475 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800477 }
478
479 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700480 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700481 if (!mDisplaySearch) {
482 return false;
483 }
484
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700485 MenuInflater inflater = getMenuInflater();
486 inflater.inflate(R.menu.options_menu, menu);
487
488 // Cache the search query (can be overriden by the OnQueryTextListener)
489 final String query = mSearchQuery;
490
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700491 mSearchMenuItem = menu.findItem(R.id.search);
492 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700493
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700494 if (mSearchMenuItem == null || mSearchView == null) {
495 return false;
496 }
497
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700498 if (mSearchResultsFragment != null) {
499 mSearchResultsFragment.setSearchView(mSearchView);
500 }
501
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700502 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700503 mSearchView.setOnQueryTextListener(this);
504 mSearchView.setOnCloseListener(this);
505
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700506 if (mSearchMenuItemExpanded) {
507 mSearchMenuItem.expandActionView();
508 }
509 mSearchView.setQuery(query, true /* submit */);
510
511 return true;
512 }
513
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700514 private static boolean isShortCutIntent(final Intent intent) {
515 Set<String> categories = intent.getCategories();
516 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
517 }
518
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700519 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700520 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700521 if (action == null) {
522 return false;
523 }
524 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
525 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
526 }
527 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700528 }
529
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700530 @Override
531 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700532 super.onCreate(savedState);
533
534 // Should happen before any call to getIntent()
535 getMetaData();
536
537 final Intent intent = getIntent();
538 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
539 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
543 Context.MODE_PRIVATE);
544
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700545 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700546 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700547
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700548 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700549 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
550
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700551 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700552 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700553
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700554 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700555
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700556 // This is a "Sub Settings" when:
557 // - this is a real SubSettings
558 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700559 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700560 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
561
562 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
563 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700564 // Check also that we are not a Theme Dialog as we don't want to override them
565 final int themeResId = getThemeResId();
566 if (themeResId != R.style.Theme_DialogWhenLarge &&
567 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
568 setTheme(R.style.Theme_SubSettings);
569 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700570 }
571
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700572 setContentView(mIsShowingDashboard ?
573 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700575 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700576
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 getFragmentManager().addOnBackStackChangedListener(this);
578
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700579 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800580 // Run the Index update only if we have some space
581 if (!Utils.isLowStorage(this)) {
582 Index.getInstance(getApplicationContext()).update();
583 } else {
584 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
585 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700586 }
587
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700588 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700589 // We are restarting from a previous saved state; used that to initialize, instead
590 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700591 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
592 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800593
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700594 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800595
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700596 ArrayList<DashboardCategory> categories =
597 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
598 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700599 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700600 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700601 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800602 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700603
604 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700605 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700606 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
607 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800608 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700609 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400610 mDisplaySearch = false;
611 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700612 if (mIsShortcut) {
613 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700614 } else if (isSubSettings) {
615 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700616 } else {
617 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700618 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700619 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700620
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700621 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700622 switchToFragment(initialFragmentName, initialArguments, true, false,
623 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000624 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700625 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700626 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700627 // Show Search affordance
628 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700629 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700630 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700631 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800632 }
633 }
634
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700635 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700636 if (mActionBar != null) {
637 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
638 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
639 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700640 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
641
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800643 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
644
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700645 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 if (buttonBar != null) {
647 buttonBar.setVisibility(View.VISIBLE);
648
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700649 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 backButton.setOnClickListener(new OnClickListener() {
651 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700652 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 finish();
654 }
655 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700656 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 skipButton.setOnClickListener(new OnClickListener() {
658 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700659 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 finish();
661 }
662 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700663 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800664 mNextButton.setOnClickListener(new OnClickListener() {
665 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700666 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800667 finish();
668 }
669 });
670
671 // set our various button parameters
672 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
673 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
674 if (TextUtils.isEmpty(buttonText)) {
675 mNextButton.setVisibility(View.GONE);
676 }
677 else {
678 mNextButton.setText(buttonText);
679 }
680 }
681 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
682 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
683 if (TextUtils.isEmpty(buttonText)) {
684 backButton.setVisibility(View.GONE);
685 }
686 else {
687 backButton.setText(buttonText);
688 }
689 }
690 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
691 skipButton.setVisibility(View.VISIBLE);
692 }
693 }
694 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700695
696 mHomeActivitiesCount = getHomeActivitiesCount();
697 }
698
699 private int getHomeActivitiesCount() {
700 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
701 getPackageManager().getHomeActivities(homeApps);
702 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800703 }
704
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700705 private void setTitleFromIntent(Intent intent) {
706 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
707 if (initialTitleResId > 0) {
708 mInitialTitle = null;
709 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100710
711 final String initialTitleResPackageName = intent.getStringExtra(
712 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
713 if (initialTitleResPackageName != null) {
714 try {
715 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
716 0 /* flags */, new UserHandle(UserHandle.myUserId()));
717 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
718 setTitle(mInitialTitle);
719 mInitialTitleResId = -1;
720 return;
721 } catch (NameNotFoundException e) {
722 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
723 }
724 } else {
725 setTitle(mInitialTitleResId);
726 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700727 } else {
728 mInitialTitleResId = -1;
729 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
730 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
731 setTitle(mInitialTitle);
732 }
733 }
734
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800735 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800736 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700737 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800738 }
739
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700740 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800741 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700742
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800743 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700744 if (mInitialTitleResId > 0) {
745 setTitle(mInitialTitleResId);
746 } else {
747 setTitle(mInitialTitle);
748 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700749 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800750 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700751
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800752 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
753 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700754
755 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800756 }
757
758 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
759 final CharSequence title;
760 final int titleRes = bse.getBreadCrumbTitleRes();
761 if (titleRes > 0) {
762 title = getText(titleRes);
763 } else {
764 title = bse.getBreadCrumbTitle();
765 }
766 if (title != null) {
767 setTitle(title);
768 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800769 }
770
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800771 @Override
772 protected void onSaveInstanceState(Bundle outState) {
773 super.onSaveInstanceState(outState);
774
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700775 if (mCategories.size() > 0) {
776 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800777 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700778
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700779 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700780 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700781
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700782 if (mDisplaySearch) {
783 // The option menus are created if the ActionBar is visible and they are also created
784 // asynchronously. If you launch Settings with an Intent action like
785 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
786 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
787 // menu item and search view are null.
788 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
789 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700790
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700791 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
792 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
793 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700794
795 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800796 }
797
798 @Override
799 public void onResume() {
800 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400801 if (mIsShowingDashboard) {
802 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
803 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800804
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700805 final int newHomeActivityCount = getHomeActivitiesCount();
806 if (newHomeActivityCount != mHomeActivitiesCount) {
807 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700808 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700809 }
810
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800811 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
812 @Override
813 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700814 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800815 }
816 };
817 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
818 mDevelopmentPreferencesListener);
819
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800820 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700821
Svetoslav990159a2014-04-14 17:14:59 -0700822 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700823
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700824 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700825 onQueryTextSubmit(mSearchQuery);
826 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800827 }
828
829 @Override
830 public void onPause() {
831 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400832 if (mIsShowingDashboard) {
833 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
834 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800835 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700836 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800837 }
838
839 @Override
840 public void onDestroy() {
841 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700842
843 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
844 mDevelopmentPreferencesListener);
845 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800846 }
847
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800848 protected boolean isValidFragment(String fragmentName) {
849 // Almost all fragments are wrapped in this,
850 // except for a few that have their own activities.
851 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
852 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
853 }
854 return false;
855 }
856
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800857 @Override
858 public Intent getIntent() {
859 Intent superIntent = super.getIntent();
860 String startingFragment = getStartingFragmentClass(superIntent);
861 // This is called from super.onCreate, isMultiPane() is not yet reliable
862 // Do not use onIsHidingHeaders either, which relies itself on this method
863 if (startingFragment != null) {
864 Intent modIntent = new Intent(superIntent);
865 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
866 Bundle args = superIntent.getExtras();
867 if (args != null) {
868 args = new Bundle(args);
869 } else {
870 args = new Bundle();
871 }
872 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100873 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800874 return modIntent;
875 }
876 return superIntent;
877 }
878
879 /**
880 * Checks if the component name in the intent is different from the Settings class and
881 * returns the class name to load as a fragment.
882 */
883 private String getStartingFragmentClass(Intent intent) {
884 if (mFragmentClass != null) return mFragmentClass;
885
886 String intentClass = intent.getComponent().getClassName();
887 if (intentClass.equals(getClass().getName())) return null;
888
889 if ("com.android.settings.ManageApplications".equals(intentClass)
890 || "com.android.settings.RunningServices".equals(intentClass)
891 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
892 // Old names of manage apps.
893 intentClass = com.android.settings.applications.ManageApplications.class.getName();
894 }
895
896 return intentClass;
897 }
898
899 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000900 * Start a new fragment containing a preference panel. If the preferences
901 * are being displayed in multi-pane mode, the given fragment class will
902 * be instantiated and placed in the appropriate pane. If running in
903 * single-pane mode, a new activity will be launched in which to show the
904 * fragment.
905 *
906 * @param fragmentClass Full name of the class implementing the fragment.
907 * @param args Any desired arguments to supply to the fragment.
908 * @param titleRes Optional resource identifier of the title of this
909 * fragment.
910 * @param titleText Optional text of the title of this fragment.
911 * @param resultTo Optional fragment that result data should be sent to.
912 * If non-null, resultTo.onActivityResult() will be called when this
913 * preference panel is done. The launched panel must use
914 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
915 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700916 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000917 */
918 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700919 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700920 String title = null;
921 if (titleRes < 0) {
922 if (titleText != null) {
923 title = titleText.toString();
924 } else {
925 // There not much we can do in that case
926 title = "";
927 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700928 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700929 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700930 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000931 }
932
933 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100934 * Start a new fragment in a new activity containing a preference panel for a given user. If the
935 * preferences are being displayed in multi-pane mode, the given fragment class will be
936 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
937 * activity will be launched in which to show the fragment.
938 *
939 * @param fragmentClass Full name of the class implementing the fragment.
940 * @param args Any desired arguments to supply to the fragment.
941 * @param titleRes Optional resource identifier of the title of this fragment.
942 * @param titleText Optional text of the title of this fragment.
943 * @param userHandle The user for which the panel has to be started.
944 */
945 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
946 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700947 // This is a workaround.
948 //
949 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
950 // starting the fragment could cause a native stack corruption. See b/17523189. However,
951 // adding that flag and start the preference panel with the same UserHandler will make it
952 // impossible to use back button to return to the previous screen. See b/20042570.
953 //
954 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
955 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
956 // when we're calling it as the same user.
957 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
958 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
959 } else {
960 String title = null;
961 if (titleRes < 0) {
962 if (titleText != null) {
963 title = titleText.toString();
964 } else {
965 // There not much we can do in that case
966 title = "";
967 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100968 }
Lifu Tangd0332852015-04-02 12:05:46 -0700969 Utils.startWithFragmentAsUser(this, fragmentClass, args,
970 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100971 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100972 }
973
974 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800975 * Called by a preference panel fragment to finish itself.
976 *
977 * @param caller The fragment that is asking to be finished.
978 * @param resultCode Optional result code to send back to the original
979 * launching fragment.
980 * @param resultData Optional result data to send back to the original
981 * launching fragment.
982 */
983 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
984 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700985 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800986 }
987
988 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000989 * Start a new fragment.
990 *
991 * @param fragment The fragment to start
992 * @param push If true, the current fragment will be pushed onto the back stack. If false,
993 * the current fragment will be replaced.
994 */
995 public void startPreferenceFragment(Fragment fragment, boolean push) {
996 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700997 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000998 if (push) {
999 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1000 transaction.addToBackStack(BACK_STACK_PREFS);
1001 } else {
1002 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1003 }
1004 transaction.commitAllowingStateLoss();
1005 }
1006
1007 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001008 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1009 */
1010 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001011 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001012 if (validate && !isValidFragment(fragmentName)) {
1013 throw new IllegalArgumentException("Invalid fragment for this activity: "
1014 + fragmentName);
1015 }
1016 Fragment f = Fragment.instantiate(this, fragmentName, args);
1017 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001018 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001019 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001020 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001021 }
1022 if (addToBackStack) {
1023 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1024 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001025 if (titleResId > 0) {
1026 transaction.setBreadCrumbTitle(titleResId);
1027 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001028 transaction.setBreadCrumbTitle(title);
1029 }
1030 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001031 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001032 return f;
1033 }
1034
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001035 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001036 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001037 *
1038 * @param categories The list in which to place the tiles categories.
1039 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001040 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001041 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001042 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1043 updateTilesList(categories);
1044 }
1045
1046 /**
1047 * Parse the given XML file as a categories description, adding each
1048 * parsed categories and tiles into the target list.
1049 *
1050 * @param resid The XML resource to load and parse.
1051 * @param target The list in which the parsed categories and tiles should be placed.
1052 */
1053 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1054 XmlResourceParser parser = null;
1055 try {
1056 parser = getResources().getXml(resid);
1057 AttributeSet attrs = Xml.asAttributeSet(parser);
1058
1059 int type;
1060 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1061 && type != XmlPullParser.START_TAG) {
1062 // Parse next until start tag is found
1063 }
1064
1065 String nodeName = parser.getName();
1066 if (!"dashboard-categories".equals(nodeName)) {
1067 throw new RuntimeException(
1068 "XML document must start with <preference-categories> tag; found"
1069 + nodeName + " at " + parser.getPositionDescription());
1070 }
1071
1072 Bundle curBundle = null;
1073
1074 final int outerDepth = parser.getDepth();
1075 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1076 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1077 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1078 continue;
1079 }
1080
1081 nodeName = parser.getName();
1082 if ("dashboard-category".equals(nodeName)) {
1083 DashboardCategory category = new DashboardCategory();
1084
1085 TypedArray sa = obtainStyledAttributes(
1086 attrs, com.android.internal.R.styleable.PreferenceHeader);
1087 category.id = sa.getResourceId(
1088 com.android.internal.R.styleable.PreferenceHeader_id,
1089 (int)DashboardCategory.CAT_ID_UNDEFINED);
1090
1091 TypedValue tv = sa.peekValue(
1092 com.android.internal.R.styleable.PreferenceHeader_title);
1093 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1094 if (tv.resourceId != 0) {
1095 category.titleRes = tv.resourceId;
1096 } else {
1097 category.title = tv.string;
1098 }
1099 }
1100 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001101 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1102 tv = sa.peekValue(
1103 com.android.internal.R.styleable.Preference_key);
1104 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1105 if (tv.resourceId != 0) {
1106 category.key = getString(tv.resourceId);
1107 } else {
1108 category.key = tv.string.toString();
1109 }
1110 }
1111 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001112
1113 final int innerDepth = parser.getDepth();
1114 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1115 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1116 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1117 continue;
1118 }
1119
1120 String innerNodeName = parser.getName();
1121 if (innerNodeName.equals("dashboard-tile")) {
1122 DashboardTile tile = new DashboardTile();
1123
1124 sa = obtainStyledAttributes(
1125 attrs, com.android.internal.R.styleable.PreferenceHeader);
1126 tile.id = sa.getResourceId(
1127 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001128 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001129 tv = sa.peekValue(
1130 com.android.internal.R.styleable.PreferenceHeader_title);
1131 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1132 if (tv.resourceId != 0) {
1133 tile.titleRes = tv.resourceId;
1134 } else {
1135 tile.title = tv.string;
1136 }
1137 }
1138 tv = sa.peekValue(
1139 com.android.internal.R.styleable.PreferenceHeader_summary);
1140 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1141 if (tv.resourceId != 0) {
1142 tile.summaryRes = tv.resourceId;
1143 } else {
1144 tile.summary = tv.string;
1145 }
1146 }
1147 tile.iconRes = sa.getResourceId(
1148 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1149 tile.fragment = sa.getString(
1150 com.android.internal.R.styleable.PreferenceHeader_fragment);
1151 sa.recycle();
1152
1153 if (curBundle == null) {
1154 curBundle = new Bundle();
1155 }
1156
1157 final int innerDepth2 = parser.getDepth();
1158 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1159 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1160 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1161 continue;
1162 }
1163
1164 String innerNodeName2 = parser.getName();
1165 if (innerNodeName2.equals("extra")) {
1166 getResources().parseBundleExtra("extra", attrs, curBundle);
1167 XmlUtils.skipCurrentTag(parser);
1168
1169 } else if (innerNodeName2.equals("intent")) {
1170 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1171
1172 } else {
1173 XmlUtils.skipCurrentTag(parser);
1174 }
1175 }
1176
1177 if (curBundle.size() > 0) {
1178 tile.fragmentArguments = curBundle;
1179 curBundle = null;
1180 }
1181
PauloftheWest38155612014-06-30 10:02:36 -07001182 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001183 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001184 category.addTile(tile);
1185 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001186
Jason Monk2ebc8a02015-02-13 15:23:19 -05001187 } else if (innerNodeName.equals("external-tiles")) {
1188 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001189 } else {
1190 XmlUtils.skipCurrentTag(parser);
1191 }
1192 }
1193
1194 target.add(category);
1195 } else {
1196 XmlUtils.skipCurrentTag(parser);
1197 }
1198 }
1199
1200 } catch (XmlPullParserException e) {
1201 throw new RuntimeException("Error parsing categories", e);
1202 } catch (IOException e) {
1203 throw new RuntimeException("Error parsing categories", e);
1204 } finally {
1205 if (parser != null) parser.close();
1206 }
1207 }
1208
1209 private void updateTilesList(List<DashboardCategory> target) {
1210 final boolean showDev = mDevelopmentPreferences.getBoolean(
1211 DevelopmentSettings.PREF_SHOW,
1212 android.os.Build.TYPE.equals("eng"));
1213
1214 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1215
1216 final int size = target.size();
1217 for (int i = 0; i < size; i++) {
1218
1219 DashboardCategory category = target.get(i);
1220
1221 // Ids are integers, so downcasting is ok
1222 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001223 int n = category.getTilesCount() - 1;
1224 while (n >= 0) {
1225
1226 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001227 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001228 id = (int) tile.id;
1229 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001230 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1231 removeTile = true;
1232 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001233 } else if (id == R.id.wifi_settings) {
1234 // Remove WiFi Settings if WiFi service is not available.
1235 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001236 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001237 }
1238 } else if (id == R.id.bluetooth_settings) {
1239 // Remove Bluetooth Settings if Bluetooth service is not available.
1240 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001241 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001242 }
1243 } else if (id == R.id.data_usage_settings) {
1244 // Remove data usage when kernel module not enabled
Jason Monkb45e27b2015-05-20 13:35:43 -04001245 if (!Utils.isBandwidthControlEnabled()) {
1246 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001247 }
1248 } else if (id == R.id.battery_settings) {
1249 // Remove battery settings when battery is not available. (e.g. TV)
1250
1251 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001252 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001253 }
1254 } else if (id == R.id.home_settings) {
1255 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001256 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001257 }
1258 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001259 boolean hasMultipleUsers =
1260 ((UserManager) getSystemService(Context.USER_SERVICE))
1261 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001262 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001263 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001264 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001265 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001266 }
1267 } else if (id == R.id.nfc_payment_settings) {
1268 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001269 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001270 } else {
1271 // Only show if NFC is on and we have the HCE feature
1272 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001273 if (adapter == null || !adapter.isEnabled() ||
1274 !getPackageManager().hasSystemFeature(
1275 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001276 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001277 }
1278 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001279 } else if (id == R.id.print_settings) {
1280 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1281 PackageManager.FEATURE_PRINTING);
1282 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001283 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001284 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001285 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001286 if (!showDev || um.hasUserRestriction(
1287 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001288 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001289 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001290 }
1291
1292 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001293 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1294 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001295 }
1296
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001297 if (removeTile && n < category.getTilesCount()) {
1298 category.removeTile(n);
1299 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001300 n--;
1301 }
1302 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001303 addExternalTiles(target);
1304 }
1305
1306 private void addExternalTiles(List<DashboardCategory> target) {
1307 Map<Pair<String, String>, DashboardTile> addedCache =
1308 new ArrayMap<Pair<String, String>, DashboardTile>();
1309 UserManager userManager = UserManager.get(this);
1310 for (UserHandle user : userManager.getUserProfiles()) {
1311 addExternalTiles(target, user, addedCache);
1312 }
1313 }
1314
1315 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1316 Map<Pair<String, String>, DashboardTile> addedCache) {
1317 PackageManager pm = getPackageManager();
1318 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1319 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1320 PackageManager.GET_META_DATA, user.getIdentifier());
1321 for (ResolveInfo resolved : results) {
1322 if (!resolved.system) {
1323 // Do not allow any app to add to settings, only system ones.
1324 continue;
1325 }
1326 ActivityInfo activityInfo = resolved.activityInfo;
1327 Bundle metaData = activityInfo.metaData;
1328 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1329 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1330 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1331 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1332 continue;
1333 }
1334 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1335 DashboardCategory category = getCategory(target, categoryKey);
1336 if (category == null) {
1337 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1338 + "category key " + categoryKey);
1339 continue;
1340 }
1341 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1342 activityInfo.name);
1343 DashboardTile tile = addedCache.get(key);
1344 if (tile == null) {
1345 tile = new DashboardTile();
1346 tile.intent = new Intent().setClassName(
1347 activityInfo.packageName, activityInfo.name);
1348 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1349
1350 if (category.externalIndex == -1) {
1351 // If no location for external tiles has been specified for this category,
1352 // then just put them at the end.
1353 category.addTile(tile);
1354 } else {
1355 category.addTile(category.externalIndex, tile);
1356 }
1357 addedCache.put(key, tile);
1358 }
1359 tile.userHandle.add(user);
1360 }
1361 }
1362
1363 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1364 for (DashboardCategory category : target) {
1365 if (categoryKey.equals(category.key)) {
1366 return category;
1367 }
1368 }
1369 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001370 }
1371
1372 private boolean updateHomeSettingTiles(DashboardTile tile) {
1373 // Once we decide to show Home settings, keep showing it forever
1374 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1375 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1376 return true;
1377 }
1378
1379 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001380 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001381 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001382 // When there's only one available home app, omit this settings
1383 // category entirely at the top level UI. If the user just
1384 // uninstalled the penultimate home app candidiate, we also
1385 // now tell them about why they aren't seeing 'Home' in the list.
1386 if (sShowNoHomeNotice) {
1387 sShowNoHomeNotice = false;
1388 NoHomeDialogFragment.show(this);
1389 }
1390 return false;
1391 } else {
1392 // Okay, we're allowing the Home settings category. Tell it, when
1393 // invoked via this front door, that we'll need to be told about the
1394 // case when the user uninstalls all but one home app.
1395 if (tile.fragmentArguments == null) {
1396 tile.fragmentArguments = new Bundle();
1397 }
1398 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1399 }
1400 } catch (Exception e) {
1401 // Can't look up the home activity; bail on configuring the icon
1402 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1403 }
1404
1405 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1406 return true;
1407 }
1408
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001409 private void getMetaData() {
1410 try {
1411 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1412 PackageManager.GET_META_DATA);
1413 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001414 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1415 } catch (NameNotFoundException nnfe) {
1416 // No recovery
1417 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1418 }
1419 }
1420
1421 // give subclasses access to the Next button
1422 public boolean hasNextButton() {
1423 return mNextButton != null;
1424 }
1425
1426 public Button getNextButton() {
1427 return mNextButton;
1428 }
1429
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001430 @Override
1431 public boolean shouldUpRecreateTask(Intent targetIntent) {
1432 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1433 }
1434
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001435 public static void requestHomeNotice() {
1436 sShowNoHomeNotice = true;
1437 }
1438
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001439 @Override
1440 public boolean onQueryTextSubmit(String query) {
1441 switchToSearchResultsFragmentIfNeeded();
1442 mSearchQuery = query;
1443 return mSearchResultsFragment.onQueryTextSubmit(query);
1444 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001445
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001446 @Override
1447 public boolean onQueryTextChange(String newText) {
1448 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001449 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001450 return false;
1451 }
1452 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001453 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001454
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001455 @Override
1456 public boolean onClose() {
1457 return false;
1458 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001459
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001460 @Override
1461 public boolean onMenuItemActionExpand(MenuItem item) {
1462 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001463 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001464 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001465 return true;
1466 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001467
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001468 @Override
1469 public boolean onMenuItemActionCollapse(MenuItem item) {
1470 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001471 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001472 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001474 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 return true;
1476 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001477
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001478 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001479 if (mSearchResultsFragment != null) {
1480 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001481 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001482 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001483 if (current != null && current instanceof SearchResultsSummary) {
1484 mSearchResultsFragment = (SearchResultsSummary) current;
1485 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001486 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001487 SearchResultsSummary.class.getName(), null, false, true,
1488 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001489 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001490 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001491 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001492 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001493
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001494 public void needToRevertToInitialFragment() {
1495 mNeedToRevertToInitialFragment = true;
1496 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001497
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001498 private void revertToInitialFragment() {
1499 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001500 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001501 mSearchMenuItemExpanded = false;
1502 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1503 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001504 if (mSearchMenuItem != null) {
1505 mSearchMenuItem.collapseActionView();
1506 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001507 }
Jim Miller0698a212014-10-16 19:49:07 -07001508
1509 public Intent getResultIntentData() {
1510 return mResultIntentData;
1511 }
1512
1513 public void setResultIntentData(Intent resultIntentData) {
1514 mResultIntentData = resultIntentData;
1515 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001516}