blob: 24209b003a33cdb514f1e55616152f3fdfc92a25 [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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070080import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070082import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070083import com.android.settings.dashboard.NoHomeDialogFragment;
84import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085import com.android.settings.deviceinfo.Memory;
86import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040087import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088import com.android.settings.fuelgauge.PowerUsageSummary;
89import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
90import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
91import com.android.settings.inputmethod.SpellCheckersSettings;
92import com.android.settings.inputmethod.UserDictionaryList;
93import com.android.settings.location.LocationSettings;
94import com.android.settings.nfc.AndroidBeam;
95import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040096import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040097import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500101import com.android.settings.notification.OtherSoundSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400102import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400103import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800104import com.android.settings.print.PrintJobSettingsFragment;
105import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500106import com.android.settings.search.DynamicIndexableContentMonitor;
107import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700108import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.tts.TextToSpeechSettings;
110import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700111import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.vpn2.VpnSettings;
113import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700114import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700116import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import com.android.settings.wifi.WifiSettings;
118import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500119
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import org.xmlpull.v1.XmlPullParser;
121import org.xmlpull.v1.XmlPullParserException;
122
123import java.io.IOException;
124import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500126import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700127import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800128
129public class SettingsActivity extends Activity
130 implements PreferenceManager.OnPreferenceTreeClickListener,
131 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100132 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700133 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100134 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800135
136 private static final String LOG_TAG = "Settings";
137
138 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700139 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700140 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
141 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700142 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700143 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700144 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800145
146 /**
147 * When starting this activity, the invoking Intent can contain this extra
148 * string to specify which fragment should be initially displayed.
149 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
150 * will call isValidFragment() to confirm that the fragment class name is valid for this
151 * activity.
152 */
153 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
154
155 /**
156 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
157 * this extra can also be specified to supply a Bundle of arguments to pass
158 * to that fragment when it is instantiated during the initial creation
159 * of the activity.
160 */
161 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
162
163 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700164 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
165 */
166 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
167
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800168 public static final String BACK_STACK_PREFS = ":settings:prefs";
169
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170 // extras that allow any preference activity to be launched as part of a wizard
171
172 // show Back and Next buttons? takes boolean parameter
173 // Back will then return RESULT_CANCELED and Next RESULT_OK
174 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
175
176 // add a Skip button?
177 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
178
179 // specify custom text for the Back or Next buttons, or cause a button to not appear
180 // at all by setting it to null
181 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
182 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
183
184 /**
185 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700186 * 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 -0800187 * that fragment.
188 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700189 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100190 /**
191 * The package name used to resolve the title resource id.
192 */
193 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
194 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700195 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
196 ":settings:show_fragment_title_resid";
197 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
198 ":settings:show_fragment_as_shortcut";
199
200 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
201 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203 private static final String META_DATA_KEY_FRAGMENT_CLASS =
204 "com.android.settings.FRAGMENT_CLASS";
205
206 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
207
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700208 private static final String EMPTY_QUERY = "";
209
Jason Monk2ebc8a02015-02-13 15:23:19 -0500210 /**
211 * Settings will search for system activities of this action and add them as a top level
212 * settings tile using the following parameters.
213 *
214 * <p>A category must be specified in the meta-data for the activity named
215 * {@link #EXTRA_CATEGORY_KEY}
216 *
217 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
218 * otherwise the label for the activity will be used.
219 *
220 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
221 * otherwise the icon for the activity will be used.
222 *
223 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
224 */
225 private static final String EXTRA_SETTINGS_ACTION =
226 "com.android.settings.action.EXTRA_SETTINGS";
227
228 /**
229 * The key used to get the category from metadata of activities of action
230 * {@link #EXTRA_SETTINGS_ACTION}
231 * The value must be one of:
232 * <li>com.android.settings.category.wireless</li>
233 * <li>com.android.settings.category.device</li>
234 * <li>com.android.settings.category.personal</li>
235 * <li>com.android.settings.category.system</li>
236 */
237 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
238
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800239 private static boolean sShowNoHomeNotice = false;
240
241 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800242
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800243 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700244 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800245
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 // Show only these settings for restricted users
247 private int[] SETTINGS_FOR_RESTRICTED = {
248 R.id.wireless_section,
249 R.id.wifi_settings,
250 R.id.bluetooth_settings,
251 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700252 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800253 R.id.wireless_settings,
254 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400255 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800256 R.id.display_settings,
257 R.id.storage_settings,
258 R.id.application_settings,
259 R.id.battery_settings,
260 R.id.personal_section,
261 R.id.location_settings,
262 R.id.security_settings,
263 R.id.language_settings,
264 R.id.user_settings,
265 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800266 R.id.system_section,
267 R.id.date_time_settings,
268 R.id.about_settings,
269 R.id.accessibility_settings,
270 R.id.print_settings,
271 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800272 R.id.home_settings,
273 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 };
275
276 private static final String[] ENTRY_FRAGMENTS = {
277 WirelessSettings.class.getName(),
278 WifiSettings.class.getName(),
279 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700280 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800281 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700282 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800283 TetherSettings.class.getName(),
284 WifiP2pSettings.class.getName(),
285 VpnSettings.class.getName(),
286 DateTimeSettings.class.getName(),
287 LocalePicker.class.getName(),
288 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700289 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 SpellCheckersSettings.class.getName(),
291 UserDictionaryList.class.getName(),
292 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700293 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 DisplaySettings.class.getName(),
295 DeviceInfoSettings.class.getName(),
296 ManageApplications.class.getName(),
297 ProcessStatsUi.class.getName(),
298 NotificationStation.class.getName(),
299 LocationSettings.class.getName(),
300 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700301 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 PrivacySettings.class.getName(),
303 DeviceAdminSettings.class.getName(),
304 AccessibilitySettings.class.getName(),
305 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
307 TextToSpeechSettings.class.getName(),
308 Memory.class.getName(),
309 DevelopmentSettings.class.getName(),
310 UsbSettings.class.getName(),
311 AndroidBeam.class.getName(),
312 WifiDisplaySettings.class.getName(),
313 PowerUsageSummary.class.getName(),
314 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000315 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 CryptKeeperSettings.class.getName(),
317 DataUsageSummary.class.getName(),
318 DreamSettings.class.getName(),
319 UserSettings.class.getName(),
320 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400321 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 PrintSettingsFragment.class.getName(),
323 PrintJobSettingsFragment.class.getName(),
324 TrustedCredentialsSettings.class.getName(),
325 PaymentSettings.class.getName(),
326 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700327 ZenModeSettings.class.getName(),
328 NotificationSettings.class.getName(),
329 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
330 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400331 InstalledAppDetails.class.getName(),
332 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700333 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700334 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000335 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400336 WifiCallingSettings.class.getName(),
337 ZenModeScheduleRuleSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800338 };
339
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700340
341 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
342 "android.settings.APPLICATION_DETAILS_SETTINGS"
343 };
344
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 private SharedPreferences mDevelopmentPreferences;
346 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
347
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 private boolean mBatteryPresent = true;
349 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 @Override
351 public void onReceive(Context context, Intent intent) {
352 String action = intent.getAction();
353 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
354 boolean batteryPresent = Utils.isBatteryPresent(intent);
355
356 if (mBatteryPresent != batteryPresent) {
357 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700358 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 }
360 }
361 }
362 };
363
Svetoslav990159a2014-04-14 17:14:59 -0700364 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
365 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700366
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700367 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700368 private SwitchBar mSwitchBar;
369
370 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700371
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700372 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700373 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700374
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700375 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700376 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700377
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700378 private ViewGroup mContent;
379
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700380 private SearchView mSearchView;
381 private MenuItem mSearchMenuItem;
382 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700383 private SearchResultsSummary mSearchResultsFragment;
384 private String mSearchQuery;
385
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700386 // Categories
387 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800388
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700389 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700390 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800391 private Handler mHandler = new Handler() {
392 @Override
393 public void handleMessage(Message msg) {
394 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700395 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700396 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
397 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700398 buildDashboardCategories(mCategories);
399 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800400 } break;
401 }
402 }
403 };
404
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700405 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700406 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700407
Jim Miller0698a212014-10-16 19:49:07 -0700408 private Intent mResultIntentData;
409
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700410 public SwitchBar getSwitchBar() {
411 return mSwitchBar;
412 }
413
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700414 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
415 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700416 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700417 }
418 return mCategories;
419 }
420
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800421 @Override
422 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
423 // Override the fragment title for Wallpaper settings
424 int titleRes = pref.getTitleRes();
425 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
426 titleRes = R.string.wallpaper_settings_fragment_title;
427 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
428 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
429 if (UserManager.get(this).isLinkedUser()) {
430 titleRes = R.string.profile_info_settings_title;
431 } else {
432 titleRes = R.string.user_info_settings_title;
433 }
434 }
435 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
436 null, 0);
437 return true;
438 }
439
440 @Override
441 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
442 return false;
443 }
444
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700445 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700446 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700447 Message msg = new Message();
448 msg.what = MSG_BUILD_CATEGORIES;
449 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800450 }
451 }
452
453 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800454 public void onConfigurationChanged(Configuration newConfig) {
455 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800456 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800457 }
458
459 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700460 protected void onStart() {
461 super.onStart();
462
463 if (mNeedToRevertToInitialFragment) {
464 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 }
467
468 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700469 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700470 if (!mDisplaySearch) {
471 return false;
472 }
473
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700474 MenuInflater inflater = getMenuInflater();
475 inflater.inflate(R.menu.options_menu, menu);
476
477 // Cache the search query (can be overriden by the OnQueryTextListener)
478 final String query = mSearchQuery;
479
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700480 mSearchMenuItem = menu.findItem(R.id.search);
481 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700482
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700483 if (mSearchMenuItem == null || mSearchView == null) {
484 return false;
485 }
486
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700487 if (mSearchResultsFragment != null) {
488 mSearchResultsFragment.setSearchView(mSearchView);
489 }
490
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700491 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700492 mSearchView.setOnQueryTextListener(this);
493 mSearchView.setOnCloseListener(this);
494
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700495 if (mSearchMenuItemExpanded) {
496 mSearchMenuItem.expandActionView();
497 }
498 mSearchView.setQuery(query, true /* submit */);
499
500 return true;
501 }
502
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700503 private static boolean isShortCutIntent(final Intent intent) {
504 Set<String> categories = intent.getCategories();
505 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
506 }
507
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700508 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700509 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700510 if (action == null) {
511 return false;
512 }
513 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
514 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
515 }
516 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700517 }
518
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700519 @Override
520 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700521 super.onCreate(savedState);
522
523 // Should happen before any call to getIntent()
524 getMetaData();
525
526 final Intent intent = getIntent();
527 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
528 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800529 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
532 Context.MODE_PRIVATE);
533
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700534 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700535 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700536
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700537 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700538 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
539
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700540 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700541 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700542
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700543 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700544
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700545 // This is a "Sub Settings" when:
546 // - this is a real SubSettings
547 // - or :settings:show_fragment_as_subsetting is passed to the Intent
548 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
549 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
550
551 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
552 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700553 // Check also that we are not a Theme Dialog as we don't want to override them
554 final int themeResId = getThemeResId();
555 if (themeResId != R.style.Theme_DialogWhenLarge &&
556 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
557 setTheme(R.style.Theme_SubSettings);
558 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700559 }
560
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700561 setContentView(mIsShowingDashboard ?
562 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800563
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700564 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700565
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 getFragmentManager().addOnBackStackChangedListener(this);
567
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700568 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800569 // Run the Index update only if we have some space
570 if (!Utils.isLowStorage(this)) {
571 Index.getInstance(getApplicationContext()).update();
572 } else {
573 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
574 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700575 }
576
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700577 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700578 // We are restarting from a previous saved state; used that to initialize, instead
579 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700580 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
581 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700583 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800584
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700585 ArrayList<DashboardCategory> categories =
586 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
587 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700588 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700589 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700590 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800591 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700592
593 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700594 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700595 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
596 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700598 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700599 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
600 // only if it is a sub settings
601 if (mIsShortcut) {
602 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700603 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700604 } else if (isSubSettings) {
605 mDisplayHomeAsUpEnabled = true;
606 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700607 } else {
608 mDisplayHomeAsUpEnabled = false;
609 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700610 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700611 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700612
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700613 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700614 switchToFragment(initialFragmentName, initialArguments, true, false,
615 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000616 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700617 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700618 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700619 // Show Search affordance
620 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700621 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700622 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700623 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800624 }
625 }
626
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700627 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700628 if (mActionBar != null) {
629 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
630 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
631 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700632 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
633
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800634 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800635 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
636
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700637 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800638 if (buttonBar != null) {
639 buttonBar.setVisibility(View.VISIBLE);
640
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700641 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 backButton.setOnClickListener(new OnClickListener() {
643 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700644 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 finish();
646 }
647 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700648 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 skipButton.setOnClickListener(new OnClickListener() {
650 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700651 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 finish();
653 }
654 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700655 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 mNextButton.setOnClickListener(new OnClickListener() {
657 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700658 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 finish();
660 }
661 });
662
663 // set our various button parameters
664 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
665 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
666 if (TextUtils.isEmpty(buttonText)) {
667 mNextButton.setVisibility(View.GONE);
668 }
669 else {
670 mNextButton.setText(buttonText);
671 }
672 }
673 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
674 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
675 if (TextUtils.isEmpty(buttonText)) {
676 backButton.setVisibility(View.GONE);
677 }
678 else {
679 backButton.setText(buttonText);
680 }
681 }
682 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
683 skipButton.setVisibility(View.VISIBLE);
684 }
685 }
686 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700687
688 mHomeActivitiesCount = getHomeActivitiesCount();
689 }
690
691 private int getHomeActivitiesCount() {
692 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
693 getPackageManager().getHomeActivities(homeApps);
694 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800695 }
696
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700697 private void setTitleFromIntent(Intent intent) {
698 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
699 if (initialTitleResId > 0) {
700 mInitialTitle = null;
701 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100702
703 final String initialTitleResPackageName = intent.getStringExtra(
704 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
705 if (initialTitleResPackageName != null) {
706 try {
707 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
708 0 /* flags */, new UserHandle(UserHandle.myUserId()));
709 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
710 setTitle(mInitialTitle);
711 mInitialTitleResId = -1;
712 return;
713 } catch (NameNotFoundException e) {
714 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
715 }
716 } else {
717 setTitle(mInitialTitleResId);
718 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700719 } else {
720 mInitialTitleResId = -1;
721 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
722 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
723 setTitle(mInitialTitle);
724 }
725 }
726
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800727 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800728 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700729 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800730 }
731
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700732 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800733 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700734
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800735 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700736 if (mInitialTitleResId > 0) {
737 setTitle(mInitialTitleResId);
738 } else {
739 setTitle(mInitialTitle);
740 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700741 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800742 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700743
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800744 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
745 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700746
747 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800748 }
749
750 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
751 final CharSequence title;
752 final int titleRes = bse.getBreadCrumbTitleRes();
753 if (titleRes > 0) {
754 title = getText(titleRes);
755 } else {
756 title = bse.getBreadCrumbTitle();
757 }
758 if (title != null) {
759 setTitle(title);
760 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800761 }
762
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800763 @Override
764 protected void onSaveInstanceState(Bundle outState) {
765 super.onSaveInstanceState(outState);
766
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700767 if (mCategories.size() > 0) {
768 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800769 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700770
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700771 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700772 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700773
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700774 if (mDisplaySearch) {
775 // The option menus are created if the ActionBar is visible and they are also created
776 // asynchronously. If you launch Settings with an Intent action like
777 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
778 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
779 // menu item and search view are null.
780 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
781 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700782
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700783 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
784 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
785 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700786
787 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800788 }
789
790 @Override
791 public void onResume() {
792 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400793 if (mIsShowingDashboard) {
794 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
795 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800796
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700797 final int newHomeActivityCount = getHomeActivitiesCount();
798 if (newHomeActivityCount != mHomeActivitiesCount) {
799 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700800 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700801 }
802
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800803 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
804 @Override
805 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700806 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800807 }
808 };
809 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
810 mDevelopmentPreferencesListener);
811
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800812 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700813
Svetoslav990159a2014-04-14 17:14:59 -0700814 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700815
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700816 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700817 onQueryTextSubmit(mSearchQuery);
818 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800819 }
820
821 @Override
822 public void onPause() {
823 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400824 if (mIsShowingDashboard) {
825 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
826 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800827 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700828 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800829 }
830
831 @Override
832 public void onDestroy() {
833 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700834
835 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
836 mDevelopmentPreferencesListener);
837 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800838 }
839
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800840 protected boolean isValidFragment(String fragmentName) {
841 // Almost all fragments are wrapped in this,
842 // except for a few that have their own activities.
843 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
844 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
845 }
846 return false;
847 }
848
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800849 @Override
850 public Intent getIntent() {
851 Intent superIntent = super.getIntent();
852 String startingFragment = getStartingFragmentClass(superIntent);
853 // This is called from super.onCreate, isMultiPane() is not yet reliable
854 // Do not use onIsHidingHeaders either, which relies itself on this method
855 if (startingFragment != null) {
856 Intent modIntent = new Intent(superIntent);
857 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
858 Bundle args = superIntent.getExtras();
859 if (args != null) {
860 args = new Bundle(args);
861 } else {
862 args = new Bundle();
863 }
864 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100865 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800866 return modIntent;
867 }
868 return superIntent;
869 }
870
871 /**
872 * Checks if the component name in the intent is different from the Settings class and
873 * returns the class name to load as a fragment.
874 */
875 private String getStartingFragmentClass(Intent intent) {
876 if (mFragmentClass != null) return mFragmentClass;
877
878 String intentClass = intent.getComponent().getClassName();
879 if (intentClass.equals(getClass().getName())) return null;
880
881 if ("com.android.settings.ManageApplications".equals(intentClass)
882 || "com.android.settings.RunningServices".equals(intentClass)
883 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
884 // Old names of manage apps.
885 intentClass = com.android.settings.applications.ManageApplications.class.getName();
886 }
887
888 return intentClass;
889 }
890
891 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000892 * Start a new fragment containing a preference panel. If the preferences
893 * are being displayed in multi-pane mode, the given fragment class will
894 * be instantiated and placed in the appropriate pane. If running in
895 * single-pane mode, a new activity will be launched in which to show the
896 * fragment.
897 *
898 * @param fragmentClass Full name of the class implementing the fragment.
899 * @param args Any desired arguments to supply to the fragment.
900 * @param titleRes Optional resource identifier of the title of this
901 * fragment.
902 * @param titleText Optional text of the title of this fragment.
903 * @param resultTo Optional fragment that result data should be sent to.
904 * If non-null, resultTo.onActivityResult() will be called when this
905 * preference panel is done. The launched panel must use
906 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
907 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700908 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000909 */
910 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700911 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700912 String title = null;
913 if (titleRes < 0) {
914 if (titleText != null) {
915 title = titleText.toString();
916 } else {
917 // There not much we can do in that case
918 title = "";
919 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700920 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700921 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700922 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000923 }
924
925 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100926 * Start a new fragment in a new activity containing a preference panel for a given user. If the
927 * preferences are being displayed in multi-pane mode, the given fragment class will be
928 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
929 * activity will be launched in which to show the fragment.
930 *
931 * @param fragmentClass Full name of the class implementing the fragment.
932 * @param args Any desired arguments to supply to the fragment.
933 * @param titleRes Optional resource identifier of the title of this fragment.
934 * @param titleText Optional text of the title of this fragment.
935 * @param userHandle The user for which the panel has to be started.
936 */
937 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
938 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700939 // This is a workaround.
940 //
941 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
942 // starting the fragment could cause a native stack corruption. See b/17523189. However,
943 // adding that flag and start the preference panel with the same UserHandler will make it
944 // impossible to use back button to return to the previous screen. See b/20042570.
945 //
946 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
947 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
948 // when we're calling it as the same user.
949 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
950 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
951 } else {
952 String title = null;
953 if (titleRes < 0) {
954 if (titleText != null) {
955 title = titleText.toString();
956 } else {
957 // There not much we can do in that case
958 title = "";
959 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100960 }
Lifu Tangd0332852015-04-02 12:05:46 -0700961 Utils.startWithFragmentAsUser(this, fragmentClass, args,
962 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100963 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100964 }
965
966 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800967 * Called by a preference panel fragment to finish itself.
968 *
969 * @param caller The fragment that is asking to be finished.
970 * @param resultCode Optional result code to send back to the original
971 * launching fragment.
972 * @param resultData Optional result data to send back to the original
973 * launching fragment.
974 */
975 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
976 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700977 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800978 }
979
980 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000981 * Start a new fragment.
982 *
983 * @param fragment The fragment to start
984 * @param push If true, the current fragment will be pushed onto the back stack. If false,
985 * the current fragment will be replaced.
986 */
987 public void startPreferenceFragment(Fragment fragment, boolean push) {
988 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700989 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000990 if (push) {
991 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
992 transaction.addToBackStack(BACK_STACK_PREFS);
993 } else {
994 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
995 }
996 transaction.commitAllowingStateLoss();
997 }
998
999 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001000 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1001 */
1002 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001003 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001004 if (validate && !isValidFragment(fragmentName)) {
1005 throw new IllegalArgumentException("Invalid fragment for this activity: "
1006 + fragmentName);
1007 }
1008 Fragment f = Fragment.instantiate(this, fragmentName, args);
1009 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001010 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001011 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001012 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001013 }
1014 if (addToBackStack) {
1015 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1016 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001017 if (titleResId > 0) {
1018 transaction.setBreadCrumbTitle(titleResId);
1019 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001020 transaction.setBreadCrumbTitle(title);
1021 }
1022 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001023 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001024 return f;
1025 }
1026
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001027 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001028 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001029 *
1030 * @param categories The list in which to place the tiles categories.
1031 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001032 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001033 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001034 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1035 updateTilesList(categories);
1036 }
1037
1038 /**
1039 * Parse the given XML file as a categories description, adding each
1040 * parsed categories and tiles into the target list.
1041 *
1042 * @param resid The XML resource to load and parse.
1043 * @param target The list in which the parsed categories and tiles should be placed.
1044 */
1045 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1046 XmlResourceParser parser = null;
1047 try {
1048 parser = getResources().getXml(resid);
1049 AttributeSet attrs = Xml.asAttributeSet(parser);
1050
1051 int type;
1052 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1053 && type != XmlPullParser.START_TAG) {
1054 // Parse next until start tag is found
1055 }
1056
1057 String nodeName = parser.getName();
1058 if (!"dashboard-categories".equals(nodeName)) {
1059 throw new RuntimeException(
1060 "XML document must start with <preference-categories> tag; found"
1061 + nodeName + " at " + parser.getPositionDescription());
1062 }
1063
1064 Bundle curBundle = null;
1065
1066 final int outerDepth = parser.getDepth();
1067 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1068 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1069 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1070 continue;
1071 }
1072
1073 nodeName = parser.getName();
1074 if ("dashboard-category".equals(nodeName)) {
1075 DashboardCategory category = new DashboardCategory();
1076
1077 TypedArray sa = obtainStyledAttributes(
1078 attrs, com.android.internal.R.styleable.PreferenceHeader);
1079 category.id = sa.getResourceId(
1080 com.android.internal.R.styleable.PreferenceHeader_id,
1081 (int)DashboardCategory.CAT_ID_UNDEFINED);
1082
1083 TypedValue tv = sa.peekValue(
1084 com.android.internal.R.styleable.PreferenceHeader_title);
1085 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1086 if (tv.resourceId != 0) {
1087 category.titleRes = tv.resourceId;
1088 } else {
1089 category.title = tv.string;
1090 }
1091 }
1092 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001093 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1094 tv = sa.peekValue(
1095 com.android.internal.R.styleable.Preference_key);
1096 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1097 if (tv.resourceId != 0) {
1098 category.key = getString(tv.resourceId);
1099 } else {
1100 category.key = tv.string.toString();
1101 }
1102 }
1103 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001104
1105 final int innerDepth = parser.getDepth();
1106 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1107 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1108 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1109 continue;
1110 }
1111
1112 String innerNodeName = parser.getName();
1113 if (innerNodeName.equals("dashboard-tile")) {
1114 DashboardTile tile = new DashboardTile();
1115
1116 sa = obtainStyledAttributes(
1117 attrs, com.android.internal.R.styleable.PreferenceHeader);
1118 tile.id = sa.getResourceId(
1119 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001120 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001121 tv = sa.peekValue(
1122 com.android.internal.R.styleable.PreferenceHeader_title);
1123 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1124 if (tv.resourceId != 0) {
1125 tile.titleRes = tv.resourceId;
1126 } else {
1127 tile.title = tv.string;
1128 }
1129 }
1130 tv = sa.peekValue(
1131 com.android.internal.R.styleable.PreferenceHeader_summary);
1132 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1133 if (tv.resourceId != 0) {
1134 tile.summaryRes = tv.resourceId;
1135 } else {
1136 tile.summary = tv.string;
1137 }
1138 }
1139 tile.iconRes = sa.getResourceId(
1140 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1141 tile.fragment = sa.getString(
1142 com.android.internal.R.styleable.PreferenceHeader_fragment);
1143 sa.recycle();
1144
1145 if (curBundle == null) {
1146 curBundle = new Bundle();
1147 }
1148
1149 final int innerDepth2 = parser.getDepth();
1150 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1151 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1152 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1153 continue;
1154 }
1155
1156 String innerNodeName2 = parser.getName();
1157 if (innerNodeName2.equals("extra")) {
1158 getResources().parseBundleExtra("extra", attrs, curBundle);
1159 XmlUtils.skipCurrentTag(parser);
1160
1161 } else if (innerNodeName2.equals("intent")) {
1162 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1163
1164 } else {
1165 XmlUtils.skipCurrentTag(parser);
1166 }
1167 }
1168
1169 if (curBundle.size() > 0) {
1170 tile.fragmentArguments = curBundle;
1171 curBundle = null;
1172 }
1173
PauloftheWest38155612014-06-30 10:02:36 -07001174 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001175 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001176 category.addTile(tile);
1177 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001178
Jason Monk2ebc8a02015-02-13 15:23:19 -05001179 } else if (innerNodeName.equals("external-tiles")) {
1180 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001181 } else {
1182 XmlUtils.skipCurrentTag(parser);
1183 }
1184 }
1185
1186 target.add(category);
1187 } else {
1188 XmlUtils.skipCurrentTag(parser);
1189 }
1190 }
1191
1192 } catch (XmlPullParserException e) {
1193 throw new RuntimeException("Error parsing categories", e);
1194 } catch (IOException e) {
1195 throw new RuntimeException("Error parsing categories", e);
1196 } finally {
1197 if (parser != null) parser.close();
1198 }
1199 }
1200
1201 private void updateTilesList(List<DashboardCategory> target) {
1202 final boolean showDev = mDevelopmentPreferences.getBoolean(
1203 DevelopmentSettings.PREF_SHOW,
1204 android.os.Build.TYPE.equals("eng"));
1205
1206 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1207
1208 final int size = target.size();
1209 for (int i = 0; i < size; i++) {
1210
1211 DashboardCategory category = target.get(i);
1212
1213 // Ids are integers, so downcasting is ok
1214 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001215 int n = category.getTilesCount() - 1;
1216 while (n >= 0) {
1217
1218 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001219 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001220 id = (int) tile.id;
1221 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001222 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1223 removeTile = true;
1224 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001225 } else if (id == R.id.wifi_settings) {
1226 // Remove WiFi Settings if WiFi service is not available.
1227 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001228 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001229 }
1230 } else if (id == R.id.bluetooth_settings) {
1231 // Remove Bluetooth Settings if Bluetooth service is not available.
1232 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
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.data_usage_settings) {
1236 // Remove data usage when kernel module not enabled
1237 final INetworkManagementService netManager = INetworkManagementService.Stub
1238 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1239 try {
1240 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001241 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001242 }
1243 } catch (RemoteException e) {
1244 // ignored
1245 }
1246 } else if (id == R.id.battery_settings) {
1247 // Remove battery settings when battery is not available. (e.g. TV)
1248
1249 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001250 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001251 }
1252 } else if (id == R.id.home_settings) {
1253 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001254 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001255 }
1256 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001257 boolean hasMultipleUsers =
1258 ((UserManager) getSystemService(Context.USER_SERVICE))
1259 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001260 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001261 || (!UserManager.supportsMultipleUsers()
1262 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001263 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001264 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 }
1266 } else if (id == R.id.nfc_payment_settings) {
1267 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001268 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001269 } else {
1270 // Only show if NFC is on and we have the HCE feature
1271 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001272 if (adapter == null || !adapter.isEnabled() ||
1273 !getPackageManager().hasSystemFeature(
1274 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001275 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001276 }
1277 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001278 } else if (id == R.id.print_settings) {
1279 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1280 PackageManager.FEATURE_PRINTING);
1281 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001282 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001283 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001284 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001285 if (!showDev || um.hasUserRestriction(
1286 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001287 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001288 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001289 }
1290
1291 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001292 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1293 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001294 }
1295
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001296 if (removeTile && n < category.getTilesCount()) {
1297 category.removeTile(n);
1298 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001299 n--;
1300 }
1301 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001302 addExternalTiles(target);
1303 }
1304
1305 private void addExternalTiles(List<DashboardCategory> target) {
1306 Map<Pair<String, String>, DashboardTile> addedCache =
1307 new ArrayMap<Pair<String, String>, DashboardTile>();
1308 UserManager userManager = UserManager.get(this);
1309 for (UserHandle user : userManager.getUserProfiles()) {
1310 addExternalTiles(target, user, addedCache);
1311 }
1312 }
1313
1314 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1315 Map<Pair<String, String>, DashboardTile> addedCache) {
1316 PackageManager pm = getPackageManager();
1317 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1318 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1319 PackageManager.GET_META_DATA, user.getIdentifier());
1320 for (ResolveInfo resolved : results) {
1321 if (!resolved.system) {
1322 // Do not allow any app to add to settings, only system ones.
1323 continue;
1324 }
1325 ActivityInfo activityInfo = resolved.activityInfo;
1326 Bundle metaData = activityInfo.metaData;
1327 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1328 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1329 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1330 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1331 continue;
1332 }
1333 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1334 DashboardCategory category = getCategory(target, categoryKey);
1335 if (category == null) {
1336 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1337 + "category key " + categoryKey);
1338 continue;
1339 }
1340 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1341 activityInfo.name);
1342 DashboardTile tile = addedCache.get(key);
1343 if (tile == null) {
1344 tile = new DashboardTile();
1345 tile.intent = new Intent().setClassName(
1346 activityInfo.packageName, activityInfo.name);
1347 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1348
1349 if (category.externalIndex == -1) {
1350 // If no location for external tiles has been specified for this category,
1351 // then just put them at the end.
1352 category.addTile(tile);
1353 } else {
1354 category.addTile(category.externalIndex, tile);
1355 }
1356 addedCache.put(key, tile);
1357 }
1358 tile.userHandle.add(user);
1359 }
1360 }
1361
1362 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1363 for (DashboardCategory category : target) {
1364 if (categoryKey.equals(category.key)) {
1365 return category;
1366 }
1367 }
1368 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001369 }
1370
1371 private boolean updateHomeSettingTiles(DashboardTile tile) {
1372 // Once we decide to show Home settings, keep showing it forever
1373 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1374 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1375 return true;
1376 }
1377
1378 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001379 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001380 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001381 // When there's only one available home app, omit this settings
1382 // category entirely at the top level UI. If the user just
1383 // uninstalled the penultimate home app candidiate, we also
1384 // now tell them about why they aren't seeing 'Home' in the list.
1385 if (sShowNoHomeNotice) {
1386 sShowNoHomeNotice = false;
1387 NoHomeDialogFragment.show(this);
1388 }
1389 return false;
1390 } else {
1391 // Okay, we're allowing the Home settings category. Tell it, when
1392 // invoked via this front door, that we'll need to be told about the
1393 // case when the user uninstalls all but one home app.
1394 if (tile.fragmentArguments == null) {
1395 tile.fragmentArguments = new Bundle();
1396 }
1397 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1398 }
1399 } catch (Exception e) {
1400 // Can't look up the home activity; bail on configuring the icon
1401 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1402 }
1403
1404 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1405 return true;
1406 }
1407
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001408 private void getMetaData() {
1409 try {
1410 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1411 PackageManager.GET_META_DATA);
1412 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001413 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1414 } catch (NameNotFoundException nnfe) {
1415 // No recovery
1416 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1417 }
1418 }
1419
1420 // give subclasses access to the Next button
1421 public boolean hasNextButton() {
1422 return mNextButton != null;
1423 }
1424
1425 public Button getNextButton() {
1426 return mNextButton;
1427 }
1428
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001429 @Override
1430 public boolean shouldUpRecreateTask(Intent targetIntent) {
1431 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1432 }
1433
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001434 public static void requestHomeNotice() {
1435 sShowNoHomeNotice = true;
1436 }
1437
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001438 @Override
1439 public boolean onQueryTextSubmit(String query) {
1440 switchToSearchResultsFragmentIfNeeded();
1441 mSearchQuery = query;
1442 return mSearchResultsFragment.onQueryTextSubmit(query);
1443 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001444
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001445 @Override
1446 public boolean onQueryTextChange(String newText) {
1447 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001448 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001449 return false;
1450 }
1451 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001452 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001453
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001454 @Override
1455 public boolean onClose() {
1456 return false;
1457 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001458
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001459 @Override
1460 public boolean onMenuItemActionExpand(MenuItem item) {
1461 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001462 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001463 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001464 return true;
1465 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001466
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001467 @Override
1468 public boolean onMenuItemActionCollapse(MenuItem item) {
1469 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001470 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001472 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001473 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001474 return true;
1475 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001477 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001478 if (mSearchResultsFragment != null) {
1479 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001481 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001482 if (current != null && current instanceof SearchResultsSummary) {
1483 mSearchResultsFragment = (SearchResultsSummary) current;
1484 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001485 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001486 SearchResultsSummary.class.getName(), null, false, true,
1487 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001488 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001489 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001491 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001492
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001493 public void needToRevertToInitialFragment() {
1494 mNeedToRevertToInitialFragment = true;
1495 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001496
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001497 private void revertToInitialFragment() {
1498 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001499 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001500 mSearchMenuItemExpanded = false;
1501 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1502 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001503 if (mSearchMenuItem != null) {
1504 mSearchMenuItem.collapseActionView();
1505 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001506 }
Jim Miller0698a212014-10-16 19:49:07 -07001507
1508 public Intent getResultIntentData() {
1509 return mResultIntentData;
1510 }
1511
1512 public void setResultIntentData(Intent resultIntentData) {
1513 mResultIntentData = resultIntentData;
1514 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001515}