blob: 4675bb221343729d3108e2b47dc8a27e84c3829d [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;
PauloftheWestc9dc66d2014-07-01 05:19:58 -070079import com.android.settings.bluetooth.MessageAccessSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070080import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070082import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070083import com.android.settings.dashboard.NoHomeDialogFragment;
84import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085import com.android.settings.deviceinfo.Memory;
86import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040087import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088import com.android.settings.fuelgauge.PowerUsageSummary;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070089import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070090import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080091import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
92import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
93import com.android.settings.inputmethod.SpellCheckersSettings;
94import com.android.settings.inputmethod.UserDictionaryList;
95import com.android.settings.location.LocationSettings;
96import com.android.settings.nfc.AndroidBeam;
97import com.android.settings.nfc.PaymentSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040098import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040099import com.android.settings.notification.NotificationAccessSettings;
100import com.android.settings.notification.NotificationSettings;
101import com.android.settings.notification.NotificationStation;
102import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800103import com.android.settings.print.PrintJobSettingsFragment;
104import com.android.settings.print.PrintSettingsFragment;
105import com.android.settings.tts.TextToSpeechSettings;
106import com.android.settings.users.UserSettings;
107import com.android.settings.vpn2.VpnSettings;
108import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700109import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700111import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.wifi.WifiSettings;
113import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400114
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import org.xmlpull.v1.XmlPullParser;
116import org.xmlpull.v1.XmlPullParserException;
117
118import java.io.IOException;
119import java.util.ArrayList;
120import java.util.Collections;
121import java.util.Comparator;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700123import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700125import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700126
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127public class SettingsActivity extends Activity
128 implements PreferenceManager.OnPreferenceTreeClickListener,
129 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700130 ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
131 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100132 MenuItem.OnActionExpandListener,
133 AuthenticatorHelper.OnAccountsUpdateListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800134
135 private static final String LOG_TAG = "Settings";
136
137 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700138 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700139 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
140 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700141 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700142 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800143
144 /**
145 * When starting this activity, the invoking Intent can contain this extra
146 * string to specify which fragment should be initially displayed.
147 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
148 * will call isValidFragment() to confirm that the fragment class name is valid for this
149 * activity.
150 */
151 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
152
153 /**
154 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
155 * this extra can also be specified to supply a Bundle of arguments to pass
156 * to that fragment when it is instantiated during the initial creation
157 * of the activity.
158 */
159 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
160
161 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700162 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
163 */
164 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
165
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800166 public static final String BACK_STACK_PREFS = ":settings:prefs";
167
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 // extras that allow any preference activity to be launched as part of a wizard
169
170 // show Back and Next buttons? takes boolean parameter
171 // Back will then return RESULT_CANCELED and Next RESULT_OK
172 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
173
174 // add a Skip button?
175 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
176
177 // specify custom text for the Back or Next buttons, or cause a button to not appear
178 // at all by setting it to null
179 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
180 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
181
182 /**
183 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700184 * 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 -0800185 * that fragment.
186 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700187 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700188 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700189 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800190
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191 private static final String META_DATA_KEY_FRAGMENT_CLASS =
192 "com.android.settings.FRAGMENT_CLASS";
193
194 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
195
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700196 private static final String EMPTY_QUERY = "";
197
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198 private static boolean sShowNoHomeNotice = false;
199
200 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800202 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700203 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800204
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205 // Show only these settings for restricted users
206 private int[] SETTINGS_FOR_RESTRICTED = {
207 R.id.wireless_section,
208 R.id.wifi_settings,
209 R.id.bluetooth_settings,
210 R.id.data_usage_settings,
211 R.id.wireless_settings,
212 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400213 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800214 R.id.display_settings,
215 R.id.storage_settings,
216 R.id.application_settings,
217 R.id.battery_settings,
218 R.id.personal_section,
219 R.id.location_settings,
220 R.id.security_settings,
221 R.id.language_settings,
222 R.id.user_settings,
223 R.id.account_settings,
224 R.id.account_add,
225 R.id.system_section,
226 R.id.date_time_settings,
227 R.id.about_settings,
228 R.id.accessibility_settings,
229 R.id.print_settings,
230 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800231 R.id.home_settings,
232 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800233 };
234
235 private static final String[] ENTRY_FRAGMENTS = {
236 WirelessSettings.class.getName(),
237 WifiSettings.class.getName(),
238 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700239 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700241 MessageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800242 TetherSettings.class.getName(),
243 WifiP2pSettings.class.getName(),
244 VpnSettings.class.getName(),
245 DateTimeSettings.class.getName(),
246 LocalePicker.class.getName(),
247 InputMethodAndLanguageSettings.class.getName(),
248 SpellCheckersSettings.class.getName(),
249 UserDictionaryList.class.getName(),
250 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 DisplaySettings.class.getName(),
252 DeviceInfoSettings.class.getName(),
253 ManageApplications.class.getName(),
254 ProcessStatsUi.class.getName(),
255 NotificationStation.class.getName(),
256 LocationSettings.class.getName(),
257 SecuritySettings.class.getName(),
258 PrivacySettings.class.getName(),
259 DeviceAdminSettings.class.getName(),
260 AccessibilitySettings.class.getName(),
261 CaptionPropertiesFragment.class.getName(),
262 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800263 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
264 TextToSpeechSettings.class.getName(),
265 Memory.class.getName(),
266 DevelopmentSettings.class.getName(),
267 UsbSettings.class.getName(),
268 AndroidBeam.class.getName(),
269 WifiDisplaySettings.class.getName(),
270 PowerUsageSummary.class.getName(),
271 AccountSyncSettings.class.getName(),
272 CryptKeeperSettings.class.getName(),
273 DataUsageSummary.class.getName(),
274 DreamSettings.class.getName(),
275 UserSettings.class.getName(),
276 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400277 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 ManageAccountsSettings.class.getName(),
279 PrintSettingsFragment.class.getName(),
280 PrintJobSettingsFragment.class.getName(),
281 TrustedCredentialsSettings.class.getName(),
282 PaymentSettings.class.getName(),
283 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700284 ZenModeSettings.class.getName(),
285 NotificationSettings.class.getName(),
286 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
287 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400288 InstalledAppDetails.class.getName(),
289 BatterySaverSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 };
291
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700292
293 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
294 "android.settings.APPLICATION_DETAILS_SETTINGS"
295 };
296
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 private SharedPreferences mDevelopmentPreferences;
298 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
299
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 private AuthenticatorHelper mAuthenticatorHelper;
301 private boolean mListeningToAccountUpdates;
302
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 private boolean mBatteryPresent = true;
304 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
305
306 @Override
307 public void onReceive(Context context, Intent intent) {
308 String action = intent.getAction();
309 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
310 boolean batteryPresent = Utils.isBatteryPresent(intent);
311
312 if (mBatteryPresent != batteryPresent) {
313 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700314 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800315 }
316 }
317 }
318 };
319
Svetoslav990159a2014-04-14 17:14:59 -0700320 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
321 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700322
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700323 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700324 private SwitchBar mSwitchBar;
325
326 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700327
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700328 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700329 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700330
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700331 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700332 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700333
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700334 private ViewGroup mContent;
335
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700336 private SearchView mSearchView;
337 private MenuItem mSearchMenuItem;
338 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700339 private SearchResultsSummary mSearchResultsFragment;
340 private String mSearchQuery;
341
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700342 // Categories
343 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
344 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700346 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 private Handler mHandler = new Handler() {
348 @Override
349 public void handleMessage(Message msg) {
350 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700351 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700352 if(mNeedToRebuildCategories) {
353 buildDashboardCategories(mCategories);
354 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 } break;
356 }
357 }
358 };
359
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700360 private boolean mNeedToRevertToInitialFragment = false;
361
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700362 public SwitchBar getSwitchBar() {
363 return mSwitchBar;
364 }
365
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700366 public AuthenticatorHelper getAuthenticatorHelper() {
367 return mAuthenticatorHelper;
368 }
369
370 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700371 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700372 buildDashboardCategories(mCategories);
373 mNeedToRebuildCategories = false;
374 }
375 return mCategories;
376 }
377
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800378 @Override
379 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
380 // Override the fragment title for Wallpaper settings
381 int titleRes = pref.getTitleRes();
382 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
383 titleRes = R.string.wallpaper_settings_fragment_title;
384 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
385 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
386 if (UserManager.get(this).isLinkedUser()) {
387 titleRes = R.string.profile_info_settings_title;
388 } else {
389 titleRes = R.string.user_info_settings_title;
390 }
391 }
392 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
393 null, 0);
394 return true;
395 }
396
397 @Override
398 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
399 return false;
400 }
401
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700402 private void invalidateCategories() {
403 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
404 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800405 }
406 }
407
408 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800409 public void onConfigurationChanged(Configuration newConfig) {
410 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800411 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800412 }
413
414 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700415 protected void onStart() {
416 super.onStart();
417
418 if (mNeedToRevertToInitialFragment) {
419 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800420 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800421 }
422
423 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700424 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700425 if (!mDisplaySearch) {
426 return false;
427 }
428
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700429 MenuInflater inflater = getMenuInflater();
430 inflater.inflate(R.menu.options_menu, menu);
431
432 // Cache the search query (can be overriden by the OnQueryTextListener)
433 final String query = mSearchQuery;
434
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700435 mSearchMenuItem = menu.findItem(R.id.search);
436 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700437
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700438 if (mSearchMenuItem == null || mSearchView == null) {
439 return false;
440 }
441
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700442 if (mSearchResultsFragment != null) {
443 mSearchResultsFragment.setSearchView(mSearchView);
444 }
445
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700446 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700447 mSearchView.setOnQueryTextListener(this);
448 mSearchView.setOnCloseListener(this);
449
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700450 if (mSearchMenuItemExpanded) {
451 mSearchMenuItem.expandActionView();
452 }
453 mSearchView.setQuery(query, true /* submit */);
454
455 return true;
456 }
457
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700458 private static boolean isShortCutIntent(final Intent intent) {
459 Set<String> categories = intent.getCategories();
460 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
461 }
462
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700463 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700464 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700465 if (action == null) {
466 return false;
467 }
468 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
469 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
470 }
471 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700472 }
473
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700474 @Override
475 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700476 super.onCreate(savedState);
477
478 // Should happen before any call to getIntent()
479 getMetaData();
480
481 final Intent intent = getIntent();
482 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
483 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800484 }
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100485 // TODO: Delete accounts tile once we have the new screen working
486 // See: http://b/15815948
487 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
488 mAuthenticatorHelper = new AuthenticatorHelper(
489 this, UserHandle.getCallingUserHandle(), um, this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100491 mAuthenticatorHelper.onAccountsUpdated(null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800493 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
494 Context.MODE_PRIVATE);
495
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700496 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700497 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700498
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700499 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700500 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
501
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700502 final ComponentName cn = getIntent().getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700503 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700504
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700505 mIsShowingDashboard = className.equals(Settings.class.getName());
506 final boolean isSubSettings = className.equals(SubSettings.class.getName());
507
508 // If this is a sub settings or not the main Dashboard and not a Shortcut and not initial
509 // Fragment then apply the correct theme for the ActionBar content inset
510 if (isSubSettings ||
511 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName == null))) {
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700512 setTheme(R.style.Theme_SubSettings);
513 }
514
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515 setContentView(R.layout.settings_main);
516
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700517 mContent = (ViewGroup) findViewById(R.id.prefs);
518
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800519 getFragmentManager().addOnBackStackChangedListener(this);
520
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700521 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700522 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700523 }
524
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700525 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700526 // We are restarting from a previous saved state; used that to initialize, instead
527 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700528 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
529 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700531 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800532
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700533 ArrayList<DashboardCategory> categories =
534 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
535 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700536 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700537 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700538 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700540
541 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700542 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700544 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700545 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
546 // only if it is a sub settings
547 if (mIsShortcut) {
548 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700549 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700550 } else if (isSubSettings) {
551 mDisplayHomeAsUpEnabled = true;
552 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700553 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700554 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700555
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700556 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700557 switchToFragment(initialFragmentName, initialArguments, true, false,
558 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000559 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700560 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700561 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700562 // Show Search affordance
563 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700564 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700565 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700566 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 }
568 }
569
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700570 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700571 if (mActionBar != null) {
572 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
573 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
574 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700575 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
576
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800578 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
579
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700580 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 if (buttonBar != null) {
582 buttonBar.setVisibility(View.VISIBLE);
583
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700584 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 backButton.setOnClickListener(new OnClickListener() {
586 public void onClick(View v) {
587 setResult(RESULT_CANCELED);
588 finish();
589 }
590 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700591 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800592 skipButton.setOnClickListener(new OnClickListener() {
593 public void onClick(View v) {
594 setResult(RESULT_OK);
595 finish();
596 }
597 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700598 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 mNextButton.setOnClickListener(new OnClickListener() {
600 public void onClick(View v) {
601 setResult(RESULT_OK);
602 finish();
603 }
604 });
605
606 // set our various button parameters
607 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
608 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
609 if (TextUtils.isEmpty(buttonText)) {
610 mNextButton.setVisibility(View.GONE);
611 }
612 else {
613 mNextButton.setText(buttonText);
614 }
615 }
616 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
617 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
618 if (TextUtils.isEmpty(buttonText)) {
619 backButton.setVisibility(View.GONE);
620 }
621 else {
622 backButton.setText(buttonText);
623 }
624 }
625 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
626 skipButton.setVisibility(View.VISIBLE);
627 }
628 }
629 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800630 }
631
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700632 private void setTitleFromIntent(Intent intent) {
633 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
634 if (initialTitleResId > 0) {
635 mInitialTitle = null;
636 mInitialTitleResId = initialTitleResId;
637 setTitle(mInitialTitleResId);
638 } else {
639 mInitialTitleResId = -1;
640 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
641 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
642 setTitle(mInitialTitle);
643 }
644 }
645
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800646 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700648 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 }
650
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700651 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800652 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700653
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800654 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700655 if (mInitialTitleResId > 0) {
656 setTitle(mInitialTitleResId);
657 } else {
658 setTitle(mInitialTitle);
659 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700660 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800661 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700662
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800663 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
664 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700665
666 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800667 }
668
669 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
670 final CharSequence title;
671 final int titleRes = bse.getBreadCrumbTitleRes();
672 if (titleRes > 0) {
673 title = getText(titleRes);
674 } else {
675 title = bse.getBreadCrumbTitle();
676 }
677 if (title != null) {
678 setTitle(title);
679 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800680 }
681
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800682 @Override
683 protected void onSaveInstanceState(Bundle outState) {
684 super.onSaveInstanceState(outState);
685
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700686 if (mCategories.size() > 0) {
687 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800688 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700689
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700690 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700691 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700692
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700693 if (mDisplaySearch) {
694 // The option menus are created if the ActionBar is visible and they are also created
695 // asynchronously. If you launch Settings with an Intent action like
696 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
697 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
698 // menu item and search view are null.
699 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
700 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700701
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700702 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
703 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
704 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800705 }
706
707 @Override
708 public void onResume() {
709 super.onResume();
710
711 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
712 @Override
713 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700714 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700715 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800716 }
717 };
718 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
719 mDevelopmentPreferencesListener);
720
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800721 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700722
Svetoslav990159a2014-04-14 17:14:59 -0700723 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700724
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700725 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700726 onQueryTextSubmit(mSearchQuery);
727 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800728 }
729
730 @Override
731 public void onPause() {
732 super.onPause();
733
734 unregisterReceiver(mBatteryInfoReceiver);
735
Svetoslav990159a2014-04-14 17:14:59 -0700736 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800737 }
738
739 @Override
740 public void onDestroy() {
741 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700742
743 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
744 mDevelopmentPreferencesListener);
745 mDevelopmentPreferencesListener = null;
746
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800747 if (mListeningToAccountUpdates) {
748 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
749 }
750 }
751
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800752 protected boolean isValidFragment(String fragmentName) {
753 // Almost all fragments are wrapped in this,
754 // except for a few that have their own activities.
755 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
756 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
757 }
758 return false;
759 }
760
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800761 @Override
762 public Intent getIntent() {
763 Intent superIntent = super.getIntent();
764 String startingFragment = getStartingFragmentClass(superIntent);
765 // This is called from super.onCreate, isMultiPane() is not yet reliable
766 // Do not use onIsHidingHeaders either, which relies itself on this method
767 if (startingFragment != null) {
768 Intent modIntent = new Intent(superIntent);
769 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
770 Bundle args = superIntent.getExtras();
771 if (args != null) {
772 args = new Bundle(args);
773 } else {
774 args = new Bundle();
775 }
776 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100777 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800778 return modIntent;
779 }
780 return superIntent;
781 }
782
783 /**
784 * Checks if the component name in the intent is different from the Settings class and
785 * returns the class name to load as a fragment.
786 */
787 private String getStartingFragmentClass(Intent intent) {
788 if (mFragmentClass != null) return mFragmentClass;
789
790 String intentClass = intent.getComponent().getClassName();
791 if (intentClass.equals(getClass().getName())) return null;
792
793 if ("com.android.settings.ManageApplications".equals(intentClass)
794 || "com.android.settings.RunningServices".equals(intentClass)
795 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
796 // Old names of manage apps.
797 intentClass = com.android.settings.applications.ManageApplications.class.getName();
798 }
799
800 return intentClass;
801 }
802
803 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000804 * Start a new fragment containing a preference panel. If the preferences
805 * are being displayed in multi-pane mode, the given fragment class will
806 * be instantiated and placed in the appropriate pane. If running in
807 * single-pane mode, a new activity will be launched in which to show the
808 * fragment.
809 *
810 * @param fragmentClass Full name of the class implementing the fragment.
811 * @param args Any desired arguments to supply to the fragment.
812 * @param titleRes Optional resource identifier of the title of this
813 * fragment.
814 * @param titleText Optional text of the title of this fragment.
815 * @param resultTo Optional fragment that result data should be sent to.
816 * If non-null, resultTo.onActivityResult() will be called when this
817 * preference panel is done. The launched panel must use
818 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
819 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700820 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000821 */
822 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700823 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700824 String title = null;
825 if (titleRes < 0) {
826 if (titleText != null) {
827 title = titleText.toString();
828 } else {
829 // There not much we can do in that case
830 title = "";
831 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700832 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700833 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700834 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000835 }
836
837 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800838 * Called by a preference panel fragment to finish itself.
839 *
840 * @param caller The fragment that is asking to be finished.
841 * @param resultCode Optional result code to send back to the original
842 * launching fragment.
843 * @param resultData Optional result data to send back to the original
844 * launching fragment.
845 */
846 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
847 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700848 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800849 }
850
851 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000852 * Start a new fragment.
853 *
854 * @param fragment The fragment to start
855 * @param push If true, the current fragment will be pushed onto the back stack. If false,
856 * the current fragment will be replaced.
857 */
858 public void startPreferenceFragment(Fragment fragment, boolean push) {
859 FragmentTransaction transaction = getFragmentManager().beginTransaction();
860 transaction.replace(R.id.prefs, fragment);
861 if (push) {
862 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
863 transaction.addToBackStack(BACK_STACK_PREFS);
864 } else {
865 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
866 }
867 transaction.commitAllowingStateLoss();
868 }
869
870 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700871 * Switch to a specific Fragment with taking care of validation, Title and BackStack
872 */
873 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700874 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700875 if (validate && !isValidFragment(fragmentName)) {
876 throw new IllegalArgumentException("Invalid fragment for this activity: "
877 + fragmentName);
878 }
879 Fragment f = Fragment.instantiate(this, fragmentName, args);
880 FragmentTransaction transaction = getFragmentManager().beginTransaction();
881 transaction.replace(R.id.prefs, f);
882 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700883 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700884 }
885 if (addToBackStack) {
886 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
887 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700888 if (titleResId > 0) {
889 transaction.setBreadCrumbTitle(titleResId);
890 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700891 transaction.setBreadCrumbTitle(title);
892 }
893 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700894 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700895 return f;
896 }
897
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700898 public void setNeedToRebuildCategories(boolean need) {
899 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700900 }
901
902 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700903 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700904 *
905 * @param categories The list in which to place the tiles categories.
906 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700907 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700908 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700909 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
910 updateTilesList(categories);
911 }
912
913 /**
914 * Parse the given XML file as a categories description, adding each
915 * parsed categories and tiles into the target list.
916 *
917 * @param resid The XML resource to load and parse.
918 * @param target The list in which the parsed categories and tiles should be placed.
919 */
920 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
921 XmlResourceParser parser = null;
922 try {
923 parser = getResources().getXml(resid);
924 AttributeSet attrs = Xml.asAttributeSet(parser);
925
926 int type;
927 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
928 && type != XmlPullParser.START_TAG) {
929 // Parse next until start tag is found
930 }
931
932 String nodeName = parser.getName();
933 if (!"dashboard-categories".equals(nodeName)) {
934 throw new RuntimeException(
935 "XML document must start with <preference-categories> tag; found"
936 + nodeName + " at " + parser.getPositionDescription());
937 }
938
939 Bundle curBundle = null;
940
941 final int outerDepth = parser.getDepth();
942 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
943 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
944 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
945 continue;
946 }
947
948 nodeName = parser.getName();
949 if ("dashboard-category".equals(nodeName)) {
950 DashboardCategory category = new DashboardCategory();
951
952 TypedArray sa = obtainStyledAttributes(
953 attrs, com.android.internal.R.styleable.PreferenceHeader);
954 category.id = sa.getResourceId(
955 com.android.internal.R.styleable.PreferenceHeader_id,
956 (int)DashboardCategory.CAT_ID_UNDEFINED);
957
958 TypedValue tv = sa.peekValue(
959 com.android.internal.R.styleable.PreferenceHeader_title);
960 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
961 if (tv.resourceId != 0) {
962 category.titleRes = tv.resourceId;
963 } else {
964 category.title = tv.string;
965 }
966 }
967 sa.recycle();
968
969 final int innerDepth = parser.getDepth();
970 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
971 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
972 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
973 continue;
974 }
975
976 String innerNodeName = parser.getName();
977 if (innerNodeName.equals("dashboard-tile")) {
978 DashboardTile tile = new DashboardTile();
979
980 sa = obtainStyledAttributes(
981 attrs, com.android.internal.R.styleable.PreferenceHeader);
982 tile.id = sa.getResourceId(
983 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700984 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700985 tv = sa.peekValue(
986 com.android.internal.R.styleable.PreferenceHeader_title);
987 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
988 if (tv.resourceId != 0) {
989 tile.titleRes = tv.resourceId;
990 } else {
991 tile.title = tv.string;
992 }
993 }
994 tv = sa.peekValue(
995 com.android.internal.R.styleable.PreferenceHeader_summary);
996 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
997 if (tv.resourceId != 0) {
998 tile.summaryRes = tv.resourceId;
999 } else {
1000 tile.summary = tv.string;
1001 }
1002 }
1003 tile.iconRes = sa.getResourceId(
1004 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1005 tile.fragment = sa.getString(
1006 com.android.internal.R.styleable.PreferenceHeader_fragment);
1007 sa.recycle();
1008
1009 if (curBundle == null) {
1010 curBundle = new Bundle();
1011 }
1012
1013 final int innerDepth2 = parser.getDepth();
1014 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1015 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1016 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1017 continue;
1018 }
1019
1020 String innerNodeName2 = parser.getName();
1021 if (innerNodeName2.equals("extra")) {
1022 getResources().parseBundleExtra("extra", attrs, curBundle);
1023 XmlUtils.skipCurrentTag(parser);
1024
1025 } else if (innerNodeName2.equals("intent")) {
1026 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1027
1028 } else {
1029 XmlUtils.skipCurrentTag(parser);
1030 }
1031 }
1032
1033 if (curBundle.size() > 0) {
1034 tile.fragmentArguments = curBundle;
1035 curBundle = null;
1036 }
1037
1038 category.addTile(tile);
1039
1040 } else {
1041 XmlUtils.skipCurrentTag(parser);
1042 }
1043 }
1044
1045 target.add(category);
1046 } else {
1047 XmlUtils.skipCurrentTag(parser);
1048 }
1049 }
1050
1051 } catch (XmlPullParserException e) {
1052 throw new RuntimeException("Error parsing categories", e);
1053 } catch (IOException e) {
1054 throw new RuntimeException("Error parsing categories", e);
1055 } finally {
1056 if (parser != null) parser.close();
1057 }
1058 }
1059
1060 private void updateTilesList(List<DashboardCategory> target) {
1061 final boolean showDev = mDevelopmentPreferences.getBoolean(
1062 DevelopmentSettings.PREF_SHOW,
1063 android.os.Build.TYPE.equals("eng"));
1064
1065 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1066
1067 final int size = target.size();
1068 for (int i = 0; i < size; i++) {
1069
1070 DashboardCategory category = target.get(i);
1071
1072 // Ids are integers, so downcasting is ok
1073 int id = (int) category.id;
1074 if (id == R.id.account_settings) {
1075 insertAccountsTiles(category);
1076 continue;
1077 }
1078 int n = category.getTilesCount() - 1;
1079 while (n >= 0) {
1080
1081 DashboardTile tile = category.getTile(n);
1082
1083 id = (int) tile.id;
1084 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1085 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1086 } else if (id == R.id.wifi_settings) {
1087 // Remove WiFi Settings if WiFi service is not available.
1088 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1089 category.removeTile(n);
1090 }
1091 } else if (id == R.id.bluetooth_settings) {
1092 // Remove Bluetooth Settings if Bluetooth service is not available.
1093 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1094 category.removeTile(n);
1095 }
1096 } else if (id == R.id.data_usage_settings) {
1097 // Remove data usage when kernel module not enabled
1098 final INetworkManagementService netManager = INetworkManagementService.Stub
1099 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1100 try {
1101 if (!netManager.isBandwidthControlEnabled()) {
1102 category.removeTile(n);
1103 }
1104 } catch (RemoteException e) {
1105 // ignored
1106 }
1107 } else if (id == R.id.battery_settings) {
1108 // Remove battery settings when battery is not available. (e.g. TV)
1109
1110 if (!mBatteryPresent) {
1111 category.removeTile(n);
1112 }
1113 } else if (id == R.id.home_settings) {
1114 if (!updateHomeSettingTiles(tile)) {
1115 category.removeTile(n);
1116 }
1117 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001118 boolean hasMultipleUsers =
1119 ((UserManager) getSystemService(Context.USER_SERVICE))
1120 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001121 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001122 || (!UserManager.supportsMultipleUsers()
1123 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001124 || Utils.isMonkeyRunning()) {
1125 category.removeTile(n);
1126 }
1127 } else if (id == R.id.nfc_payment_settings) {
1128 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1129 category.removeTile(n);
1130 } else {
1131 // Only show if NFC is on and we have the HCE feature
1132 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1133 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1134 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1135 category.removeTile(n);
1136 }
1137 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001138 } else if (id == R.id.print_settings) {
1139 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1140 PackageManager.FEATURE_PRINTING);
1141 if (!hasPrintingSupport) {
1142 category.removeTile(n);
1143 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001144 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001145 if (!showDev || um.hasUserRestriction(
1146 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001147 category.removeTile(n);
1148 }
1149 } else if (id == R.id.account_add) {
1150 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1151 category.removeTile(n);
1152 }
1153 }
1154
1155 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001156 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1157 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001158 category.removeTile(n);
1159 }
1160
1161 n--;
1162 }
1163 }
1164 }
1165
1166 private boolean updateHomeSettingTiles(DashboardTile tile) {
1167 // Once we decide to show Home settings, keep showing it forever
1168 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1169 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1170 return true;
1171 }
1172
1173 try {
1174 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1175 getPackageManager().getHomeActivities(homeApps);
1176 if (homeApps.size() < 2) {
1177 // When there's only one available home app, omit this settings
1178 // category entirely at the top level UI. If the user just
1179 // uninstalled the penultimate home app candidiate, we also
1180 // now tell them about why they aren't seeing 'Home' in the list.
1181 if (sShowNoHomeNotice) {
1182 sShowNoHomeNotice = false;
1183 NoHomeDialogFragment.show(this);
1184 }
1185 return false;
1186 } else {
1187 // Okay, we're allowing the Home settings category. Tell it, when
1188 // invoked via this front door, that we'll need to be told about the
1189 // case when the user uninstalls all but one home app.
1190 if (tile.fragmentArguments == null) {
1191 tile.fragmentArguments = new Bundle();
1192 }
1193 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1194 }
1195 } catch (Exception e) {
1196 // Can't look up the home activity; bail on configuring the icon
1197 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1198 }
1199
1200 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1201 return true;
1202 }
1203
1204 private void insertAccountsTiles(DashboardCategory target) {
1205 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1206 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1207 for (String accountType : accountTypes) {
1208 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1209 if (label == null) {
1210 continue;
1211 }
1212
1213 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1214 boolean skipToAccount = accounts.length == 1
1215 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1216 DashboardTile accountTile = new DashboardTile();
1217 accountTile.title = label;
1218 if (accountTile.extras == null) {
1219 accountTile.extras = new Bundle();
1220 }
1221 if (skipToAccount) {
1222 accountTile.fragment = AccountSyncSettings.class.getName();
1223 accountTile.fragmentArguments = new Bundle();
1224 // Need this for the icon
1225 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1226 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1227 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1228 accounts[0]);
1229 } else {
1230 accountTile.fragment = ManageAccountsSettings.class.getName();
1231 accountTile.fragmentArguments = new Bundle();
1232 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1233 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1234 accountType);
1235 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1236 label.toString());
1237 }
1238 dashboardTiles.add(accountTile);
1239 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1240 }
1241
1242 // Sort by label
1243 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1244 @Override
1245 public int compare(DashboardTile t1, DashboardTile t2) {
1246 return t1.title.toString().compareTo(t2.title.toString());
1247 }
1248 });
1249 int index = 0;
1250 for (DashboardTile tile : dashboardTiles) {
1251 target.addTile(index, tile);
1252 index++;
1253 }
1254 if (!mListeningToAccountUpdates) {
1255 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1256 mListeningToAccountUpdates = true;
1257 }
1258 }
1259
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001260 private void getMetaData() {
1261 try {
1262 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1263 PackageManager.GET_META_DATA);
1264 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001265 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1266 } catch (NameNotFoundException nnfe) {
1267 // No recovery
1268 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1269 }
1270 }
1271
1272 // give subclasses access to the Next button
1273 public boolean hasNextButton() {
1274 return mNextButton != null;
1275 }
1276
1277 public Button getNextButton() {
1278 return mNextButton;
1279 }
1280
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001281 @Override
1282 public boolean shouldUpRecreateTask(Intent targetIntent) {
1283 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1284 }
1285
1286 @Override
1287 public void onAccountsUpdated(Account[] accounts) {
1288 // TODO: watch for package upgrades to invalidate cache; see 7206643
1289 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +01001290 invalidateCategories();
1291 }
1292
1293 @Override
1294 public void onAccountsUpdate(UserHandle userHandle) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001295 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001296 }
1297
1298 public static void requestHomeNotice() {
1299 sShowNoHomeNotice = true;
1300 }
1301
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001302 @Override
1303 public boolean onQueryTextSubmit(String query) {
1304 switchToSearchResultsFragmentIfNeeded();
1305 mSearchQuery = query;
1306 return mSearchResultsFragment.onQueryTextSubmit(query);
1307 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001308
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001309 @Override
1310 public boolean onQueryTextChange(String newText) {
1311 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001312 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001313 return false;
1314 }
1315 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001316 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001317
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001318 @Override
1319 public boolean onClose() {
1320 return false;
1321 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001322
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001323 @Override
1324 public boolean onMenuItemActionExpand(MenuItem item) {
1325 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001326 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001327 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001328 return true;
1329 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001330
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001331 @Override
1332 public boolean onMenuItemActionCollapse(MenuItem item) {
1333 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001334 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001335 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001336 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001337 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001338 return true;
1339 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001340
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001341 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001342 if (mSearchResultsFragment != null) {
1343 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001344 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001345 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1346 if (current != null && current instanceof SearchResultsSummary) {
1347 mSearchResultsFragment = (SearchResultsSummary) current;
1348 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001349 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001350 SearchResultsSummary.class.getName(), null, false, true,
1351 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001352 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001353 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001354 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001355 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001356
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001357 public void needToRevertToInitialFragment() {
1358 mNeedToRevertToInitialFragment = true;
1359 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001360
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001361 private void revertToInitialFragment() {
1362 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001363 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001364 mSearchMenuItemExpanded = false;
1365 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1366 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001367 if (mSearchMenuItem != null) {
1368 mSearchMenuItem.collapseActionView();
1369 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001370 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001371}