blob: f98669f558ba9de23ed764d6b4fe5fcccf9dfd90 [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 Megliod25314d2014-03-21 19:24:43 -070066import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040067
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;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040086import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087import com.android.settings.fuelgauge.PowerUsageSummary;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070088import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070089import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
91import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
92import com.android.settings.inputmethod.SpellCheckersSettings;
93import com.android.settings.inputmethod.UserDictionaryList;
94import com.android.settings.location.LocationSettings;
95import com.android.settings.nfc.AndroidBeam;
96import com.android.settings.nfc.PaymentSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040097import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
101import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800102import com.android.settings.print.PrintJobSettingsFragment;
103import com.android.settings.print.PrintSettingsFragment;
104import com.android.settings.tts.TextToSpeechSettings;
105import com.android.settings.users.UserSettings;
106import com.android.settings.vpn2.VpnSettings;
107import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700108import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.wifi.AdvancedWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.wifi.WifiSettings;
111import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400112
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import org.xmlpull.v1.XmlPullParser;
114import org.xmlpull.v1.XmlPullParserException;
115
116import java.io.IOException;
117import java.util.ArrayList;
118import java.util.Collections;
119import java.util.Comparator;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700121import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700123import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700124
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125public class SettingsActivity extends Activity
126 implements PreferenceManager.OnPreferenceTreeClickListener,
127 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700128 ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
129 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100130 MenuItem.OnActionExpandListener,
131 AuthenticatorHelper.OnAccountsUpdateListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133 private static final String LOG_TAG = "Settings";
134
135 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700136 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
138 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700139 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700140 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 /**
143 * When starting this activity, the invoking Intent can contain this extra
144 * string to specify which fragment should be initially displayed.
145 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
146 * will call isValidFragment() to confirm that the fragment class name is valid for this
147 * activity.
148 */
149 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
150
151 /**
152 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
153 * this extra can also be specified to supply a Bundle of arguments to pass
154 * to that fragment when it is instantiated during the initial creation
155 * of the activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
158
159 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700160 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
161 */
162 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
163
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800164 public static final String BACK_STACK_PREFS = ":settings:prefs";
165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 // extras that allow any preference activity to be launched as part of a wizard
167
168 // show Back and Next buttons? takes boolean parameter
169 // Back will then return RESULT_CANCELED and Next RESULT_OK
170 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
171
172 // add a Skip button?
173 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
174
175 // specify custom text for the Back or Next buttons, or cause a button to not appear
176 // at all by setting it to null
177 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
178 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
179
180 /**
181 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700182 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800183 * that fragment.
184 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700187 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800189 private static final String META_DATA_KEY_FRAGMENT_CLASS =
190 "com.android.settings.FRAGMENT_CLASS";
191
192 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
193
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700194 private static final String EMPTY_QUERY = "";
195
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196 private static boolean sShowNoHomeNotice = false;
197
198 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800199
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800200 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700201 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800202
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203 // Show only these settings for restricted users
204 private int[] SETTINGS_FOR_RESTRICTED = {
205 R.id.wireless_section,
206 R.id.wifi_settings,
207 R.id.bluetooth_settings,
208 R.id.data_usage_settings,
209 R.id.wireless_settings,
210 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400211 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 R.id.display_settings,
213 R.id.storage_settings,
214 R.id.application_settings,
215 R.id.battery_settings,
216 R.id.personal_section,
217 R.id.location_settings,
218 R.id.security_settings,
219 R.id.language_settings,
220 R.id.user_settings,
221 R.id.account_settings,
222 R.id.account_add,
223 R.id.system_section,
224 R.id.date_time_settings,
225 R.id.about_settings,
226 R.id.accessibility_settings,
227 R.id.print_settings,
228 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800229 R.id.home_settings,
230 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800231 };
232
233 private static final String[] ENTRY_FRAGMENTS = {
234 WirelessSettings.class.getName(),
235 WifiSettings.class.getName(),
236 AdvancedWifiSettings.class.getName(),
237 BluetoothSettings.class.getName(),
238 TetherSettings.class.getName(),
239 WifiP2pSettings.class.getName(),
240 VpnSettings.class.getName(),
241 DateTimeSettings.class.getName(),
242 LocalePicker.class.getName(),
243 InputMethodAndLanguageSettings.class.getName(),
244 SpellCheckersSettings.class.getName(),
245 UserDictionaryList.class.getName(),
246 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 DisplaySettings.class.getName(),
248 DeviceInfoSettings.class.getName(),
249 ManageApplications.class.getName(),
250 ProcessStatsUi.class.getName(),
251 NotificationStation.class.getName(),
252 LocationSettings.class.getName(),
253 SecuritySettings.class.getName(),
254 PrivacySettings.class.getName(),
255 DeviceAdminSettings.class.getName(),
256 AccessibilitySettings.class.getName(),
257 CaptionPropertiesFragment.class.getName(),
258 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
260 TextToSpeechSettings.class.getName(),
261 Memory.class.getName(),
262 DevelopmentSettings.class.getName(),
263 UsbSettings.class.getName(),
264 AndroidBeam.class.getName(),
265 WifiDisplaySettings.class.getName(),
266 PowerUsageSummary.class.getName(),
267 AccountSyncSettings.class.getName(),
268 CryptKeeperSettings.class.getName(),
269 DataUsageSummary.class.getName(),
270 DreamSettings.class.getName(),
271 UserSettings.class.getName(),
272 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400273 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 ManageAccountsSettings.class.getName(),
275 PrintSettingsFragment.class.getName(),
276 PrintJobSettingsFragment.class.getName(),
277 TrustedCredentialsSettings.class.getName(),
278 PaymentSettings.class.getName(),
279 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700280 ZenModeSettings.class.getName(),
281 NotificationSettings.class.getName(),
282 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
283 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400284 InstalledAppDetails.class.getName(),
285 BatterySaverSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 };
287
288 private SharedPreferences mDevelopmentPreferences;
289 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
290
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 private AuthenticatorHelper mAuthenticatorHelper;
292 private boolean mListeningToAccountUpdates;
293
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 private boolean mBatteryPresent = true;
295 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
296
297 @Override
298 public void onReceive(Context context, Intent intent) {
299 String action = intent.getAction();
300 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
301 boolean batteryPresent = Utils.isBatteryPresent(intent);
302
303 if (mBatteryPresent != batteryPresent) {
304 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700305 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 }
307 }
308 }
309 };
310
Svetoslav990159a2014-04-14 17:14:59 -0700311 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
312 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700313
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700314 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700315 private SwitchBar mSwitchBar;
316
317 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700318
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700319 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700320 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700321
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700322 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700323 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700324
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700325 private ViewGroup mContent;
326
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700327 private SearchView mSearchView;
328 private MenuItem mSearchMenuItem;
329 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700330 private SearchResultsSummary mSearchResultsFragment;
331 private String mSearchQuery;
332
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700333 // Categories
334 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
335 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800336
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700337 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800338 private Handler mHandler = new Handler() {
339 @Override
340 public void handleMessage(Message msg) {
341 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700342 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700343 if(mNeedToRebuildCategories) {
344 buildDashboardCategories(mCategories);
345 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 } break;
347 }
348 }
349 };
350
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700351 private boolean mNeedToRevertToInitialFragment = false;
352
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700353 public SwitchBar getSwitchBar() {
354 return mSwitchBar;
355 }
356
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700357 public AuthenticatorHelper getAuthenticatorHelper() {
358 return mAuthenticatorHelper;
359 }
360
361 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700362 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700363 buildDashboardCategories(mCategories);
364 mNeedToRebuildCategories = false;
365 }
366 return mCategories;
367 }
368
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800369 @Override
370 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
371 // Override the fragment title for Wallpaper settings
372 int titleRes = pref.getTitleRes();
373 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
374 titleRes = R.string.wallpaper_settings_fragment_title;
375 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
376 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
377 if (UserManager.get(this).isLinkedUser()) {
378 titleRes = R.string.profile_info_settings_title;
379 } else {
380 titleRes = R.string.user_info_settings_title;
381 }
382 }
383 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
384 null, 0);
385 return true;
386 }
387
388 @Override
389 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
390 return false;
391 }
392
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700393 private void invalidateCategories() {
394 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
395 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800396 }
397 }
398
399 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800400 public void onConfigurationChanged(Configuration newConfig) {
401 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800402 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800403 }
404
405 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700406 protected void onStart() {
407 super.onStart();
408
409 if (mNeedToRevertToInitialFragment) {
410 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800412 }
413
414 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700415 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700416 if (!mDisplaySearch) {
417 return false;
418 }
419
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700420 MenuInflater inflater = getMenuInflater();
421 inflater.inflate(R.menu.options_menu, menu);
422
423 // Cache the search query (can be overriden by the OnQueryTextListener)
424 final String query = mSearchQuery;
425
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700426 mSearchMenuItem = menu.findItem(R.id.search);
427 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700428
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700429 if (mSearchMenuItem == null || mSearchView == null) {
430 return false;
431 }
432
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700433 if (mSearchResultsFragment != null) {
434 mSearchResultsFragment.setSearchView(mSearchView);
435 }
436
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700437 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700438 mSearchView.setOnQueryTextListener(this);
439 mSearchView.setOnCloseListener(this);
440
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700441 if (mSearchMenuItemExpanded) {
442 mSearchMenuItem.expandActionView();
443 }
444 mSearchView.setQuery(query, true /* submit */);
445
446 return true;
447 }
448
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700449 private static boolean isShortCutIntent(final Intent intent) {
450 Set<String> categories = intent.getCategories();
451 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
452 }
453
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700454 private static boolean isAppInfoIntent(final Intent intent) {
455 String action = intent.getAction();
456 return (action != null) && action.equals("android.settings.APPLICATION_DETAILS_SETTINGS");
457 }
458
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700459 @Override
460 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700461 super.onCreate(savedState);
462
463 // Should happen before any call to getIntent()
464 getMetaData();
465
466 final Intent intent = getIntent();
467 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
468 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800469 }
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100470 // TODO: Delete accounts tile once we have the new screen working
471 // See: http://b/15815948
472 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
473 mAuthenticatorHelper = new AuthenticatorHelper(
474 this, UserHandle.getCallingUserHandle(), um, this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800475 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100476 mAuthenticatorHelper.onAccountsUpdated(null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800477
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
479 Context.MODE_PRIVATE);
480
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700481 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700482 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700483
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700484 mIsShortcut = isShortCutIntent(intent) || isAppInfoIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700485 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
486
487 mIsShowingDashboard = (initialFragmentName == null) && !mIsShortcut;
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700488
489 final ComponentName cn = getIntent().getComponent();
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700490 final boolean isSubSettings = cn.getClassName().equals(SubSettings.class.getName());
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700491
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700492 // If this is a sub settings or not the main Dashboard and not a Shortcut then apply the
493 // correct theme for the ActionBar content inset
494 if (isSubSettings || (!mIsShowingDashboard && !mIsShortcut)) {
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700495 setTheme(R.style.Theme_SubSettings);
496 }
497
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800498 setContentView(R.layout.settings_main);
499
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700500 mContent = (ViewGroup) findViewById(R.id.prefs);
501
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800502 getFragmentManager().addOnBackStackChangedListener(this);
503
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700504 mDisplayHomeAsUpEnabled = true;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700505 mDisplaySearch = true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800506
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700507 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700508 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700509 }
510
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700511 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700512 // We are restarting from a previous saved state; used that to initialize, instead
513 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700514 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
515 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700517 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800518
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700519 ArrayList<DashboardCategory> categories =
520 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
521 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700522 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700523 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700524 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800525 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700526
527 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700528 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800529 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700530 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700531 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
532 // only if it is a sub settings
533 if (mIsShortcut) {
534 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700535 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700536 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700537 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700538
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700539 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700540 switchToFragment(initialFragmentName, initialArguments, true, false,
541 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000542 } else {
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700543 // No UP if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700544 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700545 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700546 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700547 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800548 }
549 }
550
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700551 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700552 if (mActionBar != null) {
553 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
554 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
555 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700556 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
557
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800559 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
560
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700561 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 if (buttonBar != null) {
563 buttonBar.setVisibility(View.VISIBLE);
564
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700565 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 backButton.setOnClickListener(new OnClickListener() {
567 public void onClick(View v) {
568 setResult(RESULT_CANCELED);
569 finish();
570 }
571 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700572 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 skipButton.setOnClickListener(new OnClickListener() {
574 public void onClick(View v) {
575 setResult(RESULT_OK);
576 finish();
577 }
578 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700579 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580 mNextButton.setOnClickListener(new OnClickListener() {
581 public void onClick(View v) {
582 setResult(RESULT_OK);
583 finish();
584 }
585 });
586
587 // set our various button parameters
588 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
589 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
590 if (TextUtils.isEmpty(buttonText)) {
591 mNextButton.setVisibility(View.GONE);
592 }
593 else {
594 mNextButton.setText(buttonText);
595 }
596 }
597 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
598 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
599 if (TextUtils.isEmpty(buttonText)) {
600 backButton.setVisibility(View.GONE);
601 }
602 else {
603 backButton.setText(buttonText);
604 }
605 }
606 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
607 skipButton.setVisibility(View.VISIBLE);
608 }
609 }
610 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800611 }
612
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700613 private void setTitleFromIntent(Intent intent) {
614 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
615 if (initialTitleResId > 0) {
616 mInitialTitle = null;
617 mInitialTitleResId = initialTitleResId;
618 setTitle(mInitialTitleResId);
619 } else {
620 mInitialTitleResId = -1;
621 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
622 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
623 setTitle(mInitialTitle);
624 }
625 }
626
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800627 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800628 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700629 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800630 }
631
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700632 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800633 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700634
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800635 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700636 if (mInitialTitleResId > 0) {
637 setTitle(mInitialTitleResId);
638 } else {
639 setTitle(mInitialTitle);
640 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700641 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800642 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700643
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800644 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
645 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700646
647 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800648 }
649
650 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
651 final CharSequence title;
652 final int titleRes = bse.getBreadCrumbTitleRes();
653 if (titleRes > 0) {
654 title = getText(titleRes);
655 } else {
656 title = bse.getBreadCrumbTitle();
657 }
658 if (title != null) {
659 setTitle(title);
660 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800661 }
662
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800663 @Override
664 protected void onSaveInstanceState(Bundle outState) {
665 super.onSaveInstanceState(outState);
666
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700667 if (mCategories.size() > 0) {
668 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700670
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700671 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700672 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700673
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700674 if (mDisplaySearch) {
675 // The option menus are created if the ActionBar is visible and they are also created
676 // asynchronously. If you launch Settings with an Intent action like
677 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
678 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
679 // menu item and search view are null.
680 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
681 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700682
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700683 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
684 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
685 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800686 }
687
688 @Override
689 public void onResume() {
690 super.onResume();
691
692 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
693 @Override
694 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700695 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700696 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800697 }
698 };
699 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
700 mDevelopmentPreferencesListener);
701
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800702 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700703
Svetoslav990159a2014-04-14 17:14:59 -0700704 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700705
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700706 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700707 onQueryTextSubmit(mSearchQuery);
708 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800709 }
710
711 @Override
712 public void onPause() {
713 super.onPause();
714
715 unregisterReceiver(mBatteryInfoReceiver);
716
Svetoslav990159a2014-04-14 17:14:59 -0700717 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800718 }
719
720 @Override
721 public void onDestroy() {
722 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700723
724 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
725 mDevelopmentPreferencesListener);
726 mDevelopmentPreferencesListener = null;
727
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800728 if (mListeningToAccountUpdates) {
729 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
730 }
731 }
732
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 protected boolean isValidFragment(String fragmentName) {
734 // Almost all fragments are wrapped in this,
735 // except for a few that have their own activities.
736 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
737 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
738 }
739 return false;
740 }
741
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800742 @Override
743 public Intent getIntent() {
744 Intent superIntent = super.getIntent();
745 String startingFragment = getStartingFragmentClass(superIntent);
746 // This is called from super.onCreate, isMultiPane() is not yet reliable
747 // Do not use onIsHidingHeaders either, which relies itself on this method
748 if (startingFragment != null) {
749 Intent modIntent = new Intent(superIntent);
750 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
751 Bundle args = superIntent.getExtras();
752 if (args != null) {
753 args = new Bundle(args);
754 } else {
755 args = new Bundle();
756 }
757 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100758 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800759 return modIntent;
760 }
761 return superIntent;
762 }
763
764 /**
765 * Checks if the component name in the intent is different from the Settings class and
766 * returns the class name to load as a fragment.
767 */
768 private String getStartingFragmentClass(Intent intent) {
769 if (mFragmentClass != null) return mFragmentClass;
770
771 String intentClass = intent.getComponent().getClassName();
772 if (intentClass.equals(getClass().getName())) return null;
773
774 if ("com.android.settings.ManageApplications".equals(intentClass)
775 || "com.android.settings.RunningServices".equals(intentClass)
776 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
777 // Old names of manage apps.
778 intentClass = com.android.settings.applications.ManageApplications.class.getName();
779 }
780
781 return intentClass;
782 }
783
784 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000785 * Start a new fragment containing a preference panel. If the preferences
786 * are being displayed in multi-pane mode, the given fragment class will
787 * be instantiated and placed in the appropriate pane. If running in
788 * single-pane mode, a new activity will be launched in which to show the
789 * fragment.
790 *
791 * @param fragmentClass Full name of the class implementing the fragment.
792 * @param args Any desired arguments to supply to the fragment.
793 * @param titleRes Optional resource identifier of the title of this
794 * fragment.
795 * @param titleText Optional text of the title of this fragment.
796 * @param resultTo Optional fragment that result data should be sent to.
797 * If non-null, resultTo.onActivityResult() will be called when this
798 * preference panel is done. The launched panel must use
799 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
800 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700801 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000802 */
803 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700804 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700805 String title = null;
806 if (titleRes < 0) {
807 if (titleText != null) {
808 title = titleText.toString();
809 } else {
810 // There not much we can do in that case
811 title = "";
812 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700813 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700814 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700815 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000816 }
817
818 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800819 * Called by a preference panel fragment to finish itself.
820 *
821 * @param caller The fragment that is asking to be finished.
822 * @param resultCode Optional result code to send back to the original
823 * launching fragment.
824 * @param resultData Optional result data to send back to the original
825 * launching fragment.
826 */
827 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
828 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700829 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800830 }
831
832 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000833 * Start a new fragment.
834 *
835 * @param fragment The fragment to start
836 * @param push If true, the current fragment will be pushed onto the back stack. If false,
837 * the current fragment will be replaced.
838 */
839 public void startPreferenceFragment(Fragment fragment, boolean push) {
840 FragmentTransaction transaction = getFragmentManager().beginTransaction();
841 transaction.replace(R.id.prefs, fragment);
842 if (push) {
843 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
844 transaction.addToBackStack(BACK_STACK_PREFS);
845 } else {
846 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
847 }
848 transaction.commitAllowingStateLoss();
849 }
850
851 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700852 * Switch to a specific Fragment with taking care of validation, Title and BackStack
853 */
854 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700855 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700856 if (validate && !isValidFragment(fragmentName)) {
857 throw new IllegalArgumentException("Invalid fragment for this activity: "
858 + fragmentName);
859 }
860 Fragment f = Fragment.instantiate(this, fragmentName, args);
861 FragmentTransaction transaction = getFragmentManager().beginTransaction();
862 transaction.replace(R.id.prefs, f);
863 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700864 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700865 }
866 if (addToBackStack) {
867 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
868 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700869 if (titleResId > 0) {
870 transaction.setBreadCrumbTitle(titleResId);
871 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700872 transaction.setBreadCrumbTitle(title);
873 }
874 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700875 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700876 return f;
877 }
878
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700879 public void setNeedToRebuildCategories(boolean need) {
880 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700881 }
882
883 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700884 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700885 *
886 * @param categories The list in which to place the tiles categories.
887 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700888 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700889 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700890 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
891 updateTilesList(categories);
892 }
893
894 /**
895 * Parse the given XML file as a categories description, adding each
896 * parsed categories and tiles into the target list.
897 *
898 * @param resid The XML resource to load and parse.
899 * @param target The list in which the parsed categories and tiles should be placed.
900 */
901 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
902 XmlResourceParser parser = null;
903 try {
904 parser = getResources().getXml(resid);
905 AttributeSet attrs = Xml.asAttributeSet(parser);
906
907 int type;
908 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
909 && type != XmlPullParser.START_TAG) {
910 // Parse next until start tag is found
911 }
912
913 String nodeName = parser.getName();
914 if (!"dashboard-categories".equals(nodeName)) {
915 throw new RuntimeException(
916 "XML document must start with <preference-categories> tag; found"
917 + nodeName + " at " + parser.getPositionDescription());
918 }
919
920 Bundle curBundle = null;
921
922 final int outerDepth = parser.getDepth();
923 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
924 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
925 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
926 continue;
927 }
928
929 nodeName = parser.getName();
930 if ("dashboard-category".equals(nodeName)) {
931 DashboardCategory category = new DashboardCategory();
932
933 TypedArray sa = obtainStyledAttributes(
934 attrs, com.android.internal.R.styleable.PreferenceHeader);
935 category.id = sa.getResourceId(
936 com.android.internal.R.styleable.PreferenceHeader_id,
937 (int)DashboardCategory.CAT_ID_UNDEFINED);
938
939 TypedValue tv = sa.peekValue(
940 com.android.internal.R.styleable.PreferenceHeader_title);
941 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
942 if (tv.resourceId != 0) {
943 category.titleRes = tv.resourceId;
944 } else {
945 category.title = tv.string;
946 }
947 }
948 sa.recycle();
949
950 final int innerDepth = parser.getDepth();
951 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
952 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
953 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
954 continue;
955 }
956
957 String innerNodeName = parser.getName();
958 if (innerNodeName.equals("dashboard-tile")) {
959 DashboardTile tile = new DashboardTile();
960
961 sa = obtainStyledAttributes(
962 attrs, com.android.internal.R.styleable.PreferenceHeader);
963 tile.id = sa.getResourceId(
964 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700965 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700966 tv = sa.peekValue(
967 com.android.internal.R.styleable.PreferenceHeader_title);
968 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
969 if (tv.resourceId != 0) {
970 tile.titleRes = tv.resourceId;
971 } else {
972 tile.title = tv.string;
973 }
974 }
975 tv = sa.peekValue(
976 com.android.internal.R.styleable.PreferenceHeader_summary);
977 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
978 if (tv.resourceId != 0) {
979 tile.summaryRes = tv.resourceId;
980 } else {
981 tile.summary = tv.string;
982 }
983 }
984 tile.iconRes = sa.getResourceId(
985 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
986 tile.fragment = sa.getString(
987 com.android.internal.R.styleable.PreferenceHeader_fragment);
988 sa.recycle();
989
990 if (curBundle == null) {
991 curBundle = new Bundle();
992 }
993
994 final int innerDepth2 = parser.getDepth();
995 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
996 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
997 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
998 continue;
999 }
1000
1001 String innerNodeName2 = parser.getName();
1002 if (innerNodeName2.equals("extra")) {
1003 getResources().parseBundleExtra("extra", attrs, curBundle);
1004 XmlUtils.skipCurrentTag(parser);
1005
1006 } else if (innerNodeName2.equals("intent")) {
1007 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1008
1009 } else {
1010 XmlUtils.skipCurrentTag(parser);
1011 }
1012 }
1013
1014 if (curBundle.size() > 0) {
1015 tile.fragmentArguments = curBundle;
1016 curBundle = null;
1017 }
1018
1019 category.addTile(tile);
1020
1021 } else {
1022 XmlUtils.skipCurrentTag(parser);
1023 }
1024 }
1025
1026 target.add(category);
1027 } else {
1028 XmlUtils.skipCurrentTag(parser);
1029 }
1030 }
1031
1032 } catch (XmlPullParserException e) {
1033 throw new RuntimeException("Error parsing categories", e);
1034 } catch (IOException e) {
1035 throw new RuntimeException("Error parsing categories", e);
1036 } finally {
1037 if (parser != null) parser.close();
1038 }
1039 }
1040
1041 private void updateTilesList(List<DashboardCategory> target) {
1042 final boolean showDev = mDevelopmentPreferences.getBoolean(
1043 DevelopmentSettings.PREF_SHOW,
1044 android.os.Build.TYPE.equals("eng"));
1045
1046 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1047
1048 final int size = target.size();
1049 for (int i = 0; i < size; i++) {
1050
1051 DashboardCategory category = target.get(i);
1052
1053 // Ids are integers, so downcasting is ok
1054 int id = (int) category.id;
1055 if (id == R.id.account_settings) {
1056 insertAccountsTiles(category);
1057 continue;
1058 }
1059 int n = category.getTilesCount() - 1;
1060 while (n >= 0) {
1061
1062 DashboardTile tile = category.getTile(n);
1063
1064 id = (int) tile.id;
1065 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1066 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1067 } else if (id == R.id.wifi_settings) {
1068 // Remove WiFi Settings if WiFi service is not available.
1069 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1070 category.removeTile(n);
1071 }
1072 } else if (id == R.id.bluetooth_settings) {
1073 // Remove Bluetooth Settings if Bluetooth service is not available.
1074 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1075 category.removeTile(n);
1076 }
1077 } else if (id == R.id.data_usage_settings) {
1078 // Remove data usage when kernel module not enabled
1079 final INetworkManagementService netManager = INetworkManagementService.Stub
1080 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1081 try {
1082 if (!netManager.isBandwidthControlEnabled()) {
1083 category.removeTile(n);
1084 }
1085 } catch (RemoteException e) {
1086 // ignored
1087 }
1088 } else if (id == R.id.battery_settings) {
1089 // Remove battery settings when battery is not available. (e.g. TV)
1090
1091 if (!mBatteryPresent) {
1092 category.removeTile(n);
1093 }
1094 } else if (id == R.id.home_settings) {
1095 if (!updateHomeSettingTiles(tile)) {
1096 category.removeTile(n);
1097 }
1098 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001099 boolean hasMultipleUsers =
1100 ((UserManager) getSystemService(Context.USER_SERVICE))
1101 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001102 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001103 || (!UserManager.supportsMultipleUsers()
1104 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001105 || Utils.isMonkeyRunning()) {
1106 category.removeTile(n);
1107 }
1108 } else if (id == R.id.nfc_payment_settings) {
1109 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1110 category.removeTile(n);
1111 } else {
1112 // Only show if NFC is on and we have the HCE feature
1113 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1114 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1115 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1116 category.removeTile(n);
1117 }
1118 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001119 } else if (id == R.id.print_settings) {
1120 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1121 PackageManager.FEATURE_PRINTING);
1122 if (!hasPrintingSupport) {
1123 category.removeTile(n);
1124 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001125 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001126 if (!showDev || um.hasUserRestriction(
1127 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001128 category.removeTile(n);
1129 }
1130 } else if (id == R.id.account_add) {
1131 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1132 category.removeTile(n);
1133 }
1134 }
1135
1136 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001137 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1138 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001139 category.removeTile(n);
1140 }
1141
1142 n--;
1143 }
1144 }
1145 }
1146
1147 private boolean updateHomeSettingTiles(DashboardTile tile) {
1148 // Once we decide to show Home settings, keep showing it forever
1149 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1150 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1151 return true;
1152 }
1153
1154 try {
1155 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1156 getPackageManager().getHomeActivities(homeApps);
1157 if (homeApps.size() < 2) {
1158 // When there's only one available home app, omit this settings
1159 // category entirely at the top level UI. If the user just
1160 // uninstalled the penultimate home app candidiate, we also
1161 // now tell them about why they aren't seeing 'Home' in the list.
1162 if (sShowNoHomeNotice) {
1163 sShowNoHomeNotice = false;
1164 NoHomeDialogFragment.show(this);
1165 }
1166 return false;
1167 } else {
1168 // Okay, we're allowing the Home settings category. Tell it, when
1169 // invoked via this front door, that we'll need to be told about the
1170 // case when the user uninstalls all but one home app.
1171 if (tile.fragmentArguments == null) {
1172 tile.fragmentArguments = new Bundle();
1173 }
1174 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1175 }
1176 } catch (Exception e) {
1177 // Can't look up the home activity; bail on configuring the icon
1178 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1179 }
1180
1181 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1182 return true;
1183 }
1184
1185 private void insertAccountsTiles(DashboardCategory target) {
1186 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1187 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1188 for (String accountType : accountTypes) {
1189 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1190 if (label == null) {
1191 continue;
1192 }
1193
1194 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1195 boolean skipToAccount = accounts.length == 1
1196 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1197 DashboardTile accountTile = new DashboardTile();
1198 accountTile.title = label;
1199 if (accountTile.extras == null) {
1200 accountTile.extras = new Bundle();
1201 }
1202 if (skipToAccount) {
1203 accountTile.fragment = AccountSyncSettings.class.getName();
1204 accountTile.fragmentArguments = new Bundle();
1205 // Need this for the icon
1206 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1207 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1208 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1209 accounts[0]);
1210 } else {
1211 accountTile.fragment = ManageAccountsSettings.class.getName();
1212 accountTile.fragmentArguments = new Bundle();
1213 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1214 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1215 accountType);
1216 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1217 label.toString());
1218 }
1219 dashboardTiles.add(accountTile);
1220 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1221 }
1222
1223 // Sort by label
1224 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1225 @Override
1226 public int compare(DashboardTile t1, DashboardTile t2) {
1227 return t1.title.toString().compareTo(t2.title.toString());
1228 }
1229 });
1230 int index = 0;
1231 for (DashboardTile tile : dashboardTiles) {
1232 target.addTile(index, tile);
1233 index++;
1234 }
1235 if (!mListeningToAccountUpdates) {
1236 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1237 mListeningToAccountUpdates = true;
1238 }
1239 }
1240
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001241 private void getMetaData() {
1242 try {
1243 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1244 PackageManager.GET_META_DATA);
1245 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001246 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1247 } catch (NameNotFoundException nnfe) {
1248 // No recovery
1249 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1250 }
1251 }
1252
1253 // give subclasses access to the Next button
1254 public boolean hasNextButton() {
1255 return mNextButton != null;
1256 }
1257
1258 public Button getNextButton() {
1259 return mNextButton;
1260 }
1261
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001262 @Override
1263 public boolean shouldUpRecreateTask(Intent targetIntent) {
1264 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1265 }
1266
1267 @Override
1268 public void onAccountsUpdated(Account[] accounts) {
1269 // TODO: watch for package upgrades to invalidate cache; see 7206643
1270 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +01001271 invalidateCategories();
1272 }
1273
1274 @Override
1275 public void onAccountsUpdate(UserHandle userHandle) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001276 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001277 }
1278
1279 public static void requestHomeNotice() {
1280 sShowNoHomeNotice = true;
1281 }
1282
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001283 @Override
1284 public boolean onQueryTextSubmit(String query) {
1285 switchToSearchResultsFragmentIfNeeded();
1286 mSearchQuery = query;
1287 return mSearchResultsFragment.onQueryTextSubmit(query);
1288 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001289
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001290 @Override
1291 public boolean onQueryTextChange(String newText) {
1292 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001293 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001294 return false;
1295 }
1296 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001297 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001298
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001299 @Override
1300 public boolean onClose() {
1301 return false;
1302 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001303
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001304 @Override
1305 public boolean onMenuItemActionExpand(MenuItem item) {
1306 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001307 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001308 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001309 return true;
1310 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001311
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001312 @Override
1313 public boolean onMenuItemActionCollapse(MenuItem item) {
1314 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001315 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001316 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001317 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001318 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001319 return true;
1320 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001321
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001322 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001323 if (mSearchResultsFragment != null) {
1324 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001325 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001326 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1327 if (current != null && current instanceof SearchResultsSummary) {
1328 mSearchResultsFragment = (SearchResultsSummary) current;
1329 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001330 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001331 SearchResultsSummary.class.getName(), null, false, true,
1332 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001333 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001334 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001335 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001336 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001337
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001338 public void needToRevertToInitialFragment() {
1339 mNeedToRevertToInitialFragment = true;
1340 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001341
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001342 private void revertToInitialFragment() {
1343 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001344 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001345 mSearchMenuItemExpanded = false;
1346 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1347 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001348 if (mSearchMenuItem != null) {
1349 mSearchMenuItem.collapseActionView();
1350 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001351 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001352}