blob: b0d3241098ba4d5f748da32ff6f0fd62b8597c8c [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;
PauloftheWest7837b992014-06-24 07:42:27 -0700110import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.WifiSettings;
112import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400113
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import org.xmlpull.v1.XmlPullParser;
115import org.xmlpull.v1.XmlPullParserException;
116
117import java.io.IOException;
118import java.util.ArrayList;
119import java.util.Collections;
120import java.util.Comparator;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700122import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700124import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700125
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126public class SettingsActivity extends Activity
127 implements PreferenceManager.OnPreferenceTreeClickListener,
128 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700129 ButtonBarHandler, OnAccountsUpdateListener, FragmentManager.OnBackStackChangedListener,
130 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100131 MenuItem.OnActionExpandListener,
132 AuthenticatorHelper.OnAccountsUpdateListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133
134 private static final String LOG_TAG = "Settings";
135
136 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700137 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
139 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700140 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700141 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
143 /**
144 * When starting this activity, the invoking Intent can contain this extra
145 * string to specify which fragment should be initially displayed.
146 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
147 * will call isValidFragment() to confirm that the fragment class name is valid for this
148 * activity.
149 */
150 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
151
152 /**
153 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
154 * this extra can also be specified to supply a Bundle of arguments to pass
155 * to that fragment when it is instantiated during the initial creation
156 * of the activity.
157 */
158 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
159
160 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700161 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
162 */
163 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
164
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800165 public static final String BACK_STACK_PREFS = ":settings:prefs";
166
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167 // extras that allow any preference activity to be launched as part of a wizard
168
169 // show Back and Next buttons? takes boolean parameter
170 // Back will then return RESULT_CANCELED and Next RESULT_OK
171 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
172
173 // add a Skip button?
174 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
175
176 // specify custom text for the Back or Next buttons, or cause a button to not appear
177 // at all by setting it to null
178 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
179 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
180
181 /**
182 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700183 * 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 -0800184 * that fragment.
185 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700187 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700188 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800189
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800190 private static final String META_DATA_KEY_FRAGMENT_CLASS =
191 "com.android.settings.FRAGMENT_CLASS";
192
193 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
194
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700195 private static final String EMPTY_QUERY = "";
196
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800197 private static boolean sShowNoHomeNotice = false;
198
199 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800201 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700202 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800203
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204 // Show only these settings for restricted users
205 private int[] SETTINGS_FOR_RESTRICTED = {
206 R.id.wireless_section,
207 R.id.wifi_settings,
208 R.id.bluetooth_settings,
209 R.id.data_usage_settings,
210 R.id.wireless_settings,
211 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400212 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800213 R.id.display_settings,
214 R.id.storage_settings,
215 R.id.application_settings,
216 R.id.battery_settings,
217 R.id.personal_section,
218 R.id.location_settings,
219 R.id.security_settings,
220 R.id.language_settings,
221 R.id.user_settings,
222 R.id.account_settings,
223 R.id.account_add,
224 R.id.system_section,
225 R.id.date_time_settings,
226 R.id.about_settings,
227 R.id.accessibility_settings,
228 R.id.print_settings,
229 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800230 R.id.home_settings,
231 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800232 };
233
234 private static final String[] ENTRY_FRAGMENTS = {
235 WirelessSettings.class.getName(),
236 WifiSettings.class.getName(),
237 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700238 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800239 BluetoothSettings.class.getName(),
240 TetherSettings.class.getName(),
241 WifiP2pSettings.class.getName(),
242 VpnSettings.class.getName(),
243 DateTimeSettings.class.getName(),
244 LocalePicker.class.getName(),
245 InputMethodAndLanguageSettings.class.getName(),
246 SpellCheckersSettings.class.getName(),
247 UserDictionaryList.class.getName(),
248 UserDictionarySettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800249 DisplaySettings.class.getName(),
250 DeviceInfoSettings.class.getName(),
251 ManageApplications.class.getName(),
252 ProcessStatsUi.class.getName(),
253 NotificationStation.class.getName(),
254 LocationSettings.class.getName(),
255 SecuritySettings.class.getName(),
256 PrivacySettings.class.getName(),
257 DeviceAdminSettings.class.getName(),
258 AccessibilitySettings.class.getName(),
259 CaptionPropertiesFragment.class.getName(),
260 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
262 TextToSpeechSettings.class.getName(),
263 Memory.class.getName(),
264 DevelopmentSettings.class.getName(),
265 UsbSettings.class.getName(),
266 AndroidBeam.class.getName(),
267 WifiDisplaySettings.class.getName(),
268 PowerUsageSummary.class.getName(),
269 AccountSyncSettings.class.getName(),
270 CryptKeeperSettings.class.getName(),
271 DataUsageSummary.class.getName(),
272 DreamSettings.class.getName(),
273 UserSettings.class.getName(),
274 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400275 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800276 ManageAccountsSettings.class.getName(),
277 PrintSettingsFragment.class.getName(),
278 PrintJobSettingsFragment.class.getName(),
279 TrustedCredentialsSettings.class.getName(),
280 PaymentSettings.class.getName(),
281 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700282 ZenModeSettings.class.getName(),
283 NotificationSettings.class.getName(),
284 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
285 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400286 InstalledAppDetails.class.getName(),
287 BatterySaverSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 };
289
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700290
291 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
292 "android.settings.APPLICATION_DETAILS_SETTINGS"
293 };
294
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800295 private SharedPreferences mDevelopmentPreferences;
296 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
297
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 private AuthenticatorHelper mAuthenticatorHelper;
299 private boolean mListeningToAccountUpdates;
300
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 private boolean mBatteryPresent = true;
302 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
303
304 @Override
305 public void onReceive(Context context, Intent intent) {
306 String action = intent.getAction();
307 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
308 boolean batteryPresent = Utils.isBatteryPresent(intent);
309
310 if (mBatteryPresent != batteryPresent) {
311 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700312 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 }
314 }
315 }
316 };
317
Svetoslav990159a2014-04-14 17:14:59 -0700318 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
319 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700320
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700321 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700322 private SwitchBar mSwitchBar;
323
324 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700325
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700326 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700327 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700328
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700329 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700330 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700331
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700332 private ViewGroup mContent;
333
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700334 private SearchView mSearchView;
335 private MenuItem mSearchMenuItem;
336 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700337 private SearchResultsSummary mSearchResultsFragment;
338 private String mSearchQuery;
339
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700340 // Categories
341 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
342 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700344 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 private Handler mHandler = new Handler() {
346 @Override
347 public void handleMessage(Message msg) {
348 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700349 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700350 if(mNeedToRebuildCategories) {
351 buildDashboardCategories(mCategories);
352 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 } break;
354 }
355 }
356 };
357
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700358 private boolean mNeedToRevertToInitialFragment = false;
359
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700360 public SwitchBar getSwitchBar() {
361 return mSwitchBar;
362 }
363
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700364 public AuthenticatorHelper getAuthenticatorHelper() {
365 return mAuthenticatorHelper;
366 }
367
368 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700369 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700370 buildDashboardCategories(mCategories);
371 mNeedToRebuildCategories = false;
372 }
373 return mCategories;
374 }
375
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800376 @Override
377 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
378 // Override the fragment title for Wallpaper settings
379 int titleRes = pref.getTitleRes();
380 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
381 titleRes = R.string.wallpaper_settings_fragment_title;
382 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
383 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
384 if (UserManager.get(this).isLinkedUser()) {
385 titleRes = R.string.profile_info_settings_title;
386 } else {
387 titleRes = R.string.user_info_settings_title;
388 }
389 }
390 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
391 null, 0);
392 return true;
393 }
394
395 @Override
396 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
397 return false;
398 }
399
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700400 private void invalidateCategories() {
401 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
402 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800403 }
404 }
405
406 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800407 public void onConfigurationChanged(Configuration newConfig) {
408 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800409 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 }
411
412 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700413 protected void onStart() {
414 super.onStart();
415
416 if (mNeedToRevertToInitialFragment) {
417 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800418 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800419 }
420
421 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700422 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700423 if (!mDisplaySearch) {
424 return false;
425 }
426
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700427 MenuInflater inflater = getMenuInflater();
428 inflater.inflate(R.menu.options_menu, menu);
429
430 // Cache the search query (can be overriden by the OnQueryTextListener)
431 final String query = mSearchQuery;
432
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700433 mSearchMenuItem = menu.findItem(R.id.search);
434 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700435
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700436 if (mSearchMenuItem == null || mSearchView == null) {
437 return false;
438 }
439
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700440 if (mSearchResultsFragment != null) {
441 mSearchResultsFragment.setSearchView(mSearchView);
442 }
443
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700444 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700445 mSearchView.setOnQueryTextListener(this);
446 mSearchView.setOnCloseListener(this);
447
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700448 if (mSearchMenuItemExpanded) {
449 mSearchMenuItem.expandActionView();
450 }
451 mSearchView.setQuery(query, true /* submit */);
452
453 return true;
454 }
455
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700456 private static boolean isShortCutIntent(final Intent intent) {
457 Set<String> categories = intent.getCategories();
458 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
459 }
460
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700461 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700462 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700463 if (action == null) {
464 return false;
465 }
466 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
467 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
468 }
469 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700470 }
471
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700472 @Override
473 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700474 super.onCreate(savedState);
475
476 // Should happen before any call to getIntent()
477 getMetaData();
478
479 final Intent intent = getIntent();
480 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
481 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800482 }
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100483 // TODO: Delete accounts tile once we have the new screen working
484 // See: http://b/15815948
485 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
486 mAuthenticatorHelper = new AuthenticatorHelper(
487 this, UserHandle.getCallingUserHandle(), um, this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +0100489 mAuthenticatorHelper.onAccountsUpdated(null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800491 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
492 Context.MODE_PRIVATE);
493
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700494 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700495 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700496
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700497 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700498 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
499
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700500 final ComponentName cn = getIntent().getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700501 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700502
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700503 mIsShowingDashboard = className.equals(Settings.class.getName());
504 final boolean isSubSettings = className.equals(SubSettings.class.getName());
505
506 // If this is a sub settings or not the main Dashboard and not a Shortcut and not initial
507 // Fragment then apply the correct theme for the ActionBar content inset
508 if (isSubSettings ||
509 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName == null))) {
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700510 setTheme(R.style.Theme_SubSettings);
511 }
512
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800513 setContentView(R.layout.settings_main);
514
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700515 mContent = (ViewGroup) findViewById(R.id.prefs);
516
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800517 getFragmentManager().addOnBackStackChangedListener(this);
518
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700519 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700520 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700521 }
522
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700523 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700524 // We are restarting from a previous saved state; used that to initialize, instead
525 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700526 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
527 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800528
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700529 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800530
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700531 ArrayList<DashboardCategory> categories =
532 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
533 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700534 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700535 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700536 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700538
539 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700540 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700542 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700543 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
544 // only if it is a sub settings
545 if (mIsShortcut) {
546 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700547 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700548 } else if (isSubSettings) {
549 mDisplayHomeAsUpEnabled = true;
550 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700551 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700552 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700553
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700554 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700555 switchToFragment(initialFragmentName, initialArguments, true, false,
556 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000557 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700558 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700559 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700560 // Show Search affordance
561 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700562 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700563 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700564 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800565 }
566 }
567
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700568 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700569 if (mActionBar != null) {
570 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
571 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
572 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700573 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
574
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
577
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700578 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579 if (buttonBar != null) {
580 buttonBar.setVisibility(View.VISIBLE);
581
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700582 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800583 backButton.setOnClickListener(new OnClickListener() {
584 public void onClick(View v) {
585 setResult(RESULT_CANCELED);
586 finish();
587 }
588 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700589 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590 skipButton.setOnClickListener(new OnClickListener() {
591 public void onClick(View v) {
592 setResult(RESULT_OK);
593 finish();
594 }
595 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700596 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 mNextButton.setOnClickListener(new OnClickListener() {
598 public void onClick(View v) {
599 setResult(RESULT_OK);
600 finish();
601 }
602 });
603
604 // set our various button parameters
605 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
606 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
607 if (TextUtils.isEmpty(buttonText)) {
608 mNextButton.setVisibility(View.GONE);
609 }
610 else {
611 mNextButton.setText(buttonText);
612 }
613 }
614 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
615 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
616 if (TextUtils.isEmpty(buttonText)) {
617 backButton.setVisibility(View.GONE);
618 }
619 else {
620 backButton.setText(buttonText);
621 }
622 }
623 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
624 skipButton.setVisibility(View.VISIBLE);
625 }
626 }
627 }
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800628 }
629
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700630 private void setTitleFromIntent(Intent intent) {
631 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
632 if (initialTitleResId > 0) {
633 mInitialTitle = null;
634 mInitialTitleResId = initialTitleResId;
635 setTitle(mInitialTitleResId);
636 } else {
637 mInitialTitleResId = -1;
638 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
639 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
640 setTitle(mInitialTitle);
641 }
642 }
643
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800644 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700646 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 }
648
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700649 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800650 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700651
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800652 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700653 if (mInitialTitleResId > 0) {
654 setTitle(mInitialTitleResId);
655 } else {
656 setTitle(mInitialTitle);
657 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700658 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800659 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700660
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800661 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
662 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700663
664 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800665 }
666
667 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
668 final CharSequence title;
669 final int titleRes = bse.getBreadCrumbTitleRes();
670 if (titleRes > 0) {
671 title = getText(titleRes);
672 } else {
673 title = bse.getBreadCrumbTitle();
674 }
675 if (title != null) {
676 setTitle(title);
677 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800678 }
679
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800680 @Override
681 protected void onSaveInstanceState(Bundle outState) {
682 super.onSaveInstanceState(outState);
683
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700684 if (mCategories.size() > 0) {
685 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800686 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700687
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700688 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700689 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700690
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700691 if (mDisplaySearch) {
692 // The option menus are created if the ActionBar is visible and they are also created
693 // asynchronously. If you launch Settings with an Intent action like
694 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
695 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
696 // menu item and search view are null.
697 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
698 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700699
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700700 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
701 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
702 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800703 }
704
705 @Override
706 public void onResume() {
707 super.onResume();
708
709 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
710 @Override
711 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700712 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700713 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800714 }
715 };
716 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
717 mDevelopmentPreferencesListener);
718
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800719 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700720
Svetoslav990159a2014-04-14 17:14:59 -0700721 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700722
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700723 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700724 onQueryTextSubmit(mSearchQuery);
725 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800726 }
727
728 @Override
729 public void onPause() {
730 super.onPause();
731
732 unregisterReceiver(mBatteryInfoReceiver);
733
Svetoslav990159a2014-04-14 17:14:59 -0700734 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800735 }
736
737 @Override
738 public void onDestroy() {
739 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700740
741 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
742 mDevelopmentPreferencesListener);
743 mDevelopmentPreferencesListener = null;
744
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800745 if (mListeningToAccountUpdates) {
746 AccountManager.get(this).removeOnAccountsUpdatedListener(this);
747 }
748 }
749
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800750 protected boolean isValidFragment(String fragmentName) {
751 // Almost all fragments are wrapped in this,
752 // except for a few that have their own activities.
753 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
754 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
755 }
756 return false;
757 }
758
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800759 @Override
760 public Intent getIntent() {
761 Intent superIntent = super.getIntent();
762 String startingFragment = getStartingFragmentClass(superIntent);
763 // This is called from super.onCreate, isMultiPane() is not yet reliable
764 // Do not use onIsHidingHeaders either, which relies itself on this method
765 if (startingFragment != null) {
766 Intent modIntent = new Intent(superIntent);
767 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
768 Bundle args = superIntent.getExtras();
769 if (args != null) {
770 args = new Bundle(args);
771 } else {
772 args = new Bundle();
773 }
774 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100775 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800776 return modIntent;
777 }
778 return superIntent;
779 }
780
781 /**
782 * Checks if the component name in the intent is different from the Settings class and
783 * returns the class name to load as a fragment.
784 */
785 private String getStartingFragmentClass(Intent intent) {
786 if (mFragmentClass != null) return mFragmentClass;
787
788 String intentClass = intent.getComponent().getClassName();
789 if (intentClass.equals(getClass().getName())) return null;
790
791 if ("com.android.settings.ManageApplications".equals(intentClass)
792 || "com.android.settings.RunningServices".equals(intentClass)
793 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
794 // Old names of manage apps.
795 intentClass = com.android.settings.applications.ManageApplications.class.getName();
796 }
797
798 return intentClass;
799 }
800
801 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000802 * Start a new fragment containing a preference panel. If the preferences
803 * are being displayed in multi-pane mode, the given fragment class will
804 * be instantiated and placed in the appropriate pane. If running in
805 * single-pane mode, a new activity will be launched in which to show the
806 * fragment.
807 *
808 * @param fragmentClass Full name of the class implementing the fragment.
809 * @param args Any desired arguments to supply to the fragment.
810 * @param titleRes Optional resource identifier of the title of this
811 * fragment.
812 * @param titleText Optional text of the title of this fragment.
813 * @param resultTo Optional fragment that result data should be sent to.
814 * If non-null, resultTo.onActivityResult() will be called when this
815 * preference panel is done. The launched panel must use
816 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
817 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700818 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000819 */
820 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700821 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700822 String title = null;
823 if (titleRes < 0) {
824 if (titleText != null) {
825 title = titleText.toString();
826 } else {
827 // There not much we can do in that case
828 title = "";
829 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700830 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700831 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700832 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000833 }
834
835 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800836 * Called by a preference panel fragment to finish itself.
837 *
838 * @param caller The fragment that is asking to be finished.
839 * @param resultCode Optional result code to send back to the original
840 * launching fragment.
841 * @param resultData Optional result data to send back to the original
842 * launching fragment.
843 */
844 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
845 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700846 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800847 }
848
849 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000850 * Start a new fragment.
851 *
852 * @param fragment The fragment to start
853 * @param push If true, the current fragment will be pushed onto the back stack. If false,
854 * the current fragment will be replaced.
855 */
856 public void startPreferenceFragment(Fragment fragment, boolean push) {
857 FragmentTransaction transaction = getFragmentManager().beginTransaction();
858 transaction.replace(R.id.prefs, fragment);
859 if (push) {
860 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
861 transaction.addToBackStack(BACK_STACK_PREFS);
862 } else {
863 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
864 }
865 transaction.commitAllowingStateLoss();
866 }
867
868 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700869 * Switch to a specific Fragment with taking care of validation, Title and BackStack
870 */
871 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700872 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700873 if (validate && !isValidFragment(fragmentName)) {
874 throw new IllegalArgumentException("Invalid fragment for this activity: "
875 + fragmentName);
876 }
877 Fragment f = Fragment.instantiate(this, fragmentName, args);
878 FragmentTransaction transaction = getFragmentManager().beginTransaction();
879 transaction.replace(R.id.prefs, f);
880 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700881 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700882 }
883 if (addToBackStack) {
884 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
885 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700886 if (titleResId > 0) {
887 transaction.setBreadCrumbTitle(titleResId);
888 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700889 transaction.setBreadCrumbTitle(title);
890 }
891 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700892 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700893 return f;
894 }
895
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700896 public void setNeedToRebuildCategories(boolean need) {
897 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700898 }
899
900 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700901 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700902 *
903 * @param categories The list in which to place the tiles categories.
904 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700905 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700906 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700907 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
908 updateTilesList(categories);
909 }
910
911 /**
912 * Parse the given XML file as a categories description, adding each
913 * parsed categories and tiles into the target list.
914 *
915 * @param resid The XML resource to load and parse.
916 * @param target The list in which the parsed categories and tiles should be placed.
917 */
918 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
919 XmlResourceParser parser = null;
920 try {
921 parser = getResources().getXml(resid);
922 AttributeSet attrs = Xml.asAttributeSet(parser);
923
924 int type;
925 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
926 && type != XmlPullParser.START_TAG) {
927 // Parse next until start tag is found
928 }
929
930 String nodeName = parser.getName();
931 if (!"dashboard-categories".equals(nodeName)) {
932 throw new RuntimeException(
933 "XML document must start with <preference-categories> tag; found"
934 + nodeName + " at " + parser.getPositionDescription());
935 }
936
937 Bundle curBundle = null;
938
939 final int outerDepth = parser.getDepth();
940 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
941 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
942 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
943 continue;
944 }
945
946 nodeName = parser.getName();
947 if ("dashboard-category".equals(nodeName)) {
948 DashboardCategory category = new DashboardCategory();
949
950 TypedArray sa = obtainStyledAttributes(
951 attrs, com.android.internal.R.styleable.PreferenceHeader);
952 category.id = sa.getResourceId(
953 com.android.internal.R.styleable.PreferenceHeader_id,
954 (int)DashboardCategory.CAT_ID_UNDEFINED);
955
956 TypedValue tv = sa.peekValue(
957 com.android.internal.R.styleable.PreferenceHeader_title);
958 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
959 if (tv.resourceId != 0) {
960 category.titleRes = tv.resourceId;
961 } else {
962 category.title = tv.string;
963 }
964 }
965 sa.recycle();
966
967 final int innerDepth = parser.getDepth();
968 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
969 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
970 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
971 continue;
972 }
973
974 String innerNodeName = parser.getName();
975 if (innerNodeName.equals("dashboard-tile")) {
976 DashboardTile tile = new DashboardTile();
977
978 sa = obtainStyledAttributes(
979 attrs, com.android.internal.R.styleable.PreferenceHeader);
980 tile.id = sa.getResourceId(
981 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700982 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700983 tv = sa.peekValue(
984 com.android.internal.R.styleable.PreferenceHeader_title);
985 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
986 if (tv.resourceId != 0) {
987 tile.titleRes = tv.resourceId;
988 } else {
989 tile.title = tv.string;
990 }
991 }
992 tv = sa.peekValue(
993 com.android.internal.R.styleable.PreferenceHeader_summary);
994 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
995 if (tv.resourceId != 0) {
996 tile.summaryRes = tv.resourceId;
997 } else {
998 tile.summary = tv.string;
999 }
1000 }
1001 tile.iconRes = sa.getResourceId(
1002 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1003 tile.fragment = sa.getString(
1004 com.android.internal.R.styleable.PreferenceHeader_fragment);
1005 sa.recycle();
1006
1007 if (curBundle == null) {
1008 curBundle = new Bundle();
1009 }
1010
1011 final int innerDepth2 = parser.getDepth();
1012 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1013 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1014 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1015 continue;
1016 }
1017
1018 String innerNodeName2 = parser.getName();
1019 if (innerNodeName2.equals("extra")) {
1020 getResources().parseBundleExtra("extra", attrs, curBundle);
1021 XmlUtils.skipCurrentTag(parser);
1022
1023 } else if (innerNodeName2.equals("intent")) {
1024 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1025
1026 } else {
1027 XmlUtils.skipCurrentTag(parser);
1028 }
1029 }
1030
1031 if (curBundle.size() > 0) {
1032 tile.fragmentArguments = curBundle;
1033 curBundle = null;
1034 }
1035
1036 category.addTile(tile);
1037
1038 } else {
1039 XmlUtils.skipCurrentTag(parser);
1040 }
1041 }
1042
1043 target.add(category);
1044 } else {
1045 XmlUtils.skipCurrentTag(parser);
1046 }
1047 }
1048
1049 } catch (XmlPullParserException e) {
1050 throw new RuntimeException("Error parsing categories", e);
1051 } catch (IOException e) {
1052 throw new RuntimeException("Error parsing categories", e);
1053 } finally {
1054 if (parser != null) parser.close();
1055 }
1056 }
1057
1058 private void updateTilesList(List<DashboardCategory> target) {
1059 final boolean showDev = mDevelopmentPreferences.getBoolean(
1060 DevelopmentSettings.PREF_SHOW,
1061 android.os.Build.TYPE.equals("eng"));
1062
1063 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1064
1065 final int size = target.size();
1066 for (int i = 0; i < size; i++) {
1067
1068 DashboardCategory category = target.get(i);
1069
1070 // Ids are integers, so downcasting is ok
1071 int id = (int) category.id;
1072 if (id == R.id.account_settings) {
1073 insertAccountsTiles(category);
1074 continue;
1075 }
1076 int n = category.getTilesCount() - 1;
1077 while (n >= 0) {
1078
1079 DashboardTile tile = category.getTile(n);
1080
1081 id = (int) tile.id;
1082 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1083 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1084 } else if (id == R.id.wifi_settings) {
1085 // Remove WiFi Settings if WiFi service is not available.
1086 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1087 category.removeTile(n);
1088 }
1089 } else if (id == R.id.bluetooth_settings) {
1090 // Remove Bluetooth Settings if Bluetooth service is not available.
1091 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1092 category.removeTile(n);
1093 }
1094 } else if (id == R.id.data_usage_settings) {
1095 // Remove data usage when kernel module not enabled
1096 final INetworkManagementService netManager = INetworkManagementService.Stub
1097 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1098 try {
1099 if (!netManager.isBandwidthControlEnabled()) {
1100 category.removeTile(n);
1101 }
1102 } catch (RemoteException e) {
1103 // ignored
1104 }
1105 } else if (id == R.id.battery_settings) {
1106 // Remove battery settings when battery is not available. (e.g. TV)
1107
1108 if (!mBatteryPresent) {
1109 category.removeTile(n);
1110 }
1111 } else if (id == R.id.home_settings) {
1112 if (!updateHomeSettingTiles(tile)) {
1113 category.removeTile(n);
1114 }
1115 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001116 boolean hasMultipleUsers =
1117 ((UserManager) getSystemService(Context.USER_SERVICE))
1118 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001119 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001120 || (!UserManager.supportsMultipleUsers()
1121 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001122 || Utils.isMonkeyRunning()) {
1123 category.removeTile(n);
1124 }
1125 } else if (id == R.id.nfc_payment_settings) {
1126 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1127 category.removeTile(n);
1128 } else {
1129 // Only show if NFC is on and we have the HCE feature
1130 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1131 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1132 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1133 category.removeTile(n);
1134 }
1135 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001136 } else if (id == R.id.print_settings) {
1137 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1138 PackageManager.FEATURE_PRINTING);
1139 if (!hasPrintingSupport) {
1140 category.removeTile(n);
1141 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001142 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001143 if (!showDev || um.hasUserRestriction(
1144 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001145 category.removeTile(n);
1146 }
1147 } else if (id == R.id.account_add) {
1148 if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
1149 category.removeTile(n);
1150 }
1151 }
1152
1153 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001154 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1155 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001156 category.removeTile(n);
1157 }
1158
1159 n--;
1160 }
1161 }
1162 }
1163
1164 private boolean updateHomeSettingTiles(DashboardTile tile) {
1165 // Once we decide to show Home settings, keep showing it forever
1166 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1167 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1168 return true;
1169 }
1170
1171 try {
1172 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
1173 getPackageManager().getHomeActivities(homeApps);
1174 if (homeApps.size() < 2) {
1175 // When there's only one available home app, omit this settings
1176 // category entirely at the top level UI. If the user just
1177 // uninstalled the penultimate home app candidiate, we also
1178 // now tell them about why they aren't seeing 'Home' in the list.
1179 if (sShowNoHomeNotice) {
1180 sShowNoHomeNotice = false;
1181 NoHomeDialogFragment.show(this);
1182 }
1183 return false;
1184 } else {
1185 // Okay, we're allowing the Home settings category. Tell it, when
1186 // invoked via this front door, that we'll need to be told about the
1187 // case when the user uninstalls all but one home app.
1188 if (tile.fragmentArguments == null) {
1189 tile.fragmentArguments = new Bundle();
1190 }
1191 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1192 }
1193 } catch (Exception e) {
1194 // Can't look up the home activity; bail on configuring the icon
1195 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1196 }
1197
1198 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1199 return true;
1200 }
1201
1202 private void insertAccountsTiles(DashboardCategory target) {
1203 String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
1204 List<DashboardTile> dashboardTiles = new ArrayList<DashboardTile>(accountTypes.length);
1205 for (String accountType : accountTypes) {
1206 CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
1207 if (label == null) {
1208 continue;
1209 }
1210
1211 Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
1212 boolean skipToAccount = accounts.length == 1
1213 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
1214 DashboardTile accountTile = new DashboardTile();
1215 accountTile.title = label;
1216 if (accountTile.extras == null) {
1217 accountTile.extras = new Bundle();
1218 }
1219 if (skipToAccount) {
1220 accountTile.fragment = AccountSyncSettings.class.getName();
1221 accountTile.fragmentArguments = new Bundle();
1222 // Need this for the icon
1223 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1224 accountTile.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
1225 accountTile.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY,
1226 accounts[0]);
1227 } else {
1228 accountTile.fragment = ManageAccountsSettings.class.getName();
1229 accountTile.fragmentArguments = new Bundle();
1230 accountTile.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
1231 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE,
1232 accountType);
1233 accountTile.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_LABEL,
1234 label.toString());
1235 }
1236 dashboardTiles.add(accountTile);
1237 mAuthenticatorHelper.preloadDrawableForType(this, accountType);
1238 }
1239
1240 // Sort by label
1241 Collections.sort(dashboardTiles, new Comparator<DashboardTile>() {
1242 @Override
1243 public int compare(DashboardTile t1, DashboardTile t2) {
1244 return t1.title.toString().compareTo(t2.title.toString());
1245 }
1246 });
1247 int index = 0;
1248 for (DashboardTile tile : dashboardTiles) {
1249 target.addTile(index, tile);
1250 index++;
1251 }
1252 if (!mListeningToAccountUpdates) {
1253 AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
1254 mListeningToAccountUpdates = true;
1255 }
1256 }
1257
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001258 private void getMetaData() {
1259 try {
1260 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1261 PackageManager.GET_META_DATA);
1262 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001263 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1264 } catch (NameNotFoundException nnfe) {
1265 // No recovery
1266 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1267 }
1268 }
1269
1270 // give subclasses access to the Next button
1271 public boolean hasNextButton() {
1272 return mNextButton != null;
1273 }
1274
1275 public Button getNextButton() {
1276 return mNextButton;
1277 }
1278
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001279 @Override
1280 public boolean shouldUpRecreateTask(Intent targetIntent) {
1281 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1282 }
1283
1284 @Override
1285 public void onAccountsUpdated(Account[] accounts) {
1286 // TODO: watch for package upgrades to invalidate cache; see 7206643
1287 mAuthenticatorHelper.updateAuthDescriptions(this);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +01001288 invalidateCategories();
1289 }
1290
1291 @Override
1292 public void onAccountsUpdate(UserHandle userHandle) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001293 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001294 }
1295
1296 public static void requestHomeNotice() {
1297 sShowNoHomeNotice = true;
1298 }
1299
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001300 @Override
1301 public boolean onQueryTextSubmit(String query) {
1302 switchToSearchResultsFragmentIfNeeded();
1303 mSearchQuery = query;
1304 return mSearchResultsFragment.onQueryTextSubmit(query);
1305 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001306
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001307 @Override
1308 public boolean onQueryTextChange(String newText) {
1309 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001310 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001311 return false;
1312 }
1313 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001314 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001315
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001316 @Override
1317 public boolean onClose() {
1318 return false;
1319 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001320
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001321 @Override
1322 public boolean onMenuItemActionExpand(MenuItem item) {
1323 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001324 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001325 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001326 return true;
1327 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001328
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001329 @Override
1330 public boolean onMenuItemActionCollapse(MenuItem item) {
1331 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001332 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001333 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001334 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001335 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001336 return true;
1337 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001338
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001339 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001340 if (mSearchResultsFragment != null) {
1341 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001342 }
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001343 Fragment current = getFragmentManager().findFragmentById(R.id.prefs);
1344 if (current != null && current instanceof SearchResultsSummary) {
1345 mSearchResultsFragment = (SearchResultsSummary) current;
1346 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001347 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001348 SearchResultsSummary.class.getName(), null, false, true,
1349 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001350 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001351 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001352 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001353 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001354
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001355 public void needToRevertToInitialFragment() {
1356 mNeedToRevertToInitialFragment = true;
1357 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001358
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001359 private void revertToInitialFragment() {
1360 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001361 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001362 mSearchMenuItemExpanded = false;
1363 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1364 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001365 if (mSearchMenuItem != null) {
1366 mSearchMenuItem.collapseActionView();
1367 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001368 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001369}