blob: 7dcdfbc7ad9bb1eaab53f3d570f8e5f01f75dc32 [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},
178 * this extra can also be specify to supply the title to be shown for
179 * 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 Meglio263bcc82014-01-17 19:17:58 -0800182
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800183 private static final String META_DATA_KEY_FRAGMENT_CLASS =
184 "com.android.settings.FRAGMENT_CLASS";
185
186 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
187
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700188 private static final String EMPTY_QUERY = "";
189
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800190 private static boolean sShowNoHomeNotice = false;
191
192 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800193
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800194 private CharSequence mInitialTitle;
195
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196 // Show only these settings for restricted users
197 private int[] SETTINGS_FOR_RESTRICTED = {
198 R.id.wireless_section,
199 R.id.wifi_settings,
200 R.id.bluetooth_settings,
201 R.id.data_usage_settings,
202 R.id.wireless_settings,
203 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400204 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205 R.id.display_settings,
206 R.id.storage_settings,
207 R.id.application_settings,
208 R.id.battery_settings,
209 R.id.personal_section,
210 R.id.location_settings,
211 R.id.security_settings,
212 R.id.language_settings,
213 R.id.user_settings,
214 R.id.account_settings,
215 R.id.account_add,
216 R.id.system_section,
217 R.id.date_time_settings,
218 R.id.about_settings,
219 R.id.accessibility_settings,
220 R.id.print_settings,
221 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800222 R.id.home_settings,
223 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 };
225
226 private static final String[] ENTRY_FRAGMENTS = {
227 WirelessSettings.class.getName(),
228 WifiSettings.class.getName(),
229 AdvancedWifiSettings.class.getName(),
230 BluetoothSettings.class.getName(),
231 TetherSettings.class.getName(),
232 WifiP2pSettings.class.getName(),
233 VpnSettings.class.getName(),
234 DateTimeSettings.class.getName(),
235 LocalePicker.class.getName(),
236 InputMethodAndLanguageSettings.class.getName(),
237 SpellCheckersSettings.class.getName(),
238 UserDictionaryList.class.getName(),
239 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 DisplaySettings.class.getName(),
241 DeviceInfoSettings.class.getName(),
242 ManageApplications.class.getName(),
243 ProcessStatsUi.class.getName(),
244 NotificationStation.class.getName(),
245 LocationSettings.class.getName(),
246 SecuritySettings.class.getName(),
247 PrivacySettings.class.getName(),
248 DeviceAdminSettings.class.getName(),
249 AccessibilitySettings.class.getName(),
250 CaptionPropertiesFragment.class.getName(),
251 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800252 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
253 TextToSpeechSettings.class.getName(),
254 Memory.class.getName(),
255 DevelopmentSettings.class.getName(),
256 UsbSettings.class.getName(),
257 AndroidBeam.class.getName(),
258 WifiDisplaySettings.class.getName(),
259 PowerUsageSummary.class.getName(),
260 AccountSyncSettings.class.getName(),
261 CryptKeeperSettings.class.getName(),
262 DataUsageSummary.class.getName(),
263 DreamSettings.class.getName(),
264 UserSettings.class.getName(),
265 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400266 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800267 ManageAccountsSettings.class.getName(),
268 PrintSettingsFragment.class.getName(),
269 PrintJobSettingsFragment.class.getName(),
270 TrustedCredentialsSettings.class.getName(),
271 PaymentSettings.class.getName(),
272 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700273 ZenModeSettings.class.getName(),
274 NotificationSettings.class.getName(),
275 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
276 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
277 InstalledAppDetails.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 };
279
280 private SharedPreferences mDevelopmentPreferences;
281 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
282
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800283 private AuthenticatorHelper mAuthenticatorHelper;
284 private boolean mListeningToAccountUpdates;
285
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 private boolean mBatteryPresent = true;
287 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
288
289 @Override
290 public void onReceive(Context context, Intent intent) {
291 String action = intent.getAction();
292 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
293 boolean batteryPresent = Utils.isBatteryPresent(intent);
294
295 if (mBatteryPresent != batteryPresent) {
296 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700297 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 }
299 }
300 }
301 };
302
Svetoslav990159a2014-04-14 17:14:59 -0700303 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
304 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700305
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700306 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700307 private SwitchBar mSwitchBar;
308
309 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700310
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700311 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700312 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700313
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700314 private boolean mIsShowingDashboard;
315
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700316 private ViewGroup mContent;
317
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700318 private SearchView mSearchView;
319 private MenuItem mSearchMenuItem;
320 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700321 private SearchResultsSummary mSearchResultsFragment;
322 private String mSearchQuery;
323
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700324 // Categories
325 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
326 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700328 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800329 private Handler mHandler = new Handler() {
330 @Override
331 public void handleMessage(Message msg) {
332 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700333 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700334 if(mNeedToRebuildCategories) {
335 buildDashboardCategories(mCategories);
336 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800337 } break;
338 }
339 }
340 };
341
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700342 private boolean mNeedToRevertToInitialFragment = false;
343
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700344 public SwitchBar getSwitchBar() {
345 return mSwitchBar;
346 }
347
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700348 public AuthenticatorHelper getAuthenticatorHelper() {
349 return mAuthenticatorHelper;
350 }
351
352 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700353 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700354 buildDashboardCategories(mCategories);
355 mNeedToRebuildCategories = false;
356 }
357 return mCategories;
358 }
359
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 @Override
361 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
362 // Override the fragment title for Wallpaper settings
363 int titleRes = pref.getTitleRes();
364 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
365 titleRes = R.string.wallpaper_settings_fragment_title;
366 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
367 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
368 if (UserManager.get(this).isLinkedUser()) {
369 titleRes = R.string.profile_info_settings_title;
370 } else {
371 titleRes = R.string.user_info_settings_title;
372 }
373 }
374 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
375 null, 0);
376 return true;
377 }
378
379 @Override
380 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
381 return false;
382 }
383
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700384 private void invalidateCategories() {
385 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
386 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800387 }
388 }
389
390 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800391 public void onConfigurationChanged(Configuration newConfig) {
392 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800393 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800394 }
395
396 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700397 protected void onStart() {
398 super.onStart();
399
400 if (mNeedToRevertToInitialFragment) {
401 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800403 }
404
405 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700406 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700407 if (!mDisplaySearch) {
408 return false;
409 }
410
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700411 MenuInflater inflater = getMenuInflater();
412 inflater.inflate(R.menu.options_menu, menu);
413
414 // Cache the search query (can be overriden by the OnQueryTextListener)
415 final String query = mSearchQuery;
416
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700417 mSearchMenuItem = menu.findItem(R.id.search);
418 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700419
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700420 if (mSearchMenuItem == null || mSearchView == null) {
421 return false;
422 }
423
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700424 if (mSearchResultsFragment != null) {
425 mSearchResultsFragment.setSearchView(mSearchView);
426 }
427
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700428 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700429 mSearchView.setOnQueryTextListener(this);
430 mSearchView.setOnCloseListener(this);
431
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700432 if (mSearchMenuItemExpanded) {
433 mSearchMenuItem.expandActionView();
434 }
435 mSearchView.setQuery(query, true /* submit */);
436
437 return true;
438 }
439
440 @Override
441 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800442 if (getIntent().hasExtra(EXTRA_UI_OPTIONS)) {
443 getWindow().setUiOptions(getIntent().getIntExtra(EXTRA_UI_OPTIONS, 0));
444 }
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700445
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800446 mAuthenticatorHelper = new AuthenticatorHelper();
447 mAuthenticatorHelper.updateAuthDescriptions(this);
448 mAuthenticatorHelper.onAccountsUpdated(this, null);
449
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800450 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
451 Context.MODE_PRIVATE);
452
453 getMetaData();
454
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700455 super.onCreate(savedState);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800456
457 setContentView(R.layout.settings_main);
458
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700459 mContent = (ViewGroup) findViewById(R.id.prefs);
460
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800461 getFragmentManager().addOnBackStackChangedListener(this);
462
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700463 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700464 mDisplaySearch = true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700466 // Getting Intent properties can only be done after the super.onCreate(...)
467 final String initialFragmentName = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
468
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700469 mIsShowingDashboard = (initialFragmentName == null);
470
471 if (mIsShowingDashboard) {
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700472 Index.getInstance(this).update();
473 }
474
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700475 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700476 // We are restarting from a previous saved state; used that to initialize, instead
477 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700478 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
479 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800480
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700481 final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
482 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
483 setTitle(mInitialTitle);
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 }
503 final String initialTitle = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
504 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
Fabrice Di Meglio832e5462014-03-06 19:12:14 -0800505 setTitle(mInitialTitle);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700506
Fabrice Di Meglio5ebabfc2014-04-21 09:40:46 -0700507 Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700508 switchToFragment( initialFragmentName, initialArguments, true, false,
509 mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000510 } else {
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700511 // No UP if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700512 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700513 mInitialTitle = getText(R.string.dashboard_title);
514 switchToFragment(DashboardSummary.class.getName(), null, false, false,
515 mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516 }
517 }
518
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700519 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700520 if (mActionBar != null) {
521 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
522 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
523 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700524 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
525
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526 // see if we should show Back/Next buttons
527 Intent intent = getIntent();
528 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
529
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700530 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531 if (buttonBar != null) {
532 buttonBar.setVisibility(View.VISIBLE);
533
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700534 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 backButton.setOnClickListener(new OnClickListener() {
536 public void onClick(View v) {
537 setResult(RESULT_CANCELED);
538 finish();
539 }
540 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700541 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 skipButton.setOnClickListener(new OnClickListener() {
543 public void onClick(View v) {
544 setResult(RESULT_OK);
545 finish();
546 }
547 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700548 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549 mNextButton.setOnClickListener(new OnClickListener() {
550 public void onClick(View v) {
551 setResult(RESULT_OK);
552 finish();
553 }
554 });
555
556 // set our various button parameters
557 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
558 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
559 if (TextUtils.isEmpty(buttonText)) {
560 mNextButton.setVisibility(View.GONE);
561 }
562 else {
563 mNextButton.setText(buttonText);
564 }
565 }
566 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
567 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
568 if (TextUtils.isEmpty(buttonText)) {
569 backButton.setVisibility(View.GONE);
570 }
571 else {
572 backButton.setText(buttonText);
573 }
574 }
575 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
576 skipButton.setVisibility(View.VISIBLE);
577 }
578 }
579 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800580 }
581
582 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800583 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700584 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 }
586
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700587 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800588 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700589
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800590 if (count == 0) {
591 setTitle(mInitialTitle);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700592 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800593 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700594
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800595 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
596 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700597
598 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800599 }
600
601 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
602 final CharSequence title;
603 final int titleRes = bse.getBreadCrumbTitleRes();
604 if (titleRes > 0) {
605 title = getText(titleRes);
606 } else {
607 title = bse.getBreadCrumbTitle();
608 }
609 if (title != null) {
610 setTitle(title);
611 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800612 }
613
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800614 @Override
615 protected void onSaveInstanceState(Bundle outState) {
616 super.onSaveInstanceState(outState);
617
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700618 if (mCategories.size() > 0) {
619 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800620 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700621
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700622 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700623 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700624
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700625 if (mDisplaySearch) {
626 // The option menus are created if the ActionBar is visible and they are also created
627 // asynchronously. If you launch Settings with an Intent action like
628 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
629 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
630 // menu item and search view are null.
631 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
632 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700633
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700634 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
635 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
636 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800637 }
638
639 @Override
640 public void onResume() {
641 super.onResume();
642
643 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
644 @Override
645 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700646 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700647 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 }
649 };
650 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
651 mDevelopmentPreferencesListener);
652
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700654
Svetoslav990159a2014-04-14 17:14:59 -0700655 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700656
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700657 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700658 onQueryTextSubmit(mSearchQuery);
659 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800660 }
661
662 @Override
663 public void onPause() {
664 super.onPause();
665
666 unregisterReceiver(mBatteryInfoReceiver);
667
Svetoslav990159a2014-04-14 17:14:59 -0700668 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 }
670
671 @Override
672 public void onDestroy() {
673 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700674
675 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
676 mDevelopmentPreferencesListener);
677 mDevelopmentPreferencesListener = null;
678
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800679 if (mListeningToAccountUpdates) {
680 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
681 }
682 }
683
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800684 protected boolean isValidFragment(String fragmentName) {
685 // Almost all fragments are wrapped in this,
686 // except for a few that have their own activities.
687 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
688 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
689 }
690 return false;
691 }
692
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 @Override
694 public Intent getIntent() {
695 Intent superIntent = super.getIntent();
696 String startingFragment = getStartingFragmentClass(superIntent);
697 // This is called from super.onCreate, isMultiPane() is not yet reliable
698 // Do not use onIsHidingHeaders either, which relies itself on this method
699 if (startingFragment != null) {
700 Intent modIntent = new Intent(superIntent);
701 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
702 Bundle args = superIntent.getExtras();
703 if (args != null) {
704 args = new Bundle(args);
705 } else {
706 args = new Bundle();
707 }
708 args.putParcelable("intent", superIntent);
709 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
710 return modIntent;
711 }
712 return superIntent;
713 }
714
715 /**
716 * Checks if the component name in the intent is different from the Settings class and
717 * returns the class name to load as a fragment.
718 */
719 private String getStartingFragmentClass(Intent intent) {
720 if (mFragmentClass != null) return mFragmentClass;
721
722 String intentClass = intent.getComponent().getClassName();
723 if (intentClass.equals(getClass().getName())) return null;
724
725 if ("com.android.settings.ManageApplications".equals(intentClass)
726 || "com.android.settings.RunningServices".equals(intentClass)
727 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
728 // Old names of manage apps.
729 intentClass = com.android.settings.applications.ManageApplications.class.getName();
730 }
731
732 return intentClass;
733 }
734
735 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000736 * Start a new fragment containing a preference panel. If the preferences
737 * are being displayed in multi-pane mode, the given fragment class will
738 * be instantiated and placed in the appropriate pane. If running in
739 * single-pane mode, a new activity will be launched in which to show the
740 * fragment.
741 *
742 * @param fragmentClass Full name of the class implementing the fragment.
743 * @param args Any desired arguments to supply to the fragment.
744 * @param titleRes Optional resource identifier of the title of this
745 * fragment.
746 * @param titleText Optional text of the title of this fragment.
747 * @param resultTo Optional fragment that result data should be sent to.
748 * If non-null, resultTo.onActivityResult() will be called when this
749 * preference panel is done. The launched panel must use
750 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
751 * @param resultRequestCode If resultTo is non-null, this is the caller's
752 * request code to be received with the resut.
753 */
754 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700755 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700756 String title;
757 if (titleRes > 0) {
758 title = getString(titleRes);
Fabrice Di Meglio821a0722014-04-08 13:56:06 -0700759 } else if (titleText != null) {
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700760 title = titleText.toString();
Fabrice Di Meglio821a0722014-04-08 13:56:06 -0700761 } else {
762 // There not much we can do in that case
763 title = "";
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700764 }
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700765 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode, title);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000766 }
767
768 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800769 * Called by a preference panel fragment to finish itself.
770 *
771 * @param caller The fragment that is asking to be finished.
772 * @param resultCode Optional result code to send back to the original
773 * launching fragment.
774 * @param resultData Optional result data to send back to the original
775 * launching fragment.
776 */
777 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
778 setResult(resultCode, resultData);
779 }
780
781 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000782 * Start a new fragment.
783 *
784 * @param fragment The fragment to start
785 * @param push If true, the current fragment will be pushed onto the back stack. If false,
786 * the current fragment will be replaced.
787 */
788 public void startPreferenceFragment(Fragment fragment, boolean push) {
789 FragmentTransaction transaction = getFragmentManager().beginTransaction();
790 transaction.replace(R.id.prefs, fragment);
791 if (push) {
792 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
793 transaction.addToBackStack(BACK_STACK_PREFS);
794 } else {
795 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
796 }
797 transaction.commitAllowingStateLoss();
798 }
799
800 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700801 * Switch to a specific Fragment with taking care of validation, Title and BackStack
802 */
803 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
804 boolean addToBackStack, CharSequence title, boolean withTransition) {
805 if (validate && !isValidFragment(fragmentName)) {
806 throw new IllegalArgumentException("Invalid fragment for this activity: "
807 + fragmentName);
808 }
809 Fragment f = Fragment.instantiate(this, fragmentName, args);
810 FragmentTransaction transaction = getFragmentManager().beginTransaction();
811 transaction.replace(R.id.prefs, f);
812 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700813 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700814 }
815 if (addToBackStack) {
816 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
817 }
818 if (title != null) {
819 transaction.setBreadCrumbTitle(title);
820 }
821 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700822 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700823 return f;
824 }
825
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700826 public void setNeedToRebuildCategories(boolean need) {
827 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700828 }
829
830 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700831 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700832 *
833 * @param categories The list in which to place the tiles categories.
834 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700835 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700836 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700837 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
838 updateTilesList(categories);
839 }
840
841 /**
842 * Parse the given XML file as a categories description, adding each
843 * parsed categories and tiles into the target list.
844 *
845 * @param resid The XML resource to load and parse.
846 * @param target The list in which the parsed categories and tiles should be placed.
847 */
848 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
849 XmlResourceParser parser = null;
850 try {
851 parser = getResources().getXml(resid);
852 AttributeSet attrs = Xml.asAttributeSet(parser);
853
854 int type;
855 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
856 && type != XmlPullParser.START_TAG) {
857 // Parse next until start tag is found
858 }
859
860 String nodeName = parser.getName();
861 if (!"dashboard-categories".equals(nodeName)) {
862 throw new RuntimeException(
863 "XML document must start with <preference-categories> tag; found"
864 + nodeName + " at " + parser.getPositionDescription());
865 }
866
867 Bundle curBundle = null;
868
869 final int outerDepth = parser.getDepth();
870 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
871 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
872 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
873 continue;
874 }
875
876 nodeName = parser.getName();
877 if ("dashboard-category".equals(nodeName)) {
878 DashboardCategory category = new DashboardCategory();
879
880 TypedArray sa = obtainStyledAttributes(
881 attrs, com.android.internal.R.styleable.PreferenceHeader);
882 category.id = sa.getResourceId(
883 com.android.internal.R.styleable.PreferenceHeader_id,
884 (int)DashboardCategory.CAT_ID_UNDEFINED);
885
886 TypedValue tv = sa.peekValue(
887 com.android.internal.R.styleable.PreferenceHeader_title);
888 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
889 if (tv.resourceId != 0) {
890 category.titleRes = tv.resourceId;
891 } else {
892 category.title = tv.string;
893 }
894 }
895 sa.recycle();
896
897 final int innerDepth = parser.getDepth();
898 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
899 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
900 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
901 continue;
902 }
903
904 String innerNodeName = parser.getName();
905 if (innerNodeName.equals("dashboard-tile")) {
906 DashboardTile tile = new DashboardTile();
907
908 sa = obtainStyledAttributes(
909 attrs, com.android.internal.R.styleable.PreferenceHeader);
910 tile.id = sa.getResourceId(
911 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700912 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700913 tv = sa.peekValue(
914 com.android.internal.R.styleable.PreferenceHeader_title);
915 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
916 if (tv.resourceId != 0) {
917 tile.titleRes = tv.resourceId;
918 } else {
919 tile.title = tv.string;
920 }
921 }
922 tv = sa.peekValue(
923 com.android.internal.R.styleable.PreferenceHeader_summary);
924 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
925 if (tv.resourceId != 0) {
926 tile.summaryRes = tv.resourceId;
927 } else {
928 tile.summary = tv.string;
929 }
930 }
931 tile.iconRes = sa.getResourceId(
932 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
933 tile.fragment = sa.getString(
934 com.android.internal.R.styleable.PreferenceHeader_fragment);
935 sa.recycle();
936
937 if (curBundle == null) {
938 curBundle = new Bundle();
939 }
940
941 final int innerDepth2 = parser.getDepth();
942 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
943 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
944 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
945 continue;
946 }
947
948 String innerNodeName2 = parser.getName();
949 if (innerNodeName2.equals("extra")) {
950 getResources().parseBundleExtra("extra", attrs, curBundle);
951 XmlUtils.skipCurrentTag(parser);
952
953 } else if (innerNodeName2.equals("intent")) {
954 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
955
956 } else {
957 XmlUtils.skipCurrentTag(parser);
958 }
959 }
960
961 if (curBundle.size() > 0) {
962 tile.fragmentArguments = curBundle;
963 curBundle = null;
964 }
965
966 category.addTile(tile);
967
968 } else {
969 XmlUtils.skipCurrentTag(parser);
970 }
971 }
972
973 target.add(category);
974 } else {
975 XmlUtils.skipCurrentTag(parser);
976 }
977 }
978
979 } catch (XmlPullParserException e) {
980 throw new RuntimeException("Error parsing categories", e);
981 } catch (IOException e) {
982 throw new RuntimeException("Error parsing categories", e);
983 } finally {
984 if (parser != null) parser.close();
985 }
986 }
987
988 private void updateTilesList(List<DashboardCategory> target) {
989 final boolean showDev = mDevelopmentPreferences.getBoolean(
990 DevelopmentSettings.PREF_SHOW,
991 android.os.Build.TYPE.equals("eng"));
992
993 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
994
995 final int size = target.size();
996 for (int i = 0; i < size; i++) {
997
998 DashboardCategory category = target.get(i);
999
1000 // Ids are integers, so downcasting is ok
1001 int id = (int) category.id;
1002 if (id == R.id.account_settings) {
1003 insertAccountsTiles(category);
1004 continue;
1005 }
1006 int n = category.getTilesCount() - 1;
1007 while (n >= 0) {
1008
1009 DashboardTile tile = category.getTile(n);
1010
1011 id = (int) tile.id;
1012 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1013 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1014 } else if (id == R.id.wifi_settings) {
1015 // Remove WiFi Settings if WiFi service is not available.
1016 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1017 category.removeTile(n);
1018 }
1019 } else if (id == R.id.bluetooth_settings) {
1020 // Remove Bluetooth Settings if Bluetooth service is not available.
1021 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1022 category.removeTile(n);
1023 }
1024 } else if (id == R.id.data_usage_settings) {
1025 // Remove data usage when kernel module not enabled
1026 final INetworkManagementService netManager = INetworkManagementService.Stub
1027 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1028 try {
1029 if (!netManager.isBandwidthControlEnabled()) {
1030 category.removeTile(n);
1031 }
1032 } catch (RemoteException e) {
1033 // ignored
1034 }
1035 } else if (id == R.id.battery_settings) {
1036 // Remove battery settings when battery is not available. (e.g. TV)
1037
1038 if (!mBatteryPresent) {
1039 category.removeTile(n);
1040 }
1041 } else if (id == R.id.home_settings) {
1042 if (!updateHomeSettingTiles(tile)) {
1043 category.removeTile(n);
1044 }
1045 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001046 boolean hasMultipleUsers =
1047 ((UserManager) getSystemService(Context.USER_SERVICE))
1048 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001049 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001050 || (!UserManager.supportsMultipleUsers()
1051 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001052 || Utils.isMonkeyRunning()) {
1053 category.removeTile(n);
1054 }
1055 } else if (id == R.id.nfc_payment_settings) {
1056 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1057 category.removeTile(n);
1058 } else {
1059 // Only show if NFC is on and we have the HCE feature
1060 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1061 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1062 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1063 category.removeTile(n);
1064 }
1065 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001066 } else if (id == R.id.print_settings) {
1067 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1068 PackageManager.FEATURE_PRINTING);
1069 if (!hasPrintingSupport) {
1070 category.removeTile(n);
1071 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001072 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001073 if (!showDev || um.hasUserRestriction(
1074 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001075 category.removeTile(n);
1076 }
1077 } else if (id == R.id.account_add) {
1078 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1079 category.removeTile(n);
1080 }
1081 }
1082
1083 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001084 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1085 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001086 category.removeTile(n);
1087 }
1088
1089 n--;
1090 }
1091 }
1092 }
1093
1094 private boolean updateHomeSettingTiles(DashboardTile tile) {
1095 // Once we decide to show Home settings, keep showing it forever
1096 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1097 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1098 return true;
1099 }
1100
1101 try {
1102 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1103 getPackageManager().getHomeActivities(homeApps);
1104 if (homeApps.size() < 2) {
1105 // When there's only one available home app, omit this settings
1106 // category entirely at the top level UI. If the user just
1107 // uninstalled the penultimate home app candidiate, we also
1108 // now tell them about why they aren't seeing 'Home' in the list.
1109 if (sShowNoHomeNotice) {
1110 sShowNoHomeNotice = false;
1111 NoHomeDialogFragment.show(this);
1112 }
1113 return false;
1114 } else {
1115 // Okay, we're allowing the Home settings category. Tell it, when
1116 // invoked via this front door, that we'll need to be told about the
1117 // case when the user uninstalls all but one home app.
1118 if (tile.fragmentArguments == null) {
1119 tile.fragmentArguments = new Bundle();
1120 }
1121 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1122 }
1123 } catch (Exception e) {
1124 // Can't look up the home activity; bail on configuring the icon
1125 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1126 }
1127
1128 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1129 return true;
1130 }
1131
1132 private void insertAccountsTiles(DashboardCategory target) {
1133 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1134 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1135 for (String accountType : accountTypes) {
1136 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1137 if (label == null) {
1138 continue;
1139 }
1140
1141 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1142 boolean skipToAccount = accounts.length == 1
1143 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1144 DashboardTile accountTile = new DashboardTile();
1145 accountTile.title = label;
1146 if (accountTile.extras == null) {
1147 accountTile.extras = new Bundle();
1148 }
1149 if (skipToAccount) {
1150 accountTile.fragment = AccountSyncSettings.class.getName();
1151 accountTile.fragmentArguments = new Bundle();
1152 // Need this for the icon
1153 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1154 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1155 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1156 accounts[0]);
1157 } else {
1158 accountTile.fragment = ManageAccountsSettings.class.getName();
1159 accountTile.fragmentArguments = new Bundle();
1160 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1161 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1162 accountType);
1163 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1164 label.toString());
1165 }
1166 dashboardTiles.add(accountTile);
1167 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1168 }
1169
1170 // Sort by label
1171 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1172 @Override
1173 public int compare(DashboardTile t1, DashboardTile t2) {
1174 return t1.title.toString().compareTo(t2.title.toString());
1175 }
1176 });
1177 int index = 0;
1178 for (DashboardTile tile : dashboardTiles) {
1179 target.addTile(index, tile);
1180 index++;
1181 }
1182 if (!mListeningToAccountUpdates) {
1183 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1184 mListeningToAccountUpdates = true;
1185 }
1186 }
1187
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001188 private void getMetaData() {
1189 try {
1190 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1191 PackageManager.GET_META_DATA);
1192 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001193 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1194 } catch (NameNotFoundException nnfe) {
1195 // No recovery
1196 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1197 }
1198 }
1199
1200 // give subclasses access to the Next button
1201 public boolean hasNextButton() {
1202 return mNextButton != null;
1203 }
1204
1205 public Button getNextButton() {
1206 return mNextButton;
1207 }
1208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001209 @Override
1210 public boolean shouldUpRecreateTask(Intent targetIntent) {
1211 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1212 }
1213
1214 @Override
1215 public void onAccountsUpdated(Account[] accounts) {
1216 // TODO: watch for package upgrades to invalidate cache; see 7206643
1217 mAuthenticatorHelper.updateAuthDescriptions(this);
1218 mAuthenticatorHelper.onAccountsUpdated(this, accounts);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001219 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001220 }
1221
1222 public static void requestHomeNotice() {
1223 sShowNoHomeNotice = true;
1224 }
1225
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001226 @Override
1227 public boolean onQueryTextSubmit(String query) {
1228 switchToSearchResultsFragmentIfNeeded();
1229 mSearchQuery = query;
1230 return mSearchResultsFragment.onQueryTextSubmit(query);
1231 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001232
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001233 @Override
1234 public boolean onQueryTextChange(String newText) {
1235 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001236 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001237 return false;
1238 }
1239 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001240 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001241
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001242 @Override
1243 public boolean onClose() {
1244 return false;
1245 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001246
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001247 @Override
1248 public boolean onMenuItemActionExpand(MenuItem item) {
1249 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001250 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001251 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001252 return true;
1253 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001254
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001255 @Override
1256 public boolean onMenuItemActionCollapse(MenuItem item) {
1257 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001258 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001259 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001260 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001261 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001262 return true;
1263 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001264
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001265 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001266 if (mSearchResultsFragment != null) {
1267 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001268 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001269 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1270 if (current != null && current instanceof SearchResultsSummary) {
1271 mSearchResultsFragment = (SearchResultsSummary) current;
1272 } else {
1273 String title = getString(R.string.search_results_title);
1274 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglio41937762014-05-13 19:51:59 -07001275 SearchResultsSummary.class.getName(), null, false, true, title,
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001276 true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001277 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001278 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001279 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001280 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001281
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001282 public void needToRevertToInitialFragment() {
1283 mNeedToRevertToInitialFragment = true;
1284 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001285
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001286 private void revertToInitialFragment() {
1287 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001288 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001289 mSearchMenuItemExpanded = false;
1290 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1291 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001292 if (mSearchMenuItem != null) {
1293 mSearchMenuItem.collapseActionView();
1294 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001295 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001296}