blob: 12dee4a05636b88ee9ac59aaf15ab32ea4764158 [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
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.accounts.OnAccountsUpdateListener;
22import android.app.ActionBar;
23import android.app.Activity;
24import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080027import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070028import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080029import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
32import android.content.SharedPreferences;
33import android.content.pm.ActivityInfo;
34import android.content.pm.PackageManager;
35import android.content.pm.PackageManager.NameNotFoundException;
36import android.content.pm.ResolveInfo;
37import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.content.res.TypedArray;
39import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.nfc.NfcAdapter;
41import android.os.Bundle;
42import android.os.Handler;
43import android.os.INetworkManagementService;
44import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.UserHandle;
48import android.os.UserManager;
49import android.preference.Preference;
50import android.preference.PreferenceFragment;
51import android.preference.PreferenceManager;
52import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070054import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.AttributeSet;
56import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080057import android.util.TypedValue;
58import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070059import android.view.Menu;
60import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080061import android.view.MenuItem;
62import android.view.View;
63import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070064import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080065import android.widget.Button;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080066
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070067import android.widget.SearchView;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080068import com.android.internal.util.ArrayUtils;
69import com.android.internal.util.XmlUtils;
70import com.android.settings.accessibility.AccessibilitySettings;
71import com.android.settings.accessibility.CaptionPropertiesFragment;
72import com.android.settings.accounts.AccountSyncSettings;
73import com.android.settings.accounts.AuthenticatorHelper;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.accounts.ManageAccountsSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070075import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.applications.ManageApplications;
77import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070079import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070082import com.android.settings.dashboard.NoHomeDialogFragment;
83import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084import com.android.settings.deviceinfo.Memory;
85import com.android.settings.deviceinfo.UsbSettings;
86import com.android.settings.fuelgauge.PowerUsageSummary;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070087import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070088import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
90import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
91import com.android.settings.inputmethod.SpellCheckersSettings;
92import com.android.settings.inputmethod.UserDictionaryList;
93import com.android.settings.location.LocationSettings;
94import com.android.settings.nfc.AndroidBeam;
95import com.android.settings.nfc.PaymentSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040096import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040097import com.android.settings.notification.NotificationAccessSettings;
98import com.android.settings.notification.NotificationSettings;
99import com.android.settings.notification.NotificationStation;
100import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800101import com.android.settings.print.PrintJobSettingsFragment;
102import com.android.settings.print.PrintSettingsFragment;
103import com.android.settings.tts.TextToSpeechSettings;
104import com.android.settings.users.UserSettings;
105import com.android.settings.vpn2.VpnSettings;
106import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700107import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.wifi.AdvancedWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.wifi.WifiSettings;
110import com.android.settings.wifi.p2p.WifiP2pSettings;
111import org.xmlpull.v1.XmlPullParser;
112import org.xmlpull.v1.XmlPullParserException;
113
114import java.io.IOException;
115import java.util.ArrayList;
116import java.util.Collections;
117import java.util.Comparator;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import java.util.List;
119
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700120import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700121
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122public class SettingsActivity extends Activity
123 implements PreferenceManager.OnPreferenceTreeClickListener,
124 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700125 ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
126 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
127 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800128
129 private static final String LOG_TAG = "Settings";
130
131 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700132 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700133 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
134 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700135 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700136 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800137
138 /**
139 * When starting this activity, the invoking Intent can contain this extra
140 * string to specify which fragment should be initially displayed.
141 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
142 * will call isValidFragment() to confirm that the fragment class name is valid for this
143 * activity.
144 */
145 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
146
147 /**
148 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
149 * this extra can also be specified to supply a Bundle of arguments to pass
150 * to that fragment when it is instantiated during the initial creation
151 * of the activity.
152 */
153 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
154
155 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700156 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
157 */
158 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
159
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800160 public static final String BACK_STACK_PREFS = ":settings:prefs";
161
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162 // extras that allow any preference activity to be launched as part of a wizard
163
164 // show Back and Next buttons? takes boolean parameter
165 // Back will then return RESULT_CANCELED and Next RESULT_OK
166 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
167
168 // add a Skip button?
169 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
170
171 // specify custom text for the Back or Next buttons, or cause a button to not appear
172 // at all by setting it to null
173 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
174 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
175
176 /**
177 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700178 * 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 -0800179 * that fragment.
180 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700181 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700182 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800183
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800184 private static final String META_DATA_KEY_FRAGMENT_CLASS =
185 "com.android.settings.FRAGMENT_CLASS";
186
187 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
188
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700189 private static final String EMPTY_QUERY = "";
190
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191 private static boolean sShowNoHomeNotice = false;
192
193 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800195 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700196 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800197
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198 // Show only these settings for restricted users
199 private int[] SETTINGS_FOR_RESTRICTED = {
200 R.id.wireless_section,
201 R.id.wifi_settings,
202 R.id.bluetooth_settings,
203 R.id.data_usage_settings,
204 R.id.wireless_settings,
205 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400206 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207 R.id.display_settings,
208 R.id.storage_settings,
209 R.id.application_settings,
210 R.id.battery_settings,
211 R.id.personal_section,
212 R.id.location_settings,
213 R.id.security_settings,
214 R.id.language_settings,
215 R.id.user_settings,
216 R.id.account_settings,
217 R.id.account_add,
218 R.id.system_section,
219 R.id.date_time_settings,
220 R.id.about_settings,
221 R.id.accessibility_settings,
222 R.id.print_settings,
223 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800224 R.id.home_settings,
225 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800226 };
227
228 private static final String[] ENTRY_FRAGMENTS = {
229 WirelessSettings.class.getName(),
230 WifiSettings.class.getName(),
231 AdvancedWifiSettings.class.getName(),
232 BluetoothSettings.class.getName(),
233 TetherSettings.class.getName(),
234 WifiP2pSettings.class.getName(),
235 VpnSettings.class.getName(),
236 DateTimeSettings.class.getName(),
237 LocalePicker.class.getName(),
238 InputMethodAndLanguageSettings.class.getName(),
239 SpellCheckersSettings.class.getName(),
240 UserDictionaryList.class.getName(),
241 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800242 DisplaySettings.class.getName(),
243 DeviceInfoSettings.class.getName(),
244 ManageApplications.class.getName(),
245 ProcessStatsUi.class.getName(),
246 NotificationStation.class.getName(),
247 LocationSettings.class.getName(),
248 SecuritySettings.class.getName(),
249 PrivacySettings.class.getName(),
250 DeviceAdminSettings.class.getName(),
251 AccessibilitySettings.class.getName(),
252 CaptionPropertiesFragment.class.getName(),
253 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
255 TextToSpeechSettings.class.getName(),
256 Memory.class.getName(),
257 DevelopmentSettings.class.getName(),
258 UsbSettings.class.getName(),
259 AndroidBeam.class.getName(),
260 WifiDisplaySettings.class.getName(),
261 PowerUsageSummary.class.getName(),
262 AccountSyncSettings.class.getName(),
263 CryptKeeperSettings.class.getName(),
264 DataUsageSummary.class.getName(),
265 DreamSettings.class.getName(),
266 UserSettings.class.getName(),
267 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400268 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 ManageAccountsSettings.class.getName(),
270 PrintSettingsFragment.class.getName(),
271 PrintJobSettingsFragment.class.getName(),
272 TrustedCredentialsSettings.class.getName(),
273 PaymentSettings.class.getName(),
274 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700275 ZenModeSettings.class.getName(),
276 NotificationSettings.class.getName(),
277 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
278 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
279 InstalledAppDetails.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800280 };
281
282 private SharedPreferences mDevelopmentPreferences;
283 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
284
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 private AuthenticatorHelper mAuthenticatorHelper;
286 private boolean mListeningToAccountUpdates;
287
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 private boolean mBatteryPresent = true;
289 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
290
291 @Override
292 public void onReceive(Context context, Intent intent) {
293 String action = intent.getAction();
294 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
295 boolean batteryPresent = Utils.isBatteryPresent(intent);
296
297 if (mBatteryPresent != batteryPresent) {
298 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700299 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 }
301 }
302 }
303 };
304
Svetoslav990159a2014-04-14 17:14:59 -0700305 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
306 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700307
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700308 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700309 private SwitchBar mSwitchBar;
310
311 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700312
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700313 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700314 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700315
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700316 private boolean mIsShowingDashboard;
317
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700318 private ViewGroup mContent;
319
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700320 private SearchView mSearchView;
321 private MenuItem mSearchMenuItem;
322 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700323 private SearchResultsSummary mSearchResultsFragment;
324 private String mSearchQuery;
325
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700326 // Categories
327 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
328 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800329
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700330 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800331 private Handler mHandler = new Handler() {
332 @Override
333 public void handleMessage(Message msg) {
334 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700335 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700336 if(mNeedToRebuildCategories) {
337 buildDashboardCategories(mCategories);
338 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800339 } break;
340 }
341 }
342 };
343
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700344 private boolean mNeedToRevertToInitialFragment = false;
345
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700346 public SwitchBar getSwitchBar() {
347 return mSwitchBar;
348 }
349
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700350 public AuthenticatorHelper getAuthenticatorHelper() {
351 return mAuthenticatorHelper;
352 }
353
354 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700355 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700356 buildDashboardCategories(mCategories);
357 mNeedToRebuildCategories = false;
358 }
359 return mCategories;
360 }
361
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 @Override
363 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
364 // Override the fragment title for Wallpaper settings
365 int titleRes = pref.getTitleRes();
366 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
367 titleRes = R.string.wallpaper_settings_fragment_title;
368 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
369 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
370 if (UserManager.get(this).isLinkedUser()) {
371 titleRes = R.string.profile_info_settings_title;
372 } else {
373 titleRes = R.string.user_info_settings_title;
374 }
375 }
376 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
377 null, 0);
378 return true;
379 }
380
381 @Override
382 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
383 return false;
384 }
385
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700386 private void invalidateCategories() {
387 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
388 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800389 }
390 }
391
392 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800393 public void onConfigurationChanged(Configuration newConfig) {
394 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800395 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800396 }
397
398 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700399 protected void onStart() {
400 super.onStart();
401
402 if (mNeedToRevertToInitialFragment) {
403 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405 }
406
407 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700408 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700409 if (!mDisplaySearch) {
410 return false;
411 }
412
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700413 MenuInflater inflater = getMenuInflater();
414 inflater.inflate(R.menu.options_menu, menu);
415
416 // Cache the search query (can be overriden by the OnQueryTextListener)
417 final String query = mSearchQuery;
418
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700419 mSearchMenuItem = menu.findItem(R.id.search);
420 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700422 if (mSearchMenuItem == null || mSearchView == null) {
423 return false;
424 }
425
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700426 if (mSearchResultsFragment != null) {
427 mSearchResultsFragment.setSearchView(mSearchView);
428 }
429
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700430 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700431 mSearchView.setOnQueryTextListener(this);
432 mSearchView.setOnCloseListener(this);
433
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700434 if (mSearchMenuItemExpanded) {
435 mSearchMenuItem.expandActionView();
436 }
437 mSearchView.setQuery(query, true /* submit */);
438
439 return true;
440 }
441
442 @Override
443 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800444 if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
445 getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
446 }
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700447
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800448 mAuthenticatorHelper = new AuthenticatorHelper();
449 mAuthenticatorHelper.updateAuthDescriptions(this);
450 mAuthenticatorHelper.onAccountsUpdated(this, null);
451
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800452 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
453 Context.MODE_PRIVATE);
454
455 getMetaData();
456
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700457 super.onCreate(savedState);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800458
459 setContentView(R.layout.settings_main);
460
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700461 mContent = (ViewGroup) findViewById(R.id.prefs);
462
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463 getFragmentManager().addOnBackStackChangedListener(this);
464
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700465 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700466 mDisplaySearch = true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800467
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700468 // Getting Intent properties can only be done after the super.onCreate(...)
469 final String initialFragmentName = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
470
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700471 mIsShowingDashboard = (initialFragmentName == null);
472
473 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700474 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700475 }
476
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700477 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700478 // We are restarting from a previous saved state; used that to initialize, instead
479 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700480 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
481 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800482
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700483 setTitleFromIntent(getIntent());
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800484
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700485 ArrayList<DashboardCategory> categories =
486 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
487 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700488 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700489 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700490 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800491 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700492
493 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700494 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700496 if (!mIsShowingDashboard) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700497 final ComponentName cn = getIntent().getComponent();
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700498 // No UP nor Search is shown we are launched thru a Settings "shortcut"
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700499 if (!cn.getClassName().equals(SubSettings.class.getName())) {
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700500 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700501 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700502 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700503 setTitleFromIntent(getIntent());
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700504
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700505 Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700506 switchToFragment(initialFragmentName, initialArguments, true, false,
507 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000508 } else {
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700509 // No UP if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700510 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700511 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700512 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700513 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800514 }
515 }
516
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700517 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700518 if (mActionBar != null) {
519 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
520 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
521 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700522 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
523
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800524 // see if we should show Back/Next buttons
525 Intent intent = getIntent();
526 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
527
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700528 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800529 if (buttonBar != null) {
530 buttonBar.setVisibility(View.VISIBLE);
531
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700532 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 backButton.setOnClickListener(new OnClickListener() {
534 public void onClick(View v) {
535 setResult(RESULT_CANCELED);
536 finish();
537 }
538 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700539 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800540 skipButton.setOnClickListener(new OnClickListener() {
541 public void onClick(View v) {
542 setResult(RESULT_OK);
543 finish();
544 }
545 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700546 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 mNextButton.setOnClickListener(new OnClickListener() {
548 public void onClick(View v) {
549 setResult(RESULT_OK);
550 finish();
551 }
552 });
553
554 // set our various button parameters
555 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
556 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
557 if (TextUtils.isEmpty(buttonText)) {
558 mNextButton.setVisibility(View.GONE);
559 }
560 else {
561 mNextButton.setText(buttonText);
562 }
563 }
564 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
565 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
566 if (TextUtils.isEmpty(buttonText)) {
567 backButton.setVisibility(View.GONE);
568 }
569 else {
570 backButton.setText(buttonText);
571 }
572 }
573 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
574 skipButton.setVisibility(View.VISIBLE);
575 }
576 }
577 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800578 }
579
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700580 private void setTitleFromIntent(Intent intent) {
581 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
582 if (initialTitleResId > 0) {
583 mInitialTitle = null;
584 mInitialTitleResId = initialTitleResId;
585 setTitle(mInitialTitleResId);
586 } else {
587 mInitialTitleResId = -1;
588 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
589 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
590 setTitle(mInitialTitle);
591 }
592 }
593
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800594 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700596 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 }
598
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700599 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800600 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700601
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800602 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700603 if (mInitialTitleResId > 0) {
604 setTitle(mInitialTitleResId);
605 } else {
606 setTitle(mInitialTitle);
607 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700608 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800609 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700610
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800611 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
612 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700613
614 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800615 }
616
617 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
618 final CharSequence title;
619 final int titleRes = bse.getBreadCrumbTitleRes();
620 if (titleRes > 0) {
621 title = getText(titleRes);
622 } else {
623 title = bse.getBreadCrumbTitle();
624 }
625 if (title != null) {
626 setTitle(title);
627 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800628 }
629
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800630 @Override
631 protected void onSaveInstanceState(Bundle outState) {
632 super.onSaveInstanceState(outState);
633
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700634 if (mCategories.size() > 0) {
635 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800636 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700637
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700638 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700639 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700640
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700641 if (mDisplaySearch) {
642 // The option menus are created if the ActionBar is visible and they are also created
643 // asynchronously. If you launch Settings with an Intent action like
644 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
645 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
646 // menu item and search view are null.
647 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
648 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700649
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700650 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
651 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
652 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 }
654
655 @Override
656 public void onResume() {
657 super.onResume();
658
659 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
660 @Override
661 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700662 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700663 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800664 }
665 };
666 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
667 mDevelopmentPreferencesListener);
668
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700670
Svetoslav990159a2014-04-14 17:14:59 -0700671 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700672
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700673 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700674 onQueryTextSubmit(mSearchQuery);
675 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800676 }
677
678 @Override
679 public void onPause() {
680 super.onPause();
681
682 unregisterReceiver(mBatteryInfoReceiver);
683
Svetoslav990159a2014-04-14 17:14:59 -0700684 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800685 }
686
687 @Override
688 public void onDestroy() {
689 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700690
691 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
692 mDevelopmentPreferencesListener);
693 mDevelopmentPreferencesListener = null;
694
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800695 if (mListeningToAccountUpdates) {
696 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
697 }
698 }
699
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800700 protected boolean isValidFragment(String fragmentName) {
701 // Almost all fragments are wrapped in this,
702 // except for a few that have their own activities.
703 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
704 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
705 }
706 return false;
707 }
708
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800709 @Override
710 public Intent getIntent() {
711 Intent superIntent = super.getIntent();
712 String startingFragment = getStartingFragmentClass(superIntent);
713 // This is called from super.onCreate, isMultiPane() is not yet reliable
714 // Do not use onIsHidingHeaders either, which relies itself on this method
715 if (startingFragment != null) {
716 Intent modIntent = new Intent(superIntent);
717 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
718 Bundle args = superIntent.getExtras();
719 if (args != null) {
720 args = new Bundle(args);
721 } else {
722 args = new Bundle();
723 }
724 args.putParcelable("intent", superIntent);
725 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
726 return modIntent;
727 }
728 return superIntent;
729 }
730
731 /**
732 * Checks if the component name in the intent is different from the Settings class and
733 * returns the class name to load as a fragment.
734 */
735 private String getStartingFragmentClass(Intent intent) {
736 if (mFragmentClass != null) return mFragmentClass;
737
738 String intentClass = intent.getComponent().getClassName();
739 if (intentClass.equals(getClass().getName())) return null;
740
741 if ("com.android.settings.ManageApplications".equals(intentClass)
742 || "com.android.settings.RunningServices".equals(intentClass)
743 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
744 // Old names of manage apps.
745 intentClass = com.android.settings.applications.ManageApplications.class.getName();
746 }
747
748 return intentClass;
749 }
750
751 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000752 * Start a new fragment containing a preference panel. If the preferences
753 * are being displayed in multi-pane mode, the given fragment class will
754 * be instantiated and placed in the appropriate pane. If running in
755 * single-pane mode, a new activity will be launched in which to show the
756 * fragment.
757 *
758 * @param fragmentClass Full name of the class implementing the fragment.
759 * @param args Any desired arguments to supply to the fragment.
760 * @param titleRes Optional resource identifier of the title of this
761 * fragment.
762 * @param titleText Optional text of the title of this fragment.
763 * @param resultTo Optional fragment that result data should be sent to.
764 * If non-null, resultTo.onActivityResult() will be called when this
765 * preference panel is done. The launched panel must use
766 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
767 * @param resultRequestCode If resultTo is non-null, this is the caller's
768 * request code to be received with the resut.
769 */
770 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700771 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700772 String title = null;
773 if (titleRes < 0) {
774 if (titleText != null) {
775 title = titleText.toString();
776 } else {
777 // There not much we can do in that case
778 title = "";
779 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700780 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700781 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
782 titleRes, title);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000783 }
784
785 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800786 * Called by a preference panel fragment to finish itself.
787 *
788 * @param caller The fragment that is asking to be finished.
789 * @param resultCode Optional result code to send back to the original
790 * launching fragment.
791 * @param resultData Optional result data to send back to the original
792 * launching fragment.
793 */
794 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
795 setResult(resultCode, resultData);
796 }
797
798 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000799 * Start a new fragment.
800 *
801 * @param fragment The fragment to start
802 * @param push If true, the current fragment will be pushed onto the back stack. If false,
803 * the current fragment will be replaced.
804 */
805 public void startPreferenceFragment(Fragment fragment, boolean push) {
806 FragmentTransaction transaction = getFragmentManager().beginTransaction();
807 transaction.replace(R.id.prefs, fragment);
808 if (push) {
809 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
810 transaction.addToBackStack(BACK_STACK_PREFS);
811 } else {
812 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
813 }
814 transaction.commitAllowingStateLoss();
815 }
816
817 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700818 * Switch to a specific Fragment with taking care of validation, Title and BackStack
819 */
820 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700821 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700822 if (validate && !isValidFragment(fragmentName)) {
823 throw new IllegalArgumentException("Invalid fragment for this activity: "
824 + fragmentName);
825 }
826 Fragment f = Fragment.instantiate(this, fragmentName, args);
827 FragmentTransaction transaction = getFragmentManager().beginTransaction();
828 transaction.replace(R.id.prefs, f);
829 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700830 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700831 }
832 if (addToBackStack) {
833 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
834 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700835 if (titleResId > 0) {
836 transaction.setBreadCrumbTitle(titleResId);
837 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700838 transaction.setBreadCrumbTitle(title);
839 }
840 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700841 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700842 return f;
843 }
844
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700845 public void setNeedToRebuildCategories(boolean need) {
846 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700847 }
848
849 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700850 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700851 *
852 * @param categories The list in which to place the tiles categories.
853 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700854 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700855 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700856 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
857 updateTilesList(categories);
858 }
859
860 /**
861 * Parse the given XML file as a categories description, adding each
862 * parsed categories and tiles into the target list.
863 *
864 * @param resid The XML resource to load and parse.
865 * @param target The list in which the parsed categories and tiles should be placed.
866 */
867 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
868 XmlResourceParser parser = null;
869 try {
870 parser = getResources().getXml(resid);
871 AttributeSet attrs = Xml.asAttributeSet(parser);
872
873 int type;
874 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
875 && type != XmlPullParser.START_TAG) {
876 // Parse next until start tag is found
877 }
878
879 String nodeName = parser.getName();
880 if (!"dashboard-categories".equals(nodeName)) {
881 throw new RuntimeException(
882 "XML document must start with <preference-categories> tag; found"
883 + nodeName + " at " + parser.getPositionDescription());
884 }
885
886 Bundle curBundle = null;
887
888 final int outerDepth = parser.getDepth();
889 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
890 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
891 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
892 continue;
893 }
894
895 nodeName = parser.getName();
896 if ("dashboard-category".equals(nodeName)) {
897 DashboardCategory category = new DashboardCategory();
898
899 TypedArray sa = obtainStyledAttributes(
900 attrs, com.android.internal.R.styleable.PreferenceHeader);
901 category.id = sa.getResourceId(
902 com.android.internal.R.styleable.PreferenceHeader_id,
903 (int)DashboardCategory.CAT_ID_UNDEFINED);
904
905 TypedValue tv = sa.peekValue(
906 com.android.internal.R.styleable.PreferenceHeader_title);
907 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
908 if (tv.resourceId != 0) {
909 category.titleRes = tv.resourceId;
910 } else {
911 category.title = tv.string;
912 }
913 }
914 sa.recycle();
915
916 final int innerDepth = parser.getDepth();
917 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
918 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
919 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
920 continue;
921 }
922
923 String innerNodeName = parser.getName();
924 if (innerNodeName.equals("dashboard-tile")) {
925 DashboardTile tile = new DashboardTile();
926
927 sa = obtainStyledAttributes(
928 attrs, com.android.internal.R.styleable.PreferenceHeader);
929 tile.id = sa.getResourceId(
930 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700931 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700932 tv = sa.peekValue(
933 com.android.internal.R.styleable.PreferenceHeader_title);
934 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
935 if (tv.resourceId != 0) {
936 tile.titleRes = tv.resourceId;
937 } else {
938 tile.title = tv.string;
939 }
940 }
941 tv = sa.peekValue(
942 com.android.internal.R.styleable.PreferenceHeader_summary);
943 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
944 if (tv.resourceId != 0) {
945 tile.summaryRes = tv.resourceId;
946 } else {
947 tile.summary = tv.string;
948 }
949 }
950 tile.iconRes = sa.getResourceId(
951 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
952 tile.fragment = sa.getString(
953 com.android.internal.R.styleable.PreferenceHeader_fragment);
954 sa.recycle();
955
956 if (curBundle == null) {
957 curBundle = new Bundle();
958 }
959
960 final int innerDepth2 = parser.getDepth();
961 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
962 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
963 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
964 continue;
965 }
966
967 String innerNodeName2 = parser.getName();
968 if (innerNodeName2.equals("extra")) {
969 getResources().parseBundleExtra("extra", attrs, curBundle);
970 XmlUtils.skipCurrentTag(parser);
971
972 } else if (innerNodeName2.equals("intent")) {
973 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
974
975 } else {
976 XmlUtils.skipCurrentTag(parser);
977 }
978 }
979
980 if (curBundle.size() > 0) {
981 tile.fragmentArguments = curBundle;
982 curBundle = null;
983 }
984
985 category.addTile(tile);
986
987 } else {
988 XmlUtils.skipCurrentTag(parser);
989 }
990 }
991
992 target.add(category);
993 } else {
994 XmlUtils.skipCurrentTag(parser);
995 }
996 }
997
998 } catch (XmlPullParserException e) {
999 throw new RuntimeException("Error parsing categories", e);
1000 } catch (IOException e) {
1001 throw new RuntimeException("Error parsing categories", e);
1002 } finally {
1003 if (parser != null) parser.close();
1004 }
1005 }
1006
1007 private void updateTilesList(List<DashboardCategory> target) {
1008 final boolean showDev = mDevelopmentPreferences.getBoolean(
1009 DevelopmentSettings.PREF_SHOW,
1010 android.os.Build.TYPE.equals("eng"));
1011
1012 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1013
1014 final int size = target.size();
1015 for (int i = 0; i < size; i++) {
1016
1017 DashboardCategory category = target.get(i);
1018
1019 // Ids are integers, so downcasting is ok
1020 int id = (int) category.id;
1021 if (id == R.id.account_settings) {
1022 insertAccountsTiles(category);
1023 continue;
1024 }
1025 int n = category.getTilesCount() - 1;
1026 while (n >= 0) {
1027
1028 DashboardTile tile = category.getTile(n);
1029
1030 id = (int) tile.id;
1031 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1032 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1033 } else if (id == R.id.wifi_settings) {
1034 // Remove WiFi Settings if WiFi service is not available.
1035 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1036 category.removeTile(n);
1037 }
1038 } else if (id == R.id.bluetooth_settings) {
1039 // Remove Bluetooth Settings if Bluetooth service is not available.
1040 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1041 category.removeTile(n);
1042 }
1043 } else if (id == R.id.data_usage_settings) {
1044 // Remove data usage when kernel module not enabled
1045 final INetworkManagementService netManager = INetworkManagementService.Stub
1046 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1047 try {
1048 if (!netManager.isBandwidthControlEnabled()) {
1049 category.removeTile(n);
1050 }
1051 } catch (RemoteException e) {
1052 // ignored
1053 }
1054 } else if (id == R.id.battery_settings) {
1055 // Remove battery settings when battery is not available. (e.g. TV)
1056
1057 if (!mBatteryPresent) {
1058 category.removeTile(n);
1059 }
1060 } else if (id == R.id.home_settings) {
1061 if (!updateHomeSettingTiles(tile)) {
1062 category.removeTile(n);
1063 }
1064 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001065 boolean hasMultipleUsers =
1066 ((UserManager) getSystemService(Context.USER_SERVICE))
1067 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001068 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001069 || (!UserManager.supportsMultipleUsers()
1070 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001071 || Utils.isMonkeyRunning()) {
1072 category.removeTile(n);
1073 }
1074 } else if (id == R.id.nfc_payment_settings) {
1075 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1076 category.removeTile(n);
1077 } else {
1078 // Only show if NFC is on and we have the HCE feature
1079 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1080 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1081 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1082 category.removeTile(n);
1083 }
1084 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001085 } else if (id == R.id.print_settings) {
1086 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1087 PackageManager.FEATURE_PRINTING);
1088 if (!hasPrintingSupport) {
1089 category.removeTile(n);
1090 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001091 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001092 if (!showDev || um.hasUserRestriction(
1093 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001094 category.removeTile(n);
1095 }
1096 } else if (id == R.id.account_add) {
1097 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1098 category.removeTile(n);
1099 }
1100 }
1101
1102 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001103 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1104 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001105 category.removeTile(n);
1106 }
1107
1108 n--;
1109 }
1110 }
1111 }
1112
1113 private boolean updateHomeSettingTiles(DashboardTile tile) {
1114 // Once we decide to show Home settings, keep showing it forever
1115 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1116 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1117 return true;
1118 }
1119
1120 try {
1121 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1122 getPackageManager().getHomeActivities(homeApps);
1123 if (homeApps.size() < 2) {
1124 // When there's only one available home app, omit this settings
1125 // category entirely at the top level UI. If the user just
1126 // uninstalled the penultimate home app candidiate, we also
1127 // now tell them about why they aren't seeing 'Home' in the list.
1128 if (sShowNoHomeNotice) {
1129 sShowNoHomeNotice = false;
1130 NoHomeDialogFragment.show(this);
1131 }
1132 return false;
1133 } else {
1134 // Okay, we're allowing the Home settings category. Tell it, when
1135 // invoked via this front door, that we'll need to be told about the
1136 // case when the user uninstalls all but one home app.
1137 if (tile.fragmentArguments == null) {
1138 tile.fragmentArguments = new Bundle();
1139 }
1140 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1141 }
1142 } catch (Exception e) {
1143 // Can't look up the home activity; bail on configuring the icon
1144 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1145 }
1146
1147 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1148 return true;
1149 }
1150
1151 private void insertAccountsTiles(DashboardCategory target) {
1152 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1153 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1154 for (String accountType : accountTypes) {
1155 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1156 if (label == null) {
1157 continue;
1158 }
1159
1160 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1161 boolean skipToAccount = accounts.length == 1
1162 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1163 DashboardTile accountTile = new DashboardTile();
1164 accountTile.title = label;
1165 if (accountTile.extras == null) {
1166 accountTile.extras = new Bundle();
1167 }
1168 if (skipToAccount) {
1169 accountTile.fragment = AccountSyncSettings.class.getName();
1170 accountTile.fragmentArguments = new Bundle();
1171 // Need this for the icon
1172 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1173 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1174 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1175 accounts[0]);
1176 } else {
1177 accountTile.fragment = ManageAccountsSettings.class.getName();
1178 accountTile.fragmentArguments = new Bundle();
1179 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1180 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1181 accountType);
1182 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1183 label.toString());
1184 }
1185 dashboardTiles.add(accountTile);
1186 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1187 }
1188
1189 // Sort by label
1190 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1191 @Override
1192 public int compare(DashboardTile t1, DashboardTile t2) {
1193 return t1.title.toString().compareTo(t2.title.toString());
1194 }
1195 });
1196 int index = 0;
1197 for (DashboardTile tile : dashboardTiles) {
1198 target.addTile(index, tile);
1199 index++;
1200 }
1201 if (!mListeningToAccountUpdates) {
1202 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1203 mListeningToAccountUpdates = true;
1204 }
1205 }
1206
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001207 private void getMetaData() {
1208 try {
1209 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1210 PackageManager.GET_META_DATA);
1211 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001212 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1213 } catch (NameNotFoundException nnfe) {
1214 // No recovery
1215 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1216 }
1217 }
1218
1219 // give subclasses access to the Next button
1220 public boolean hasNextButton() {
1221 return mNextButton != null;
1222 }
1223
1224 public Button getNextButton() {
1225 return mNextButton;
1226 }
1227
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001228 @Override
1229 public boolean shouldUpRecreateTask(Intent targetIntent) {
1230 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1231 }
1232
1233 @Override
1234 public void onAccountsUpdated(Account[] accounts) {
1235 // TODO: watch for package upgrades to invalidate cache; see 7206643
1236 mAuthenticatorHelper.updateAuthDescriptions(this);
1237 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001238 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001239 }
1240
1241 public static void requestHomeNotice() {
1242 sShowNoHomeNotice = true;
1243 }
1244
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001245 @Override
1246 public boolean onQueryTextSubmit(String query) {
1247 switchToSearchResultsFragmentIfNeeded();
1248 mSearchQuery = query;
1249 return mSearchResultsFragment.onQueryTextSubmit(query);
1250 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001251
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001252 @Override
1253 public boolean onQueryTextChange(String newText) {
1254 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001255 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001256 return false;
1257 }
1258 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001259 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001260
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001261 @Override
1262 public boolean onClose() {
1263 return false;
1264 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001265
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001266 @Override
1267 public boolean onMenuItemActionExpand(MenuItem item) {
1268 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001269 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001270 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001271 return true;
1272 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001273
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001274 @Override
1275 public boolean onMenuItemActionCollapse(MenuItem item) {
1276 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001277 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001278 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001279 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001280 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001281 return true;
1282 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001283
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001284 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001285 if (mSearchResultsFragment != null) {
1286 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001287 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001288 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1289 if (current != null && current instanceof SearchResultsSummary) {
1290 mSearchResultsFragment = (SearchResultsSummary) current;
1291 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001292 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001293 SearchResultsSummary.class.getName(), null, false, true,
1294 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001295 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001296 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001297 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001298 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001299
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001300 public void needToRevertToInitialFragment() {
1301 mNeedToRevertToInitialFragment = true;
1302 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001303
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001304 private void revertToInitialFragment() {
1305 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001306 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001307 mSearchMenuItemExpanded = false;
1308 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1309 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001310 if (mSearchMenuItem != null) {
1311 mSearchMenuItem.collapseActionView();
1312 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001313 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001314}