blob: eb002dd427a2d7c1b1ffdf09c219dd83bf46ed50 [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;
42import android.os.INetworkManagementService;
43import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.preference.Preference;
49import android.preference.PreferenceFragment;
50import android.preference.PreferenceManager;
51import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.AttributeSet;
56import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050057import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.util.TypedValue;
59import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070060import android.view.Menu;
61import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.view.MenuItem;
63import android.view.View;
64import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070065import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080066import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070067import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040068
Chris Wren8a963ba2015-03-20 10:29:14 -040069import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.internal.util.ArrayUtils;
71import com.android.internal.util.XmlUtils;
72import com.android.settings.accessibility.AccessibilitySettings;
73import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000074import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070076import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.applications.ManageApplications;
78import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040079import com.android.settings.applications.UsageAccessDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080082import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070083import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070084import com.android.settings.dashboard.NoHomeDialogFragment;
85import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070086import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070087import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070088import com.android.settings.deviceinfo.PublicVolumeSettings;
89import com.android.settings.deviceinfo.StorageSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040091import com.android.settings.fuelgauge.BatterySaverSettings;
Jason Monk1eb54eb2015-04-29 12:46:42 -040092import com.android.settings.fuelgauge.PowerUsageDetail;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080093import com.android.settings.fuelgauge.PowerUsageSummary;
94import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
95import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
96import com.android.settings.inputmethod.SpellCheckersSettings;
97import com.android.settings.inputmethod.UserDictionaryList;
98import com.android.settings.location.LocationSettings;
99import com.android.settings.nfc.AndroidBeam;
100import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -0400101import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400102import com.android.settings.notification.NotificationAccessSettings;
103import com.android.settings.notification.NotificationSettings;
104import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500105import com.android.settings.notification.OtherSoundSettings;
John Spurlock08531a82015-05-07 17:45:43 -0400106import com.android.settings.notification.ZenAccessSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400107import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400108import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400109import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400110import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400111import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.print.PrintJobSettingsFragment;
113import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500114import com.android.settings.search.DynamicIndexableContentMonitor;
115import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700116import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import com.android.settings.tts.TextToSpeechSettings;
118import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700119import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import com.android.settings.vpn2.VpnSettings;
121import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700122import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700124import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125import com.android.settings.wifi.WifiSettings;
126import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500127
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800128import org.xmlpull.v1.XmlPullParser;
129import org.xmlpull.v1.XmlPullParserException;
130
131import java.io.IOException;
132import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500134import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700135import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800136
137public class SettingsActivity extends Activity
138 implements PreferenceManager.OnPreferenceTreeClickListener,
139 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100140 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700141 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100142 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800143
144 private static final String LOG_TAG = "Settings";
145
146 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700147 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700148 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
149 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700150 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700151 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700152 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153
154 /**
155 * When starting this activity, the invoking Intent can contain this extra
156 * string to specify which fragment should be initially displayed.
157 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
158 * will call isValidFragment() to confirm that the fragment class name is valid for this
159 * activity.
160 */
161 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
162
163 /**
164 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
165 * this extra can also be specified to supply a Bundle of arguments to pass
166 * to that fragment when it is instantiated during the initial creation
167 * of the activity.
168 */
169 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
170
171 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700172 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
173 */
174 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
175
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800176 public static final String BACK_STACK_PREFS = ":settings:prefs";
177
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800178 // extras that allow any preference activity to be launched as part of a wizard
179
180 // show Back and Next buttons? takes boolean parameter
181 // Back will then return RESULT_CANCELED and Next RESULT_OK
182 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
183
184 // add a Skip button?
185 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
186
187 // specify custom text for the Back or Next buttons, or cause a button to not appear
188 // at all by setting it to null
189 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
190 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
191
192 /**
193 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700194 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 * that fragment.
196 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700197 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100198 /**
199 * The package name used to resolve the title resource id.
200 */
201 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
202 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700203 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
204 ":settings:show_fragment_title_resid";
205 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
206 ":settings:show_fragment_as_shortcut";
207
208 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
209 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 private static final String META_DATA_KEY_FRAGMENT_CLASS =
212 "com.android.settings.FRAGMENT_CLASS";
213
214 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
215
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700216 private static final String EMPTY_QUERY = "";
217
Jason Monk2ebc8a02015-02-13 15:23:19 -0500218 /**
219 * Settings will search for system activities of this action and add them as a top level
220 * settings tile using the following parameters.
221 *
222 * <p>A category must be specified in the meta-data for the activity named
223 * {@link #EXTRA_CATEGORY_KEY}
224 *
225 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
226 * otherwise the label for the activity will be used.
227 *
228 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
229 * otherwise the icon for the activity will be used.
230 *
231 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
232 */
233 private static final String EXTRA_SETTINGS_ACTION =
234 "com.android.settings.action.EXTRA_SETTINGS";
235
236 /**
237 * The key used to get the category from metadata of activities of action
238 * {@link #EXTRA_SETTINGS_ACTION}
239 * The value must be one of:
240 * <li>com.android.settings.category.wireless</li>
241 * <li>com.android.settings.category.device</li>
242 * <li>com.android.settings.category.personal</li>
243 * <li>com.android.settings.category.system</li>
244 */
245 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
246
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 private static boolean sShowNoHomeNotice = false;
248
249 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800251 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700252 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800253
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254 // Show only these settings for restricted users
255 private int[] SETTINGS_FOR_RESTRICTED = {
256 R.id.wireless_section,
257 R.id.wifi_settings,
258 R.id.bluetooth_settings,
259 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700260 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 R.id.wireless_settings,
262 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400263 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800264 R.id.display_settings,
265 R.id.storage_settings,
266 R.id.application_settings,
267 R.id.battery_settings,
268 R.id.personal_section,
269 R.id.location_settings,
270 R.id.security_settings,
271 R.id.language_settings,
272 R.id.user_settings,
273 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 R.id.system_section,
275 R.id.date_time_settings,
276 R.id.about_settings,
277 R.id.accessibility_settings,
278 R.id.print_settings,
279 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800280 R.id.home_settings,
281 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 };
283
284 private static final String[] ENTRY_FRAGMENTS = {
285 WirelessSettings.class.getName(),
286 WifiSettings.class.getName(),
287 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700288 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700290 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 TetherSettings.class.getName(),
292 WifiP2pSettings.class.getName(),
293 VpnSettings.class.getName(),
294 DateTimeSettings.class.getName(),
295 LocalePicker.class.getName(),
296 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700297 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 SpellCheckersSettings.class.getName(),
299 UserDictionaryList.class.getName(),
300 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700301 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 DisplaySettings.class.getName(),
303 DeviceInfoSettings.class.getName(),
304 ManageApplications.class.getName(),
305 ProcessStatsUi.class.getName(),
306 NotificationStation.class.getName(),
307 LocationSettings.class.getName(),
308 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400309 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 PrivacySettings.class.getName(),
311 DeviceAdminSettings.class.getName(),
312 AccessibilitySettings.class.getName(),
313 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
315 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700316 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700317 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700318 PrivateVolumeSettings.class.getName(),
319 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 DevelopmentSettings.class.getName(),
321 UsbSettings.class.getName(),
322 AndroidBeam.class.getName(),
323 WifiDisplaySettings.class.getName(),
324 PowerUsageSummary.class.getName(),
325 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000326 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327 CryptKeeperSettings.class.getName(),
328 DataUsageSummary.class.getName(),
329 DreamSettings.class.getName(),
330 UserSettings.class.getName(),
331 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400332 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800333 PrintSettingsFragment.class.getName(),
334 PrintJobSettingsFragment.class.getName(),
335 TrustedCredentialsSettings.class.getName(),
336 PaymentSettings.class.getName(),
337 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700338 ZenModeSettings.class.getName(),
339 NotificationSettings.class.getName(),
340 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
341 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400342 InstalledAppDetails.class.getName(),
343 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700344 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700345 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000346 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400347 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400348 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400349 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400350 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400351 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400352 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400353 PowerUsageDetail.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 };
355
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700356
357 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
358 "android.settings.APPLICATION_DETAILS_SETTINGS"
359 };
360
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 private SharedPreferences mDevelopmentPreferences;
362 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
363
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 private boolean mBatteryPresent = true;
365 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 @Override
367 public void onReceive(Context context, Intent intent) {
368 String action = intent.getAction();
369 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
370 boolean batteryPresent = Utils.isBatteryPresent(intent);
371
372 if (mBatteryPresent != batteryPresent) {
373 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700374 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800375 }
376 }
377 }
378 };
379
Svetoslav990159a2014-04-14 17:14:59 -0700380 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
381 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700382
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700383 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700384 private SwitchBar mSwitchBar;
385
386 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700387
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700388 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700389 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700390
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700391 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700392 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700393
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700394 private ViewGroup mContent;
395
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700396 private SearchView mSearchView;
397 private MenuItem mSearchMenuItem;
398 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700399 private SearchResultsSummary mSearchResultsFragment;
400 private String mSearchQuery;
401
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700402 // Categories
403 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700405 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700406 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800407 private Handler mHandler = new Handler() {
408 @Override
409 public void handleMessage(Message msg) {
410 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700411 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700412 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
413 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700414 buildDashboardCategories(mCategories);
415 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800416 } break;
417 }
418 }
419 };
420
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700422 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700423
Jim Miller0698a212014-10-16 19:49:07 -0700424 private Intent mResultIntentData;
425
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700426 public SwitchBar getSwitchBar() {
427 return mSwitchBar;
428 }
429
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700430 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
431 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700432 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700433 }
434 return mCategories;
435 }
436
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800437 @Override
438 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
439 // Override the fragment title for Wallpaper settings
440 int titleRes = pref.getTitleRes();
441 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
442 titleRes = R.string.wallpaper_settings_fragment_title;
443 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
444 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
445 if (UserManager.get(this).isLinkedUser()) {
446 titleRes = R.string.profile_info_settings_title;
447 } else {
448 titleRes = R.string.user_info_settings_title;
449 }
450 }
451 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
452 null, 0);
453 return true;
454 }
455
456 @Override
457 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
458 return false;
459 }
460
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700461 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700462 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700463 Message msg = new Message();
464 msg.what = MSG_BUILD_CATEGORIES;
465 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 }
467 }
468
469 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800470 public void onConfigurationChanged(Configuration newConfig) {
471 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800472 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
474
475 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700476 protected void onStart() {
477 super.onStart();
478
479 if (mNeedToRevertToInitialFragment) {
480 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800482 }
483
484 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700485 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700486 if (!mDisplaySearch) {
487 return false;
488 }
489
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700490 MenuInflater inflater = getMenuInflater();
491 inflater.inflate(R.menu.options_menu, menu);
492
493 // Cache the search query (can be overriden by the OnQueryTextListener)
494 final String query = mSearchQuery;
495
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700496 mSearchMenuItem = menu.findItem(R.id.search);
497 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700498
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700499 if (mSearchMenuItem == null || mSearchView == null) {
500 return false;
501 }
502
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700503 if (mSearchResultsFragment != null) {
504 mSearchResultsFragment.setSearchView(mSearchView);
505 }
506
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700507 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700508 mSearchView.setOnQueryTextListener(this);
509 mSearchView.setOnCloseListener(this);
510
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700511 if (mSearchMenuItemExpanded) {
512 mSearchMenuItem.expandActionView();
513 }
514 mSearchView.setQuery(query, true /* submit */);
515
516 return true;
517 }
518
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700519 private static boolean isShortCutIntent(final Intent intent) {
520 Set<String> categories = intent.getCategories();
521 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
522 }
523
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700524 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700525 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700526 if (action == null) {
527 return false;
528 }
529 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
530 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
531 }
532 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700533 }
534
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700535 @Override
536 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700537 super.onCreate(savedState);
538
539 // Should happen before any call to getIntent()
540 getMetaData();
541
542 final Intent intent = getIntent();
543 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
544 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
548 Context.MODE_PRIVATE);
549
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700550 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700551 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700552
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700553 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700554 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
555
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700556 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700557 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700558
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700559 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700560
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700561 // This is a "Sub Settings" when:
562 // - this is a real SubSettings
563 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700564 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700565 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
566
567 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
568 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700569 // Check also that we are not a Theme Dialog as we don't want to override them
570 final int themeResId = getThemeResId();
571 if (themeResId != R.style.Theme_DialogWhenLarge &&
572 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
573 setTheme(R.style.Theme_SubSettings);
574 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700575 }
576
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700577 setContentView(mIsShowingDashboard ?
578 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700580 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700581
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582 getFragmentManager().addOnBackStackChangedListener(this);
583
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700584 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800585 // Run the Index update only if we have some space
586 if (!Utils.isLowStorage(this)) {
587 Index.getInstance(getApplicationContext()).update();
588 } else {
589 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
590 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700591 }
592
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700593 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700594 // We are restarting from a previous saved state; used that to initialize, instead
595 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700596 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
597 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800598
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700599 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800600
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700601 ArrayList<DashboardCategory> categories =
602 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
603 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700604 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700605 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700606 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800607 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700608
609 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700610 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700611 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
612 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800613 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700614 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700615 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
616 // only if it is a sub settings
617 if (mIsShortcut) {
618 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700619 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700620 } else if (isSubSettings) {
621 mDisplayHomeAsUpEnabled = true;
622 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700623 } else {
624 mDisplayHomeAsUpEnabled = false;
625 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700626 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700627 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700628
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700629 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700630 switchToFragment(initialFragmentName, initialArguments, true, false,
631 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000632 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700633 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700634 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700635 // Show Search affordance
636 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700637 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700638 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700639 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 }
641 }
642
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700643 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700644 if (mActionBar != null) {
645 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
646 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
647 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700648 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
649
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
652
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700653 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 if (buttonBar != null) {
655 buttonBar.setVisibility(View.VISIBLE);
656
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700657 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 backButton.setOnClickListener(new OnClickListener() {
659 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700660 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 finish();
662 }
663 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700664 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800665 skipButton.setOnClickListener(new OnClickListener() {
666 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700667 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800668 finish();
669 }
670 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700671 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800672 mNextButton.setOnClickListener(new OnClickListener() {
673 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700674 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800675 finish();
676 }
677 });
678
679 // set our various button parameters
680 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
681 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
682 if (TextUtils.isEmpty(buttonText)) {
683 mNextButton.setVisibility(View.GONE);
684 }
685 else {
686 mNextButton.setText(buttonText);
687 }
688 }
689 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
690 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
691 if (TextUtils.isEmpty(buttonText)) {
692 backButton.setVisibility(View.GONE);
693 }
694 else {
695 backButton.setText(buttonText);
696 }
697 }
698 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
699 skipButton.setVisibility(View.VISIBLE);
700 }
701 }
702 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700703
704 mHomeActivitiesCount = getHomeActivitiesCount();
705 }
706
707 private int getHomeActivitiesCount() {
708 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
709 getPackageManager().getHomeActivities(homeApps);
710 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800711 }
712
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700713 private void setTitleFromIntent(Intent intent) {
714 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
715 if (initialTitleResId > 0) {
716 mInitialTitle = null;
717 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100718
719 final String initialTitleResPackageName = intent.getStringExtra(
720 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
721 if (initialTitleResPackageName != null) {
722 try {
723 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
724 0 /* flags */, new UserHandle(UserHandle.myUserId()));
725 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
726 setTitle(mInitialTitle);
727 mInitialTitleResId = -1;
728 return;
729 } catch (NameNotFoundException e) {
730 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
731 }
732 } else {
733 setTitle(mInitialTitleResId);
734 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700735 } else {
736 mInitialTitleResId = -1;
737 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
738 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
739 setTitle(mInitialTitle);
740 }
741 }
742
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800743 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800744 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700745 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800746 }
747
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800749 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700750
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800751 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700752 if (mInitialTitleResId > 0) {
753 setTitle(mInitialTitleResId);
754 } else {
755 setTitle(mInitialTitle);
756 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700757 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800758 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700759
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800760 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
761 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700762
763 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800764 }
765
766 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
767 final CharSequence title;
768 final int titleRes = bse.getBreadCrumbTitleRes();
769 if (titleRes > 0) {
770 title = getText(titleRes);
771 } else {
772 title = bse.getBreadCrumbTitle();
773 }
774 if (title != null) {
775 setTitle(title);
776 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800777 }
778
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800779 @Override
780 protected void onSaveInstanceState(Bundle outState) {
781 super.onSaveInstanceState(outState);
782
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700783 if (mCategories.size() > 0) {
784 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800785 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700786
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700787 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700788 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700789
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700790 if (mDisplaySearch) {
791 // The option menus are created if the ActionBar is visible and they are also created
792 // asynchronously. If you launch Settings with an Intent action like
793 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
794 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
795 // menu item and search view are null.
796 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
797 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700798
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700799 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
800 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
801 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700802
803 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800804 }
805
806 @Override
807 public void onResume() {
808 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400809 if (mIsShowingDashboard) {
810 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
811 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800812
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700813 final int newHomeActivityCount = getHomeActivitiesCount();
814 if (newHomeActivityCount != mHomeActivitiesCount) {
815 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700816 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700817 }
818
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800819 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
820 @Override
821 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700822 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800823 }
824 };
825 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
826 mDevelopmentPreferencesListener);
827
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800828 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700829
Svetoslav990159a2014-04-14 17:14:59 -0700830 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700831
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700832 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700833 onQueryTextSubmit(mSearchQuery);
834 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800835 }
836
837 @Override
838 public void onPause() {
839 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400840 if (mIsShowingDashboard) {
841 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
842 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800843 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700844 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800845 }
846
847 @Override
848 public void onDestroy() {
849 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700850
851 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
852 mDevelopmentPreferencesListener);
853 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 }
855
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800856 protected boolean isValidFragment(String fragmentName) {
857 // Almost all fragments are wrapped in this,
858 // except for a few that have their own activities.
859 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
860 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
861 }
862 return false;
863 }
864
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800865 @Override
866 public Intent getIntent() {
867 Intent superIntent = super.getIntent();
868 String startingFragment = getStartingFragmentClass(superIntent);
869 // This is called from super.onCreate, isMultiPane() is not yet reliable
870 // Do not use onIsHidingHeaders either, which relies itself on this method
871 if (startingFragment != null) {
872 Intent modIntent = new Intent(superIntent);
873 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
874 Bundle args = superIntent.getExtras();
875 if (args != null) {
876 args = new Bundle(args);
877 } else {
878 args = new Bundle();
879 }
880 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100881 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800882 return modIntent;
883 }
884 return superIntent;
885 }
886
887 /**
888 * Checks if the component name in the intent is different from the Settings class and
889 * returns the class name to load as a fragment.
890 */
891 private String getStartingFragmentClass(Intent intent) {
892 if (mFragmentClass != null) return mFragmentClass;
893
894 String intentClass = intent.getComponent().getClassName();
895 if (intentClass.equals(getClass().getName())) return null;
896
897 if ("com.android.settings.ManageApplications".equals(intentClass)
898 || "com.android.settings.RunningServices".equals(intentClass)
899 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
900 // Old names of manage apps.
901 intentClass = com.android.settings.applications.ManageApplications.class.getName();
902 }
903
904 return intentClass;
905 }
906
907 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000908 * Start a new fragment containing a preference panel. If the preferences
909 * are being displayed in multi-pane mode, the given fragment class will
910 * be instantiated and placed in the appropriate pane. If running in
911 * single-pane mode, a new activity will be launched in which to show the
912 * fragment.
913 *
914 * @param fragmentClass Full name of the class implementing the fragment.
915 * @param args Any desired arguments to supply to the fragment.
916 * @param titleRes Optional resource identifier of the title of this
917 * fragment.
918 * @param titleText Optional text of the title of this fragment.
919 * @param resultTo Optional fragment that result data should be sent to.
920 * If non-null, resultTo.onActivityResult() will be called when this
921 * preference panel is done. The launched panel must use
922 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
923 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700924 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000925 */
926 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700927 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700928 String title = null;
929 if (titleRes < 0) {
930 if (titleText != null) {
931 title = titleText.toString();
932 } else {
933 // There not much we can do in that case
934 title = "";
935 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700936 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700937 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700938 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000939 }
940
941 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100942 * Start a new fragment in a new activity containing a preference panel for a given user. If the
943 * preferences are being displayed in multi-pane mode, the given fragment class will be
944 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
945 * activity will be launched in which to show the fragment.
946 *
947 * @param fragmentClass Full name of the class implementing the fragment.
948 * @param args Any desired arguments to supply to the fragment.
949 * @param titleRes Optional resource identifier of the title of this fragment.
950 * @param titleText Optional text of the title of this fragment.
951 * @param userHandle The user for which the panel has to be started.
952 */
953 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
954 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700955 // This is a workaround.
956 //
957 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
958 // starting the fragment could cause a native stack corruption. See b/17523189. However,
959 // adding that flag and start the preference panel with the same UserHandler will make it
960 // impossible to use back button to return to the previous screen. See b/20042570.
961 //
962 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
963 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
964 // when we're calling it as the same user.
965 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
966 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
967 } else {
968 String title = null;
969 if (titleRes < 0) {
970 if (titleText != null) {
971 title = titleText.toString();
972 } else {
973 // There not much we can do in that case
974 title = "";
975 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100976 }
Lifu Tangd0332852015-04-02 12:05:46 -0700977 Utils.startWithFragmentAsUser(this, fragmentClass, args,
978 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100979 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100980 }
981
982 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800983 * Called by a preference panel fragment to finish itself.
984 *
985 * @param caller The fragment that is asking to be finished.
986 * @param resultCode Optional result code to send back to the original
987 * launching fragment.
988 * @param resultData Optional result data to send back to the original
989 * launching fragment.
990 */
991 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
992 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700993 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800994 }
995
996 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000997 * Start a new fragment.
998 *
999 * @param fragment The fragment to start
1000 * @param push If true, the current fragment will be pushed onto the back stack. If false,
1001 * the current fragment will be replaced.
1002 */
1003 public void startPreferenceFragment(Fragment fragment, boolean push) {
1004 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001005 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001006 if (push) {
1007 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1008 transaction.addToBackStack(BACK_STACK_PREFS);
1009 } else {
1010 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1011 }
1012 transaction.commitAllowingStateLoss();
1013 }
1014
1015 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001016 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1017 */
1018 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001019 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001020 if (validate && !isValidFragment(fragmentName)) {
1021 throw new IllegalArgumentException("Invalid fragment for this activity: "
1022 + fragmentName);
1023 }
1024 Fragment f = Fragment.instantiate(this, fragmentName, args);
1025 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001026 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001027 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001028 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001029 }
1030 if (addToBackStack) {
1031 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1032 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001033 if (titleResId > 0) {
1034 transaction.setBreadCrumbTitle(titleResId);
1035 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001036 transaction.setBreadCrumbTitle(title);
1037 }
1038 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001039 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001040 return f;
1041 }
1042
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001043 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001044 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001045 *
1046 * @param categories The list in which to place the tiles categories.
1047 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001048 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001049 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001050 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1051 updateTilesList(categories);
1052 }
1053
1054 /**
1055 * Parse the given XML file as a categories description, adding each
1056 * parsed categories and tiles into the target list.
1057 *
1058 * @param resid The XML resource to load and parse.
1059 * @param target The list in which the parsed categories and tiles should be placed.
1060 */
1061 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1062 XmlResourceParser parser = null;
1063 try {
1064 parser = getResources().getXml(resid);
1065 AttributeSet attrs = Xml.asAttributeSet(parser);
1066
1067 int type;
1068 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1069 && type != XmlPullParser.START_TAG) {
1070 // Parse next until start tag is found
1071 }
1072
1073 String nodeName = parser.getName();
1074 if (!"dashboard-categories".equals(nodeName)) {
1075 throw new RuntimeException(
1076 "XML document must start with <preference-categories> tag; found"
1077 + nodeName + " at " + parser.getPositionDescription());
1078 }
1079
1080 Bundle curBundle = null;
1081
1082 final int outerDepth = parser.getDepth();
1083 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1084 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1085 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1086 continue;
1087 }
1088
1089 nodeName = parser.getName();
1090 if ("dashboard-category".equals(nodeName)) {
1091 DashboardCategory category = new DashboardCategory();
1092
1093 TypedArray sa = obtainStyledAttributes(
1094 attrs, com.android.internal.R.styleable.PreferenceHeader);
1095 category.id = sa.getResourceId(
1096 com.android.internal.R.styleable.PreferenceHeader_id,
1097 (int)DashboardCategory.CAT_ID_UNDEFINED);
1098
1099 TypedValue tv = sa.peekValue(
1100 com.android.internal.R.styleable.PreferenceHeader_title);
1101 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1102 if (tv.resourceId != 0) {
1103 category.titleRes = tv.resourceId;
1104 } else {
1105 category.title = tv.string;
1106 }
1107 }
1108 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001109 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1110 tv = sa.peekValue(
1111 com.android.internal.R.styleable.Preference_key);
1112 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1113 if (tv.resourceId != 0) {
1114 category.key = getString(tv.resourceId);
1115 } else {
1116 category.key = tv.string.toString();
1117 }
1118 }
1119 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001120
1121 final int innerDepth = parser.getDepth();
1122 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1123 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1124 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1125 continue;
1126 }
1127
1128 String innerNodeName = parser.getName();
1129 if (innerNodeName.equals("dashboard-tile")) {
1130 DashboardTile tile = new DashboardTile();
1131
1132 sa = obtainStyledAttributes(
1133 attrs, com.android.internal.R.styleable.PreferenceHeader);
1134 tile.id = sa.getResourceId(
1135 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001136 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001137 tv = sa.peekValue(
1138 com.android.internal.R.styleable.PreferenceHeader_title);
1139 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1140 if (tv.resourceId != 0) {
1141 tile.titleRes = tv.resourceId;
1142 } else {
1143 tile.title = tv.string;
1144 }
1145 }
1146 tv = sa.peekValue(
1147 com.android.internal.R.styleable.PreferenceHeader_summary);
1148 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1149 if (tv.resourceId != 0) {
1150 tile.summaryRes = tv.resourceId;
1151 } else {
1152 tile.summary = tv.string;
1153 }
1154 }
1155 tile.iconRes = sa.getResourceId(
1156 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1157 tile.fragment = sa.getString(
1158 com.android.internal.R.styleable.PreferenceHeader_fragment);
1159 sa.recycle();
1160
1161 if (curBundle == null) {
1162 curBundle = new Bundle();
1163 }
1164
1165 final int innerDepth2 = parser.getDepth();
1166 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1167 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1168 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1169 continue;
1170 }
1171
1172 String innerNodeName2 = parser.getName();
1173 if (innerNodeName2.equals("extra")) {
1174 getResources().parseBundleExtra("extra", attrs, curBundle);
1175 XmlUtils.skipCurrentTag(parser);
1176
1177 } else if (innerNodeName2.equals("intent")) {
1178 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1179
1180 } else {
1181 XmlUtils.skipCurrentTag(parser);
1182 }
1183 }
1184
1185 if (curBundle.size() > 0) {
1186 tile.fragmentArguments = curBundle;
1187 curBundle = null;
1188 }
1189
PauloftheWest38155612014-06-30 10:02:36 -07001190 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001191 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001192 category.addTile(tile);
1193 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001194
Jason Monk2ebc8a02015-02-13 15:23:19 -05001195 } else if (innerNodeName.equals("external-tiles")) {
1196 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001197 } else {
1198 XmlUtils.skipCurrentTag(parser);
1199 }
1200 }
1201
1202 target.add(category);
1203 } else {
1204 XmlUtils.skipCurrentTag(parser);
1205 }
1206 }
1207
1208 } catch (XmlPullParserException e) {
1209 throw new RuntimeException("Error parsing categories", e);
1210 } catch (IOException e) {
1211 throw new RuntimeException("Error parsing categories", e);
1212 } finally {
1213 if (parser != null) parser.close();
1214 }
1215 }
1216
1217 private void updateTilesList(List<DashboardCategory> target) {
1218 final boolean showDev = mDevelopmentPreferences.getBoolean(
1219 DevelopmentSettings.PREF_SHOW,
1220 android.os.Build.TYPE.equals("eng"));
1221
1222 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1223
1224 final int size = target.size();
1225 for (int i = 0; i < size; i++) {
1226
1227 DashboardCategory category = target.get(i);
1228
1229 // Ids are integers, so downcasting is ok
1230 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001231 int n = category.getTilesCount() - 1;
1232 while (n >= 0) {
1233
1234 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001235 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001236 id = (int) tile.id;
1237 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001238 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1239 removeTile = true;
1240 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001241 } else if (id == R.id.wifi_settings) {
1242 // Remove WiFi Settings if WiFi service is not available.
1243 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001244 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001245 }
1246 } else if (id == R.id.bluetooth_settings) {
1247 // Remove Bluetooth Settings if Bluetooth service is not available.
1248 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001249 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001250 }
1251 } else if (id == R.id.data_usage_settings) {
1252 // Remove data usage when kernel module not enabled
1253 final INetworkManagementService netManager = INetworkManagementService.Stub
1254 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1255 try {
1256 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001257 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001258 }
1259 } catch (RemoteException e) {
1260 // ignored
1261 }
1262 } else if (id == R.id.battery_settings) {
1263 // Remove battery settings when battery is not available. (e.g. TV)
1264
1265 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001266 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 }
1268 } else if (id == R.id.home_settings) {
1269 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001270 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001271 }
1272 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001273 boolean hasMultipleUsers =
1274 ((UserManager) getSystemService(Context.USER_SERVICE))
1275 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001276 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001277 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001278 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001279 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001280 }
1281 } else if (id == R.id.nfc_payment_settings) {
1282 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001283 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001284 } else {
1285 // Only show if NFC is on and we have the HCE feature
1286 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001287 if (adapter == null || !adapter.isEnabled() ||
1288 !getPackageManager().hasSystemFeature(
1289 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001290 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001291 }
1292 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001293 } else if (id == R.id.print_settings) {
1294 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1295 PackageManager.FEATURE_PRINTING);
1296 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001297 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001298 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001299 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001300 if (!showDev || um.hasUserRestriction(
1301 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001302 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001303 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001304 }
1305
1306 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001307 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1308 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001309 }
1310
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001311 if (removeTile && n < category.getTilesCount()) {
1312 category.removeTile(n);
1313 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001314 n--;
1315 }
1316 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001317 addExternalTiles(target);
1318 }
1319
1320 private void addExternalTiles(List<DashboardCategory> target) {
1321 Map<Pair<String, String>, DashboardTile> addedCache =
1322 new ArrayMap<Pair<String, String>, DashboardTile>();
1323 UserManager userManager = UserManager.get(this);
1324 for (UserHandle user : userManager.getUserProfiles()) {
1325 addExternalTiles(target, user, addedCache);
1326 }
1327 }
1328
1329 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1330 Map<Pair<String, String>, DashboardTile> addedCache) {
1331 PackageManager pm = getPackageManager();
1332 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1333 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1334 PackageManager.GET_META_DATA, user.getIdentifier());
1335 for (ResolveInfo resolved : results) {
1336 if (!resolved.system) {
1337 // Do not allow any app to add to settings, only system ones.
1338 continue;
1339 }
1340 ActivityInfo activityInfo = resolved.activityInfo;
1341 Bundle metaData = activityInfo.metaData;
1342 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1343 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1344 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1345 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1346 continue;
1347 }
1348 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1349 DashboardCategory category = getCategory(target, categoryKey);
1350 if (category == null) {
1351 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1352 + "category key " + categoryKey);
1353 continue;
1354 }
1355 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1356 activityInfo.name);
1357 DashboardTile tile = addedCache.get(key);
1358 if (tile == null) {
1359 tile = new DashboardTile();
1360 tile.intent = new Intent().setClassName(
1361 activityInfo.packageName, activityInfo.name);
1362 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1363
1364 if (category.externalIndex == -1) {
1365 // If no location for external tiles has been specified for this category,
1366 // then just put them at the end.
1367 category.addTile(tile);
1368 } else {
1369 category.addTile(category.externalIndex, tile);
1370 }
1371 addedCache.put(key, tile);
1372 }
1373 tile.userHandle.add(user);
1374 }
1375 }
1376
1377 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1378 for (DashboardCategory category : target) {
1379 if (categoryKey.equals(category.key)) {
1380 return category;
1381 }
1382 }
1383 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001384 }
1385
1386 private boolean updateHomeSettingTiles(DashboardTile tile) {
1387 // Once we decide to show Home settings, keep showing it forever
1388 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1389 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1390 return true;
1391 }
1392
1393 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001394 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001395 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001396 // When there's only one available home app, omit this settings
1397 // category entirely at the top level UI. If the user just
1398 // uninstalled the penultimate home app candidiate, we also
1399 // now tell them about why they aren't seeing 'Home' in the list.
1400 if (sShowNoHomeNotice) {
1401 sShowNoHomeNotice = false;
1402 NoHomeDialogFragment.show(this);
1403 }
1404 return false;
1405 } else {
1406 // Okay, we're allowing the Home settings category. Tell it, when
1407 // invoked via this front door, that we'll need to be told about the
1408 // case when the user uninstalls all but one home app.
1409 if (tile.fragmentArguments == null) {
1410 tile.fragmentArguments = new Bundle();
1411 }
1412 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1413 }
1414 } catch (Exception e) {
1415 // Can't look up the home activity; bail on configuring the icon
1416 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1417 }
1418
1419 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1420 return true;
1421 }
1422
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001423 private void getMetaData() {
1424 try {
1425 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1426 PackageManager.GET_META_DATA);
1427 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001428 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1429 } catch (NameNotFoundException nnfe) {
1430 // No recovery
1431 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1432 }
1433 }
1434
1435 // give subclasses access to the Next button
1436 public boolean hasNextButton() {
1437 return mNextButton != null;
1438 }
1439
1440 public Button getNextButton() {
1441 return mNextButton;
1442 }
1443
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001444 @Override
1445 public boolean shouldUpRecreateTask(Intent targetIntent) {
1446 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1447 }
1448
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001449 public static void requestHomeNotice() {
1450 sShowNoHomeNotice = true;
1451 }
1452
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001453 @Override
1454 public boolean onQueryTextSubmit(String query) {
1455 switchToSearchResultsFragmentIfNeeded();
1456 mSearchQuery = query;
1457 return mSearchResultsFragment.onQueryTextSubmit(query);
1458 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001459
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001460 @Override
1461 public boolean onQueryTextChange(String newText) {
1462 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001463 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001464 return false;
1465 }
1466 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001467 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001468
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001469 @Override
1470 public boolean onClose() {
1471 return false;
1472 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001474 @Override
1475 public boolean onMenuItemActionExpand(MenuItem item) {
1476 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001477 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001478 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001479 return true;
1480 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001481
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001482 @Override
1483 public boolean onMenuItemActionCollapse(MenuItem item) {
1484 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001485 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001486 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001487 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001488 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001489 return true;
1490 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001491
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001492 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001493 if (mSearchResultsFragment != null) {
1494 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001495 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001496 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001497 if (current != null && current instanceof SearchResultsSummary) {
1498 mSearchResultsFragment = (SearchResultsSummary) current;
1499 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001500 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001501 SearchResultsSummary.class.getName(), null, false, true,
1502 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001503 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001504 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001505 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001506 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001507
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001508 public void needToRevertToInitialFragment() {
1509 mNeedToRevertToInitialFragment = true;
1510 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001511
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001512 private void revertToInitialFragment() {
1513 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001514 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001515 mSearchMenuItemExpanded = false;
1516 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1517 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001518 if (mSearchMenuItem != null) {
1519 mSearchMenuItem.collapseActionView();
1520 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001521 }
Jim Miller0698a212014-10-16 19:49:07 -07001522
1523 public Intent getResultIntentData() {
1524 return mResultIntentData;
1525 }
1526
1527 public void setResultIntentData(Intent resultIntentData) {
1528 mResultIntentData = resultIntentData;
1529 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001530}