blob: caf2491612b800dedbf31abb1e9e09a724ee08f6 [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 Sharkey42833b22015-04-11 21:27:33 -070086import com.android.settings.deviceinfo.PublicVolumeSettings;
87import com.android.settings.deviceinfo.StorageSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040089import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.fuelgauge.PowerUsageSummary;
91import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
92import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
93import com.android.settings.inputmethod.SpellCheckersSettings;
94import com.android.settings.inputmethod.UserDictionaryList;
95import com.android.settings.location.LocationSettings;
96import com.android.settings.nfc.AndroidBeam;
97import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040098import com.android.settings.notification.AppNotificationSettings;
John Spurlock4a350512014-04-08 14:08:21 -040099import com.android.settings.notification.NotificationAccessSettings;
100import com.android.settings.notification.NotificationSettings;
101import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500102import com.android.settings.notification.OtherSoundSettings;
John Spurlockc96a5dc2015-04-10 11:59:54 -0400103import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400104import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400105import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400106import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800107import com.android.settings.print.PrintJobSettingsFragment;
108import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500109import com.android.settings.search.DynamicIndexableContentMonitor;
110import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700111import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.tts.TextToSpeechSettings;
113import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700114import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import com.android.settings.vpn2.VpnSettings;
116import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700117import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700119import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import com.android.settings.wifi.WifiSettings;
121import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500122
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123import org.xmlpull.v1.XmlPullParser;
124import org.xmlpull.v1.XmlPullParserException;
125
126import java.io.IOException;
127import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800128import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500129import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700130import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131
132public class SettingsActivity extends Activity
133 implements PreferenceManager.OnPreferenceTreeClickListener,
134 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100135 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100137 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800138
139 private static final String LOG_TAG = "Settings";
140
141 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700142 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700143 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
144 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700145 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700146 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700147 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800148
149 /**
150 * When starting this activity, the invoking Intent can contain this extra
151 * string to specify which fragment should be initially displayed.
152 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
153 * will call isValidFragment() to confirm that the fragment class name is valid for this
154 * activity.
155 */
156 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
157
158 /**
159 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
160 * this extra can also be specified to supply a Bundle of arguments to pass
161 * to that fragment when it is instantiated during the initial creation
162 * of the activity.
163 */
164 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
165
166 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700167 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
168 */
169 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
170
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800171 public static final String BACK_STACK_PREFS = ":settings:prefs";
172
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 // extras that allow any preference activity to be launched as part of a wizard
174
175 // show Back and Next buttons? takes boolean parameter
176 // Back will then return RESULT_CANCELED and Next RESULT_OK
177 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
178
179 // add a Skip button?
180 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
181
182 // specify custom text for the Back or Next buttons, or cause a button to not appear
183 // at all by setting it to null
184 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
185 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
186
187 /**
188 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700189 * 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 -0800190 * that fragment.
191 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700192 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100193 /**
194 * The package name used to resolve the title resource id.
195 */
196 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
197 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700198 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
199 ":settings:show_fragment_title_resid";
200 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
201 ":settings:show_fragment_as_shortcut";
202
203 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
204 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206 private static final String META_DATA_KEY_FRAGMENT_CLASS =
207 "com.android.settings.FRAGMENT_CLASS";
208
209 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
210
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700211 private static final String EMPTY_QUERY = "";
212
Jason Monk2ebc8a02015-02-13 15:23:19 -0500213 /**
214 * Settings will search for system activities of this action and add them as a top level
215 * settings tile using the following parameters.
216 *
217 * <p>A category must be specified in the meta-data for the activity named
218 * {@link #EXTRA_CATEGORY_KEY}
219 *
220 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
221 * otherwise the label for the activity will be used.
222 *
223 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
224 * otherwise the icon for the activity will be used.
225 *
226 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
227 */
228 private static final String EXTRA_SETTINGS_ACTION =
229 "com.android.settings.action.EXTRA_SETTINGS";
230
231 /**
232 * The key used to get the category from metadata of activities of action
233 * {@link #EXTRA_SETTINGS_ACTION}
234 * The value must be one of:
235 * <li>com.android.settings.category.wireless</li>
236 * <li>com.android.settings.category.device</li>
237 * <li>com.android.settings.category.personal</li>
238 * <li>com.android.settings.category.system</li>
239 */
240 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
241
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800242 private static boolean sShowNoHomeNotice = false;
243
244 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800245
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800246 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700247 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800248
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800249 // Show only these settings for restricted users
250 private int[] SETTINGS_FOR_RESTRICTED = {
251 R.id.wireless_section,
252 R.id.wifi_settings,
253 R.id.bluetooth_settings,
254 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700255 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800256 R.id.wireless_settings,
257 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400258 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 R.id.display_settings,
260 R.id.storage_settings,
261 R.id.application_settings,
262 R.id.battery_settings,
263 R.id.personal_section,
264 R.id.location_settings,
265 R.id.security_settings,
266 R.id.language_settings,
267 R.id.user_settings,
268 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 R.id.system_section,
270 R.id.date_time_settings,
271 R.id.about_settings,
272 R.id.accessibility_settings,
273 R.id.print_settings,
274 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800275 R.id.home_settings,
276 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800277 };
278
279 private static final String[] ENTRY_FRAGMENTS = {
280 WirelessSettings.class.getName(),
281 WifiSettings.class.getName(),
282 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700283 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800284 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700285 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 TetherSettings.class.getName(),
287 WifiP2pSettings.class.getName(),
288 VpnSettings.class.getName(),
289 DateTimeSettings.class.getName(),
290 LocalePicker.class.getName(),
291 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700292 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 SpellCheckersSettings.class.getName(),
294 UserDictionaryList.class.getName(),
295 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700296 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 DisplaySettings.class.getName(),
298 DeviceInfoSettings.class.getName(),
299 ManageApplications.class.getName(),
300 ProcessStatsUi.class.getName(),
301 NotificationStation.class.getName(),
302 LocationSettings.class.getName(),
303 SecuritySettings.class.getName(),
Jason Monkd8da51c2015-04-17 14:34:12 -0400304 UsageAccessDetails.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305 PrivacySettings.class.getName(),
306 DeviceAdminSettings.class.getName(),
307 AccessibilitySettings.class.getName(),
308 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
310 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700311 StorageSettings.class.getName(),
312 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 DevelopmentSettings.class.getName(),
314 UsbSettings.class.getName(),
315 AndroidBeam.class.getName(),
316 WifiDisplaySettings.class.getName(),
317 PowerUsageSummary.class.getName(),
318 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000319 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 CryptKeeperSettings.class.getName(),
321 DataUsageSummary.class.getName(),
322 DreamSettings.class.getName(),
323 UserSettings.class.getName(),
324 NotificationAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 PrintSettingsFragment.class.getName(),
326 PrintJobSettingsFragment.class.getName(),
327 TrustedCredentialsSettings.class.getName(),
328 PaymentSettings.class.getName(),
329 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700330 ZenModeSettings.class.getName(),
331 NotificationSettings.class.getName(),
332 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
333 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400334 InstalledAppDetails.class.getName(),
335 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700336 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700337 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000338 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400339 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400340 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400341 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400342 ZenModeExternalRuleSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343 };
344
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700345
346 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
347 "android.settings.APPLICATION_DETAILS_SETTINGS"
348 };
349
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 private SharedPreferences mDevelopmentPreferences;
351 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
352
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 private boolean mBatteryPresent = true;
354 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 @Override
356 public void onReceive(Context context, Intent intent) {
357 String action = intent.getAction();
358 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
359 boolean batteryPresent = Utils.isBatteryPresent(intent);
360
361 if (mBatteryPresent != batteryPresent) {
362 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700363 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 }
365 }
366 }
367 };
368
Svetoslav990159a2014-04-14 17:14:59 -0700369 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
370 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700371
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700372 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700373 private SwitchBar mSwitchBar;
374
375 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700376
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700377 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700378 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700379
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700380 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700381 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700382
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700383 private ViewGroup mContent;
384
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700385 private SearchView mSearchView;
386 private MenuItem mSearchMenuItem;
387 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700388 private SearchResultsSummary mSearchResultsFragment;
389 private String mSearchQuery;
390
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700391 // Categories
392 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800393
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700394 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700395 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800396 private Handler mHandler = new Handler() {
397 @Override
398 public void handleMessage(Message msg) {
399 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700400 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700401 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
402 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700403 buildDashboardCategories(mCategories);
404 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405 } break;
406 }
407 }
408 };
409
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700410 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700411 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700412
Jim Miller0698a212014-10-16 19:49:07 -0700413 private Intent mResultIntentData;
414
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700415 public SwitchBar getSwitchBar() {
416 return mSwitchBar;
417 }
418
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700419 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
420 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700421 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700422 }
423 return mCategories;
424 }
425
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800426 @Override
427 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
428 // Override the fragment title for Wallpaper settings
429 int titleRes = pref.getTitleRes();
430 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
431 titleRes = R.string.wallpaper_settings_fragment_title;
432 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
433 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
434 if (UserManager.get(this).isLinkedUser()) {
435 titleRes = R.string.profile_info_settings_title;
436 } else {
437 titleRes = R.string.user_info_settings_title;
438 }
439 }
440 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
441 null, 0);
442 return true;
443 }
444
445 @Override
446 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
447 return false;
448 }
449
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700450 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700451 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700452 Message msg = new Message();
453 msg.what = MSG_BUILD_CATEGORIES;
454 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800455 }
456 }
457
458 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800459 public void onConfigurationChanged(Configuration newConfig) {
460 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800461 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800462 }
463
464 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700465 protected void onStart() {
466 super.onStart();
467
468 if (mNeedToRevertToInitialFragment) {
469 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800470 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 }
472
473 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700474 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700475 if (!mDisplaySearch) {
476 return false;
477 }
478
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700479 MenuInflater inflater = getMenuInflater();
480 inflater.inflate(R.menu.options_menu, menu);
481
482 // Cache the search query (can be overriden by the OnQueryTextListener)
483 final String query = mSearchQuery;
484
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700485 mSearchMenuItem = menu.findItem(R.id.search);
486 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700487
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700488 if (mSearchMenuItem == null || mSearchView == null) {
489 return false;
490 }
491
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700492 if (mSearchResultsFragment != null) {
493 mSearchResultsFragment.setSearchView(mSearchView);
494 }
495
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700496 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700497 mSearchView.setOnQueryTextListener(this);
498 mSearchView.setOnCloseListener(this);
499
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700500 if (mSearchMenuItemExpanded) {
501 mSearchMenuItem.expandActionView();
502 }
503 mSearchView.setQuery(query, true /* submit */);
504
505 return true;
506 }
507
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700508 private static boolean isShortCutIntent(final Intent intent) {
509 Set<String> categories = intent.getCategories();
510 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
511 }
512
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700513 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700514 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700515 if (action == null) {
516 return false;
517 }
518 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
519 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
520 }
521 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700522 }
523
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700524 @Override
525 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700526 super.onCreate(savedState);
527
528 // Should happen before any call to getIntent()
529 getMetaData();
530
531 final Intent intent = getIntent();
532 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
533 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
537 Context.MODE_PRIVATE);
538
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700539 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700540 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700541
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700542 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700543 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
544
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700545 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700546 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700547
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700548 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700549
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700550 // This is a "Sub Settings" when:
551 // - this is a real SubSettings
552 // - or :settings:show_fragment_as_subsetting is passed to the Intent
553 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
554 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
555
556 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
557 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700558 // Check also that we are not a Theme Dialog as we don't want to override them
559 final int themeResId = getThemeResId();
560 if (themeResId != R.style.Theme_DialogWhenLarge &&
561 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
562 setTheme(R.style.Theme_SubSettings);
563 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700564 }
565
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700566 setContentView(mIsShowingDashboard ?
567 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700569 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700570
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571 getFragmentManager().addOnBackStackChangedListener(this);
572
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700573 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800574 // Run the Index update only if we have some space
575 if (!Utils.isLowStorage(this)) {
576 Index.getInstance(getApplicationContext()).update();
577 } else {
578 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
579 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700580 }
581
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700582 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700583 // We are restarting from a previous saved state; used that to initialize, instead
584 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700585 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
586 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800587
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700588 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800589
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700590 ArrayList<DashboardCategory> categories =
591 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
592 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700593 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700594 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700595 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800596 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700597
598 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700599 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700600 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
601 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800602 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700603 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700604 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
605 // only if it is a sub settings
606 if (mIsShortcut) {
607 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700608 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700609 } else if (isSubSettings) {
610 mDisplayHomeAsUpEnabled = true;
611 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700612 } else {
613 mDisplayHomeAsUpEnabled = false;
614 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700615 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700616 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700617
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700618 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700619 switchToFragment(initialFragmentName, initialArguments, true, false,
620 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000621 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700622 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700623 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700624 // Show Search affordance
625 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700626 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700627 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700628 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800629 }
630 }
631
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700632 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700633 if (mActionBar != null) {
634 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
635 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
636 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700637 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
638
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800639 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
641
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700642 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800643 if (buttonBar != null) {
644 buttonBar.setVisibility(View.VISIBLE);
645
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700646 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 backButton.setOnClickListener(new OnClickListener() {
648 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700649 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 finish();
651 }
652 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700653 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 skipButton.setOnClickListener(new OnClickListener() {
655 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700656 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 finish();
658 }
659 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700660 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 mNextButton.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 });
667
668 // set our various button parameters
669 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
670 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
671 if (TextUtils.isEmpty(buttonText)) {
672 mNextButton.setVisibility(View.GONE);
673 }
674 else {
675 mNextButton.setText(buttonText);
676 }
677 }
678 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
679 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
680 if (TextUtils.isEmpty(buttonText)) {
681 backButton.setVisibility(View.GONE);
682 }
683 else {
684 backButton.setText(buttonText);
685 }
686 }
687 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
688 skipButton.setVisibility(View.VISIBLE);
689 }
690 }
691 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700692
693 mHomeActivitiesCount = getHomeActivitiesCount();
694 }
695
696 private int getHomeActivitiesCount() {
697 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
698 getPackageManager().getHomeActivities(homeApps);
699 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800700 }
701
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700702 private void setTitleFromIntent(Intent intent) {
703 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
704 if (initialTitleResId > 0) {
705 mInitialTitle = null;
706 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100707
708 final String initialTitleResPackageName = intent.getStringExtra(
709 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
710 if (initialTitleResPackageName != null) {
711 try {
712 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
713 0 /* flags */, new UserHandle(UserHandle.myUserId()));
714 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
715 setTitle(mInitialTitle);
716 mInitialTitleResId = -1;
717 return;
718 } catch (NameNotFoundException e) {
719 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
720 }
721 } else {
722 setTitle(mInitialTitleResId);
723 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700724 } else {
725 mInitialTitleResId = -1;
726 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
727 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
728 setTitle(mInitialTitle);
729 }
730 }
731
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800732 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700734 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800735 }
736
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700737 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800738 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700739
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800740 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700741 if (mInitialTitleResId > 0) {
742 setTitle(mInitialTitleResId);
743 } else {
744 setTitle(mInitialTitle);
745 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700746 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800747 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800749 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
750 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700751
752 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800753 }
754
755 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
756 final CharSequence title;
757 final int titleRes = bse.getBreadCrumbTitleRes();
758 if (titleRes > 0) {
759 title = getText(titleRes);
760 } else {
761 title = bse.getBreadCrumbTitle();
762 }
763 if (title != null) {
764 setTitle(title);
765 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800766 }
767
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800768 @Override
769 protected void onSaveInstanceState(Bundle outState) {
770 super.onSaveInstanceState(outState);
771
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700772 if (mCategories.size() > 0) {
773 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700775
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700776 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700777 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700778
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700779 if (mDisplaySearch) {
780 // The option menus are created if the ActionBar is visible and they are also created
781 // asynchronously. If you launch Settings with an Intent action like
782 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
783 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
784 // menu item and search view are null.
785 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
786 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700787
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700788 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
789 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
790 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700791
792 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800793 }
794
795 @Override
796 public void onResume() {
797 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400798 if (mIsShowingDashboard) {
799 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
800 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800801
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700802 final int newHomeActivityCount = getHomeActivitiesCount();
803 if (newHomeActivityCount != mHomeActivitiesCount) {
804 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700805 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700806 }
807
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800808 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
809 @Override
810 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700811 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800812 }
813 };
814 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
815 mDevelopmentPreferencesListener);
816
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800817 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700818
Svetoslav990159a2014-04-14 17:14:59 -0700819 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700820
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700821 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700822 onQueryTextSubmit(mSearchQuery);
823 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800824 }
825
826 @Override
827 public void onPause() {
828 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400829 if (mIsShowingDashboard) {
830 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
831 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800832 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700833 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800834 }
835
836 @Override
837 public void onDestroy() {
838 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700839
840 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
841 mDevelopmentPreferencesListener);
842 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800843 }
844
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800845 protected boolean isValidFragment(String fragmentName) {
846 // Almost all fragments are wrapped in this,
847 // except for a few that have their own activities.
848 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
849 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
850 }
851 return false;
852 }
853
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 @Override
855 public Intent getIntent() {
856 Intent superIntent = super.getIntent();
857 String startingFragment = getStartingFragmentClass(superIntent);
858 // This is called from super.onCreate, isMultiPane() is not yet reliable
859 // Do not use onIsHidingHeaders either, which relies itself on this method
860 if (startingFragment != null) {
861 Intent modIntent = new Intent(superIntent);
862 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
863 Bundle args = superIntent.getExtras();
864 if (args != null) {
865 args = new Bundle(args);
866 } else {
867 args = new Bundle();
868 }
869 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100870 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800871 return modIntent;
872 }
873 return superIntent;
874 }
875
876 /**
877 * Checks if the component name in the intent is different from the Settings class and
878 * returns the class name to load as a fragment.
879 */
880 private String getStartingFragmentClass(Intent intent) {
881 if (mFragmentClass != null) return mFragmentClass;
882
883 String intentClass = intent.getComponent().getClassName();
884 if (intentClass.equals(getClass().getName())) return null;
885
886 if ("com.android.settings.ManageApplications".equals(intentClass)
887 || "com.android.settings.RunningServices".equals(intentClass)
888 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
889 // Old names of manage apps.
890 intentClass = com.android.settings.applications.ManageApplications.class.getName();
891 }
892
893 return intentClass;
894 }
895
896 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000897 * Start a new fragment containing a preference panel. If the preferences
898 * are being displayed in multi-pane mode, the given fragment class will
899 * be instantiated and placed in the appropriate pane. If running in
900 * single-pane mode, a new activity will be launched in which to show the
901 * fragment.
902 *
903 * @param fragmentClass Full name of the class implementing the fragment.
904 * @param args Any desired arguments to supply to the fragment.
905 * @param titleRes Optional resource identifier of the title of this
906 * fragment.
907 * @param titleText Optional text of the title of this fragment.
908 * @param resultTo Optional fragment that result data should be sent to.
909 * If non-null, resultTo.onActivityResult() will be called when this
910 * preference panel is done. The launched panel must use
911 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
912 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700913 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000914 */
915 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700916 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700917 String title = null;
918 if (titleRes < 0) {
919 if (titleText != null) {
920 title = titleText.toString();
921 } else {
922 // There not much we can do in that case
923 title = "";
924 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700925 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700926 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700927 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000928 }
929
930 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100931 * Start a new fragment in a new activity containing a preference panel for a given user. If the
932 * preferences are being displayed in multi-pane mode, the given fragment class will be
933 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
934 * activity will be launched in which to show the fragment.
935 *
936 * @param fragmentClass Full name of the class implementing the fragment.
937 * @param args Any desired arguments to supply to the fragment.
938 * @param titleRes Optional resource identifier of the title of this fragment.
939 * @param titleText Optional text of the title of this fragment.
940 * @param userHandle The user for which the panel has to be started.
941 */
942 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
943 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700944 // This is a workaround.
945 //
946 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
947 // starting the fragment could cause a native stack corruption. See b/17523189. However,
948 // adding that flag and start the preference panel with the same UserHandler will make it
949 // impossible to use back button to return to the previous screen. See b/20042570.
950 //
951 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
952 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
953 // when we're calling it as the same user.
954 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
955 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
956 } else {
957 String title = null;
958 if (titleRes < 0) {
959 if (titleText != null) {
960 title = titleText.toString();
961 } else {
962 // There not much we can do in that case
963 title = "";
964 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100965 }
Lifu Tangd0332852015-04-02 12:05:46 -0700966 Utils.startWithFragmentAsUser(this, fragmentClass, args,
967 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100968 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100969 }
970
971 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800972 * Called by a preference panel fragment to finish itself.
973 *
974 * @param caller The fragment that is asking to be finished.
975 * @param resultCode Optional result code to send back to the original
976 * launching fragment.
977 * @param resultData Optional result data to send back to the original
978 * launching fragment.
979 */
980 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
981 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700982 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800983 }
984
985 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000986 * Start a new fragment.
987 *
988 * @param fragment The fragment to start
989 * @param push If true, the current fragment will be pushed onto the back stack. If false,
990 * the current fragment will be replaced.
991 */
992 public void startPreferenceFragment(Fragment fragment, boolean push) {
993 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700994 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000995 if (push) {
996 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
997 transaction.addToBackStack(BACK_STACK_PREFS);
998 } else {
999 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
1000 }
1001 transaction.commitAllowingStateLoss();
1002 }
1003
1004 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001005 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1006 */
1007 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001008 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001009 if (validate && !isValidFragment(fragmentName)) {
1010 throw new IllegalArgumentException("Invalid fragment for this activity: "
1011 + fragmentName);
1012 }
1013 Fragment f = Fragment.instantiate(this, fragmentName, args);
1014 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001015 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001016 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001017 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001018 }
1019 if (addToBackStack) {
1020 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1021 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001022 if (titleResId > 0) {
1023 transaction.setBreadCrumbTitle(titleResId);
1024 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001025 transaction.setBreadCrumbTitle(title);
1026 }
1027 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001028 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001029 return f;
1030 }
1031
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001032 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001033 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001034 *
1035 * @param categories The list in which to place the tiles categories.
1036 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001037 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001038 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001039 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1040 updateTilesList(categories);
1041 }
1042
1043 /**
1044 * Parse the given XML file as a categories description, adding each
1045 * parsed categories and tiles into the target list.
1046 *
1047 * @param resid The XML resource to load and parse.
1048 * @param target The list in which the parsed categories and tiles should be placed.
1049 */
1050 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1051 XmlResourceParser parser = null;
1052 try {
1053 parser = getResources().getXml(resid);
1054 AttributeSet attrs = Xml.asAttributeSet(parser);
1055
1056 int type;
1057 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1058 && type != XmlPullParser.START_TAG) {
1059 // Parse next until start tag is found
1060 }
1061
1062 String nodeName = parser.getName();
1063 if (!"dashboard-categories".equals(nodeName)) {
1064 throw new RuntimeException(
1065 "XML document must start with <preference-categories> tag; found"
1066 + nodeName + " at " + parser.getPositionDescription());
1067 }
1068
1069 Bundle curBundle = null;
1070
1071 final int outerDepth = parser.getDepth();
1072 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1073 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1074 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1075 continue;
1076 }
1077
1078 nodeName = parser.getName();
1079 if ("dashboard-category".equals(nodeName)) {
1080 DashboardCategory category = new DashboardCategory();
1081
1082 TypedArray sa = obtainStyledAttributes(
1083 attrs, com.android.internal.R.styleable.PreferenceHeader);
1084 category.id = sa.getResourceId(
1085 com.android.internal.R.styleable.PreferenceHeader_id,
1086 (int)DashboardCategory.CAT_ID_UNDEFINED);
1087
1088 TypedValue tv = sa.peekValue(
1089 com.android.internal.R.styleable.PreferenceHeader_title);
1090 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1091 if (tv.resourceId != 0) {
1092 category.titleRes = tv.resourceId;
1093 } else {
1094 category.title = tv.string;
1095 }
1096 }
1097 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001098 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1099 tv = sa.peekValue(
1100 com.android.internal.R.styleable.Preference_key);
1101 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1102 if (tv.resourceId != 0) {
1103 category.key = getString(tv.resourceId);
1104 } else {
1105 category.key = tv.string.toString();
1106 }
1107 }
1108 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001109
1110 final int innerDepth = parser.getDepth();
1111 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1112 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1113 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1114 continue;
1115 }
1116
1117 String innerNodeName = parser.getName();
1118 if (innerNodeName.equals("dashboard-tile")) {
1119 DashboardTile tile = new DashboardTile();
1120
1121 sa = obtainStyledAttributes(
1122 attrs, com.android.internal.R.styleable.PreferenceHeader);
1123 tile.id = sa.getResourceId(
1124 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001125 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001126 tv = sa.peekValue(
1127 com.android.internal.R.styleable.PreferenceHeader_title);
1128 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1129 if (tv.resourceId != 0) {
1130 tile.titleRes = tv.resourceId;
1131 } else {
1132 tile.title = tv.string;
1133 }
1134 }
1135 tv = sa.peekValue(
1136 com.android.internal.R.styleable.PreferenceHeader_summary);
1137 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1138 if (tv.resourceId != 0) {
1139 tile.summaryRes = tv.resourceId;
1140 } else {
1141 tile.summary = tv.string;
1142 }
1143 }
1144 tile.iconRes = sa.getResourceId(
1145 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1146 tile.fragment = sa.getString(
1147 com.android.internal.R.styleable.PreferenceHeader_fragment);
1148 sa.recycle();
1149
1150 if (curBundle == null) {
1151 curBundle = new Bundle();
1152 }
1153
1154 final int innerDepth2 = parser.getDepth();
1155 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1156 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1157 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1158 continue;
1159 }
1160
1161 String innerNodeName2 = parser.getName();
1162 if (innerNodeName2.equals("extra")) {
1163 getResources().parseBundleExtra("extra", attrs, curBundle);
1164 XmlUtils.skipCurrentTag(parser);
1165
1166 } else if (innerNodeName2.equals("intent")) {
1167 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1168
1169 } else {
1170 XmlUtils.skipCurrentTag(parser);
1171 }
1172 }
1173
1174 if (curBundle.size() > 0) {
1175 tile.fragmentArguments = curBundle;
1176 curBundle = null;
1177 }
1178
PauloftheWest38155612014-06-30 10:02:36 -07001179 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001180 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001181 category.addTile(tile);
1182 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001183
Jason Monk2ebc8a02015-02-13 15:23:19 -05001184 } else if (innerNodeName.equals("external-tiles")) {
1185 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001186 } else {
1187 XmlUtils.skipCurrentTag(parser);
1188 }
1189 }
1190
1191 target.add(category);
1192 } else {
1193 XmlUtils.skipCurrentTag(parser);
1194 }
1195 }
1196
1197 } catch (XmlPullParserException e) {
1198 throw new RuntimeException("Error parsing categories", e);
1199 } catch (IOException e) {
1200 throw new RuntimeException("Error parsing categories", e);
1201 } finally {
1202 if (parser != null) parser.close();
1203 }
1204 }
1205
1206 private void updateTilesList(List<DashboardCategory> target) {
1207 final boolean showDev = mDevelopmentPreferences.getBoolean(
1208 DevelopmentSettings.PREF_SHOW,
1209 android.os.Build.TYPE.equals("eng"));
1210
1211 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1212
1213 final int size = target.size();
1214 for (int i = 0; i < size; i++) {
1215
1216 DashboardCategory category = target.get(i);
1217
1218 // Ids are integers, so downcasting is ok
1219 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001220 int n = category.getTilesCount() - 1;
1221 while (n >= 0) {
1222
1223 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001224 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001225 id = (int) tile.id;
1226 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001227 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1228 removeTile = true;
1229 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001230 } else if (id == R.id.wifi_settings) {
1231 // Remove WiFi Settings if WiFi service is not available.
1232 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001233 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001234 }
1235 } else if (id == R.id.bluetooth_settings) {
1236 // Remove Bluetooth Settings if Bluetooth service is not available.
1237 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001238 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001239 }
1240 } else if (id == R.id.data_usage_settings) {
1241 // Remove data usage when kernel module not enabled
1242 final INetworkManagementService netManager = INetworkManagementService.Stub
1243 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1244 try {
1245 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001246 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001247 }
1248 } catch (RemoteException e) {
1249 // ignored
1250 }
1251 } else if (id == R.id.battery_settings) {
1252 // Remove battery settings when battery is not available. (e.g. TV)
1253
1254 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001255 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001256 }
1257 } else if (id == R.id.home_settings) {
1258 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001259 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001260 }
1261 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001262 boolean hasMultipleUsers =
1263 ((UserManager) getSystemService(Context.USER_SERVICE))
1264 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001266 || (!UserManager.supportsMultipleUsers()
1267 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001268 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001269 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001270 }
1271 } else if (id == R.id.nfc_payment_settings) {
1272 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001273 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001274 } else {
1275 // Only show if NFC is on and we have the HCE feature
1276 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001277 if (adapter == null || !adapter.isEnabled() ||
1278 !getPackageManager().hasSystemFeature(
1279 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001280 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001281 }
1282 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001283 } else if (id == R.id.print_settings) {
1284 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1285 PackageManager.FEATURE_PRINTING);
1286 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001287 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001288 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001289 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001290 if (!showDev || um.hasUserRestriction(
1291 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001292 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001293 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001294 }
1295
1296 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001297 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1298 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001299 }
1300
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001301 if (removeTile && n < category.getTilesCount()) {
1302 category.removeTile(n);
1303 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001304 n--;
1305 }
1306 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001307 addExternalTiles(target);
1308 }
1309
1310 private void addExternalTiles(List<DashboardCategory> target) {
1311 Map<Pair<String, String>, DashboardTile> addedCache =
1312 new ArrayMap<Pair<String, String>, DashboardTile>();
1313 UserManager userManager = UserManager.get(this);
1314 for (UserHandle user : userManager.getUserProfiles()) {
1315 addExternalTiles(target, user, addedCache);
1316 }
1317 }
1318
1319 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1320 Map<Pair<String, String>, DashboardTile> addedCache) {
1321 PackageManager pm = getPackageManager();
1322 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1323 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1324 PackageManager.GET_META_DATA, user.getIdentifier());
1325 for (ResolveInfo resolved : results) {
1326 if (!resolved.system) {
1327 // Do not allow any app to add to settings, only system ones.
1328 continue;
1329 }
1330 ActivityInfo activityInfo = resolved.activityInfo;
1331 Bundle metaData = activityInfo.metaData;
1332 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1333 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1334 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1335 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1336 continue;
1337 }
1338 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1339 DashboardCategory category = getCategory(target, categoryKey);
1340 if (category == null) {
1341 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1342 + "category key " + categoryKey);
1343 continue;
1344 }
1345 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1346 activityInfo.name);
1347 DashboardTile tile = addedCache.get(key);
1348 if (tile == null) {
1349 tile = new DashboardTile();
1350 tile.intent = new Intent().setClassName(
1351 activityInfo.packageName, activityInfo.name);
1352 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1353
1354 if (category.externalIndex == -1) {
1355 // If no location for external tiles has been specified for this category,
1356 // then just put them at the end.
1357 category.addTile(tile);
1358 } else {
1359 category.addTile(category.externalIndex, tile);
1360 }
1361 addedCache.put(key, tile);
1362 }
1363 tile.userHandle.add(user);
1364 }
1365 }
1366
1367 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1368 for (DashboardCategory category : target) {
1369 if (categoryKey.equals(category.key)) {
1370 return category;
1371 }
1372 }
1373 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001374 }
1375
1376 private boolean updateHomeSettingTiles(DashboardTile tile) {
1377 // Once we decide to show Home settings, keep showing it forever
1378 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1379 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1380 return true;
1381 }
1382
1383 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001384 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001385 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001386 // When there's only one available home app, omit this settings
1387 // category entirely at the top level UI. If the user just
1388 // uninstalled the penultimate home app candidiate, we also
1389 // now tell them about why they aren't seeing 'Home' in the list.
1390 if (sShowNoHomeNotice) {
1391 sShowNoHomeNotice = false;
1392 NoHomeDialogFragment.show(this);
1393 }
1394 return false;
1395 } else {
1396 // Okay, we're allowing the Home settings category. Tell it, when
1397 // invoked via this front door, that we'll need to be told about the
1398 // case when the user uninstalls all but one home app.
1399 if (tile.fragmentArguments == null) {
1400 tile.fragmentArguments = new Bundle();
1401 }
1402 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1403 }
1404 } catch (Exception e) {
1405 // Can't look up the home activity; bail on configuring the icon
1406 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1407 }
1408
1409 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1410 return true;
1411 }
1412
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001413 private void getMetaData() {
1414 try {
1415 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1416 PackageManager.GET_META_DATA);
1417 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001418 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1419 } catch (NameNotFoundException nnfe) {
1420 // No recovery
1421 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1422 }
1423 }
1424
1425 // give subclasses access to the Next button
1426 public boolean hasNextButton() {
1427 return mNextButton != null;
1428 }
1429
1430 public Button getNextButton() {
1431 return mNextButton;
1432 }
1433
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001434 @Override
1435 public boolean shouldUpRecreateTask(Intent targetIntent) {
1436 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1437 }
1438
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001439 public static void requestHomeNotice() {
1440 sShowNoHomeNotice = true;
1441 }
1442
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001443 @Override
1444 public boolean onQueryTextSubmit(String query) {
1445 switchToSearchResultsFragmentIfNeeded();
1446 mSearchQuery = query;
1447 return mSearchResultsFragment.onQueryTextSubmit(query);
1448 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001449
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001450 @Override
1451 public boolean onQueryTextChange(String newText) {
1452 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001453 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001454 return false;
1455 }
1456 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001457 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001458
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001459 @Override
1460 public boolean onClose() {
1461 return false;
1462 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001463
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001464 @Override
1465 public boolean onMenuItemActionExpand(MenuItem item) {
1466 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001467 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001468 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001469 return true;
1470 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001471
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001472 @Override
1473 public boolean onMenuItemActionCollapse(MenuItem item) {
1474 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001475 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001476 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001477 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001478 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001479 return true;
1480 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001481
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001482 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001483 if (mSearchResultsFragment != null) {
1484 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001485 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001486 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001487 if (current != null && current instanceof SearchResultsSummary) {
1488 mSearchResultsFragment = (SearchResultsSummary) current;
1489 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001491 SearchResultsSummary.class.getName(), null, false, true,
1492 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001493 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001494 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001495 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001496 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001497
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001498 public void needToRevertToInitialFragment() {
1499 mNeedToRevertToInitialFragment = true;
1500 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001501
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001502 private void revertToInitialFragment() {
1503 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001504 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001505 mSearchMenuItemExpanded = false;
1506 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1507 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001508 if (mSearchMenuItem != null) {
1509 mSearchMenuItem.collapseActionView();
1510 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001511 }
Jim Miller0698a212014-10-16 19:49:07 -07001512
1513 public Intent getResultIntentData() {
1514 return mResultIntentData;
1515 }
1516
1517 public void setResultIntentData(Intent resultIntentData) {
1518 mResultIntentData = resultIntentData;
1519 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001520}