blob: 226ea0e5f7a62c12194bd8f7dfa36f1248800feb [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 Spurlockf57bad72015-04-30 09:26:15 -0400105import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400106import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400107import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400108import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400109import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.print.PrintJobSettingsFragment;
111import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500112import com.android.settings.search.DynamicIndexableContentMonitor;
113import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700114import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import com.android.settings.tts.TextToSpeechSettings;
116import com.android.settings.users.UserSettings;
117import com.android.settings.vpn2.VpnSettings;
118import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700119import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700121import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import com.android.settings.wifi.WifiSettings;
123import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500124
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125import org.xmlpull.v1.XmlPullParser;
126import org.xmlpull.v1.XmlPullParserException;
127
128import java.io.IOException;
129import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800130import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500131import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700132import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133
134public class SettingsActivity extends Activity
135 implements PreferenceManager.OnPreferenceTreeClickListener,
136 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100137 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100139 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800140
141 private static final String LOG_TAG = "Settings";
142
143 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700144 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700145 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
146 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700147 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700148 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700149 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800150
151 /**
152 * When starting this activity, the invoking Intent can contain this extra
153 * string to specify which fragment should be initially displayed.
154 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
155 * will call isValidFragment() to confirm that the fragment class name is valid for this
156 * activity.
157 */
158 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
159
160 /**
161 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
162 * this extra can also be specified to supply a Bundle of arguments to pass
163 * to that fragment when it is instantiated during the initial creation
164 * of the activity.
165 */
166 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
167
168 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700169 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
170 */
171 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
172
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800173 public static final String BACK_STACK_PREFS = ":settings:prefs";
174
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 // extras that allow any preference activity to be launched as part of a wizard
176
177 // show Back and Next buttons? takes boolean parameter
178 // Back will then return RESULT_CANCELED and Next RESULT_OK
179 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
180
181 // add a Skip button?
182 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
183
184 // specify custom text for the Back or Next buttons, or cause a button to not appear
185 // at all by setting it to null
186 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
187 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
188
189 /**
190 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700191 * 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 -0800192 * that fragment.
193 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700194 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100195 /**
196 * The package name used to resolve the title resource id.
197 */
198 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
199 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700200 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
201 ":settings:show_fragment_title_resid";
202 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
203 ":settings:show_fragment_as_shortcut";
204
205 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
206 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208 private static final String META_DATA_KEY_FRAGMENT_CLASS =
209 "com.android.settings.FRAGMENT_CLASS";
210
211 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
212
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700213 private static final String EMPTY_QUERY = "";
214
Jason Monk2ebc8a02015-02-13 15:23:19 -0500215 /**
216 * Settings will search for system activities of this action and add them as a top level
217 * settings tile using the following parameters.
218 *
219 * <p>A category must be specified in the meta-data for the activity named
220 * {@link #EXTRA_CATEGORY_KEY}
221 *
222 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
223 * otherwise the label for the activity will be used.
224 *
225 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
226 * otherwise the icon for the activity will be used.
227 *
228 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
229 */
230 private static final String EXTRA_SETTINGS_ACTION =
231 "com.android.settings.action.EXTRA_SETTINGS";
232
233 /**
234 * The key used to get the category from metadata of activities of action
235 * {@link #EXTRA_SETTINGS_ACTION}
236 * The value must be one of:
237 * <li>com.android.settings.category.wireless</li>
238 * <li>com.android.settings.category.device</li>
239 * <li>com.android.settings.category.personal</li>
240 * <li>com.android.settings.category.system</li>
241 */
242 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
243
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244 private static boolean sShowNoHomeNotice = false;
245
246 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800248 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700249 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800250
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 // Show only these settings for restricted users
252 private int[] SETTINGS_FOR_RESTRICTED = {
253 R.id.wireless_section,
254 R.id.wifi_settings,
255 R.id.bluetooth_settings,
256 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700257 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 R.id.wireless_settings,
259 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400260 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 R.id.display_settings,
262 R.id.storage_settings,
263 R.id.application_settings,
264 R.id.battery_settings,
265 R.id.personal_section,
266 R.id.location_settings,
267 R.id.security_settings,
268 R.id.language_settings,
269 R.id.user_settings,
270 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 R.id.system_section,
272 R.id.date_time_settings,
273 R.id.about_settings,
274 R.id.accessibility_settings,
275 R.id.print_settings,
276 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800277 R.id.home_settings,
278 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279 };
280
281 private static final String[] ENTRY_FRAGMENTS = {
282 WirelessSettings.class.getName(),
283 WifiSettings.class.getName(),
284 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700285 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700287 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 TetherSettings.class.getName(),
289 WifiP2pSettings.class.getName(),
290 VpnSettings.class.getName(),
291 DateTimeSettings.class.getName(),
292 LocalePicker.class.getName(),
293 InputMethodAndLanguageSettings.class.getName(),
294 SpellCheckersSettings.class.getName(),
295 UserDictionaryList.class.getName(),
296 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700297 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 DisplaySettings.class.getName(),
299 DeviceInfoSettings.class.getName(),
300 ManageApplications.class.getName(),
Xiyuan Xia86a55402015-06-02 14:55:32 -0700301 ManageAssist.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 ProcessStatsUi.class.getName(),
303 NotificationStation.class.getName(),
304 LocationSettings.class.getName(),
305 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400306 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 PrivacySettings.class.getName(),
308 DeviceAdminSettings.class.getName(),
309 AccessibilitySettings.class.getName(),
310 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
312 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700313 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700314 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700315 PrivateVolumeSettings.class.getName(),
316 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800317 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 AndroidBeam.class.getName(),
319 WifiDisplaySettings.class.getName(),
320 PowerUsageSummary.class.getName(),
321 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000322 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 CryptKeeperSettings.class.getName(),
324 DataUsageSummary.class.getName(),
325 DreamSettings.class.getName(),
326 UserSettings.class.getName(),
327 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400328 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800329 PrintSettingsFragment.class.getName(),
330 PrintJobSettingsFragment.class.getName(),
331 TrustedCredentialsSettings.class.getName(),
332 PaymentSettings.class.getName(),
333 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700334 ZenModeSettings.class.getName(),
335 NotificationSettings.class.getName(),
336 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
337 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400338 InstalledAppDetails.class.getName(),
339 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700340 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700341 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000342 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400343 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400344 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400345 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400346 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400347 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400348 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400349 PowerUsageDetail.class.getName(),
Jason Monk2cdafc62015-06-12 12:32:50 -0400350 ProcessStatsSummary.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351 };
352
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700353
354 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
355 "android.settings.APPLICATION_DETAILS_SETTINGS"
356 };
357
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 private SharedPreferences mDevelopmentPreferences;
359 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
360
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 private boolean mBatteryPresent = true;
362 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 @Override
364 public void onReceive(Context context, Intent intent) {
365 String action = intent.getAction();
366 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
367 boolean batteryPresent = Utils.isBatteryPresent(intent);
368
369 if (mBatteryPresent != batteryPresent) {
370 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700371 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800372 }
373 }
374 }
375 };
376
Svetoslav990159a2014-04-14 17:14:59 -0700377 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
378 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700379
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700380 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700381 private SwitchBar mSwitchBar;
382
383 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700384
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700385 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700386 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700387
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700388 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700389 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700390
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700391 private ViewGroup mContent;
392
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700393 private SearchView mSearchView;
394 private MenuItem mSearchMenuItem;
395 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700396 private SearchResultsSummary mSearchResultsFragment;
397 private String mSearchQuery;
398
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700399 // Categories
400 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700402 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700403 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404 private Handler mHandler = new Handler() {
405 @Override
406 public void handleMessage(Message msg) {
407 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700408 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700409 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
410 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700411 buildDashboardCategories(mCategories);
412 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800413 } break;
414 }
415 }
416 };
417
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700418 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700419 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700420
Jim Miller0698a212014-10-16 19:49:07 -0700421 private Intent mResultIntentData;
422
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700423 public SwitchBar getSwitchBar() {
424 return mSwitchBar;
425 }
426
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700427 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
428 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700429 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700430 }
431 return mCategories;
432 }
433
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800434 @Override
435 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
436 // Override the fragment title for Wallpaper settings
437 int titleRes = pref.getTitleRes();
438 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
439 titleRes = R.string.wallpaper_settings_fragment_title;
440 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
441 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
442 if (UserManager.get(this).isLinkedUser()) {
443 titleRes = R.string.profile_info_settings_title;
444 } else {
445 titleRes = R.string.user_info_settings_title;
446 }
447 }
448 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
449 null, 0);
450 return true;
451 }
452
453 @Override
454 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
455 return false;
456 }
457
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700458 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700459 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700460 Message msg = new Message();
461 msg.what = MSG_BUILD_CATEGORIES;
462 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463 }
464 }
465
466 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800467 public void onConfigurationChanged(Configuration newConfig) {
468 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800469 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800470 }
471
472 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700473 protected void onStart() {
474 super.onStart();
475
476 if (mNeedToRevertToInitialFragment) {
477 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800479 }
480
481 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700482 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700483 if (!mDisplaySearch) {
484 return false;
485 }
486
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700487 MenuInflater inflater = getMenuInflater();
488 inflater.inflate(R.menu.options_menu, menu);
489
490 // Cache the search query (can be overriden by the OnQueryTextListener)
491 final String query = mSearchQuery;
492
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700493 mSearchMenuItem = menu.findItem(R.id.search);
494 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700495
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700496 if (mSearchMenuItem == null || mSearchView == null) {
497 return false;
498 }
499
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700500 if (mSearchResultsFragment != null) {
501 mSearchResultsFragment.setSearchView(mSearchView);
502 }
503
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700504 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700505 mSearchView.setOnQueryTextListener(this);
506 mSearchView.setOnCloseListener(this);
507
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700508 if (mSearchMenuItemExpanded) {
509 mSearchMenuItem.expandActionView();
510 }
511 mSearchView.setQuery(query, true /* submit */);
512
513 return true;
514 }
515
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700516 private static boolean isShortCutIntent(final Intent intent) {
517 Set<String> categories = intent.getCategories();
518 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
519 }
520
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700521 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700522 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700523 if (action == null) {
524 return false;
525 }
526 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
527 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
528 }
529 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700530 }
531
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700532 @Override
533 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700534 super.onCreate(savedState);
535
536 // Should happen before any call to getIntent()
537 getMetaData();
538
539 final Intent intent = getIntent();
540 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
541 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800544 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
545 Context.MODE_PRIVATE);
546
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700547 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700548 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700549
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700550 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700551 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
552
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700553 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700554 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700555
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700556 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700557
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700558 // This is a "Sub Settings" when:
559 // - this is a real SubSettings
560 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700561 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700562 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
563
564 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
565 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700566 // Check also that we are not a Theme Dialog as we don't want to override them
567 final int themeResId = getThemeResId();
568 if (themeResId != R.style.Theme_DialogWhenLarge &&
569 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
570 setTheme(R.style.Theme_SubSettings);
571 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700572 }
573
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700574 setContentView(mIsShowingDashboard ?
575 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700577 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700578
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579 getFragmentManager().addOnBackStackChangedListener(this);
580
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700581 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800582 // Run the Index update only if we have some space
583 if (!Utils.isLowStorage(this)) {
584 Index.getInstance(getApplicationContext()).update();
585 } else {
586 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
587 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700588 }
589
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700590 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700591 // We are restarting from a previous saved state; used that to initialize, instead
592 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700593 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
594 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700596 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800597
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700598 ArrayList<DashboardCategory> categories =
599 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
600 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700601 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700602 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700603 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700605
606 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700607 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700608 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
609 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800610 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700611 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400612 mDisplaySearch = false;
613 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700614 if (mIsShortcut) {
615 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700616 } else if (isSubSettings) {
617 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700618 } else {
619 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700620 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700621 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700622
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700623 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700624 switchToFragment(initialFragmentName, initialArguments, true, false,
625 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000626 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700627 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700628 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700629 // Show Search affordance
630 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700631 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700632 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700633 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800634 }
635 }
636
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700637 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700638 if (mActionBar != null) {
639 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
640 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
641 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700642 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
643
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800644 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
646
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700647 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 if (buttonBar != null) {
649 buttonBar.setVisibility(View.VISIBLE);
650
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700651 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 backButton.setOnClickListener(new OnClickListener() {
653 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700654 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 finish();
656 }
657 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700658 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 skipButton.setOnClickListener(new OnClickListener() {
660 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700661 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800662 finish();
663 }
664 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700665 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 mNextButton.setOnClickListener(new OnClickListener() {
667 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700668 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 finish();
670 }
671 });
672
673 // set our various button parameters
674 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
675 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
676 if (TextUtils.isEmpty(buttonText)) {
677 mNextButton.setVisibility(View.GONE);
678 }
679 else {
680 mNextButton.setText(buttonText);
681 }
682 }
683 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
684 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
685 if (TextUtils.isEmpty(buttonText)) {
686 backButton.setVisibility(View.GONE);
687 }
688 else {
689 backButton.setText(buttonText);
690 }
691 }
692 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
693 skipButton.setVisibility(View.VISIBLE);
694 }
695 }
696 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700697
698 mHomeActivitiesCount = getHomeActivitiesCount();
699 }
700
701 private int getHomeActivitiesCount() {
702 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
703 getPackageManager().getHomeActivities(homeApps);
704 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800705 }
706
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700707 private void setTitleFromIntent(Intent intent) {
708 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
709 if (initialTitleResId > 0) {
710 mInitialTitle = null;
711 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100712
713 final String initialTitleResPackageName = intent.getStringExtra(
714 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
715 if (initialTitleResPackageName != null) {
716 try {
717 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
718 0 /* flags */, new UserHandle(UserHandle.myUserId()));
719 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
720 setTitle(mInitialTitle);
721 mInitialTitleResId = -1;
722 return;
723 } catch (NameNotFoundException e) {
724 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
725 }
726 } else {
727 setTitle(mInitialTitleResId);
728 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700729 } else {
730 mInitialTitleResId = -1;
731 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
732 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
733 setTitle(mInitialTitle);
734 }
735 }
736
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800737 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800738 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700739 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800740 }
741
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700742 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800743 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700744
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800745 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700746 if (mInitialTitleResId > 0) {
747 setTitle(mInitialTitleResId);
748 } else {
749 setTitle(mInitialTitle);
750 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700751 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800752 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700753
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800754 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
755 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700756
757 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800758 }
759
760 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
761 final CharSequence title;
762 final int titleRes = bse.getBreadCrumbTitleRes();
763 if (titleRes > 0) {
764 title = getText(titleRes);
765 } else {
766 title = bse.getBreadCrumbTitle();
767 }
768 if (title != null) {
769 setTitle(title);
770 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800771 }
772
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800773 @Override
774 protected void onSaveInstanceState(Bundle outState) {
775 super.onSaveInstanceState(outState);
776
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700777 if (mCategories.size() > 0) {
778 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800779 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700780
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700781 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700782 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700783
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700784 if (mDisplaySearch) {
785 // The option menus are created if the ActionBar is visible and they are also created
786 // asynchronously. If you launch Settings with an Intent action like
787 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
788 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
789 // menu item and search view are null.
790 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
791 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700792
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700793 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
794 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
795 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700796
797 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800798 }
799
800 @Override
801 public void onResume() {
802 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400803 if (mIsShowingDashboard) {
804 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
805 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800806
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700807 final int newHomeActivityCount = getHomeActivitiesCount();
808 if (newHomeActivityCount != mHomeActivitiesCount) {
809 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700810 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700811 }
812
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800813 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
814 @Override
815 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700816 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800817 }
818 };
819 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
820 mDevelopmentPreferencesListener);
821
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800822 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700823
Svetoslav990159a2014-04-14 17:14:59 -0700824 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700825
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700826 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700827 onQueryTextSubmit(mSearchQuery);
828 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800829 }
830
831 @Override
832 public void onPause() {
833 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400834 if (mIsShowingDashboard) {
835 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
836 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800837 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700838 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800839 }
840
841 @Override
842 public void onDestroy() {
843 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700844
845 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
846 mDevelopmentPreferencesListener);
847 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800848 }
849
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800850 protected boolean isValidFragment(String fragmentName) {
851 // Almost all fragments are wrapped in this,
852 // except for a few that have their own activities.
853 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
854 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
855 }
856 return false;
857 }
858
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800859 @Override
860 public Intent getIntent() {
861 Intent superIntent = super.getIntent();
862 String startingFragment = getStartingFragmentClass(superIntent);
863 // This is called from super.onCreate, isMultiPane() is not yet reliable
864 // Do not use onIsHidingHeaders either, which relies itself on this method
865 if (startingFragment != null) {
866 Intent modIntent = new Intent(superIntent);
867 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
868 Bundle args = superIntent.getExtras();
869 if (args != null) {
870 args = new Bundle(args);
871 } else {
872 args = new Bundle();
873 }
874 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100875 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800876 return modIntent;
877 }
878 return superIntent;
879 }
880
881 /**
882 * Checks if the component name in the intent is different from the Settings class and
883 * returns the class name to load as a fragment.
884 */
885 private String getStartingFragmentClass(Intent intent) {
886 if (mFragmentClass != null) return mFragmentClass;
887
888 String intentClass = intent.getComponent().getClassName();
889 if (intentClass.equals(getClass().getName())) return null;
890
891 if ("com.android.settings.ManageApplications".equals(intentClass)
892 || "com.android.settings.RunningServices".equals(intentClass)
893 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
894 // Old names of manage apps.
895 intentClass = com.android.settings.applications.ManageApplications.class.getName();
896 }
897
898 return intentClass;
899 }
900
901 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000902 * Start a new fragment containing a preference panel. If the preferences
903 * are being displayed in multi-pane mode, the given fragment class will
904 * be instantiated and placed in the appropriate pane. If running in
905 * single-pane mode, a new activity will be launched in which to show the
906 * fragment.
907 *
908 * @param fragmentClass Full name of the class implementing the fragment.
909 * @param args Any desired arguments to supply to the fragment.
910 * @param titleRes Optional resource identifier of the title of this
911 * fragment.
912 * @param titleText Optional text of the title of this fragment.
913 * @param resultTo Optional fragment that result data should be sent to.
914 * If non-null, resultTo.onActivityResult() will be called when this
915 * preference panel is done. The launched panel must use
916 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
917 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700918 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000919 */
920 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700921 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700922 String title = null;
923 if (titleRes < 0) {
924 if (titleText != null) {
925 title = titleText.toString();
926 } else {
927 // There not much we can do in that case
928 title = "";
929 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700930 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700931 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700932 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000933 }
934
935 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100936 * Start a new fragment in a new activity containing a preference panel for a given user. If the
937 * preferences are being displayed in multi-pane mode, the given fragment class will be
938 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
939 * activity will be launched in which to show the fragment.
940 *
941 * @param fragmentClass Full name of the class implementing the fragment.
942 * @param args Any desired arguments to supply to the fragment.
943 * @param titleRes Optional resource identifier of the title of this fragment.
944 * @param titleText Optional text of the title of this fragment.
945 * @param userHandle The user for which the panel has to be started.
946 */
947 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
948 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700949 // This is a workaround.
950 //
951 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
952 // starting the fragment could cause a native stack corruption. See b/17523189. However,
953 // adding that flag and start the preference panel with the same UserHandler will make it
954 // impossible to use back button to return to the previous screen. See b/20042570.
955 //
956 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
957 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
958 // when we're calling it as the same user.
959 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
960 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
961 } else {
962 String title = null;
963 if (titleRes < 0) {
964 if (titleText != null) {
965 title = titleText.toString();
966 } else {
967 // There not much we can do in that case
968 title = "";
969 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100970 }
Lifu Tangd0332852015-04-02 12:05:46 -0700971 Utils.startWithFragmentAsUser(this, fragmentClass, args,
972 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100973 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100974 }
975
976 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800977 * Called by a preference panel fragment to finish itself.
978 *
979 * @param caller The fragment that is asking to be finished.
980 * @param resultCode Optional result code to send back to the original
981 * launching fragment.
982 * @param resultData Optional result data to send back to the original
983 * launching fragment.
984 */
985 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
986 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700987 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800988 }
989
990 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000991 * Start a new fragment.
992 *
993 * @param fragment The fragment to start
994 * @param push If true, the current fragment will be pushed onto the back stack. If false,
995 * the current fragment will be replaced.
996 */
997 public void startPreferenceFragment(Fragment fragment, boolean push) {
998 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700999 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001000 if (push) {
1001 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1002 transaction.addToBackStack(BACK_STACK_PREFS);
1003 } else {
1004 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1005 }
1006 transaction.commitAllowingStateLoss();
1007 }
1008
1009 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001010 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1011 */
1012 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001013 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001014 if (validate && !isValidFragment(fragmentName)) {
1015 throw new IllegalArgumentException("Invalid fragment for this activity: "
1016 + fragmentName);
1017 }
1018 Fragment f = Fragment.instantiate(this, fragmentName, args);
1019 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001020 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001021 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001022 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001023 }
1024 if (addToBackStack) {
1025 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1026 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001027 if (titleResId > 0) {
1028 transaction.setBreadCrumbTitle(titleResId);
1029 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001030 transaction.setBreadCrumbTitle(title);
1031 }
1032 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001033 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001034 return f;
1035 }
1036
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001037 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001038 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001039 *
1040 * @param categories The list in which to place the tiles categories.
1041 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001042 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001043 categories.clear();
Jason Monk2acff552015-06-11 15:47:29 -04001044 loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001045 updateTilesList(categories);
1046 }
1047
1048 /**
1049 * Parse the given XML file as a categories description, adding each
1050 * parsed categories and tiles into the target list.
1051 *
1052 * @param resid The XML resource to load and parse.
1053 * @param target The list in which the parsed categories and tiles should be placed.
1054 */
Jason Monk2acff552015-06-11 15:47:29 -04001055 public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
1056 Context context) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001057 XmlResourceParser parser = null;
1058 try {
Jason Monk2acff552015-06-11 15:47:29 -04001059 parser = context.getResources().getXml(resid);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001060 AttributeSet attrs = Xml.asAttributeSet(parser);
1061
1062 int type;
1063 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1064 && type != XmlPullParser.START_TAG) {
1065 // Parse next until start tag is found
1066 }
1067
1068 String nodeName = parser.getName();
1069 if (!"dashboard-categories".equals(nodeName)) {
1070 throw new RuntimeException(
1071 "XML document must start with <preference-categories> tag; found"
1072 + nodeName + " at " + parser.getPositionDescription());
1073 }
1074
1075 Bundle curBundle = null;
1076
1077 final int outerDepth = parser.getDepth();
1078 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1079 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1080 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1081 continue;
1082 }
1083
1084 nodeName = parser.getName();
1085 if ("dashboard-category".equals(nodeName)) {
1086 DashboardCategory category = new DashboardCategory();
1087
Jason Monk2acff552015-06-11 15:47:29 -04001088 TypedArray sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001089 attrs, com.android.internal.R.styleable.PreferenceHeader);
1090 category.id = sa.getResourceId(
1091 com.android.internal.R.styleable.PreferenceHeader_id,
1092 (int)DashboardCategory.CAT_ID_UNDEFINED);
1093
1094 TypedValue tv = sa.peekValue(
1095 com.android.internal.R.styleable.PreferenceHeader_title);
1096 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1097 if (tv.resourceId != 0) {
1098 category.titleRes = tv.resourceId;
1099 } else {
1100 category.title = tv.string;
1101 }
1102 }
1103 sa.recycle();
Jason Monk2acff552015-06-11 15:47:29 -04001104 sa = context.obtainStyledAttributes(attrs,
1105 com.android.internal.R.styleable.Preference);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001106 tv = sa.peekValue(
1107 com.android.internal.R.styleable.Preference_key);
1108 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1109 if (tv.resourceId != 0) {
Jason Monk2acff552015-06-11 15:47:29 -04001110 category.key = context.getString(tv.resourceId);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001111 } else {
1112 category.key = tv.string.toString();
1113 }
1114 }
1115 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001116
1117 final int innerDepth = parser.getDepth();
1118 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1119 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1120 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1121 continue;
1122 }
1123
1124 String innerNodeName = parser.getName();
1125 if (innerNodeName.equals("dashboard-tile")) {
1126 DashboardTile tile = new DashboardTile();
1127
Jason Monk2acff552015-06-11 15:47:29 -04001128 sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001129 attrs, com.android.internal.R.styleable.PreferenceHeader);
1130 tile.id = sa.getResourceId(
1131 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001132 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001133 tv = sa.peekValue(
1134 com.android.internal.R.styleable.PreferenceHeader_title);
1135 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1136 if (tv.resourceId != 0) {
1137 tile.titleRes = tv.resourceId;
1138 } else {
1139 tile.title = tv.string;
1140 }
1141 }
1142 tv = sa.peekValue(
1143 com.android.internal.R.styleable.PreferenceHeader_summary);
1144 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1145 if (tv.resourceId != 0) {
1146 tile.summaryRes = tv.resourceId;
1147 } else {
1148 tile.summary = tv.string;
1149 }
1150 }
1151 tile.iconRes = sa.getResourceId(
1152 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1153 tile.fragment = sa.getString(
1154 com.android.internal.R.styleable.PreferenceHeader_fragment);
1155 sa.recycle();
1156
1157 if (curBundle == null) {
1158 curBundle = new Bundle();
1159 }
1160
1161 final int innerDepth2 = parser.getDepth();
1162 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1163 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1164 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1165 continue;
1166 }
1167
1168 String innerNodeName2 = parser.getName();
1169 if (innerNodeName2.equals("extra")) {
Jason Monk2acff552015-06-11 15:47:29 -04001170 context.getResources().parseBundleExtra("extra", attrs,
1171 curBundle);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001172 XmlUtils.skipCurrentTag(parser);
1173
1174 } else if (innerNodeName2.equals("intent")) {
Jason Monk2acff552015-06-11 15:47:29 -04001175 tile.intent = Intent.parseIntent(context.getResources(), parser,
1176 attrs);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001177
1178 } else {
1179 XmlUtils.skipCurrentTag(parser);
1180 }
1181 }
1182
1183 if (curBundle.size() > 0) {
1184 tile.fragmentArguments = curBundle;
1185 curBundle = null;
1186 }
1187
PauloftheWest38155612014-06-30 10:02:36 -07001188 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Jason Monk2acff552015-06-11 15:47:29 -04001189 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(context)){
PauloftheWest38155612014-06-30 10:02:36 -07001190 category.addTile(tile);
1191 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001192
Jason Monk2ebc8a02015-02-13 15:23:19 -05001193 } else if (innerNodeName.equals("external-tiles")) {
1194 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001195 } else {
1196 XmlUtils.skipCurrentTag(parser);
1197 }
1198 }
1199
1200 target.add(category);
1201 } else {
1202 XmlUtils.skipCurrentTag(parser);
1203 }
1204 }
1205
1206 } catch (XmlPullParserException e) {
1207 throw new RuntimeException("Error parsing categories", e);
1208 } catch (IOException e) {
1209 throw new RuntimeException("Error parsing categories", e);
1210 } finally {
1211 if (parser != null) parser.close();
1212 }
1213 }
1214
1215 private void updateTilesList(List<DashboardCategory> target) {
1216 final boolean showDev = mDevelopmentPreferences.getBoolean(
1217 DevelopmentSettings.PREF_SHOW,
1218 android.os.Build.TYPE.equals("eng"));
1219
1220 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1221
1222 final int size = target.size();
1223 for (int i = 0; i < size; i++) {
1224
1225 DashboardCategory category = target.get(i);
1226
1227 // Ids are integers, so downcasting is ok
1228 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001229 int n = category.getTilesCount() - 1;
1230 while (n >= 0) {
1231
1232 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001233 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001234 id = (int) tile.id;
1235 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001236 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1237 removeTile = true;
1238 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001239 } else if (id == R.id.wifi_settings) {
1240 // Remove WiFi Settings if WiFi service is not available.
1241 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001242 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001243 }
1244 } else if (id == R.id.bluetooth_settings) {
1245 // Remove Bluetooth Settings if Bluetooth service is not available.
1246 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001247 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001248 }
1249 } else if (id == R.id.data_usage_settings) {
1250 // Remove data usage when kernel module not enabled
Jason Monkb45e27b2015-05-20 13:35:43 -04001251 if (!Utils.isBandwidthControlEnabled()) {
1252 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001253 }
1254 } else if (id == R.id.battery_settings) {
1255 // Remove battery settings when battery is not available. (e.g. TV)
1256
1257 if (!mBatteryPresent) {
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.home_settings) {
1261 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001262 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001263 }
1264 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001265 boolean hasMultipleUsers =
1266 ((UserManager) getSystemService(Context.USER_SERVICE))
1267 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001268 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001269 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001270 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001271 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001272 }
1273 } else if (id == R.id.nfc_payment_settings) {
1274 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001275 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001276 } else {
1277 // Only show if NFC is on and we have the HCE feature
1278 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001279 if (adapter == null || !adapter.isEnabled() ||
1280 !getPackageManager().hasSystemFeature(
1281 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001282 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001283 }
1284 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001285 } else if (id == R.id.print_settings) {
1286 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1287 PackageManager.FEATURE_PRINTING);
1288 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001289 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001290 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001291 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001292 if (!showDev || um.hasUserRestriction(
1293 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001294 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001295 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001296 }
1297
1298 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001299 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1300 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001301 }
1302
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001303 if (removeTile && n < category.getTilesCount()) {
1304 category.removeTile(n);
1305 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001306 n--;
1307 }
1308 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001309 addExternalTiles(target);
1310 }
1311
1312 private void addExternalTiles(List<DashboardCategory> target) {
1313 Map<Pair<String, String>, DashboardTile> addedCache =
1314 new ArrayMap<Pair<String, String>, DashboardTile>();
1315 UserManager userManager = UserManager.get(this);
1316 for (UserHandle user : userManager.getUserProfiles()) {
1317 addExternalTiles(target, user, addedCache);
1318 }
1319 }
1320
1321 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1322 Map<Pair<String, String>, DashboardTile> addedCache) {
1323 PackageManager pm = getPackageManager();
1324 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1325 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1326 PackageManager.GET_META_DATA, user.getIdentifier());
1327 for (ResolveInfo resolved : results) {
1328 if (!resolved.system) {
1329 // Do not allow any app to add to settings, only system ones.
1330 continue;
1331 }
1332 ActivityInfo activityInfo = resolved.activityInfo;
1333 Bundle metaData = activityInfo.metaData;
1334 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1335 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1336 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1337 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1338 continue;
1339 }
1340 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1341 DashboardCategory category = getCategory(target, categoryKey);
1342 if (category == null) {
1343 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1344 + "category key " + categoryKey);
1345 continue;
1346 }
1347 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1348 activityInfo.name);
1349 DashboardTile tile = addedCache.get(key);
1350 if (tile == null) {
1351 tile = new DashboardTile();
1352 tile.intent = new Intent().setClassName(
1353 activityInfo.packageName, activityInfo.name);
1354 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1355
1356 if (category.externalIndex == -1) {
1357 // If no location for external tiles has been specified for this category,
1358 // then just put them at the end.
1359 category.addTile(tile);
1360 } else {
1361 category.addTile(category.externalIndex, tile);
1362 }
1363 addedCache.put(key, tile);
1364 }
1365 tile.userHandle.add(user);
1366 }
1367 }
1368
1369 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1370 for (DashboardCategory category : target) {
1371 if (categoryKey.equals(category.key)) {
1372 return category;
1373 }
1374 }
1375 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001376 }
1377
1378 private boolean updateHomeSettingTiles(DashboardTile tile) {
1379 // Once we decide to show Home settings, keep showing it forever
1380 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1381 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1382 return true;
1383 }
1384
1385 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001386 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001387 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001388 // When there's only one available home app, omit this settings
1389 // category entirely at the top level UI. If the user just
1390 // uninstalled the penultimate home app candidiate, we also
1391 // now tell them about why they aren't seeing 'Home' in the list.
1392 if (sShowNoHomeNotice) {
1393 sShowNoHomeNotice = false;
1394 NoHomeDialogFragment.show(this);
1395 }
1396 return false;
1397 } else {
1398 // Okay, we're allowing the Home settings category. Tell it, when
1399 // invoked via this front door, that we'll need to be told about the
1400 // case when the user uninstalls all but one home app.
1401 if (tile.fragmentArguments == null) {
1402 tile.fragmentArguments = new Bundle();
1403 }
1404 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1405 }
1406 } catch (Exception e) {
1407 // Can't look up the home activity; bail on configuring the icon
1408 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1409 }
1410
1411 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1412 return true;
1413 }
1414
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001415 private void getMetaData() {
1416 try {
1417 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1418 PackageManager.GET_META_DATA);
1419 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001420 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1421 } catch (NameNotFoundException nnfe) {
1422 // No recovery
1423 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1424 }
1425 }
1426
1427 // give subclasses access to the Next button
1428 public boolean hasNextButton() {
1429 return mNextButton != null;
1430 }
1431
1432 public Button getNextButton() {
1433 return mNextButton;
1434 }
1435
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001436 @Override
1437 public boolean shouldUpRecreateTask(Intent targetIntent) {
1438 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1439 }
1440
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001441 public static void requestHomeNotice() {
1442 sShowNoHomeNotice = true;
1443 }
1444
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001445 @Override
1446 public boolean onQueryTextSubmit(String query) {
1447 switchToSearchResultsFragmentIfNeeded();
1448 mSearchQuery = query;
1449 return mSearchResultsFragment.onQueryTextSubmit(query);
1450 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001451
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001452 @Override
1453 public boolean onQueryTextChange(String newText) {
1454 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001455 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001456 return false;
1457 }
1458 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001459 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001460
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001461 @Override
1462 public boolean onClose() {
1463 return false;
1464 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001465
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001466 @Override
1467 public boolean onMenuItemActionExpand(MenuItem item) {
1468 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001469 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001470 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 return true;
1472 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001474 @Override
1475 public boolean onMenuItemActionCollapse(MenuItem item) {
1476 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001477 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001478 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001479 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001481 return true;
1482 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001483
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001484 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001485 if (mSearchResultsFragment != null) {
1486 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001487 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001488 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001489 if (current != null && current instanceof SearchResultsSummary) {
1490 mSearchResultsFragment = (SearchResultsSummary) current;
1491 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001493 SearchResultsSummary.class.getName(), null, false, true,
1494 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001495 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001496 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001497 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001498 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001499
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001500 public void needToRevertToInitialFragment() {
1501 mNeedToRevertToInitialFragment = true;
1502 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001503
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001504 private void revertToInitialFragment() {
1505 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001506 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001507 mSearchMenuItemExpanded = false;
1508 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1509 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001510 if (mSearchMenuItem != null) {
1511 mSearchMenuItem.collapseActionView();
1512 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001513 }
Jim Miller0698a212014-10-16 19:49:07 -07001514
1515 public Intent getResultIntentData() {
1516 return mResultIntentData;
1517 }
1518
1519 public void setResultIntentData(Intent resultIntentData) {
1520 mResultIntentData = resultIntentData;
1521 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001522}