blob: 5e1acb1769c2c6fbc7aa14e6d7027458df2208d7 [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 Spurlockc96a5dc2015-04-10 11:59:54 -0400104import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400105import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400106import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400107import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.print.PrintJobSettingsFragment;
109import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500110import com.android.settings.search.DynamicIndexableContentMonitor;
111import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700112import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.tts.TextToSpeechSettings;
114import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700115import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import com.android.settings.vpn2.VpnSettings;
117import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700118import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700120import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import com.android.settings.wifi.WifiSettings;
122import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500123
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124import org.xmlpull.v1.XmlPullParser;
125import org.xmlpull.v1.XmlPullParserException;
126
127import java.io.IOException;
128import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800129import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500130import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700131import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133public class SettingsActivity extends Activity
134 implements PreferenceManager.OnPreferenceTreeClickListener,
135 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100136 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100138 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800139
140 private static final String LOG_TAG = "Settings";
141
142 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700143 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700144 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
145 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700146 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700147 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700148 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149
150 /**
151 * When starting this activity, the invoking Intent can contain this extra
152 * string to specify which fragment should be initially displayed.
153 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
154 * will call isValidFragment() to confirm that the fragment class name is valid for this
155 * activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
158
159 /**
160 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
161 * this extra can also be specified to supply a Bundle of arguments to pass
162 * to that fragment when it is instantiated during the initial creation
163 * of the activity.
164 */
165 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
166
167 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700168 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
169 */
170 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
171
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800172 public static final String BACK_STACK_PREFS = ":settings:prefs";
173
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174 // extras that allow any preference activity to be launched as part of a wizard
175
176 // show Back and Next buttons? takes boolean parameter
177 // Back will then return RESULT_CANCELED and Next RESULT_OK
178 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
179
180 // add a Skip button?
181 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
182
183 // specify custom text for the Back or Next buttons, or cause a button to not appear
184 // at all by setting it to null
185 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
186 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
187
188 /**
189 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700190 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191 * that fragment.
192 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700193 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100194 /**
195 * The package name used to resolve the title resource id.
196 */
197 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
198 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700199 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
200 ":settings:show_fragment_title_resid";
201 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
202 ":settings:show_fragment_as_shortcut";
203
204 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
205 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207 private static final String META_DATA_KEY_FRAGMENT_CLASS =
208 "com.android.settings.FRAGMENT_CLASS";
209
210 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
211
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700212 private static final String EMPTY_QUERY = "";
213
Jason Monk2ebc8a02015-02-13 15:23:19 -0500214 /**
215 * Settings will search for system activities of this action and add them as a top level
216 * settings tile using the following parameters.
217 *
218 * <p>A category must be specified in the meta-data for the activity named
219 * {@link #EXTRA_CATEGORY_KEY}
220 *
221 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
222 * otherwise the label for the activity will be used.
223 *
224 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
225 * otherwise the icon for the activity will be used.
226 *
227 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
228 */
229 private static final String EXTRA_SETTINGS_ACTION =
230 "com.android.settings.action.EXTRA_SETTINGS";
231
232 /**
233 * The key used to get the category from metadata of activities of action
234 * {@link #EXTRA_SETTINGS_ACTION}
235 * The value must be one of:
236 * <li>com.android.settings.category.wireless</li>
237 * <li>com.android.settings.category.device</li>
238 * <li>com.android.settings.category.personal</li>
239 * <li>com.android.settings.category.system</li>
240 */
241 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
242
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243 private static boolean sShowNoHomeNotice = false;
244
245 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800247 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700248 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800249
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 // Show only these settings for restricted users
251 private int[] SETTINGS_FOR_RESTRICTED = {
252 R.id.wireless_section,
253 R.id.wifi_settings,
254 R.id.bluetooth_settings,
255 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700256 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 R.id.wireless_settings,
258 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400259 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800260 R.id.display_settings,
261 R.id.storage_settings,
262 R.id.application_settings,
263 R.id.battery_settings,
264 R.id.personal_section,
265 R.id.location_settings,
266 R.id.security_settings,
267 R.id.language_settings,
268 R.id.user_settings,
269 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270 R.id.system_section,
271 R.id.date_time_settings,
272 R.id.about_settings,
273 R.id.accessibility_settings,
274 R.id.print_settings,
275 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800276 R.id.home_settings,
277 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 };
279
280 private static final String[] ENTRY_FRAGMENTS = {
281 WirelessSettings.class.getName(),
282 WifiSettings.class.getName(),
283 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700284 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700286 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 TetherSettings.class.getName(),
288 WifiP2pSettings.class.getName(),
289 VpnSettings.class.getName(),
290 DateTimeSettings.class.getName(),
291 LocalePicker.class.getName(),
292 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700293 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 SpellCheckersSettings.class.getName(),
295 UserDictionaryList.class.getName(),
296 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700297 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 DisplaySettings.class.getName(),
299 DeviceInfoSettings.class.getName(),
300 ManageApplications.class.getName(),
301 ProcessStatsUi.class.getName(),
302 NotificationStation.class.getName(),
303 LocationSettings.class.getName(),
304 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400305 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 PrivacySettings.class.getName(),
307 DeviceAdminSettings.class.getName(),
308 AccessibilitySettings.class.getName(),
309 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
311 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700312 StorageSettings.class.getName(),
313 PublicVolumeSettings.class.getName(),
Jeff Sharkeya16257d2015-04-28 13:41:01 -0700314 PrivateVolumeForget.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800315 DevelopmentSettings.class.getName(),
316 UsbSettings.class.getName(),
317 AndroidBeam.class.getName(),
318 WifiDisplaySettings.class.getName(),
319 PowerUsageSummary.class.getName(),
320 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000321 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 CryptKeeperSettings.class.getName(),
323 DataUsageSummary.class.getName(),
324 DreamSettings.class.getName(),
325 UserSettings.class.getName(),
326 NotificationAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327 PrintSettingsFragment.class.getName(),
328 PrintJobSettingsFragment.class.getName(),
329 TrustedCredentialsSettings.class.getName(),
330 PaymentSettings.class.getName(),
331 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700332 ZenModeSettings.class.getName(),
333 NotificationSettings.class.getName(),
334 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
335 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400336 InstalledAppDetails.class.getName(),
337 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700338 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700339 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000340 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400341 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400342 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400343 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400344 ZenModeExternalRuleSettings.class.getName(),
Jason Monk2583fc12015-03-25 09:46:30 -0400345 ProcessStatsUi.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 };
347
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700348
349 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
350 "android.settings.APPLICATION_DETAILS_SETTINGS"
351 };
352
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 private SharedPreferences mDevelopmentPreferences;
354 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
355
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 private boolean mBatteryPresent = true;
357 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 @Override
359 public void onReceive(Context context, Intent intent) {
360 String action = intent.getAction();
361 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
362 boolean batteryPresent = Utils.isBatteryPresent(intent);
363
364 if (mBatteryPresent != batteryPresent) {
365 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700366 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800367 }
368 }
369 }
370 };
371
Svetoslav990159a2014-04-14 17:14:59 -0700372 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
373 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700374
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700375 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700376 private SwitchBar mSwitchBar;
377
378 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700379
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700380 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700381 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700382
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700383 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700384 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700385
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700386 private ViewGroup mContent;
387
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700388 private SearchView mSearchView;
389 private MenuItem mSearchMenuItem;
390 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700391 private SearchResultsSummary mSearchResultsFragment;
392 private String mSearchQuery;
393
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700394 // Categories
395 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800396
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700397 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700398 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800399 private Handler mHandler = new Handler() {
400 @Override
401 public void handleMessage(Message msg) {
402 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700403 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700404 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
405 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700406 buildDashboardCategories(mCategories);
407 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800408 } break;
409 }
410 }
411 };
412
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700413 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700414 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700415
Jim Miller0698a212014-10-16 19:49:07 -0700416 private Intent mResultIntentData;
417
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700418 public SwitchBar getSwitchBar() {
419 return mSwitchBar;
420 }
421
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700422 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
423 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700424 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700425 }
426 return mCategories;
427 }
428
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800429 @Override
430 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
431 // Override the fragment title for Wallpaper settings
432 int titleRes = pref.getTitleRes();
433 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
434 titleRes = R.string.wallpaper_settings_fragment_title;
435 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
436 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
437 if (UserManager.get(this).isLinkedUser()) {
438 titleRes = R.string.profile_info_settings_title;
439 } else {
440 titleRes = R.string.user_info_settings_title;
441 }
442 }
443 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
444 null, 0);
445 return true;
446 }
447
448 @Override
449 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
450 return false;
451 }
452
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700453 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700454 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700455 Message msg = new Message();
456 msg.what = MSG_BUILD_CATEGORIES;
457 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800458 }
459 }
460
461 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800462 public void onConfigurationChanged(Configuration newConfig) {
463 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800464 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465 }
466
467 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700468 protected void onStart() {
469 super.onStart();
470
471 if (mNeedToRevertToInitialFragment) {
472 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 }
475
476 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700477 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700478 if (!mDisplaySearch) {
479 return false;
480 }
481
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700482 MenuInflater inflater = getMenuInflater();
483 inflater.inflate(R.menu.options_menu, menu);
484
485 // Cache the search query (can be overriden by the OnQueryTextListener)
486 final String query = mSearchQuery;
487
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700488 mSearchMenuItem = menu.findItem(R.id.search);
489 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700490
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700491 if (mSearchMenuItem == null || mSearchView == null) {
492 return false;
493 }
494
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700495 if (mSearchResultsFragment != null) {
496 mSearchResultsFragment.setSearchView(mSearchView);
497 }
498
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700499 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700500 mSearchView.setOnQueryTextListener(this);
501 mSearchView.setOnCloseListener(this);
502
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700503 if (mSearchMenuItemExpanded) {
504 mSearchMenuItem.expandActionView();
505 }
506 mSearchView.setQuery(query, true /* submit */);
507
508 return true;
509 }
510
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700511 private static boolean isShortCutIntent(final Intent intent) {
512 Set<String> categories = intent.getCategories();
513 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
514 }
515
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700516 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700517 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700518 if (action == null) {
519 return false;
520 }
521 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
522 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
523 }
524 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700525 }
526
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700527 @Override
528 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700529 super.onCreate(savedState);
530
531 // Should happen before any call to getIntent()
532 getMetaData();
533
534 final Intent intent = getIntent();
535 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
536 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
540 Context.MODE_PRIVATE);
541
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700542 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700543 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700544
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700545 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700546 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
547
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700548 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700549 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700550
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700551 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700552
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700553 // This is a "Sub Settings" when:
554 // - this is a real SubSettings
555 // - or :settings:show_fragment_as_subsetting is passed to the Intent
556 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
557 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
558
559 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
560 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700561 // Check also that we are not a Theme Dialog as we don't want to override them
562 final int themeResId = getThemeResId();
563 if (themeResId != R.style.Theme_DialogWhenLarge &&
564 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
565 setTheme(R.style.Theme_SubSettings);
566 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700567 }
568
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700569 setContentView(mIsShowingDashboard ?
570 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700572 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700573
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 getFragmentManager().addOnBackStackChangedListener(this);
575
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700576 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800577 // Run the Index update only if we have some space
578 if (!Utils.isLowStorage(this)) {
579 Index.getInstance(getApplicationContext()).update();
580 } else {
581 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
582 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700583 }
584
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700585 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700586 // We are restarting from a previous saved state; used that to initialize, instead
587 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700588 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
589 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700591 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800592
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700593 ArrayList<DashboardCategory> categories =
594 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
595 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700596 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700597 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700598 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700600
601 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700602 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700603 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
604 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800605 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700606 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700607 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
608 // only if it is a sub settings
609 if (mIsShortcut) {
610 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700611 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700612 } else if (isSubSettings) {
613 mDisplayHomeAsUpEnabled = true;
614 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700615 } else {
616 mDisplayHomeAsUpEnabled = false;
617 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700618 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700619 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700620
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700621 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700622 switchToFragment(initialFragmentName, initialArguments, true, false,
623 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000624 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700625 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700626 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700627 // Show Search affordance
628 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700629 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700630 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700631 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800632 }
633 }
634
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700635 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700636 if (mActionBar != null) {
637 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
638 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
639 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700640 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
641
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800643 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
644
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700645 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 if (buttonBar != null) {
647 buttonBar.setVisibility(View.VISIBLE);
648
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700649 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 backButton.setOnClickListener(new OnClickListener() {
651 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700652 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 finish();
654 }
655 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700656 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 skipButton.setOnClickListener(new OnClickListener() {
658 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700659 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 finish();
661 }
662 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700663 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800664 mNextButton.setOnClickListener(new OnClickListener() {
665 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700666 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800667 finish();
668 }
669 });
670
671 // set our various button parameters
672 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
673 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
674 if (TextUtils.isEmpty(buttonText)) {
675 mNextButton.setVisibility(View.GONE);
676 }
677 else {
678 mNextButton.setText(buttonText);
679 }
680 }
681 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
682 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
683 if (TextUtils.isEmpty(buttonText)) {
684 backButton.setVisibility(View.GONE);
685 }
686 else {
687 backButton.setText(buttonText);
688 }
689 }
690 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
691 skipButton.setVisibility(View.VISIBLE);
692 }
693 }
694 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700695
696 mHomeActivitiesCount = getHomeActivitiesCount();
697 }
698
699 private int getHomeActivitiesCount() {
700 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
701 getPackageManager().getHomeActivities(homeApps);
702 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800703 }
704
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700705 private void setTitleFromIntent(Intent intent) {
706 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
707 if (initialTitleResId > 0) {
708 mInitialTitle = null;
709 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100710
711 final String initialTitleResPackageName = intent.getStringExtra(
712 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
713 if (initialTitleResPackageName != null) {
714 try {
715 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
716 0 /* flags */, new UserHandle(UserHandle.myUserId()));
717 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
718 setTitle(mInitialTitle);
719 mInitialTitleResId = -1;
720 return;
721 } catch (NameNotFoundException e) {
722 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
723 }
724 } else {
725 setTitle(mInitialTitleResId);
726 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700727 } else {
728 mInitialTitleResId = -1;
729 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
730 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
731 setTitle(mInitialTitle);
732 }
733 }
734
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800735 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800736 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700737 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800738 }
739
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700740 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800741 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700742
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800743 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700744 if (mInitialTitleResId > 0) {
745 setTitle(mInitialTitleResId);
746 } else {
747 setTitle(mInitialTitle);
748 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700749 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800750 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700751
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800752 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
753 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700754
755 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800756 }
757
758 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
759 final CharSequence title;
760 final int titleRes = bse.getBreadCrumbTitleRes();
761 if (titleRes > 0) {
762 title = getText(titleRes);
763 } else {
764 title = bse.getBreadCrumbTitle();
765 }
766 if (title != null) {
767 setTitle(title);
768 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800769 }
770
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800771 @Override
772 protected void onSaveInstanceState(Bundle outState) {
773 super.onSaveInstanceState(outState);
774
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700775 if (mCategories.size() > 0) {
776 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800777 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700778
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700779 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700780 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700781
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700782 if (mDisplaySearch) {
783 // The option menus are created if the ActionBar is visible and they are also created
784 // asynchronously. If you launch Settings with an Intent action like
785 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
786 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
787 // menu item and search view are null.
788 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
789 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700790
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700791 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
792 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
793 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700794
795 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800796 }
797
798 @Override
799 public void onResume() {
800 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400801 if (mIsShowingDashboard) {
802 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
803 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800804
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700805 final int newHomeActivityCount = getHomeActivitiesCount();
806 if (newHomeActivityCount != mHomeActivitiesCount) {
807 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700808 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700809 }
810
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800811 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
812 @Override
813 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700814 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800815 }
816 };
817 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
818 mDevelopmentPreferencesListener);
819
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800820 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700821
Svetoslav990159a2014-04-14 17:14:59 -0700822 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700823
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700824 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700825 onQueryTextSubmit(mSearchQuery);
826 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800827 }
828
829 @Override
830 public void onPause() {
831 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400832 if (mIsShowingDashboard) {
833 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
834 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800835 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700836 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800837 }
838
839 @Override
840 public void onDestroy() {
841 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700842
843 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
844 mDevelopmentPreferencesListener);
845 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800846 }
847
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800848 protected boolean isValidFragment(String fragmentName) {
849 // Almost all fragments are wrapped in this,
850 // except for a few that have their own activities.
851 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
852 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
853 }
854 return false;
855 }
856
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800857 @Override
858 public Intent getIntent() {
859 Intent superIntent = super.getIntent();
860 String startingFragment = getStartingFragmentClass(superIntent);
861 // This is called from super.onCreate, isMultiPane() is not yet reliable
862 // Do not use onIsHidingHeaders either, which relies itself on this method
863 if (startingFragment != null) {
864 Intent modIntent = new Intent(superIntent);
865 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
866 Bundle args = superIntent.getExtras();
867 if (args != null) {
868 args = new Bundle(args);
869 } else {
870 args = new Bundle();
871 }
872 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100873 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800874 return modIntent;
875 }
876 return superIntent;
877 }
878
879 /**
880 * Checks if the component name in the intent is different from the Settings class and
881 * returns the class name to load as a fragment.
882 */
883 private String getStartingFragmentClass(Intent intent) {
884 if (mFragmentClass != null) return mFragmentClass;
885
886 String intentClass = intent.getComponent().getClassName();
887 if (intentClass.equals(getClass().getName())) return null;
888
889 if ("com.android.settings.ManageApplications".equals(intentClass)
890 || "com.android.settings.RunningServices".equals(intentClass)
891 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
892 // Old names of manage apps.
893 intentClass = com.android.settings.applications.ManageApplications.class.getName();
894 }
895
896 return intentClass;
897 }
898
899 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000900 * Start a new fragment containing a preference panel. If the preferences
901 * are being displayed in multi-pane mode, the given fragment class will
902 * be instantiated and placed in the appropriate pane. If running in
903 * single-pane mode, a new activity will be launched in which to show the
904 * fragment.
905 *
906 * @param fragmentClass Full name of the class implementing the fragment.
907 * @param args Any desired arguments to supply to the fragment.
908 * @param titleRes Optional resource identifier of the title of this
909 * fragment.
910 * @param titleText Optional text of the title of this fragment.
911 * @param resultTo Optional fragment that result data should be sent to.
912 * If non-null, resultTo.onActivityResult() will be called when this
913 * preference panel is done. The launched panel must use
914 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
915 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700916 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000917 */
918 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700919 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700920 String title = null;
921 if (titleRes < 0) {
922 if (titleText != null) {
923 title = titleText.toString();
924 } else {
925 // There not much we can do in that case
926 title = "";
927 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700928 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700929 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700930 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000931 }
932
933 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100934 * Start a new fragment in a new activity containing a preference panel for a given user. If the
935 * preferences are being displayed in multi-pane mode, the given fragment class will be
936 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
937 * activity will be launched in which to show the fragment.
938 *
939 * @param fragmentClass Full name of the class implementing the fragment.
940 * @param args Any desired arguments to supply to the fragment.
941 * @param titleRes Optional resource identifier of the title of this fragment.
942 * @param titleText Optional text of the title of this fragment.
943 * @param userHandle The user for which the panel has to be started.
944 */
945 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
946 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700947 // This is a workaround.
948 //
949 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
950 // starting the fragment could cause a native stack corruption. See b/17523189. However,
951 // adding that flag and start the preference panel with the same UserHandler will make it
952 // impossible to use back button to return to the previous screen. See b/20042570.
953 //
954 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
955 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
956 // when we're calling it as the same user.
957 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
958 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
959 } else {
960 String title = null;
961 if (titleRes < 0) {
962 if (titleText != null) {
963 title = titleText.toString();
964 } else {
965 // There not much we can do in that case
966 title = "";
967 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100968 }
Lifu Tangd0332852015-04-02 12:05:46 -0700969 Utils.startWithFragmentAsUser(this, fragmentClass, args,
970 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100971 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100972 }
973
974 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800975 * Called by a preference panel fragment to finish itself.
976 *
977 * @param caller The fragment that is asking to be finished.
978 * @param resultCode Optional result code to send back to the original
979 * launching fragment.
980 * @param resultData Optional result data to send back to the original
981 * launching fragment.
982 */
983 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
984 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700985 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800986 }
987
988 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000989 * Start a new fragment.
990 *
991 * @param fragment The fragment to start
992 * @param push If true, the current fragment will be pushed onto the back stack. If false,
993 * the current fragment will be replaced.
994 */
995 public void startPreferenceFragment(Fragment fragment, boolean push) {
996 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700997 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000998 if (push) {
999 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
1000 transaction.addToBackStack(BACK_STACK_PREFS);
1001 } else {
1002 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1003 }
1004 transaction.commitAllowingStateLoss();
1005 }
1006
1007 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001008 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1009 */
1010 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001011 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001012 if (validate && !isValidFragment(fragmentName)) {
1013 throw new IllegalArgumentException("Invalid fragment for this activity: "
1014 + fragmentName);
1015 }
1016 Fragment f = Fragment.instantiate(this, fragmentName, args);
1017 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001018 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001019 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001020 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001021 }
1022 if (addToBackStack) {
1023 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1024 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001025 if (titleResId > 0) {
1026 transaction.setBreadCrumbTitle(titleResId);
1027 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001028 transaction.setBreadCrumbTitle(title);
1029 }
1030 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001031 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001032 return f;
1033 }
1034
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001035 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001036 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001037 *
1038 * @param categories The list in which to place the tiles categories.
1039 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001040 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001041 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001042 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1043 updateTilesList(categories);
1044 }
1045
1046 /**
1047 * Parse the given XML file as a categories description, adding each
1048 * parsed categories and tiles into the target list.
1049 *
1050 * @param resid The XML resource to load and parse.
1051 * @param target The list in which the parsed categories and tiles should be placed.
1052 */
1053 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1054 XmlResourceParser parser = null;
1055 try {
1056 parser = getResources().getXml(resid);
1057 AttributeSet attrs = Xml.asAttributeSet(parser);
1058
1059 int type;
1060 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1061 && type != XmlPullParser.START_TAG) {
1062 // Parse next until start tag is found
1063 }
1064
1065 String nodeName = parser.getName();
1066 if (!"dashboard-categories".equals(nodeName)) {
1067 throw new RuntimeException(
1068 "XML document must start with <preference-categories> tag; found"
1069 + nodeName + " at " + parser.getPositionDescription());
1070 }
1071
1072 Bundle curBundle = null;
1073
1074 final int outerDepth = parser.getDepth();
1075 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1076 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1077 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1078 continue;
1079 }
1080
1081 nodeName = parser.getName();
1082 if ("dashboard-category".equals(nodeName)) {
1083 DashboardCategory category = new DashboardCategory();
1084
1085 TypedArray sa = obtainStyledAttributes(
1086 attrs, com.android.internal.R.styleable.PreferenceHeader);
1087 category.id = sa.getResourceId(
1088 com.android.internal.R.styleable.PreferenceHeader_id,
1089 (int)DashboardCategory.CAT_ID_UNDEFINED);
1090
1091 TypedValue tv = sa.peekValue(
1092 com.android.internal.R.styleable.PreferenceHeader_title);
1093 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1094 if (tv.resourceId != 0) {
1095 category.titleRes = tv.resourceId;
1096 } else {
1097 category.title = tv.string;
1098 }
1099 }
1100 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001101 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1102 tv = sa.peekValue(
1103 com.android.internal.R.styleable.Preference_key);
1104 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1105 if (tv.resourceId != 0) {
1106 category.key = getString(tv.resourceId);
1107 } else {
1108 category.key = tv.string.toString();
1109 }
1110 }
1111 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001112
1113 final int innerDepth = parser.getDepth();
1114 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1115 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1116 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1117 continue;
1118 }
1119
1120 String innerNodeName = parser.getName();
1121 if (innerNodeName.equals("dashboard-tile")) {
1122 DashboardTile tile = new DashboardTile();
1123
1124 sa = obtainStyledAttributes(
1125 attrs, com.android.internal.R.styleable.PreferenceHeader);
1126 tile.id = sa.getResourceId(
1127 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001128 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001129 tv = sa.peekValue(
1130 com.android.internal.R.styleable.PreferenceHeader_title);
1131 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1132 if (tv.resourceId != 0) {
1133 tile.titleRes = tv.resourceId;
1134 } else {
1135 tile.title = tv.string;
1136 }
1137 }
1138 tv = sa.peekValue(
1139 com.android.internal.R.styleable.PreferenceHeader_summary);
1140 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1141 if (tv.resourceId != 0) {
1142 tile.summaryRes = tv.resourceId;
1143 } else {
1144 tile.summary = tv.string;
1145 }
1146 }
1147 tile.iconRes = sa.getResourceId(
1148 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1149 tile.fragment = sa.getString(
1150 com.android.internal.R.styleable.PreferenceHeader_fragment);
1151 sa.recycle();
1152
1153 if (curBundle == null) {
1154 curBundle = new Bundle();
1155 }
1156
1157 final int innerDepth2 = parser.getDepth();
1158 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1159 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1160 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1161 continue;
1162 }
1163
1164 String innerNodeName2 = parser.getName();
1165 if (innerNodeName2.equals("extra")) {
1166 getResources().parseBundleExtra("extra", attrs, curBundle);
1167 XmlUtils.skipCurrentTag(parser);
1168
1169 } else if (innerNodeName2.equals("intent")) {
1170 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1171
1172 } else {
1173 XmlUtils.skipCurrentTag(parser);
1174 }
1175 }
1176
1177 if (curBundle.size() > 0) {
1178 tile.fragmentArguments = curBundle;
1179 curBundle = null;
1180 }
1181
PauloftheWest38155612014-06-30 10:02:36 -07001182 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001183 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001184 category.addTile(tile);
1185 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001186
Jason Monk2ebc8a02015-02-13 15:23:19 -05001187 } else if (innerNodeName.equals("external-tiles")) {
1188 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001189 } else {
1190 XmlUtils.skipCurrentTag(parser);
1191 }
1192 }
1193
1194 target.add(category);
1195 } else {
1196 XmlUtils.skipCurrentTag(parser);
1197 }
1198 }
1199
1200 } catch (XmlPullParserException e) {
1201 throw new RuntimeException("Error parsing categories", e);
1202 } catch (IOException e) {
1203 throw new RuntimeException("Error parsing categories", e);
1204 } finally {
1205 if (parser != null) parser.close();
1206 }
1207 }
1208
1209 private void updateTilesList(List<DashboardCategory> target) {
1210 final boolean showDev = mDevelopmentPreferences.getBoolean(
1211 DevelopmentSettings.PREF_SHOW,
1212 android.os.Build.TYPE.equals("eng"));
1213
1214 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1215
1216 final int size = target.size();
1217 for (int i = 0; i < size; i++) {
1218
1219 DashboardCategory category = target.get(i);
1220
1221 // Ids are integers, so downcasting is ok
1222 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001223 int n = category.getTilesCount() - 1;
1224 while (n >= 0) {
1225
1226 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001227 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001228 id = (int) tile.id;
1229 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001230 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1231 removeTile = true;
1232 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001233 } else if (id == R.id.wifi_settings) {
1234 // Remove WiFi Settings if WiFi service is not available.
1235 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001236 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001237 }
1238 } else if (id == R.id.bluetooth_settings) {
1239 // Remove Bluetooth Settings if Bluetooth service is not available.
1240 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001241 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001242 }
1243 } else if (id == R.id.data_usage_settings) {
1244 // Remove data usage when kernel module not enabled
1245 final INetworkManagementService netManager = INetworkManagementService.Stub
1246 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1247 try {
1248 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001249 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001250 }
1251 } catch (RemoteException e) {
1252 // ignored
1253 }
1254 } else if (id == R.id.battery_settings) {
1255 // Remove battery settings when battery is not available. (e.g. TV)
1256
1257 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001258 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001259 }
1260 } else if (id == R.id.home_settings) {
1261 if (!updateHomeSettingTiles(tile)) {
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.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001265 boolean hasMultipleUsers =
1266 ((UserManager) getSystemService(Context.USER_SERVICE))
1267 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001268 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001269 || (!UserManager.supportsMultipleUsers()
1270 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001271 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001272 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001273 }
1274 } else if (id == R.id.nfc_payment_settings) {
1275 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001276 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001277 } else {
1278 // Only show if NFC is on and we have the HCE feature
1279 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001280 if (adapter == null || !adapter.isEnabled() ||
1281 !getPackageManager().hasSystemFeature(
1282 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001283 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001284 }
1285 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001286 } else if (id == R.id.print_settings) {
1287 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1288 PackageManager.FEATURE_PRINTING);
1289 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001290 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001291 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001292 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001293 if (!showDev || um.hasUserRestriction(
1294 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001295 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001296 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001297 }
1298
1299 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001300 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1301 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001302 }
1303
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001304 if (removeTile && n < category.getTilesCount()) {
1305 category.removeTile(n);
1306 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001307 n--;
1308 }
1309 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001310 addExternalTiles(target);
1311 }
1312
1313 private void addExternalTiles(List<DashboardCategory> target) {
1314 Map<Pair<String, String>, DashboardTile> addedCache =
1315 new ArrayMap<Pair<String, String>, DashboardTile>();
1316 UserManager userManager = UserManager.get(this);
1317 for (UserHandle user : userManager.getUserProfiles()) {
1318 addExternalTiles(target, user, addedCache);
1319 }
1320 }
1321
1322 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1323 Map<Pair<String, String>, DashboardTile> addedCache) {
1324 PackageManager pm = getPackageManager();
1325 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1326 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1327 PackageManager.GET_META_DATA, user.getIdentifier());
1328 for (ResolveInfo resolved : results) {
1329 if (!resolved.system) {
1330 // Do not allow any app to add to settings, only system ones.
1331 continue;
1332 }
1333 ActivityInfo activityInfo = resolved.activityInfo;
1334 Bundle metaData = activityInfo.metaData;
1335 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1336 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1337 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1338 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1339 continue;
1340 }
1341 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1342 DashboardCategory category = getCategory(target, categoryKey);
1343 if (category == null) {
1344 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1345 + "category key " + categoryKey);
1346 continue;
1347 }
1348 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1349 activityInfo.name);
1350 DashboardTile tile = addedCache.get(key);
1351 if (tile == null) {
1352 tile = new DashboardTile();
1353 tile.intent = new Intent().setClassName(
1354 activityInfo.packageName, activityInfo.name);
1355 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1356
1357 if (category.externalIndex == -1) {
1358 // If no location for external tiles has been specified for this category,
1359 // then just put them at the end.
1360 category.addTile(tile);
1361 } else {
1362 category.addTile(category.externalIndex, tile);
1363 }
1364 addedCache.put(key, tile);
1365 }
1366 tile.userHandle.add(user);
1367 }
1368 }
1369
1370 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1371 for (DashboardCategory category : target) {
1372 if (categoryKey.equals(category.key)) {
1373 return category;
1374 }
1375 }
1376 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001377 }
1378
1379 private boolean updateHomeSettingTiles(DashboardTile tile) {
1380 // Once we decide to show Home settings, keep showing it forever
1381 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1382 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1383 return true;
1384 }
1385
1386 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001387 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001388 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001389 // When there's only one available home app, omit this settings
1390 // category entirely at the top level UI. If the user just
1391 // uninstalled the penultimate home app candidiate, we also
1392 // now tell them about why they aren't seeing 'Home' in the list.
1393 if (sShowNoHomeNotice) {
1394 sShowNoHomeNotice = false;
1395 NoHomeDialogFragment.show(this);
1396 }
1397 return false;
1398 } else {
1399 // Okay, we're allowing the Home settings category. Tell it, when
1400 // invoked via this front door, that we'll need to be told about the
1401 // case when the user uninstalls all but one home app.
1402 if (tile.fragmentArguments == null) {
1403 tile.fragmentArguments = new Bundle();
1404 }
1405 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1406 }
1407 } catch (Exception e) {
1408 // Can't look up the home activity; bail on configuring the icon
1409 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1410 }
1411
1412 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1413 return true;
1414 }
1415
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001416 private void getMetaData() {
1417 try {
1418 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1419 PackageManager.GET_META_DATA);
1420 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001421 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1422 } catch (NameNotFoundException nnfe) {
1423 // No recovery
1424 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1425 }
1426 }
1427
1428 // give subclasses access to the Next button
1429 public boolean hasNextButton() {
1430 return mNextButton != null;
1431 }
1432
1433 public Button getNextButton() {
1434 return mNextButton;
1435 }
1436
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001437 @Override
1438 public boolean shouldUpRecreateTask(Intent targetIntent) {
1439 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1440 }
1441
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001442 public static void requestHomeNotice() {
1443 sShowNoHomeNotice = true;
1444 }
1445
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001446 @Override
1447 public boolean onQueryTextSubmit(String query) {
1448 switchToSearchResultsFragmentIfNeeded();
1449 mSearchQuery = query;
1450 return mSearchResultsFragment.onQueryTextSubmit(query);
1451 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001452
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001453 @Override
1454 public boolean onQueryTextChange(String newText) {
1455 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001456 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001457 return false;
1458 }
1459 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001460 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001461
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001462 @Override
1463 public boolean onClose() {
1464 return false;
1465 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001466
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001467 @Override
1468 public boolean onMenuItemActionExpand(MenuItem item) {
1469 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001470 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001471 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001472 return true;
1473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001474
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 @Override
1476 public boolean onMenuItemActionCollapse(MenuItem item) {
1477 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001478 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001479 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001481 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001482 return true;
1483 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001484
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001485 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001486 if (mSearchResultsFragment != null) {
1487 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001488 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001489 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 if (current != null && current instanceof SearchResultsSummary) {
1491 mSearchResultsFragment = (SearchResultsSummary) current;
1492 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001493 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001494 SearchResultsSummary.class.getName(), null, false, true,
1495 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001496 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001497 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001498 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001499 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001500
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001501 public void needToRevertToInitialFragment() {
1502 mNeedToRevertToInitialFragment = true;
1503 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001504
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001505 private void revertToInitialFragment() {
1506 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001507 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001508 mSearchMenuItemExpanded = false;
1509 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1510 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001511 if (mSearchMenuItem != null) {
1512 mSearchMenuItem.collapseActionView();
1513 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001514 }
Jim Miller0698a212014-10-16 19:49:07 -07001515
1516 public Intent getResultIntentData() {
1517 return mResultIntentData;
1518 }
1519
1520 public void setResultIntentData(Intent resultIntentData) {
1521 mResultIntentData = resultIntentData;
1522 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001523}