blob: 89dbc99d1ae136ef95893be1cf6e6b68e7841f6e [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 Spurlockf57bad72015-04-30 09:26:15 -0400106import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400107import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400108import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400109import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400110import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.print.PrintJobSettingsFragment;
112import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500113import com.android.settings.search.DynamicIndexableContentMonitor;
114import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700115import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import com.android.settings.tts.TextToSpeechSettings;
117import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700118import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import com.android.settings.vpn2.VpnSettings;
120import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700121import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700123import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124import com.android.settings.wifi.WifiSettings;
125import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500126
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127import org.xmlpull.v1.XmlPullParser;
128import org.xmlpull.v1.XmlPullParserException;
129
130import java.io.IOException;
131import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500133import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700134import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800135
136public class SettingsActivity extends Activity
137 implements PreferenceManager.OnPreferenceTreeClickListener,
138 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100139 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700140 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100141 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
143 private static final String LOG_TAG = "Settings";
144
145 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700146 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700147 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
148 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700149 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700150 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700151 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152
153 /**
154 * When starting this activity, the invoking Intent can contain this extra
155 * string to specify which fragment should be initially displayed.
156 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
157 * will call isValidFragment() to confirm that the fragment class name is valid for this
158 * activity.
159 */
160 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
161
162 /**
163 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
164 * this extra can also be specified to supply a Bundle of arguments to pass
165 * to that fragment when it is instantiated during the initial creation
166 * of the activity.
167 */
168 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
169
170 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700171 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
172 */
173 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
174
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800175 public static final String BACK_STACK_PREFS = ":settings:prefs";
176
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 // extras that allow any preference activity to be launched as part of a wizard
178
179 // show Back and Next buttons? takes boolean parameter
180 // Back will then return RESULT_CANCELED and Next RESULT_OK
181 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
182
183 // add a Skip button?
184 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
185
186 // specify custom text for the Back or Next buttons, or cause a button to not appear
187 // at all by setting it to null
188 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
189 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
190
191 /**
192 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700193 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194 * that fragment.
195 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700196 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100197 /**
198 * The package name used to resolve the title resource id.
199 */
200 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
201 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700202 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
203 ":settings:show_fragment_title_resid";
204 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
205 ":settings:show_fragment_as_shortcut";
206
207 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
208 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210 private static final String META_DATA_KEY_FRAGMENT_CLASS =
211 "com.android.settings.FRAGMENT_CLASS";
212
213 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
214
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700215 private static final String EMPTY_QUERY = "";
216
Jason Monk2ebc8a02015-02-13 15:23:19 -0500217 /**
218 * Settings will search for system activities of this action and add them as a top level
219 * settings tile using the following parameters.
220 *
221 * <p>A category must be specified in the meta-data for the activity named
222 * {@link #EXTRA_CATEGORY_KEY}
223 *
224 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
225 * otherwise the label for the activity will be used.
226 *
227 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
228 * otherwise the icon for the activity will be used.
229 *
230 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
231 */
232 private static final String EXTRA_SETTINGS_ACTION =
233 "com.android.settings.action.EXTRA_SETTINGS";
234
235 /**
236 * The key used to get the category from metadata of activities of action
237 * {@link #EXTRA_SETTINGS_ACTION}
238 * The value must be one of:
239 * <li>com.android.settings.category.wireless</li>
240 * <li>com.android.settings.category.device</li>
241 * <li>com.android.settings.category.personal</li>
242 * <li>com.android.settings.category.system</li>
243 */
244 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
245
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 private static boolean sShowNoHomeNotice = false;
247
248 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800249
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800250 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700251 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800252
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800253 // Show only these settings for restricted users
254 private int[] SETTINGS_FOR_RESTRICTED = {
255 R.id.wireless_section,
256 R.id.wifi_settings,
257 R.id.bluetooth_settings,
258 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700259 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 R.id.wireless_settings,
261 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400262 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800263 R.id.display_settings,
264 R.id.storage_settings,
265 R.id.application_settings,
266 R.id.battery_settings,
267 R.id.personal_section,
268 R.id.location_settings,
269 R.id.security_settings,
270 R.id.language_settings,
271 R.id.user_settings,
272 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 R.id.system_section,
274 R.id.date_time_settings,
275 R.id.about_settings,
276 R.id.accessibility_settings,
277 R.id.print_settings,
278 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800279 R.id.home_settings,
280 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800281 };
282
283 private static final String[] ENTRY_FRAGMENTS = {
284 WirelessSettings.class.getName(),
285 WifiSettings.class.getName(),
286 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700287 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700289 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 TetherSettings.class.getName(),
291 WifiP2pSettings.class.getName(),
292 VpnSettings.class.getName(),
293 DateTimeSettings.class.getName(),
294 LocalePicker.class.getName(),
295 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700296 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 SpellCheckersSettings.class.getName(),
298 UserDictionaryList.class.getName(),
299 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700300 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 DisplaySettings.class.getName(),
302 DeviceInfoSettings.class.getName(),
303 ManageApplications.class.getName(),
304 ProcessStatsUi.class.getName(),
305 NotificationStation.class.getName(),
306 LocationSettings.class.getName(),
307 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400308 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 PrivacySettings.class.getName(),
310 DeviceAdminSettings.class.getName(),
311 AccessibilitySettings.class.getName(),
312 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
314 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700315 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700316 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700317 PrivateVolumeSettings.class.getName(),
318 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800319 DevelopmentSettings.class.getName(),
320 UsbSettings.class.getName(),
321 AndroidBeam.class.getName(),
322 WifiDisplaySettings.class.getName(),
323 PowerUsageSummary.class.getName(),
324 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000325 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 CryptKeeperSettings.class.getName(),
327 DataUsageSummary.class.getName(),
328 DreamSettings.class.getName(),
329 UserSettings.class.getName(),
330 NotificationAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800331 PrintSettingsFragment.class.getName(),
332 PrintJobSettingsFragment.class.getName(),
333 TrustedCredentialsSettings.class.getName(),
334 PaymentSettings.class.getName(),
335 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700336 ZenModeSettings.class.getName(),
337 NotificationSettings.class.getName(),
338 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
339 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400340 InstalledAppDetails.class.getName(),
341 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700342 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700343 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000344 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400345 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400346 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400347 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400348 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400349 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400350 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400351 PowerUsageDetail.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 };
353
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700354
355 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
356 "android.settings.APPLICATION_DETAILS_SETTINGS"
357 };
358
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 private SharedPreferences mDevelopmentPreferences;
360 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
361
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 private boolean mBatteryPresent = true;
363 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 @Override
365 public void onReceive(Context context, Intent intent) {
366 String action = intent.getAction();
367 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
368 boolean batteryPresent = Utils.isBatteryPresent(intent);
369
370 if (mBatteryPresent != batteryPresent) {
371 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700372 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 }
374 }
375 }
376 };
377
Svetoslav990159a2014-04-14 17:14:59 -0700378 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
379 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700380
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700381 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700382 private SwitchBar mSwitchBar;
383
384 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700385
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700386 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700387 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700388
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700389 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700390 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700391
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700392 private ViewGroup mContent;
393
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700394 private SearchView mSearchView;
395 private MenuItem mSearchMenuItem;
396 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700397 private SearchResultsSummary mSearchResultsFragment;
398 private String mSearchQuery;
399
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700400 // Categories
401 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700403 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700404 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405 private Handler mHandler = new Handler() {
406 @Override
407 public void handleMessage(Message msg) {
408 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700409 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700410 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
411 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700412 buildDashboardCategories(mCategories);
413 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800414 } break;
415 }
416 }
417 };
418
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700419 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700420 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421
Jim Miller0698a212014-10-16 19:49:07 -0700422 private Intent mResultIntentData;
423
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700424 public SwitchBar getSwitchBar() {
425 return mSwitchBar;
426 }
427
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700428 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
429 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700430 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700431 }
432 return mCategories;
433 }
434
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800435 @Override
436 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
437 // Override the fragment title for Wallpaper settings
438 int titleRes = pref.getTitleRes();
439 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
440 titleRes = R.string.wallpaper_settings_fragment_title;
441 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
442 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
443 if (UserManager.get(this).isLinkedUser()) {
444 titleRes = R.string.profile_info_settings_title;
445 } else {
446 titleRes = R.string.user_info_settings_title;
447 }
448 }
449 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
450 null, 0);
451 return true;
452 }
453
454 @Override
455 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
456 return false;
457 }
458
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700459 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700460 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700461 Message msg = new Message();
462 msg.what = MSG_BUILD_CATEGORIES;
463 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 }
465 }
466
467 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800468 public void onConfigurationChanged(Configuration newConfig) {
469 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800470 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 }
472
473 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700474 protected void onStart() {
475 super.onStart();
476
477 if (mNeedToRevertToInitialFragment) {
478 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800479 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800480 }
481
482 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700483 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700484 if (!mDisplaySearch) {
485 return false;
486 }
487
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700488 MenuInflater inflater = getMenuInflater();
489 inflater.inflate(R.menu.options_menu, menu);
490
491 // Cache the search query (can be overriden by the OnQueryTextListener)
492 final String query = mSearchQuery;
493
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700494 mSearchMenuItem = menu.findItem(R.id.search);
495 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700496
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700497 if (mSearchMenuItem == null || mSearchView == null) {
498 return false;
499 }
500
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700501 if (mSearchResultsFragment != null) {
502 mSearchResultsFragment.setSearchView(mSearchView);
503 }
504
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700505 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700506 mSearchView.setOnQueryTextListener(this);
507 mSearchView.setOnCloseListener(this);
508
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700509 if (mSearchMenuItemExpanded) {
510 mSearchMenuItem.expandActionView();
511 }
512 mSearchView.setQuery(query, true /* submit */);
513
514 return true;
515 }
516
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700517 private static boolean isShortCutIntent(final Intent intent) {
518 Set<String> categories = intent.getCategories();
519 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
520 }
521
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700522 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700523 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700524 if (action == null) {
525 return false;
526 }
527 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
528 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
529 }
530 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700531 }
532
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700533 @Override
534 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700535 super.onCreate(savedState);
536
537 // Should happen before any call to getIntent()
538 getMetaData();
539
540 final Intent intent = getIntent();
541 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
542 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800544
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
546 Context.MODE_PRIVATE);
547
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700548 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700549 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700550
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700551 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700552 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
553
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700554 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700555 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700556
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700557 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700558
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700559 // This is a "Sub Settings" when:
560 // - this is a real SubSettings
561 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700562 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700563 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
564
565 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
566 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700567 // Check also that we are not a Theme Dialog as we don't want to override them
568 final int themeResId = getThemeResId();
569 if (themeResId != R.style.Theme_DialogWhenLarge &&
570 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
571 setTheme(R.style.Theme_SubSettings);
572 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700573 }
574
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700575 setContentView(mIsShowingDashboard ?
576 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700578 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700579
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580 getFragmentManager().addOnBackStackChangedListener(this);
581
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700582 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800583 // Run the Index update only if we have some space
584 if (!Utils.isLowStorage(this)) {
585 Index.getInstance(getApplicationContext()).update();
586 } else {
587 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
588 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700589 }
590
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700591 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700592 // We are restarting from a previous saved state; used that to initialize, instead
593 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700594 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
595 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800596
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700597 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800598
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700599 ArrayList<DashboardCategory> categories =
600 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
601 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700602 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700603 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700604 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800605 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700606
607 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700608 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700609 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
610 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800611 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700612 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700613 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
614 // only if it is a sub settings
615 if (mIsShortcut) {
616 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700617 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700618 } else if (isSubSettings) {
619 mDisplayHomeAsUpEnabled = true;
620 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700621 } else {
622 mDisplayHomeAsUpEnabled = false;
623 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700624 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700625 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700626
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700627 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 switchToFragment(initialFragmentName, initialArguments, true, false,
629 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000630 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700631 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700632 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700633 // Show Search affordance
634 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700635 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700636 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700637 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800638 }
639 }
640
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700641 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700642 if (mActionBar != null) {
643 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
644 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
645 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700646 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
647
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
650
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700651 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 if (buttonBar != null) {
653 buttonBar.setVisibility(View.VISIBLE);
654
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700655 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 backButton.setOnClickListener(new OnClickListener() {
657 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700658 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 finish();
660 }
661 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700662 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800663 skipButton.setOnClickListener(new OnClickListener() {
664 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700665 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 finish();
667 }
668 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700669 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800670 mNextButton.setOnClickListener(new OnClickListener() {
671 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700672 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800673 finish();
674 }
675 });
676
677 // set our various button parameters
678 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
679 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
680 if (TextUtils.isEmpty(buttonText)) {
681 mNextButton.setVisibility(View.GONE);
682 }
683 else {
684 mNextButton.setText(buttonText);
685 }
686 }
687 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
688 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
689 if (TextUtils.isEmpty(buttonText)) {
690 backButton.setVisibility(View.GONE);
691 }
692 else {
693 backButton.setText(buttonText);
694 }
695 }
696 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
697 skipButton.setVisibility(View.VISIBLE);
698 }
699 }
700 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700701
702 mHomeActivitiesCount = getHomeActivitiesCount();
703 }
704
705 private int getHomeActivitiesCount() {
706 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
707 getPackageManager().getHomeActivities(homeApps);
708 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800709 }
710
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700711 private void setTitleFromIntent(Intent intent) {
712 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
713 if (initialTitleResId > 0) {
714 mInitialTitle = null;
715 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100716
717 final String initialTitleResPackageName = intent.getStringExtra(
718 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
719 if (initialTitleResPackageName != null) {
720 try {
721 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
722 0 /* flags */, new UserHandle(UserHandle.myUserId()));
723 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
724 setTitle(mInitialTitle);
725 mInitialTitleResId = -1;
726 return;
727 } catch (NameNotFoundException e) {
728 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
729 }
730 } else {
731 setTitle(mInitialTitleResId);
732 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700733 } else {
734 mInitialTitleResId = -1;
735 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
736 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
737 setTitle(mInitialTitle);
738 }
739 }
740
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800741 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800742 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700743 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800744 }
745
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700746 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800747 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800749 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700750 if (mInitialTitleResId > 0) {
751 setTitle(mInitialTitleResId);
752 } else {
753 setTitle(mInitialTitle);
754 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700755 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800756 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700757
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800758 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
759 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700760
761 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800762 }
763
764 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
765 final CharSequence title;
766 final int titleRes = bse.getBreadCrumbTitleRes();
767 if (titleRes > 0) {
768 title = getText(titleRes);
769 } else {
770 title = bse.getBreadCrumbTitle();
771 }
772 if (title != null) {
773 setTitle(title);
774 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800775 }
776
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800777 @Override
778 protected void onSaveInstanceState(Bundle outState) {
779 super.onSaveInstanceState(outState);
780
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700781 if (mCategories.size() > 0) {
782 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800783 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700784
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700785 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700786 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700787
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700788 if (mDisplaySearch) {
789 // The option menus are created if the ActionBar is visible and they are also created
790 // asynchronously. If you launch Settings with an Intent action like
791 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
792 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
793 // menu item and search view are null.
794 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
795 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700796
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700797 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
798 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
799 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700800
801 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800802 }
803
804 @Override
805 public void onResume() {
806 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400807 if (mIsShowingDashboard) {
808 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
809 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800810
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700811 final int newHomeActivityCount = getHomeActivitiesCount();
812 if (newHomeActivityCount != mHomeActivitiesCount) {
813 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700814 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700815 }
816
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800817 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
818 @Override
819 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700820 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800821 }
822 };
823 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
824 mDevelopmentPreferencesListener);
825
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800826 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700827
Svetoslav990159a2014-04-14 17:14:59 -0700828 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700829
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700830 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700831 onQueryTextSubmit(mSearchQuery);
832 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800833 }
834
835 @Override
836 public void onPause() {
837 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400838 if (mIsShowingDashboard) {
839 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
840 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800841 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700842 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800843 }
844
845 @Override
846 public void onDestroy() {
847 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700848
849 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
850 mDevelopmentPreferencesListener);
851 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800852 }
853
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 protected boolean isValidFragment(String fragmentName) {
855 // Almost all fragments are wrapped in this,
856 // except for a few that have their own activities.
857 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
858 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
859 }
860 return false;
861 }
862
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800863 @Override
864 public Intent getIntent() {
865 Intent superIntent = super.getIntent();
866 String startingFragment = getStartingFragmentClass(superIntent);
867 // This is called from super.onCreate, isMultiPane() is not yet reliable
868 // Do not use onIsHidingHeaders either, which relies itself on this method
869 if (startingFragment != null) {
870 Intent modIntent = new Intent(superIntent);
871 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
872 Bundle args = superIntent.getExtras();
873 if (args != null) {
874 args = new Bundle(args);
875 } else {
876 args = new Bundle();
877 }
878 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100879 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800880 return modIntent;
881 }
882 return superIntent;
883 }
884
885 /**
886 * Checks if the component name in the intent is different from the Settings class and
887 * returns the class name to load as a fragment.
888 */
889 private String getStartingFragmentClass(Intent intent) {
890 if (mFragmentClass != null) return mFragmentClass;
891
892 String intentClass = intent.getComponent().getClassName();
893 if (intentClass.equals(getClass().getName())) return null;
894
895 if ("com.android.settings.ManageApplications".equals(intentClass)
896 || "com.android.settings.RunningServices".equals(intentClass)
897 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
898 // Old names of manage apps.
899 intentClass = com.android.settings.applications.ManageApplications.class.getName();
900 }
901
902 return intentClass;
903 }
904
905 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000906 * Start a new fragment containing a preference panel. If the preferences
907 * are being displayed in multi-pane mode, the given fragment class will
908 * be instantiated and placed in the appropriate pane. If running in
909 * single-pane mode, a new activity will be launched in which to show the
910 * fragment.
911 *
912 * @param fragmentClass Full name of the class implementing the fragment.
913 * @param args Any desired arguments to supply to the fragment.
914 * @param titleRes Optional resource identifier of the title of this
915 * fragment.
916 * @param titleText Optional text of the title of this fragment.
917 * @param resultTo Optional fragment that result data should be sent to.
918 * If non-null, resultTo.onActivityResult() will be called when this
919 * preference panel is done. The launched panel must use
920 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
921 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700922 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000923 */
924 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700925 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700926 String title = null;
927 if (titleRes < 0) {
928 if (titleText != null) {
929 title = titleText.toString();
930 } else {
931 // There not much we can do in that case
932 title = "";
933 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700934 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700935 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700936 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000937 }
938
939 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100940 * Start a new fragment in a new activity containing a preference panel for a given user. If the
941 * preferences are being displayed in multi-pane mode, the given fragment class will be
942 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
943 * activity will be launched in which to show the fragment.
944 *
945 * @param fragmentClass Full name of the class implementing the fragment.
946 * @param args Any desired arguments to supply to the fragment.
947 * @param titleRes Optional resource identifier of the title of this fragment.
948 * @param titleText Optional text of the title of this fragment.
949 * @param userHandle The user for which the panel has to be started.
950 */
951 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
952 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700953 // This is a workaround.
954 //
955 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
956 // starting the fragment could cause a native stack corruption. See b/17523189. However,
957 // adding that flag and start the preference panel with the same UserHandler will make it
958 // impossible to use back button to return to the previous screen. See b/20042570.
959 //
960 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
961 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
962 // when we're calling it as the same user.
963 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
964 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
965 } else {
966 String title = null;
967 if (titleRes < 0) {
968 if (titleText != null) {
969 title = titleText.toString();
970 } else {
971 // There not much we can do in that case
972 title = "";
973 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100974 }
Lifu Tangd0332852015-04-02 12:05:46 -0700975 Utils.startWithFragmentAsUser(this, fragmentClass, args,
976 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100977 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100978 }
979
980 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800981 * Called by a preference panel fragment to finish itself.
982 *
983 * @param caller The fragment that is asking to be finished.
984 * @param resultCode Optional result code to send back to the original
985 * launching fragment.
986 * @param resultData Optional result data to send back to the original
987 * launching fragment.
988 */
989 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
990 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700991 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800992 }
993
994 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000995 * Start a new fragment.
996 *
997 * @param fragment The fragment to start
998 * @param push If true, the current fragment will be pushed onto the back stack. If false,
999 * the current fragment will be replaced.
1000 */
1001 public void startPreferenceFragment(Fragment fragment, boolean push) {
1002 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001003 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001004 if (push) {
1005 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1006 transaction.addToBackStack(BACK_STACK_PREFS);
1007 } else {
1008 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1009 }
1010 transaction.commitAllowingStateLoss();
1011 }
1012
1013 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001014 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1015 */
1016 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001017 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001018 if (validate && !isValidFragment(fragmentName)) {
1019 throw new IllegalArgumentException("Invalid fragment for this activity: "
1020 + fragmentName);
1021 }
1022 Fragment f = Fragment.instantiate(this, fragmentName, args);
1023 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001024 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001025 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001026 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001027 }
1028 if (addToBackStack) {
1029 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1030 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001031 if (titleResId > 0) {
1032 transaction.setBreadCrumbTitle(titleResId);
1033 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001034 transaction.setBreadCrumbTitle(title);
1035 }
1036 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001037 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001038 return f;
1039 }
1040
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001041 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001042 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001043 *
1044 * @param categories The list in which to place the tiles categories.
1045 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001046 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001047 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001048 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1049 updateTilesList(categories);
1050 }
1051
1052 /**
1053 * Parse the given XML file as a categories description, adding each
1054 * parsed categories and tiles into the target list.
1055 *
1056 * @param resid The XML resource to load and parse.
1057 * @param target The list in which the parsed categories and tiles should be placed.
1058 */
1059 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1060 XmlResourceParser parser = null;
1061 try {
1062 parser = getResources().getXml(resid);
1063 AttributeSet attrs = Xml.asAttributeSet(parser);
1064
1065 int type;
1066 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1067 && type != XmlPullParser.START_TAG) {
1068 // Parse next until start tag is found
1069 }
1070
1071 String nodeName = parser.getName();
1072 if (!"dashboard-categories".equals(nodeName)) {
1073 throw new RuntimeException(
1074 "XML document must start with <preference-categories> tag; found"
1075 + nodeName + " at " + parser.getPositionDescription());
1076 }
1077
1078 Bundle curBundle = null;
1079
1080 final int outerDepth = parser.getDepth();
1081 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1082 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1083 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1084 continue;
1085 }
1086
1087 nodeName = parser.getName();
1088 if ("dashboard-category".equals(nodeName)) {
1089 DashboardCategory category = new DashboardCategory();
1090
1091 TypedArray sa = obtainStyledAttributes(
1092 attrs, com.android.internal.R.styleable.PreferenceHeader);
1093 category.id = sa.getResourceId(
1094 com.android.internal.R.styleable.PreferenceHeader_id,
1095 (int)DashboardCategory.CAT_ID_UNDEFINED);
1096
1097 TypedValue tv = sa.peekValue(
1098 com.android.internal.R.styleable.PreferenceHeader_title);
1099 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1100 if (tv.resourceId != 0) {
1101 category.titleRes = tv.resourceId;
1102 } else {
1103 category.title = tv.string;
1104 }
1105 }
1106 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001107 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1108 tv = sa.peekValue(
1109 com.android.internal.R.styleable.Preference_key);
1110 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1111 if (tv.resourceId != 0) {
1112 category.key = getString(tv.resourceId);
1113 } else {
1114 category.key = tv.string.toString();
1115 }
1116 }
1117 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001118
1119 final int innerDepth = parser.getDepth();
1120 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1121 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1122 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1123 continue;
1124 }
1125
1126 String innerNodeName = parser.getName();
1127 if (innerNodeName.equals("dashboard-tile")) {
1128 DashboardTile tile = new DashboardTile();
1129
1130 sa = obtainStyledAttributes(
1131 attrs, com.android.internal.R.styleable.PreferenceHeader);
1132 tile.id = sa.getResourceId(
1133 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001134 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001135 tv = sa.peekValue(
1136 com.android.internal.R.styleable.PreferenceHeader_title);
1137 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1138 if (tv.resourceId != 0) {
1139 tile.titleRes = tv.resourceId;
1140 } else {
1141 tile.title = tv.string;
1142 }
1143 }
1144 tv = sa.peekValue(
1145 com.android.internal.R.styleable.PreferenceHeader_summary);
1146 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1147 if (tv.resourceId != 0) {
1148 tile.summaryRes = tv.resourceId;
1149 } else {
1150 tile.summary = tv.string;
1151 }
1152 }
1153 tile.iconRes = sa.getResourceId(
1154 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1155 tile.fragment = sa.getString(
1156 com.android.internal.R.styleable.PreferenceHeader_fragment);
1157 sa.recycle();
1158
1159 if (curBundle == null) {
1160 curBundle = new Bundle();
1161 }
1162
1163 final int innerDepth2 = parser.getDepth();
1164 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1165 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1166 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1167 continue;
1168 }
1169
1170 String innerNodeName2 = parser.getName();
1171 if (innerNodeName2.equals("extra")) {
1172 getResources().parseBundleExtra("extra", attrs, curBundle);
1173 XmlUtils.skipCurrentTag(parser);
1174
1175 } else if (innerNodeName2.equals("intent")) {
1176 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1177
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.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001189 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
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
1251 final INetworkManagementService netManager = INetworkManagementService.Stub
1252 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1253 try {
1254 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001255 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001256 }
1257 } catch (RemoteException e) {
1258 // ignored
1259 }
1260 } else if (id == R.id.battery_settings) {
1261 // Remove battery settings when battery is not available. (e.g. TV)
1262
1263 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001264 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 }
1266 } else if (id == R.id.home_settings) {
1267 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001268 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001269 }
1270 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001271 boolean hasMultipleUsers =
1272 ((UserManager) getSystemService(Context.USER_SERVICE))
1273 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001274 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001275 || (!UserManager.supportsMultipleUsers()
1276 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001277 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001278 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001279 }
1280 } else if (id == R.id.nfc_payment_settings) {
1281 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001282 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001283 } else {
1284 // Only show if NFC is on and we have the HCE feature
1285 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001286 if (adapter == null || !adapter.isEnabled() ||
1287 !getPackageManager().hasSystemFeature(
1288 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001289 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001290 }
1291 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001292 } else if (id == R.id.print_settings) {
1293 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1294 PackageManager.FEATURE_PRINTING);
1295 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001296 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001297 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001298 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001299 if (!showDev || um.hasUserRestriction(
1300 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001301 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001302 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001303 }
1304
1305 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001306 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1307 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001308 }
1309
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001310 if (removeTile && n < category.getTilesCount()) {
1311 category.removeTile(n);
1312 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001313 n--;
1314 }
1315 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001316 addExternalTiles(target);
1317 }
1318
1319 private void addExternalTiles(List<DashboardCategory> target) {
1320 Map<Pair<String, String>, DashboardTile> addedCache =
1321 new ArrayMap<Pair<String, String>, DashboardTile>();
1322 UserManager userManager = UserManager.get(this);
1323 for (UserHandle user : userManager.getUserProfiles()) {
1324 addExternalTiles(target, user, addedCache);
1325 }
1326 }
1327
1328 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1329 Map<Pair<String, String>, DashboardTile> addedCache) {
1330 PackageManager pm = getPackageManager();
1331 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1332 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1333 PackageManager.GET_META_DATA, user.getIdentifier());
1334 for (ResolveInfo resolved : results) {
1335 if (!resolved.system) {
1336 // Do not allow any app to add to settings, only system ones.
1337 continue;
1338 }
1339 ActivityInfo activityInfo = resolved.activityInfo;
1340 Bundle metaData = activityInfo.metaData;
1341 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1342 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1343 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1344 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1345 continue;
1346 }
1347 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1348 DashboardCategory category = getCategory(target, categoryKey);
1349 if (category == null) {
1350 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1351 + "category key " + categoryKey);
1352 continue;
1353 }
1354 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1355 activityInfo.name);
1356 DashboardTile tile = addedCache.get(key);
1357 if (tile == null) {
1358 tile = new DashboardTile();
1359 tile.intent = new Intent().setClassName(
1360 activityInfo.packageName, activityInfo.name);
1361 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1362
1363 if (category.externalIndex == -1) {
1364 // If no location for external tiles has been specified for this category,
1365 // then just put them at the end.
1366 category.addTile(tile);
1367 } else {
1368 category.addTile(category.externalIndex, tile);
1369 }
1370 addedCache.put(key, tile);
1371 }
1372 tile.userHandle.add(user);
1373 }
1374 }
1375
1376 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1377 for (DashboardCategory category : target) {
1378 if (categoryKey.equals(category.key)) {
1379 return category;
1380 }
1381 }
1382 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001383 }
1384
1385 private boolean updateHomeSettingTiles(DashboardTile tile) {
1386 // Once we decide to show Home settings, keep showing it forever
1387 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1388 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1389 return true;
1390 }
1391
1392 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001393 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001394 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001395 // When there's only one available home app, omit this settings
1396 // category entirely at the top level UI. If the user just
1397 // uninstalled the penultimate home app candidiate, we also
1398 // now tell them about why they aren't seeing 'Home' in the list.
1399 if (sShowNoHomeNotice) {
1400 sShowNoHomeNotice = false;
1401 NoHomeDialogFragment.show(this);
1402 }
1403 return false;
1404 } else {
1405 // Okay, we're allowing the Home settings category. Tell it, when
1406 // invoked via this front door, that we'll need to be told about the
1407 // case when the user uninstalls all but one home app.
1408 if (tile.fragmentArguments == null) {
1409 tile.fragmentArguments = new Bundle();
1410 }
1411 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1412 }
1413 } catch (Exception e) {
1414 // Can't look up the home activity; bail on configuring the icon
1415 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1416 }
1417
1418 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1419 return true;
1420 }
1421
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001422 private void getMetaData() {
1423 try {
1424 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1425 PackageManager.GET_META_DATA);
1426 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001427 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1428 } catch (NameNotFoundException nnfe) {
1429 // No recovery
1430 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1431 }
1432 }
1433
1434 // give subclasses access to the Next button
1435 public boolean hasNextButton() {
1436 return mNextButton != null;
1437 }
1438
1439 public Button getNextButton() {
1440 return mNextButton;
1441 }
1442
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001443 @Override
1444 public boolean shouldUpRecreateTask(Intent targetIntent) {
1445 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1446 }
1447
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001448 public static void requestHomeNotice() {
1449 sShowNoHomeNotice = true;
1450 }
1451
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001452 @Override
1453 public boolean onQueryTextSubmit(String query) {
1454 switchToSearchResultsFragmentIfNeeded();
1455 mSearchQuery = query;
1456 return mSearchResultsFragment.onQueryTextSubmit(query);
1457 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001458
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001459 @Override
1460 public boolean onQueryTextChange(String newText) {
1461 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001462 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001463 return false;
1464 }
1465 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001466 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001467
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001468 @Override
1469 public boolean onClose() {
1470 return false;
1471 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001472
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001473 @Override
1474 public boolean onMenuItemActionExpand(MenuItem item) {
1475 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001476 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001477 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001478 return true;
1479 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001481 @Override
1482 public boolean onMenuItemActionCollapse(MenuItem item) {
1483 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001484 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001485 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001486 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001487 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001488 return true;
1489 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001490
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001491 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 if (mSearchResultsFragment != null) {
1493 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001494 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001495 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001496 if (current != null && current instanceof SearchResultsSummary) {
1497 mSearchResultsFragment = (SearchResultsSummary) current;
1498 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001499 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001500 SearchResultsSummary.class.getName(), null, false, true,
1501 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001502 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001503 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001504 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001505 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001506
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001507 public void needToRevertToInitialFragment() {
1508 mNeedToRevertToInitialFragment = true;
1509 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001510
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001511 private void revertToInitialFragment() {
1512 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001513 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001514 mSearchMenuItemExpanded = false;
1515 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1516 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001517 if (mSearchMenuItem != null) {
1518 mSearchMenuItem.collapseActionView();
1519 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001520 }
Jim Miller0698a212014-10-16 19:49:07 -07001521
1522 public Intent getResultIntentData() {
1523 return mResultIntentData;
1524 }
1525
1526 public void setResultIntentData(Intent resultIntentData) {
1527 mResultIntentData = resultIntentData;
1528 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001529}