blob: 502f164f6cf31f729c77e4925446403c51944ee9 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason Monk2ebc8a02015-02-13 15:23:19 -050019import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
20
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.ActionBar;
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.nfc.NfcAdapter;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.INetworkManagementService;
43import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.preference.Preference;
49import android.preference.PreferenceFragment;
50import android.preference.PreferenceManager;
51import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.AttributeSet;
56import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050057import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.util.TypedValue;
59import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070060import android.view.Menu;
61import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.view.MenuItem;
63import android.view.View;
64import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070065import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080066import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070067import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040068
Chris Wren8a963ba2015-03-20 10:29:14 -040069import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.internal.util.ArrayUtils;
71import com.android.internal.util.XmlUtils;
72import com.android.settings.accessibility.AccessibilitySettings;
73import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000074import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070076import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.applications.ManageApplications;
78import com.android.settings.applications.ProcessStatsUi;
Jason Monkd8da51c2015-04-17 14:34:12 -040079import com.android.settings.applications.UsageAccessDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080082import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070083import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070084import com.android.settings.dashboard.NoHomeDialogFragment;
85import com.android.settings.dashboard.SearchResultsSummary;
Jeff Sharkeya16257d2015-04-28 13:41:01 -070086import com.android.settings.deviceinfo.PrivateVolumeForget;
Jeff Sharkeye77f0682015-04-29 11:24:57 -070087import com.android.settings.deviceinfo.PrivateVolumeSettings;
Jeff Sharkey42833b22015-04-11 21:27:33 -070088import com.android.settings.deviceinfo.PublicVolumeSettings;
89import com.android.settings.deviceinfo.StorageSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040091import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080092import com.android.settings.fuelgauge.PowerUsageSummary;
93import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
94import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
95import com.android.settings.inputmethod.SpellCheckersSettings;
96import com.android.settings.inputmethod.UserDictionaryList;
97import com.android.settings.location.LocationSettings;
98import com.android.settings.nfc.AndroidBeam;
99import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -0400100import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400101import com.android.settings.notification.NotificationAccessSettings;
102import com.android.settings.notification.NotificationSettings;
103import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500104import com.android.settings.notification.OtherSoundSettings;
John Spurlockf57bad72015-04-30 09:26:15 -0400105import com.android.settings.notification.ZenModeEventRuleSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400106import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400107import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400108import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400109import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.print.PrintJobSettingsFragment;
111import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500112import com.android.settings.search.DynamicIndexableContentMonitor;
113import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700114import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import com.android.settings.tts.TextToSpeechSettings;
116import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700117import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import com.android.settings.vpn2.VpnSettings;
119import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700120import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700122import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123import com.android.settings.wifi.WifiSettings;
124import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500125
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126import org.xmlpull.v1.XmlPullParser;
127import org.xmlpull.v1.XmlPullParserException;
128
129import java.io.IOException;
130import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500132import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700133import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800134
135public 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(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700295 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 SpellCheckersSettings.class.getName(),
297 UserDictionaryList.class.getName(),
298 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700299 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 DisplaySettings.class.getName(),
301 DeviceInfoSettings.class.getName(),
302 ManageApplications.class.getName(),
303 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(),
319 UsbSettings.class.getName(),
320 AndroidBeam.class.getName(),
321 WifiDisplaySettings.class.getName(),
322 PowerUsageSummary.class.getName(),
323 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000324 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 CryptKeeperSettings.class.getName(),
326 DataUsageSummary.class.getName(),
327 DreamSettings.class.getName(),
328 UserSettings.class.getName(),
329 NotificationAccessSettings.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 Spurlock45fa1402015-04-09 12:50:04 -0400346 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockf57bad72015-04-30 09:26:15 -0400347 ZenModeEventRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400348 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400349 ProcessStatsUi.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 };
351
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700352
353 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
354 "android.settings.APPLICATION_DETAILS_SETTINGS"
355 };
356
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 private SharedPreferences mDevelopmentPreferences;
358 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
359
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 private boolean mBatteryPresent = true;
361 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 @Override
363 public void onReceive(Context context, Intent intent) {
364 String action = intent.getAction();
365 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
366 boolean batteryPresent = Utils.isBatteryPresent(intent);
367
368 if (mBatteryPresent != batteryPresent) {
369 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700370 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800371 }
372 }
373 }
374 };
375
Svetoslav990159a2014-04-14 17:14:59 -0700376 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
377 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700378
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700379 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700380 private SwitchBar mSwitchBar;
381
382 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700383
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700384 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700385 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700386
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700387 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700388 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700389
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700390 private ViewGroup mContent;
391
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700392 private SearchView mSearchView;
393 private MenuItem mSearchMenuItem;
394 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700395 private SearchResultsSummary mSearchResultsFragment;
396 private String mSearchQuery;
397
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700398 // Categories
399 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800400
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700401 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700402 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800403 private Handler mHandler = new Handler() {
404 @Override
405 public void handleMessage(Message msg) {
406 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700407 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700408 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
409 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700410 buildDashboardCategories(mCategories);
411 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800412 } break;
413 }
414 }
415 };
416
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700417 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700418 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700419
Jim Miller0698a212014-10-16 19:49:07 -0700420 private Intent mResultIntentData;
421
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700422 public SwitchBar getSwitchBar() {
423 return mSwitchBar;
424 }
425
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700426 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
427 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700428 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700429 }
430 return mCategories;
431 }
432
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800433 @Override
434 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
435 // Override the fragment title for Wallpaper settings
436 int titleRes = pref.getTitleRes();
437 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
438 titleRes = R.string.wallpaper_settings_fragment_title;
439 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
440 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
441 if (UserManager.get(this).isLinkedUser()) {
442 titleRes = R.string.profile_info_settings_title;
443 } else {
444 titleRes = R.string.user_info_settings_title;
445 }
446 }
447 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
448 null, 0);
449 return true;
450 }
451
452 @Override
453 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
454 return false;
455 }
456
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700457 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700458 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700459 Message msg = new Message();
460 msg.what = MSG_BUILD_CATEGORIES;
461 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800462 }
463 }
464
465 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 public void onConfigurationChanged(Configuration newConfig) {
467 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800468 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800469 }
470
471 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700472 protected void onStart() {
473 super.onStart();
474
475 if (mNeedToRevertToInitialFragment) {
476 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800477 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 }
479
480 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700481 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700482 if (!mDisplaySearch) {
483 return false;
484 }
485
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700486 MenuInflater inflater = getMenuInflater();
487 inflater.inflate(R.menu.options_menu, menu);
488
489 // Cache the search query (can be overriden by the OnQueryTextListener)
490 final String query = mSearchQuery;
491
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700492 mSearchMenuItem = menu.findItem(R.id.search);
493 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700494
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700495 if (mSearchMenuItem == null || mSearchView == null) {
496 return false;
497 }
498
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700499 if (mSearchResultsFragment != null) {
500 mSearchResultsFragment.setSearchView(mSearchView);
501 }
502
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700503 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700504 mSearchView.setOnQueryTextListener(this);
505 mSearchView.setOnCloseListener(this);
506
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700507 if (mSearchMenuItemExpanded) {
508 mSearchMenuItem.expandActionView();
509 }
510 mSearchView.setQuery(query, true /* submit */);
511
512 return true;
513 }
514
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700515 private static boolean isShortCutIntent(final Intent intent) {
516 Set<String> categories = intent.getCategories();
517 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
518 }
519
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700520 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700521 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700522 if (action == null) {
523 return false;
524 }
525 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
526 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
527 }
528 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700529 }
530
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700531 @Override
532 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700533 super.onCreate(savedState);
534
535 // Should happen before any call to getIntent()
536 getMetaData();
537
538 final Intent intent = getIntent();
539 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
540 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
544 Context.MODE_PRIVATE);
545
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700546 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700547 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700548
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700549 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700550 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
551
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700552 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700553 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700554
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700555 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700556
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700557 // This is a "Sub Settings" when:
558 // - this is a real SubSettings
559 // - or :settings:show_fragment_as_subsetting is passed to the Intent
560 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
561 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
562
563 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
564 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700565 // Check also that we are not a Theme Dialog as we don't want to override them
566 final int themeResId = getThemeResId();
567 if (themeResId != R.style.Theme_DialogWhenLarge &&
568 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
569 setTheme(R.style.Theme_SubSettings);
570 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700571 }
572
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700573 setContentView(mIsShowingDashboard ?
574 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700576 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700577
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800578 getFragmentManager().addOnBackStackChangedListener(this);
579
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700580 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800581 // Run the Index update only if we have some space
582 if (!Utils.isLowStorage(this)) {
583 Index.getInstance(getApplicationContext()).update();
584 } else {
585 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
586 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700587 }
588
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700589 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700590 // We are restarting from a previous saved state; used that to initialize, instead
591 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700592 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
593 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700595 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800596
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700597 ArrayList<DashboardCategory> categories =
598 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
599 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700600 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700601 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700602 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800603 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700604
605 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700606 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700607 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
608 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800609 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700610 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700611 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
612 // only if it is a sub settings
613 if (mIsShortcut) {
614 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700615 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700616 } else if (isSubSettings) {
617 mDisplayHomeAsUpEnabled = true;
618 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700619 } else {
620 mDisplayHomeAsUpEnabled = false;
621 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700622 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700623 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700624
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700625 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700626 switchToFragment(initialFragmentName, initialArguments, true, false,
627 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000628 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700629 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700630 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700631 // Show Search affordance
632 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700633 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700634 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700635 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800636 }
637 }
638
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700639 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700640 if (mActionBar != null) {
641 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
642 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
643 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700644 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
645
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
648
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700649 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 if (buttonBar != null) {
651 buttonBar.setVisibility(View.VISIBLE);
652
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700653 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 backButton.setOnClickListener(new OnClickListener() {
655 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700656 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 finish();
658 }
659 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700660 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 skipButton.setOnClickListener(new OnClickListener() {
662 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700663 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800664 finish();
665 }
666 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700667 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800668 mNextButton.setOnClickListener(new OnClickListener() {
669 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700670 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800671 finish();
672 }
673 });
674
675 // set our various button parameters
676 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
677 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
678 if (TextUtils.isEmpty(buttonText)) {
679 mNextButton.setVisibility(View.GONE);
680 }
681 else {
682 mNextButton.setText(buttonText);
683 }
684 }
685 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
686 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
687 if (TextUtils.isEmpty(buttonText)) {
688 backButton.setVisibility(View.GONE);
689 }
690 else {
691 backButton.setText(buttonText);
692 }
693 }
694 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
695 skipButton.setVisibility(View.VISIBLE);
696 }
697 }
698 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700699
700 mHomeActivitiesCount = getHomeActivitiesCount();
701 }
702
703 private int getHomeActivitiesCount() {
704 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
705 getPackageManager().getHomeActivities(homeApps);
706 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800707 }
708
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700709 private void setTitleFromIntent(Intent intent) {
710 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
711 if (initialTitleResId > 0) {
712 mInitialTitle = null;
713 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100714
715 final String initialTitleResPackageName = intent.getStringExtra(
716 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
717 if (initialTitleResPackageName != null) {
718 try {
719 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
720 0 /* flags */, new UserHandle(UserHandle.myUserId()));
721 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
722 setTitle(mInitialTitle);
723 mInitialTitleResId = -1;
724 return;
725 } catch (NameNotFoundException e) {
726 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
727 }
728 } else {
729 setTitle(mInitialTitleResId);
730 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700731 } else {
732 mInitialTitleResId = -1;
733 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
734 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
735 setTitle(mInitialTitle);
736 }
737 }
738
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800739 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800740 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700741 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800742 }
743
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700744 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800745 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700746
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800747 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700748 if (mInitialTitleResId > 0) {
749 setTitle(mInitialTitleResId);
750 } else {
751 setTitle(mInitialTitle);
752 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700753 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800754 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700755
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800756 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
757 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700758
759 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800760 }
761
762 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
763 final CharSequence title;
764 final int titleRes = bse.getBreadCrumbTitleRes();
765 if (titleRes > 0) {
766 title = getText(titleRes);
767 } else {
768 title = bse.getBreadCrumbTitle();
769 }
770 if (title != null) {
771 setTitle(title);
772 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800773 }
774
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800775 @Override
776 protected void onSaveInstanceState(Bundle outState) {
777 super.onSaveInstanceState(outState);
778
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700779 if (mCategories.size() > 0) {
780 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800781 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700782
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700783 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700784 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700785
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700786 if (mDisplaySearch) {
787 // The option menus are created if the ActionBar is visible and they are also created
788 // asynchronously. If you launch Settings with an Intent action like
789 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
790 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
791 // menu item and search view are null.
792 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
793 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700794
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700795 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
796 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
797 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700798
799 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800800 }
801
802 @Override
803 public void onResume() {
804 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400805 if (mIsShowingDashboard) {
806 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
807 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800808
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700809 final int newHomeActivityCount = getHomeActivitiesCount();
810 if (newHomeActivityCount != mHomeActivitiesCount) {
811 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700812 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700813 }
814
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800815 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
816 @Override
817 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700818 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800819 }
820 };
821 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
822 mDevelopmentPreferencesListener);
823
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800824 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700825
Svetoslav990159a2014-04-14 17:14:59 -0700826 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700827
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700828 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700829 onQueryTextSubmit(mSearchQuery);
830 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800831 }
832
833 @Override
834 public void onPause() {
835 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400836 if (mIsShowingDashboard) {
837 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
838 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800839 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700840 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800841 }
842
843 @Override
844 public void onDestroy() {
845 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700846
847 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
848 mDevelopmentPreferencesListener);
849 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800850 }
851
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800852 protected boolean isValidFragment(String fragmentName) {
853 // Almost all fragments are wrapped in this,
854 // except for a few that have their own activities.
855 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
856 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
857 }
858 return false;
859 }
860
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800861 @Override
862 public Intent getIntent() {
863 Intent superIntent = super.getIntent();
864 String startingFragment = getStartingFragmentClass(superIntent);
865 // This is called from super.onCreate, isMultiPane() is not yet reliable
866 // Do not use onIsHidingHeaders either, which relies itself on this method
867 if (startingFragment != null) {
868 Intent modIntent = new Intent(superIntent);
869 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
870 Bundle args = superIntent.getExtras();
871 if (args != null) {
872 args = new Bundle(args);
873 } else {
874 args = new Bundle();
875 }
876 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100877 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800878 return modIntent;
879 }
880 return superIntent;
881 }
882
883 /**
884 * Checks if the component name in the intent is different from the Settings class and
885 * returns the class name to load as a fragment.
886 */
887 private String getStartingFragmentClass(Intent intent) {
888 if (mFragmentClass != null) return mFragmentClass;
889
890 String intentClass = intent.getComponent().getClassName();
891 if (intentClass.equals(getClass().getName())) return null;
892
893 if ("com.android.settings.ManageApplications".equals(intentClass)
894 || "com.android.settings.RunningServices".equals(intentClass)
895 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
896 // Old names of manage apps.
897 intentClass = com.android.settings.applications.ManageApplications.class.getName();
898 }
899
900 return intentClass;
901 }
902
903 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000904 * Start a new fragment containing a preference panel. If the preferences
905 * are being displayed in multi-pane mode, the given fragment class will
906 * be instantiated and placed in the appropriate pane. If running in
907 * single-pane mode, a new activity will be launched in which to show the
908 * fragment.
909 *
910 * @param fragmentClass Full name of the class implementing the fragment.
911 * @param args Any desired arguments to supply to the fragment.
912 * @param titleRes Optional resource identifier of the title of this
913 * fragment.
914 * @param titleText Optional text of the title of this fragment.
915 * @param resultTo Optional fragment that result data should be sent to.
916 * If non-null, resultTo.onActivityResult() will be called when this
917 * preference panel is done. The launched panel must use
918 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
919 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700920 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000921 */
922 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700923 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700924 String title = null;
925 if (titleRes < 0) {
926 if (titleText != null) {
927 title = titleText.toString();
928 } else {
929 // There not much we can do in that case
930 title = "";
931 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700932 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700933 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700934 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000935 }
936
937 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100938 * Start a new fragment in a new activity containing a preference panel for a given user. If the
939 * preferences are being displayed in multi-pane mode, the given fragment class will be
940 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
941 * activity will be launched in which to show the fragment.
942 *
943 * @param fragmentClass Full name of the class implementing the fragment.
944 * @param args Any desired arguments to supply to the fragment.
945 * @param titleRes Optional resource identifier of the title of this fragment.
946 * @param titleText Optional text of the title of this fragment.
947 * @param userHandle The user for which the panel has to be started.
948 */
949 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
950 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700951 // This is a workaround.
952 //
953 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
954 // starting the fragment could cause a native stack corruption. See b/17523189. However,
955 // adding that flag and start the preference panel with the same UserHandler will make it
956 // impossible to use back button to return to the previous screen. See b/20042570.
957 //
958 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
959 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
960 // when we're calling it as the same user.
961 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
962 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
963 } else {
964 String title = null;
965 if (titleRes < 0) {
966 if (titleText != null) {
967 title = titleText.toString();
968 } else {
969 // There not much we can do in that case
970 title = "";
971 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100972 }
Lifu Tangd0332852015-04-02 12:05:46 -0700973 Utils.startWithFragmentAsUser(this, fragmentClass, args,
974 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100975 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100976 }
977
978 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800979 * Called by a preference panel fragment to finish itself.
980 *
981 * @param caller The fragment that is asking to be finished.
982 * @param resultCode Optional result code to send back to the original
983 * launching fragment.
984 * @param resultData Optional result data to send back to the original
985 * launching fragment.
986 */
987 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
988 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700989 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800990 }
991
992 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000993 * Start a new fragment.
994 *
995 * @param fragment The fragment to start
996 * @param push If true, the current fragment will be pushed onto the back stack. If false,
997 * the current fragment will be replaced.
998 */
999 public void startPreferenceFragment(Fragment fragment, boolean push) {
1000 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001001 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +00001002 if (push) {
1003 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1004 transaction.addToBackStack(BACK_STACK_PREFS);
1005 } else {
1006 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1007 }
1008 transaction.commitAllowingStateLoss();
1009 }
1010
1011 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001012 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1013 */
1014 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001015 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001016 if (validate && !isValidFragment(fragmentName)) {
1017 throw new IllegalArgumentException("Invalid fragment for this activity: "
1018 + fragmentName);
1019 }
1020 Fragment f = Fragment.instantiate(this, fragmentName, args);
1021 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001022 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001023 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001024 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001025 }
1026 if (addToBackStack) {
1027 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1028 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001029 if (titleResId > 0) {
1030 transaction.setBreadCrumbTitle(titleResId);
1031 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001032 transaction.setBreadCrumbTitle(title);
1033 }
1034 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001035 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001036 return f;
1037 }
1038
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001039 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001040 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001041 *
1042 * @param categories The list in which to place the tiles categories.
1043 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001044 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001045 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001046 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1047 updateTilesList(categories);
1048 }
1049
1050 /**
1051 * Parse the given XML file as a categories description, adding each
1052 * parsed categories and tiles into the target list.
1053 *
1054 * @param resid The XML resource to load and parse.
1055 * @param target The list in which the parsed categories and tiles should be placed.
1056 */
1057 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1058 XmlResourceParser parser = null;
1059 try {
1060 parser = getResources().getXml(resid);
1061 AttributeSet attrs = Xml.asAttributeSet(parser);
1062
1063 int type;
1064 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1065 && type != XmlPullParser.START_TAG) {
1066 // Parse next until start tag is found
1067 }
1068
1069 String nodeName = parser.getName();
1070 if (!"dashboard-categories".equals(nodeName)) {
1071 throw new RuntimeException(
1072 "XML document must start with <preference-categories> tag; found"
1073 + nodeName + " at " + parser.getPositionDescription());
1074 }
1075
1076 Bundle curBundle = null;
1077
1078 final int outerDepth = parser.getDepth();
1079 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1080 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1081 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1082 continue;
1083 }
1084
1085 nodeName = parser.getName();
1086 if ("dashboard-category".equals(nodeName)) {
1087 DashboardCategory category = new DashboardCategory();
1088
1089 TypedArray sa = obtainStyledAttributes(
1090 attrs, com.android.internal.R.styleable.PreferenceHeader);
1091 category.id = sa.getResourceId(
1092 com.android.internal.R.styleable.PreferenceHeader_id,
1093 (int)DashboardCategory.CAT_ID_UNDEFINED);
1094
1095 TypedValue tv = sa.peekValue(
1096 com.android.internal.R.styleable.PreferenceHeader_title);
1097 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1098 if (tv.resourceId != 0) {
1099 category.titleRes = tv.resourceId;
1100 } else {
1101 category.title = tv.string;
1102 }
1103 }
1104 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001105 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1106 tv = sa.peekValue(
1107 com.android.internal.R.styleable.Preference_key);
1108 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1109 if (tv.resourceId != 0) {
1110 category.key = getString(tv.resourceId);
1111 } else {
1112 category.key = tv.string.toString();
1113 }
1114 }
1115 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001116
1117 final int innerDepth = parser.getDepth();
1118 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1119 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1120 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1121 continue;
1122 }
1123
1124 String innerNodeName = parser.getName();
1125 if (innerNodeName.equals("dashboard-tile")) {
1126 DashboardTile tile = new DashboardTile();
1127
1128 sa = obtainStyledAttributes(
1129 attrs, com.android.internal.R.styleable.PreferenceHeader);
1130 tile.id = sa.getResourceId(
1131 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001132 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001133 tv = sa.peekValue(
1134 com.android.internal.R.styleable.PreferenceHeader_title);
1135 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1136 if (tv.resourceId != 0) {
1137 tile.titleRes = tv.resourceId;
1138 } else {
1139 tile.title = tv.string;
1140 }
1141 }
1142 tv = sa.peekValue(
1143 com.android.internal.R.styleable.PreferenceHeader_summary);
1144 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1145 if (tv.resourceId != 0) {
1146 tile.summaryRes = tv.resourceId;
1147 } else {
1148 tile.summary = tv.string;
1149 }
1150 }
1151 tile.iconRes = sa.getResourceId(
1152 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1153 tile.fragment = sa.getString(
1154 com.android.internal.R.styleable.PreferenceHeader_fragment);
1155 sa.recycle();
1156
1157 if (curBundle == null) {
1158 curBundle = new Bundle();
1159 }
1160
1161 final int innerDepth2 = parser.getDepth();
1162 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1163 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1164 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1165 continue;
1166 }
1167
1168 String innerNodeName2 = parser.getName();
1169 if (innerNodeName2.equals("extra")) {
1170 getResources().parseBundleExtra("extra", attrs, curBundle);
1171 XmlUtils.skipCurrentTag(parser);
1172
1173 } else if (innerNodeName2.equals("intent")) {
1174 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1175
1176 } else {
1177 XmlUtils.skipCurrentTag(parser);
1178 }
1179 }
1180
1181 if (curBundle.size() > 0) {
1182 tile.fragmentArguments = curBundle;
1183 curBundle = null;
1184 }
1185
PauloftheWest38155612014-06-30 10:02:36 -07001186 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001187 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001188 category.addTile(tile);
1189 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001190
Jason Monk2ebc8a02015-02-13 15:23:19 -05001191 } else if (innerNodeName.equals("external-tiles")) {
1192 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001193 } else {
1194 XmlUtils.skipCurrentTag(parser);
1195 }
1196 }
1197
1198 target.add(category);
1199 } else {
1200 XmlUtils.skipCurrentTag(parser);
1201 }
1202 }
1203
1204 } catch (XmlPullParserException e) {
1205 throw new RuntimeException("Error parsing categories", e);
1206 } catch (IOException e) {
1207 throw new RuntimeException("Error parsing categories", e);
1208 } finally {
1209 if (parser != null) parser.close();
1210 }
1211 }
1212
1213 private void updateTilesList(List<DashboardCategory> target) {
1214 final boolean showDev = mDevelopmentPreferences.getBoolean(
1215 DevelopmentSettings.PREF_SHOW,
1216 android.os.Build.TYPE.equals("eng"));
1217
1218 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1219
1220 final int size = target.size();
1221 for (int i = 0; i < size; i++) {
1222
1223 DashboardCategory category = target.get(i);
1224
1225 // Ids are integers, so downcasting is ok
1226 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001227 int n = category.getTilesCount() - 1;
1228 while (n >= 0) {
1229
1230 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001231 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001232 id = (int) tile.id;
1233 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001234 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1235 removeTile = true;
1236 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001237 } else if (id == R.id.wifi_settings) {
1238 // Remove WiFi Settings if WiFi service is not available.
1239 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001240 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001241 }
1242 } else if (id == R.id.bluetooth_settings) {
1243 // Remove Bluetooth Settings if Bluetooth service is not available.
1244 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001245 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001246 }
1247 } else if (id == R.id.data_usage_settings) {
1248 // Remove data usage when kernel module not enabled
1249 final INetworkManagementService netManager = INetworkManagementService.Stub
1250 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1251 try {
1252 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001253 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001254 }
1255 } catch (RemoteException e) {
1256 // ignored
1257 }
1258 } else if (id == R.id.battery_settings) {
1259 // Remove battery settings when battery is not available. (e.g. TV)
1260
1261 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001262 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001263 }
1264 } else if (id == R.id.home_settings) {
1265 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001266 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 }
1268 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001269 boolean hasMultipleUsers =
1270 ((UserManager) getSystemService(Context.USER_SERVICE))
1271 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001272 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001273 || (!UserManager.supportsMultipleUsers()
1274 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001275 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001276 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001277 }
1278 } else if (id == R.id.nfc_payment_settings) {
1279 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001280 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001281 } else {
1282 // Only show if NFC is on and we have the HCE feature
1283 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001284 if (adapter == null || !adapter.isEnabled() ||
1285 !getPackageManager().hasSystemFeature(
1286 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001287 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001288 }
1289 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001290 } else if (id == R.id.print_settings) {
1291 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1292 PackageManager.FEATURE_PRINTING);
1293 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001294 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001295 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001296 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001297 if (!showDev || um.hasUserRestriction(
1298 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001299 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001300 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001301 }
1302
1303 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001304 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1305 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001306 }
1307
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001308 if (removeTile && n < category.getTilesCount()) {
1309 category.removeTile(n);
1310 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001311 n--;
1312 }
1313 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001314 addExternalTiles(target);
1315 }
1316
1317 private void addExternalTiles(List<DashboardCategory> target) {
1318 Map<Pair<String, String>, DashboardTile> addedCache =
1319 new ArrayMap<Pair<String, String>, DashboardTile>();
1320 UserManager userManager = UserManager.get(this);
1321 for (UserHandle user : userManager.getUserProfiles()) {
1322 addExternalTiles(target, user, addedCache);
1323 }
1324 }
1325
1326 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1327 Map<Pair<String, String>, DashboardTile> addedCache) {
1328 PackageManager pm = getPackageManager();
1329 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1330 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1331 PackageManager.GET_META_DATA, user.getIdentifier());
1332 for (ResolveInfo resolved : results) {
1333 if (!resolved.system) {
1334 // Do not allow any app to add to settings, only system ones.
1335 continue;
1336 }
1337 ActivityInfo activityInfo = resolved.activityInfo;
1338 Bundle metaData = activityInfo.metaData;
1339 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1340 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1341 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1342 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1343 continue;
1344 }
1345 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1346 DashboardCategory category = getCategory(target, categoryKey);
1347 if (category == null) {
1348 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1349 + "category key " + categoryKey);
1350 continue;
1351 }
1352 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1353 activityInfo.name);
1354 DashboardTile tile = addedCache.get(key);
1355 if (tile == null) {
1356 tile = new DashboardTile();
1357 tile.intent = new Intent().setClassName(
1358 activityInfo.packageName, activityInfo.name);
1359 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1360
1361 if (category.externalIndex == -1) {
1362 // If no location for external tiles has been specified for this category,
1363 // then just put them at the end.
1364 category.addTile(tile);
1365 } else {
1366 category.addTile(category.externalIndex, tile);
1367 }
1368 addedCache.put(key, tile);
1369 }
1370 tile.userHandle.add(user);
1371 }
1372 }
1373
1374 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1375 for (DashboardCategory category : target) {
1376 if (categoryKey.equals(category.key)) {
1377 return category;
1378 }
1379 }
1380 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001381 }
1382
1383 private boolean updateHomeSettingTiles(DashboardTile tile) {
1384 // Once we decide to show Home settings, keep showing it forever
1385 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1386 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1387 return true;
1388 }
1389
1390 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001391 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001392 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001393 // When there's only one available home app, omit this settings
1394 // category entirely at the top level UI. If the user just
1395 // uninstalled the penultimate home app candidiate, we also
1396 // now tell them about why they aren't seeing 'Home' in the list.
1397 if (sShowNoHomeNotice) {
1398 sShowNoHomeNotice = false;
1399 NoHomeDialogFragment.show(this);
1400 }
1401 return false;
1402 } else {
1403 // Okay, we're allowing the Home settings category. Tell it, when
1404 // invoked via this front door, that we'll need to be told about the
1405 // case when the user uninstalls all but one home app.
1406 if (tile.fragmentArguments == null) {
1407 tile.fragmentArguments = new Bundle();
1408 }
1409 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1410 }
1411 } catch (Exception e) {
1412 // Can't look up the home activity; bail on configuring the icon
1413 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1414 }
1415
1416 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1417 return true;
1418 }
1419
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001420 private void getMetaData() {
1421 try {
1422 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1423 PackageManager.GET_META_DATA);
1424 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001425 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1426 } catch (NameNotFoundException nnfe) {
1427 // No recovery
1428 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1429 }
1430 }
1431
1432 // give subclasses access to the Next button
1433 public boolean hasNextButton() {
1434 return mNextButton != null;
1435 }
1436
1437 public Button getNextButton() {
1438 return mNextButton;
1439 }
1440
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001441 @Override
1442 public boolean shouldUpRecreateTask(Intent targetIntent) {
1443 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1444 }
1445
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001446 public static void requestHomeNotice() {
1447 sShowNoHomeNotice = true;
1448 }
1449
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001450 @Override
1451 public boolean onQueryTextSubmit(String query) {
1452 switchToSearchResultsFragmentIfNeeded();
1453 mSearchQuery = query;
1454 return mSearchResultsFragment.onQueryTextSubmit(query);
1455 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001456
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001457 @Override
1458 public boolean onQueryTextChange(String newText) {
1459 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001460 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001461 return false;
1462 }
1463 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001464 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001465
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001466 @Override
1467 public boolean onClose() {
1468 return false;
1469 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001470
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 @Override
1472 public boolean onMenuItemActionExpand(MenuItem item) {
1473 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001474 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001475 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001476 return true;
1477 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001478
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001479 @Override
1480 public boolean onMenuItemActionCollapse(MenuItem item) {
1481 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001482 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001483 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001484 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001485 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001486 return true;
1487 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001488
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001489 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 if (mSearchResultsFragment != null) {
1491 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001492 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001493 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001494 if (current != null && current instanceof SearchResultsSummary) {
1495 mSearchResultsFragment = (SearchResultsSummary) current;
1496 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001497 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001498 SearchResultsSummary.class.getName(), null, false, true,
1499 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001500 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001501 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001502 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001503 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001504
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001505 public void needToRevertToInitialFragment() {
1506 mNeedToRevertToInitialFragment = true;
1507 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001508
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001509 private void revertToInitialFragment() {
1510 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001511 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001512 mSearchMenuItemExpanded = false;
1513 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1514 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001515 if (mSearchMenuItem != null) {
1516 mSearchMenuItem.collapseActionView();
1517 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001518 }
Jim Miller0698a212014-10-16 19:49:07 -07001519
1520 public Intent getResultIntentData() {
1521 return mResultIntentData;
1522 }
1523
1524 public void setResultIntentData(Intent resultIntentData) {
1525 mResultIntentData = resultIntentData;
1526 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001527}