blob: 25adef1ba25534ffefc862535fd4ed9db5b9fc9a [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason Monk2ebc8a02015-02-13 15:23:19 -050019import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
20
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.ActionBar;
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.nfc.NfcAdapter;
40import android.os.Bundle;
41import android.os.Handler;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080043import android.os.UserHandle;
44import android.os.UserManager;
45import android.preference.Preference;
46import android.preference.PreferenceFragment;
47import android.preference.PreferenceManager;
48import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070050import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050051import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.AttributeSet;
53import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.TypedValue;
56import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070057import android.view.Menu;
58import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080059import android.view.MenuItem;
60import android.view.View;
61import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070062import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080063import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070064import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040065
Chris Wren8a963ba2015-03-20 10:29:14 -040066import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080067import com.android.internal.util.ArrayUtils;
68import com.android.internal.util.XmlUtils;
69import com.android.settings.accessibility.AccessibilitySettings;
70import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000071import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070073import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.applications.ManageApplications;
75import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040076import com.android.settings.applications.UsageAccessDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070078import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070080import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070081import com.android.settings.dashboard.NoHomeDialogFragment;
82import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070083import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070084import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070085import com.android.settings.deviceinfo.PublicVolumeSettings;
86import com.android.settings.deviceinfo.StorageSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040087import com.android.settings.fuelgauge.BatterySaverSettings;
Jason Monk1eb54eb2015-04-29 12:46:42 -040088import com.android.settings.fuelgauge.PowerUsageDetail;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.fuelgauge.PowerUsageSummary;
90import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
91import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
92import com.android.settings.inputmethod.SpellCheckersSettings;
93import com.android.settings.inputmethod.UserDictionaryList;
94import com.android.settings.location.LocationSettings;
95import com.android.settings.nfc.AndroidBeam;
96import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040097import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500101import com.android.settings.notification.OtherSoundSettings;
John Spurlock08531a82015-05-07 17:45:43 -0400102import com.android.settings.notification.ZenAccessSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400103import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400104import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400105import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400106import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400107import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.print.PrintJobSettingsFragment;
109import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500110import com.android.settings.search.DynamicIndexableContentMonitor;
111import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700112import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.tts.TextToSpeechSettings;
114import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700115import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import com.android.settings.vpn2.VpnSettings;
117import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700118import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700120import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import com.android.settings.wifi.WifiSettings;
122import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500123
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124import org.xmlpull.v1.XmlPullParser;
125import org.xmlpull.v1.XmlPullParserException;
126
127import java.io.IOException;
128import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800129import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500130import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700131import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133public class SettingsActivity extends Activity
134 implements PreferenceManager.OnPreferenceTreeClickListener,
135 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100136 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100138 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800139
140 private static final String LOG_TAG = "Settings";
141
142 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700143 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700144 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
145 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700146 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700147 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700148 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149
150 /**
151 * When starting this activity, the invoking Intent can contain this extra
152 * string to specify which fragment should be initially displayed.
153 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
154 * will call isValidFragment() to confirm that the fragment class name is valid for this
155 * activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
158
159 /**
160 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
161 * this extra can also be specified to supply a Bundle of arguments to pass
162 * to that fragment when it is instantiated during the initial creation
163 * of the activity.
164 */
165 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
166
167 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700168 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
169 */
170 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
171
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800172 public static final String BACK_STACK_PREFS = ":settings:prefs";
173
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174 // extras that allow any preference activity to be launched as part of a wizard
175
176 // show Back and Next buttons? takes boolean parameter
177 // Back will then return RESULT_CANCELED and Next RESULT_OK
178 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
179
180 // add a Skip button?
181 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
182
183 // specify custom text for the Back or Next buttons, or cause a button to not appear
184 // at all by setting it to null
185 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
186 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
187
188 /**
189 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700190 * 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 -0800191 * that fragment.
192 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700193 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100194 /**
195 * The package name used to resolve the title resource id.
196 */
197 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
198 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700199 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
200 ":settings:show_fragment_title_resid";
201 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
202 ":settings:show_fragment_as_shortcut";
203
204 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
205 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207 private static final String META_DATA_KEY_FRAGMENT_CLASS =
208 "com.android.settings.FRAGMENT_CLASS";
209
210 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
211
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700212 private static final String EMPTY_QUERY = "";
213
Jason Monk2ebc8a02015-02-13 15:23:19 -0500214 /**
215 * Settings will search for system activities of this action and add them as a top level
216 * settings tile using the following parameters.
217 *
218 * <p>A category must be specified in the meta-data for the activity named
219 * {@link #EXTRA_CATEGORY_KEY}
220 *
221 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
222 * otherwise the label for the activity will be used.
223 *
224 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
225 * otherwise the icon for the activity will be used.
226 *
227 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
228 */
229 private static final String EXTRA_SETTINGS_ACTION =
230 "com.android.settings.action.EXTRA_SETTINGS";
231
232 /**
233 * The key used to get the category from metadata of activities of action
234 * {@link #EXTRA_SETTINGS_ACTION}
235 * The value must be one of:
236 * <li>com.android.settings.category.wireless</li>
237 * <li>com.android.settings.category.device</li>
238 * <li>com.android.settings.category.personal</li>
239 * <li>com.android.settings.category.system</li>
240 */
241 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
242
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243 private static boolean sShowNoHomeNotice = false;
244
245 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800247 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700248 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800249
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 // Show only these settings for restricted users
251 private int[] SETTINGS_FOR_RESTRICTED = {
252 R.id.wireless_section,
253 R.id.wifi_settings,
254 R.id.bluetooth_settings,
255 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700256 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 R.id.wireless_settings,
258 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400259 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 R.id.display_settings,
261 R.id.storage_settings,
262 R.id.application_settings,
263 R.id.battery_settings,
264 R.id.personal_section,
265 R.id.location_settings,
266 R.id.security_settings,
267 R.id.language_settings,
268 R.id.user_settings,
269 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270 R.id.system_section,
271 R.id.date_time_settings,
272 R.id.about_settings,
273 R.id.accessibility_settings,
274 R.id.print_settings,
275 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800276 R.id.home_settings,
277 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 };
279
280 private static final String[] ENTRY_FRAGMENTS = {
281 WirelessSettings.class.getName(),
282 WifiSettings.class.getName(),
283 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700284 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700286 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 TetherSettings.class.getName(),
288 WifiP2pSettings.class.getName(),
289 VpnSettings.class.getName(),
290 DateTimeSettings.class.getName(),
291 LocalePicker.class.getName(),
292 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700293 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 SpellCheckersSettings.class.getName(),
295 UserDictionaryList.class.getName(),
296 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700297 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 DisplaySettings.class.getName(),
299 DeviceInfoSettings.class.getName(),
300 ManageApplications.class.getName(),
301 ProcessStatsUi.class.getName(),
302 NotificationStation.class.getName(),
303 LocationSettings.class.getName(),
304 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400305 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 PrivacySettings.class.getName(),
307 DeviceAdminSettings.class.getName(),
308 AccessibilitySettings.class.getName(),
309 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
311 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700312 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700313 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700314 PrivateVolumeSettings.class.getName(),
315 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800317 AndroidBeam.class.getName(),
318 WifiDisplaySettings.class.getName(),
319 PowerUsageSummary.class.getName(),
320 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000321 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 CryptKeeperSettings.class.getName(),
323 DataUsageSummary.class.getName(),
324 DreamSettings.class.getName(),
325 UserSettings.class.getName(),
326 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400327 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800328 PrintSettingsFragment.class.getName(),
329 PrintJobSettingsFragment.class.getName(),
330 TrustedCredentialsSettings.class.getName(),
331 PaymentSettings.class.getName(),
332 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700333 ZenModeSettings.class.getName(),
334 NotificationSettings.class.getName(),
335 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
336 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400337 InstalledAppDetails.class.getName(),
338 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700339 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700340 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000341 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400342 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400343 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400344 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400345 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400346 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400347 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400348 PowerUsageDetail.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 };
350
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700351
352 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
353 "android.settings.APPLICATION_DETAILS_SETTINGS"
354 };
355
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 private SharedPreferences mDevelopmentPreferences;
357 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
358
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 private boolean mBatteryPresent = true;
360 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 @Override
362 public void onReceive(Context context, Intent intent) {
363 String action = intent.getAction();
364 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
365 boolean batteryPresent = Utils.isBatteryPresent(intent);
366
367 if (mBatteryPresent != batteryPresent) {
368 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700369 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 }
371 }
372 }
373 };
374
Svetoslav990159a2014-04-14 17:14:59 -0700375 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
376 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700377
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700378 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700379 private SwitchBar mSwitchBar;
380
381 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700382
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700383 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700384 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700385
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700386 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700387 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700388
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700389 private ViewGroup mContent;
390
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700391 private SearchView mSearchView;
392 private MenuItem mSearchMenuItem;
393 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700394 private SearchResultsSummary mSearchResultsFragment;
395 private String mSearchQuery;
396
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700397 // Categories
398 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800399
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700400 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700401 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402 private Handler mHandler = new Handler() {
403 @Override
404 public void handleMessage(Message msg) {
405 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700406 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700407 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
408 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700409 buildDashboardCategories(mCategories);
410 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 } break;
412 }
413 }
414 };
415
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700416 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700417 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700418
Jim Miller0698a212014-10-16 19:49:07 -0700419 private Intent mResultIntentData;
420
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700421 public SwitchBar getSwitchBar() {
422 return mSwitchBar;
423 }
424
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700425 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
426 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700427 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700428 }
429 return mCategories;
430 }
431
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800432 @Override
433 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
434 // Override the fragment title for Wallpaper settings
435 int titleRes = pref.getTitleRes();
436 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
437 titleRes = R.string.wallpaper_settings_fragment_title;
438 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
439 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
440 if (UserManager.get(this).isLinkedUser()) {
441 titleRes = R.string.profile_info_settings_title;
442 } else {
443 titleRes = R.string.user_info_settings_title;
444 }
445 }
446 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
447 null, 0);
448 return true;
449 }
450
451 @Override
452 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
453 return false;
454 }
455
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700456 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700457 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700458 Message msg = new Message();
459 msg.what = MSG_BUILD_CATEGORIES;
460 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800461 }
462 }
463
464 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465 public void onConfigurationChanged(Configuration newConfig) {
466 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800467 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800468 }
469
470 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700471 protected void onStart() {
472 super.onStart();
473
474 if (mNeedToRevertToInitialFragment) {
475 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800477 }
478
479 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700480 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700481 if (!mDisplaySearch) {
482 return false;
483 }
484
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700485 MenuInflater inflater = getMenuInflater();
486 inflater.inflate(R.menu.options_menu, menu);
487
488 // Cache the search query (can be overriden by the OnQueryTextListener)
489 final String query = mSearchQuery;
490
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700491 mSearchMenuItem = menu.findItem(R.id.search);
492 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700493
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700494 if (mSearchMenuItem == null || mSearchView == null) {
495 return false;
496 }
497
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700498 if (mSearchResultsFragment != null) {
499 mSearchResultsFragment.setSearchView(mSearchView);
500 }
501
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700502 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700503 mSearchView.setOnQueryTextListener(this);
504 mSearchView.setOnCloseListener(this);
505
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700506 if (mSearchMenuItemExpanded) {
507 mSearchMenuItem.expandActionView();
508 }
509 mSearchView.setQuery(query, true /* submit */);
510
511 return true;
512 }
513
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700514 private static boolean isShortCutIntent(final Intent intent) {
515 Set<String> categories = intent.getCategories();
516 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
517 }
518
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700519 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700520 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700521 if (action == null) {
522 return false;
523 }
524 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
525 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
526 }
527 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700528 }
529
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700530 @Override
531 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700532 super.onCreate(savedState);
533
534 // Should happen before any call to getIntent()
535 getMetaData();
536
537 final Intent intent = getIntent();
538 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
539 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
543 Context.MODE_PRIVATE);
544
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700545 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700546 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700547
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700548 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700549 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
550
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700551 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700552 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700553
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700554 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700555
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700556 // This is a "Sub Settings" when:
557 // - this is a real SubSettings
558 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700559 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700560 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
561
562 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
563 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700564 // Check also that we are not a Theme Dialog as we don't want to override them
565 final int themeResId = getThemeResId();
566 if (themeResId != R.style.Theme_DialogWhenLarge &&
567 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
568 setTheme(R.style.Theme_SubSettings);
569 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700570 }
571
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700572 setContentView(mIsShowingDashboard ?
573 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700575 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700576
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 getFragmentManager().addOnBackStackChangedListener(this);
578
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700579 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800580 // Run the Index update only if we have some space
581 if (!Utils.isLowStorage(this)) {
582 Index.getInstance(getApplicationContext()).update();
583 } else {
584 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
585 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700586 }
587
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700588 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700589 // We are restarting from a previous saved state; used that to initialize, instead
590 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700591 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
592 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800593
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700594 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800595
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700596 ArrayList<DashboardCategory> categories =
597 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
598 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700599 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700600 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700601 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800602 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700603
604 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700605 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700606 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
607 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800608 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700609 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700610 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
611 // only if it is a sub settings
612 if (mIsShortcut) {
613 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700614 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700615 } else if (isSubSettings) {
616 mDisplayHomeAsUpEnabled = true;
617 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700618 } else {
619 mDisplayHomeAsUpEnabled = false;
620 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700621 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700622 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700623
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700624 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700625 switchToFragment(initialFragmentName, initialArguments, true, false,
626 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000627 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700628 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700629 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700630 // Show Search affordance
631 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700632 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700633 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700634 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800635 }
636 }
637
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700638 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700639 if (mActionBar != null) {
640 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
641 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
642 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700643 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
644
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
647
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700648 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 if (buttonBar != null) {
650 buttonBar.setVisibility(View.VISIBLE);
651
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700652 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 backButton.setOnClickListener(new OnClickListener() {
654 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700655 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 finish();
657 }
658 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700659 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 skipButton.setOnClickListener(new OnClickListener() {
661 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700662 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800663 finish();
664 }
665 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700666 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800667 mNextButton.setOnClickListener(new OnClickListener() {
668 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700669 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800670 finish();
671 }
672 });
673
674 // set our various button parameters
675 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
676 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
677 if (TextUtils.isEmpty(buttonText)) {
678 mNextButton.setVisibility(View.GONE);
679 }
680 else {
681 mNextButton.setText(buttonText);
682 }
683 }
684 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
685 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
686 if (TextUtils.isEmpty(buttonText)) {
687 backButton.setVisibility(View.GONE);
688 }
689 else {
690 backButton.setText(buttonText);
691 }
692 }
693 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
694 skipButton.setVisibility(View.VISIBLE);
695 }
696 }
697 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700698
699 mHomeActivitiesCount = getHomeActivitiesCount();
700 }
701
702 private int getHomeActivitiesCount() {
703 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
704 getPackageManager().getHomeActivities(homeApps);
705 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800706 }
707
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700708 private void setTitleFromIntent(Intent intent) {
709 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
710 if (initialTitleResId > 0) {
711 mInitialTitle = null;
712 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100713
714 final String initialTitleResPackageName = intent.getStringExtra(
715 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
716 if (initialTitleResPackageName != null) {
717 try {
718 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
719 0 /* flags */, new UserHandle(UserHandle.myUserId()));
720 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
721 setTitle(mInitialTitle);
722 mInitialTitleResId = -1;
723 return;
724 } catch (NameNotFoundException e) {
725 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
726 }
727 } else {
728 setTitle(mInitialTitleResId);
729 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700730 } else {
731 mInitialTitleResId = -1;
732 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
733 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
734 setTitle(mInitialTitle);
735 }
736 }
737
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800738 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800739 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700740 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800741 }
742
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700743 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800744 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700745
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800746 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700747 if (mInitialTitleResId > 0) {
748 setTitle(mInitialTitleResId);
749 } else {
750 setTitle(mInitialTitle);
751 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700752 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800753 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700754
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800755 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
756 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700757
758 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800759 }
760
761 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
762 final CharSequence title;
763 final int titleRes = bse.getBreadCrumbTitleRes();
764 if (titleRes > 0) {
765 title = getText(titleRes);
766 } else {
767 title = bse.getBreadCrumbTitle();
768 }
769 if (title != null) {
770 setTitle(title);
771 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800772 }
773
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 @Override
775 protected void onSaveInstanceState(Bundle outState) {
776 super.onSaveInstanceState(outState);
777
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700778 if (mCategories.size() > 0) {
779 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800780 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700781
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700782 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700783 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700784
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700785 if (mDisplaySearch) {
786 // The option menus are created if the ActionBar is visible and they are also created
787 // asynchronously. If you launch Settings with an Intent action like
788 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
789 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
790 // menu item and search view are null.
791 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
792 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700793
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700794 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
795 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
796 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700797
798 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800799 }
800
801 @Override
802 public void onResume() {
803 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400804 if (mIsShowingDashboard) {
805 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
806 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800807
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700808 final int newHomeActivityCount = getHomeActivitiesCount();
809 if (newHomeActivityCount != mHomeActivitiesCount) {
810 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700811 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700812 }
813
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800814 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
815 @Override
816 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700817 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800818 }
819 };
820 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
821 mDevelopmentPreferencesListener);
822
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800823 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700824
Svetoslav990159a2014-04-14 17:14:59 -0700825 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700826
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700827 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700828 onQueryTextSubmit(mSearchQuery);
829 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800830 }
831
832 @Override
833 public void onPause() {
834 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400835 if (mIsShowingDashboard) {
836 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
837 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800838 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700839 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800840 }
841
842 @Override
843 public void onDestroy() {
844 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700845
846 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
847 mDevelopmentPreferencesListener);
848 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800849 }
850
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800851 protected boolean isValidFragment(String fragmentName) {
852 // Almost all fragments are wrapped in this,
853 // except for a few that have their own activities.
854 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
855 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
856 }
857 return false;
858 }
859
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800860 @Override
861 public Intent getIntent() {
862 Intent superIntent = super.getIntent();
863 String startingFragment = getStartingFragmentClass(superIntent);
864 // This is called from super.onCreate, isMultiPane() is not yet reliable
865 // Do not use onIsHidingHeaders either, which relies itself on this method
866 if (startingFragment != null) {
867 Intent modIntent = new Intent(superIntent);
868 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
869 Bundle args = superIntent.getExtras();
870 if (args != null) {
871 args = new Bundle(args);
872 } else {
873 args = new Bundle();
874 }
875 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100876 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800877 return modIntent;
878 }
879 return superIntent;
880 }
881
882 /**
883 * Checks if the component name in the intent is different from the Settings class and
884 * returns the class name to load as a fragment.
885 */
886 private String getStartingFragmentClass(Intent intent) {
887 if (mFragmentClass != null) return mFragmentClass;
888
889 String intentClass = intent.getComponent().getClassName();
890 if (intentClass.equals(getClass().getName())) return null;
891
892 if ("com.android.settings.ManageApplications".equals(intentClass)
893 || "com.android.settings.RunningServices".equals(intentClass)
894 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
895 // Old names of manage apps.
896 intentClass = com.android.settings.applications.ManageApplications.class.getName();
897 }
898
899 return intentClass;
900 }
901
902 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000903 * Start a new fragment containing a preference panel. If the preferences
904 * are being displayed in multi-pane mode, the given fragment class will
905 * be instantiated and placed in the appropriate pane. If running in
906 * single-pane mode, a new activity will be launched in which to show the
907 * fragment.
908 *
909 * @param fragmentClass Full name of the class implementing the fragment.
910 * @param args Any desired arguments to supply to the fragment.
911 * @param titleRes Optional resource identifier of the title of this
912 * fragment.
913 * @param titleText Optional text of the title of this fragment.
914 * @param resultTo Optional fragment that result data should be sent to.
915 * If non-null, resultTo.onActivityResult() will be called when this
916 * preference panel is done. The launched panel must use
917 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
918 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700919 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000920 */
921 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700922 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700923 String title = null;
924 if (titleRes < 0) {
925 if (titleText != null) {
926 title = titleText.toString();
927 } else {
928 // There not much we can do in that case
929 title = "";
930 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700931 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700932 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700933 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000934 }
935
936 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100937 * Start a new fragment in a new activity containing a preference panel for a given user. If the
938 * preferences are being displayed in multi-pane mode, the given fragment class will be
939 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
940 * activity will be launched in which to show the fragment.
941 *
942 * @param fragmentClass Full name of the class implementing the fragment.
943 * @param args Any desired arguments to supply to the fragment.
944 * @param titleRes Optional resource identifier of the title of this fragment.
945 * @param titleText Optional text of the title of this fragment.
946 * @param userHandle The user for which the panel has to be started.
947 */
948 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
949 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700950 // This is a workaround.
951 //
952 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
953 // starting the fragment could cause a native stack corruption. See b/17523189. However,
954 // adding that flag and start the preference panel with the same UserHandler will make it
955 // impossible to use back button to return to the previous screen. See b/20042570.
956 //
957 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
958 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
959 // when we're calling it as the same user.
960 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
961 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
962 } else {
963 String title = null;
964 if (titleRes < 0) {
965 if (titleText != null) {
966 title = titleText.toString();
967 } else {
968 // There not much we can do in that case
969 title = "";
970 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100971 }
Lifu Tangd0332852015-04-02 12:05:46 -0700972 Utils.startWithFragmentAsUser(this, fragmentClass, args,
973 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100974 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100975 }
976
977 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800978 * Called by a preference panel fragment to finish itself.
979 *
980 * @param caller The fragment that is asking to be finished.
981 * @param resultCode Optional result code to send back to the original
982 * launching fragment.
983 * @param resultData Optional result data to send back to the original
984 * launching fragment.
985 */
986 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
987 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700988 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800989 }
990
991 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000992 * Start a new fragment.
993 *
994 * @param fragment The fragment to start
995 * @param push If true, the current fragment will be pushed onto the back stack. If false,
996 * the current fragment will be replaced.
997 */
998 public void startPreferenceFragment(Fragment fragment, boolean push) {
999 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001000 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001001 if (push) {
1002 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1003 transaction.addToBackStack(BACK_STACK_PREFS);
1004 } else {
1005 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1006 }
1007 transaction.commitAllowingStateLoss();
1008 }
1009
1010 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001011 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1012 */
1013 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001014 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001015 if (validate && !isValidFragment(fragmentName)) {
1016 throw new IllegalArgumentException("Invalid fragment for this activity: "
1017 + fragmentName);
1018 }
1019 Fragment f = Fragment.instantiate(this, fragmentName, args);
1020 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001021 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001022 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001023 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001024 }
1025 if (addToBackStack) {
1026 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1027 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001028 if (titleResId > 0) {
1029 transaction.setBreadCrumbTitle(titleResId);
1030 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001031 transaction.setBreadCrumbTitle(title);
1032 }
1033 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001034 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001035 return f;
1036 }
1037
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001038 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001039 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001040 *
1041 * @param categories The list in which to place the tiles categories.
1042 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001043 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001044 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001045 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1046 updateTilesList(categories);
1047 }
1048
1049 /**
1050 * Parse the given XML file as a categories description, adding each
1051 * parsed categories and tiles into the target list.
1052 *
1053 * @param resid The XML resource to load and parse.
1054 * @param target The list in which the parsed categories and tiles should be placed.
1055 */
1056 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1057 XmlResourceParser parser = null;
1058 try {
1059 parser = getResources().getXml(resid);
1060 AttributeSet attrs = Xml.asAttributeSet(parser);
1061
1062 int type;
1063 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1064 && type != XmlPullParser.START_TAG) {
1065 // Parse next until start tag is found
1066 }
1067
1068 String nodeName = parser.getName();
1069 if (!"dashboard-categories".equals(nodeName)) {
1070 throw new RuntimeException(
1071 "XML document must start with <preference-categories> tag; found"
1072 + nodeName + " at " + parser.getPositionDescription());
1073 }
1074
1075 Bundle curBundle = null;
1076
1077 final int outerDepth = parser.getDepth();
1078 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1079 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1080 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1081 continue;
1082 }
1083
1084 nodeName = parser.getName();
1085 if ("dashboard-category".equals(nodeName)) {
1086 DashboardCategory category = new DashboardCategory();
1087
1088 TypedArray sa = obtainStyledAttributes(
1089 attrs, com.android.internal.R.styleable.PreferenceHeader);
1090 category.id = sa.getResourceId(
1091 com.android.internal.R.styleable.PreferenceHeader_id,
1092 (int)DashboardCategory.CAT_ID_UNDEFINED);
1093
1094 TypedValue tv = sa.peekValue(
1095 com.android.internal.R.styleable.PreferenceHeader_title);
1096 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1097 if (tv.resourceId != 0) {
1098 category.titleRes = tv.resourceId;
1099 } else {
1100 category.title = tv.string;
1101 }
1102 }
1103 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001104 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1105 tv = sa.peekValue(
1106 com.android.internal.R.styleable.Preference_key);
1107 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1108 if (tv.resourceId != 0) {
1109 category.key = getString(tv.resourceId);
1110 } else {
1111 category.key = tv.string.toString();
1112 }
1113 }
1114 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001115
1116 final int innerDepth = parser.getDepth();
1117 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1118 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1119 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1120 continue;
1121 }
1122
1123 String innerNodeName = parser.getName();
1124 if (innerNodeName.equals("dashboard-tile")) {
1125 DashboardTile tile = new DashboardTile();
1126
1127 sa = obtainStyledAttributes(
1128 attrs, com.android.internal.R.styleable.PreferenceHeader);
1129 tile.id = sa.getResourceId(
1130 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001131 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001132 tv = sa.peekValue(
1133 com.android.internal.R.styleable.PreferenceHeader_title);
1134 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1135 if (tv.resourceId != 0) {
1136 tile.titleRes = tv.resourceId;
1137 } else {
1138 tile.title = tv.string;
1139 }
1140 }
1141 tv = sa.peekValue(
1142 com.android.internal.R.styleable.PreferenceHeader_summary);
1143 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1144 if (tv.resourceId != 0) {
1145 tile.summaryRes = tv.resourceId;
1146 } else {
1147 tile.summary = tv.string;
1148 }
1149 }
1150 tile.iconRes = sa.getResourceId(
1151 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1152 tile.fragment = sa.getString(
1153 com.android.internal.R.styleable.PreferenceHeader_fragment);
1154 sa.recycle();
1155
1156 if (curBundle == null) {
1157 curBundle = new Bundle();
1158 }
1159
1160 final int innerDepth2 = parser.getDepth();
1161 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1162 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1163 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1164 continue;
1165 }
1166
1167 String innerNodeName2 = parser.getName();
1168 if (innerNodeName2.equals("extra")) {
1169 getResources().parseBundleExtra("extra", attrs, curBundle);
1170 XmlUtils.skipCurrentTag(parser);
1171
1172 } else if (innerNodeName2.equals("intent")) {
1173 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1174
1175 } else {
1176 XmlUtils.skipCurrentTag(parser);
1177 }
1178 }
1179
1180 if (curBundle.size() > 0) {
1181 tile.fragmentArguments = curBundle;
1182 curBundle = null;
1183 }
1184
PauloftheWest38155612014-06-30 10:02:36 -07001185 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001186 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001187 category.addTile(tile);
1188 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001189
Jason Monk2ebc8a02015-02-13 15:23:19 -05001190 } else if (innerNodeName.equals("external-tiles")) {
1191 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001192 } else {
1193 XmlUtils.skipCurrentTag(parser);
1194 }
1195 }
1196
1197 target.add(category);
1198 } else {
1199 XmlUtils.skipCurrentTag(parser);
1200 }
1201 }
1202
1203 } catch (XmlPullParserException e) {
1204 throw new RuntimeException("Error parsing categories", e);
1205 } catch (IOException e) {
1206 throw new RuntimeException("Error parsing categories", e);
1207 } finally {
1208 if (parser != null) parser.close();
1209 }
1210 }
1211
1212 private void updateTilesList(List<DashboardCategory> target) {
1213 final boolean showDev = mDevelopmentPreferences.getBoolean(
1214 DevelopmentSettings.PREF_SHOW,
1215 android.os.Build.TYPE.equals("eng"));
1216
1217 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1218
1219 final int size = target.size();
1220 for (int i = 0; i < size; i++) {
1221
1222 DashboardCategory category = target.get(i);
1223
1224 // Ids are integers, so downcasting is ok
1225 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001226 int n = category.getTilesCount() - 1;
1227 while (n >= 0) {
1228
1229 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001230 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001231 id = (int) tile.id;
1232 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001233 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1234 removeTile = true;
1235 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001236 } else if (id == R.id.wifi_settings) {
1237 // Remove WiFi Settings if WiFi service is not available.
1238 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001239 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001240 }
1241 } else if (id == R.id.bluetooth_settings) {
1242 // Remove Bluetooth Settings if Bluetooth service is not available.
1243 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
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.data_usage_settings) {
1247 // Remove data usage when kernel module not enabled
Jason Monkb45e27b2015-05-20 13:35:43 -04001248 if (!Utils.isBandwidthControlEnabled()) {
1249 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001250 }
1251 } else if (id == R.id.battery_settings) {
1252 // Remove battery settings when battery is not available. (e.g. TV)
1253
1254 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001255 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001256 }
1257 } else if (id == R.id.home_settings) {
1258 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001259 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001260 }
1261 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001262 boolean hasMultipleUsers =
1263 ((UserManager) getSystemService(Context.USER_SERVICE))
1264 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001266 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 || Utils.isMonkeyRunning()) {
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.nfc_payment_settings) {
1271 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001272 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001273 } else {
1274 // Only show if NFC is on and we have the HCE feature
1275 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001276 if (adapter == null || !adapter.isEnabled() ||
1277 !getPackageManager().hasSystemFeature(
1278 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001279 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001280 }
1281 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001282 } else if (id == R.id.print_settings) {
1283 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1284 PackageManager.FEATURE_PRINTING);
1285 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001286 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001287 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001288 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001289 if (!showDev || um.hasUserRestriction(
1290 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001291 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001292 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001293 }
1294
1295 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001296 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1297 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001298 }
1299
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001300 if (removeTile && n < category.getTilesCount()) {
1301 category.removeTile(n);
1302 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001303 n--;
1304 }
1305 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001306 addExternalTiles(target);
1307 }
1308
1309 private void addExternalTiles(List<DashboardCategory> target) {
1310 Map<Pair<String, String>, DashboardTile> addedCache =
1311 new ArrayMap<Pair<String, String>, DashboardTile>();
1312 UserManager userManager = UserManager.get(this);
1313 for (UserHandle user : userManager.getUserProfiles()) {
1314 addExternalTiles(target, user, addedCache);
1315 }
1316 }
1317
1318 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1319 Map<Pair<String, String>, DashboardTile> addedCache) {
1320 PackageManager pm = getPackageManager();
1321 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1322 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1323 PackageManager.GET_META_DATA, user.getIdentifier());
1324 for (ResolveInfo resolved : results) {
1325 if (!resolved.system) {
1326 // Do not allow any app to add to settings, only system ones.
1327 continue;
1328 }
1329 ActivityInfo activityInfo = resolved.activityInfo;
1330 Bundle metaData = activityInfo.metaData;
1331 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1332 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1333 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1334 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1335 continue;
1336 }
1337 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1338 DashboardCategory category = getCategory(target, categoryKey);
1339 if (category == null) {
1340 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1341 + "category key " + categoryKey);
1342 continue;
1343 }
1344 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1345 activityInfo.name);
1346 DashboardTile tile = addedCache.get(key);
1347 if (tile == null) {
1348 tile = new DashboardTile();
1349 tile.intent = new Intent().setClassName(
1350 activityInfo.packageName, activityInfo.name);
1351 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1352
1353 if (category.externalIndex == -1) {
1354 // If no location for external tiles has been specified for this category,
1355 // then just put them at the end.
1356 category.addTile(tile);
1357 } else {
1358 category.addTile(category.externalIndex, tile);
1359 }
1360 addedCache.put(key, tile);
1361 }
1362 tile.userHandle.add(user);
1363 }
1364 }
1365
1366 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1367 for (DashboardCategory category : target) {
1368 if (categoryKey.equals(category.key)) {
1369 return category;
1370 }
1371 }
1372 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001373 }
1374
1375 private boolean updateHomeSettingTiles(DashboardTile tile) {
1376 // Once we decide to show Home settings, keep showing it forever
1377 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1378 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1379 return true;
1380 }
1381
1382 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001383 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001384 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001385 // When there's only one available home app, omit this settings
1386 // category entirely at the top level UI. If the user just
1387 // uninstalled the penultimate home app candidiate, we also
1388 // now tell them about why they aren't seeing 'Home' in the list.
1389 if (sShowNoHomeNotice) {
1390 sShowNoHomeNotice = false;
1391 NoHomeDialogFragment.show(this);
1392 }
1393 return false;
1394 } else {
1395 // Okay, we're allowing the Home settings category. Tell it, when
1396 // invoked via this front door, that we'll need to be told about the
1397 // case when the user uninstalls all but one home app.
1398 if (tile.fragmentArguments == null) {
1399 tile.fragmentArguments = new Bundle();
1400 }
1401 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1402 }
1403 } catch (Exception e) {
1404 // Can't look up the home activity; bail on configuring the icon
1405 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1406 }
1407
1408 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1409 return true;
1410 }
1411
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001412 private void getMetaData() {
1413 try {
1414 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1415 PackageManager.GET_META_DATA);
1416 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001417 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1418 } catch (NameNotFoundException nnfe) {
1419 // No recovery
1420 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1421 }
1422 }
1423
1424 // give subclasses access to the Next button
1425 public boolean hasNextButton() {
1426 return mNextButton != null;
1427 }
1428
1429 public Button getNextButton() {
1430 return mNextButton;
1431 }
1432
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001433 @Override
1434 public boolean shouldUpRecreateTask(Intent targetIntent) {
1435 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1436 }
1437
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001438 public static void requestHomeNotice() {
1439 sShowNoHomeNotice = true;
1440 }
1441
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001442 @Override
1443 public boolean onQueryTextSubmit(String query) {
1444 switchToSearchResultsFragmentIfNeeded();
1445 mSearchQuery = query;
1446 return mSearchResultsFragment.onQueryTextSubmit(query);
1447 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001448
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001449 @Override
1450 public boolean onQueryTextChange(String newText) {
1451 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001452 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001453 return false;
1454 }
1455 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001456 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001457
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001458 @Override
1459 public boolean onClose() {
1460 return false;
1461 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001462
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001463 @Override
1464 public boolean onMenuItemActionExpand(MenuItem item) {
1465 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001466 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001467 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001468 return true;
1469 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001470
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 @Override
1472 public boolean onMenuItemActionCollapse(MenuItem item) {
1473 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001474 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476 }
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 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001482 if (mSearchResultsFragment != null) {
1483 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001484 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001485 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001486 if (current != null && current instanceof SearchResultsSummary) {
1487 mSearchResultsFragment = (SearchResultsSummary) current;
1488 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001489 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001490 SearchResultsSummary.class.getName(), null, false, true,
1491 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001493 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001494 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001495 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001496
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001497 public void needToRevertToInitialFragment() {
1498 mNeedToRevertToInitialFragment = true;
1499 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001500
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001501 private void revertToInitialFragment() {
1502 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001503 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001504 mSearchMenuItemExpanded = false;
1505 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1506 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001507 if (mSearchMenuItem != null) {
1508 mSearchMenuItem.collapseActionView();
1509 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001510 }
Jim Miller0698a212014-10-16 19:49:07 -07001511
1512 public Intent getResultIntentData() {
1513 return mResultIntentData;
1514 }
1515
1516 public void setResultIntentData(Intent resultIntentData) {
1517 mResultIntentData = resultIntentData;
1518 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001519}