blob: 0fdf04b5c74d0acf52ed54b21c420c9779862217 [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 Spurlock4a350512014-04-08 14:08:21 -0400103import com.android.settings.notification.ZenModeSettings;
John Spurlock45fa1402015-04-09 12:50:04 -0400104import com.android.settings.notification.ZenModeScheduleRuleSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800105import com.android.settings.print.PrintJobSettingsFragment;
106import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500107import com.android.settings.search.DynamicIndexableContentMonitor;
108import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700109import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.tts.TextToSpeechSettings;
111import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700112import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.vpn2.VpnSettings;
114import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700115import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700117import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import com.android.settings.wifi.WifiSettings;
119import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500120
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import org.xmlpull.v1.XmlPullParser;
122import org.xmlpull.v1.XmlPullParserException;
123
124import java.io.IOException;
125import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500127import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700128import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800129
130public class SettingsActivity extends Activity
131 implements PreferenceManager.OnPreferenceTreeClickListener,
132 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100133 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700134 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100135 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800136
137 private static final String LOG_TAG = "Settings";
138
139 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700140 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700141 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
142 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700143 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700144 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700145 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800146
147 /**
148 * When starting this activity, the invoking Intent can contain this extra
149 * string to specify which fragment should be initially displayed.
150 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
151 * will call isValidFragment() to confirm that the fragment class name is valid for this
152 * activity.
153 */
154 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
155
156 /**
157 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
158 * this extra can also be specified to supply a Bundle of arguments to pass
159 * to that fragment when it is instantiated during the initial creation
160 * of the activity.
161 */
162 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
163
164 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700165 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
166 */
167 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
168
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800169 public static final String BACK_STACK_PREFS = ":settings:prefs";
170
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800171 // extras that allow any preference activity to be launched as part of a wizard
172
173 // show Back and Next buttons? takes boolean parameter
174 // Back will then return RESULT_CANCELED and Next RESULT_OK
175 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
176
177 // add a Skip button?
178 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
179
180 // specify custom text for the Back or Next buttons, or cause a button to not appear
181 // at all by setting it to null
182 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
183 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
184
185 /**
186 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700187 * 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 -0800188 * that fragment.
189 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700190 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100191 /**
192 * The package name used to resolve the title resource id.
193 */
194 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
195 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700196 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
197 ":settings:show_fragment_title_resid";
198 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
199 ":settings:show_fragment_as_shortcut";
200
201 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
202 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204 private static final String META_DATA_KEY_FRAGMENT_CLASS =
205 "com.android.settings.FRAGMENT_CLASS";
206
207 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
208
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700209 private static final String EMPTY_QUERY = "";
210
Jason Monk2ebc8a02015-02-13 15:23:19 -0500211 /**
212 * Settings will search for system activities of this action and add them as a top level
213 * settings tile using the following parameters.
214 *
215 * <p>A category must be specified in the meta-data for the activity named
216 * {@link #EXTRA_CATEGORY_KEY}
217 *
218 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
219 * otherwise the label for the activity will be used.
220 *
221 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
222 * otherwise the icon for the activity will be used.
223 *
224 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
225 */
226 private static final String EXTRA_SETTINGS_ACTION =
227 "com.android.settings.action.EXTRA_SETTINGS";
228
229 /**
230 * The key used to get the category from metadata of activities of action
231 * {@link #EXTRA_SETTINGS_ACTION}
232 * The value must be one of:
233 * <li>com.android.settings.category.wireless</li>
234 * <li>com.android.settings.category.device</li>
235 * <li>com.android.settings.category.personal</li>
236 * <li>com.android.settings.category.system</li>
237 */
238 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
239
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 private static boolean sShowNoHomeNotice = false;
241
242 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800244 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700245 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800246
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 // Show only these settings for restricted users
248 private int[] SETTINGS_FOR_RESTRICTED = {
249 R.id.wireless_section,
250 R.id.wifi_settings,
251 R.id.bluetooth_settings,
252 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700253 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254 R.id.wireless_settings,
255 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400256 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 R.id.display_settings,
258 R.id.storage_settings,
259 R.id.application_settings,
260 R.id.battery_settings,
261 R.id.personal_section,
262 R.id.location_settings,
263 R.id.security_settings,
264 R.id.language_settings,
265 R.id.user_settings,
266 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800267 R.id.system_section,
268 R.id.date_time_settings,
269 R.id.about_settings,
270 R.id.accessibility_settings,
271 R.id.print_settings,
272 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800273 R.id.home_settings,
274 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800275 };
276
277 private static final String[] ENTRY_FRAGMENTS = {
278 WirelessSettings.class.getName(),
279 WifiSettings.class.getName(),
280 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700281 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700283 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800284 TetherSettings.class.getName(),
285 WifiP2pSettings.class.getName(),
286 VpnSettings.class.getName(),
287 DateTimeSettings.class.getName(),
288 LocalePicker.class.getName(),
289 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700290 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 SpellCheckersSettings.class.getName(),
292 UserDictionaryList.class.getName(),
293 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700294 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800295 DisplaySettings.class.getName(),
296 DeviceInfoSettings.class.getName(),
297 ManageApplications.class.getName(),
298 ProcessStatsUi.class.getName(),
299 NotificationStation.class.getName(),
300 LocationSettings.class.getName(),
301 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700302 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 PrivacySettings.class.getName(),
304 DeviceAdminSettings.class.getName(),
305 AccessibilitySettings.class.getName(),
306 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
308 TextToSpeechSettings.class.getName(),
Jeff Sharkey42833b22015-04-11 21:27:33 -0700309 StorageSettings.class.getName(),
310 PublicVolumeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 DevelopmentSettings.class.getName(),
312 UsbSettings.class.getName(),
313 AndroidBeam.class.getName(),
314 WifiDisplaySettings.class.getName(),
315 PowerUsageSummary.class.getName(),
316 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000317 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 CryptKeeperSettings.class.getName(),
319 DataUsageSummary.class.getName(),
320 DreamSettings.class.getName(),
321 UserSettings.class.getName(),
322 NotificationAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 PrintSettingsFragment.class.getName(),
324 PrintJobSettingsFragment.class.getName(),
325 TrustedCredentialsSettings.class.getName(),
326 PaymentSettings.class.getName(),
327 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700328 ZenModeSettings.class.getName(),
329 NotificationSettings.class.getName(),
330 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
331 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400332 InstalledAppDetails.class.getName(),
333 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700334 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700335 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000336 ApnSettings.class.getName(),
John Spurlock45fa1402015-04-09 12:50:04 -0400337 WifiCallingSettings.class.getName(),
338 ZenModeScheduleRuleSettings.class.getName(),
John Spurlockc96a5dc2015-04-10 11:59:54 -0400339 ZenModeExternalRuleSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800340 };
341
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700342
343 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
344 "android.settings.APPLICATION_DETAILS_SETTINGS"
345 };
346
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 private SharedPreferences mDevelopmentPreferences;
348 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
349
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800350 private boolean mBatteryPresent = true;
351 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 @Override
353 public void onReceive(Context context, Intent intent) {
354 String action = intent.getAction();
355 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
356 boolean batteryPresent = Utils.isBatteryPresent(intent);
357
358 if (mBatteryPresent != batteryPresent) {
359 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700360 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 }
362 }
363 }
364 };
365
Svetoslav990159a2014-04-14 17:14:59 -0700366 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
367 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700368
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700369 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700370 private SwitchBar mSwitchBar;
371
372 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700373
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700374 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700375 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700376
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700377 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700378 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700379
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700380 private ViewGroup mContent;
381
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700382 private SearchView mSearchView;
383 private MenuItem mSearchMenuItem;
384 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700385 private SearchResultsSummary mSearchResultsFragment;
386 private String mSearchQuery;
387
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700388 // Categories
389 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800390
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700391 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700392 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800393 private Handler mHandler = new Handler() {
394 @Override
395 public void handleMessage(Message msg) {
396 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700397 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700398 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
399 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700400 buildDashboardCategories(mCategories);
401 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402 } break;
403 }
404 }
405 };
406
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700407 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700408 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700409
Jim Miller0698a212014-10-16 19:49:07 -0700410 private Intent mResultIntentData;
411
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700412 public SwitchBar getSwitchBar() {
413 return mSwitchBar;
414 }
415
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700416 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
417 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700418 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700419 }
420 return mCategories;
421 }
422
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800423 @Override
424 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
425 // Override the fragment title for Wallpaper settings
426 int titleRes = pref.getTitleRes();
427 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
428 titleRes = R.string.wallpaper_settings_fragment_title;
429 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
430 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
431 if (UserManager.get(this).isLinkedUser()) {
432 titleRes = R.string.profile_info_settings_title;
433 } else {
434 titleRes = R.string.user_info_settings_title;
435 }
436 }
437 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
438 null, 0);
439 return true;
440 }
441
442 @Override
443 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
444 return false;
445 }
446
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700447 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700448 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700449 Message msg = new Message();
450 msg.what = MSG_BUILD_CATEGORIES;
451 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800452 }
453 }
454
455 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800456 public void onConfigurationChanged(Configuration newConfig) {
457 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800458 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800459 }
460
461 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700462 protected void onStart() {
463 super.onStart();
464
465 if (mNeedToRevertToInitialFragment) {
466 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800467 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800468 }
469
470 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700471 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700472 if (!mDisplaySearch) {
473 return false;
474 }
475
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700476 MenuInflater inflater = getMenuInflater();
477 inflater.inflate(R.menu.options_menu, menu);
478
479 // Cache the search query (can be overriden by the OnQueryTextListener)
480 final String query = mSearchQuery;
481
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700482 mSearchMenuItem = menu.findItem(R.id.search);
483 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700484
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700485 if (mSearchMenuItem == null || mSearchView == null) {
486 return false;
487 }
488
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700489 if (mSearchResultsFragment != null) {
490 mSearchResultsFragment.setSearchView(mSearchView);
491 }
492
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700493 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700494 mSearchView.setOnQueryTextListener(this);
495 mSearchView.setOnCloseListener(this);
496
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700497 if (mSearchMenuItemExpanded) {
498 mSearchMenuItem.expandActionView();
499 }
500 mSearchView.setQuery(query, true /* submit */);
501
502 return true;
503 }
504
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700505 private static boolean isShortCutIntent(final Intent intent) {
506 Set<String> categories = intent.getCategories();
507 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
508 }
509
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700510 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700511 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700512 if (action == null) {
513 return false;
514 }
515 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
516 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
517 }
518 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700519 }
520
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700521 @Override
522 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700523 super.onCreate(savedState);
524
525 // Should happen before any call to getIntent()
526 getMetaData();
527
528 final Intent intent = getIntent();
529 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
530 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
534 Context.MODE_PRIVATE);
535
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700536 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700537 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700538
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700539 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700540 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
541
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700542 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700543 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700544
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700545 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700546
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700547 // This is a "Sub Settings" when:
548 // - this is a real SubSettings
549 // - or :settings:show_fragment_as_subsetting is passed to the Intent
550 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
551 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
552
553 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
554 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700555 // Check also that we are not a Theme Dialog as we don't want to override them
556 final int themeResId = getThemeResId();
557 if (themeResId != R.style.Theme_DialogWhenLarge &&
558 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
559 setTheme(R.style.Theme_SubSettings);
560 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700561 }
562
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700563 setContentView(mIsShowingDashboard ?
564 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800565
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700566 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700567
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568 getFragmentManager().addOnBackStackChangedListener(this);
569
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700570 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800571 // Run the Index update only if we have some space
572 if (!Utils.isLowStorage(this)) {
573 Index.getInstance(getApplicationContext()).update();
574 } else {
575 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
576 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700577 }
578
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700579 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700580 // We are restarting from a previous saved state; used that to initialize, instead
581 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700582 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
583 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800584
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700585 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800586
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700587 ArrayList<DashboardCategory> categories =
588 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
589 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700590 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700591 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700592 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800593 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700594
595 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700596 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700597 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
598 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700600 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700601 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
602 // only if it is a sub settings
603 if (mIsShortcut) {
604 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700605 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700606 } else if (isSubSettings) {
607 mDisplayHomeAsUpEnabled = true;
608 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700609 } else {
610 mDisplayHomeAsUpEnabled = false;
611 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700612 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700613 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700614
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700615 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700616 switchToFragment(initialFragmentName, initialArguments, true, false,
617 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000618 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700619 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700620 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700621 // Show Search affordance
622 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700623 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700624 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700625 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800626 }
627 }
628
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700629 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700630 if (mActionBar != null) {
631 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
632 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
633 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700634 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
635
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800636 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800637 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
638
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700639 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 if (buttonBar != null) {
641 buttonBar.setVisibility(View.VISIBLE);
642
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700643 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800644 backButton.setOnClickListener(new OnClickListener() {
645 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700646 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 finish();
648 }
649 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700650 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 skipButton.setOnClickListener(new OnClickListener() {
652 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700653 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 finish();
655 }
656 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700657 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 mNextButton.setOnClickListener(new OnClickListener() {
659 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700660 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 finish();
662 }
663 });
664
665 // set our various button parameters
666 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
667 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
668 if (TextUtils.isEmpty(buttonText)) {
669 mNextButton.setVisibility(View.GONE);
670 }
671 else {
672 mNextButton.setText(buttonText);
673 }
674 }
675 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
676 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
677 if (TextUtils.isEmpty(buttonText)) {
678 backButton.setVisibility(View.GONE);
679 }
680 else {
681 backButton.setText(buttonText);
682 }
683 }
684 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
685 skipButton.setVisibility(View.VISIBLE);
686 }
687 }
688 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700689
690 mHomeActivitiesCount = getHomeActivitiesCount();
691 }
692
693 private int getHomeActivitiesCount() {
694 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
695 getPackageManager().getHomeActivities(homeApps);
696 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800697 }
698
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700699 private void setTitleFromIntent(Intent intent) {
700 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
701 if (initialTitleResId > 0) {
702 mInitialTitle = null;
703 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100704
705 final String initialTitleResPackageName = intent.getStringExtra(
706 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
707 if (initialTitleResPackageName != null) {
708 try {
709 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
710 0 /* flags */, new UserHandle(UserHandle.myUserId()));
711 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
712 setTitle(mInitialTitle);
713 mInitialTitleResId = -1;
714 return;
715 } catch (NameNotFoundException e) {
716 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
717 }
718 } else {
719 setTitle(mInitialTitleResId);
720 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700721 } else {
722 mInitialTitleResId = -1;
723 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
724 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
725 setTitle(mInitialTitle);
726 }
727 }
728
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800729 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800730 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700731 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800732 }
733
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700734 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800735 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700736
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800737 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700738 if (mInitialTitleResId > 0) {
739 setTitle(mInitialTitleResId);
740 } else {
741 setTitle(mInitialTitle);
742 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700743 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800744 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700745
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800746 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
747 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700748
749 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800750 }
751
752 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
753 final CharSequence title;
754 final int titleRes = bse.getBreadCrumbTitleRes();
755 if (titleRes > 0) {
756 title = getText(titleRes);
757 } else {
758 title = bse.getBreadCrumbTitle();
759 }
760 if (title != null) {
761 setTitle(title);
762 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800763 }
764
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800765 @Override
766 protected void onSaveInstanceState(Bundle outState) {
767 super.onSaveInstanceState(outState);
768
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700769 if (mCategories.size() > 0) {
770 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800771 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700772
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700773 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700774 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700775
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700776 if (mDisplaySearch) {
777 // The option menus are created if the ActionBar is visible and they are also created
778 // asynchronously. If you launch Settings with an Intent action like
779 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
780 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
781 // menu item and search view are null.
782 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
783 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700784
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700785 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
786 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
787 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700788
789 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800790 }
791
792 @Override
793 public void onResume() {
794 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400795 if (mIsShowingDashboard) {
796 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
797 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800798
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700799 final int newHomeActivityCount = getHomeActivitiesCount();
800 if (newHomeActivityCount != mHomeActivitiesCount) {
801 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700802 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700803 }
804
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800805 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
806 @Override
807 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700808 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800809 }
810 };
811 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
812 mDevelopmentPreferencesListener);
813
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800814 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700815
Svetoslav990159a2014-04-14 17:14:59 -0700816 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700817
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700818 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700819 onQueryTextSubmit(mSearchQuery);
820 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800821 }
822
823 @Override
824 public void onPause() {
825 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400826 if (mIsShowingDashboard) {
827 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
828 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800829 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700830 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800831 }
832
833 @Override
834 public void onDestroy() {
835 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700836
837 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
838 mDevelopmentPreferencesListener);
839 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800840 }
841
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800842 protected boolean isValidFragment(String fragmentName) {
843 // Almost all fragments are wrapped in this,
844 // except for a few that have their own activities.
845 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
846 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
847 }
848 return false;
849 }
850
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800851 @Override
852 public Intent getIntent() {
853 Intent superIntent = super.getIntent();
854 String startingFragment = getStartingFragmentClass(superIntent);
855 // This is called from super.onCreate, isMultiPane() is not yet reliable
856 // Do not use onIsHidingHeaders either, which relies itself on this method
857 if (startingFragment != null) {
858 Intent modIntent = new Intent(superIntent);
859 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
860 Bundle args = superIntent.getExtras();
861 if (args != null) {
862 args = new Bundle(args);
863 } else {
864 args = new Bundle();
865 }
866 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100867 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800868 return modIntent;
869 }
870 return superIntent;
871 }
872
873 /**
874 * Checks if the component name in the intent is different from the Settings class and
875 * returns the class name to load as a fragment.
876 */
877 private String getStartingFragmentClass(Intent intent) {
878 if (mFragmentClass != null) return mFragmentClass;
879
880 String intentClass = intent.getComponent().getClassName();
881 if (intentClass.equals(getClass().getName())) return null;
882
883 if ("com.android.settings.ManageApplications".equals(intentClass)
884 || "com.android.settings.RunningServices".equals(intentClass)
885 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
886 // Old names of manage apps.
887 intentClass = com.android.settings.applications.ManageApplications.class.getName();
888 }
889
890 return intentClass;
891 }
892
893 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000894 * Start a new fragment containing a preference panel. If the preferences
895 * are being displayed in multi-pane mode, the given fragment class will
896 * be instantiated and placed in the appropriate pane. If running in
897 * single-pane mode, a new activity will be launched in which to show the
898 * fragment.
899 *
900 * @param fragmentClass Full name of the class implementing the fragment.
901 * @param args Any desired arguments to supply to the fragment.
902 * @param titleRes Optional resource identifier of the title of this
903 * fragment.
904 * @param titleText Optional text of the title of this fragment.
905 * @param resultTo Optional fragment that result data should be sent to.
906 * If non-null, resultTo.onActivityResult() will be called when this
907 * preference panel is done. The launched panel must use
908 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
909 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700910 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000911 */
912 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700913 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700914 String title = null;
915 if (titleRes < 0) {
916 if (titleText != null) {
917 title = titleText.toString();
918 } else {
919 // There not much we can do in that case
920 title = "";
921 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700922 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700923 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700924 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000925 }
926
927 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100928 * Start a new fragment in a new activity containing a preference panel for a given user. If the
929 * preferences are being displayed in multi-pane mode, the given fragment class will be
930 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
931 * activity will be launched in which to show the fragment.
932 *
933 * @param fragmentClass Full name of the class implementing the fragment.
934 * @param args Any desired arguments to supply to the fragment.
935 * @param titleRes Optional resource identifier of the title of this fragment.
936 * @param titleText Optional text of the title of this fragment.
937 * @param userHandle The user for which the panel has to be started.
938 */
939 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
940 CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700941 // This is a workaround.
942 //
943 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
944 // starting the fragment could cause a native stack corruption. See b/17523189. However,
945 // adding that flag and start the preference panel with the same UserHandler will make it
946 // impossible to use back button to return to the previous screen. See b/20042570.
947 //
948 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
949 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
950 // when we're calling it as the same user.
951 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
952 startPreferencePanel(fragmentClass, args, titleRes, titleText, null, 0);
953 } else {
954 String title = null;
955 if (titleRes < 0) {
956 if (titleText != null) {
957 title = titleText.toString();
958 } else {
959 // There not much we can do in that case
960 title = "";
961 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100962 }
Lifu Tangd0332852015-04-02 12:05:46 -0700963 Utils.startWithFragmentAsUser(this, fragmentClass, args,
964 titleRes, title, mIsShortcut, userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100965 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100966 }
967
968 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800969 * Called by a preference panel fragment to finish itself.
970 *
971 * @param caller The fragment that is asking to be finished.
972 * @param resultCode Optional result code to send back to the original
973 * launching fragment.
974 * @param resultData Optional result data to send back to the original
975 * launching fragment.
976 */
977 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
978 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700979 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800980 }
981
982 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000983 * Start a new fragment.
984 *
985 * @param fragment The fragment to start
986 * @param push If true, the current fragment will be pushed onto the back stack. If false,
987 * the current fragment will be replaced.
988 */
989 public void startPreferenceFragment(Fragment fragment, boolean push) {
990 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700991 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000992 if (push) {
993 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
994 transaction.addToBackStack(BACK_STACK_PREFS);
995 } else {
996 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
997 }
998 transaction.commitAllowingStateLoss();
999 }
1000
1001 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001002 * Switch to a specific Fragment with taking care of validation, Title and BackStack
1003 */
1004 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001005 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001006 if (validate && !isValidFragment(fragmentName)) {
1007 throw new IllegalArgumentException("Invalid fragment for this activity: "
1008 + fragmentName);
1009 }
1010 Fragment f = Fragment.instantiate(this, fragmentName, args);
1011 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001012 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001013 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001014 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001015 }
1016 if (addToBackStack) {
1017 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1018 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001019 if (titleResId > 0) {
1020 transaction.setBreadCrumbTitle(titleResId);
1021 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001022 transaction.setBreadCrumbTitle(title);
1023 }
1024 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001025 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001026 return f;
1027 }
1028
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001029 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001030 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001031 *
1032 * @param categories The list in which to place the tiles categories.
1033 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001034 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001035 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001036 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1037 updateTilesList(categories);
1038 }
1039
1040 /**
1041 * Parse the given XML file as a categories description, adding each
1042 * parsed categories and tiles into the target list.
1043 *
1044 * @param resid The XML resource to load and parse.
1045 * @param target The list in which the parsed categories and tiles should be placed.
1046 */
1047 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1048 XmlResourceParser parser = null;
1049 try {
1050 parser = getResources().getXml(resid);
1051 AttributeSet attrs = Xml.asAttributeSet(parser);
1052
1053 int type;
1054 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1055 && type != XmlPullParser.START_TAG) {
1056 // Parse next until start tag is found
1057 }
1058
1059 String nodeName = parser.getName();
1060 if (!"dashboard-categories".equals(nodeName)) {
1061 throw new RuntimeException(
1062 "XML document must start with <preference-categories> tag; found"
1063 + nodeName + " at " + parser.getPositionDescription());
1064 }
1065
1066 Bundle curBundle = null;
1067
1068 final int outerDepth = parser.getDepth();
1069 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1070 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1071 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1072 continue;
1073 }
1074
1075 nodeName = parser.getName();
1076 if ("dashboard-category".equals(nodeName)) {
1077 DashboardCategory category = new DashboardCategory();
1078
1079 TypedArray sa = obtainStyledAttributes(
1080 attrs, com.android.internal.R.styleable.PreferenceHeader);
1081 category.id = sa.getResourceId(
1082 com.android.internal.R.styleable.PreferenceHeader_id,
1083 (int)DashboardCategory.CAT_ID_UNDEFINED);
1084
1085 TypedValue tv = sa.peekValue(
1086 com.android.internal.R.styleable.PreferenceHeader_title);
1087 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1088 if (tv.resourceId != 0) {
1089 category.titleRes = tv.resourceId;
1090 } else {
1091 category.title = tv.string;
1092 }
1093 }
1094 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001095 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1096 tv = sa.peekValue(
1097 com.android.internal.R.styleable.Preference_key);
1098 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1099 if (tv.resourceId != 0) {
1100 category.key = getString(tv.resourceId);
1101 } else {
1102 category.key = tv.string.toString();
1103 }
1104 }
1105 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001106
1107 final int innerDepth = parser.getDepth();
1108 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1109 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1110 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1111 continue;
1112 }
1113
1114 String innerNodeName = parser.getName();
1115 if (innerNodeName.equals("dashboard-tile")) {
1116 DashboardTile tile = new DashboardTile();
1117
1118 sa = obtainStyledAttributes(
1119 attrs, com.android.internal.R.styleable.PreferenceHeader);
1120 tile.id = sa.getResourceId(
1121 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001122 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001123 tv = sa.peekValue(
1124 com.android.internal.R.styleable.PreferenceHeader_title);
1125 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1126 if (tv.resourceId != 0) {
1127 tile.titleRes = tv.resourceId;
1128 } else {
1129 tile.title = tv.string;
1130 }
1131 }
1132 tv = sa.peekValue(
1133 com.android.internal.R.styleable.PreferenceHeader_summary);
1134 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1135 if (tv.resourceId != 0) {
1136 tile.summaryRes = tv.resourceId;
1137 } else {
1138 tile.summary = tv.string;
1139 }
1140 }
1141 tile.iconRes = sa.getResourceId(
1142 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1143 tile.fragment = sa.getString(
1144 com.android.internal.R.styleable.PreferenceHeader_fragment);
1145 sa.recycle();
1146
1147 if (curBundle == null) {
1148 curBundle = new Bundle();
1149 }
1150
1151 final int innerDepth2 = parser.getDepth();
1152 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1153 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1154 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1155 continue;
1156 }
1157
1158 String innerNodeName2 = parser.getName();
1159 if (innerNodeName2.equals("extra")) {
1160 getResources().parseBundleExtra("extra", attrs, curBundle);
1161 XmlUtils.skipCurrentTag(parser);
1162
1163 } else if (innerNodeName2.equals("intent")) {
1164 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1165
1166 } else {
1167 XmlUtils.skipCurrentTag(parser);
1168 }
1169 }
1170
1171 if (curBundle.size() > 0) {
1172 tile.fragmentArguments = curBundle;
1173 curBundle = null;
1174 }
1175
PauloftheWest38155612014-06-30 10:02:36 -07001176 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001177 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001178 category.addTile(tile);
1179 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001180
Jason Monk2ebc8a02015-02-13 15:23:19 -05001181 } else if (innerNodeName.equals("external-tiles")) {
1182 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001183 } else {
1184 XmlUtils.skipCurrentTag(parser);
1185 }
1186 }
1187
1188 target.add(category);
1189 } else {
1190 XmlUtils.skipCurrentTag(parser);
1191 }
1192 }
1193
1194 } catch (XmlPullParserException e) {
1195 throw new RuntimeException("Error parsing categories", e);
1196 } catch (IOException e) {
1197 throw new RuntimeException("Error parsing categories", e);
1198 } finally {
1199 if (parser != null) parser.close();
1200 }
1201 }
1202
1203 private void updateTilesList(List<DashboardCategory> target) {
1204 final boolean showDev = mDevelopmentPreferences.getBoolean(
1205 DevelopmentSettings.PREF_SHOW,
1206 android.os.Build.TYPE.equals("eng"));
1207
1208 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1209
1210 final int size = target.size();
1211 for (int i = 0; i < size; i++) {
1212
1213 DashboardCategory category = target.get(i);
1214
1215 // Ids are integers, so downcasting is ok
1216 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001217 int n = category.getTilesCount() - 1;
1218 while (n >= 0) {
1219
1220 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001221 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001222 id = (int) tile.id;
1223 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001224 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1225 removeTile = true;
1226 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001227 } else if (id == R.id.wifi_settings) {
1228 // Remove WiFi Settings if WiFi service is not available.
1229 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001230 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001231 }
1232 } else if (id == R.id.bluetooth_settings) {
1233 // Remove Bluetooth Settings if Bluetooth service is not available.
1234 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001235 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001236 }
1237 } else if (id == R.id.data_usage_settings) {
1238 // Remove data usage when kernel module not enabled
1239 final INetworkManagementService netManager = INetworkManagementService.Stub
1240 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1241 try {
1242 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001243 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001244 }
1245 } catch (RemoteException e) {
1246 // ignored
1247 }
1248 } else if (id == R.id.battery_settings) {
1249 // Remove battery settings when battery is not available. (e.g. TV)
1250
1251 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001252 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001253 }
1254 } else if (id == R.id.home_settings) {
1255 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001256 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001257 }
1258 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001259 boolean hasMultipleUsers =
1260 ((UserManager) getSystemService(Context.USER_SERVICE))
1261 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001262 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001263 || (!UserManager.supportsMultipleUsers()
1264 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001265 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001266 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 }
1268 } else if (id == R.id.nfc_payment_settings) {
1269 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001270 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001271 } else {
1272 // Only show if NFC is on and we have the HCE feature
1273 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001274 if (adapter == null || !adapter.isEnabled() ||
1275 !getPackageManager().hasSystemFeature(
1276 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001277 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001278 }
1279 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001280 } else if (id == R.id.print_settings) {
1281 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1282 PackageManager.FEATURE_PRINTING);
1283 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001284 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001285 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001286 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001287 if (!showDev || um.hasUserRestriction(
1288 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001289 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001290 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001291 }
1292
1293 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001294 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1295 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001296 }
1297
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001298 if (removeTile && n < category.getTilesCount()) {
1299 category.removeTile(n);
1300 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001301 n--;
1302 }
1303 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001304 addExternalTiles(target);
1305 }
1306
1307 private void addExternalTiles(List<DashboardCategory> target) {
1308 Map<Pair<String, String>, DashboardTile> addedCache =
1309 new ArrayMap<Pair<String, String>, DashboardTile>();
1310 UserManager userManager = UserManager.get(this);
1311 for (UserHandle user : userManager.getUserProfiles()) {
1312 addExternalTiles(target, user, addedCache);
1313 }
1314 }
1315
1316 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1317 Map<Pair<String, String>, DashboardTile> addedCache) {
1318 PackageManager pm = getPackageManager();
1319 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1320 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1321 PackageManager.GET_META_DATA, user.getIdentifier());
1322 for (ResolveInfo resolved : results) {
1323 if (!resolved.system) {
1324 // Do not allow any app to add to settings, only system ones.
1325 continue;
1326 }
1327 ActivityInfo activityInfo = resolved.activityInfo;
1328 Bundle metaData = activityInfo.metaData;
1329 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1330 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1331 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1332 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1333 continue;
1334 }
1335 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1336 DashboardCategory category = getCategory(target, categoryKey);
1337 if (category == null) {
1338 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1339 + "category key " + categoryKey);
1340 continue;
1341 }
1342 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1343 activityInfo.name);
1344 DashboardTile tile = addedCache.get(key);
1345 if (tile == null) {
1346 tile = new DashboardTile();
1347 tile.intent = new Intent().setClassName(
1348 activityInfo.packageName, activityInfo.name);
1349 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1350
1351 if (category.externalIndex == -1) {
1352 // If no location for external tiles has been specified for this category,
1353 // then just put them at the end.
1354 category.addTile(tile);
1355 } else {
1356 category.addTile(category.externalIndex, tile);
1357 }
1358 addedCache.put(key, tile);
1359 }
1360 tile.userHandle.add(user);
1361 }
1362 }
1363
1364 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1365 for (DashboardCategory category : target) {
1366 if (categoryKey.equals(category.key)) {
1367 return category;
1368 }
1369 }
1370 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001371 }
1372
1373 private boolean updateHomeSettingTiles(DashboardTile tile) {
1374 // Once we decide to show Home settings, keep showing it forever
1375 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1376 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1377 return true;
1378 }
1379
1380 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001381 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001382 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001383 // When there's only one available home app, omit this settings
1384 // category entirely at the top level UI. If the user just
1385 // uninstalled the penultimate home app candidiate, we also
1386 // now tell them about why they aren't seeing 'Home' in the list.
1387 if (sShowNoHomeNotice) {
1388 sShowNoHomeNotice = false;
1389 NoHomeDialogFragment.show(this);
1390 }
1391 return false;
1392 } else {
1393 // Okay, we're allowing the Home settings category. Tell it, when
1394 // invoked via this front door, that we'll need to be told about the
1395 // case when the user uninstalls all but one home app.
1396 if (tile.fragmentArguments == null) {
1397 tile.fragmentArguments = new Bundle();
1398 }
1399 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1400 }
1401 } catch (Exception e) {
1402 // Can't look up the home activity; bail on configuring the icon
1403 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1404 }
1405
1406 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1407 return true;
1408 }
1409
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001410 private void getMetaData() {
1411 try {
1412 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1413 PackageManager.GET_META_DATA);
1414 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001415 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1416 } catch (NameNotFoundException nnfe) {
1417 // No recovery
1418 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1419 }
1420 }
1421
1422 // give subclasses access to the Next button
1423 public boolean hasNextButton() {
1424 return mNextButton != null;
1425 }
1426
1427 public Button getNextButton() {
1428 return mNextButton;
1429 }
1430
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001431 @Override
1432 public boolean shouldUpRecreateTask(Intent targetIntent) {
1433 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1434 }
1435
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001436 public static void requestHomeNotice() {
1437 sShowNoHomeNotice = true;
1438 }
1439
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001440 @Override
1441 public boolean onQueryTextSubmit(String query) {
1442 switchToSearchResultsFragmentIfNeeded();
1443 mSearchQuery = query;
1444 return mSearchResultsFragment.onQueryTextSubmit(query);
1445 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001446
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001447 @Override
1448 public boolean onQueryTextChange(String newText) {
1449 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001450 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001451 return false;
1452 }
1453 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001454 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001455
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001456 @Override
1457 public boolean onClose() {
1458 return false;
1459 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001460
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001461 @Override
1462 public boolean onMenuItemActionExpand(MenuItem item) {
1463 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001464 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001465 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001466 return true;
1467 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001468
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001469 @Override
1470 public boolean onMenuItemActionCollapse(MenuItem item) {
1471 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001472 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001473 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001474 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001475 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001476 return true;
1477 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001478
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001479 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001480 if (mSearchResultsFragment != null) {
1481 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001482 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001483 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001484 if (current != null && current instanceof SearchResultsSummary) {
1485 mSearchResultsFragment = (SearchResultsSummary) current;
1486 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001487 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001488 SearchResultsSummary.class.getName(), null, false, true,
1489 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001490 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001491 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001492 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001493 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001494
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001495 public void needToRevertToInitialFragment() {
1496 mNeedToRevertToInitialFragment = true;
1497 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001498
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001499 private void revertToInitialFragment() {
1500 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001501 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001502 mSearchMenuItemExpanded = false;
1503 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1504 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001505 if (mSearchMenuItem != null) {
1506 mSearchMenuItem.collapseActionView();
1507 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001508 }
Jim Miller0698a212014-10-16 19:49:07 -07001509
1510 public Intent getResultIntentData() {
1511 return mResultIntentData;
1512 }
1513
1514 public void setResultIntentData(Intent resultIntentData) {
1515 mResultIntentData = resultIntentData;
1516 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001517}