blob: e8be34a43680c81c90dc215b7514f41abc1f3579 [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;
Jeff Sharkey42833b22015-04-11 21:27:33 -070085import com.android.settings.deviceinfo.PublicVolumeSettings;
86import com.android.settings.deviceinfo.StorageSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040088import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.fuelgauge.PowerUsageSummary;
90import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
91import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
92import com.android.settings.inputmethod.SpellCheckersSettings;
93import com.android.settings.inputmethod.UserDictionaryList;
94import com.android.settings.location.LocationSettings;
95import com.android.settings.nfc.AndroidBeam;
96import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040097import com.android.settings.notification.AppNotificationSettings;
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 Spurlockc96a5dc2015-04-10 11:59:54 -0400102import com.android.settings.notification.ZenModeExternalRuleSettings;
John Spurlockc1df2aa2015-04-13 20:57:06 -0400103import com.android.settings.notification.ZenModePrioritySettings;
John Spurlock4a350512014-04-08 14:08:21 -0400104import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400105import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800106import com.android.settings.print.PrintJobSettingsFragment;
107import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500108import com.android.settings.search.DynamicIndexableContentMonitor;
109import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700110import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.tts.TextToSpeechSettings;
112import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700113import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import com.android.settings.vpn2.VpnSettings;
115import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700116import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700118import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import com.android.settings.wifi.WifiSettings;
120import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500121
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import org.xmlpull.v1.XmlPullParser;
123import org.xmlpull.v1.XmlPullParserException;
124
125import java.io.IOException;
126import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500128import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700129import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800130
131public class SettingsActivity extends Activity
132 implements PreferenceManager.OnPreferenceTreeClickListener,
133 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100134 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700135 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100136 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800137
138 private static final String LOG_TAG = "Settings";
139
140 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700141 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700142 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
143 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700144 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700145 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700146 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800147
148 /**
149 * When starting this activity, the invoking Intent can contain this extra
150 * string to specify which fragment should be initially displayed.
151 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
152 * will call isValidFragment() to confirm that the fragment class name is valid for this
153 * activity.
154 */
155 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
156
157 /**
158 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
159 * this extra can also be specified to supply a Bundle of arguments to pass
160 * to that fragment when it is instantiated during the initial creation
161 * of the activity.
162 */
163 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
164
165 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700166 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
167 */
168 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
169
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800170 public static final String BACK_STACK_PREFS = ":settings:prefs";
171
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 // extras that allow any preference activity to be launched as part of a wizard
173
174 // show Back and Next buttons? takes boolean parameter
175 // Back will then return RESULT_CANCELED and Next RESULT_OK
176 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
177
178 // add a Skip button?
179 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
180
181 // specify custom text for the Back or Next buttons, or cause a button to not appear
182 // at all by setting it to null
183 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
184 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
185
186 /**
187 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700188 * 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 -0800189 * that fragment.
190 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700191 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100192 /**
193 * The package name used to resolve the title resource id.
194 */
195 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
196 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700197 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
198 ":settings:show_fragment_title_resid";
199 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
200 ":settings:show_fragment_as_shortcut";
201
202 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
203 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205 private static final String META_DATA_KEY_FRAGMENT_CLASS =
206 "com.android.settings.FRAGMENT_CLASS";
207
208 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
209
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700210 private static final String EMPTY_QUERY = "";
211
Jason Monk2ebc8a02015-02-13 15:23:19 -0500212 /**
213 * Settings will search for system activities of this action and add them as a top level
214 * settings tile using the following parameters.
215 *
216 * <p>A category must be specified in the meta-data for the activity named
217 * {@link #EXTRA_CATEGORY_KEY}
218 *
219 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
220 * otherwise the label for the activity will be used.
221 *
222 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
223 * otherwise the icon for the activity will be used.
224 *
225 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
226 */
227 private static final String EXTRA_SETTINGS_ACTION =
228 "com.android.settings.action.EXTRA_SETTINGS";
229
230 /**
231 * The key used to get the category from metadata of activities of action
232 * {@link #EXTRA_SETTINGS_ACTION}
233 * The value must be one of:
234 * <li>com.android.settings.category.wireless</li>
235 * <li>com.android.settings.category.device</li>
236 * <li>com.android.settings.category.personal</li>
237 * <li>com.android.settings.category.system</li>
238 */
239 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
240
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800241 private static boolean sShowNoHomeNotice = false;
242
243 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800245 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700246 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800247
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248 // Show only these settings for restricted users
249 private int[] SETTINGS_FOR_RESTRICTED = {
250 R.id.wireless_section,
251 R.id.wifi_settings,
252 R.id.bluetooth_settings,
253 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700254 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800255 R.id.wireless_settings,
256 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400257 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 R.id.display_settings,
259 R.id.storage_settings,
260 R.id.application_settings,
261 R.id.battery_settings,
262 R.id.personal_section,
263 R.id.location_settings,
264 R.id.security_settings,
265 R.id.language_settings,
266 R.id.user_settings,
267 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800268 R.id.system_section,
269 R.id.date_time_settings,
270 R.id.about_settings,
271 R.id.accessibility_settings,
272 R.id.print_settings,
273 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800274 R.id.home_settings,
275 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800276 };
277
278 private static final String[] ENTRY_FRAGMENTS = {
279 WirelessSettings.class.getName(),
280 WifiSettings.class.getName(),
281 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700282 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800283 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700284 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 TetherSettings.class.getName(),
286 WifiP2pSettings.class.getName(),
287 VpnSettings.class.getName(),
288 DateTimeSettings.class.getName(),
289 LocalePicker.class.getName(),
290 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700291 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800292 SpellCheckersSettings.class.getName(),
293 UserDictionaryList.class.getName(),
294 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700295 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 DisplaySettings.class.getName(),
297 DeviceInfoSettings.class.getName(),
298 ManageApplications.class.getName(),
299 ProcessStatsUi.class.getName(),
300 NotificationStation.class.getName(),
301 LocationSettings.class.getName(),
302 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700303 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 PrivacySettings.class.getName(),
305 DeviceAdminSettings.class.getName(),
306 AccessibilitySettings.class.getName(),
307 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
309 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700310 StorageSettings.class.getName(),
311 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 DevelopmentSettings.class.getName(),
313 UsbSettings.class.getName(),
314 AndroidBeam.class.getName(),
315 WifiDisplaySettings.class.getName(),
316 PowerUsageSummary.class.getName(),
317 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000318 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800319 CryptKeeperSettings.class.getName(),
320 DataUsageSummary.class.getName(),
321 DreamSettings.class.getName(),
322 UserSettings.class.getName(),
323 NotificationAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 PrintSettingsFragment.class.getName(),
325 PrintJobSettingsFragment.class.getName(),
326 TrustedCredentialsSettings.class.getName(),
327 PaymentSettings.class.getName(),
328 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700329 ZenModeSettings.class.getName(),
330 NotificationSettings.class.getName(),
331 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
332 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400333 InstalledAppDetails.class.getName(),
334 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700335 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700336 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000337 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400338 WifiCallingSettings.class.getName(),
John Spurlockc1df2aa2015-04-13 20:57:06 -0400339 ZenModePrioritySettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400340 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400341 ZenModeExternalRuleSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 };
343
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700344
345 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
346 "android.settings.APPLICATION_DETAILS_SETTINGS"
347 };
348
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 private SharedPreferences mDevelopmentPreferences;
350 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
351
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 private boolean mBatteryPresent = true;
353 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 @Override
355 public void onReceive(Context context, Intent intent) {
356 String action = intent.getAction();
357 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
358 boolean batteryPresent = Utils.isBatteryPresent(intent);
359
360 if (mBatteryPresent != batteryPresent) {
361 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700362 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 }
364 }
365 }
366 };
367
Svetoslav990159a2014-04-14 17:14:59 -0700368 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
369 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700370
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700371 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700372 private SwitchBar mSwitchBar;
373
374 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700375
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700376 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700377 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700378
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700379 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700380 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700381
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700382 private ViewGroup mContent;
383
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700384 private SearchView mSearchView;
385 private MenuItem mSearchMenuItem;
386 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700387 private SearchResultsSummary mSearchResultsFragment;
388 private String mSearchQuery;
389
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700390 // Categories
391 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800392
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700393 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700394 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800395 private Handler mHandler = new Handler() {
396 @Override
397 public void handleMessage(Message msg) {
398 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700399 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700400 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
401 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700402 buildDashboardCategories(mCategories);
403 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404 } break;
405 }
406 }
407 };
408
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700409 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700410 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700411
Jim Miller0698a212014-10-16 19:49:07 -0700412 private Intent mResultIntentData;
413
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700414 public SwitchBar getSwitchBar() {
415 return mSwitchBar;
416 }
417
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700418 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
419 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700420 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700421 }
422 return mCategories;
423 }
424
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800425 @Override
426 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
427 // Override the fragment title for Wallpaper settings
428 int titleRes = pref.getTitleRes();
429 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
430 titleRes = R.string.wallpaper_settings_fragment_title;
431 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
432 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
433 if (UserManager.get(this).isLinkedUser()) {
434 titleRes = R.string.profile_info_settings_title;
435 } else {
436 titleRes = R.string.user_info_settings_title;
437 }
438 }
439 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
440 null, 0);
441 return true;
442 }
443
444 @Override
445 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
446 return false;
447 }
448
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700449 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700450 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700451 Message msg = new Message();
452 msg.what = MSG_BUILD_CATEGORIES;
453 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800454 }
455 }
456
457 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800458 public void onConfigurationChanged(Configuration newConfig) {
459 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800460 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800461 }
462
463 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700464 protected void onStart() {
465 super.onStart();
466
467 if (mNeedToRevertToInitialFragment) {
468 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800469 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800470 }
471
472 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700473 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700474 if (!mDisplaySearch) {
475 return false;
476 }
477
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700478 MenuInflater inflater = getMenuInflater();
479 inflater.inflate(R.menu.options_menu, menu);
480
481 // Cache the search query (can be overriden by the OnQueryTextListener)
482 final String query = mSearchQuery;
483
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700484 mSearchMenuItem = menu.findItem(R.id.search);
485 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700486
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700487 if (mSearchMenuItem == null || mSearchView == null) {
488 return false;
489 }
490
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700491 if (mSearchResultsFragment != null) {
492 mSearchResultsFragment.setSearchView(mSearchView);
493 }
494
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700495 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700496 mSearchView.setOnQueryTextListener(this);
497 mSearchView.setOnCloseListener(this);
498
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700499 if (mSearchMenuItemExpanded) {
500 mSearchMenuItem.expandActionView();
501 }
502 mSearchView.setQuery(query, true /* submit */);
503
504 return true;
505 }
506
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700507 private static boolean isShortCutIntent(final Intent intent) {
508 Set<String> categories = intent.getCategories();
509 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
510 }
511
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700512 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700513 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700514 if (action == null) {
515 return false;
516 }
517 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
518 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
519 }
520 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700521 }
522
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700523 @Override
524 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700525 super.onCreate(savedState);
526
527 // Should happen before any call to getIntent()
528 getMetaData();
529
530 final Intent intent = getIntent();
531 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
532 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
536 Context.MODE_PRIVATE);
537
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700538 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700539 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700540
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700541 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700542 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
543
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700544 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700545 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700546
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700547 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700548
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700549 // This is a "Sub Settings" when:
550 // - this is a real SubSettings
551 // - or :settings:show_fragment_as_subsetting is passed to the Intent
552 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
553 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
554
555 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
556 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700557 // Check also that we are not a Theme Dialog as we don't want to override them
558 final int themeResId = getThemeResId();
559 if (themeResId != R.style.Theme_DialogWhenLarge &&
560 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
561 setTheme(R.style.Theme_SubSettings);
562 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700563 }
564
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700565 setContentView(mIsShowingDashboard ?
566 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700568 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700569
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 getFragmentManager().addOnBackStackChangedListener(this);
571
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700572 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800573 // Run the Index update only if we have some space
574 if (!Utils.isLowStorage(this)) {
575 Index.getInstance(getApplicationContext()).update();
576 } else {
577 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
578 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700579 }
580
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700581 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700582 // We are restarting from a previous saved state; used that to initialize, instead
583 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700584 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
585 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800586
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700587 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800588
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700589 ArrayList<DashboardCategory> categories =
590 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
591 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700592 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700593 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700594 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700596
597 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700598 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700599 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
600 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700602 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700603 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
604 // only if it is a sub settings
605 if (mIsShortcut) {
606 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700607 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700608 } else if (isSubSettings) {
609 mDisplayHomeAsUpEnabled = true;
610 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700611 } else {
612 mDisplayHomeAsUpEnabled = false;
613 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700614 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700615 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700616
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700617 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700618 switchToFragment(initialFragmentName, initialArguments, true, false,
619 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000620 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700621 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700622 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700623 // Show Search affordance
624 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700625 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700626 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700627 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800628 }
629 }
630
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700631 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700632 if (mActionBar != null) {
633 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
634 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
635 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700636 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
637
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800638 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800639 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
640
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700641 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 if (buttonBar != null) {
643 buttonBar.setVisibility(View.VISIBLE);
644
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700645 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 backButton.setOnClickListener(new OnClickListener() {
647 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700648 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 finish();
650 }
651 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700652 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 skipButton.setOnClickListener(new OnClickListener() {
654 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700655 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 finish();
657 }
658 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700659 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 mNextButton.setOnClickListener(new OnClickListener() {
661 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700662 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800663 finish();
664 }
665 });
666
667 // set our various button parameters
668 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
669 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
670 if (TextUtils.isEmpty(buttonText)) {
671 mNextButton.setVisibility(View.GONE);
672 }
673 else {
674 mNextButton.setText(buttonText);
675 }
676 }
677 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
678 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
679 if (TextUtils.isEmpty(buttonText)) {
680 backButton.setVisibility(View.GONE);
681 }
682 else {
683 backButton.setText(buttonText);
684 }
685 }
686 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
687 skipButton.setVisibility(View.VISIBLE);
688 }
689 }
690 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700691
692 mHomeActivitiesCount = getHomeActivitiesCount();
693 }
694
695 private int getHomeActivitiesCount() {
696 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
697 getPackageManager().getHomeActivities(homeApps);
698 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800699 }
700
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700701 private void setTitleFromIntent(Intent intent) {
702 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
703 if (initialTitleResId > 0) {
704 mInitialTitle = null;
705 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100706
707 final String initialTitleResPackageName = intent.getStringExtra(
708 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
709 if (initialTitleResPackageName != null) {
710 try {
711 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
712 0 /* flags */, new UserHandle(UserHandle.myUserId()));
713 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
714 setTitle(mInitialTitle);
715 mInitialTitleResId = -1;
716 return;
717 } catch (NameNotFoundException e) {
718 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
719 }
720 } else {
721 setTitle(mInitialTitleResId);
722 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700723 } else {
724 mInitialTitleResId = -1;
725 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
726 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
727 setTitle(mInitialTitle);
728 }
729 }
730
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800731 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800732 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700733 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800734 }
735
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700736 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800737 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700738
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800739 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700740 if (mInitialTitleResId > 0) {
741 setTitle(mInitialTitleResId);
742 } else {
743 setTitle(mInitialTitle);
744 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700745 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800746 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700747
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800748 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
749 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700750
751 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800752 }
753
754 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
755 final CharSequence title;
756 final int titleRes = bse.getBreadCrumbTitleRes();
757 if (titleRes > 0) {
758 title = getText(titleRes);
759 } else {
760 title = bse.getBreadCrumbTitle();
761 }
762 if (title != null) {
763 setTitle(title);
764 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800765 }
766
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800767 @Override
768 protected void onSaveInstanceState(Bundle outState) {
769 super.onSaveInstanceState(outState);
770
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700771 if (mCategories.size() > 0) {
772 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800773 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700774
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700775 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700776 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700777
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700778 if (mDisplaySearch) {
779 // The option menus are created if the ActionBar is visible and they are also created
780 // asynchronously. If you launch Settings with an Intent action like
781 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
782 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
783 // menu item and search view are null.
784 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
785 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700786
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700787 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
788 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
789 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700790
791 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800792 }
793
794 @Override
795 public void onResume() {
796 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400797 if (mIsShowingDashboard) {
798 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
799 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800800
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700801 final int newHomeActivityCount = getHomeActivitiesCount();
802 if (newHomeActivityCount != mHomeActivitiesCount) {
803 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700804 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700805 }
806
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800807 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
808 @Override
809 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700810 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800811 }
812 };
813 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
814 mDevelopmentPreferencesListener);
815
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800816 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700817
Svetoslav990159a2014-04-14 17:14:59 -0700818 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700819
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700820 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700821 onQueryTextSubmit(mSearchQuery);
822 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800823 }
824
825 @Override
826 public void onPause() {
827 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400828 if (mIsShowingDashboard) {
829 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
830 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800831 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700832 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800833 }
834
835 @Override
836 public void onDestroy() {
837 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700838
839 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
840 mDevelopmentPreferencesListener);
841 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800842 }
843
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800844 protected boolean isValidFragment(String fragmentName) {
845 // Almost all fragments are wrapped in this,
846 // except for a few that have their own activities.
847 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
848 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
849 }
850 return false;
851 }
852
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800853 @Override
854 public Intent getIntent() {
855 Intent superIntent = super.getIntent();
856 String startingFragment = getStartingFragmentClass(superIntent);
857 // This is called from super.onCreate, isMultiPane() is not yet reliable
858 // Do not use onIsHidingHeaders either, which relies itself on this method
859 if (startingFragment != null) {
860 Intent modIntent = new Intent(superIntent);
861 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
862 Bundle args = superIntent.getExtras();
863 if (args != null) {
864 args = new Bundle(args);
865 } else {
866 args = new Bundle();
867 }
868 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100869 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800870 return modIntent;
871 }
872 return superIntent;
873 }
874
875 /**
876 * Checks if the component name in the intent is different from the Settings class and
877 * returns the class name to load as a fragment.
878 */
879 private String getStartingFragmentClass(Intent intent) {
880 if (mFragmentClass != null) return mFragmentClass;
881
882 String intentClass = intent.getComponent().getClassName();
883 if (intentClass.equals(getClass().getName())) return null;
884
885 if ("com.android.settings.ManageApplications".equals(intentClass)
886 || "com.android.settings.RunningServices".equals(intentClass)
887 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
888 // Old names of manage apps.
889 intentClass = com.android.settings.applications.ManageApplications.class.getName();
890 }
891
892 return intentClass;
893 }
894
895 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000896 * Start a new fragment containing a preference panel. If the preferences
897 * are being displayed in multi-pane mode, the given fragment class will
898 * be instantiated and placed in the appropriate pane. If running in
899 * single-pane mode, a new activity will be launched in which to show the
900 * fragment.
901 *
902 * @param fragmentClass Full name of the class implementing the fragment.
903 * @param args Any desired arguments to supply to the fragment.
904 * @param titleRes Optional resource identifier of the title of this
905 * fragment.
906 * @param titleText Optional text of the title of this fragment.
907 * @param resultTo Optional fragment that result data should be sent to.
908 * If non-null, resultTo.onActivityResult() will be called when this
909 * preference panel is done. The launched panel must use
910 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
911 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700912 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000913 */
914 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700915 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700916 String title = null;
917 if (titleRes < 0) {
918 if (titleText != null) {
919 title = titleText.toString();
920 } else {
921 // There not much we can do in that case
922 title = "";
923 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700924 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700925 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700926 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000927 }
928
929 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100930 * Start a new fragment in a new activity containing a preference panel for a given user. If the
931 * preferences are being displayed in multi-pane mode, the given fragment class will be
932 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
933 * activity will be launched in which to show the fragment.
934 *
935 * @param fragmentClass Full name of the class implementing the fragment.
936 * @param args Any desired arguments to supply to the fragment.
937 * @param titleRes Optional resource identifier of the title of this fragment.
938 * @param titleText Optional text of the title of this fragment.
939 * @param userHandle The user for which the panel has to be started.
940 */
941 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
942 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700943 // This is a workaround.
944 //
945 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
946 // starting the fragment could cause a native stack corruption. See b/17523189. However,
947 // adding that flag and start the preference panel with the same UserHandler will make it
948 // impossible to use back button to return to the previous screen. See b/20042570.
949 //
950 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
951 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
952 // when we're calling it as the same user.
953 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
954 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
955 } else {
956 String title = null;
957 if (titleRes < 0) {
958 if (titleText != null) {
959 title = titleText.toString();
960 } else {
961 // There not much we can do in that case
962 title = "";
963 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100964 }
Lifu Tangd0332852015-04-02 12:05:46 -0700965 Utils.startWithFragmentAsUser(this, fragmentClass, args,
966 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100967 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100968 }
969
970 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800971 * Called by a preference panel fragment to finish itself.
972 *
973 * @param caller The fragment that is asking to be finished.
974 * @param resultCode Optional result code to send back to the original
975 * launching fragment.
976 * @param resultData Optional result data to send back to the original
977 * launching fragment.
978 */
979 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
980 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700981 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800982 }
983
984 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000985 * Start a new fragment.
986 *
987 * @param fragment The fragment to start
988 * @param push If true, the current fragment will be pushed onto the back stack. If false,
989 * the current fragment will be replaced.
990 */
991 public void startPreferenceFragment(Fragment fragment, boolean push) {
992 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700993 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000994 if (push) {
995 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
996 transaction.addToBackStack(BACK_STACK_PREFS);
997 } else {
998 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
999 }
1000 transaction.commitAllowingStateLoss();
1001 }
1002
1003 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001004 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1005 */
1006 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001007 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001008 if (validate && !isValidFragment(fragmentName)) {
1009 throw new IllegalArgumentException("Invalid fragment for this activity: "
1010 + fragmentName);
1011 }
1012 Fragment f = Fragment.instantiate(this, fragmentName, args);
1013 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001014 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001015 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001016 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001017 }
1018 if (addToBackStack) {
1019 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1020 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001021 if (titleResId > 0) {
1022 transaction.setBreadCrumbTitle(titleResId);
1023 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001024 transaction.setBreadCrumbTitle(title);
1025 }
1026 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001027 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001028 return f;
1029 }
1030
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001031 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001032 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001033 *
1034 * @param categories The list in which to place the tiles categories.
1035 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001036 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001037 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001038 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1039 updateTilesList(categories);
1040 }
1041
1042 /**
1043 * Parse the given XML file as a categories description, adding each
1044 * parsed categories and tiles into the target list.
1045 *
1046 * @param resid The XML resource to load and parse.
1047 * @param target The list in which the parsed categories and tiles should be placed.
1048 */
1049 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1050 XmlResourceParser parser = null;
1051 try {
1052 parser = getResources().getXml(resid);
1053 AttributeSet attrs = Xml.asAttributeSet(parser);
1054
1055 int type;
1056 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1057 && type != XmlPullParser.START_TAG) {
1058 // Parse next until start tag is found
1059 }
1060
1061 String nodeName = parser.getName();
1062 if (!"dashboard-categories".equals(nodeName)) {
1063 throw new RuntimeException(
1064 "XML document must start with <preference-categories> tag; found"
1065 + nodeName + " at " + parser.getPositionDescription());
1066 }
1067
1068 Bundle curBundle = null;
1069
1070 final int outerDepth = parser.getDepth();
1071 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1072 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1073 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1074 continue;
1075 }
1076
1077 nodeName = parser.getName();
1078 if ("dashboard-category".equals(nodeName)) {
1079 DashboardCategory category = new DashboardCategory();
1080
1081 TypedArray sa = obtainStyledAttributes(
1082 attrs, com.android.internal.R.styleable.PreferenceHeader);
1083 category.id = sa.getResourceId(
1084 com.android.internal.R.styleable.PreferenceHeader_id,
1085 (int)DashboardCategory.CAT_ID_UNDEFINED);
1086
1087 TypedValue tv = sa.peekValue(
1088 com.android.internal.R.styleable.PreferenceHeader_title);
1089 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1090 if (tv.resourceId != 0) {
1091 category.titleRes = tv.resourceId;
1092 } else {
1093 category.title = tv.string;
1094 }
1095 }
1096 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001097 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1098 tv = sa.peekValue(
1099 com.android.internal.R.styleable.Preference_key);
1100 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1101 if (tv.resourceId != 0) {
1102 category.key = getString(tv.resourceId);
1103 } else {
1104 category.key = tv.string.toString();
1105 }
1106 }
1107 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001108
1109 final int innerDepth = parser.getDepth();
1110 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1111 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1112 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1113 continue;
1114 }
1115
1116 String innerNodeName = parser.getName();
1117 if (innerNodeName.equals("dashboard-tile")) {
1118 DashboardTile tile = new DashboardTile();
1119
1120 sa = obtainStyledAttributes(
1121 attrs, com.android.internal.R.styleable.PreferenceHeader);
1122 tile.id = sa.getResourceId(
1123 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001124 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001125 tv = sa.peekValue(
1126 com.android.internal.R.styleable.PreferenceHeader_title);
1127 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1128 if (tv.resourceId != 0) {
1129 tile.titleRes = tv.resourceId;
1130 } else {
1131 tile.title = tv.string;
1132 }
1133 }
1134 tv = sa.peekValue(
1135 com.android.internal.R.styleable.PreferenceHeader_summary);
1136 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1137 if (tv.resourceId != 0) {
1138 tile.summaryRes = tv.resourceId;
1139 } else {
1140 tile.summary = tv.string;
1141 }
1142 }
1143 tile.iconRes = sa.getResourceId(
1144 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1145 tile.fragment = sa.getString(
1146 com.android.internal.R.styleable.PreferenceHeader_fragment);
1147 sa.recycle();
1148
1149 if (curBundle == null) {
1150 curBundle = new Bundle();
1151 }
1152
1153 final int innerDepth2 = parser.getDepth();
1154 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1155 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1156 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1157 continue;
1158 }
1159
1160 String innerNodeName2 = parser.getName();
1161 if (innerNodeName2.equals("extra")) {
1162 getResources().parseBundleExtra("extra", attrs, curBundle);
1163 XmlUtils.skipCurrentTag(parser);
1164
1165 } else if (innerNodeName2.equals("intent")) {
1166 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1167
1168 } else {
1169 XmlUtils.skipCurrentTag(parser);
1170 }
1171 }
1172
1173 if (curBundle.size() > 0) {
1174 tile.fragmentArguments = curBundle;
1175 curBundle = null;
1176 }
1177
PauloftheWest38155612014-06-30 10:02:36 -07001178 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001179 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001180 category.addTile(tile);
1181 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001182
Jason Monk2ebc8a02015-02-13 15:23:19 -05001183 } else if (innerNodeName.equals("external-tiles")) {
1184 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001185 } else {
1186 XmlUtils.skipCurrentTag(parser);
1187 }
1188 }
1189
1190 target.add(category);
1191 } else {
1192 XmlUtils.skipCurrentTag(parser);
1193 }
1194 }
1195
1196 } catch (XmlPullParserException e) {
1197 throw new RuntimeException("Error parsing categories", e);
1198 } catch (IOException e) {
1199 throw new RuntimeException("Error parsing categories", e);
1200 } finally {
1201 if (parser != null) parser.close();
1202 }
1203 }
1204
1205 private void updateTilesList(List<DashboardCategory> target) {
1206 final boolean showDev = mDevelopmentPreferences.getBoolean(
1207 DevelopmentSettings.PREF_SHOW,
1208 android.os.Build.TYPE.equals("eng"));
1209
1210 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1211
1212 final int size = target.size();
1213 for (int i = 0; i < size; i++) {
1214
1215 DashboardCategory category = target.get(i);
1216
1217 // Ids are integers, so downcasting is ok
1218 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001219 int n = category.getTilesCount() - 1;
1220 while (n >= 0) {
1221
1222 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001223 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001224 id = (int) tile.id;
1225 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001226 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1227 removeTile = true;
1228 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001229 } else if (id == R.id.wifi_settings) {
1230 // Remove WiFi Settings if WiFi service is not available.
1231 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001232 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001233 }
1234 } else if (id == R.id.bluetooth_settings) {
1235 // Remove Bluetooth Settings if Bluetooth service is not available.
1236 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001237 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001238 }
1239 } else if (id == R.id.data_usage_settings) {
1240 // Remove data usage when kernel module not enabled
1241 final INetworkManagementService netManager = INetworkManagementService.Stub
1242 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1243 try {
1244 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001245 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001246 }
1247 } catch (RemoteException e) {
1248 // ignored
1249 }
1250 } else if (id == R.id.battery_settings) {
1251 // Remove battery settings when battery is not available. (e.g. TV)
1252
1253 if (!mBatteryPresent) {
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.home_settings) {
1257 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001258 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001259 }
1260 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001261 boolean hasMultipleUsers =
1262 ((UserManager) getSystemService(Context.USER_SERVICE))
1263 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001264 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001265 || (!UserManager.supportsMultipleUsers()
1266 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001268 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001269 }
1270 } else if (id == R.id.nfc_payment_settings) {
1271 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001272 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001273 } else {
1274 // Only show if NFC is on and we have the HCE feature
1275 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001276 if (adapter == null || !adapter.isEnabled() ||
1277 !getPackageManager().hasSystemFeature(
1278 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001279 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001280 }
1281 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001282 } else if (id == R.id.print_settings) {
1283 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1284 PackageManager.FEATURE_PRINTING);
1285 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001286 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001287 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001288 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001289 if (!showDev || um.hasUserRestriction(
1290 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001291 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001292 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001293 }
1294
1295 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001296 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1297 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001298 }
1299
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001300 if (removeTile && n < category.getTilesCount()) {
1301 category.removeTile(n);
1302 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001303 n--;
1304 }
1305 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001306 addExternalTiles(target);
1307 }
1308
1309 private void addExternalTiles(List<DashboardCategory> target) {
1310 Map<Pair<String, String>, DashboardTile> addedCache =
1311 new ArrayMap<Pair<String, String>, DashboardTile>();
1312 UserManager userManager = UserManager.get(this);
1313 for (UserHandle user : userManager.getUserProfiles()) {
1314 addExternalTiles(target, user, addedCache);
1315 }
1316 }
1317
1318 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1319 Map<Pair<String, String>, DashboardTile> addedCache) {
1320 PackageManager pm = getPackageManager();
1321 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1322 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1323 PackageManager.GET_META_DATA, user.getIdentifier());
1324 for (ResolveInfo resolved : results) {
1325 if (!resolved.system) {
1326 // Do not allow any app to add to settings, only system ones.
1327 continue;
1328 }
1329 ActivityInfo activityInfo = resolved.activityInfo;
1330 Bundle metaData = activityInfo.metaData;
1331 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1332 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1333 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1334 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1335 continue;
1336 }
1337 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1338 DashboardCategory category = getCategory(target, categoryKey);
1339 if (category == null) {
1340 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1341 + "category key " + categoryKey);
1342 continue;
1343 }
1344 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1345 activityInfo.name);
1346 DashboardTile tile = addedCache.get(key);
1347 if (tile == null) {
1348 tile = new DashboardTile();
1349 tile.intent = new Intent().setClassName(
1350 activityInfo.packageName, activityInfo.name);
1351 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1352
1353 if (category.externalIndex == -1) {
1354 // If no location for external tiles has been specified for this category,
1355 // then just put them at the end.
1356 category.addTile(tile);
1357 } else {
1358 category.addTile(category.externalIndex, tile);
1359 }
1360 addedCache.put(key, tile);
1361 }
1362 tile.userHandle.add(user);
1363 }
1364 }
1365
1366 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1367 for (DashboardCategory category : target) {
1368 if (categoryKey.equals(category.key)) {
1369 return category;
1370 }
1371 }
1372 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001373 }
1374
1375 private boolean updateHomeSettingTiles(DashboardTile tile) {
1376 // Once we decide to show Home settings, keep showing it forever
1377 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1378 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1379 return true;
1380 }
1381
1382 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001383 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001384 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001385 // When there's only one available home app, omit this settings
1386 // category entirely at the top level UI. If the user just
1387 // uninstalled the penultimate home app candidiate, we also
1388 // now tell them about why they aren't seeing 'Home' in the list.
1389 if (sShowNoHomeNotice) {
1390 sShowNoHomeNotice = false;
1391 NoHomeDialogFragment.show(this);
1392 }
1393 return false;
1394 } else {
1395 // Okay, we're allowing the Home settings category. Tell it, when
1396 // invoked via this front door, that we'll need to be told about the
1397 // case when the user uninstalls all but one home app.
1398 if (tile.fragmentArguments == null) {
1399 tile.fragmentArguments = new Bundle();
1400 }
1401 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1402 }
1403 } catch (Exception e) {
1404 // Can't look up the home activity; bail on configuring the icon
1405 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1406 }
1407
1408 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1409 return true;
1410 }
1411
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001412 private void getMetaData() {
1413 try {
1414 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1415 PackageManager.GET_META_DATA);
1416 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001417 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1418 } catch (NameNotFoundException nnfe) {
1419 // No recovery
1420 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1421 }
1422 }
1423
1424 // give subclasses access to the Next button
1425 public boolean hasNextButton() {
1426 return mNextButton != null;
1427 }
1428
1429 public Button getNextButton() {
1430 return mNextButton;
1431 }
1432
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001433 @Override
1434 public boolean shouldUpRecreateTask(Intent targetIntent) {
1435 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1436 }
1437
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001438 public static void requestHomeNotice() {
1439 sShowNoHomeNotice = true;
1440 }
1441
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001442 @Override
1443 public boolean onQueryTextSubmit(String query) {
1444 switchToSearchResultsFragmentIfNeeded();
1445 mSearchQuery = query;
1446 return mSearchResultsFragment.onQueryTextSubmit(query);
1447 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001448
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001449 @Override
1450 public boolean onQueryTextChange(String newText) {
1451 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001452 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001453 return false;
1454 }
1455 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001456 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001457
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001458 @Override
1459 public boolean onClose() {
1460 return false;
1461 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001462
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001463 @Override
1464 public boolean onMenuItemActionExpand(MenuItem item) {
1465 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001466 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001467 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001468 return true;
1469 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001470
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 @Override
1472 public boolean onMenuItemActionCollapse(MenuItem item) {
1473 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001474 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001477 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001478 return true;
1479 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001481 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001482 if (mSearchResultsFragment != null) {
1483 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001484 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001485 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001486 if (current != null && current instanceof SearchResultsSummary) {
1487 mSearchResultsFragment = (SearchResultsSummary) current;
1488 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001489 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001490 SearchResultsSummary.class.getName(), null, false, true,
1491 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001493 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001494 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001495 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001496
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001497 public void needToRevertToInitialFragment() {
1498 mNeedToRevertToInitialFragment = true;
1499 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001500
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001501 private void revertToInitialFragment() {
1502 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001503 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001504 mSearchMenuItemExpanded = false;
1505 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1506 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001507 if (mSearchMenuItem != null) {
1508 mSearchMenuItem.collapseActionView();
1509 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001510 }
Jim Miller0698a212014-10-16 19:49:07 -07001511
1512 public Intent getResultIntentData() {
1513 return mResultIntentData;
1514 }
1515
1516 public void setResultIntentData(Intent resultIntentData) {
1517 mResultIntentData = resultIntentData;
1518 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001519}