blob: f4cc35986e145e54221b3f4fbe0d3daa03e43604 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason Monk2ebc8a02015-02-13 15:23:19 -050019import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
20
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.ActionBar;
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.nfc.NfcAdapter;
40import android.os.Bundle;
41import android.os.Handler;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080043import android.os.UserHandle;
44import android.os.UserManager;
45import android.preference.Preference;
46import android.preference.PreferenceFragment;
47import android.preference.PreferenceManager;
48import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050051import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.AttributeSet;
53import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.TypedValue;
56import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070057import android.view.Menu;
58import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080059import android.view.MenuItem;
60import android.view.View;
61import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070062import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080063import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070064import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040065
Chris Wren8a963ba2015-03-20 10:29:14 -040066import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080067import com.android.internal.util.ArrayUtils;
68import com.android.internal.util.XmlUtils;
69import com.android.settings.accessibility.AccessibilitySettings;
70import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000071import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070073import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.applications.ManageApplications;
Xiyuan Xia86a55402015-06-02 14:55:32 -070075import com.android.settings.applications.ManageAssist;
Jason Monk2cdafc62015-06-12 12:32:50 -040076import com.android.settings.applications.ProcessStatsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040078import com.android.settings.applications.UsageAccessDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070080import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070082import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070083import com.android.settings.dashboard.NoHomeDialogFragment;
84import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070085import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070086import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070087import com.android.settings.deviceinfo.PublicVolumeSettings;
88import com.android.settings.deviceinfo.StorageSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040089import com.android.settings.fuelgauge.BatterySaverSettings;
Jason Monk1eb54eb2015-04-29 12:46:42 -040090import com.android.settings.fuelgauge.PowerUsageDetail;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080091import com.android.settings.fuelgauge.PowerUsageSummary;
92import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
93import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
94import com.android.settings.inputmethod.SpellCheckersSettings;
95import com.android.settings.inputmethod.UserDictionaryList;
96import com.android.settings.location.LocationSettings;
97import com.android.settings.nfc.AndroidBeam;
98import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040099import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400100import com.android.settings.notification.NotificationAccessSettings;
101import com.android.settings.notification.NotificationSettings;
102import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500103import com.android.settings.notification.OtherSoundSettings;
John Spurlock08531a82015-05-07 17:45:43 -0400104import com.android.settings.notification.ZenAccessSettings;
John Spurlock533a5662015-06-19 10:47:36 -0400105import com.android.settings.notification.ZenModeAutomationSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400106import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400107import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400108import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400109import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400110import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.print.PrintJobSettingsFragment;
112import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500113import com.android.settings.search.DynamicIndexableContentMonitor;
114import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700115import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import com.android.settings.tts.TextToSpeechSettings;
117import com.android.settings.users.UserSettings;
118import com.android.settings.vpn2.VpnSettings;
119import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700120import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700122import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123import com.android.settings.wifi.WifiSettings;
124import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500125
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126import org.xmlpull.v1.XmlPullParser;
127import org.xmlpull.v1.XmlPullParserException;
128
129import java.io.IOException;
130import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500132import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700133import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800134
135public class SettingsActivity extends Activity
136 implements PreferenceManager.OnPreferenceTreeClickListener,
137 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100138 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700139 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100140 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 private static final String LOG_TAG = "Settings";
143
144 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700145 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700146 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
147 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700148 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700149 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700150 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151
152 /**
153 * When starting this activity, the invoking Intent can contain this extra
154 * string to specify which fragment should be initially displayed.
155 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
156 * will call isValidFragment() to confirm that the fragment class name is valid for this
157 * activity.
158 */
159 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
160
161 /**
162 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
163 * this extra can also be specified to supply a Bundle of arguments to pass
164 * to that fragment when it is instantiated during the initial creation
165 * of the activity.
166 */
167 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
168
169 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700170 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
171 */
172 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
173
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800174 public static final String BACK_STACK_PREFS = ":settings:prefs";
175
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800176 // extras that allow any preference activity to be launched as part of a wizard
177
178 // show Back and Next buttons? takes boolean parameter
179 // Back will then return RESULT_CANCELED and Next RESULT_OK
180 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
181
182 // add a Skip button?
183 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
184
185 // specify custom text for the Back or Next buttons, or cause a button to not appear
186 // at all by setting it to null
187 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
188 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
189
190 /**
191 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700192 * 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 -0800193 * that fragment.
194 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700195 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100196 /**
197 * The package name used to resolve the title resource id.
198 */
199 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
200 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700201 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
202 ":settings:show_fragment_title_resid";
203 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
204 ":settings:show_fragment_as_shortcut";
205
206 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
207 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 private static final String META_DATA_KEY_FRAGMENT_CLASS =
210 "com.android.settings.FRAGMENT_CLASS";
211
212 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
213
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700214 private static final String EMPTY_QUERY = "";
215
Jason Monk2ebc8a02015-02-13 15:23:19 -0500216 /**
217 * Settings will search for system activities of this action and add them as a top level
218 * settings tile using the following parameters.
219 *
220 * <p>A category must be specified in the meta-data for the activity named
221 * {@link #EXTRA_CATEGORY_KEY}
222 *
223 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
224 * otherwise the label for the activity will be used.
225 *
226 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
227 * otherwise the icon for the activity will be used.
228 *
229 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
230 */
231 private static final String EXTRA_SETTINGS_ACTION =
232 "com.android.settings.action.EXTRA_SETTINGS";
233
234 /**
235 * The key used to get the category from metadata of activities of action
236 * {@link #EXTRA_SETTINGS_ACTION}
237 * The value must be one of:
238 * <li>com.android.settings.category.wireless</li>
239 * <li>com.android.settings.category.device</li>
240 * <li>com.android.settings.category.personal</li>
241 * <li>com.android.settings.category.system</li>
242 */
243 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
244
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800245 private static boolean sShowNoHomeNotice = false;
246
247 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800249 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700250 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800251
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800252 // Show only these settings for restricted users
253 private int[] SETTINGS_FOR_RESTRICTED = {
254 R.id.wireless_section,
255 R.id.wifi_settings,
256 R.id.bluetooth_settings,
257 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700258 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 R.id.wireless_settings,
260 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400261 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800262 R.id.display_settings,
263 R.id.storage_settings,
264 R.id.application_settings,
265 R.id.battery_settings,
266 R.id.personal_section,
267 R.id.location_settings,
268 R.id.security_settings,
269 R.id.language_settings,
270 R.id.user_settings,
271 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272 R.id.system_section,
273 R.id.date_time_settings,
274 R.id.about_settings,
275 R.id.accessibility_settings,
276 R.id.print_settings,
277 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800278 R.id.home_settings,
279 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800280 };
281
282 private static final String[] ENTRY_FRAGMENTS = {
283 WirelessSettings.class.getName(),
284 WifiSettings.class.getName(),
285 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700286 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700288 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 TetherSettings.class.getName(),
290 WifiP2pSettings.class.getName(),
291 VpnSettings.class.getName(),
292 DateTimeSettings.class.getName(),
293 LocalePicker.class.getName(),
294 InputMethodAndLanguageSettings.class.getName(),
295 SpellCheckersSettings.class.getName(),
296 UserDictionaryList.class.getName(),
297 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700298 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 DisplaySettings.class.getName(),
300 DeviceInfoSettings.class.getName(),
301 ManageApplications.class.getName(),
Xiyuan Xia86a55402015-06-02 14:55:32 -0700302 ManageAssist.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 ProcessStatsUi.class.getName(),
304 NotificationStation.class.getName(),
305 LocationSettings.class.getName(),
306 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400307 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 PrivacySettings.class.getName(),
309 DeviceAdminSettings.class.getName(),
310 AccessibilitySettings.class.getName(),
311 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
313 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700314 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700315 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700316 PrivateVolumeSettings.class.getName(),
317 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800319 AndroidBeam.class.getName(),
320 WifiDisplaySettings.class.getName(),
321 PowerUsageSummary.class.getName(),
322 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000323 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 CryptKeeperSettings.class.getName(),
325 DataUsageSummary.class.getName(),
326 DreamSettings.class.getName(),
327 UserSettings.class.getName(),
328 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400329 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800330 PrintSettingsFragment.class.getName(),
331 PrintJobSettingsFragment.class.getName(),
332 TrustedCredentialsSettings.class.getName(),
333 PaymentSettings.class.getName(),
334 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700335 ZenModeSettings.class.getName(),
336 NotificationSettings.class.getName(),
337 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
338 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400339 InstalledAppDetails.class.getName(),
340 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700341 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700342 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000343 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400344 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400345 ZenModePrioritySettings.class.getName(),
John Spurlock533a5662015-06-19 10:47:36 -0400346 ZenModeAutomationSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400347 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400348 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400349 ZenModeExternalRuleSettings.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(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 };
354
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700355
356 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
357 "android.settings.APPLICATION_DETAILS_SETTINGS"
358 };
359
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 private SharedPreferences mDevelopmentPreferences;
361 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
362
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 private boolean mBatteryPresent = true;
364 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800365 @Override
366 public void onReceive(Context context, Intent intent) {
367 String action = intent.getAction();
368 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
369 boolean batteryPresent = Utils.isBatteryPresent(intent);
370
371 if (mBatteryPresent != batteryPresent) {
372 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700373 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800374 }
375 }
376 }
377 };
378
Svetoslav990159a2014-04-14 17:14:59 -0700379 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
380 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700381
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700382 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700383 private SwitchBar mSwitchBar;
384
385 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700386
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700387 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700388 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700389
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700390 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700391 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700392
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700393 private ViewGroup mContent;
394
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700395 private SearchView mSearchView;
396 private MenuItem mSearchMenuItem;
397 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700398 private SearchResultsSummary mSearchResultsFragment;
399 private String mSearchQuery;
400
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700401 // Categories
402 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800403
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700404 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700405 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800406 private Handler mHandler = new Handler() {
407 @Override
408 public void handleMessage(Message msg) {
409 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700410 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700411 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
412 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700413 buildDashboardCategories(mCategories);
414 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800415 } break;
416 }
417 }
418 };
419
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700420 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700421 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700422
Jim Miller0698a212014-10-16 19:49:07 -0700423 private Intent mResultIntentData;
424
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700425 public SwitchBar getSwitchBar() {
426 return mSwitchBar;
427 }
428
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700429 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
430 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700431 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700432 }
433 return mCategories;
434 }
435
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800436 @Override
437 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
438 // Override the fragment title for Wallpaper settings
439 int titleRes = pref.getTitleRes();
440 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
441 titleRes = R.string.wallpaper_settings_fragment_title;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800442 }
443 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
444 null, 0);
445 return true;
446 }
447
448 @Override
449 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
450 return false;
451 }
452
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700453 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700454 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700455 Message msg = new Message();
456 msg.what = MSG_BUILD_CATEGORIES;
457 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800458 }
459 }
460
461 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800462 public void onConfigurationChanged(Configuration newConfig) {
463 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800464 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465 }
466
467 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700468 protected void onStart() {
469 super.onStart();
470
471 if (mNeedToRevertToInitialFragment) {
472 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 }
475
476 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700477 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700478 if (!mDisplaySearch) {
479 return false;
480 }
481
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700482 MenuInflater inflater = getMenuInflater();
483 inflater.inflate(R.menu.options_menu, menu);
484
485 // Cache the search query (can be overriden by the OnQueryTextListener)
486 final String query = mSearchQuery;
487
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700488 mSearchMenuItem = menu.findItem(R.id.search);
489 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700490
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700491 if (mSearchMenuItem == null || mSearchView == null) {
492 return false;
493 }
494
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700495 if (mSearchResultsFragment != null) {
496 mSearchResultsFragment.setSearchView(mSearchView);
497 }
498
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700499 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700500 mSearchView.setOnQueryTextListener(this);
501 mSearchView.setOnCloseListener(this);
502
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700503 if (mSearchMenuItemExpanded) {
504 mSearchMenuItem.expandActionView();
505 }
506 mSearchView.setQuery(query, true /* submit */);
507
508 return true;
509 }
510
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700511 private static boolean isShortCutIntent(final Intent intent) {
512 Set<String> categories = intent.getCategories();
513 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
514 }
515
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700516 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700517 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700518 if (action == null) {
519 return false;
520 }
521 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
522 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
523 }
524 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700525 }
526
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700527 @Override
528 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700529 super.onCreate(savedState);
530
531 // Should happen before any call to getIntent()
532 getMetaData();
533
534 final Intent intent = getIntent();
535 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
536 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
540 Context.MODE_PRIVATE);
541
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700542 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700543 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700544
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700545 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700546 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
547
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700548 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700549 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700550
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700551 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700552
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700553 // This is a "Sub Settings" when:
554 // - this is a real SubSettings
555 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700556 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700557 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
558
559 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
560 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700561 // Check also that we are not a Theme Dialog as we don't want to override them
562 final int themeResId = getThemeResId();
563 if (themeResId != R.style.Theme_DialogWhenLarge &&
564 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
565 setTheme(R.style.Theme_SubSettings);
566 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700567 }
568
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700569 setContentView(mIsShowingDashboard ?
570 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700572 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700573
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 getFragmentManager().addOnBackStackChangedListener(this);
575
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700576 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800577 // Run the Index update only if we have some space
578 if (!Utils.isLowStorage(this)) {
579 Index.getInstance(getApplicationContext()).update();
580 } else {
581 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
582 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700583 }
584
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700585 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700586 // We are restarting from a previous saved state; used that to initialize, instead
587 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700588 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
589 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700591 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800592
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700593 ArrayList<DashboardCategory> categories =
594 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
595 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700596 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700597 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700598 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700600
601 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700602 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700603 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
604 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800605 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700606 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400607 mDisplaySearch = false;
608 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700609 if (mIsShortcut) {
610 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700611 } else if (isSubSettings) {
612 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700613 } else {
614 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700615 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700616 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700617
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700618 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700619 switchToFragment(initialFragmentName, initialArguments, true, false,
620 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000621 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700622 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700623 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700624 // Show Search affordance
625 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700626 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700627 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800629 }
630 }
631
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700632 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700633 if (mActionBar != null) {
634 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
635 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
636 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700637 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
638
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800639 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
641
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700642 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800643 if (buttonBar != null) {
644 buttonBar.setVisibility(View.VISIBLE);
645
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700646 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 backButton.setOnClickListener(new OnClickListener() {
648 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700649 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 finish();
651 }
652 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700653 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 skipButton.setOnClickListener(new OnClickListener() {
655 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700656 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 finish();
658 }
659 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700660 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 mNextButton.setOnClickListener(new OnClickListener() {
662 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700663 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800664 finish();
665 }
666 });
667
668 // set our various button parameters
669 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
670 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
671 if (TextUtils.isEmpty(buttonText)) {
672 mNextButton.setVisibility(View.GONE);
673 }
674 else {
675 mNextButton.setText(buttonText);
676 }
677 }
678 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
679 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
680 if (TextUtils.isEmpty(buttonText)) {
681 backButton.setVisibility(View.GONE);
682 }
683 else {
684 backButton.setText(buttonText);
685 }
686 }
687 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
688 skipButton.setVisibility(View.VISIBLE);
689 }
690 }
691 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700692
693 mHomeActivitiesCount = getHomeActivitiesCount();
694 }
695
696 private int getHomeActivitiesCount() {
697 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
698 getPackageManager().getHomeActivities(homeApps);
699 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800700 }
701
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700702 private void setTitleFromIntent(Intent intent) {
703 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
704 if (initialTitleResId > 0) {
705 mInitialTitle = null;
706 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100707
708 final String initialTitleResPackageName = intent.getStringExtra(
709 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
710 if (initialTitleResPackageName != null) {
711 try {
712 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
713 0 /* flags */, new UserHandle(UserHandle.myUserId()));
714 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
715 setTitle(mInitialTitle);
716 mInitialTitleResId = -1;
717 return;
718 } catch (NameNotFoundException e) {
719 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
720 }
721 } else {
722 setTitle(mInitialTitleResId);
723 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700724 } else {
725 mInitialTitleResId = -1;
726 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
727 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
728 setTitle(mInitialTitle);
729 }
730 }
731
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800732 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700734 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800735 }
736
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700737 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800738 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700739
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800740 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700741 if (mInitialTitleResId > 0) {
742 setTitle(mInitialTitleResId);
743 } else {
744 setTitle(mInitialTitle);
745 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700746 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800747 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800749 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
750 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700751
752 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800753 }
754
755 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
756 final CharSequence title;
757 final int titleRes = bse.getBreadCrumbTitleRes();
758 if (titleRes > 0) {
759 title = getText(titleRes);
760 } else {
761 title = bse.getBreadCrumbTitle();
762 }
763 if (title != null) {
764 setTitle(title);
765 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800766 }
767
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800768 @Override
769 protected void onSaveInstanceState(Bundle outState) {
770 super.onSaveInstanceState(outState);
771
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700772 if (mCategories.size() > 0) {
773 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700775
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700776 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700777 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700778
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700779 if (mDisplaySearch) {
780 // The option menus are created if the ActionBar is visible and they are also created
781 // asynchronously. If you launch Settings with an Intent action like
782 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
783 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
784 // menu item and search view are null.
785 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
786 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700787
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700788 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
789 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
790 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700791
792 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800793 }
794
795 @Override
796 public void onResume() {
797 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400798 if (mIsShowingDashboard) {
799 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
800 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800801
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700802 final int newHomeActivityCount = getHomeActivitiesCount();
803 if (newHomeActivityCount != mHomeActivitiesCount) {
804 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700805 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700806 }
807
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800808 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
809 @Override
810 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700811 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800812 }
813 };
814 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
815 mDevelopmentPreferencesListener);
816
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800817 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700818
Svetoslav990159a2014-04-14 17:14:59 -0700819 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700820
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700821 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700822 onQueryTextSubmit(mSearchQuery);
823 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800824 }
825
826 @Override
827 public void onPause() {
828 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400829 if (mIsShowingDashboard) {
830 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
831 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800832 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700833 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800834 }
835
836 @Override
837 public void onDestroy() {
838 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700839
840 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
841 mDevelopmentPreferencesListener);
842 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800843 }
844
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800845 protected boolean isValidFragment(String fragmentName) {
846 // Almost all fragments are wrapped in this,
847 // except for a few that have their own activities.
848 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
849 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
850 }
851 return false;
852 }
853
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 @Override
855 public Intent getIntent() {
856 Intent superIntent = super.getIntent();
857 String startingFragment = getStartingFragmentClass(superIntent);
858 // This is called from super.onCreate, isMultiPane() is not yet reliable
859 // Do not use onIsHidingHeaders either, which relies itself on this method
860 if (startingFragment != null) {
861 Intent modIntent = new Intent(superIntent);
862 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
863 Bundle args = superIntent.getExtras();
864 if (args != null) {
865 args = new Bundle(args);
866 } else {
867 args = new Bundle();
868 }
869 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100870 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800871 return modIntent;
872 }
873 return superIntent;
874 }
875
876 /**
877 * Checks if the component name in the intent is different from the Settings class and
878 * returns the class name to load as a fragment.
879 */
880 private String getStartingFragmentClass(Intent intent) {
881 if (mFragmentClass != null) return mFragmentClass;
882
883 String intentClass = intent.getComponent().getClassName();
884 if (intentClass.equals(getClass().getName())) return null;
885
886 if ("com.android.settings.ManageApplications".equals(intentClass)
887 || "com.android.settings.RunningServices".equals(intentClass)
888 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
889 // Old names of manage apps.
890 intentClass = com.android.settings.applications.ManageApplications.class.getName();
891 }
892
893 return intentClass;
894 }
895
896 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000897 * Start a new fragment containing a preference panel. If the preferences
898 * are being displayed in multi-pane mode, the given fragment class will
899 * be instantiated and placed in the appropriate pane. If running in
900 * single-pane mode, a new activity will be launched in which to show the
901 * fragment.
902 *
903 * @param fragmentClass Full name of the class implementing the fragment.
904 * @param args Any desired arguments to supply to the fragment.
905 * @param titleRes Optional resource identifier of the title of this
906 * fragment.
907 * @param titleText Optional text of the title of this fragment.
908 * @param resultTo Optional fragment that result data should be sent to.
909 * If non-null, resultTo.onActivityResult() will be called when this
910 * preference panel is done. The launched panel must use
911 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
912 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700913 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000914 */
915 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700916 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700917 String title = null;
918 if (titleRes < 0) {
919 if (titleText != null) {
920 title = titleText.toString();
921 } else {
922 // There not much we can do in that case
923 title = "";
924 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700925 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700926 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700927 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000928 }
929
930 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100931 * Start a new fragment in a new activity containing a preference panel for a given user. If the
932 * preferences are being displayed in multi-pane mode, the given fragment class will be
933 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
934 * activity will be launched in which to show the fragment.
935 *
936 * @param fragmentClass Full name of the class implementing the fragment.
937 * @param args Any desired arguments to supply to the fragment.
938 * @param titleRes Optional resource identifier of the title of this fragment.
939 * @param titleText Optional text of the title of this fragment.
940 * @param userHandle The user for which the panel has to be started.
941 */
942 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
943 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700944 // This is a workaround.
945 //
946 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
947 // starting the fragment could cause a native stack corruption. See b/17523189. However,
948 // adding that flag and start the preference panel with the same UserHandler will make it
949 // impossible to use back button to return to the previous screen. See b/20042570.
950 //
951 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
952 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
953 // when we're calling it as the same user.
954 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
955 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
956 } else {
957 String title = null;
958 if (titleRes < 0) {
959 if (titleText != null) {
960 title = titleText.toString();
961 } else {
962 // There not much we can do in that case
963 title = "";
964 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100965 }
Lifu Tangd0332852015-04-02 12:05:46 -0700966 Utils.startWithFragmentAsUser(this, fragmentClass, args,
967 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100968 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100969 }
970
971 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800972 * Called by a preference panel fragment to finish itself.
973 *
974 * @param caller The fragment that is asking to be finished.
975 * @param resultCode Optional result code to send back to the original
976 * launching fragment.
977 * @param resultData Optional result data to send back to the original
978 * launching fragment.
979 */
980 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
981 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700982 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800983 }
984
985 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000986 * Start a new fragment.
987 *
988 * @param fragment The fragment to start
989 * @param push If true, the current fragment will be pushed onto the back stack. If false,
990 * the current fragment will be replaced.
991 */
992 public void startPreferenceFragment(Fragment fragment, boolean push) {
993 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700994 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000995 if (push) {
996 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
997 transaction.addToBackStack(BACK_STACK_PREFS);
998 } else {
999 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1000 }
1001 transaction.commitAllowingStateLoss();
1002 }
1003
1004 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001005 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1006 */
1007 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001008 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001009 if (validate && !isValidFragment(fragmentName)) {
1010 throw new IllegalArgumentException("Invalid fragment for this activity: "
1011 + fragmentName);
1012 }
1013 Fragment f = Fragment.instantiate(this, fragmentName, args);
1014 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001015 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001016 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001017 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001018 }
1019 if (addToBackStack) {
1020 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1021 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001022 if (titleResId > 0) {
1023 transaction.setBreadCrumbTitle(titleResId);
1024 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001025 transaction.setBreadCrumbTitle(title);
1026 }
1027 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001028 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001029 return f;
1030 }
1031
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001032 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001033 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001034 *
1035 * @param categories The list in which to place the tiles categories.
1036 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001037 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001038 categories.clear();
Jason Monk2acff552015-06-11 15:47:29 -04001039 loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001040 updateTilesList(categories);
1041 }
1042
1043 /**
1044 * Parse the given XML file as a categories description, adding each
1045 * parsed categories and tiles into the target list.
1046 *
1047 * @param resid The XML resource to load and parse.
1048 * @param target The list in which the parsed categories and tiles should be placed.
1049 */
Jason Monk2acff552015-06-11 15:47:29 -04001050 public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
1051 Context context) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001052 XmlResourceParser parser = null;
1053 try {
Jason Monk2acff552015-06-11 15:47:29 -04001054 parser = context.getResources().getXml(resid);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001055 AttributeSet attrs = Xml.asAttributeSet(parser);
1056
1057 int type;
1058 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1059 && type != XmlPullParser.START_TAG) {
1060 // Parse next until start tag is found
1061 }
1062
1063 String nodeName = parser.getName();
1064 if (!"dashboard-categories".equals(nodeName)) {
1065 throw new RuntimeException(
1066 "XML document must start with <preference-categories> tag; found"
1067 + nodeName + " at " + parser.getPositionDescription());
1068 }
1069
1070 Bundle curBundle = null;
1071
1072 final int outerDepth = parser.getDepth();
1073 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1074 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1075 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1076 continue;
1077 }
1078
1079 nodeName = parser.getName();
1080 if ("dashboard-category".equals(nodeName)) {
1081 DashboardCategory category = new DashboardCategory();
1082
Jason Monk2acff552015-06-11 15:47:29 -04001083 TypedArray sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001084 attrs, com.android.internal.R.styleable.PreferenceHeader);
1085 category.id = sa.getResourceId(
1086 com.android.internal.R.styleable.PreferenceHeader_id,
1087 (int)DashboardCategory.CAT_ID_UNDEFINED);
1088
1089 TypedValue tv = sa.peekValue(
1090 com.android.internal.R.styleable.PreferenceHeader_title);
1091 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1092 if (tv.resourceId != 0) {
1093 category.titleRes = tv.resourceId;
1094 } else {
1095 category.title = tv.string;
1096 }
1097 }
1098 sa.recycle();
Jason Monk2acff552015-06-11 15:47:29 -04001099 sa = context.obtainStyledAttributes(attrs,
1100 com.android.internal.R.styleable.Preference);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001101 tv = sa.peekValue(
1102 com.android.internal.R.styleable.Preference_key);
1103 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1104 if (tv.resourceId != 0) {
Jason Monk2acff552015-06-11 15:47:29 -04001105 category.key = context.getString(tv.resourceId);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001106 } else {
1107 category.key = tv.string.toString();
1108 }
1109 }
1110 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001111
1112 final int innerDepth = parser.getDepth();
1113 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1114 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1115 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1116 continue;
1117 }
1118
1119 String innerNodeName = parser.getName();
1120 if (innerNodeName.equals("dashboard-tile")) {
1121 DashboardTile tile = new DashboardTile();
1122
Jason Monk2acff552015-06-11 15:47:29 -04001123 sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001124 attrs, com.android.internal.R.styleable.PreferenceHeader);
1125 tile.id = sa.getResourceId(
1126 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001127 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001128 tv = sa.peekValue(
1129 com.android.internal.R.styleable.PreferenceHeader_title);
1130 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1131 if (tv.resourceId != 0) {
1132 tile.titleRes = tv.resourceId;
1133 } else {
1134 tile.title = tv.string;
1135 }
1136 }
1137 tv = sa.peekValue(
1138 com.android.internal.R.styleable.PreferenceHeader_summary);
1139 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1140 if (tv.resourceId != 0) {
1141 tile.summaryRes = tv.resourceId;
1142 } else {
1143 tile.summary = tv.string;
1144 }
1145 }
1146 tile.iconRes = sa.getResourceId(
1147 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1148 tile.fragment = sa.getString(
1149 com.android.internal.R.styleable.PreferenceHeader_fragment);
1150 sa.recycle();
1151
1152 if (curBundle == null) {
1153 curBundle = new Bundle();
1154 }
1155
1156 final int innerDepth2 = parser.getDepth();
1157 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1158 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1159 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1160 continue;
1161 }
1162
1163 String innerNodeName2 = parser.getName();
1164 if (innerNodeName2.equals("extra")) {
Jason Monk2acff552015-06-11 15:47:29 -04001165 context.getResources().parseBundleExtra("extra", attrs,
1166 curBundle);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001167 XmlUtils.skipCurrentTag(parser);
1168
1169 } else if (innerNodeName2.equals("intent")) {
Jason Monk2acff552015-06-11 15:47:29 -04001170 tile.intent = Intent.parseIntent(context.getResources(), parser,
1171 attrs);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001172
1173 } else {
1174 XmlUtils.skipCurrentTag(parser);
1175 }
1176 }
1177
1178 if (curBundle.size() > 0) {
1179 tile.fragmentArguments = curBundle;
1180 curBundle = null;
1181 }
1182
PauloftheWest38155612014-06-30 10:02:36 -07001183 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Jason Monk2acff552015-06-11 15:47:29 -04001184 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(context)){
PauloftheWest38155612014-06-30 10:02:36 -07001185 category.addTile(tile);
1186 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001187
Jason Monk2ebc8a02015-02-13 15:23:19 -05001188 } else if (innerNodeName.equals("external-tiles")) {
1189 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001190 } else {
1191 XmlUtils.skipCurrentTag(parser);
1192 }
1193 }
1194
1195 target.add(category);
1196 } else {
1197 XmlUtils.skipCurrentTag(parser);
1198 }
1199 }
1200
1201 } catch (XmlPullParserException e) {
1202 throw new RuntimeException("Error parsing categories", e);
1203 } catch (IOException e) {
1204 throw new RuntimeException("Error parsing categories", e);
1205 } finally {
1206 if (parser != null) parser.close();
1207 }
1208 }
1209
1210 private void updateTilesList(List<DashboardCategory> target) {
1211 final boolean showDev = mDevelopmentPreferences.getBoolean(
1212 DevelopmentSettings.PREF_SHOW,
1213 android.os.Build.TYPE.equals("eng"));
1214
Xiaohui Chen44879a32015-07-22 13:53:22 -07001215 final UserManager um = UserManager.get(this);
1216 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001217
1218 final int size = target.size();
1219 for (int i = 0; i < size; i++) {
1220
1221 DashboardCategory category = target.get(i);
1222
1223 // Ids are integers, so downcasting is ok
1224 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001225 int n = category.getTilesCount() - 1;
1226 while (n >= 0) {
1227
1228 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001229 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001230 id = (int) tile.id;
1231 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001232 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1233 removeTile = true;
1234 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001235 } else if (id == R.id.wifi_settings) {
1236 // Remove WiFi Settings if WiFi service is not available.
1237 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001238 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001239 }
1240 } else if (id == R.id.bluetooth_settings) {
1241 // Remove Bluetooth Settings if Bluetooth service is not available.
1242 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001243 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001244 }
1245 } else if (id == R.id.data_usage_settings) {
1246 // Remove data usage when kernel module not enabled
Jason Monkb45e27b2015-05-20 13:35:43 -04001247 if (!Utils.isBandwidthControlEnabled()) {
1248 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001249 }
1250 } else if (id == R.id.battery_settings) {
1251 // Remove battery settings when battery is not available. (e.g. TV)
1252
1253 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001254 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001255 }
1256 } else if (id == R.id.home_settings) {
1257 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001258 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001259 }
1260 } else if (id == R.id.user_settings) {
1261 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001262 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001263 || Utils.isMonkeyRunning()) {
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.nfc_payment_settings) {
1267 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001268 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001269 } else {
1270 // Only show if NFC is on and we have the HCE feature
1271 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001272 if (adapter == null || !adapter.isEnabled() ||
1273 !getPackageManager().hasSystemFeature(
1274 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001275 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001276 }
1277 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001278 } else if (id == R.id.print_settings) {
1279 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1280 PackageManager.FEATURE_PRINTING);
1281 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001282 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001283 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001284 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001285 if (!showDev || um.hasUserRestriction(
1286 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001287 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001288 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001289 }
1290
Xiaohui Chen44879a32015-07-22 13:53:22 -07001291 if (UserHandle.MU_ENABLED && !isAdmin
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001292 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1293 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001294 }
1295
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001296 if (removeTile && n < category.getTilesCount()) {
1297 category.removeTile(n);
1298 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001299 n--;
1300 }
1301 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001302 addExternalTiles(target);
1303 }
1304
1305 private void addExternalTiles(List<DashboardCategory> target) {
1306 Map<Pair<String, String>, DashboardTile> addedCache =
1307 new ArrayMap<Pair<String, String>, DashboardTile>();
1308 UserManager userManager = UserManager.get(this);
1309 for (UserHandle user : userManager.getUserProfiles()) {
1310 addExternalTiles(target, user, addedCache);
1311 }
1312 }
1313
1314 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1315 Map<Pair<String, String>, DashboardTile> addedCache) {
1316 PackageManager pm = getPackageManager();
1317 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1318 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1319 PackageManager.GET_META_DATA, user.getIdentifier());
1320 for (ResolveInfo resolved : results) {
1321 if (!resolved.system) {
1322 // Do not allow any app to add to settings, only system ones.
1323 continue;
1324 }
1325 ActivityInfo activityInfo = resolved.activityInfo;
1326 Bundle metaData = activityInfo.metaData;
1327 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1328 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1329 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1330 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1331 continue;
1332 }
1333 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1334 DashboardCategory category = getCategory(target, categoryKey);
1335 if (category == null) {
1336 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1337 + "category key " + categoryKey);
1338 continue;
1339 }
1340 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1341 activityInfo.name);
1342 DashboardTile tile = addedCache.get(key);
1343 if (tile == null) {
1344 tile = new DashboardTile();
1345 tile.intent = new Intent().setClassName(
1346 activityInfo.packageName, activityInfo.name);
1347 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1348
1349 if (category.externalIndex == -1) {
1350 // If no location for external tiles has been specified for this category,
1351 // then just put them at the end.
1352 category.addTile(tile);
1353 } else {
1354 category.addTile(category.externalIndex, tile);
1355 }
1356 addedCache.put(key, tile);
1357 }
1358 tile.userHandle.add(user);
1359 }
1360 }
1361
1362 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1363 for (DashboardCategory category : target) {
1364 if (categoryKey.equals(category.key)) {
1365 return category;
1366 }
1367 }
1368 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001369 }
1370
1371 private boolean updateHomeSettingTiles(DashboardTile tile) {
1372 // Once we decide to show Home settings, keep showing it forever
1373 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1374 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1375 return true;
1376 }
1377
1378 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001379 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001380 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001381 // When there's only one available home app, omit this settings
1382 // category entirely at the top level UI. If the user just
1383 // uninstalled the penultimate home app candidiate, we also
1384 // now tell them about why they aren't seeing 'Home' in the list.
1385 if (sShowNoHomeNotice) {
1386 sShowNoHomeNotice = false;
1387 NoHomeDialogFragment.show(this);
1388 }
1389 return false;
1390 } else {
1391 // Okay, we're allowing the Home settings category. Tell it, when
1392 // invoked via this front door, that we'll need to be told about the
1393 // case when the user uninstalls all but one home app.
1394 if (tile.fragmentArguments == null) {
1395 tile.fragmentArguments = new Bundle();
1396 }
1397 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1398 }
1399 } catch (Exception e) {
1400 // Can't look up the home activity; bail on configuring the icon
1401 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1402 }
1403
1404 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1405 return true;
1406 }
1407
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001408 private void getMetaData() {
1409 try {
1410 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1411 PackageManager.GET_META_DATA);
1412 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001413 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1414 } catch (NameNotFoundException nnfe) {
1415 // No recovery
1416 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1417 }
1418 }
1419
1420 // give subclasses access to the Next button
1421 public boolean hasNextButton() {
1422 return mNextButton != null;
1423 }
1424
1425 public Button getNextButton() {
1426 return mNextButton;
1427 }
1428
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001429 @Override
1430 public boolean shouldUpRecreateTask(Intent targetIntent) {
1431 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1432 }
1433
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001434 public static void requestHomeNotice() {
1435 sShowNoHomeNotice = true;
1436 }
1437
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001438 @Override
1439 public boolean onQueryTextSubmit(String query) {
1440 switchToSearchResultsFragmentIfNeeded();
1441 mSearchQuery = query;
1442 return mSearchResultsFragment.onQueryTextSubmit(query);
1443 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001444
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001445 @Override
1446 public boolean onQueryTextChange(String newText) {
1447 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001448 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001449 return false;
1450 }
1451 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001452 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001453
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001454 @Override
1455 public boolean onClose() {
1456 return false;
1457 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001458
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001459 @Override
1460 public boolean onMenuItemActionExpand(MenuItem item) {
1461 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001462 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001463 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001464 return true;
1465 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001466
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001467 @Override
1468 public boolean onMenuItemActionCollapse(MenuItem item) {
1469 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001470 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001472 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001474 return true;
1475 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001477 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001478 if (mSearchResultsFragment != null) {
1479 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001481 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001482 if (current != null && current instanceof SearchResultsSummary) {
1483 mSearchResultsFragment = (SearchResultsSummary) current;
1484 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001485 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001486 SearchResultsSummary.class.getName(), null, false, true,
1487 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001488 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001489 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001491 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001492
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001493 public void needToRevertToInitialFragment() {
1494 mNeedToRevertToInitialFragment = true;
1495 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001496
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001497 private void revertToInitialFragment() {
1498 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001499 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001500 mSearchMenuItemExpanded = false;
1501 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1502 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001503 if (mSearchMenuItem != null) {
1504 mSearchMenuItem.collapseActionView();
1505 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001506 }
Jim Miller0698a212014-10-16 19:49:07 -07001507
1508 public Intent getResultIntentData() {
1509 return mResultIntentData;
1510 }
1511
1512 public void setResultIntentData(Intent resultIntentData) {
1513 mResultIntentData = resultIntentData;
1514 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001515}