blob: 18bab2c074d021caadabfc51fe9b151cd1c3719c [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 Meglio263bcc82014-01-17 19:17:58 -0800136
137 /**
138 * When starting this activity, the invoking Intent can contain this extra
139 * string to specify which fragment should be initially displayed.
140 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
141 * will call isValidFragment() to confirm that the fragment class name is valid for this
142 * activity.
143 */
144 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
145
146 /**
147 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
148 * this extra can also be specified to supply a Bundle of arguments to pass
149 * to that fragment when it is instantiated during the initial creation
150 * of the activity.
151 */
152 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
153
154 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700155 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
156 */
157 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
158
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800159 public static final String BACK_STACK_PREFS = ":settings:prefs";
160
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161 // extras that allow any preference activity to be launched as part of a wizard
162
163 // show Back and Next buttons? takes boolean parameter
164 // Back will then return RESULT_CANCELED and Next RESULT_OK
165 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
166
167 // add a Skip button?
168 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
169
170 // specify custom text for the Back or Next buttons, or cause a button to not appear
171 // at all by setting it to null
172 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
173 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
174
175 /**
176 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
177 * this extra can also be specify to supply the title to be shown for
178 * that fragment.
179 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700180 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800181
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 private static final String META_DATA_KEY_FRAGMENT_CLASS =
183 "com.android.settings.FRAGMENT_CLASS";
184
185 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
186
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700187 private static final String EMPTY_QUERY = "";
188
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800189 private static boolean sShowNoHomeNotice = false;
190
191 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800192
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800193 private CharSequence mInitialTitle;
194
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 // Show only these settings for restricted users
196 private int[] SETTINGS_FOR_RESTRICTED = {
197 R.id.wireless_section,
198 R.id.wifi_settings,
199 R.id.bluetooth_settings,
200 R.id.data_usage_settings,
201 R.id.wireless_settings,
202 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400203 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204 R.id.display_settings,
205 R.id.storage_settings,
206 R.id.application_settings,
207 R.id.battery_settings,
208 R.id.personal_section,
209 R.id.location_settings,
210 R.id.security_settings,
211 R.id.language_settings,
212 R.id.user_settings,
213 R.id.account_settings,
214 R.id.account_add,
215 R.id.system_section,
216 R.id.date_time_settings,
217 R.id.about_settings,
218 R.id.accessibility_settings,
219 R.id.print_settings,
220 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800221 R.id.home_settings,
222 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800223 };
224
225 private static final String[] ENTRY_FRAGMENTS = {
226 WirelessSettings.class.getName(),
227 WifiSettings.class.getName(),
228 AdvancedWifiSettings.class.getName(),
229 BluetoothSettings.class.getName(),
230 TetherSettings.class.getName(),
231 WifiP2pSettings.class.getName(),
232 VpnSettings.class.getName(),
233 DateTimeSettings.class.getName(),
234 LocalePicker.class.getName(),
235 InputMethodAndLanguageSettings.class.getName(),
236 SpellCheckersSettings.class.getName(),
237 UserDictionaryList.class.getName(),
238 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800239 DisplaySettings.class.getName(),
240 DeviceInfoSettings.class.getName(),
241 ManageApplications.class.getName(),
242 ProcessStatsUi.class.getName(),
243 NotificationStation.class.getName(),
244 LocationSettings.class.getName(),
245 SecuritySettings.class.getName(),
246 PrivacySettings.class.getName(),
247 DeviceAdminSettings.class.getName(),
248 AccessibilitySettings.class.getName(),
249 CaptionPropertiesFragment.class.getName(),
250 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
252 TextToSpeechSettings.class.getName(),
253 Memory.class.getName(),
254 DevelopmentSettings.class.getName(),
255 UsbSettings.class.getName(),
256 AndroidBeam.class.getName(),
257 WifiDisplaySettings.class.getName(),
258 PowerUsageSummary.class.getName(),
259 AccountSyncSettings.class.getName(),
260 CryptKeeperSettings.class.getName(),
261 DataUsageSummary.class.getName(),
262 DreamSettings.class.getName(),
263 UserSettings.class.getName(),
264 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400265 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800266 ManageAccountsSettings.class.getName(),
267 PrintSettingsFragment.class.getName(),
268 PrintJobSettingsFragment.class.getName(),
269 TrustedCredentialsSettings.class.getName(),
270 PaymentSettings.class.getName(),
271 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700272 ZenModeSettings.class.getName(),
273 NotificationSettings.class.getName(),
274 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
275 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
276 InstalledAppDetails.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800277 };
278
279 private SharedPreferences mDevelopmentPreferences;
280 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
281
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 private AuthenticatorHelper mAuthenticatorHelper;
283 private boolean mListeningToAccountUpdates;
284
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 private boolean mBatteryPresent = true;
286 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
287
288 @Override
289 public void onReceive(Context context, Intent intent) {
290 String action = intent.getAction();
291 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
292 boolean batteryPresent = Utils.isBatteryPresent(intent);
293
294 if (mBatteryPresent != batteryPresent) {
295 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700296 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 }
298 }
299 }
300 };
301
Svetoslav990159a2014-04-14 17:14:59 -0700302 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
303 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700304
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700305 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700306 private SwitchBar mSwitchBar;
307
308 private Button mNextButton;
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700309 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700310
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700311 private boolean mIsShowingDashboard;
312
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700313 private ViewGroup mContent;
314
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700315 private SearchView mSearchView;
316 private MenuItem mSearchMenuItem;
317 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700318 private SearchResultsSummary mSearchResultsFragment;
319 private String mSearchQuery;
320
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700321 // Categories
322 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
323 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700325 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 private Handler mHandler = new Handler() {
327 @Override
328 public void handleMessage(Message msg) {
329 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700330 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700331 if(mNeedToRebuildCategories) {
332 buildDashboardCategories(mCategories);
333 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800334 } break;
335 }
336 }
337 };
338
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700339 private boolean mNeedToRevertToInitialFragment = false;
340
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700341 public SwitchBar getSwitchBar() {
342 return mSwitchBar;
343 }
344
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700345 public AuthenticatorHelper getAuthenticatorHelper() {
346 return mAuthenticatorHelper;
347 }
348
349 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700350 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700351 buildDashboardCategories(mCategories);
352 mNeedToRebuildCategories = false;
353 }
354 return mCategories;
355 }
356
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 @Override
358 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
359 // Override the fragment title for Wallpaper settings
360 int titleRes = pref.getTitleRes();
361 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
362 titleRes = R.string.wallpaper_settings_fragment_title;
363 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
364 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
365 if (UserManager.get(this).isLinkedUser()) {
366 titleRes = R.string.profile_info_settings_title;
367 } else {
368 titleRes = R.string.user_info_settings_title;
369 }
370 }
371 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
372 null, 0);
373 return true;
374 }
375
376 @Override
377 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
378 return false;
379 }
380
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700381 private void invalidateCategories() {
382 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
383 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800384 }
385 }
386
387 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800388 public void onConfigurationChanged(Configuration newConfig) {
389 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800390 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800391 }
392
393 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700394 protected void onStart() {
395 super.onStart();
396
397 if (mNeedToRevertToInitialFragment) {
398 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800399 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800400 }
401
402 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700403 public boolean onCreateOptionsMenu(Menu menu) {
404 MenuInflater inflater = getMenuInflater();
405 inflater.inflate(R.menu.options_menu, menu);
406
407 // Cache the search query (can be overriden by the OnQueryTextListener)
408 final String query = mSearchQuery;
409
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700410 mSearchMenuItem = menu.findItem(R.id.search);
411 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700412
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700413 if (mSearchMenuItem == null || mSearchView == null) {
414 return false;
415 }
416
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700417 if (mSearchResultsFragment != null) {
418 mSearchResultsFragment.setSearchView(mSearchView);
419 }
420
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700421 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700422 mSearchView.setOnQueryTextListener(this);
423 mSearchView.setOnCloseListener(this);
424
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700425 if (mSearchMenuItemExpanded) {
426 mSearchMenuItem.expandActionView();
427 }
428 mSearchView.setQuery(query, true /* submit */);
429
430 return true;
431 }
432
433 @Override
434 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800435 if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
436 getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
437 }
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700438
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800439 mAuthenticatorHelper = new AuthenticatorHelper();
440 mAuthenticatorHelper.updateAuthDescriptions(this);
441 mAuthenticatorHelper.onAccountsUpdated(this, null);
442
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800443 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
444 Context.MODE_PRIVATE);
445
446 getMetaData();
447
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700448 super.onCreate(savedState);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800449
450 setContentView(R.layout.settings_main);
451
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700452 mContent = (ViewGroup) findViewById(R.id.prefs);
453
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800454 getFragmentManager().addOnBackStackChangedListener(this);
455
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700456 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800457
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700458 // Getting Intent properties can only be done after the super.onCreate(...)
459 final String initialFragmentName = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
460
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700461 mIsShowingDashboard = (initialFragmentName == null);
462
463 if (mIsShowingDashboard) {
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700464 Index.getInstance(this).update();
465 }
466
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700467 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700468 // We are restarting from a previous saved state; used that to initialize, instead
469 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700470 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
471 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800472
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700473 final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
474 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
475 setTitle(mInitialTitle);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800476
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700477 ArrayList<DashboardCategory> categories =
478 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
479 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700480 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700481 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700482 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800483 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700484
485 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800486 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700487 if (!mIsShowingDashboard) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700488 final ComponentName cn = getIntent().getComponent();
489 // No UP is we are launched thru a Settings shortcut
490 if (!cn.getClassName().equals(SubSettings.class.getName())) {
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700491 mDisplayHomeAsUpEnabled = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700492 }
493 final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
494 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
Fabrice Di Meglio832e5462014-03-06 19:12:14 -0800495 setTitle(mInitialTitle);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700496
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700497 Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700498 switchToFragment( initialFragmentName, initialArguments, true, false,
499 mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000500 } else {
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700501 // No UP if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700502 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700503 mInitialTitle = getText(R.string.dashboard_title);
504 switchToFragment(DashboardSummary.class.getName(), null, false, false,
505 mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800506 }
507 }
508
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700509 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700510 if (mActionBar != null) {
511 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
512 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
513 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700514 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
515
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516 // see if we should show Back/Next buttons
517 Intent intent = getIntent();
518 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
519
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700520 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800521 if (buttonBar != null) {
522 buttonBar.setVisibility(View.VISIBLE);
523
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700524 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800525 backButton.setOnClickListener(new OnClickListener() {
526 public void onClick(View v) {
527 setResult(RESULT_CANCELED);
528 finish();
529 }
530 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700531 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532 skipButton.setOnClickListener(new OnClickListener() {
533 public void onClick(View v) {
534 setResult(RESULT_OK);
535 finish();
536 }
537 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700538 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 mNextButton.setOnClickListener(new OnClickListener() {
540 public void onClick(View v) {
541 setResult(RESULT_OK);
542 finish();
543 }
544 });
545
546 // set our various button parameters
547 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
548 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
549 if (TextUtils.isEmpty(buttonText)) {
550 mNextButton.setVisibility(View.GONE);
551 }
552 else {
553 mNextButton.setText(buttonText);
554 }
555 }
556 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
557 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
558 if (TextUtils.isEmpty(buttonText)) {
559 backButton.setVisibility(View.GONE);
560 }
561 else {
562 backButton.setText(buttonText);
563 }
564 }
565 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
566 skipButton.setVisibility(View.VISIBLE);
567 }
568 }
569 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800570 }
571
572 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700574 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 }
576
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700577 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800578 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700579
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800580 if (count == 0) {
581 setTitle(mInitialTitle);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700582 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800583 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700584
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800585 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
586 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700587
588 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800589 }
590
591 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
592 final CharSequence title;
593 final int titleRes = bse.getBreadCrumbTitleRes();
594 if (titleRes > 0) {
595 title = getText(titleRes);
596 } else {
597 title = bse.getBreadCrumbTitle();
598 }
599 if (title != null) {
600 setTitle(title);
601 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800602 }
603
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 @Override
605 protected void onSaveInstanceState(Bundle outState) {
606 super.onSaveInstanceState(outState);
607
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700608 if (mCategories.size() > 0) {
609 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800610 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700611
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700612 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
613
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700614 // The option menus are created if the ActionBar is visible and they are also created
615 // asynchronously. If you launch Settings with an Intent action like
616 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
617 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
618 // menu item and search view are null.
619 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
620 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
621
622 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
623 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800624 }
625
626 @Override
627 public void onResume() {
628 super.onResume();
629
630 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
631 @Override
632 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700633 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800634 }
635 };
636 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
637 mDevelopmentPreferencesListener);
638
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800639 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700640
Svetoslav990159a2014-04-14 17:14:59 -0700641 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700642
643 if(!TextUtils.isEmpty(mSearchQuery)) {
644 onQueryTextSubmit(mSearchQuery);
645 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 }
647
648 @Override
649 public void onPause() {
650 super.onPause();
651
652 unregisterReceiver(mBatteryInfoReceiver);
653
Svetoslav990159a2014-04-14 17:14:59 -0700654 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 }
656
657 @Override
658 public void onDestroy() {
659 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700660
661 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
662 mDevelopmentPreferencesListener);
663 mDevelopmentPreferencesListener = null;
664
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800665 if (mListeningToAccountUpdates) {
666 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
667 }
668 }
669
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800670 protected boolean isValidFragment(String fragmentName) {
671 // Almost all fragments are wrapped in this,
672 // except for a few that have their own activities.
673 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
674 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
675 }
676 return false;
677 }
678
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800679 @Override
680 public Intent getIntent() {
681 Intent superIntent = super.getIntent();
682 String startingFragment = getStartingFragmentClass(superIntent);
683 // This is called from super.onCreate, isMultiPane() is not yet reliable
684 // Do not use onIsHidingHeaders either, which relies itself on this method
685 if (startingFragment != null) {
686 Intent modIntent = new Intent(superIntent);
687 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
688 Bundle args = superIntent.getExtras();
689 if (args != null) {
690 args = new Bundle(args);
691 } else {
692 args = new Bundle();
693 }
694 args.putParcelable("intent", superIntent);
695 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
696 return modIntent;
697 }
698 return superIntent;
699 }
700
701 /**
702 * Checks if the component name in the intent is different from the Settings class and
703 * returns the class name to load as a fragment.
704 */
705 private String getStartingFragmentClass(Intent intent) {
706 if (mFragmentClass != null) return mFragmentClass;
707
708 String intentClass = intent.getComponent().getClassName();
709 if (intentClass.equals(getClass().getName())) return null;
710
711 if ("com.android.settings.ManageApplications".equals(intentClass)
712 || "com.android.settings.RunningServices".equals(intentClass)
713 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
714 // Old names of manage apps.
715 intentClass = com.android.settings.applications.ManageApplications.class.getName();
716 }
717
718 return intentClass;
719 }
720
721 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000722 * Start a new fragment containing a preference panel. If the preferences
723 * are being displayed in multi-pane mode, the given fragment class will
724 * be instantiated and placed in the appropriate pane. If running in
725 * single-pane mode, a new activity will be launched in which to show the
726 * fragment.
727 *
728 * @param fragmentClass Full name of the class implementing the fragment.
729 * @param args Any desired arguments to supply to the fragment.
730 * @param titleRes Optional resource identifier of the title of this
731 * fragment.
732 * @param titleText Optional text of the title of this fragment.
733 * @param resultTo Optional fragment that result data should be sent to.
734 * If non-null, resultTo.onActivityResult() will be called when this
735 * preference panel is done. The launched panel must use
736 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
737 * @param resultRequestCode If resultTo is non-null, this is the caller's
738 * request code to be received with the resut.
739 */
740 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700741 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700742 String title;
743 if (titleRes > 0) {
744 title = getString(titleRes);
Fabrice Di Meglio821a0722014-04-08 13:56:06 -0700745 } else if (titleText != null) {
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700746 title = titleText.toString();
Fabrice Di Meglio821a0722014-04-08 13:56:06 -0700747 } else {
748 // There not much we can do in that case
749 title = "";
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700750 }
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700751 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode, title);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000752 }
753
754 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800755 * Called by a preference panel fragment to finish itself.
756 *
757 * @param caller The fragment that is asking to be finished.
758 * @param resultCode Optional result code to send back to the original
759 * launching fragment.
760 * @param resultData Optional result data to send back to the original
761 * launching fragment.
762 */
763 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
764 setResult(resultCode, resultData);
765 }
766
767 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000768 * Start a new fragment.
769 *
770 * @param fragment The fragment to start
771 * @param push If true, the current fragment will be pushed onto the back stack. If false,
772 * the current fragment will be replaced.
773 */
774 public void startPreferenceFragment(Fragment fragment, boolean push) {
775 FragmentTransaction transaction = getFragmentManager().beginTransaction();
776 transaction.replace(R.id.prefs, fragment);
777 if (push) {
778 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
779 transaction.addToBackStack(BACK_STACK_PREFS);
780 } else {
781 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
782 }
783 transaction.commitAllowingStateLoss();
784 }
785
786 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700787 * Switch to a specific Fragment with taking care of validation, Title and BackStack
788 */
789 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
790 boolean addToBackStack, CharSequence title, boolean withTransition) {
791 if (validate && !isValidFragment(fragmentName)) {
792 throw new IllegalArgumentException("Invalid fragment for this activity: "
793 + fragmentName);
794 }
795 Fragment f = Fragment.instantiate(this, fragmentName, args);
796 FragmentTransaction transaction = getFragmentManager().beginTransaction();
797 transaction.replace(R.id.prefs, f);
798 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700799 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700800 }
801 if (addToBackStack) {
802 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
803 }
804 if (title != null) {
805 transaction.setBreadCrumbTitle(title);
806 }
807 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700808 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700809 return f;
810 }
811
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700812 public void setNeedToRebuildCategories(boolean need) {
813 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700814 }
815
816 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700817 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700818 *
819 * @param categories The list in which to place the tiles categories.
820 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700821 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700822 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700823 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
824 updateTilesList(categories);
825 }
826
827 /**
828 * Parse the given XML file as a categories description, adding each
829 * parsed categories and tiles into the target list.
830 *
831 * @param resid The XML resource to load and parse.
832 * @param target The list in which the parsed categories and tiles should be placed.
833 */
834 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
835 XmlResourceParser parser = null;
836 try {
837 parser = getResources().getXml(resid);
838 AttributeSet attrs = Xml.asAttributeSet(parser);
839
840 int type;
841 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
842 && type != XmlPullParser.START_TAG) {
843 // Parse next until start tag is found
844 }
845
846 String nodeName = parser.getName();
847 if (!"dashboard-categories".equals(nodeName)) {
848 throw new RuntimeException(
849 "XML document must start with <preference-categories> tag; found"
850 + nodeName + " at " + parser.getPositionDescription());
851 }
852
853 Bundle curBundle = null;
854
855 final int outerDepth = parser.getDepth();
856 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
857 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
858 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
859 continue;
860 }
861
862 nodeName = parser.getName();
863 if ("dashboard-category".equals(nodeName)) {
864 DashboardCategory category = new DashboardCategory();
865
866 TypedArray sa = obtainStyledAttributes(
867 attrs, com.android.internal.R.styleable.PreferenceHeader);
868 category.id = sa.getResourceId(
869 com.android.internal.R.styleable.PreferenceHeader_id,
870 (int)DashboardCategory.CAT_ID_UNDEFINED);
871
872 TypedValue tv = sa.peekValue(
873 com.android.internal.R.styleable.PreferenceHeader_title);
874 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
875 if (tv.resourceId != 0) {
876 category.titleRes = tv.resourceId;
877 } else {
878 category.title = tv.string;
879 }
880 }
881 sa.recycle();
882
883 final int innerDepth = parser.getDepth();
884 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
885 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
886 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
887 continue;
888 }
889
890 String innerNodeName = parser.getName();
891 if (innerNodeName.equals("dashboard-tile")) {
892 DashboardTile tile = new DashboardTile();
893
894 sa = obtainStyledAttributes(
895 attrs, com.android.internal.R.styleable.PreferenceHeader);
896 tile.id = sa.getResourceId(
897 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700898 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700899 tv = sa.peekValue(
900 com.android.internal.R.styleable.PreferenceHeader_title);
901 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
902 if (tv.resourceId != 0) {
903 tile.titleRes = tv.resourceId;
904 } else {
905 tile.title = tv.string;
906 }
907 }
908 tv = sa.peekValue(
909 com.android.internal.R.styleable.PreferenceHeader_summary);
910 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
911 if (tv.resourceId != 0) {
912 tile.summaryRes = tv.resourceId;
913 } else {
914 tile.summary = tv.string;
915 }
916 }
917 tile.iconRes = sa.getResourceId(
918 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
919 tile.fragment = sa.getString(
920 com.android.internal.R.styleable.PreferenceHeader_fragment);
921 sa.recycle();
922
923 if (curBundle == null) {
924 curBundle = new Bundle();
925 }
926
927 final int innerDepth2 = parser.getDepth();
928 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
929 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
930 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
931 continue;
932 }
933
934 String innerNodeName2 = parser.getName();
935 if (innerNodeName2.equals("extra")) {
936 getResources().parseBundleExtra("extra", attrs, curBundle);
937 XmlUtils.skipCurrentTag(parser);
938
939 } else if (innerNodeName2.equals("intent")) {
940 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
941
942 } else {
943 XmlUtils.skipCurrentTag(parser);
944 }
945 }
946
947 if (curBundle.size() > 0) {
948 tile.fragmentArguments = curBundle;
949 curBundle = null;
950 }
951
952 category.addTile(tile);
953
954 } else {
955 XmlUtils.skipCurrentTag(parser);
956 }
957 }
958
959 target.add(category);
960 } else {
961 XmlUtils.skipCurrentTag(parser);
962 }
963 }
964
965 } catch (XmlPullParserException e) {
966 throw new RuntimeException("Error parsing categories", e);
967 } catch (IOException e) {
968 throw new RuntimeException("Error parsing categories", e);
969 } finally {
970 if (parser != null) parser.close();
971 }
972 }
973
974 private void updateTilesList(List<DashboardCategory> target) {
975 final boolean showDev = mDevelopmentPreferences.getBoolean(
976 DevelopmentSettings.PREF_SHOW,
977 android.os.Build.TYPE.equals("eng"));
978
979 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
980
981 final int size = target.size();
982 for (int i = 0; i < size; i++) {
983
984 DashboardCategory category = target.get(i);
985
986 // Ids are integers, so downcasting is ok
987 int id = (int) category.id;
988 if (id == R.id.account_settings) {
989 insertAccountsTiles(category);
990 continue;
991 }
992 int n = category.getTilesCount() - 1;
993 while (n >= 0) {
994
995 DashboardTile tile = category.getTile(n);
996
997 id = (int) tile.id;
998 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
999 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1000 } else if (id == R.id.wifi_settings) {
1001 // Remove WiFi Settings if WiFi service is not available.
1002 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1003 category.removeTile(n);
1004 }
1005 } else if (id == R.id.bluetooth_settings) {
1006 // Remove Bluetooth Settings if Bluetooth service is not available.
1007 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1008 category.removeTile(n);
1009 }
1010 } else if (id == R.id.data_usage_settings) {
1011 // Remove data usage when kernel module not enabled
1012 final INetworkManagementService netManager = INetworkManagementService.Stub
1013 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1014 try {
1015 if (!netManager.isBandwidthControlEnabled()) {
1016 category.removeTile(n);
1017 }
1018 } catch (RemoteException e) {
1019 // ignored
1020 }
1021 } else if (id == R.id.battery_settings) {
1022 // Remove battery settings when battery is not available. (e.g. TV)
1023
1024 if (!mBatteryPresent) {
1025 category.removeTile(n);
1026 }
1027 } else if (id == R.id.home_settings) {
1028 if (!updateHomeSettingTiles(tile)) {
1029 category.removeTile(n);
1030 }
1031 } else if (id == R.id.user_settings) {
1032 if (!UserHandle.MU_ENABLED
1033 || !UserManager.supportsMultipleUsers()
1034 || Utils.isMonkeyRunning()) {
1035 category.removeTile(n);
1036 }
1037 } else if (id == R.id.nfc_payment_settings) {
1038 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1039 category.removeTile(n);
1040 } else {
1041 // Only show if NFC is on and we have the HCE feature
1042 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1043 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1044 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1045 category.removeTile(n);
1046 }
1047 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001048 } else if (id == R.id.print_settings) {
1049 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1050 PackageManager.FEATURE_PRINTING);
1051 if (!hasPrintingSupport) {
1052 category.removeTile(n);
1053 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001054 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001055 if (!showDev || um.hasUserRestriction(
1056 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001057 category.removeTile(n);
1058 }
1059 } else if (id == R.id.account_add) {
1060 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1061 category.removeTile(n);
1062 }
1063 }
1064
1065 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001066 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1067 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001068 category.removeTile(n);
1069 }
1070
1071 n--;
1072 }
1073 }
1074 }
1075
1076 private boolean updateHomeSettingTiles(DashboardTile tile) {
1077 // Once we decide to show Home settings, keep showing it forever
1078 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1079 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1080 return true;
1081 }
1082
1083 try {
1084 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1085 getPackageManager().getHomeActivities(homeApps);
1086 if (homeApps.size() < 2) {
1087 // When there's only one available home app, omit this settings
1088 // category entirely at the top level UI. If the user just
1089 // uninstalled the penultimate home app candidiate, we also
1090 // now tell them about why they aren't seeing 'Home' in the list.
1091 if (sShowNoHomeNotice) {
1092 sShowNoHomeNotice = false;
1093 NoHomeDialogFragment.show(this);
1094 }
1095 return false;
1096 } else {
1097 // Okay, we're allowing the Home settings category. Tell it, when
1098 // invoked via this front door, that we'll need to be told about the
1099 // case when the user uninstalls all but one home app.
1100 if (tile.fragmentArguments == null) {
1101 tile.fragmentArguments = new Bundle();
1102 }
1103 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1104 }
1105 } catch (Exception e) {
1106 // Can't look up the home activity; bail on configuring the icon
1107 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1108 }
1109
1110 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1111 return true;
1112 }
1113
1114 private void insertAccountsTiles(DashboardCategory target) {
1115 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1116 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1117 for (String accountType : accountTypes) {
1118 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1119 if (label == null) {
1120 continue;
1121 }
1122
1123 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1124 boolean skipToAccount = accounts.length == 1
1125 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1126 DashboardTile accountTile = new DashboardTile();
1127 accountTile.title = label;
1128 if (accountTile.extras == null) {
1129 accountTile.extras = new Bundle();
1130 }
1131 if (skipToAccount) {
1132 accountTile.fragment = AccountSyncSettings.class.getName();
1133 accountTile.fragmentArguments = new Bundle();
1134 // Need this for the icon
1135 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1136 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1137 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1138 accounts[0]);
1139 } else {
1140 accountTile.fragment = ManageAccountsSettings.class.getName();
1141 accountTile.fragmentArguments = new Bundle();
1142 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1143 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1144 accountType);
1145 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1146 label.toString());
1147 }
1148 dashboardTiles.add(accountTile);
1149 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1150 }
1151
1152 // Sort by label
1153 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1154 @Override
1155 public int compare(DashboardTile t1, DashboardTile t2) {
1156 return t1.title.toString().compareTo(t2.title.toString());
1157 }
1158 });
1159 int index = 0;
1160 for (DashboardTile tile : dashboardTiles) {
1161 target.addTile(index, tile);
1162 index++;
1163 }
1164 if (!mListeningToAccountUpdates) {
1165 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1166 mListeningToAccountUpdates = true;
1167 }
1168 }
1169
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001170 private void getMetaData() {
1171 try {
1172 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1173 PackageManager.GET_META_DATA);
1174 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001175 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1176 } catch (NameNotFoundException nnfe) {
1177 // No recovery
1178 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1179 }
1180 }
1181
1182 // give subclasses access to the Next button
1183 public boolean hasNextButton() {
1184 return mNextButton != null;
1185 }
1186
1187 public Button getNextButton() {
1188 return mNextButton;
1189 }
1190
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001191 @Override
1192 public boolean shouldUpRecreateTask(Intent targetIntent) {
1193 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1194 }
1195
1196 @Override
1197 public void onAccountsUpdated(Account[] accounts) {
1198 // TODO: watch for package upgrades to invalidate cache; see 7206643
1199 mAuthenticatorHelper.updateAuthDescriptions(this);
1200 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001201 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001202 }
1203
1204 public static void requestHomeNotice() {
1205 sShowNoHomeNotice = true;
1206 }
1207
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001208 @Override
1209 public boolean onQueryTextSubmit(String query) {
1210 switchToSearchResultsFragmentIfNeeded();
1211 mSearchQuery = query;
1212 return mSearchResultsFragment.onQueryTextSubmit(query);
1213 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001214
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001215 @Override
1216 public boolean onQueryTextChange(String newText) {
1217 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001218 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001219 return false;
1220 }
1221 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001222 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001223
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001224 @Override
1225 public boolean onClose() {
1226 return false;
1227 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001228
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001229 @Override
1230 public boolean onMenuItemActionExpand(MenuItem item) {
1231 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001232 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001233 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001234 return true;
1235 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001236
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001237 @Override
1238 public boolean onMenuItemActionCollapse(MenuItem item) {
1239 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001240 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001241 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001242 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001243 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001244 return true;
1245 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001246
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001247 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001248 if (mSearchResultsFragment != null) {
1249 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001250 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001251 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1252 if (current != null && current instanceof SearchResultsSummary) {
1253 mSearchResultsFragment = (SearchResultsSummary) current;
1254 } else {
1255 String title = getString(R.string.search_results_title);
1256 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglio41937762014-05-13 19:51:59 -07001257 SearchResultsSummary.class.getName(), null, false, true, title,
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001258 true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001259 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001260 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001261 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001262 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001263
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001264 public void needToRevertToInitialFragment() {
1265 mNeedToRevertToInitialFragment = true;
1266 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001267
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001268 private void revertToInitialFragment() {
1269 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001270 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001271 mSearchMenuItemExpanded = false;
1272 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1273 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001274 if (mSearchMenuItem != null) {
1275 mSearchMenuItem.collapseActionView();
1276 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001277 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001278}