blob: c979f76ae95c5f1d5157ff277132e4de7237c8fe [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
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080019import android.app.ActionBar;
20import android.app.Activity;
21import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.pm.ResolveInfo;
34import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080035import android.content.res.TypedArray;
36import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.nfc.NfcAdapter;
38import android.os.Bundle;
39import android.os.Handler;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080041import android.os.UserHandle;
42import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v14.preference.PreferenceFragment;
44import android.support.v7.preference.Preference;
45import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080046import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070047import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050048import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.util.AttributeSet;
50import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050051import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.TypedValue;
53import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070054import android.view.Menu;
55import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080056import android.view.MenuItem;
57import android.view.View;
58import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070059import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080060import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070061import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040062
Chris Wren8a963ba2015-03-20 10:29:14 -040063import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080064import com.android.internal.util.ArrayUtils;
65import com.android.internal.util.XmlUtils;
66import com.android.settings.accessibility.AccessibilitySettings;
67import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000068import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080069import com.android.settings.accounts.AccountSyncSettings;
Billy Laufee78562015-07-27 12:57:07 +010070import com.android.settings.applications.DrawOverlayDetails;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070071import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.applications.ManageApplications;
Xiyuan Xia86a55402015-06-02 14:55:32 -070073import com.android.settings.applications.ManageAssist;
Jason Monk2cdafc62015-06-12 12:32:50 -040074import com.android.settings.applications.ProcessStatsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040076import com.android.settings.applications.UsageAccessDetails;
Billy Laufee78562015-07-27 12:57:07 +010077import com.android.settings.applications.WriteSettingsDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070079import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070082import com.android.settings.dashboard.NoHomeDialogFragment;
83import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070084import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070085import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070086import com.android.settings.deviceinfo.PublicVolumeSettings;
87import com.android.settings.deviceinfo.StorageSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040088import com.android.settings.fuelgauge.BatterySaverSettings;
Jason Monk1eb54eb2015-04-29 12:46:42 -040089import com.android.settings.fuelgauge.PowerUsageDetail;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.fuelgauge.PowerUsageSummary;
91import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
92import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
93import com.android.settings.inputmethod.SpellCheckersSettings;
94import com.android.settings.inputmethod.UserDictionaryList;
95import com.android.settings.location.LocationSettings;
96import com.android.settings.nfc.AndroidBeam;
97import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040098import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -040099import com.android.settings.notification.NotificationAccessSettings;
100import com.android.settings.notification.NotificationSettings;
101import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500102import com.android.settings.notification.OtherSoundSettings;
John Spurlock08531a82015-05-07 17:45:43 -0400103import com.android.settings.notification.ZenAccessSettings;
John Spurlock533a5662015-06-19 10:47:36 -0400104import com.android.settings.notification.ZenModeAutomationSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400105import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400106import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400107import com.android.settings.notification.ZenModeScheduleRuleSettings;
Jason Monk39b46742015-09-10 15:52:51 -0400108import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.print.PrintJobSettingsFragment;
110import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500111import com.android.settings.search.DynamicIndexableContentMonitor;
112import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700113import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import com.android.settings.tts.TextToSpeechSettings;
115import com.android.settings.users.UserSettings;
116import 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
Jason Monk39b46742015-09-10 15:52:51 -0400133import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
134
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800135public class SettingsActivity extends Activity
136 implements PreferenceManager.OnPreferenceTreeClickListener,
137 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100138 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700139 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100140 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 private static final String LOG_TAG = "Settings";
143
144 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700145 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700146 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
147 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700148 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700149 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700150 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151
152 /**
153 * When starting this activity, the invoking Intent can contain this extra
154 * string to specify which fragment should be initially displayed.
155 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
156 * will call isValidFragment() to confirm that the fragment class name is valid for this
157 * activity.
158 */
159 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
160
161 /**
162 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
163 * this extra can also be specified to supply a Bundle of arguments to pass
164 * to that fragment when it is instantiated during the initial creation
165 * of the activity.
166 */
167 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
168
169 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700170 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
171 */
172 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
173
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800174 public static final String BACK_STACK_PREFS = ":settings:prefs";
175
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800176 // extras that allow any preference activity to be launched as part of a wizard
177
178 // show Back and Next buttons? takes boolean parameter
179 // Back will then return RESULT_CANCELED and Next RESULT_OK
180 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
181
182 // add a Skip button?
183 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
184
185 // specify custom text for the Back or Next buttons, or cause a button to not appear
186 // at all by setting it to null
187 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
188 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
189
190 /**
191 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700192 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800193 * that fragment.
194 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700195 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100196 /**
197 * The package name used to resolve the title resource id.
198 */
199 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
200 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700201 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
202 ":settings:show_fragment_title_resid";
203 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
204 ":settings:show_fragment_as_shortcut";
205
206 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
207 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 private static final String META_DATA_KEY_FRAGMENT_CLASS =
210 "com.android.settings.FRAGMENT_CLASS";
211
212 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
213
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700214 private static final String EMPTY_QUERY = "";
215
Jason Monk2ebc8a02015-02-13 15:23:19 -0500216 /**
217 * Settings will search for system activities of this action and add them as a top level
218 * settings tile using the following parameters.
219 *
220 * <p>A category must be specified in the meta-data for the activity named
221 * {@link #EXTRA_CATEGORY_KEY}
222 *
223 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
224 * otherwise the label for the activity will be used.
225 *
226 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
227 * otherwise the icon for the activity will be used.
228 *
229 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
230 */
231 private static final String EXTRA_SETTINGS_ACTION =
232 "com.android.settings.action.EXTRA_SETTINGS";
233
234 /**
235 * The key used to get the category from metadata of activities of action
236 * {@link #EXTRA_SETTINGS_ACTION}
237 * The value must be one of:
238 * <li>com.android.settings.category.wireless</li>
239 * <li>com.android.settings.category.device</li>
240 * <li>com.android.settings.category.personal</li>
241 * <li>com.android.settings.category.system</li>
242 */
243 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
244
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800245 private static boolean sShowNoHomeNotice = false;
246
247 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800249 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700250 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800251
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800252 // Show only these settings for restricted users
253 private int[] SETTINGS_FOR_RESTRICTED = {
254 R.id.wireless_section,
255 R.id.wifi_settings,
256 R.id.bluetooth_settings,
257 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700258 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 R.id.wireless_settings,
260 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400261 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800262 R.id.display_settings,
263 R.id.storage_settings,
264 R.id.application_settings,
265 R.id.battery_settings,
266 R.id.personal_section,
267 R.id.location_settings,
268 R.id.security_settings,
269 R.id.language_settings,
270 R.id.user_settings,
271 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272 R.id.system_section,
273 R.id.date_time_settings,
274 R.id.about_settings,
275 R.id.accessibility_settings,
276 R.id.print_settings,
277 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800278 R.id.home_settings,
279 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800280 };
281
282 private static final String[] ENTRY_FRAGMENTS = {
283 WirelessSettings.class.getName(),
284 WifiSettings.class.getName(),
285 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700286 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700288 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 TetherSettings.class.getName(),
290 WifiP2pSettings.class.getName(),
291 VpnSettings.class.getName(),
292 DateTimeSettings.class.getName(),
293 LocalePicker.class.getName(),
294 InputMethodAndLanguageSettings.class.getName(),
295 SpellCheckersSettings.class.getName(),
296 UserDictionaryList.class.getName(),
297 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700298 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 DisplaySettings.class.getName(),
300 DeviceInfoSettings.class.getName(),
301 ManageApplications.class.getName(),
Xiyuan Xia86a55402015-06-02 14:55:32 -0700302 ManageAssist.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 ProcessStatsUi.class.getName(),
304 NotificationStation.class.getName(),
305 LocationSettings.class.getName(),
306 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400307 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 PrivacySettings.class.getName(),
309 DeviceAdminSettings.class.getName(),
310 AccessibilitySettings.class.getName(),
311 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
313 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700314 StorageSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700315 PrivateVolumeForget.class.getName(),
Jeff Sharkeye77f0682015-04-29 11:24:57 -0700316 PrivateVolumeSettings.class.getName(),
317 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 DevelopmentSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800319 AndroidBeam.class.getName(),
320 WifiDisplaySettings.class.getName(),
321 PowerUsageSummary.class.getName(),
322 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000323 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 CryptKeeperSettings.class.getName(),
325 DataUsageSummary.class.getName(),
326 DreamSettings.class.getName(),
327 UserSettings.class.getName(),
328 NotificationAccessSettings.class.getName(),
John Spurlock08531a82015-05-07 17:45:43 -0400329 ZenAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800330 PrintSettingsFragment.class.getName(),
331 PrintJobSettingsFragment.class.getName(),
332 TrustedCredentialsSettings.class.getName(),
333 PaymentSettings.class.getName(),
334 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700335 ZenModeSettings.class.getName(),
336 NotificationSettings.class.getName(),
337 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
338 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400339 InstalledAppDetails.class.getName(),
340 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700341 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700342 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000343 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400344 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400345 ZenModePrioritySettings.class.getName(),
John Spurlock533a5662015-06-19 10:47:36 -0400346 ZenModeAutomationSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400347 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400348 ZenModeEventRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400349 ProcessStatsUi.class.getName(),
Jason Monk1eb54eb2015-04-29 12:46:42 -0400350 PowerUsageDetail.class.getName(),
Jason Monk2cdafc62015-06-12 12:32:50 -0400351 ProcessStatsSummary.class.getName(),
Billy Laufee78562015-07-27 12:57:07 +0100352 DrawOverlayDetails.class.getName(),
353 WriteSettingsDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 };
355
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700356
357 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
358 "android.settings.APPLICATION_DETAILS_SETTINGS"
359 };
360
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 private SharedPreferences mDevelopmentPreferences;
362 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
363
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 private boolean mBatteryPresent = true;
365 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 @Override
367 public void onReceive(Context context, Intent intent) {
368 String action = intent.getAction();
369 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
370 boolean batteryPresent = Utils.isBatteryPresent(intent);
371
372 if (mBatteryPresent != batteryPresent) {
373 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700374 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800375 }
376 }
377 }
378 };
379
Svetoslav990159a2014-04-14 17:14:59 -0700380 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
381 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700382
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700383 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700384 private SwitchBar mSwitchBar;
385
386 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700387
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700388 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700389 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700390
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700391 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700392 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700393
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700394 private ViewGroup mContent;
395
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700396 private SearchView mSearchView;
397 private MenuItem mSearchMenuItem;
398 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700399 private SearchResultsSummary mSearchResultsFragment;
400 private String mSearchQuery;
401
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700402 // Categories
403 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700405 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700406 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800407 private Handler mHandler = new Handler() {
408 @Override
409 public void handleMessage(Message msg) {
410 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700411 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700412 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
413 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700414 buildDashboardCategories(mCategories);
415 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800416 } break;
417 }
418 }
419 };
420
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700422 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700423
Jim Miller0698a212014-10-16 19:49:07 -0700424 private Intent mResultIntentData;
425
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700426 public SwitchBar getSwitchBar() {
427 return mSwitchBar;
428 }
429
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700430 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
431 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700432 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700433 }
434 return mCategories;
435 }
436
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800437 @Override
438 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
439 // Override the fragment title for Wallpaper settings
Jason Monk39b46742015-09-10 15:52:51 -0400440 CharSequence title = pref.getTitle();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800441 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
Jason Monk39b46742015-09-10 15:52:51 -0400442 title = getString(R.string.wallpaper_settings_fragment_title);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800443 }
Jason Monk39b46742015-09-10 15:52:51 -0400444 startPreferencePanel(pref.getFragment(), pref.getExtras(), 0, title,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800445 null, 0);
446 return true;
447 }
448
449 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400450 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800451 return false;
452 }
453
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700454 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700455 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700456 Message msg = new Message();
457 msg.what = MSG_BUILD_CATEGORIES;
458 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800459 }
460 }
461
462 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463 public void onConfigurationChanged(Configuration newConfig) {
464 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800465 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 }
467
468 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700469 protected void onStart() {
470 super.onStart();
471
472 if (mNeedToRevertToInitialFragment) {
473 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800475 }
476
477 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700478 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700479 if (!mDisplaySearch) {
480 return false;
481 }
482
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700483 MenuInflater inflater = getMenuInflater();
484 inflater.inflate(R.menu.options_menu, menu);
485
486 // Cache the search query (can be overriden by the OnQueryTextListener)
487 final String query = mSearchQuery;
488
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700489 mSearchMenuItem = menu.findItem(R.id.search);
490 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700491
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700492 if (mSearchMenuItem == null || mSearchView == null) {
493 return false;
494 }
495
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700496 if (mSearchResultsFragment != null) {
497 mSearchResultsFragment.setSearchView(mSearchView);
498 }
499
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700500 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700501 mSearchView.setOnQueryTextListener(this);
502 mSearchView.setOnCloseListener(this);
503
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700504 if (mSearchMenuItemExpanded) {
505 mSearchMenuItem.expandActionView();
506 }
507 mSearchView.setQuery(query, true /* submit */);
508
509 return true;
510 }
511
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700512 private static boolean isShortCutIntent(final Intent intent) {
513 Set<String> categories = intent.getCategories();
514 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
515 }
516
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700517 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700518 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700519 if (action == null) {
520 return false;
521 }
522 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
523 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
524 }
525 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700526 }
527
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700528 @Override
529 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700530 super.onCreate(savedState);
531
532 // Should happen before any call to getIntent()
533 getMetaData();
534
535 final Intent intent = getIntent();
536 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
537 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
541 Context.MODE_PRIVATE);
542
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700543 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700544 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700545
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700546 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700547 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
548
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700549 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700550 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700551
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700552 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700553
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700554 // This is a "Sub Settings" when:
555 // - this is a real SubSettings
556 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700557 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700558 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
559
560 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
561 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700562 // Check also that we are not a Theme Dialog as we don't want to override them
563 final int themeResId = getThemeResId();
564 if (themeResId != R.style.Theme_DialogWhenLarge &&
565 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
566 setTheme(R.style.Theme_SubSettings);
567 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700568 }
569
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700570 setContentView(mIsShowingDashboard ?
571 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800572
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700573 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700574
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 getFragmentManager().addOnBackStackChangedListener(this);
576
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700577 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800578 // Run the Index update only if we have some space
579 if (!Utils.isLowStorage(this)) {
580 Index.getInstance(getApplicationContext()).update();
581 } else {
582 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
583 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700584 }
585
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700586 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700587 // We are restarting from a previous saved state; used that to initialize, instead
588 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700589 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
590 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800591
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700592 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800593
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700594 ArrayList<DashboardCategory> categories =
595 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
596 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700597 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700598 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700599 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800600 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700601
602 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700603 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700604 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
605 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800606 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700607 if (!mIsShowingDashboard) {
Jason Monkea8b1a72015-06-02 16:46:34 -0400608 mDisplaySearch = false;
609 // UP will be shown only if it is a sub settings
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700610 if (mIsShortcut) {
611 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700612 } else if (isSubSettings) {
613 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700614 } else {
615 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700616 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700617 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700618
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700619 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700620 switchToFragment(initialFragmentName, initialArguments, true, false,
621 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000622 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700623 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700624 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700625 // Show Search affordance
626 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700627 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700628 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700629 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800630 }
631 }
632
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700633 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700634 if (mActionBar != null) {
635 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
636 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
637 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700638 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
639
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800641 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
642
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700643 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800644 if (buttonBar != null) {
645 buttonBar.setVisibility(View.VISIBLE);
646
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700647 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 backButton.setOnClickListener(new OnClickListener() {
649 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700650 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 finish();
652 }
653 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700654 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 skipButton.setOnClickListener(new OnClickListener() {
656 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700657 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 finish();
659 }
660 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700661 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800662 mNextButton.setOnClickListener(new OnClickListener() {
663 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700664 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800665 finish();
666 }
667 });
668
669 // set our various button parameters
670 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
671 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
672 if (TextUtils.isEmpty(buttonText)) {
673 mNextButton.setVisibility(View.GONE);
674 }
675 else {
676 mNextButton.setText(buttonText);
677 }
678 }
679 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
680 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
681 if (TextUtils.isEmpty(buttonText)) {
682 backButton.setVisibility(View.GONE);
683 }
684 else {
685 backButton.setText(buttonText);
686 }
687 }
688 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
689 skipButton.setVisibility(View.VISIBLE);
690 }
691 }
692 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700693
694 mHomeActivitiesCount = getHomeActivitiesCount();
695 }
696
697 private int getHomeActivitiesCount() {
698 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
699 getPackageManager().getHomeActivities(homeApps);
700 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800701 }
702
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700703 private void setTitleFromIntent(Intent intent) {
704 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
705 if (initialTitleResId > 0) {
706 mInitialTitle = null;
707 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100708
709 final String initialTitleResPackageName = intent.getStringExtra(
710 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
711 if (initialTitleResPackageName != null) {
712 try {
713 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
714 0 /* flags */, new UserHandle(UserHandle.myUserId()));
715 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
716 setTitle(mInitialTitle);
717 mInitialTitleResId = -1;
718 return;
719 } catch (NameNotFoundException e) {
720 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
721 }
722 } else {
723 setTitle(mInitialTitleResId);
724 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700725 } else {
726 mInitialTitleResId = -1;
727 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
728 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
729 setTitle(mInitialTitle);
730 }
731 }
732
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800733 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800734 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700735 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800736 }
737
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700738 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800739 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700740
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800741 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700742 if (mInitialTitleResId > 0) {
743 setTitle(mInitialTitleResId);
744 } else {
745 setTitle(mInitialTitle);
746 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700747 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800748 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700749
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800750 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
751 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700752
753 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800754 }
755
756 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
757 final CharSequence title;
758 final int titleRes = bse.getBreadCrumbTitleRes();
759 if (titleRes > 0) {
760 title = getText(titleRes);
761 } else {
762 title = bse.getBreadCrumbTitle();
763 }
764 if (title != null) {
765 setTitle(title);
766 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800767 }
768
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800769 @Override
770 protected void onSaveInstanceState(Bundle outState) {
771 super.onSaveInstanceState(outState);
772
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700773 if (mCategories.size() > 0) {
774 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800775 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700776
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700777 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700778 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700779
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700780 if (mDisplaySearch) {
781 // The option menus are created if the ActionBar is visible and they are also created
782 // asynchronously. If you launch Settings with an Intent action like
783 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
784 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
785 // menu item and search view are null.
786 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
787 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700788
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700789 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
790 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
791 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700792
793 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800794 }
795
796 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400797 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800798 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400799 if (mIsShowingDashboard) {
800 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
801 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800802
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700803 final int newHomeActivityCount = getHomeActivitiesCount();
804 if (newHomeActivityCount != mHomeActivitiesCount) {
805 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700806 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700807 }
808
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800809 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
810 @Override
811 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700812 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800813 }
814 };
815 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
816 mDevelopmentPreferencesListener);
817
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800818 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700819
Svetoslav990159a2014-04-14 17:14:59 -0700820 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700821
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700822 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700823 onQueryTextSubmit(mSearchQuery);
824 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800825 }
826
827 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400828 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800829 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400830 if (mIsShowingDashboard) {
831 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
832 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800833 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700834 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800835 }
836
837 @Override
838 public void onDestroy() {
839 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700840
841 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
842 mDevelopmentPreferencesListener);
843 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800844 }
845
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800846 protected boolean isValidFragment(String fragmentName) {
847 // Almost all fragments are wrapped in this,
848 // except for a few that have their own activities.
849 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
850 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
851 }
852 return false;
853 }
854
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800855 @Override
856 public Intent getIntent() {
857 Intent superIntent = super.getIntent();
858 String startingFragment = getStartingFragmentClass(superIntent);
859 // This is called from super.onCreate, isMultiPane() is not yet reliable
860 // Do not use onIsHidingHeaders either, which relies itself on this method
861 if (startingFragment != null) {
862 Intent modIntent = new Intent(superIntent);
863 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
864 Bundle args = superIntent.getExtras();
865 if (args != null) {
866 args = new Bundle(args);
867 } else {
868 args = new Bundle();
869 }
870 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100871 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800872 return modIntent;
873 }
874 return superIntent;
875 }
876
877 /**
878 * Checks if the component name in the intent is different from the Settings class and
879 * returns the class name to load as a fragment.
880 */
881 private String getStartingFragmentClass(Intent intent) {
882 if (mFragmentClass != null) return mFragmentClass;
883
884 String intentClass = intent.getComponent().getClassName();
885 if (intentClass.equals(getClass().getName())) return null;
886
887 if ("com.android.settings.ManageApplications".equals(intentClass)
888 || "com.android.settings.RunningServices".equals(intentClass)
889 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
890 // Old names of manage apps.
891 intentClass = com.android.settings.applications.ManageApplications.class.getName();
892 }
893
894 return intentClass;
895 }
896
897 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000898 * Start a new fragment containing a preference panel. If the preferences
899 * are being displayed in multi-pane mode, the given fragment class will
900 * be instantiated and placed in the appropriate pane. If running in
901 * single-pane mode, a new activity will be launched in which to show the
902 * fragment.
903 *
904 * @param fragmentClass Full name of the class implementing the fragment.
905 * @param args Any desired arguments to supply to the fragment.
906 * @param titleRes Optional resource identifier of the title of this
907 * fragment.
908 * @param titleText Optional text of the title of this fragment.
909 * @param resultTo Optional fragment that result data should be sent to.
910 * If non-null, resultTo.onActivityResult() will be called when this
911 * preference panel is done. The launched panel must use
912 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
913 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700914 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000915 */
916 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700917 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700918 String title = null;
919 if (titleRes < 0) {
920 if (titleText != null) {
921 title = titleText.toString();
922 } else {
923 // There not much we can do in that case
924 title = "";
925 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700926 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700927 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700928 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000929 }
930
931 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100932 * Start a new fragment in a new activity containing a preference panel for a given user. If the
933 * preferences are being displayed in multi-pane mode, the given fragment class will be
934 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
935 * activity will be launched in which to show the fragment.
936 *
937 * @param fragmentClass Full name of the class implementing the fragment.
938 * @param args Any desired arguments to supply to the fragment.
939 * @param titleRes Optional resource identifier of the title of this fragment.
940 * @param titleText Optional text of the title of this fragment.
941 * @param userHandle The user for which the panel has to be started.
942 */
943 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
944 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700945 // This is a workaround.
946 //
947 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
948 // starting the fragment could cause a native stack corruption. See b/17523189. However,
949 // adding that flag and start the preference panel with the same UserHandler will make it
950 // impossible to use back button to return to the previous screen. See b/20042570.
951 //
952 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
953 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
954 // when we're calling it as the same user.
955 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
956 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
957 } else {
958 String title = null;
959 if (titleRes < 0) {
960 if (titleText != null) {
961 title = titleText.toString();
962 } else {
963 // There not much we can do in that case
964 title = "";
965 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100966 }
Lifu Tangd0332852015-04-02 12:05:46 -0700967 Utils.startWithFragmentAsUser(this, fragmentClass, args,
968 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100969 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100970 }
971
972 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800973 * Called by a preference panel fragment to finish itself.
974 *
975 * @param caller The fragment that is asking to be finished.
976 * @param resultCode Optional result code to send back to the original
977 * launching fragment.
978 * @param resultData Optional result data to send back to the original
979 * launching fragment.
980 */
981 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
982 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700983 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800984 }
985
986 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000987 * Start a new fragment.
988 *
989 * @param fragment The fragment to start
990 * @param push If true, the current fragment will be pushed onto the back stack. If false,
991 * the current fragment will be replaced.
992 */
993 public void startPreferenceFragment(Fragment fragment, boolean push) {
994 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700995 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000996 if (push) {
997 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
998 transaction.addToBackStack(BACK_STACK_PREFS);
999 } else {
1000 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1001 }
1002 transaction.commitAllowingStateLoss();
1003 }
1004
1005 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001006 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1007 */
1008 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001009 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001010 if (validate && !isValidFragment(fragmentName)) {
1011 throw new IllegalArgumentException("Invalid fragment for this activity: "
1012 + fragmentName);
1013 }
1014 Fragment f = Fragment.instantiate(this, fragmentName, args);
1015 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001016 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001017 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001018 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001019 }
1020 if (addToBackStack) {
1021 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1022 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001023 if (titleResId > 0) {
1024 transaction.setBreadCrumbTitle(titleResId);
1025 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001026 transaction.setBreadCrumbTitle(title);
1027 }
1028 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001029 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001030 return f;
1031 }
1032
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001033 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001034 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001035 *
1036 * @param categories The list in which to place the tiles categories.
1037 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001038 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001039 categories.clear();
Jason Monk2acff552015-06-11 15:47:29 -04001040 loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001041 updateTilesList(categories);
1042 }
1043
1044 /**
1045 * Parse the given XML file as a categories description, adding each
1046 * parsed categories and tiles into the target list.
1047 *
1048 * @param resid The XML resource to load and parse.
1049 * @param target The list in which the parsed categories and tiles should be placed.
1050 */
Jason Monk2acff552015-06-11 15:47:29 -04001051 public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
1052 Context context) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001053 XmlResourceParser parser = null;
1054 try {
Jason Monk2acff552015-06-11 15:47:29 -04001055 parser = context.getResources().getXml(resid);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001056 AttributeSet attrs = Xml.asAttributeSet(parser);
1057
1058 int type;
1059 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1060 && type != XmlPullParser.START_TAG) {
1061 // Parse next until start tag is found
1062 }
1063
1064 String nodeName = parser.getName();
1065 if (!"dashboard-categories".equals(nodeName)) {
1066 throw new RuntimeException(
1067 "XML document must start with <preference-categories> tag; found"
1068 + nodeName + " at " + parser.getPositionDescription());
1069 }
1070
1071 Bundle curBundle = null;
1072
1073 final int outerDepth = parser.getDepth();
1074 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1075 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1076 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1077 continue;
1078 }
1079
1080 nodeName = parser.getName();
1081 if ("dashboard-category".equals(nodeName)) {
1082 DashboardCategory category = new DashboardCategory();
1083
Jason Monk2acff552015-06-11 15:47:29 -04001084 TypedArray sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001085 attrs, com.android.internal.R.styleable.PreferenceHeader);
1086 category.id = sa.getResourceId(
1087 com.android.internal.R.styleable.PreferenceHeader_id,
1088 (int)DashboardCategory.CAT_ID_UNDEFINED);
1089
1090 TypedValue tv = sa.peekValue(
1091 com.android.internal.R.styleable.PreferenceHeader_title);
1092 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1093 if (tv.resourceId != 0) {
1094 category.titleRes = tv.resourceId;
1095 } else {
1096 category.title = tv.string;
1097 }
1098 }
1099 sa.recycle();
Jason Monk2acff552015-06-11 15:47:29 -04001100 sa = context.obtainStyledAttributes(attrs,
1101 com.android.internal.R.styleable.Preference);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001102 tv = sa.peekValue(
1103 com.android.internal.R.styleable.Preference_key);
1104 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1105 if (tv.resourceId != 0) {
Jason Monk2acff552015-06-11 15:47:29 -04001106 category.key = context.getString(tv.resourceId);
Jason Monk2ebc8a02015-02-13 15:23:19 -05001107 } else {
1108 category.key = tv.string.toString();
1109 }
1110 }
1111 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001112
1113 final int innerDepth = parser.getDepth();
1114 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1115 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1116 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1117 continue;
1118 }
1119
1120 String innerNodeName = parser.getName();
1121 if (innerNodeName.equals("dashboard-tile")) {
1122 DashboardTile tile = new DashboardTile();
1123
Jason Monk2acff552015-06-11 15:47:29 -04001124 sa = context.obtainStyledAttributes(
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001125 attrs, com.android.internal.R.styleable.PreferenceHeader);
1126 tile.id = sa.getResourceId(
1127 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001128 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001129 tv = sa.peekValue(
1130 com.android.internal.R.styleable.PreferenceHeader_title);
1131 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1132 if (tv.resourceId != 0) {
1133 tile.titleRes = tv.resourceId;
1134 } else {
1135 tile.title = tv.string;
1136 }
1137 }
1138 tv = sa.peekValue(
1139 com.android.internal.R.styleable.PreferenceHeader_summary);
1140 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1141 if (tv.resourceId != 0) {
1142 tile.summaryRes = tv.resourceId;
1143 } else {
1144 tile.summary = tv.string;
1145 }
1146 }
1147 tile.iconRes = sa.getResourceId(
1148 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1149 tile.fragment = sa.getString(
1150 com.android.internal.R.styleable.PreferenceHeader_fragment);
1151 sa.recycle();
1152
1153 if (curBundle == null) {
1154 curBundle = new Bundle();
1155 }
1156
1157 final int innerDepth2 = parser.getDepth();
1158 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1159 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1160 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1161 continue;
1162 }
1163
1164 String innerNodeName2 = parser.getName();
1165 if (innerNodeName2.equals("extra")) {
Jason Monk2acff552015-06-11 15:47:29 -04001166 context.getResources().parseBundleExtra("extra", attrs,
1167 curBundle);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001168 XmlUtils.skipCurrentTag(parser);
1169
1170 } else if (innerNodeName2.equals("intent")) {
Jason Monk2acff552015-06-11 15:47:29 -04001171 tile.intent = Intent.parseIntent(context.getResources(), parser,
1172 attrs);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001173
1174 } else {
1175 XmlUtils.skipCurrentTag(parser);
1176 }
1177 }
1178
1179 if (curBundle.size() > 0) {
1180 tile.fragmentArguments = curBundle;
1181 curBundle = null;
1182 }
1183
PauloftheWest38155612014-06-30 10:02:36 -07001184 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Jason Monk2acff552015-06-11 15:47:29 -04001185 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(context)){
PauloftheWest38155612014-06-30 10:02:36 -07001186 category.addTile(tile);
1187 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001188
Jason Monk2ebc8a02015-02-13 15:23:19 -05001189 } else if (innerNodeName.equals("external-tiles")) {
1190 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001191 } else {
1192 XmlUtils.skipCurrentTag(parser);
1193 }
1194 }
1195
1196 target.add(category);
1197 } else {
1198 XmlUtils.skipCurrentTag(parser);
1199 }
1200 }
1201
1202 } catch (XmlPullParserException e) {
1203 throw new RuntimeException("Error parsing categories", e);
1204 } catch (IOException e) {
1205 throw new RuntimeException("Error parsing categories", e);
1206 } finally {
1207 if (parser != null) parser.close();
1208 }
1209 }
1210
1211 private void updateTilesList(List<DashboardCategory> target) {
1212 final boolean showDev = mDevelopmentPreferences.getBoolean(
1213 DevelopmentSettings.PREF_SHOW,
1214 android.os.Build.TYPE.equals("eng"));
1215
Xiaohui Chen44879a32015-07-22 13:53:22 -07001216 final UserManager um = UserManager.get(this);
1217 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001218
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) {
1262 if (!UserHandle.MU_ENABLED
Nicolas Prevot36940542015-05-13 17:19:21 -07001263 || !UserManager.supportsMultipleUsers()
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001264 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001265 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001266 }
1267 } else if (id == R.id.nfc_payment_settings) {
1268 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001269 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001270 } else {
1271 // Only show if NFC is on and we have the HCE feature
1272 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001273 if (adapter == null || !adapter.isEnabled() ||
1274 !getPackageManager().hasSystemFeature(
1275 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001276 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001277 }
1278 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001279 } else if (id == R.id.print_settings) {
1280 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1281 PackageManager.FEATURE_PRINTING);
1282 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001283 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001284 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001285 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001286 if (!showDev || um.hasUserRestriction(
1287 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001288 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001289 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001290 }
1291
Xiaohui Chen44879a32015-07-22 13:53:22 -07001292 if (UserHandle.MU_ENABLED && !isAdmin
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001293 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1294 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001295 }
1296
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001297 if (removeTile && n < category.getTilesCount()) {
1298 category.removeTile(n);
1299 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001300 n--;
1301 }
1302 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001303 addExternalTiles(target);
1304 }
1305
1306 private void addExternalTiles(List<DashboardCategory> target) {
1307 Map<Pair<String, String>, DashboardTile> addedCache =
1308 new ArrayMap<Pair<String, String>, DashboardTile>();
1309 UserManager userManager = UserManager.get(this);
1310 for (UserHandle user : userManager.getUserProfiles()) {
1311 addExternalTiles(target, user, addedCache);
1312 }
1313 }
1314
1315 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1316 Map<Pair<String, String>, DashboardTile> addedCache) {
1317 PackageManager pm = getPackageManager();
1318 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1319 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1320 PackageManager.GET_META_DATA, user.getIdentifier());
1321 for (ResolveInfo resolved : results) {
1322 if (!resolved.system) {
1323 // Do not allow any app to add to settings, only system ones.
1324 continue;
1325 }
1326 ActivityInfo activityInfo = resolved.activityInfo;
1327 Bundle metaData = activityInfo.metaData;
1328 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1329 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1330 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1331 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1332 continue;
1333 }
1334 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1335 DashboardCategory category = getCategory(target, categoryKey);
1336 if (category == null) {
1337 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1338 + "category key " + categoryKey);
1339 continue;
1340 }
1341 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1342 activityInfo.name);
1343 DashboardTile tile = addedCache.get(key);
1344 if (tile == null) {
1345 tile = new DashboardTile();
1346 tile.intent = new Intent().setClassName(
1347 activityInfo.packageName, activityInfo.name);
1348 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1349
1350 if (category.externalIndex == -1) {
1351 // If no location for external tiles has been specified for this category,
1352 // then just put them at the end.
1353 category.addTile(tile);
1354 } else {
1355 category.addTile(category.externalIndex, tile);
1356 }
1357 addedCache.put(key, tile);
1358 }
1359 tile.userHandle.add(user);
1360 }
1361 }
1362
1363 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1364 for (DashboardCategory category : target) {
1365 if (categoryKey.equals(category.key)) {
1366 return category;
1367 }
1368 }
1369 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001370 }
1371
1372 private boolean updateHomeSettingTiles(DashboardTile tile) {
1373 // Once we decide to show Home settings, keep showing it forever
1374 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1375 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1376 return true;
1377 }
1378
1379 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001380 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001381 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001382 // When there's only one available home app, omit this settings
1383 // category entirely at the top level UI. If the user just
1384 // uninstalled the penultimate home app candidiate, we also
1385 // now tell them about why they aren't seeing 'Home' in the list.
1386 if (sShowNoHomeNotice) {
1387 sShowNoHomeNotice = false;
1388 NoHomeDialogFragment.show(this);
1389 }
1390 return false;
1391 } else {
1392 // Okay, we're allowing the Home settings category. Tell it, when
1393 // invoked via this front door, that we'll need to be told about the
1394 // case when the user uninstalls all but one home app.
1395 if (tile.fragmentArguments == null) {
1396 tile.fragmentArguments = new Bundle();
1397 }
1398 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1399 }
1400 } catch (Exception e) {
1401 // Can't look up the home activity; bail on configuring the icon
1402 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1403 }
1404
1405 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1406 return true;
1407 }
1408
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001409 private void getMetaData() {
1410 try {
1411 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1412 PackageManager.GET_META_DATA);
1413 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001414 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1415 } catch (NameNotFoundException nnfe) {
1416 // No recovery
1417 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1418 }
1419 }
1420
1421 // give subclasses access to the Next button
1422 public boolean hasNextButton() {
1423 return mNextButton != null;
1424 }
1425
1426 public Button getNextButton() {
1427 return mNextButton;
1428 }
1429
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001430 @Override
1431 public boolean shouldUpRecreateTask(Intent targetIntent) {
1432 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1433 }
1434
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001435 public static void requestHomeNotice() {
1436 sShowNoHomeNotice = true;
1437 }
1438
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001439 @Override
1440 public boolean onQueryTextSubmit(String query) {
1441 switchToSearchResultsFragmentIfNeeded();
1442 mSearchQuery = query;
1443 return mSearchResultsFragment.onQueryTextSubmit(query);
1444 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001445
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001446 @Override
1447 public boolean onQueryTextChange(String newText) {
1448 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001449 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001450 return false;
1451 }
1452 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001453 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001454
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001455 @Override
1456 public boolean onClose() {
1457 return false;
1458 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001459
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001460 @Override
1461 public boolean onMenuItemActionExpand(MenuItem item) {
1462 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001463 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001464 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001465 return true;
1466 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001467
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001468 @Override
1469 public boolean onMenuItemActionCollapse(MenuItem item) {
1470 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001471 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001472 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001474 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 return true;
1476 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001477
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001478 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001479 if (mSearchResultsFragment != null) {
1480 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001481 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001482 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001483 if (current != null && current instanceof SearchResultsSummary) {
1484 mSearchResultsFragment = (SearchResultsSummary) current;
1485 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001486 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001487 SearchResultsSummary.class.getName(), null, false, true,
1488 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001489 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001490 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001491 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001492 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001493
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001494 public void needToRevertToInitialFragment() {
1495 mNeedToRevertToInitialFragment = true;
1496 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001497
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001498 private void revertToInitialFragment() {
1499 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001500 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001501 mSearchMenuItemExpanded = false;
1502 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1503 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001504 if (mSearchMenuItem != null) {
1505 mSearchMenuItem.collapseActionView();
1506 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001507 }
Jim Miller0698a212014-10-16 19:49:07 -07001508
1509 public Intent getResultIntentData() {
1510 return mResultIntentData;
1511 }
1512
1513 public void setResultIntentData(Intent resultIntentData) {
1514 mResultIntentData = resultIntentData;
1515 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001516}