blob: df7e0060500918e46773feb06a3ca802d997b432 [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
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080019import android.app.ActionBar;
20import android.app.Activity;
21import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.pm.ResolveInfo;
34import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080035import android.content.res.TypedArray;
36import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.nfc.NfcAdapter;
38import android.os.Bundle;
39import android.os.Handler;
40import android.os.INetworkManagementService;
41import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.UserHandle;
45import android.os.UserManager;
46import android.preference.Preference;
47import android.preference.PreferenceFragment;
48import android.preference.PreferenceManager;
49import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.AttributeSet;
53import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import android.util.TypedValue;
55import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070056import android.view.Menu;
57import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.view.MenuItem;
59import android.view.View;
60import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070061import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070063import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040064
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080065import com.android.internal.util.ArrayUtils;
66import com.android.internal.util.XmlUtils;
67import com.android.settings.accessibility.AccessibilitySettings;
68import com.android.settings.accessibility.CaptionPropertiesFragment;
69import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070070import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import com.android.settings.applications.ManageApplications;
72import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import com.android.settings.bluetooth.BluetoothSettings;
PauloftheWestc9dc66d2014-07-01 05:19:58 -070074import com.android.settings.bluetooth.MessageAccessSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070075import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070077import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070078import com.android.settings.dashboard.NoHomeDialogFragment;
79import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.deviceinfo.Memory;
81import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040082import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083import com.android.settings.fuelgauge.PowerUsageSummary;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070084import com.android.settings.notification.AppNotificationSettings;
85import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070086import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070087import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
89import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
90import com.android.settings.inputmethod.SpellCheckersSettings;
91import com.android.settings.inputmethod.UserDictionaryList;
92import com.android.settings.location.LocationSettings;
93import com.android.settings.nfc.AndroidBeam;
94import com.android.settings.nfc.PaymentSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040095import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040096import com.android.settings.notification.NotificationAccessSettings;
97import com.android.settings.notification.NotificationSettings;
98import com.android.settings.notification.NotificationStation;
99import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800100import com.android.settings.print.PrintJobSettingsFragment;
101import com.android.settings.print.PrintSettingsFragment;
102import com.android.settings.tts.TextToSpeechSettings;
103import com.android.settings.users.UserSettings;
104import com.android.settings.vpn2.VpnSettings;
105import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700106import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800107import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700108import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.wifi.WifiSettings;
110import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400111
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import org.xmlpull.v1.XmlPullParser;
113import org.xmlpull.v1.XmlPullParserException;
114
115import java.io.IOException;
116import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700118import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700120import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700121
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122public class SettingsActivity extends Activity
123 implements PreferenceManager.OnPreferenceTreeClickListener,
124 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100125 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700126 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100127 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800128
129 private static final String LOG_TAG = "Settings";
130
131 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700132 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700133 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
134 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700135 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700136 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700137 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800138
139 /**
140 * When starting this activity, the invoking Intent can contain this extra
141 * string to specify which fragment should be initially displayed.
142 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
143 * will call isValidFragment() to confirm that the fragment class name is valid for this
144 * activity.
145 */
146 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
147
148 /**
149 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
150 * this extra can also be specified to supply a Bundle of arguments to pass
151 * to that fragment when it is instantiated during the initial creation
152 * of the activity.
153 */
154 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
155
156 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700157 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
158 */
159 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
160
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800161 public static final String BACK_STACK_PREFS = ":settings:prefs";
162
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163 // extras that allow any preference activity to be launched as part of a wizard
164
165 // show Back and Next buttons? takes boolean parameter
166 // Back will then return RESULT_CANCELED and Next RESULT_OK
167 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
168
169 // add a Skip button?
170 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
171
172 // specify custom text for the Back or Next buttons, or cause a button to not appear
173 // at all by setting it to null
174 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
175 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
176
177 /**
178 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700179 * 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 -0800180 * that fragment.
181 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700182 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700183 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700184 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800185
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800186 private static final String META_DATA_KEY_FRAGMENT_CLASS =
187 "com.android.settings.FRAGMENT_CLASS";
188
189 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
190
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700191 private static final String EMPTY_QUERY = "";
192
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800193 private static boolean sShowNoHomeNotice = false;
194
195 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800197 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700198 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800199
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200 // Show only these settings for restricted users
201 private int[] SETTINGS_FOR_RESTRICTED = {
202 R.id.wireless_section,
203 R.id.wifi_settings,
204 R.id.bluetooth_settings,
205 R.id.data_usage_settings,
206 R.id.wireless_settings,
207 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400208 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 R.id.display_settings,
210 R.id.storage_settings,
211 R.id.application_settings,
212 R.id.battery_settings,
213 R.id.personal_section,
214 R.id.location_settings,
215 R.id.security_settings,
216 R.id.language_settings,
217 R.id.user_settings,
218 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800219 R.id.system_section,
220 R.id.date_time_settings,
221 R.id.about_settings,
222 R.id.accessibility_settings,
223 R.id.print_settings,
224 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800225 R.id.home_settings,
226 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800227 };
228
229 private static final String[] ENTRY_FRAGMENTS = {
230 WirelessSettings.class.getName(),
231 WifiSettings.class.getName(),
232 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700233 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800234 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700235 MessageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800236 TetherSettings.class.getName(),
237 WifiP2pSettings.class.getName(),
238 VpnSettings.class.getName(),
239 DateTimeSettings.class.getName(),
240 LocalePicker.class.getName(),
241 InputMethodAndLanguageSettings.class.getName(),
242 SpellCheckersSettings.class.getName(),
243 UserDictionaryList.class.getName(),
244 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700245 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 DisplaySettings.class.getName(),
247 DeviceInfoSettings.class.getName(),
248 ManageApplications.class.getName(),
249 ProcessStatsUi.class.getName(),
250 NotificationStation.class.getName(),
251 LocationSettings.class.getName(),
252 SecuritySettings.class.getName(),
253 PrivacySettings.class.getName(),
254 DeviceAdminSettings.class.getName(),
255 AccessibilitySettings.class.getName(),
256 CaptionPropertiesFragment.class.getName(),
257 com.android.settings.accessibility.ToggleInversionPreferenceFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
259 TextToSpeechSettings.class.getName(),
260 Memory.class.getName(),
261 DevelopmentSettings.class.getName(),
262 UsbSettings.class.getName(),
263 AndroidBeam.class.getName(),
264 WifiDisplaySettings.class.getName(),
265 PowerUsageSummary.class.getName(),
266 AccountSyncSettings.class.getName(),
267 CryptKeeperSettings.class.getName(),
268 DataUsageSummary.class.getName(),
269 DreamSettings.class.getName(),
270 UserSettings.class.getName(),
271 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400272 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 PrintSettingsFragment.class.getName(),
274 PrintJobSettingsFragment.class.getName(),
275 TrustedCredentialsSettings.class.getName(),
276 PaymentSettings.class.getName(),
277 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700278 ZenModeSettings.class.getName(),
279 NotificationSettings.class.getName(),
280 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
281 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400282 InstalledAppDetails.class.getName(),
283 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700284 AppNotificationSettings.class.getName(),
285 OtherSoundSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 };
287
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700288
289 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
290 "android.settings.APPLICATION_DETAILS_SETTINGS"
291 };
292
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 private SharedPreferences mDevelopmentPreferences;
294 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
295
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 private boolean mBatteryPresent = true;
297 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800298 @Override
299 public void onReceive(Context context, Intent intent) {
300 String action = intent.getAction();
301 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
302 boolean batteryPresent = Utils.isBatteryPresent(intent);
303
304 if (mBatteryPresent != batteryPresent) {
305 mBatteryPresent = batteryPresent;
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700306 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 }
308 }
309 }
310 };
311
Svetoslav990159a2014-04-14 17:14:59 -0700312 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
313 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700314
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700315 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700316 private SwitchBar mSwitchBar;
317
318 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700319
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700320 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700321 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700322
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700323 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700324 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700325
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700326 private ViewGroup mContent;
327
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700328 private SearchView mSearchView;
329 private MenuItem mSearchMenuItem;
330 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700331 private SearchResultsSummary mSearchResultsFragment;
332 private String mSearchQuery;
333
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700334 // Categories
335 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
336 private boolean mNeedToRebuildCategories;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800337
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700338 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800339 private Handler mHandler = new Handler() {
340 @Override
341 public void handleMessage(Message msg) {
342 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700343 case MSG_BUILD_CATEGORIES: {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700344 if(mNeedToRebuildCategories) {
345 buildDashboardCategories(mCategories);
346 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 } break;
348 }
349 }
350 };
351
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700352 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700353 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700354
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700355 public SwitchBar getSwitchBar() {
356 return mSwitchBar;
357 }
358
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700359 public List<DashboardCategory> getDashboardCategories() {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700360 if (mNeedToRebuildCategories || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700361 buildDashboardCategories(mCategories);
362 mNeedToRebuildCategories = false;
363 }
364 return mCategories;
365 }
366
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800367 @Override
368 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
369 // Override the fragment title for Wallpaper settings
370 int titleRes = pref.getTitleRes();
371 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
372 titleRes = R.string.wallpaper_settings_fragment_title;
373 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
374 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
375 if (UserManager.get(this).isLinkedUser()) {
376 titleRes = R.string.profile_info_settings_title;
377 } else {
378 titleRes = R.string.user_info_settings_title;
379 }
380 }
381 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
382 null, 0);
383 return true;
384 }
385
386 @Override
387 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
388 return false;
389 }
390
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700391 private void invalidateCategories() {
392 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
393 mHandler.sendEmptyMessage(MSG_BUILD_CATEGORIES);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800394 }
395 }
396
397 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800398 public void onConfigurationChanged(Configuration newConfig) {
399 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800400 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401 }
402
403 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700404 protected void onStart() {
405 super.onStart();
406
407 if (mNeedToRevertToInitialFragment) {
408 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800409 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800410 }
411
412 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700413 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700414 if (!mDisplaySearch) {
415 return false;
416 }
417
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700418 MenuInflater inflater = getMenuInflater();
419 inflater.inflate(R.menu.options_menu, menu);
420
421 // Cache the search query (can be overriden by the OnQueryTextListener)
422 final String query = mSearchQuery;
423
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700424 mSearchMenuItem = menu.findItem(R.id.search);
425 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700426
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700427 if (mSearchMenuItem == null || mSearchView == null) {
428 return false;
429 }
430
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700431 if (mSearchResultsFragment != null) {
432 mSearchResultsFragment.setSearchView(mSearchView);
433 }
434
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700435 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700436 mSearchView.setOnQueryTextListener(this);
437 mSearchView.setOnCloseListener(this);
438
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700439 if (mSearchMenuItemExpanded) {
440 mSearchMenuItem.expandActionView();
441 }
442 mSearchView.setQuery(query, true /* submit */);
443
444 return true;
445 }
446
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700447 private static boolean isShortCutIntent(final Intent intent) {
448 Set<String> categories = intent.getCategories();
449 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
450 }
451
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700452 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700453 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700454 if (action == null) {
455 return false;
456 }
457 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
458 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
459 }
460 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700461 }
462
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700463 @Override
464 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700465 super.onCreate(savedState);
466
467 // Should happen before any call to getIntent()
468 getMetaData();
469
470 final Intent intent = getIntent();
471 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
472 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800475 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
476 Context.MODE_PRIVATE);
477
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700478 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700479 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700480
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700481 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700482 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
483
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700484 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700485 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700486
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700487 mIsShowingDashboard = className.equals(Settings.class.getName());
488 final boolean isSubSettings = className.equals(SubSettings.class.getName());
489
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700490 // If this is a sub settings or not the main Dashboard and not a Shortcut and an initial
491 // Fragment then apply the SubSettings theme for the ActionBar content insets
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700492 if (isSubSettings ||
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700493 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName != null))) {
494 // Check also that we are not a Theme Dialog as we don't want to override them
495 final int themeResId = getThemeResId();
496 if (themeResId != R.style.Theme_DialogWhenLarge &&
497 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
498 setTheme(R.style.Theme_SubSettings);
499 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700500 }
501
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700502 setContentView(mIsShowingDashboard ?
503 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800504
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700505 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700506
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507 getFragmentManager().addOnBackStackChangedListener(this);
508
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700509 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700510 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700511 }
512
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700513 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700514 // We are restarting from a previous saved state; used that to initialize, instead
515 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700516 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
517 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800518
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700519 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800520
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700521 ArrayList<DashboardCategory> categories =
522 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
523 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700524 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700525 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700526 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700528
529 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700530 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700531 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
532 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700534 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700535 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
536 // only if it is a sub settings
537 if (mIsShortcut) {
538 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700539 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700540 } else if (isSubSettings) {
541 mDisplayHomeAsUpEnabled = true;
542 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700543 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700544 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700545
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700546 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700547 switchToFragment(initialFragmentName, initialArguments, true, false,
548 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000549 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700550 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700551 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700552 // Show Search affordance
553 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700554 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700555 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700556 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800557 }
558 }
559
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700560 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700561 if (mActionBar != null) {
562 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
563 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
564 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700565 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
566
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
569
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700570 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571 if (buttonBar != null) {
572 buttonBar.setVisibility(View.VISIBLE);
573
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700574 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 backButton.setOnClickListener(new OnClickListener() {
576 public void onClick(View v) {
577 setResult(RESULT_CANCELED);
578 finish();
579 }
580 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700581 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582 skipButton.setOnClickListener(new OnClickListener() {
583 public void onClick(View v) {
584 setResult(RESULT_OK);
585 finish();
586 }
587 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700588 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800589 mNextButton.setOnClickListener(new OnClickListener() {
590 public void onClick(View v) {
591 setResult(RESULT_OK);
592 finish();
593 }
594 });
595
596 // set our various button parameters
597 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
598 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
599 if (TextUtils.isEmpty(buttonText)) {
600 mNextButton.setVisibility(View.GONE);
601 }
602 else {
603 mNextButton.setText(buttonText);
604 }
605 }
606 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
607 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
608 if (TextUtils.isEmpty(buttonText)) {
609 backButton.setVisibility(View.GONE);
610 }
611 else {
612 backButton.setText(buttonText);
613 }
614 }
615 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
616 skipButton.setVisibility(View.VISIBLE);
617 }
618 }
619 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700620
621 mHomeActivitiesCount = getHomeActivitiesCount();
622 }
623
624 private int getHomeActivitiesCount() {
625 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
626 getPackageManager().getHomeActivities(homeApps);
627 return homeApps.size();
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 Meglioceb335f2014-07-23 16:25:30 -0700703
704 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800705 }
706
707 @Override
708 public void onResume() {
709 super.onResume();
710
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700711 final int newHomeActivityCount = getHomeActivitiesCount();
712 if (newHomeActivityCount != mHomeActivitiesCount) {
713 mHomeActivitiesCount = newHomeActivityCount;
714 setNeedToRebuildCategories(true);
715 invalidateCategories();
716 }
717
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800718 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
719 @Override
720 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliobb051782014-06-04 17:33:25 -0700721 setNeedToRebuildCategories(true);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700722 invalidateCategories();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800723 }
724 };
725 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
726 mDevelopmentPreferencesListener);
727
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800728 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700729
Svetoslav990159a2014-04-14 17:14:59 -0700730 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700731
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700732 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700733 onQueryTextSubmit(mSearchQuery);
734 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800735 }
736
737 @Override
738 public void onPause() {
739 super.onPause();
740
741 unregisterReceiver(mBatteryInfoReceiver);
742
Svetoslav990159a2014-04-14 17:14:59 -0700743 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800744 }
745
746 @Override
747 public void onDestroy() {
748 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700749
750 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
751 mDevelopmentPreferencesListener);
752 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800753 }
754
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800755 protected boolean isValidFragment(String fragmentName) {
756 // Almost all fragments are wrapped in this,
757 // except for a few that have their own activities.
758 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
759 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
760 }
761 return false;
762 }
763
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800764 @Override
765 public Intent getIntent() {
766 Intent superIntent = super.getIntent();
767 String startingFragment = getStartingFragmentClass(superIntent);
768 // This is called from super.onCreate, isMultiPane() is not yet reliable
769 // Do not use onIsHidingHeaders either, which relies itself on this method
770 if (startingFragment != null) {
771 Intent modIntent = new Intent(superIntent);
772 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
773 Bundle args = superIntent.getExtras();
774 if (args != null) {
775 args = new Bundle(args);
776 } else {
777 args = new Bundle();
778 }
779 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100780 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800781 return modIntent;
782 }
783 return superIntent;
784 }
785
786 /**
787 * Checks if the component name in the intent is different from the Settings class and
788 * returns the class name to load as a fragment.
789 */
790 private String getStartingFragmentClass(Intent intent) {
791 if (mFragmentClass != null) return mFragmentClass;
792
793 String intentClass = intent.getComponent().getClassName();
794 if (intentClass.equals(getClass().getName())) return null;
795
796 if ("com.android.settings.ManageApplications".equals(intentClass)
797 || "com.android.settings.RunningServices".equals(intentClass)
798 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
799 // Old names of manage apps.
800 intentClass = com.android.settings.applications.ManageApplications.class.getName();
801 }
802
803 return intentClass;
804 }
805
806 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000807 * Start a new fragment containing a preference panel. If the preferences
808 * are being displayed in multi-pane mode, the given fragment class will
809 * be instantiated and placed in the appropriate pane. If running in
810 * single-pane mode, a new activity will be launched in which to show the
811 * fragment.
812 *
813 * @param fragmentClass Full name of the class implementing the fragment.
814 * @param args Any desired arguments to supply to the fragment.
815 * @param titleRes Optional resource identifier of the title of this
816 * fragment.
817 * @param titleText Optional text of the title of this fragment.
818 * @param resultTo Optional fragment that result data should be sent to.
819 * If non-null, resultTo.onActivityResult() will be called when this
820 * preference panel is done. The launched panel must use
821 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
822 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700823 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000824 */
825 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700826 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700827 String title = null;
828 if (titleRes < 0) {
829 if (titleText != null) {
830 title = titleText.toString();
831 } else {
832 // There not much we can do in that case
833 title = "";
834 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700835 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700836 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700837 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000838 }
839
840 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800841 * Called by a preference panel fragment to finish itself.
842 *
843 * @param caller The fragment that is asking to be finished.
844 * @param resultCode Optional result code to send back to the original
845 * launching fragment.
846 * @param resultData Optional result data to send back to the original
847 * launching fragment.
848 */
849 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
850 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700851 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800852 }
853
854 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000855 * Start a new fragment.
856 *
857 * @param fragment The fragment to start
858 * @param push If true, the current fragment will be pushed onto the back stack. If false,
859 * the current fragment will be replaced.
860 */
861 public void startPreferenceFragment(Fragment fragment, boolean push) {
862 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700863 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000864 if (push) {
865 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
866 transaction.addToBackStack(BACK_STACK_PREFS);
867 } else {
868 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
869 }
870 transaction.commitAllowingStateLoss();
871 }
872
873 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700874 * Switch to a specific Fragment with taking care of validation, Title and BackStack
875 */
876 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700877 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700878 if (validate && !isValidFragment(fragmentName)) {
879 throw new IllegalArgumentException("Invalid fragment for this activity: "
880 + fragmentName);
881 }
882 Fragment f = Fragment.instantiate(this, fragmentName, args);
883 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700884 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700885 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700886 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700887 }
888 if (addToBackStack) {
889 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
890 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700891 if (titleResId > 0) {
892 transaction.setBreadCrumbTitle(titleResId);
893 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700894 transaction.setBreadCrumbTitle(title);
895 }
896 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700897 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700898 return f;
899 }
900
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700901 public void setNeedToRebuildCategories(boolean need) {
902 mNeedToRebuildCategories = need;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700903 }
904
905 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700906 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700907 *
908 * @param categories The list in which to place the tiles categories.
909 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700910 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700911 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700912 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
913 updateTilesList(categories);
914 }
915
916 /**
917 * Parse the given XML file as a categories description, adding each
918 * parsed categories and tiles into the target list.
919 *
920 * @param resid The XML resource to load and parse.
921 * @param target The list in which the parsed categories and tiles should be placed.
922 */
923 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
924 XmlResourceParser parser = null;
925 try {
926 parser = getResources().getXml(resid);
927 AttributeSet attrs = Xml.asAttributeSet(parser);
928
929 int type;
930 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
931 && type != XmlPullParser.START_TAG) {
932 // Parse next until start tag is found
933 }
934
935 String nodeName = parser.getName();
936 if (!"dashboard-categories".equals(nodeName)) {
937 throw new RuntimeException(
938 "XML document must start with <preference-categories> tag; found"
939 + nodeName + " at " + parser.getPositionDescription());
940 }
941
942 Bundle curBundle = null;
943
944 final int outerDepth = parser.getDepth();
945 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
946 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
947 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
948 continue;
949 }
950
951 nodeName = parser.getName();
952 if ("dashboard-category".equals(nodeName)) {
953 DashboardCategory category = new DashboardCategory();
954
955 TypedArray sa = obtainStyledAttributes(
956 attrs, com.android.internal.R.styleable.PreferenceHeader);
957 category.id = sa.getResourceId(
958 com.android.internal.R.styleable.PreferenceHeader_id,
959 (int)DashboardCategory.CAT_ID_UNDEFINED);
960
961 TypedValue tv = sa.peekValue(
962 com.android.internal.R.styleable.PreferenceHeader_title);
963 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
964 if (tv.resourceId != 0) {
965 category.titleRes = tv.resourceId;
966 } else {
967 category.title = tv.string;
968 }
969 }
970 sa.recycle();
971
972 final int innerDepth = parser.getDepth();
973 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
974 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
975 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
976 continue;
977 }
978
979 String innerNodeName = parser.getName();
980 if (innerNodeName.equals("dashboard-tile")) {
981 DashboardTile tile = new DashboardTile();
982
983 sa = obtainStyledAttributes(
984 attrs, com.android.internal.R.styleable.PreferenceHeader);
985 tile.id = sa.getResourceId(
986 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700987 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700988 tv = sa.peekValue(
989 com.android.internal.R.styleable.PreferenceHeader_title);
990 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
991 if (tv.resourceId != 0) {
992 tile.titleRes = tv.resourceId;
993 } else {
994 tile.title = tv.string;
995 }
996 }
997 tv = sa.peekValue(
998 com.android.internal.R.styleable.PreferenceHeader_summary);
999 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1000 if (tv.resourceId != 0) {
1001 tile.summaryRes = tv.resourceId;
1002 } else {
1003 tile.summary = tv.string;
1004 }
1005 }
1006 tile.iconRes = sa.getResourceId(
1007 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1008 tile.fragment = sa.getString(
1009 com.android.internal.R.styleable.PreferenceHeader_fragment);
1010 sa.recycle();
1011
1012 if (curBundle == null) {
1013 curBundle = new Bundle();
1014 }
1015
1016 final int innerDepth2 = parser.getDepth();
1017 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1018 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1019 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1020 continue;
1021 }
1022
1023 String innerNodeName2 = parser.getName();
1024 if (innerNodeName2.equals("extra")) {
1025 getResources().parseBundleExtra("extra", attrs, curBundle);
1026 XmlUtils.skipCurrentTag(parser);
1027
1028 } else if (innerNodeName2.equals("intent")) {
1029 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1030
1031 } else {
1032 XmlUtils.skipCurrentTag(parser);
1033 }
1034 }
1035
1036 if (curBundle.size() > 0) {
1037 tile.fragmentArguments = curBundle;
1038 curBundle = null;
1039 }
1040
1041 category.addTile(tile);
1042
1043 } else {
1044 XmlUtils.skipCurrentTag(parser);
1045 }
1046 }
1047
1048 target.add(category);
1049 } else {
1050 XmlUtils.skipCurrentTag(parser);
1051 }
1052 }
1053
1054 } catch (XmlPullParserException e) {
1055 throw new RuntimeException("Error parsing categories", e);
1056 } catch (IOException e) {
1057 throw new RuntimeException("Error parsing categories", e);
1058 } finally {
1059 if (parser != null) parser.close();
1060 }
1061 }
1062
1063 private void updateTilesList(List<DashboardCategory> target) {
1064 final boolean showDev = mDevelopmentPreferences.getBoolean(
1065 DevelopmentSettings.PREF_SHOW,
1066 android.os.Build.TYPE.equals("eng"));
1067
1068 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1069
1070 final int size = target.size();
1071 for (int i = 0; i < size; i++) {
1072
1073 DashboardCategory category = target.get(i);
1074
1075 // Ids are integers, so downcasting is ok
1076 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001077 int n = category.getTilesCount() - 1;
1078 while (n >= 0) {
1079
1080 DashboardTile tile = category.getTile(n);
1081
1082 id = (int) tile.id;
1083 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1084 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1085 } else if (id == R.id.wifi_settings) {
1086 // Remove WiFi Settings if WiFi service is not available.
1087 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1088 category.removeTile(n);
1089 }
1090 } else if (id == R.id.bluetooth_settings) {
1091 // Remove Bluetooth Settings if Bluetooth service is not available.
1092 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1093 category.removeTile(n);
1094 }
1095 } else if (id == R.id.data_usage_settings) {
1096 // Remove data usage when kernel module not enabled
1097 final INetworkManagementService netManager = INetworkManagementService.Stub
1098 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1099 try {
1100 if (!netManager.isBandwidthControlEnabled()) {
1101 category.removeTile(n);
1102 }
1103 } catch (RemoteException e) {
1104 // ignored
1105 }
1106 } else if (id == R.id.battery_settings) {
1107 // Remove battery settings when battery is not available. (e.g. TV)
1108
1109 if (!mBatteryPresent) {
1110 category.removeTile(n);
1111 }
1112 } else if (id == R.id.home_settings) {
1113 if (!updateHomeSettingTiles(tile)) {
1114 category.removeTile(n);
1115 }
1116 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001117 boolean hasMultipleUsers =
1118 ((UserManager) getSystemService(Context.USER_SERVICE))
1119 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001120 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001121 || (!UserManager.supportsMultipleUsers()
1122 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001123 || Utils.isMonkeyRunning()) {
1124 category.removeTile(n);
1125 }
1126 } else if (id == R.id.nfc_payment_settings) {
1127 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1128 category.removeTile(n);
1129 } else {
1130 // Only show if NFC is on and we have the HCE feature
1131 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1132 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1133 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1134 category.removeTile(n);
1135 }
1136 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001137 } else if (id == R.id.print_settings) {
1138 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1139 PackageManager.FEATURE_PRINTING);
1140 if (!hasPrintingSupport) {
1141 category.removeTile(n);
1142 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001143 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001144 if (!showDev || um.hasUserRestriction(
1145 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001146 category.removeTile(n);
1147 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001148 }
1149
1150 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001151 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1152 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001153 category.removeTile(n);
1154 }
1155
1156 n--;
1157 }
1158 }
1159 }
1160
1161 private boolean updateHomeSettingTiles(DashboardTile tile) {
1162 // Once we decide to show Home settings, keep showing it forever
1163 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1164 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1165 return true;
1166 }
1167
1168 try {
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001169 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001170 // When there's only one available home app, omit this settings
1171 // category entirely at the top level UI. If the user just
1172 // uninstalled the penultimate home app candidiate, we also
1173 // now tell them about why they aren't seeing 'Home' in the list.
1174 if (sShowNoHomeNotice) {
1175 sShowNoHomeNotice = false;
1176 NoHomeDialogFragment.show(this);
1177 }
1178 return false;
1179 } else {
1180 // Okay, we're allowing the Home settings category. Tell it, when
1181 // invoked via this front door, that we'll need to be told about the
1182 // case when the user uninstalls all but one home app.
1183 if (tile.fragmentArguments == null) {
1184 tile.fragmentArguments = new Bundle();
1185 }
1186 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1187 }
1188 } catch (Exception e) {
1189 // Can't look up the home activity; bail on configuring the icon
1190 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1191 }
1192
1193 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1194 return true;
1195 }
1196
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001197 private void getMetaData() {
1198 try {
1199 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1200 PackageManager.GET_META_DATA);
1201 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001202 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1203 } catch (NameNotFoundException nnfe) {
1204 // No recovery
1205 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1206 }
1207 }
1208
1209 // give subclasses access to the Next button
1210 public boolean hasNextButton() {
1211 return mNextButton != null;
1212 }
1213
1214 public Button getNextButton() {
1215 return mNextButton;
1216 }
1217
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001218 @Override
1219 public boolean shouldUpRecreateTask(Intent targetIntent) {
1220 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1221 }
1222
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001223 public static void requestHomeNotice() {
1224 sShowNoHomeNotice = true;
1225 }
1226
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001227 @Override
1228 public boolean onQueryTextSubmit(String query) {
1229 switchToSearchResultsFragmentIfNeeded();
1230 mSearchQuery = query;
1231 return mSearchResultsFragment.onQueryTextSubmit(query);
1232 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001233
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001234 @Override
1235 public boolean onQueryTextChange(String newText) {
1236 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001237 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001238 return false;
1239 }
1240 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001241 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001242
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001243 @Override
1244 public boolean onClose() {
1245 return false;
1246 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001247
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001248 @Override
1249 public boolean onMenuItemActionExpand(MenuItem item) {
1250 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001251 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001252 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001253 return true;
1254 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001255
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001256 @Override
1257 public boolean onMenuItemActionCollapse(MenuItem item) {
1258 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001259 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001260 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001261 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001262 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001263 return true;
1264 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001265
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001266 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001267 if (mSearchResultsFragment != null) {
1268 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001269 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001270 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001271 if (current != null && current instanceof SearchResultsSummary) {
1272 mSearchResultsFragment = (SearchResultsSummary) current;
1273 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001274 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001275 SearchResultsSummary.class.getName(), null, false, true,
1276 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001277 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001278 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001279 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001280 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001281
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001282 public void needToRevertToInitialFragment() {
1283 mNeedToRevertToInitialFragment = true;
1284 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001285
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001286 private void revertToInitialFragment() {
1287 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001288 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001289 mSearchMenuItemExpanded = false;
1290 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1291 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001292 if (mSearchMenuItem != null) {
1293 mSearchMenuItem.collapseActionView();
1294 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001295 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001296}