blob: 8edec09f5f5dab97b864f5a5973df72c2e01112e [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;
Billy Laufee78562015-07-27 12:57:07 +010073import com.android.settings.applications.DrawOverlayDetails;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070074import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.applications.ManageApplications;
Xiyuan Xia86a55402015-06-02 14:55:32 -070076import com.android.settings.applications.ManageAssist;
Jason Monk2cdafc62015-06-12 12:32:50 -040077import com.android.settings.applications.ProcessStatsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040079import com.android.settings.applications.UsageAccessDetails;
Billy Laufee78562015-07-27 12:57:07 +010080import com.android.settings.applications.WriteSettingsDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070082import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070084import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070085import com.android.settings.dashboard.NoHomeDialogFragment;
86import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070087import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070088import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070089import com.android.settings.deviceinfo.PublicVolumeSettings;
90import com.android.settings.deviceinfo.StorageSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040091import com.android.settings.fuelgauge.BatterySaverSettings;
Jason Monk1eb54eb2015-04-29 12:46:42 -040092import com.android.settings.fuelgauge.PowerUsageDetail;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080093import com.android.settings.fuelgauge.PowerUsageSummary;
94import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
95import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
96import com.android.settings.inputmethod.SpellCheckersSettings;
97import com.android.settings.inputmethod.UserDictionaryList;
98import com.android.settings.location.LocationSettings;
99import com.android.settings.nfc.AndroidBeam;
100import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -0400101import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400102import com.android.settings.notification.NotificationAccessSettings;
103import com.android.settings.notification.NotificationSettings;
104import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500105import com.android.settings.notification.OtherSoundSettings;
John Spurlock08531a82015-05-07 17:45:43 -0400106import com.android.settings.notification.ZenAccessSettings;
John Spurlock533a5662015-06-19 10:47:36 -0400107import com.android.settings.notification.ZenModeAutomationSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400108import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400109import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400110import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400111import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400112import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.print.PrintJobSettingsFragment;
114import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500115import com.android.settings.search.DynamicIndexableContentMonitor;
116import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700117import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import com.android.settings.tts.TextToSpeechSettings;
119import com.android.settings.users.UserSettings;
120import com.android.settings.vpn2.VpnSettings;
121import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700122import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700124import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125import com.android.settings.wifi.WifiSettings;
126import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500127
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800128import org.xmlpull.v1.XmlPullParser;
129import org.xmlpull.v1.XmlPullParserException;
130
131import java.io.IOException;
132import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500134import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700135import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800136
137public class SettingsActivity extends Activity
138 implements PreferenceManager.OnPreferenceTreeClickListener,
139 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100140 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700141 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100142 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800143
144 private static final String LOG_TAG = "Settings";
145
146 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700147 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700148 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
149 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700150 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700151 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700152 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153
154 /**
155 * When starting this activity, the invoking Intent can contain this extra
156 * string to specify which fragment should be initially displayed.
157 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
158 * will call isValidFragment() to confirm that the fragment class name is valid for this
159 * activity.
160 */
161 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
162
163 /**
164 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
165 * this extra can also be specified to supply a Bundle of arguments to pass
166 * to that fragment when it is instantiated during the initial creation
167 * of the activity.
168 */
169 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
170
171 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700172 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
173 */
174 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
175
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800176 public static final String BACK_STACK_PREFS = ":settings:prefs";
177
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800178 // extras that allow any preference activity to be launched as part of a wizard
179
180 // show Back and Next buttons? takes boolean parameter
181 // Back will then return RESULT_CANCELED and Next RESULT_OK
182 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
183
184 // add a Skip button?
185 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
186
187 // specify custom text for the Back or Next buttons, or cause a button to not appear
188 // at all by setting it to null
189 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
190 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
191
192 /**
193 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700194 * 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 -0800195 * that fragment.
196 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700197 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100198 /**
199 * The package name used to resolve the title resource id.
200 */
201 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
202 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700203 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
204 ":settings:show_fragment_title_resid";
205 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
206 ":settings:show_fragment_as_shortcut";
207
208 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
209 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 private static final String META_DATA_KEY_FRAGMENT_CLASS =
212 "com.android.settings.FRAGMENT_CLASS";
213
214 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
215
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700216 private static final String EMPTY_QUERY = "";
217
Jason Monk2ebc8a02015-02-13 15:23:19 -0500218 /**
219 * Settings will search for system activities of this action and add them as a top level
220 * settings tile using the following parameters.
221 *
222 * <p>A category must be specified in the meta-data for the activity named
223 * {@link #EXTRA_CATEGORY_KEY}
224 *
225 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
226 * otherwise the label for the activity will be used.
227 *
228 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
229 * otherwise the icon for the activity will be used.
230 *
231 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
232 */
233 private static final String EXTRA_SETTINGS_ACTION =
234 "com.android.settings.action.EXTRA_SETTINGS";
235
236 /**
237 * The key used to get the category from metadata of activities of action
238 * {@link #EXTRA_SETTINGS_ACTION}
239 * The value must be one of:
240 * <li>com.android.settings.category.wireless</li>
241 * <li>com.android.settings.category.device</li>
242 * <li>com.android.settings.category.personal</li>
243 * <li>com.android.settings.category.system</li>
244 */
245 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
246
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 private static boolean sShowNoHomeNotice = false;
248
249 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800251 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700252 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800253
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254 // Show only these settings for restricted users
255 private int[] SETTINGS_FOR_RESTRICTED = {
256 R.id.wireless_section,
257 R.id.wifi_settings,
258 R.id.bluetooth_settings,
259 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700260 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 R.id.wireless_settings,
262 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400263 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800264 R.id.display_settings,
265 R.id.storage_settings,
266 R.id.application_settings,
267 R.id.battery_settings,
268 R.id.personal_section,
269 R.id.location_settings,
270 R.id.security_settings,
271 R.id.language_settings,
272 R.id.user_settings,
273 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 R.id.system_section,
275 R.id.date_time_settings,
276 R.id.about_settings,
277 R.id.accessibility_settings,
278 R.id.print_settings,
279 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800280 R.id.home_settings,
281 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 };
283
284 private static final String[] ENTRY_FRAGMENTS = {
285 WirelessSettings.class.getName(),
286 WifiSettings.class.getName(),
287 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700288 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700290 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 TetherSettings.class.getName(),
292 WifiP2pSettings.class.getName(),
293 VpnSettings.class.getName(),
294 DateTimeSettings.class.getName(),
295 LocalePicker.class.getName(),
296 InputMethodAndLanguageSettings.class.getName(),
297 SpellCheckersSettings.class.getName(),
298 UserDictionaryList.class.getName(),
299 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700300 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 DisplaySettings.class.getName(),
302 DeviceInfoSettings.class.getName(),
303 ManageApplications.class.getName(),
Xiyuan Xia86a55402015-06-02 14:55:32 -0700304 ManageAssist.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305 ProcessStatsUi.class.getName(),
306 NotificationStation.class.getName(),
307 LocationSettings.class.getName(),
308 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400309 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 PrivacySettings.class.getName(),
311 DeviceAdminSettings.class.getName(),
312 AccessibilitySettings.class.getName(),
313 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
315 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700316 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700317 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700318 PrivateVolumeSettings.class.getName(),
319 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 AndroidBeam.class.getName(),
322 WifiDisplaySettings.class.getName(),
323 PowerUsageSummary.class.getName(),
324 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000325 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 CryptKeeperSettings.class.getName(),
327 DataUsageSummary.class.getName(),
328 DreamSettings.class.getName(),
329 UserSettings.class.getName(),
330 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400331 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800332 PrintSettingsFragment.class.getName(),
333 PrintJobSettingsFragment.class.getName(),
334 TrustedCredentialsSettings.class.getName(),
335 PaymentSettings.class.getName(),
336 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700337 ZenModeSettings.class.getName(),
338 NotificationSettings.class.getName(),
339 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
340 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400341 InstalledAppDetails.class.getName(),
342 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700343 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700344 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000345 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400346 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400347 ZenModePrioritySettings.class.getName(),
John Spurlock533a5662015-06-19 10:47:36 -0400348 ZenModeAutomationSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400349 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400350 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400351 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400352 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400353 PowerUsageDetail.class.getName(),
Jason Monk2cdafc62015-06-12 12:32:50 -0400354 ProcessStatsSummary.class.getName(),
Billy Laufee78562015-07-27 12:57:07 +0100355 DrawOverlayDetails.class.getName(),
356 WriteSettingsDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 };
358
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700359
360 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
361 "android.settings.APPLICATION_DETAILS_SETTINGS"
362 };
363
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 private SharedPreferences mDevelopmentPreferences;
365 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
366
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800367 private boolean mBatteryPresent = true;
368 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 @Override
370 public void onReceive(Context context, Intent intent) {
371 String action = intent.getAction();
372 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
373 boolean batteryPresent = Utils.isBatteryPresent(intent);
374
375 if (mBatteryPresent != batteryPresent) {
376 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700377 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800378 }
379 }
380 }
381 };
382
Svetoslav990159a2014-04-14 17:14:59 -0700383 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
384 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700385
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700386 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700387 private SwitchBar mSwitchBar;
388
389 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700390
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700391 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700392 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700393
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700394 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700395 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700396
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700397 private ViewGroup mContent;
398
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700399 private SearchView mSearchView;
400 private MenuItem mSearchMenuItem;
401 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700402 private SearchResultsSummary mSearchResultsFragment;
403 private String mSearchQuery;
404
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700405 // Categories
406 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800407
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700408 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700409 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 private Handler mHandler = new Handler() {
411 @Override
412 public void handleMessage(Message msg) {
413 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700414 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700415 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
416 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700417 buildDashboardCategories(mCategories);
418 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800419 } break;
420 }
421 }
422 };
423
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700424 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700425 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700426
Jim Miller0698a212014-10-16 19:49:07 -0700427 private Intent mResultIntentData;
428
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700429 public SwitchBar getSwitchBar() {
430 return mSwitchBar;
431 }
432
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700433 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
434 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700435 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700436 }
437 return mCategories;
438 }
439
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800440 @Override
441 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
442 // Override the fragment title for Wallpaper settings
443 int titleRes = pref.getTitleRes();
444 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
445 titleRes = R.string.wallpaper_settings_fragment_title;
446 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
447 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
448 if (UserManager.get(this).isLinkedUser()) {
449 titleRes = R.string.profile_info_settings_title;
450 } else {
451 titleRes = R.string.user_info_settings_title;
452 }
453 }
454 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
455 null, 0);
456 return true;
457 }
458
459 @Override
460 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
461 return false;
462 }
463
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700464 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700465 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700466 Message msg = new Message();
467 msg.what = MSG_BUILD_CATEGORIES;
468 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800469 }
470 }
471
472 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 public void onConfigurationChanged(Configuration newConfig) {
474 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800475 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
477
478 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700479 protected void onStart() {
480 super.onStart();
481
482 if (mNeedToRevertToInitialFragment) {
483 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800484 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800485 }
486
487 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700488 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700489 if (!mDisplaySearch) {
490 return false;
491 }
492
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700493 MenuInflater inflater = getMenuInflater();
494 inflater.inflate(R.menu.options_menu, menu);
495
496 // Cache the search query (can be overriden by the OnQueryTextListener)
497 final String query = mSearchQuery;
498
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700499 mSearchMenuItem = menu.findItem(R.id.search);
500 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700501
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700502 if (mSearchMenuItem == null || mSearchView == null) {
503 return false;
504 }
505
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700506 if (mSearchResultsFragment != null) {
507 mSearchResultsFragment.setSearchView(mSearchView);
508 }
509
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700510 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700511 mSearchView.setOnQueryTextListener(this);
512 mSearchView.setOnCloseListener(this);
513
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700514 if (mSearchMenuItemExpanded) {
515 mSearchMenuItem.expandActionView();
516 }
517 mSearchView.setQuery(query, true /* submit */);
518
519 return true;
520 }
521
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700522 private static boolean isShortCutIntent(final Intent intent) {
523 Set<String> categories = intent.getCategories();
524 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
525 }
526
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700527 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700528 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700529 if (action == null) {
530 return false;
531 }
532 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
533 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
534 }
535 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700536 }
537
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700538 @Override
539 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700540 super.onCreate(savedState);
541
542 // Should happen before any call to getIntent()
543 getMetaData();
544
545 final Intent intent = getIntent();
546 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
547 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800548 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800550 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
551 Context.MODE_PRIVATE);
552
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700553 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700554 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700555
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700556 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700557 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
558
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700559 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700560 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700561
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700562 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700563
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700564 // This is a "Sub Settings" when:
565 // - this is a real SubSettings
566 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700567 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700568 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
569
570 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
571 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700572 // Check also that we are not a Theme Dialog as we don't want to override them
573 final int themeResId = getThemeResId();
574 if (themeResId != R.style.Theme_DialogWhenLarge &&
575 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
576 setTheme(R.style.Theme_SubSettings);
577 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700578 }
579
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700580 setContentView(mIsShowingDashboard ?
581 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700583 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700584
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 getFragmentManager().addOnBackStackChangedListener(this);
586
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700587 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800588 // Run the Index update only if we have some space
589 if (!Utils.isLowStorage(this)) {
590 Index.getInstance(getApplicationContext()).update();
591 } else {
592 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
593 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700594 }
595
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700596 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700597 // We are restarting from a previous saved state; used that to initialize, instead
598 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700599 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
600 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700602 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800603
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700604 ArrayList<DashboardCategory> categories =
605 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
606 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700607 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700608 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700609 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800610 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700611
612 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700613 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700614 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
615 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800616 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700617 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400618 mDisplaySearch = false;
619 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700620 if (mIsShortcut) {
621 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700622 } else if (isSubSettings) {
623 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700624 } else {
625 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700626 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700627 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700628
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700629 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700630 switchToFragment(initialFragmentName, initialArguments, true, false,
631 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000632 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700633 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700634 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700635 // Show Search affordance
636 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700637 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700638 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700639 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 }
641 }
642
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700643 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700644 if (mActionBar != null) {
645 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
646 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
647 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700648 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
649
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
652
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700653 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 if (buttonBar != null) {
655 buttonBar.setVisibility(View.VISIBLE);
656
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700657 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 backButton.setOnClickListener(new OnClickListener() {
659 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700660 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 finish();
662 }
663 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700664 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800665 skipButton.setOnClickListener(new OnClickListener() {
666 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700667 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800668 finish();
669 }
670 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700671 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800672 mNextButton.setOnClickListener(new OnClickListener() {
673 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700674 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800675 finish();
676 }
677 });
678
679 // set our various button parameters
680 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
681 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
682 if (TextUtils.isEmpty(buttonText)) {
683 mNextButton.setVisibility(View.GONE);
684 }
685 else {
686 mNextButton.setText(buttonText);
687 }
688 }
689 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
690 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
691 if (TextUtils.isEmpty(buttonText)) {
692 backButton.setVisibility(View.GONE);
693 }
694 else {
695 backButton.setText(buttonText);
696 }
697 }
698 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
699 skipButton.setVisibility(View.VISIBLE);
700 }
701 }
702 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700703
704 mHomeActivitiesCount = getHomeActivitiesCount();
705 }
706
707 private int getHomeActivitiesCount() {
708 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
709 getPackageManager().getHomeActivities(homeApps);
710 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800711 }
712
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700713 private void setTitleFromIntent(Intent intent) {
714 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
715 if (initialTitleResId > 0) {
716 mInitialTitle = null;
717 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100718
719 final String initialTitleResPackageName = intent.getStringExtra(
720 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
721 if (initialTitleResPackageName != null) {
722 try {
723 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
724 0 /* flags */, new UserHandle(UserHandle.myUserId()));
725 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
726 setTitle(mInitialTitle);
727 mInitialTitleResId = -1;
728 return;
729 } catch (NameNotFoundException e) {
730 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
731 }
732 } else {
733 setTitle(mInitialTitleResId);
734 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700735 } else {
736 mInitialTitleResId = -1;
737 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
738 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
739 setTitle(mInitialTitle);
740 }
741 }
742
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800743 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800744 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700745 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800746 }
747
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800749 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700750
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800751 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700752 if (mInitialTitleResId > 0) {
753 setTitle(mInitialTitleResId);
754 } else {
755 setTitle(mInitialTitle);
756 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700757 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800758 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700759
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800760 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
761 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700762
763 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800764 }
765
766 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
767 final CharSequence title;
768 final int titleRes = bse.getBreadCrumbTitleRes();
769 if (titleRes > 0) {
770 title = getText(titleRes);
771 } else {
772 title = bse.getBreadCrumbTitle();
773 }
774 if (title != null) {
775 setTitle(title);
776 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800777 }
778
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800779 @Override
780 protected void onSaveInstanceState(Bundle outState) {
781 super.onSaveInstanceState(outState);
782
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700783 if (mCategories.size() > 0) {
784 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800785 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700786
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700787 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700788 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700789
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700790 if (mDisplaySearch) {
791 // The option menus are created if the ActionBar is visible and they are also created
792 // asynchronously. If you launch Settings with an Intent action like
793 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
794 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
795 // menu item and search view are null.
796 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
797 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700798
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700799 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
800 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
801 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700802
803 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800804 }
805
806 @Override
807 public void onResume() {
808 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400809 if (mIsShowingDashboard) {
810 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
811 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800812
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700813 final int newHomeActivityCount = getHomeActivitiesCount();
814 if (newHomeActivityCount != mHomeActivitiesCount) {
815 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700816 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700817 }
818
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800819 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
820 @Override
821 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700822 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800823 }
824 };
825 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
826 mDevelopmentPreferencesListener);
827
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800828 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700829
Svetoslav990159a2014-04-14 17:14:59 -0700830 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700831
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700832 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700833 onQueryTextSubmit(mSearchQuery);
834 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800835 }
836
837 @Override
838 public void onPause() {
839 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400840 if (mIsShowingDashboard) {
841 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
842 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800843 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700844 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800845 }
846
847 @Override
848 public void onDestroy() {
849 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700850
851 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
852 mDevelopmentPreferencesListener);
853 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 }
855
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800856 protected boolean isValidFragment(String fragmentName) {
857 // Almost all fragments are wrapped in this,
858 // except for a few that have their own activities.
859 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
860 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
861 }
862 return false;
863 }
864
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800865 @Override
866 public Intent getIntent() {
867 Intent superIntent = super.getIntent();
868 String startingFragment = getStartingFragmentClass(superIntent);
869 // This is called from super.onCreate, isMultiPane() is not yet reliable
870 // Do not use onIsHidingHeaders either, which relies itself on this method
871 if (startingFragment != null) {
872 Intent modIntent = new Intent(superIntent);
873 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
874 Bundle args = superIntent.getExtras();
875 if (args != null) {
876 args = new Bundle(args);
877 } else {
878 args = new Bundle();
879 }
880 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100881 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800882 return modIntent;
883 }
884 return superIntent;
885 }
886
887 /**
888 * Checks if the component name in the intent is different from the Settings class and
889 * returns the class name to load as a fragment.
890 */
891 private String getStartingFragmentClass(Intent intent) {
892 if (mFragmentClass != null) return mFragmentClass;
893
894 String intentClass = intent.getComponent().getClassName();
895 if (intentClass.equals(getClass().getName())) return null;
896
897 if ("com.android.settings.ManageApplications".equals(intentClass)
898 || "com.android.settings.RunningServices".equals(intentClass)
899 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
900 // Old names of manage apps.
901 intentClass = com.android.settings.applications.ManageApplications.class.getName();
902 }
903
904 return intentClass;
905 }
906
907 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000908 * Start a new fragment containing a preference panel. If the preferences
909 * are being displayed in multi-pane mode, the given fragment class will
910 * be instantiated and placed in the appropriate pane. If running in
911 * single-pane mode, a new activity will be launched in which to show the
912 * fragment.
913 *
914 * @param fragmentClass Full name of the class implementing the fragment.
915 * @param args Any desired arguments to supply to the fragment.
916 * @param titleRes Optional resource identifier of the title of this
917 * fragment.
918 * @param titleText Optional text of the title of this fragment.
919 * @param resultTo Optional fragment that result data should be sent to.
920 * If non-null, resultTo.onActivityResult() will be called when this
921 * preference panel is done. The launched panel must use
922 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
923 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700924 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000925 */
926 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700927 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700928 String title = null;
929 if (titleRes < 0) {
930 if (titleText != null) {
931 title = titleText.toString();
932 } else {
933 // There not much we can do in that case
934 title = "";
935 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700936 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700937 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700938 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000939 }
940
941 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100942 * Start a new fragment in a new activity containing a preference panel for a given user. If the
943 * preferences are being displayed in multi-pane mode, the given fragment class will be
944 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
945 * activity will be launched in which to show the fragment.
946 *
947 * @param fragmentClass Full name of the class implementing the fragment.
948 * @param args Any desired arguments to supply to the fragment.
949 * @param titleRes Optional resource identifier of the title of this fragment.
950 * @param titleText Optional text of the title of this fragment.
951 * @param userHandle The user for which the panel has to be started.
952 */
953 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
954 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700955 // This is a workaround.
956 //
957 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
958 // starting the fragment could cause a native stack corruption. See b/17523189. However,
959 // adding that flag and start the preference panel with the same UserHandler will make it
960 // impossible to use back button to return to the previous screen. See b/20042570.
961 //
962 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
963 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
964 // when we're calling it as the same user.
965 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
966 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
967 } else {
968 String title = null;
969 if (titleRes < 0) {
970 if (titleText != null) {
971 title = titleText.toString();
972 } else {
973 // There not much we can do in that case
974 title = "";
975 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100976 }
Lifu Tangd0332852015-04-02 12:05:46 -0700977 Utils.startWithFragmentAsUser(this, fragmentClass, args,
978 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100979 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100980 }
981
982 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800983 * Called by a preference panel fragment to finish itself.
984 *
985 * @param caller The fragment that is asking to be finished.
986 * @param resultCode Optional result code to send back to the original
987 * launching fragment.
988 * @param resultData Optional result data to send back to the original
989 * launching fragment.
990 */
991 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
992 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700993 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800994 }
995
996 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000997 * Start a new fragment.
998 *
999 * @param fragment The fragment to start
1000 * @param push If true, the current fragment will be pushed onto the back stack. If false,
1001 * the current fragment will be replaced.
1002 */
1003 public void startPreferenceFragment(Fragment fragment, boolean push) {
1004 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001005 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001006 if (push) {
1007 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1008 transaction.addToBackStack(BACK_STACK_PREFS);
1009 } else {
1010 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1011 }
1012 transaction.commitAllowingStateLoss();
1013 }
1014
1015 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001016 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1017 */
1018 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001019 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001020 if (validate && !isValidFragment(fragmentName)) {
1021 throw new IllegalArgumentException("Invalid fragment for this activity: "
1022 + fragmentName);
1023 }
1024 Fragment f = Fragment.instantiate(this, fragmentName, args);
1025 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001026 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001027 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001028 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001029 }
1030 if (addToBackStack) {
1031 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1032 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001033 if (titleResId > 0) {
1034 transaction.setBreadCrumbTitle(titleResId);
1035 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001036 transaction.setBreadCrumbTitle(title);
1037 }
1038 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001039 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001040 return f;
1041 }
1042
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001043 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001044 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001045 *
1046 * @param categories The list in which to place the tiles categories.
1047 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001048 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001049 categories.clear();
Jason Monk2acff552015-06-11 15:47:29 -04001050 loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001051 updateTilesList(categories);
1052 }
1053
1054 /**
1055 * Parse the given XML file as a categories description, adding each
1056 * parsed categories and tiles into the target list.
1057 *
1058 * @param resid The XML resource to load and parse.
1059 * @param target The list in which the parsed categories and tiles should be placed.
1060 */
Jason Monk2acff552015-06-11 15:47:29 -04001061 public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
1062 Context context) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001063 XmlResourceParser parser = null;
1064 try {
Jason Monk2acff552015-06-11 15:47:29 -04001065 parser = context.getResources().getXml(resid);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001066 AttributeSet attrs = Xml.asAttributeSet(parser);
1067
1068 int type;
1069 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1070 && type != XmlPullParser.START_TAG) {
1071 // Parse next until start tag is found
1072 }
1073
1074 String nodeName = parser.getName();
1075 if (!"dashboard-categories".equals(nodeName)) {
1076 throw new RuntimeException(
1077 "XML document must start with <preference-categories> tag; found"
1078 + nodeName + " at " + parser.getPositionDescription());
1079 }
1080
1081 Bundle curBundle = null;
1082
1083 final int outerDepth = parser.getDepth();
1084 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1085 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1086 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1087 continue;
1088 }
1089
1090 nodeName = parser.getName();
1091 if ("dashboard-category".equals(nodeName)) {
1092 DashboardCategory category = new DashboardCategory();
1093
Jason Monk2acff552015-06-11 15:47:29 -04001094 TypedArray sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001095 attrs, com.android.internal.R.styleable.PreferenceHeader);
1096 category.id = sa.getResourceId(
1097 com.android.internal.R.styleable.PreferenceHeader_id,
1098 (int)DashboardCategory.CAT_ID_UNDEFINED);
1099
1100 TypedValue tv = sa.peekValue(
1101 com.android.internal.R.styleable.PreferenceHeader_title);
1102 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1103 if (tv.resourceId != 0) {
1104 category.titleRes = tv.resourceId;
1105 } else {
1106 category.title = tv.string;
1107 }
1108 }
1109 sa.recycle();
Jason Monk2acff552015-06-11 15:47:29 -04001110 sa = context.obtainStyledAttributes(attrs,
1111 com.android.internal.R.styleable.Preference);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001112 tv = sa.peekValue(
1113 com.android.internal.R.styleable.Preference_key);
1114 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1115 if (tv.resourceId != 0) {
Jason Monk2acff552015-06-11 15:47:29 -04001116 category.key = context.getString(tv.resourceId);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001117 } else {
1118 category.key = tv.string.toString();
1119 }
1120 }
1121 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001122
1123 final int innerDepth = parser.getDepth();
1124 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1125 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1126 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1127 continue;
1128 }
1129
1130 String innerNodeName = parser.getName();
1131 if (innerNodeName.equals("dashboard-tile")) {
1132 DashboardTile tile = new DashboardTile();
1133
Jason Monk2acff552015-06-11 15:47:29 -04001134 sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001135 attrs, com.android.internal.R.styleable.PreferenceHeader);
1136 tile.id = sa.getResourceId(
1137 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001138 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001139 tv = sa.peekValue(
1140 com.android.internal.R.styleable.PreferenceHeader_title);
1141 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1142 if (tv.resourceId != 0) {
1143 tile.titleRes = tv.resourceId;
1144 } else {
1145 tile.title = tv.string;
1146 }
1147 }
1148 tv = sa.peekValue(
1149 com.android.internal.R.styleable.PreferenceHeader_summary);
1150 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1151 if (tv.resourceId != 0) {
1152 tile.summaryRes = tv.resourceId;
1153 } else {
1154 tile.summary = tv.string;
1155 }
1156 }
1157 tile.iconRes = sa.getResourceId(
1158 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1159 tile.fragment = sa.getString(
1160 com.android.internal.R.styleable.PreferenceHeader_fragment);
1161 sa.recycle();
1162
1163 if (curBundle == null) {
1164 curBundle = new Bundle();
1165 }
1166
1167 final int innerDepth2 = parser.getDepth();
1168 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1169 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1170 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1171 continue;
1172 }
1173
1174 String innerNodeName2 = parser.getName();
1175 if (innerNodeName2.equals("extra")) {
Jason Monk2acff552015-06-11 15:47:29 -04001176 context.getResources().parseBundleExtra("extra", attrs,
1177 curBundle);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001178 XmlUtils.skipCurrentTag(parser);
1179
1180 } else if (innerNodeName2.equals("intent")) {
Jason Monk2acff552015-06-11 15:47:29 -04001181 tile.intent = Intent.parseIntent(context.getResources(), parser,
1182 attrs);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001183
1184 } else {
1185 XmlUtils.skipCurrentTag(parser);
1186 }
1187 }
1188
1189 if (curBundle.size() > 0) {
1190 tile.fragmentArguments = curBundle;
1191 curBundle = null;
1192 }
1193
PauloftheWest38155612014-06-30 10:02:36 -07001194 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Jason Monk2acff552015-06-11 15:47:29 -04001195 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(context)){
PauloftheWest38155612014-06-30 10:02:36 -07001196 category.addTile(tile);
1197 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001198
Jason Monk2ebc8a02015-02-13 15:23:19 -05001199 } else if (innerNodeName.equals("external-tiles")) {
1200 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001201 } else {
1202 XmlUtils.skipCurrentTag(parser);
1203 }
1204 }
1205
1206 target.add(category);
1207 } else {
1208 XmlUtils.skipCurrentTag(parser);
1209 }
1210 }
1211
1212 } catch (XmlPullParserException e) {
1213 throw new RuntimeException("Error parsing categories", e);
1214 } catch (IOException e) {
1215 throw new RuntimeException("Error parsing categories", e);
1216 } finally {
1217 if (parser != null) parser.close();
1218 }
1219 }
1220
1221 private void updateTilesList(List<DashboardCategory> target) {
1222 final boolean showDev = mDevelopmentPreferences.getBoolean(
1223 DevelopmentSettings.PREF_SHOW,
1224 android.os.Build.TYPE.equals("eng"));
1225
1226 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1227
1228 final int size = target.size();
1229 for (int i = 0; i < size; i++) {
1230
1231 DashboardCategory category = target.get(i);
1232
1233 // Ids are integers, so downcasting is ok
1234 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001235 int n = category.getTilesCount() - 1;
1236 while (n >= 0) {
1237
1238 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001239 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001240 id = (int) tile.id;
1241 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001242 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1243 removeTile = true;
1244 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001245 } else if (id == R.id.wifi_settings) {
1246 // Remove WiFi Settings if WiFi service is not available.
1247 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001248 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001249 }
1250 } else if (id == R.id.bluetooth_settings) {
1251 // Remove Bluetooth Settings if Bluetooth service is not available.
1252 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001253 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001254 }
1255 } else if (id == R.id.data_usage_settings) {
1256 // Remove data usage when kernel module not enabled
Jason Monkb45e27b2015-05-20 13:35:43 -04001257 if (!Utils.isBandwidthControlEnabled()) {
1258 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001259 }
1260 } else if (id == R.id.battery_settings) {
1261 // Remove battery settings when battery is not available. (e.g. TV)
1262
1263 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001264 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 }
1266 } else if (id == R.id.home_settings) {
1267 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001268 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001269 }
1270 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001271 boolean hasMultipleUsers =
1272 ((UserManager) getSystemService(Context.USER_SERVICE))
1273 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001274 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001275 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001276 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001277 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001278 }
1279 } else if (id == R.id.nfc_payment_settings) {
1280 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001281 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001282 } else {
1283 // Only show if NFC is on and we have the HCE feature
1284 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001285 if (adapter == null || !adapter.isEnabled() ||
1286 !getPackageManager().hasSystemFeature(
1287 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001288 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001289 }
1290 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001291 } else if (id == R.id.print_settings) {
1292 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1293 PackageManager.FEATURE_PRINTING);
1294 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001295 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001296 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001297 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001298 if (!showDev || um.hasUserRestriction(
1299 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001300 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001301 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001302 }
1303
1304 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001305 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1306 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001307 }
1308
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001309 if (removeTile && n < category.getTilesCount()) {
1310 category.removeTile(n);
1311 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001312 n--;
1313 }
1314 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001315 addExternalTiles(target);
1316 }
1317
1318 private void addExternalTiles(List<DashboardCategory> target) {
1319 Map<Pair<String, String>, DashboardTile> addedCache =
1320 new ArrayMap<Pair<String, String>, DashboardTile>();
1321 UserManager userManager = UserManager.get(this);
1322 for (UserHandle user : userManager.getUserProfiles()) {
1323 addExternalTiles(target, user, addedCache);
1324 }
1325 }
1326
1327 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1328 Map<Pair<String, String>, DashboardTile> addedCache) {
1329 PackageManager pm = getPackageManager();
1330 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1331 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1332 PackageManager.GET_META_DATA, user.getIdentifier());
1333 for (ResolveInfo resolved : results) {
1334 if (!resolved.system) {
1335 // Do not allow any app to add to settings, only system ones.
1336 continue;
1337 }
1338 ActivityInfo activityInfo = resolved.activityInfo;
1339 Bundle metaData = activityInfo.metaData;
1340 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1341 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1342 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1343 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1344 continue;
1345 }
1346 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1347 DashboardCategory category = getCategory(target, categoryKey);
1348 if (category == null) {
1349 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1350 + "category key " + categoryKey);
1351 continue;
1352 }
1353 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1354 activityInfo.name);
1355 DashboardTile tile = addedCache.get(key);
1356 if (tile == null) {
1357 tile = new DashboardTile();
1358 tile.intent = new Intent().setClassName(
1359 activityInfo.packageName, activityInfo.name);
1360 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1361
1362 if (category.externalIndex == -1) {
1363 // If no location for external tiles has been specified for this category,
1364 // then just put them at the end.
1365 category.addTile(tile);
1366 } else {
1367 category.addTile(category.externalIndex, tile);
1368 }
1369 addedCache.put(key, tile);
1370 }
1371 tile.userHandle.add(user);
1372 }
1373 }
1374
1375 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1376 for (DashboardCategory category : target) {
1377 if (categoryKey.equals(category.key)) {
1378 return category;
1379 }
1380 }
1381 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001382 }
1383
1384 private boolean updateHomeSettingTiles(DashboardTile tile) {
1385 // Once we decide to show Home settings, keep showing it forever
1386 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1387 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1388 return true;
1389 }
1390
1391 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001392 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001393 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001394 // When there's only one available home app, omit this settings
1395 // category entirely at the top level UI. If the user just
1396 // uninstalled the penultimate home app candidiate, we also
1397 // now tell them about why they aren't seeing 'Home' in the list.
1398 if (sShowNoHomeNotice) {
1399 sShowNoHomeNotice = false;
1400 NoHomeDialogFragment.show(this);
1401 }
1402 return false;
1403 } else {
1404 // Okay, we're allowing the Home settings category. Tell it, when
1405 // invoked via this front door, that we'll need to be told about the
1406 // case when the user uninstalls all but one home app.
1407 if (tile.fragmentArguments == null) {
1408 tile.fragmentArguments = new Bundle();
1409 }
1410 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1411 }
1412 } catch (Exception e) {
1413 // Can't look up the home activity; bail on configuring the icon
1414 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1415 }
1416
1417 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1418 return true;
1419 }
1420
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001421 private void getMetaData() {
1422 try {
1423 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1424 PackageManager.GET_META_DATA);
1425 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001426 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1427 } catch (NameNotFoundException nnfe) {
1428 // No recovery
1429 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1430 }
1431 }
1432
1433 // give subclasses access to the Next button
1434 public boolean hasNextButton() {
1435 return mNextButton != null;
1436 }
1437
1438 public Button getNextButton() {
1439 return mNextButton;
1440 }
1441
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001442 @Override
1443 public boolean shouldUpRecreateTask(Intent targetIntent) {
1444 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1445 }
1446
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001447 public static void requestHomeNotice() {
1448 sShowNoHomeNotice = true;
1449 }
1450
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001451 @Override
1452 public boolean onQueryTextSubmit(String query) {
1453 switchToSearchResultsFragmentIfNeeded();
1454 mSearchQuery = query;
1455 return mSearchResultsFragment.onQueryTextSubmit(query);
1456 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001457
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001458 @Override
1459 public boolean onQueryTextChange(String newText) {
1460 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001461 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001462 return false;
1463 }
1464 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001465 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001466
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001467 @Override
1468 public boolean onClose() {
1469 return false;
1470 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001471
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001472 @Override
1473 public boolean onMenuItemActionExpand(MenuItem item) {
1474 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001475 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001477 return true;
1478 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001479
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001480 @Override
1481 public boolean onMenuItemActionCollapse(MenuItem item) {
1482 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001483 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001484 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001485 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001486 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001487 return true;
1488 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001489
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001490 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001491 if (mSearchResultsFragment != null) {
1492 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001493 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001494 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001495 if (current != null && current instanceof SearchResultsSummary) {
1496 mSearchResultsFragment = (SearchResultsSummary) current;
1497 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001498 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001499 SearchResultsSummary.class.getName(), null, false, true,
1500 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001501 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001502 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001503 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001504 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001505
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001506 public void needToRevertToInitialFragment() {
1507 mNeedToRevertToInitialFragment = true;
1508 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001509
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001510 private void revertToInitialFragment() {
1511 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001512 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001513 mSearchMenuItemExpanded = false;
1514 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1515 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001516 if (mSearchMenuItem != null) {
1517 mSearchMenuItem.collapseActionView();
1518 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001519 }
Jim Miller0698a212014-10-16 19:49:07 -07001520
1521 public Intent getResultIntentData() {
1522 return mResultIntentData;
1523 }
1524
1525 public void setResultIntentData(Intent resultIntentData) {
1526 mResultIntentData = resultIntentData;
1527 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001528}