blob: 271261bc34b2b49eef96abd381bfd00a205d3be9 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason Monk2ebc8a02015-02-13 15:23:19 -050019import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
20
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.ActionBar;
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.nfc.NfcAdapter;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.INetworkManagementService;
43import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.preference.Preference;
49import android.preference.PreferenceFragment;
50import android.preference.PreferenceManager;
51import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.AttributeSet;
56import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050057import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.util.TypedValue;
59import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070060import android.view.Menu;
61import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.view.MenuItem;
63import android.view.View;
64import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070065import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080066import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070067import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040068
Chris Wren8a963ba2015-03-20 10:29:14 -040069import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.internal.util.ArrayUtils;
71import com.android.internal.util.XmlUtils;
72import com.android.settings.accessibility.AccessibilitySettings;
73import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000074import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070076import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.applications.ManageApplications;
78import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040079import com.android.settings.applications.UsageAccessDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080082import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070083import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070084import com.android.settings.dashboard.NoHomeDialogFragment;
85import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070086import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkey42833b22015-04-11 21:27:33 -070087import com.android.settings.deviceinfo.PublicVolumeSettings;
88import com.android.settings.deviceinfo.StorageSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040090import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080091import com.android.settings.fuelgauge.PowerUsageSummary;
92import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
93import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
94import com.android.settings.inputmethod.SpellCheckersSettings;
95import com.android.settings.inputmethod.UserDictionaryList;
96import com.android.settings.location.LocationSettings;
97import com.android.settings.nfc.AndroidBeam;
98import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040099import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400100import com.android.settings.notification.NotificationAccessSettings;
101import com.android.settings.notification.NotificationSettings;
102import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500103import com.android.settings.notification.OtherSoundSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400104import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400105import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400106import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400107import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400108import com.android.settings.notification.ZenModeScheduleRuleSettings;
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;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700116import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import com.android.settings.vpn2.VpnSettings;
118import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700119import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700121import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import com.android.settings.wifi.WifiSettings;
123import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500124
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125import org.xmlpull.v1.XmlPullParser;
126import org.xmlpull.v1.XmlPullParserException;
127
128import java.io.IOException;
129import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800130import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500131import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700132import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133
134public class SettingsActivity extends Activity
135 implements PreferenceManager.OnPreferenceTreeClickListener,
136 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100137 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100139 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800140
141 private static final String LOG_TAG = "Settings";
142
143 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700144 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700145 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
146 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700147 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700148 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700149 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800150
151 /**
152 * When starting this activity, the invoking Intent can contain this extra
153 * string to specify which fragment should be initially displayed.
154 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
155 * will call isValidFragment() to confirm that the fragment class name is valid for this
156 * activity.
157 */
158 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
159
160 /**
161 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
162 * this extra can also be specified to supply a Bundle of arguments to pass
163 * to that fragment when it is instantiated during the initial creation
164 * of the activity.
165 */
166 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
167
168 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700169 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
170 */
171 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
172
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800173 public static final String BACK_STACK_PREFS = ":settings:prefs";
174
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 // extras that allow any preference activity to be launched as part of a wizard
176
177 // show Back and Next buttons? takes boolean parameter
178 // Back will then return RESULT_CANCELED and Next RESULT_OK
179 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
180
181 // add a Skip button?
182 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
183
184 // specify custom text for the Back or Next buttons, or cause a button to not appear
185 // at all by setting it to null
186 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
187 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
188
189 /**
190 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700191 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800192 * that fragment.
193 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700194 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100195 /**
196 * The package name used to resolve the title resource id.
197 */
198 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
199 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700200 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
201 ":settings:show_fragment_title_resid";
202 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
203 ":settings:show_fragment_as_shortcut";
204
205 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
206 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208 private static final String META_DATA_KEY_FRAGMENT_CLASS =
209 "com.android.settings.FRAGMENT_CLASS";
210
211 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
212
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700213 private static final String EMPTY_QUERY = "";
214
Jason Monk2ebc8a02015-02-13 15:23:19 -0500215 /**
216 * Settings will search for system activities of this action and add them as a top level
217 * settings tile using the following parameters.
218 *
219 * <p>A category must be specified in the meta-data for the activity named
220 * {@link #EXTRA_CATEGORY_KEY}
221 *
222 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
223 * otherwise the label for the activity will be used.
224 *
225 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
226 * otherwise the icon for the activity will be used.
227 *
228 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
229 */
230 private static final String EXTRA_SETTINGS_ACTION =
231 "com.android.settings.action.EXTRA_SETTINGS";
232
233 /**
234 * The key used to get the category from metadata of activities of action
235 * {@link #EXTRA_SETTINGS_ACTION}
236 * The value must be one of:
237 * <li>com.android.settings.category.wireless</li>
238 * <li>com.android.settings.category.device</li>
239 * <li>com.android.settings.category.personal</li>
240 * <li>com.android.settings.category.system</li>
241 */
242 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
243
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244 private static boolean sShowNoHomeNotice = false;
245
246 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800248 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700249 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800250
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 // Show only these settings for restricted users
252 private int[] SETTINGS_FOR_RESTRICTED = {
253 R.id.wireless_section,
254 R.id.wifi_settings,
255 R.id.bluetooth_settings,
256 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700257 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 R.id.wireless_settings,
259 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400260 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 R.id.display_settings,
262 R.id.storage_settings,
263 R.id.application_settings,
264 R.id.battery_settings,
265 R.id.personal_section,
266 R.id.location_settings,
267 R.id.security_settings,
268 R.id.language_settings,
269 R.id.user_settings,
270 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 R.id.system_section,
272 R.id.date_time_settings,
273 R.id.about_settings,
274 R.id.accessibility_settings,
275 R.id.print_settings,
276 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800277 R.id.home_settings,
278 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279 };
280
281 private static final String[] ENTRY_FRAGMENTS = {
282 WirelessSettings.class.getName(),
283 WifiSettings.class.getName(),
284 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700285 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700287 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 TetherSettings.class.getName(),
289 WifiP2pSettings.class.getName(),
290 VpnSettings.class.getName(),
291 DateTimeSettings.class.getName(),
292 LocalePicker.class.getName(),
293 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700294 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800295 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(),
302 ProcessStatsUi.class.getName(),
303 NotificationStation.class.getName(),
304 LocationSettings.class.getName(),
305 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400306 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 PrivacySettings.class.getName(),
308 DeviceAdminSettings.class.getName(),
309 AccessibilitySettings.class.getName(),
310 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
312 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700313 StorageSettings.class.getName(),
314 PublicVolumeSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700315 PrivateVolumeForget.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 DevelopmentSettings.class.getName(),
317 UsbSettings.class.getName(),
318 AndroidBeam.class.getName(),
319 WifiDisplaySettings.class.getName(),
320 PowerUsageSummary.class.getName(),
321 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000322 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 CryptKeeperSettings.class.getName(),
324 DataUsageSummary.class.getName(),
325 DreamSettings.class.getName(),
326 UserSettings.class.getName(),
327 NotificationAccessSettings.class.getName(),
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(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 };
349
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700350
351 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
352 "android.settings.APPLICATION_DETAILS_SETTINGS"
353 };
354
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 private SharedPreferences mDevelopmentPreferences;
356 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
357
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 private boolean mBatteryPresent = true;
359 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 @Override
361 public void onReceive(Context context, Intent intent) {
362 String action = intent.getAction();
363 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
364 boolean batteryPresent = Utils.isBatteryPresent(intent);
365
366 if (mBatteryPresent != batteryPresent) {
367 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700368 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 }
370 }
371 }
372 };
373
Svetoslav990159a2014-04-14 17:14:59 -0700374 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
375 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700376
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700377 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700378 private SwitchBar mSwitchBar;
379
380 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700381
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700382 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700383 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700384
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700385 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700386 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700387
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700388 private ViewGroup mContent;
389
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700390 private SearchView mSearchView;
391 private MenuItem mSearchMenuItem;
392 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700393 private SearchResultsSummary mSearchResultsFragment;
394 private String mSearchQuery;
395
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700396 // Categories
397 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800398
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700399 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700400 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401 private Handler mHandler = new Handler() {
402 @Override
403 public void handleMessage(Message msg) {
404 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700405 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700406 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
407 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700408 buildDashboardCategories(mCategories);
409 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 } break;
411 }
412 }
413 };
414
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700415 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700416 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700417
Jim Miller0698a212014-10-16 19:49:07 -0700418 private Intent mResultIntentData;
419
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700420 public SwitchBar getSwitchBar() {
421 return mSwitchBar;
422 }
423
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700424 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
425 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700426 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700427 }
428 return mCategories;
429 }
430
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800431 @Override
432 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
433 // Override the fragment title for Wallpaper settings
434 int titleRes = pref.getTitleRes();
435 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
436 titleRes = R.string.wallpaper_settings_fragment_title;
437 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
438 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
439 if (UserManager.get(this).isLinkedUser()) {
440 titleRes = R.string.profile_info_settings_title;
441 } else {
442 titleRes = R.string.user_info_settings_title;
443 }
444 }
445 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
446 null, 0);
447 return true;
448 }
449
450 @Override
451 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
452 return false;
453 }
454
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700455 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700456 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700457 Message msg = new Message();
458 msg.what = MSG_BUILD_CATEGORIES;
459 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800460 }
461 }
462
463 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 public void onConfigurationChanged(Configuration newConfig) {
465 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800466 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800467 }
468
469 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700470 protected void onStart() {
471 super.onStart();
472
473 if (mNeedToRevertToInitialFragment) {
474 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800475 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
477
478 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700479 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700480 if (!mDisplaySearch) {
481 return false;
482 }
483
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700484 MenuInflater inflater = getMenuInflater();
485 inflater.inflate(R.menu.options_menu, menu);
486
487 // Cache the search query (can be overriden by the OnQueryTextListener)
488 final String query = mSearchQuery;
489
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700490 mSearchMenuItem = menu.findItem(R.id.search);
491 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700492
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700493 if (mSearchMenuItem == null || mSearchView == null) {
494 return false;
495 }
496
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700497 if (mSearchResultsFragment != null) {
498 mSearchResultsFragment.setSearchView(mSearchView);
499 }
500
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700501 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700502 mSearchView.setOnQueryTextListener(this);
503 mSearchView.setOnCloseListener(this);
504
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700505 if (mSearchMenuItemExpanded) {
506 mSearchMenuItem.expandActionView();
507 }
508 mSearchView.setQuery(query, true /* submit */);
509
510 return true;
511 }
512
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700513 private static boolean isShortCutIntent(final Intent intent) {
514 Set<String> categories = intent.getCategories();
515 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
516 }
517
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700518 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700519 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700520 if (action == null) {
521 return false;
522 }
523 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
524 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
525 }
526 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700527 }
528
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700529 @Override
530 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700531 super.onCreate(savedState);
532
533 // Should happen before any call to getIntent()
534 getMetaData();
535
536 final Intent intent = getIntent();
537 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
538 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
542 Context.MODE_PRIVATE);
543
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700544 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700545 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700546
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700547 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700548 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
549
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700550 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700551 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700552
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700553 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700554
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700555 // This is a "Sub Settings" when:
556 // - this is a real SubSettings
557 // - or :settings:show_fragment_as_subsetting is passed to the Intent
558 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
559 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
560
561 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
562 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700563 // Check also that we are not a Theme Dialog as we don't want to override them
564 final int themeResId = getThemeResId();
565 if (themeResId != R.style.Theme_DialogWhenLarge &&
566 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
567 setTheme(R.style.Theme_SubSettings);
568 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700569 }
570
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700571 setContentView(mIsShowingDashboard ?
572 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700574 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700575
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 getFragmentManager().addOnBackStackChangedListener(this);
577
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700578 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800579 // Run the Index update only if we have some space
580 if (!Utils.isLowStorage(this)) {
581 Index.getInstance(getApplicationContext()).update();
582 } else {
583 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
584 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700585 }
586
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700587 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700588 // We are restarting from a previous saved state; used that to initialize, instead
589 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700590 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
591 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800592
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700593 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800594
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700595 ArrayList<DashboardCategory> categories =
596 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
597 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700598 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700599 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700600 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700602
603 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700604 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700605 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
606 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800607 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700608 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700609 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
610 // only if it is a sub settings
611 if (mIsShortcut) {
612 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700613 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700614 } else if (isSubSettings) {
615 mDisplayHomeAsUpEnabled = true;
616 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700617 } else {
618 mDisplayHomeAsUpEnabled = false;
619 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700620 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700621 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700622
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700623 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700624 switchToFragment(initialFragmentName, initialArguments, true, false,
625 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000626 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700627 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700628 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700629 // Show Search affordance
630 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700631 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700632 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700633 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800634 }
635 }
636
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700637 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700638 if (mActionBar != null) {
639 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
640 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
641 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700642 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
643
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800644 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
646
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700647 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 if (buttonBar != null) {
649 buttonBar.setVisibility(View.VISIBLE);
650
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700651 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 backButton.setOnClickListener(new OnClickListener() {
653 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700654 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 finish();
656 }
657 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700658 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 skipButton.setOnClickListener(new OnClickListener() {
660 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700661 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800662 finish();
663 }
664 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700665 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800666 mNextButton.setOnClickListener(new OnClickListener() {
667 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700668 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 finish();
670 }
671 });
672
673 // set our various button parameters
674 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
675 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
676 if (TextUtils.isEmpty(buttonText)) {
677 mNextButton.setVisibility(View.GONE);
678 }
679 else {
680 mNextButton.setText(buttonText);
681 }
682 }
683 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
684 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
685 if (TextUtils.isEmpty(buttonText)) {
686 backButton.setVisibility(View.GONE);
687 }
688 else {
689 backButton.setText(buttonText);
690 }
691 }
692 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
693 skipButton.setVisibility(View.VISIBLE);
694 }
695 }
696 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700697
698 mHomeActivitiesCount = getHomeActivitiesCount();
699 }
700
701 private int getHomeActivitiesCount() {
702 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
703 getPackageManager().getHomeActivities(homeApps);
704 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800705 }
706
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700707 private void setTitleFromIntent(Intent intent) {
708 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
709 if (initialTitleResId > 0) {
710 mInitialTitle = null;
711 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100712
713 final String initialTitleResPackageName = intent.getStringExtra(
714 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
715 if (initialTitleResPackageName != null) {
716 try {
717 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
718 0 /* flags */, new UserHandle(UserHandle.myUserId()));
719 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
720 setTitle(mInitialTitle);
721 mInitialTitleResId = -1;
722 return;
723 } catch (NameNotFoundException e) {
724 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
725 }
726 } else {
727 setTitle(mInitialTitleResId);
728 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700729 } else {
730 mInitialTitleResId = -1;
731 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
732 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
733 setTitle(mInitialTitle);
734 }
735 }
736
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800737 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800738 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700739 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800740 }
741
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700742 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800743 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700744
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800745 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700746 if (mInitialTitleResId > 0) {
747 setTitle(mInitialTitleResId);
748 } else {
749 setTitle(mInitialTitle);
750 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700751 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800752 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700753
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800754 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
755 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700756
757 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800758 }
759
760 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
761 final CharSequence title;
762 final int titleRes = bse.getBreadCrumbTitleRes();
763 if (titleRes > 0) {
764 title = getText(titleRes);
765 } else {
766 title = bse.getBreadCrumbTitle();
767 }
768 if (title != null) {
769 setTitle(title);
770 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800771 }
772
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800773 @Override
774 protected void onSaveInstanceState(Bundle outState) {
775 super.onSaveInstanceState(outState);
776
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700777 if (mCategories.size() > 0) {
778 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800779 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700780
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700781 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700782 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700783
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700784 if (mDisplaySearch) {
785 // The option menus are created if the ActionBar is visible and they are also created
786 // asynchronously. If you launch Settings with an Intent action like
787 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
788 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
789 // menu item and search view are null.
790 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
791 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700792
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700793 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
794 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
795 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700796
797 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800798 }
799
800 @Override
801 public void onResume() {
802 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400803 if (mIsShowingDashboard) {
804 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
805 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800806
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700807 final int newHomeActivityCount = getHomeActivitiesCount();
808 if (newHomeActivityCount != mHomeActivitiesCount) {
809 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700810 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700811 }
812
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800813 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
814 @Override
815 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700816 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800817 }
818 };
819 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
820 mDevelopmentPreferencesListener);
821
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800822 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700823
Svetoslav990159a2014-04-14 17:14:59 -0700824 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700825
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700826 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700827 onQueryTextSubmit(mSearchQuery);
828 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800829 }
830
831 @Override
832 public void onPause() {
833 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400834 if (mIsShowingDashboard) {
835 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
836 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800837 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700838 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800839 }
840
841 @Override
842 public void onDestroy() {
843 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700844
845 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
846 mDevelopmentPreferencesListener);
847 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800848 }
849
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800850 protected boolean isValidFragment(String fragmentName) {
851 // Almost all fragments are wrapped in this,
852 // except for a few that have their own activities.
853 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
854 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
855 }
856 return false;
857 }
858
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800859 @Override
860 public Intent getIntent() {
861 Intent superIntent = super.getIntent();
862 String startingFragment = getStartingFragmentClass(superIntent);
863 // This is called from super.onCreate, isMultiPane() is not yet reliable
864 // Do not use onIsHidingHeaders either, which relies itself on this method
865 if (startingFragment != null) {
866 Intent modIntent = new Intent(superIntent);
867 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
868 Bundle args = superIntent.getExtras();
869 if (args != null) {
870 args = new Bundle(args);
871 } else {
872 args = new Bundle();
873 }
874 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100875 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800876 return modIntent;
877 }
878 return superIntent;
879 }
880
881 /**
882 * Checks if the component name in the intent is different from the Settings class and
883 * returns the class name to load as a fragment.
884 */
885 private String getStartingFragmentClass(Intent intent) {
886 if (mFragmentClass != null) return mFragmentClass;
887
888 String intentClass = intent.getComponent().getClassName();
889 if (intentClass.equals(getClass().getName())) return null;
890
891 if ("com.android.settings.ManageApplications".equals(intentClass)
892 || "com.android.settings.RunningServices".equals(intentClass)
893 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
894 // Old names of manage apps.
895 intentClass = com.android.settings.applications.ManageApplications.class.getName();
896 }
897
898 return intentClass;
899 }
900
901 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000902 * Start a new fragment containing a preference panel. If the preferences
903 * are being displayed in multi-pane mode, the given fragment class will
904 * be instantiated and placed in the appropriate pane. If running in
905 * single-pane mode, a new activity will be launched in which to show the
906 * fragment.
907 *
908 * @param fragmentClass Full name of the class implementing the fragment.
909 * @param args Any desired arguments to supply to the fragment.
910 * @param titleRes Optional resource identifier of the title of this
911 * fragment.
912 * @param titleText Optional text of the title of this fragment.
913 * @param resultTo Optional fragment that result data should be sent to.
914 * If non-null, resultTo.onActivityResult() will be called when this
915 * preference panel is done. The launched panel must use
916 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
917 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700918 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000919 */
920 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700921 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700922 String title = null;
923 if (titleRes < 0) {
924 if (titleText != null) {
925 title = titleText.toString();
926 } else {
927 // There not much we can do in that case
928 title = "";
929 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700930 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700931 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700932 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000933 }
934
935 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100936 * Start a new fragment in a new activity containing a preference panel for a given user. If the
937 * preferences are being displayed in multi-pane mode, the given fragment class will be
938 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
939 * activity will be launched in which to show the fragment.
940 *
941 * @param fragmentClass Full name of the class implementing the fragment.
942 * @param args Any desired arguments to supply to the fragment.
943 * @param titleRes Optional resource identifier of the title of this fragment.
944 * @param titleText Optional text of the title of this fragment.
945 * @param userHandle The user for which the panel has to be started.
946 */
947 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
948 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700949 // This is a workaround.
950 //
951 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
952 // starting the fragment could cause a native stack corruption. See b/17523189. However,
953 // adding that flag and start the preference panel with the same UserHandler will make it
954 // impossible to use back button to return to the previous screen. See b/20042570.
955 //
956 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
957 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
958 // when we're calling it as the same user.
959 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
960 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
961 } else {
962 String title = null;
963 if (titleRes < 0) {
964 if (titleText != null) {
965 title = titleText.toString();
966 } else {
967 // There not much we can do in that case
968 title = "";
969 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100970 }
Lifu Tangd0332852015-04-02 12:05:46 -0700971 Utils.startWithFragmentAsUser(this, fragmentClass, args,
972 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100973 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100974 }
975
976 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800977 * Called by a preference panel fragment to finish itself.
978 *
979 * @param caller The fragment that is asking to be finished.
980 * @param resultCode Optional result code to send back to the original
981 * launching fragment.
982 * @param resultData Optional result data to send back to the original
983 * launching fragment.
984 */
985 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
986 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700987 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800988 }
989
990 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000991 * Start a new fragment.
992 *
993 * @param fragment The fragment to start
994 * @param push If true, the current fragment will be pushed onto the back stack. If false,
995 * the current fragment will be replaced.
996 */
997 public void startPreferenceFragment(Fragment fragment, boolean push) {
998 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700999 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001000 if (push) {
1001 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1002 transaction.addToBackStack(BACK_STACK_PREFS);
1003 } else {
1004 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1005 }
1006 transaction.commitAllowingStateLoss();
1007 }
1008
1009 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001010 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1011 */
1012 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001013 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001014 if (validate && !isValidFragment(fragmentName)) {
1015 throw new IllegalArgumentException("Invalid fragment for this activity: "
1016 + fragmentName);
1017 }
1018 Fragment f = Fragment.instantiate(this, fragmentName, args);
1019 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001020 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001021 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001022 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001023 }
1024 if (addToBackStack) {
1025 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1026 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001027 if (titleResId > 0) {
1028 transaction.setBreadCrumbTitle(titleResId);
1029 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001030 transaction.setBreadCrumbTitle(title);
1031 }
1032 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001033 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001034 return f;
1035 }
1036
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001037 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001038 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001039 *
1040 * @param categories The list in which to place the tiles categories.
1041 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001042 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001043 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001044 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1045 updateTilesList(categories);
1046 }
1047
1048 /**
1049 * Parse the given XML file as a categories description, adding each
1050 * parsed categories and tiles into the target list.
1051 *
1052 * @param resid The XML resource to load and parse.
1053 * @param target The list in which the parsed categories and tiles should be placed.
1054 */
1055 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1056 XmlResourceParser parser = null;
1057 try {
1058 parser = getResources().getXml(resid);
1059 AttributeSet attrs = Xml.asAttributeSet(parser);
1060
1061 int type;
1062 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1063 && type != XmlPullParser.START_TAG) {
1064 // Parse next until start tag is found
1065 }
1066
1067 String nodeName = parser.getName();
1068 if (!"dashboard-categories".equals(nodeName)) {
1069 throw new RuntimeException(
1070 "XML document must start with <preference-categories> tag; found"
1071 + nodeName + " at " + parser.getPositionDescription());
1072 }
1073
1074 Bundle curBundle = null;
1075
1076 final int outerDepth = parser.getDepth();
1077 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1078 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1079 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1080 continue;
1081 }
1082
1083 nodeName = parser.getName();
1084 if ("dashboard-category".equals(nodeName)) {
1085 DashboardCategory category = new DashboardCategory();
1086
1087 TypedArray sa = obtainStyledAttributes(
1088 attrs, com.android.internal.R.styleable.PreferenceHeader);
1089 category.id = sa.getResourceId(
1090 com.android.internal.R.styleable.PreferenceHeader_id,
1091 (int)DashboardCategory.CAT_ID_UNDEFINED);
1092
1093 TypedValue tv = sa.peekValue(
1094 com.android.internal.R.styleable.PreferenceHeader_title);
1095 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1096 if (tv.resourceId != 0) {
1097 category.titleRes = tv.resourceId;
1098 } else {
1099 category.title = tv.string;
1100 }
1101 }
1102 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001103 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1104 tv = sa.peekValue(
1105 com.android.internal.R.styleable.Preference_key);
1106 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1107 if (tv.resourceId != 0) {
1108 category.key = getString(tv.resourceId);
1109 } else {
1110 category.key = tv.string.toString();
1111 }
1112 }
1113 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001114
1115 final int innerDepth = parser.getDepth();
1116 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1117 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1118 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1119 continue;
1120 }
1121
1122 String innerNodeName = parser.getName();
1123 if (innerNodeName.equals("dashboard-tile")) {
1124 DashboardTile tile = new DashboardTile();
1125
1126 sa = obtainStyledAttributes(
1127 attrs, com.android.internal.R.styleable.PreferenceHeader);
1128 tile.id = sa.getResourceId(
1129 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001130 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001131 tv = sa.peekValue(
1132 com.android.internal.R.styleable.PreferenceHeader_title);
1133 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1134 if (tv.resourceId != 0) {
1135 tile.titleRes = tv.resourceId;
1136 } else {
1137 tile.title = tv.string;
1138 }
1139 }
1140 tv = sa.peekValue(
1141 com.android.internal.R.styleable.PreferenceHeader_summary);
1142 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1143 if (tv.resourceId != 0) {
1144 tile.summaryRes = tv.resourceId;
1145 } else {
1146 tile.summary = tv.string;
1147 }
1148 }
1149 tile.iconRes = sa.getResourceId(
1150 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1151 tile.fragment = sa.getString(
1152 com.android.internal.R.styleable.PreferenceHeader_fragment);
1153 sa.recycle();
1154
1155 if (curBundle == null) {
1156 curBundle = new Bundle();
1157 }
1158
1159 final int innerDepth2 = parser.getDepth();
1160 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1161 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1162 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1163 continue;
1164 }
1165
1166 String innerNodeName2 = parser.getName();
1167 if (innerNodeName2.equals("extra")) {
1168 getResources().parseBundleExtra("extra", attrs, curBundle);
1169 XmlUtils.skipCurrentTag(parser);
1170
1171 } else if (innerNodeName2.equals("intent")) {
1172 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1173
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.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001185 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
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
1216 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1217
1218 final int size = target.size();
1219 for (int i = 0; i < size; i++) {
1220
1221 DashboardCategory category = target.get(i);
1222
1223 // Ids are integers, so downcasting is ok
1224 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001225 int n = category.getTilesCount() - 1;
1226 while (n >= 0) {
1227
1228 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001229 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001230 id = (int) tile.id;
1231 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001232 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1233 removeTile = true;
1234 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001235 } else if (id == R.id.wifi_settings) {
1236 // Remove WiFi Settings if WiFi service is not available.
1237 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001238 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001239 }
1240 } else if (id == R.id.bluetooth_settings) {
1241 // Remove Bluetooth Settings if Bluetooth service is not available.
1242 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001243 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001244 }
1245 } else if (id == R.id.data_usage_settings) {
1246 // Remove data usage when kernel module not enabled
1247 final INetworkManagementService netManager = INetworkManagementService.Stub
1248 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1249 try {
1250 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001251 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001252 }
1253 } catch (RemoteException e) {
1254 // ignored
1255 }
1256 } else if (id == R.id.battery_settings) {
1257 // Remove battery settings when battery is not available. (e.g. TV)
1258
1259 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001260 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001261 }
1262 } else if (id == R.id.home_settings) {
1263 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001264 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 }
1266 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001267 boolean hasMultipleUsers =
1268 ((UserManager) getSystemService(Context.USER_SERVICE))
1269 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001270 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001271 || (!UserManager.supportsMultipleUsers()
1272 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001273 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001274 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001275 }
1276 } else if (id == R.id.nfc_payment_settings) {
1277 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001278 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001279 } else {
1280 // Only show if NFC is on and we have the HCE feature
1281 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001282 if (adapter == null || !adapter.isEnabled() ||
1283 !getPackageManager().hasSystemFeature(
1284 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001285 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001286 }
1287 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001288 } else if (id == R.id.print_settings) {
1289 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1290 PackageManager.FEATURE_PRINTING);
1291 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001292 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001293 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001294 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001295 if (!showDev || um.hasUserRestriction(
1296 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001297 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001298 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001299 }
1300
1301 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001302 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1303 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001304 }
1305
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001306 if (removeTile && n < category.getTilesCount()) {
1307 category.removeTile(n);
1308 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001309 n--;
1310 }
1311 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001312 addExternalTiles(target);
1313 }
1314
1315 private void addExternalTiles(List<DashboardCategory> target) {
1316 Map<Pair<String, String>, DashboardTile> addedCache =
1317 new ArrayMap<Pair<String, String>, DashboardTile>();
1318 UserManager userManager = UserManager.get(this);
1319 for (UserHandle user : userManager.getUserProfiles()) {
1320 addExternalTiles(target, user, addedCache);
1321 }
1322 }
1323
1324 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1325 Map<Pair<String, String>, DashboardTile> addedCache) {
1326 PackageManager pm = getPackageManager();
1327 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1328 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1329 PackageManager.GET_META_DATA, user.getIdentifier());
1330 for (ResolveInfo resolved : results) {
1331 if (!resolved.system) {
1332 // Do not allow any app to add to settings, only system ones.
1333 continue;
1334 }
1335 ActivityInfo activityInfo = resolved.activityInfo;
1336 Bundle metaData = activityInfo.metaData;
1337 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1338 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1339 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1340 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1341 continue;
1342 }
1343 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1344 DashboardCategory category = getCategory(target, categoryKey);
1345 if (category == null) {
1346 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1347 + "category key " + categoryKey);
1348 continue;
1349 }
1350 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1351 activityInfo.name);
1352 DashboardTile tile = addedCache.get(key);
1353 if (tile == null) {
1354 tile = new DashboardTile();
1355 tile.intent = new Intent().setClassName(
1356 activityInfo.packageName, activityInfo.name);
1357 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1358
1359 if (category.externalIndex == -1) {
1360 // If no location for external tiles has been specified for this category,
1361 // then just put them at the end.
1362 category.addTile(tile);
1363 } else {
1364 category.addTile(category.externalIndex, tile);
1365 }
1366 addedCache.put(key, tile);
1367 }
1368 tile.userHandle.add(user);
1369 }
1370 }
1371
1372 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1373 for (DashboardCategory category : target) {
1374 if (categoryKey.equals(category.key)) {
1375 return category;
1376 }
1377 }
1378 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001379 }
1380
1381 private boolean updateHomeSettingTiles(DashboardTile tile) {
1382 // Once we decide to show Home settings, keep showing it forever
1383 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1384 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1385 return true;
1386 }
1387
1388 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001389 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001390 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001391 // When there's only one available home app, omit this settings
1392 // category entirely at the top level UI. If the user just
1393 // uninstalled the penultimate home app candidiate, we also
1394 // now tell them about why they aren't seeing 'Home' in the list.
1395 if (sShowNoHomeNotice) {
1396 sShowNoHomeNotice = false;
1397 NoHomeDialogFragment.show(this);
1398 }
1399 return false;
1400 } else {
1401 // Okay, we're allowing the Home settings category. Tell it, when
1402 // invoked via this front door, that we'll need to be told about the
1403 // case when the user uninstalls all but one home app.
1404 if (tile.fragmentArguments == null) {
1405 tile.fragmentArguments = new Bundle();
1406 }
1407 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1408 }
1409 } catch (Exception e) {
1410 // Can't look up the home activity; bail on configuring the icon
1411 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1412 }
1413
1414 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1415 return true;
1416 }
1417
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001418 private void getMetaData() {
1419 try {
1420 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1421 PackageManager.GET_META_DATA);
1422 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001423 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1424 } catch (NameNotFoundException nnfe) {
1425 // No recovery
1426 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1427 }
1428 }
1429
1430 // give subclasses access to the Next button
1431 public boolean hasNextButton() {
1432 return mNextButton != null;
1433 }
1434
1435 public Button getNextButton() {
1436 return mNextButton;
1437 }
1438
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001439 @Override
1440 public boolean shouldUpRecreateTask(Intent targetIntent) {
1441 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1442 }
1443
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001444 public static void requestHomeNotice() {
1445 sShowNoHomeNotice = true;
1446 }
1447
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001448 @Override
1449 public boolean onQueryTextSubmit(String query) {
1450 switchToSearchResultsFragmentIfNeeded();
1451 mSearchQuery = query;
1452 return mSearchResultsFragment.onQueryTextSubmit(query);
1453 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001454
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001455 @Override
1456 public boolean onQueryTextChange(String newText) {
1457 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001458 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001459 return false;
1460 }
1461 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001462 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001463
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001464 @Override
1465 public boolean onClose() {
1466 return false;
1467 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001468
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001469 @Override
1470 public boolean onMenuItemActionExpand(MenuItem item) {
1471 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001472 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001474 return true;
1475 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001477 @Override
1478 public boolean onMenuItemActionCollapse(MenuItem item) {
1479 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001480 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001481 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001482 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001483 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001484 return true;
1485 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001486
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001487 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001488 if (mSearchResultsFragment != null) {
1489 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001490 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001491 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 if (current != null && current instanceof SearchResultsSummary) {
1493 mSearchResultsFragment = (SearchResultsSummary) current;
1494 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001495 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001496 SearchResultsSummary.class.getName(), null, false, true,
1497 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001498 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001499 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001500 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001501 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001502
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001503 public void needToRevertToInitialFragment() {
1504 mNeedToRevertToInitialFragment = true;
1505 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001506
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001507 private void revertToInitialFragment() {
1508 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001509 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001510 mSearchMenuItemExpanded = false;
1511 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1512 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001513 if (mSearchMenuItem != null) {
1514 mSearchMenuItem.collapseActionView();
1515 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001516 }
Jim Miller0698a212014-10-16 19:49:07 -07001517
1518 public Intent getResultIntentData() {
1519 return mResultIntentData;
1520 }
1521
1522 public void setResultIntentData(Intent resultIntentData) {
1523 mResultIntentData = resultIntentData;
1524 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001525}