blob: d830c33644de8ed87e6bca704a44a6bace3d0830 [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 Spurlockc1df2aa2015-04-13 20:57:06 -0400109import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400110import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400111import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.print.PrintJobSettingsFragment;
113import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500114import com.android.settings.search.DynamicIndexableContentMonitor;
115import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700116import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import com.android.settings.tts.TextToSpeechSettings;
118import com.android.settings.users.UserSettings;
119import com.android.settings.vpn2.VpnSettings;
120import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700121import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700123import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124import com.android.settings.wifi.WifiSettings;
125import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500126
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127import org.xmlpull.v1.XmlPullParser;
128import org.xmlpull.v1.XmlPullParserException;
129
130import java.io.IOException;
131import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500133import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700134import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800135
136public class SettingsActivity extends Activity
137 implements PreferenceManager.OnPreferenceTreeClickListener,
138 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100139 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700140 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100141 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
143 private static final String LOG_TAG = "Settings";
144
145 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700146 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700147 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
148 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700149 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700150 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700151 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152
153 /**
154 * When starting this activity, the invoking Intent can contain this extra
155 * string to specify which fragment should be initially displayed.
156 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
157 * will call isValidFragment() to confirm that the fragment class name is valid for this
158 * activity.
159 */
160 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
161
162 /**
163 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
164 * this extra can also be specified to supply a Bundle of arguments to pass
165 * to that fragment when it is instantiated during the initial creation
166 * of the activity.
167 */
168 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
169
170 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700171 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
172 */
173 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
174
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800175 public static final String BACK_STACK_PREFS = ":settings:prefs";
176
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 // extras that allow any preference activity to be launched as part of a wizard
178
179 // show Back and Next buttons? takes boolean parameter
180 // Back will then return RESULT_CANCELED and Next RESULT_OK
181 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
182
183 // add a Skip button?
184 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
185
186 // specify custom text for the Back or Next buttons, or cause a button to not appear
187 // at all by setting it to null
188 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
189 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
190
191 /**
192 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700193 * 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 -0800194 * that fragment.
195 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700196 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100197 /**
198 * The package name used to resolve the title resource id.
199 */
200 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
201 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700202 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
203 ":settings:show_fragment_title_resid";
204 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
205 ":settings:show_fragment_as_shortcut";
206
207 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
208 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210 private static final String META_DATA_KEY_FRAGMENT_CLASS =
211 "com.android.settings.FRAGMENT_CLASS";
212
213 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
214
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700215 private static final String EMPTY_QUERY = "";
216
Jason Monk2ebc8a02015-02-13 15:23:19 -0500217 /**
218 * Settings will search for system activities of this action and add them as a top level
219 * settings tile using the following parameters.
220 *
221 * <p>A category must be specified in the meta-data for the activity named
222 * {@link #EXTRA_CATEGORY_KEY}
223 *
224 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
225 * otherwise the label for the activity will be used.
226 *
227 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
228 * otherwise the icon for the activity will be used.
229 *
230 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
231 */
232 private static final String EXTRA_SETTINGS_ACTION =
233 "com.android.settings.action.EXTRA_SETTINGS";
234
235 /**
236 * The key used to get the category from metadata of activities of action
237 * {@link #EXTRA_SETTINGS_ACTION}
238 * The value must be one of:
239 * <li>com.android.settings.category.wireless</li>
240 * <li>com.android.settings.category.device</li>
241 * <li>com.android.settings.category.personal</li>
242 * <li>com.android.settings.category.system</li>
243 */
244 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
245
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 private static boolean sShowNoHomeNotice = false;
247
248 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800249
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800250 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700251 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800252
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800253 // Show only these settings for restricted users
254 private int[] SETTINGS_FOR_RESTRICTED = {
255 R.id.wireless_section,
256 R.id.wifi_settings,
257 R.id.bluetooth_settings,
258 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700259 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 R.id.wireless_settings,
261 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400262 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800263 R.id.display_settings,
264 R.id.storage_settings,
265 R.id.application_settings,
266 R.id.battery_settings,
267 R.id.personal_section,
268 R.id.location_settings,
269 R.id.security_settings,
270 R.id.language_settings,
271 R.id.user_settings,
272 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 R.id.system_section,
274 R.id.date_time_settings,
275 R.id.about_settings,
276 R.id.accessibility_settings,
277 R.id.print_settings,
278 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800279 R.id.home_settings,
280 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800281 };
282
283 private static final String[] ENTRY_FRAGMENTS = {
284 WirelessSettings.class.getName(),
285 WifiSettings.class.getName(),
286 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700287 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700289 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 TetherSettings.class.getName(),
291 WifiP2pSettings.class.getName(),
292 VpnSettings.class.getName(),
293 DateTimeSettings.class.getName(),
294 LocalePicker.class.getName(),
295 InputMethodAndLanguageSettings.class.getName(),
296 SpellCheckersSettings.class.getName(),
297 UserDictionaryList.class.getName(),
298 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700299 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 DisplaySettings.class.getName(),
301 DeviceInfoSettings.class.getName(),
302 ManageApplications.class.getName(),
Xiyuan Xia86a55402015-06-02 14:55:32 -0700303 ManageAssist.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 ProcessStatsUi.class.getName(),
305 NotificationStation.class.getName(),
306 LocationSettings.class.getName(),
307 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400308 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 PrivacySettings.class.getName(),
310 DeviceAdminSettings.class.getName(),
311 AccessibilitySettings.class.getName(),
312 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
314 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700315 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700316 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700317 PrivateVolumeSettings.class.getName(),
318 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800319 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 AndroidBeam.class.getName(),
321 WifiDisplaySettings.class.getName(),
322 PowerUsageSummary.class.getName(),
323 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000324 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 CryptKeeperSettings.class.getName(),
326 DataUsageSummary.class.getName(),
327 DreamSettings.class.getName(),
328 UserSettings.class.getName(),
329 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400330 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800331 PrintSettingsFragment.class.getName(),
332 PrintJobSettingsFragment.class.getName(),
333 TrustedCredentialsSettings.class.getName(),
334 PaymentSettings.class.getName(),
335 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700336 ZenModeSettings.class.getName(),
337 NotificationSettings.class.getName(),
338 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
339 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400340 InstalledAppDetails.class.getName(),
341 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700342 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700343 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000344 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400345 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400346 ZenModePrioritySettings.class.getName(),
John Spurlock533a5662015-06-19 10:47:36 -0400347 ZenModeAutomationSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400348 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400349 ZenModeEventRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400350 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400351 PowerUsageDetail.class.getName(),
Jason Monk2cdafc62015-06-12 12:32:50 -0400352 ProcessStatsSummary.class.getName(),
Billy Laufee78562015-07-27 12:57:07 +0100353 DrawOverlayDetails.class.getName(),
354 WriteSettingsDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 };
356
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700357
358 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
359 "android.settings.APPLICATION_DETAILS_SETTINGS"
360 };
361
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 private SharedPreferences mDevelopmentPreferences;
363 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
364
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800365 private boolean mBatteryPresent = true;
366 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800367 @Override
368 public void onReceive(Context context, Intent intent) {
369 String action = intent.getAction();
370 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
371 boolean batteryPresent = Utils.isBatteryPresent(intent);
372
373 if (mBatteryPresent != batteryPresent) {
374 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700375 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800376 }
377 }
378 }
379 };
380
Svetoslav990159a2014-04-14 17:14:59 -0700381 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
382 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700383
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700384 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700385 private SwitchBar mSwitchBar;
386
387 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700388
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700389 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700390 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700391
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700392 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700393 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700394
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700395 private ViewGroup mContent;
396
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700397 private SearchView mSearchView;
398 private MenuItem mSearchMenuItem;
399 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700400 private SearchResultsSummary mSearchResultsFragment;
401 private String mSearchQuery;
402
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700403 // Categories
404 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700406 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700407 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800408 private Handler mHandler = new Handler() {
409 @Override
410 public void handleMessage(Message msg) {
411 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700412 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700413 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
414 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700415 buildDashboardCategories(mCategories);
416 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800417 } break;
418 }
419 }
420 };
421
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700422 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700423 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700424
Jim Miller0698a212014-10-16 19:49:07 -0700425 private Intent mResultIntentData;
426
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700427 public SwitchBar getSwitchBar() {
428 return mSwitchBar;
429 }
430
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700431 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
432 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700433 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700434 }
435 return mCategories;
436 }
437
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800438 @Override
439 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
440 // Override the fragment title for Wallpaper settings
441 int titleRes = pref.getTitleRes();
442 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
443 titleRes = R.string.wallpaper_settings_fragment_title;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800444 }
445 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
446 null, 0);
447 return true;
448 }
449
450 @Override
451 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
452 return false;
453 }
454
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700455 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700456 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700457 Message msg = new Message();
458 msg.what = MSG_BUILD_CATEGORIES;
459 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800460 }
461 }
462
463 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 public void onConfigurationChanged(Configuration newConfig) {
465 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800466 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800467 }
468
469 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700470 protected void onStart() {
471 super.onStart();
472
473 if (mNeedToRevertToInitialFragment) {
474 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800475 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
477
478 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700479 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700480 if (!mDisplaySearch) {
481 return false;
482 }
483
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700484 MenuInflater inflater = getMenuInflater();
485 inflater.inflate(R.menu.options_menu, menu);
486
487 // Cache the search query (can be overriden by the OnQueryTextListener)
488 final String query = mSearchQuery;
489
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700490 mSearchMenuItem = menu.findItem(R.id.search);
491 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700492
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700493 if (mSearchMenuItem == null || mSearchView == null) {
494 return false;
495 }
496
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700497 if (mSearchResultsFragment != null) {
498 mSearchResultsFragment.setSearchView(mSearchView);
499 }
500
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700501 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700502 mSearchView.setOnQueryTextListener(this);
503 mSearchView.setOnCloseListener(this);
504
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700505 if (mSearchMenuItemExpanded) {
506 mSearchMenuItem.expandActionView();
507 }
508 mSearchView.setQuery(query, true /* submit */);
509
510 return true;
511 }
512
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700513 private static boolean isShortCutIntent(final Intent intent) {
514 Set<String> categories = intent.getCategories();
515 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
516 }
517
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700518 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700519 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700520 if (action == null) {
521 return false;
522 }
523 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
524 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
525 }
526 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700527 }
528
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700529 @Override
530 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700531 super.onCreate(savedState);
532
533 // Should happen before any call to getIntent()
534 getMetaData();
535
536 final Intent intent = getIntent();
537 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
538 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
542 Context.MODE_PRIVATE);
543
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700544 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700545 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700546
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700547 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700548 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
549
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700550 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700551 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700552
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700553 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700554
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700555 // This is a "Sub Settings" when:
556 // - this is a real SubSettings
557 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700558 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700559 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
560
561 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
562 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700563 // Check also that we are not a Theme Dialog as we don't want to override them
564 final int themeResId = getThemeResId();
565 if (themeResId != R.style.Theme_DialogWhenLarge &&
566 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
567 setTheme(R.style.Theme_SubSettings);
568 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700569 }
570
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700571 setContentView(mIsShowingDashboard ?
572 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700574 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700575
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 getFragmentManager().addOnBackStackChangedListener(this);
577
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700578 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800579 // Run the Index update only if we have some space
580 if (!Utils.isLowStorage(this)) {
581 Index.getInstance(getApplicationContext()).update();
582 } else {
583 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
584 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700585 }
586
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700587 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700588 // We are restarting from a previous saved state; used that to initialize, instead
589 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700590 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
591 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800592
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700593 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800594
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700595 ArrayList<DashboardCategory> categories =
596 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
597 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700598 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700599 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700600 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700602
603 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700604 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700605 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
606 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800607 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700608 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400609 mDisplaySearch = false;
610 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700611 if (mIsShortcut) {
612 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700613 } else if (isSubSettings) {
614 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700615 } else {
616 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700617 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700618 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700619
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700620 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700621 switchToFragment(initialFragmentName, initialArguments, true, false,
622 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000623 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700624 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700625 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700626 // Show Search affordance
627 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700629 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700630 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800631 }
632 }
633
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700634 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700635 if (mActionBar != null) {
636 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
637 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
638 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700639 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
640
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800641 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
643
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700644 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 if (buttonBar != null) {
646 buttonBar.setVisibility(View.VISIBLE);
647
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700648 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 backButton.setOnClickListener(new OnClickListener() {
650 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700651 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 finish();
653 }
654 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700655 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 skipButton.setOnClickListener(new OnClickListener() {
657 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700658 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 finish();
660 }
661 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700662 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800663 mNextButton.setOnClickListener(new OnClickListener() {
664 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700665 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 finish();
667 }
668 });
669
670 // set our various button parameters
671 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
672 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
673 if (TextUtils.isEmpty(buttonText)) {
674 mNextButton.setVisibility(View.GONE);
675 }
676 else {
677 mNextButton.setText(buttonText);
678 }
679 }
680 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
681 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
682 if (TextUtils.isEmpty(buttonText)) {
683 backButton.setVisibility(View.GONE);
684 }
685 else {
686 backButton.setText(buttonText);
687 }
688 }
689 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
690 skipButton.setVisibility(View.VISIBLE);
691 }
692 }
693 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700694
695 mHomeActivitiesCount = getHomeActivitiesCount();
696 }
697
698 private int getHomeActivitiesCount() {
699 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
700 getPackageManager().getHomeActivities(homeApps);
701 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800702 }
703
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700704 private void setTitleFromIntent(Intent intent) {
705 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
706 if (initialTitleResId > 0) {
707 mInitialTitle = null;
708 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100709
710 final String initialTitleResPackageName = intent.getStringExtra(
711 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
712 if (initialTitleResPackageName != null) {
713 try {
714 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
715 0 /* flags */, new UserHandle(UserHandle.myUserId()));
716 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
717 setTitle(mInitialTitle);
718 mInitialTitleResId = -1;
719 return;
720 } catch (NameNotFoundException e) {
721 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
722 }
723 } else {
724 setTitle(mInitialTitleResId);
725 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700726 } else {
727 mInitialTitleResId = -1;
728 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
729 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
730 setTitle(mInitialTitle);
731 }
732 }
733
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800734 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800735 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700736 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800737 }
738
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700739 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800740 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700741
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800742 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700743 if (mInitialTitleResId > 0) {
744 setTitle(mInitialTitleResId);
745 } else {
746 setTitle(mInitialTitle);
747 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800749 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700750
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800751 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
752 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700753
754 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800755 }
756
757 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
758 final CharSequence title;
759 final int titleRes = bse.getBreadCrumbTitleRes();
760 if (titleRes > 0) {
761 title = getText(titleRes);
762 } else {
763 title = bse.getBreadCrumbTitle();
764 }
765 if (title != null) {
766 setTitle(title);
767 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800768 }
769
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800770 @Override
771 protected void onSaveInstanceState(Bundle outState) {
772 super.onSaveInstanceState(outState);
773
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700774 if (mCategories.size() > 0) {
775 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800776 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700777
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700778 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700779 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700780
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700781 if (mDisplaySearch) {
782 // The option menus are created if the ActionBar is visible and they are also created
783 // asynchronously. If you launch Settings with an Intent action like
784 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
785 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
786 // menu item and search view are null.
787 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
788 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700789
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700790 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
791 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
792 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700793
794 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800795 }
796
797 @Override
798 public void onResume() {
799 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400800 if (mIsShowingDashboard) {
801 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
802 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800803
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700804 final int newHomeActivityCount = getHomeActivitiesCount();
805 if (newHomeActivityCount != mHomeActivitiesCount) {
806 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700807 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700808 }
809
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800810 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
811 @Override
812 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700813 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800814 }
815 };
816 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
817 mDevelopmentPreferencesListener);
818
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800819 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700820
Svetoslav990159a2014-04-14 17:14:59 -0700821 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700822
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700823 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700824 onQueryTextSubmit(mSearchQuery);
825 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800826 }
827
828 @Override
829 public void onPause() {
830 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400831 if (mIsShowingDashboard) {
832 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
833 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800834 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700835 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800836 }
837
838 @Override
839 public void onDestroy() {
840 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700841
842 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
843 mDevelopmentPreferencesListener);
844 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800845 }
846
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800847 protected boolean isValidFragment(String fragmentName) {
848 // Almost all fragments are wrapped in this,
849 // except for a few that have their own activities.
850 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
851 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
852 }
853 return false;
854 }
855
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800856 @Override
857 public Intent getIntent() {
858 Intent superIntent = super.getIntent();
859 String startingFragment = getStartingFragmentClass(superIntent);
860 // This is called from super.onCreate, isMultiPane() is not yet reliable
861 // Do not use onIsHidingHeaders either, which relies itself on this method
862 if (startingFragment != null) {
863 Intent modIntent = new Intent(superIntent);
864 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
865 Bundle args = superIntent.getExtras();
866 if (args != null) {
867 args = new Bundle(args);
868 } else {
869 args = new Bundle();
870 }
871 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100872 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800873 return modIntent;
874 }
875 return superIntent;
876 }
877
878 /**
879 * Checks if the component name in the intent is different from the Settings class and
880 * returns the class name to load as a fragment.
881 */
882 private String getStartingFragmentClass(Intent intent) {
883 if (mFragmentClass != null) return mFragmentClass;
884
885 String intentClass = intent.getComponent().getClassName();
886 if (intentClass.equals(getClass().getName())) return null;
887
888 if ("com.android.settings.ManageApplications".equals(intentClass)
889 || "com.android.settings.RunningServices".equals(intentClass)
890 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
891 // Old names of manage apps.
892 intentClass = com.android.settings.applications.ManageApplications.class.getName();
893 }
894
895 return intentClass;
896 }
897
898 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000899 * Start a new fragment containing a preference panel. If the preferences
900 * are being displayed in multi-pane mode, the given fragment class will
901 * be instantiated and placed in the appropriate pane. If running in
902 * single-pane mode, a new activity will be launched in which to show the
903 * fragment.
904 *
905 * @param fragmentClass Full name of the class implementing the fragment.
906 * @param args Any desired arguments to supply to the fragment.
907 * @param titleRes Optional resource identifier of the title of this
908 * fragment.
909 * @param titleText Optional text of the title of this fragment.
910 * @param resultTo Optional fragment that result data should be sent to.
911 * If non-null, resultTo.onActivityResult() will be called when this
912 * preference panel is done. The launched panel must use
913 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
914 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700915 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000916 */
917 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700918 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700919 String title = null;
920 if (titleRes < 0) {
921 if (titleText != null) {
922 title = titleText.toString();
923 } else {
924 // There not much we can do in that case
925 title = "";
926 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700927 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700928 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700929 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000930 }
931
932 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100933 * Start a new fragment in a new activity containing a preference panel for a given user. If the
934 * preferences are being displayed in multi-pane mode, the given fragment class will be
935 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
936 * activity will be launched in which to show the fragment.
937 *
938 * @param fragmentClass Full name of the class implementing the fragment.
939 * @param args Any desired arguments to supply to the fragment.
940 * @param titleRes Optional resource identifier of the title of this fragment.
941 * @param titleText Optional text of the title of this fragment.
942 * @param userHandle The user for which the panel has to be started.
943 */
944 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
945 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700946 // This is a workaround.
947 //
948 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
949 // starting the fragment could cause a native stack corruption. See b/17523189. However,
950 // adding that flag and start the preference panel with the same UserHandler will make it
951 // impossible to use back button to return to the previous screen. See b/20042570.
952 //
953 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
954 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
955 // when we're calling it as the same user.
956 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
957 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
958 } else {
959 String title = null;
960 if (titleRes < 0) {
961 if (titleText != null) {
962 title = titleText.toString();
963 } else {
964 // There not much we can do in that case
965 title = "";
966 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100967 }
Lifu Tangd0332852015-04-02 12:05:46 -0700968 Utils.startWithFragmentAsUser(this, fragmentClass, args,
969 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100970 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100971 }
972
973 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800974 * Called by a preference panel fragment to finish itself.
975 *
976 * @param caller The fragment that is asking to be finished.
977 * @param resultCode Optional result code to send back to the original
978 * launching fragment.
979 * @param resultData Optional result data to send back to the original
980 * launching fragment.
981 */
982 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
983 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700984 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800985 }
986
987 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000988 * Start a new fragment.
989 *
990 * @param fragment The fragment to start
991 * @param push If true, the current fragment will be pushed onto the back stack. If false,
992 * the current fragment will be replaced.
993 */
994 public void startPreferenceFragment(Fragment fragment, boolean push) {
995 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700996 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000997 if (push) {
998 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
999 transaction.addToBackStack(BACK_STACK_PREFS);
1000 } else {
1001 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1002 }
1003 transaction.commitAllowingStateLoss();
1004 }
1005
1006 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001007 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1008 */
1009 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001010 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001011 if (validate && !isValidFragment(fragmentName)) {
1012 throw new IllegalArgumentException("Invalid fragment for this activity: "
1013 + fragmentName);
1014 }
1015 Fragment f = Fragment.instantiate(this, fragmentName, args);
1016 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001017 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001018 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001019 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001020 }
1021 if (addToBackStack) {
1022 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1023 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001024 if (titleResId > 0) {
1025 transaction.setBreadCrumbTitle(titleResId);
1026 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001027 transaction.setBreadCrumbTitle(title);
1028 }
1029 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001030 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001031 return f;
1032 }
1033
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001034 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001035 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001036 *
1037 * @param categories The list in which to place the tiles categories.
1038 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001039 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001040 categories.clear();
Jason Monk2acff552015-06-11 15:47:29 -04001041 loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001042 updateTilesList(categories);
1043 }
1044
1045 /**
1046 * Parse the given XML file as a categories description, adding each
1047 * parsed categories and tiles into the target list.
1048 *
1049 * @param resid The XML resource to load and parse.
1050 * @param target The list in which the parsed categories and tiles should be placed.
1051 */
Jason Monk2acff552015-06-11 15:47:29 -04001052 public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
1053 Context context) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001054 XmlResourceParser parser = null;
1055 try {
Jason Monk2acff552015-06-11 15:47:29 -04001056 parser = context.getResources().getXml(resid);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001057 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
Jason Monk2acff552015-06-11 15:47:29 -04001085 TypedArray sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001086 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 Monk2acff552015-06-11 15:47:29 -04001101 sa = context.obtainStyledAttributes(attrs,
1102 com.android.internal.R.styleable.Preference);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001103 tv = sa.peekValue(
1104 com.android.internal.R.styleable.Preference_key);
1105 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1106 if (tv.resourceId != 0) {
Jason Monk2acff552015-06-11 15:47:29 -04001107 category.key = context.getString(tv.resourceId);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001108 } else {
1109 category.key = tv.string.toString();
1110 }
1111 }
1112 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001113
1114 final int innerDepth = parser.getDepth();
1115 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1116 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1117 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1118 continue;
1119 }
1120
1121 String innerNodeName = parser.getName();
1122 if (innerNodeName.equals("dashboard-tile")) {
1123 DashboardTile tile = new DashboardTile();
1124
Jason Monk2acff552015-06-11 15:47:29 -04001125 sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001126 attrs, com.android.internal.R.styleable.PreferenceHeader);
1127 tile.id = sa.getResourceId(
1128 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001129 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001130 tv = sa.peekValue(
1131 com.android.internal.R.styleable.PreferenceHeader_title);
1132 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1133 if (tv.resourceId != 0) {
1134 tile.titleRes = tv.resourceId;
1135 } else {
1136 tile.title = tv.string;
1137 }
1138 }
1139 tv = sa.peekValue(
1140 com.android.internal.R.styleable.PreferenceHeader_summary);
1141 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1142 if (tv.resourceId != 0) {
1143 tile.summaryRes = tv.resourceId;
1144 } else {
1145 tile.summary = tv.string;
1146 }
1147 }
1148 tile.iconRes = sa.getResourceId(
1149 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1150 tile.fragment = sa.getString(
1151 com.android.internal.R.styleable.PreferenceHeader_fragment);
1152 sa.recycle();
1153
1154 if (curBundle == null) {
1155 curBundle = new Bundle();
1156 }
1157
1158 final int innerDepth2 = parser.getDepth();
1159 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1160 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1161 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1162 continue;
1163 }
1164
1165 String innerNodeName2 = parser.getName();
1166 if (innerNodeName2.equals("extra")) {
Jason Monk2acff552015-06-11 15:47:29 -04001167 context.getResources().parseBundleExtra("extra", attrs,
1168 curBundle);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001169 XmlUtils.skipCurrentTag(parser);
1170
1171 } else if (innerNodeName2.equals("intent")) {
Jason Monk2acff552015-06-11 15:47:29 -04001172 tile.intent = Intent.parseIntent(context.getResources(), parser,
1173 attrs);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001174
1175 } else {
1176 XmlUtils.skipCurrentTag(parser);
1177 }
1178 }
1179
1180 if (curBundle.size() > 0) {
1181 tile.fragmentArguments = curBundle;
1182 curBundle = null;
1183 }
1184
PauloftheWest38155612014-06-30 10:02:36 -07001185 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Jason Monk2acff552015-06-11 15:47:29 -04001186 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(context)){
PauloftheWest38155612014-06-30 10:02:36 -07001187 category.addTile(tile);
1188 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001189
Jason Monk2ebc8a02015-02-13 15:23:19 -05001190 } else if (innerNodeName.equals("external-tiles")) {
1191 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001192 } else {
1193 XmlUtils.skipCurrentTag(parser);
1194 }
1195 }
1196
1197 target.add(category);
1198 } else {
1199 XmlUtils.skipCurrentTag(parser);
1200 }
1201 }
1202
1203 } catch (XmlPullParserException e) {
1204 throw new RuntimeException("Error parsing categories", e);
1205 } catch (IOException e) {
1206 throw new RuntimeException("Error parsing categories", e);
1207 } finally {
1208 if (parser != null) parser.close();
1209 }
1210 }
1211
1212 private void updateTilesList(List<DashboardCategory> target) {
1213 final boolean showDev = mDevelopmentPreferences.getBoolean(
1214 DevelopmentSettings.PREF_SHOW,
1215 android.os.Build.TYPE.equals("eng"));
1216
Xiaohui Chen44879a32015-07-22 13:53:22 -07001217 final UserManager um = UserManager.get(this);
1218 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001219
1220 final int size = target.size();
1221 for (int i = 0; i < size; i++) {
1222
1223 DashboardCategory category = target.get(i);
1224
1225 // Ids are integers, so downcasting is ok
1226 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001227 int n = category.getTilesCount() - 1;
1228 while (n >= 0) {
1229
1230 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001231 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001232 id = (int) tile.id;
1233 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001234 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1235 removeTile = true;
1236 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001237 } else if (id == R.id.wifi_settings) {
1238 // Remove WiFi Settings if WiFi service is not available.
1239 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001240 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001241 }
1242 } else if (id == R.id.bluetooth_settings) {
1243 // Remove Bluetooth Settings if Bluetooth service is not available.
1244 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001245 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001246 }
1247 } else if (id == R.id.data_usage_settings) {
1248 // Remove data usage when kernel module not enabled
Jason Monkb45e27b2015-05-20 13:35:43 -04001249 if (!Utils.isBandwidthControlEnabled()) {
1250 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001251 }
1252 } else if (id == R.id.battery_settings) {
1253 // Remove battery settings when battery is not available. (e.g. TV)
1254
1255 if (!mBatteryPresent) {
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.home_settings) {
1259 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001260 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001261 }
1262 } else if (id == R.id.user_settings) {
1263 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001264 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001266 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 }
1268 } else if (id == R.id.nfc_payment_settings) {
1269 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001270 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001271 } else {
1272 // Only show if NFC is on and we have the HCE feature
1273 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001274 if (adapter == null || !adapter.isEnabled() ||
1275 !getPackageManager().hasSystemFeature(
1276 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001277 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001278 }
1279 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001280 } else if (id == R.id.print_settings) {
1281 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1282 PackageManager.FEATURE_PRINTING);
1283 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001284 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001285 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001286 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001287 if (!showDev || um.hasUserRestriction(
1288 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001289 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001290 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001291 }
1292
Xiaohui Chen44879a32015-07-22 13:53:22 -07001293 if (UserHandle.MU_ENABLED && !isAdmin
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001294 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1295 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001296 }
1297
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001298 if (removeTile && n < category.getTilesCount()) {
1299 category.removeTile(n);
1300 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001301 n--;
1302 }
1303 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001304 addExternalTiles(target);
1305 }
1306
1307 private void addExternalTiles(List<DashboardCategory> target) {
1308 Map<Pair<String, String>, DashboardTile> addedCache =
1309 new ArrayMap<Pair<String, String>, DashboardTile>();
1310 UserManager userManager = UserManager.get(this);
1311 for (UserHandle user : userManager.getUserProfiles()) {
1312 addExternalTiles(target, user, addedCache);
1313 }
1314 }
1315
1316 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1317 Map<Pair<String, String>, DashboardTile> addedCache) {
1318 PackageManager pm = getPackageManager();
1319 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1320 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1321 PackageManager.GET_META_DATA, user.getIdentifier());
1322 for (ResolveInfo resolved : results) {
1323 if (!resolved.system) {
1324 // Do not allow any app to add to settings, only system ones.
1325 continue;
1326 }
1327 ActivityInfo activityInfo = resolved.activityInfo;
1328 Bundle metaData = activityInfo.metaData;
1329 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1330 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1331 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1332 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1333 continue;
1334 }
1335 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1336 DashboardCategory category = getCategory(target, categoryKey);
1337 if (category == null) {
1338 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1339 + "category key " + categoryKey);
1340 continue;
1341 }
1342 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1343 activityInfo.name);
1344 DashboardTile tile = addedCache.get(key);
1345 if (tile == null) {
1346 tile = new DashboardTile();
1347 tile.intent = new Intent().setClassName(
1348 activityInfo.packageName, activityInfo.name);
1349 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1350
1351 if (category.externalIndex == -1) {
1352 // If no location for external tiles has been specified for this category,
1353 // then just put them at the end.
1354 category.addTile(tile);
1355 } else {
1356 category.addTile(category.externalIndex, tile);
1357 }
1358 addedCache.put(key, tile);
1359 }
1360 tile.userHandle.add(user);
1361 }
1362 }
1363
1364 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1365 for (DashboardCategory category : target) {
1366 if (categoryKey.equals(category.key)) {
1367 return category;
1368 }
1369 }
1370 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001371 }
1372
1373 private boolean updateHomeSettingTiles(DashboardTile tile) {
1374 // Once we decide to show Home settings, keep showing it forever
1375 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1376 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1377 return true;
1378 }
1379
1380 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001381 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001382 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001383 // When there's only one available home app, omit this settings
1384 // category entirely at the top level UI. If the user just
1385 // uninstalled the penultimate home app candidiate, we also
1386 // now tell them about why they aren't seeing 'Home' in the list.
1387 if (sShowNoHomeNotice) {
1388 sShowNoHomeNotice = false;
1389 NoHomeDialogFragment.show(this);
1390 }
1391 return false;
1392 } else {
1393 // Okay, we're allowing the Home settings category. Tell it, when
1394 // invoked via this front door, that we'll need to be told about the
1395 // case when the user uninstalls all but one home app.
1396 if (tile.fragmentArguments == null) {
1397 tile.fragmentArguments = new Bundle();
1398 }
1399 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1400 }
1401 } catch (Exception e) {
1402 // Can't look up the home activity; bail on configuring the icon
1403 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1404 }
1405
1406 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1407 return true;
1408 }
1409
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001410 private void getMetaData() {
1411 try {
1412 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1413 PackageManager.GET_META_DATA);
1414 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001415 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1416 } catch (NameNotFoundException nnfe) {
1417 // No recovery
1418 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1419 }
1420 }
1421
1422 // give subclasses access to the Next button
1423 public boolean hasNextButton() {
1424 return mNextButton != null;
1425 }
1426
1427 public Button getNextButton() {
1428 return mNextButton;
1429 }
1430
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001431 @Override
1432 public boolean shouldUpRecreateTask(Intent targetIntent) {
1433 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1434 }
1435
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001436 public static void requestHomeNotice() {
1437 sShowNoHomeNotice = true;
1438 }
1439
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001440 @Override
1441 public boolean onQueryTextSubmit(String query) {
1442 switchToSearchResultsFragmentIfNeeded();
1443 mSearchQuery = query;
1444 return mSearchResultsFragment.onQueryTextSubmit(query);
1445 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001446
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001447 @Override
1448 public boolean onQueryTextChange(String newText) {
1449 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001450 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001451 return false;
1452 }
1453 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001454 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001455
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001456 @Override
1457 public boolean onClose() {
1458 return false;
1459 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001460
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001461 @Override
1462 public boolean onMenuItemActionExpand(MenuItem item) {
1463 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001464 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001465 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001466 return true;
1467 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001468
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001469 @Override
1470 public boolean onMenuItemActionCollapse(MenuItem item) {
1471 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001472 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001473 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001474 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001475 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001476 return true;
1477 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001478
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001479 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001480 if (mSearchResultsFragment != null) {
1481 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001482 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001483 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001484 if (current != null && current instanceof SearchResultsSummary) {
1485 mSearchResultsFragment = (SearchResultsSummary) current;
1486 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001487 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001488 SearchResultsSummary.class.getName(), null, false, true,
1489 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001491 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001493 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001494
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001495 public void needToRevertToInitialFragment() {
1496 mNeedToRevertToInitialFragment = true;
1497 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001498
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001499 private void revertToInitialFragment() {
1500 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001501 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001502 mSearchMenuItemExpanded = false;
1503 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1504 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001505 if (mSearchMenuItem != null) {
1506 mSearchMenuItem.collapseActionView();
1507 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001508 }
Jim Miller0698a212014-10-16 19:49:07 -07001509
1510 public Intent getResultIntentData() {
1511 return mResultIntentData;
1512 }
1513
1514 public void setResultIntentData(Intent resultIntentData) {
1515 mResultIntentData = resultIntentData;
1516 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001517}