blob: 76a730078bd50b610161065ff2a5d188e0c2ff03 [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;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000069import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070071import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.applications.ManageApplications;
73import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.bluetooth.BluetoothSettings;
PauloftheWestc9dc66d2014-07-01 05:19:58 -070075import com.android.settings.bluetooth.MessageAccessSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070076import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070078import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070079import com.android.settings.dashboard.NoHomeDialogFragment;
80import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.deviceinfo.Memory;
82import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040083import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084import com.android.settings.fuelgauge.PowerUsageSummary;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070085import com.android.settings.notification.AppNotificationSettings;
86import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -070087import com.android.settings.quicklaunch.QuickLaunchSettings;
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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700120import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700122import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700123
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124public class SettingsActivity extends Activity
125 implements PreferenceManager.OnPreferenceTreeClickListener,
126 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100127 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700128 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100129 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800130
131 private static final String LOG_TAG = "Settings";
132
133 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700134 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700135 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
136 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700137 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700138 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700139 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800140
141 /**
142 * When starting this activity, the invoking Intent can contain this extra
143 * string to specify which fragment should be initially displayed.
144 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
145 * will call isValidFragment() to confirm that the fragment class name is valid for this
146 * activity.
147 */
148 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
149
150 /**
151 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
152 * this extra can also be specified to supply a Bundle of arguments to pass
153 * to that fragment when it is instantiated during the initial creation
154 * of the activity.
155 */
156 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
157
158 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700159 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
160 */
161 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
162
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800163 public static final String BACK_STACK_PREFS = ":settings:prefs";
164
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165 // extras that allow any preference activity to be launched as part of a wizard
166
167 // show Back and Next buttons? takes boolean parameter
168 // Back will then return RESULT_CANCELED and Next RESULT_OK
169 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
170
171 // add a Skip button?
172 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
173
174 // specify custom text for the Back or Next buttons, or cause a button to not appear
175 // at all by setting it to null
176 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
177 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
178
179 /**
180 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700181 * 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 -0800182 * that fragment.
183 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700184 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700186 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800187
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188 private static final String META_DATA_KEY_FRAGMENT_CLASS =
189 "com.android.settings.FRAGMENT_CLASS";
190
191 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
192
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700193 private static final String EMPTY_QUERY = "";
194
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 private static boolean sShowNoHomeNotice = false;
196
197 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800199 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700200 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 // Show only these settings for restricted users
203 private int[] SETTINGS_FOR_RESTRICTED = {
204 R.id.wireless_section,
205 R.id.wifi_settings,
206 R.id.bluetooth_settings,
207 R.id.data_usage_settings,
208 R.id.wireless_settings,
209 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400210 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 R.id.display_settings,
212 R.id.storage_settings,
213 R.id.application_settings,
214 R.id.battery_settings,
215 R.id.personal_section,
216 R.id.location_settings,
217 R.id.security_settings,
218 R.id.language_settings,
219 R.id.user_settings,
220 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800221 R.id.system_section,
222 R.id.date_time_settings,
223 R.id.about_settings,
224 R.id.accessibility_settings,
225 R.id.print_settings,
226 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800227 R.id.home_settings,
228 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800229 };
230
231 private static final String[] ENTRY_FRAGMENTS = {
232 WirelessSettings.class.getName(),
233 WifiSettings.class.getName(),
234 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700235 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800236 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700237 MessageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800238 TetherSettings.class.getName(),
239 WifiP2pSettings.class.getName(),
240 VpnSettings.class.getName(),
241 DateTimeSettings.class.getName(),
242 LocalePicker.class.getName(),
243 InputMethodAndLanguageSettings.class.getName(),
244 SpellCheckersSettings.class.getName(),
245 UserDictionaryList.class.getName(),
246 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700247 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248 DisplaySettings.class.getName(),
249 DeviceInfoSettings.class.getName(),
250 ManageApplications.class.getName(),
251 ProcessStatsUi.class.getName(),
252 NotificationStation.class.getName(),
253 LocationSettings.class.getName(),
254 SecuritySettings.class.getName(),
255 PrivacySettings.class.getName(),
256 DeviceAdminSettings.class.getName(),
257 AccessibilitySettings.class.getName(),
258 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
260 TextToSpeechSettings.class.getName(),
261 Memory.class.getName(),
262 DevelopmentSettings.class.getName(),
263 UsbSettings.class.getName(),
264 AndroidBeam.class.getName(),
265 WifiDisplaySettings.class.getName(),
266 PowerUsageSummary.class.getName(),
267 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000268 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 CryptKeeperSettings.class.getName(),
270 DataUsageSummary.class.getName(),
271 DreamSettings.class.getName(),
272 UserSettings.class.getName(),
273 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400274 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800275 PrintSettingsFragment.class.getName(),
276 PrintJobSettingsFragment.class.getName(),
277 TrustedCredentialsSettings.class.getName(),
278 PaymentSettings.class.getName(),
279 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700280 ZenModeSettings.class.getName(),
281 NotificationSettings.class.getName(),
282 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
283 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400284 InstalledAppDetails.class.getName(),
285 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700286 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700287 OtherSoundSettings.class.getName(),
288 QuickLaunchSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 };
290
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700291
292 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
293 "android.settings.APPLICATION_DETAILS_SETTINGS"
294 };
295
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296 private SharedPreferences mDevelopmentPreferences;
297 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
298
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 private boolean mBatteryPresent = true;
300 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 @Override
302 public void onReceive(Context context, Intent intent) {
303 String action = intent.getAction();
304 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
305 boolean batteryPresent = Utils.isBatteryPresent(intent);
306
307 if (mBatteryPresent != batteryPresent) {
308 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700309 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 }
311 }
312 }
313 };
314
Svetoslav990159a2014-04-14 17:14:59 -0700315 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
316 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700317
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700318 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700319 private SwitchBar mSwitchBar;
320
321 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700322
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700323 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700324 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700325
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700326 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700327 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700328
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700329 private ViewGroup mContent;
330
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700331 private SearchView mSearchView;
332 private MenuItem mSearchMenuItem;
333 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700334 private SearchResultsSummary mSearchResultsFragment;
335 private String mSearchQuery;
336
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700337 // Categories
338 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800339
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700340 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700341 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 private Handler mHandler = new Handler() {
343 @Override
344 public void handleMessage(Message msg) {
345 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700346 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700347 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
348 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700349 buildDashboardCategories(mCategories);
350 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351 } break;
352 }
353 }
354 };
355
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700356 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700357 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700358
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700359 public SwitchBar getSwitchBar() {
360 return mSwitchBar;
361 }
362
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700363 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
364 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700365 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700366 }
367 return mCategories;
368 }
369
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 @Override
371 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
372 // Override the fragment title for Wallpaper settings
373 int titleRes = pref.getTitleRes();
374 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
375 titleRes = R.string.wallpaper_settings_fragment_title;
376 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
377 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
378 if (UserManager.get(this).isLinkedUser()) {
379 titleRes = R.string.profile_info_settings_title;
380 } else {
381 titleRes = R.string.user_info_settings_title;
382 }
383 }
384 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
385 null, 0);
386 return true;
387 }
388
389 @Override
390 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
391 return false;
392 }
393
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700394 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700395 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700396 Message msg = new Message();
397 msg.what = MSG_BUILD_CATEGORIES;
398 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800399 }
400 }
401
402 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800403 public void onConfigurationChanged(Configuration newConfig) {
404 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800405 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800406 }
407
408 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700409 protected void onStart() {
410 super.onStart();
411
412 if (mNeedToRevertToInitialFragment) {
413 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800414 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800415 }
416
417 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700418 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700419 if (!mDisplaySearch) {
420 return false;
421 }
422
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700423 MenuInflater inflater = getMenuInflater();
424 inflater.inflate(R.menu.options_menu, menu);
425
426 // Cache the search query (can be overriden by the OnQueryTextListener)
427 final String query = mSearchQuery;
428
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700429 mSearchMenuItem = menu.findItem(R.id.search);
430 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700431
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700432 if (mSearchMenuItem == null || mSearchView == null) {
433 return false;
434 }
435
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700436 if (mSearchResultsFragment != null) {
437 mSearchResultsFragment.setSearchView(mSearchView);
438 }
439
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700440 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700441 mSearchView.setOnQueryTextListener(this);
442 mSearchView.setOnCloseListener(this);
443
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700444 if (mSearchMenuItemExpanded) {
445 mSearchMenuItem.expandActionView();
446 }
447 mSearchView.setQuery(query, true /* submit */);
448
449 return true;
450 }
451
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700452 private static boolean isShortCutIntent(final Intent intent) {
453 Set<String> categories = intent.getCategories();
454 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
455 }
456
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700457 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700458 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700459 if (action == null) {
460 return false;
461 }
462 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
463 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
464 }
465 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700466 }
467
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700468 @Override
469 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700470 super.onCreate(savedState);
471
472 // Should happen before any call to getIntent()
473 getMetaData();
474
475 final Intent intent = getIntent();
476 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
477 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800479
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800480 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
481 Context.MODE_PRIVATE);
482
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700483 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700484 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700485
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700486 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700487 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
488
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700489 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700490 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700491
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700492 mIsShowingDashboard = className.equals(Settings.class.getName());
493 final boolean isSubSettings = className.equals(SubSettings.class.getName());
494
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700495 // If this is a sub settings or not the main Dashboard and not a Shortcut and an initial
496 // Fragment then apply the SubSettings theme for the ActionBar content insets
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700497 if (isSubSettings ||
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700498 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName != null))) {
499 // Check also that we are not a Theme Dialog as we don't want to override them
500 final int themeResId = getThemeResId();
501 if (themeResId != R.style.Theme_DialogWhenLarge &&
502 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
503 setTheme(R.style.Theme_SubSettings);
504 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700505 }
506
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700507 setContentView(mIsShowingDashboard ?
508 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700510 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700511
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512 getFragmentManager().addOnBackStackChangedListener(this);
513
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700514 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700515 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700516 }
517
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700518 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700519 // We are restarting from a previous saved state; used that to initialize, instead
520 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700521 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
522 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800523
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700524 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800525
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700526 ArrayList<DashboardCategory> categories =
527 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
528 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700529 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700530 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700531 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700533
534 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700535 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700536 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
537 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700539 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700540 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
541 // only if it is a sub settings
542 if (mIsShortcut) {
543 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700544 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700545 } else if (isSubSettings) {
546 mDisplayHomeAsUpEnabled = true;
547 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700548 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700549 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700550
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700551 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700552 switchToFragment(initialFragmentName, initialArguments, true, false,
553 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000554 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700555 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700556 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700557 // Show Search affordance
558 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700559 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700560 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700561 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 }
563 }
564
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700565 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700566 if (mActionBar != null) {
567 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
568 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
569 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700570 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
571
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800572 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
574
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700575 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 if (buttonBar != null) {
577 buttonBar.setVisibility(View.VISIBLE);
578
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700579 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580 backButton.setOnClickListener(new OnClickListener() {
581 public void onClick(View v) {
582 setResult(RESULT_CANCELED);
583 finish();
584 }
585 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700586 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800587 skipButton.setOnClickListener(new OnClickListener() {
588 public void onClick(View v) {
589 setResult(RESULT_OK);
590 finish();
591 }
592 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700593 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 mNextButton.setOnClickListener(new OnClickListener() {
595 public void onClick(View v) {
596 setResult(RESULT_OK);
597 finish();
598 }
599 });
600
601 // set our various button parameters
602 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
603 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
604 if (TextUtils.isEmpty(buttonText)) {
605 mNextButton.setVisibility(View.GONE);
606 }
607 else {
608 mNextButton.setText(buttonText);
609 }
610 }
611 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
612 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
613 if (TextUtils.isEmpty(buttonText)) {
614 backButton.setVisibility(View.GONE);
615 }
616 else {
617 backButton.setText(buttonText);
618 }
619 }
620 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
621 skipButton.setVisibility(View.VISIBLE);
622 }
623 }
624 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700625
626 mHomeActivitiesCount = getHomeActivitiesCount();
627 }
628
629 private int getHomeActivitiesCount() {
630 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
631 getPackageManager().getHomeActivities(homeApps);
632 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800633 }
634
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700635 private void setTitleFromIntent(Intent intent) {
636 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
637 if (initialTitleResId > 0) {
638 mInitialTitle = null;
639 mInitialTitleResId = initialTitleResId;
640 setTitle(mInitialTitleResId);
641 } else {
642 mInitialTitleResId = -1;
643 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
644 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
645 setTitle(mInitialTitle);
646 }
647 }
648
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800649 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700651 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 }
653
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700654 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800655 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700656
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800657 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700658 if (mInitialTitleResId > 0) {
659 setTitle(mInitialTitleResId);
660 } else {
661 setTitle(mInitialTitle);
662 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700663 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800664 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700665
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800666 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
667 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700668
669 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800670 }
671
672 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
673 final CharSequence title;
674 final int titleRes = bse.getBreadCrumbTitleRes();
675 if (titleRes > 0) {
676 title = getText(titleRes);
677 } else {
678 title = bse.getBreadCrumbTitle();
679 }
680 if (title != null) {
681 setTitle(title);
682 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800683 }
684
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800685 @Override
686 protected void onSaveInstanceState(Bundle outState) {
687 super.onSaveInstanceState(outState);
688
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700689 if (mCategories.size() > 0) {
690 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800691 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700692
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700693 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700694 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700695
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700696 if (mDisplaySearch) {
697 // The option menus are created if the ActionBar is visible and they are also created
698 // asynchronously. If you launch Settings with an Intent action like
699 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
700 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
701 // menu item and search view are null.
702 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
703 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700704
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700705 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
706 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
707 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700708
709 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800710 }
711
712 @Override
713 public void onResume() {
714 super.onResume();
715
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700716 final int newHomeActivityCount = getHomeActivitiesCount();
717 if (newHomeActivityCount != mHomeActivitiesCount) {
718 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700719 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700720 }
721
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800722 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
723 @Override
724 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700725 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800726 }
727 };
728 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
729 mDevelopmentPreferencesListener);
730
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800731 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700732
Svetoslav990159a2014-04-14 17:14:59 -0700733 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700734
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700735 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700736 onQueryTextSubmit(mSearchQuery);
737 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800738 }
739
740 @Override
741 public void onPause() {
742 super.onPause();
743
744 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700745 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800746 }
747
748 @Override
749 public void onDestroy() {
750 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700751
752 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
753 mDevelopmentPreferencesListener);
754 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800755 }
756
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800757 protected boolean isValidFragment(String fragmentName) {
758 // Almost all fragments are wrapped in this,
759 // except for a few that have their own activities.
760 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
761 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
762 }
763 return false;
764 }
765
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800766 @Override
767 public Intent getIntent() {
768 Intent superIntent = super.getIntent();
769 String startingFragment = getStartingFragmentClass(superIntent);
770 // This is called from super.onCreate, isMultiPane() is not yet reliable
771 // Do not use onIsHidingHeaders either, which relies itself on this method
772 if (startingFragment != null) {
773 Intent modIntent = new Intent(superIntent);
774 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
775 Bundle args = superIntent.getExtras();
776 if (args != null) {
777 args = new Bundle(args);
778 } else {
779 args = new Bundle();
780 }
781 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100782 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800783 return modIntent;
784 }
785 return superIntent;
786 }
787
788 /**
789 * Checks if the component name in the intent is different from the Settings class and
790 * returns the class name to load as a fragment.
791 */
792 private String getStartingFragmentClass(Intent intent) {
793 if (mFragmentClass != null) return mFragmentClass;
794
795 String intentClass = intent.getComponent().getClassName();
796 if (intentClass.equals(getClass().getName())) return null;
797
798 if ("com.android.settings.ManageApplications".equals(intentClass)
799 || "com.android.settings.RunningServices".equals(intentClass)
800 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
801 // Old names of manage apps.
802 intentClass = com.android.settings.applications.ManageApplications.class.getName();
803 }
804
805 return intentClass;
806 }
807
808 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000809 * Start a new fragment containing a preference panel. If the preferences
810 * are being displayed in multi-pane mode, the given fragment class will
811 * be instantiated and placed in the appropriate pane. If running in
812 * single-pane mode, a new activity will be launched in which to show the
813 * fragment.
814 *
815 * @param fragmentClass Full name of the class implementing the fragment.
816 * @param args Any desired arguments to supply to the fragment.
817 * @param titleRes Optional resource identifier of the title of this
818 * fragment.
819 * @param titleText Optional text of the title of this fragment.
820 * @param resultTo Optional fragment that result data should be sent to.
821 * If non-null, resultTo.onActivityResult() will be called when this
822 * preference panel is done. The launched panel must use
823 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
824 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700825 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000826 */
827 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700828 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700829 String title = null;
830 if (titleRes < 0) {
831 if (titleText != null) {
832 title = titleText.toString();
833 } else {
834 // There not much we can do in that case
835 title = "";
836 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700837 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700838 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700839 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000840 }
841
842 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800843 * Called by a preference panel fragment to finish itself.
844 *
845 * @param caller The fragment that is asking to be finished.
846 * @param resultCode Optional result code to send back to the original
847 * launching fragment.
848 * @param resultData Optional result data to send back to the original
849 * launching fragment.
850 */
851 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
852 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700853 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 }
855
856 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000857 * Start a new fragment.
858 *
859 * @param fragment The fragment to start
860 * @param push If true, the current fragment will be pushed onto the back stack. If false,
861 * the current fragment will be replaced.
862 */
863 public void startPreferenceFragment(Fragment fragment, boolean push) {
864 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700865 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000866 if (push) {
867 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
868 transaction.addToBackStack(BACK_STACK_PREFS);
869 } else {
870 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
871 }
872 transaction.commitAllowingStateLoss();
873 }
874
875 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700876 * Switch to a specific Fragment with taking care of validation, Title and BackStack
877 */
878 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700879 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700880 if (validate && !isValidFragment(fragmentName)) {
881 throw new IllegalArgumentException("Invalid fragment for this activity: "
882 + fragmentName);
883 }
884 Fragment f = Fragment.instantiate(this, fragmentName, args);
885 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700886 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700887 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700888 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700889 }
890 if (addToBackStack) {
891 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
892 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700893 if (titleResId > 0) {
894 transaction.setBreadCrumbTitle(titleResId);
895 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700896 transaction.setBreadCrumbTitle(title);
897 }
898 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700899 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700900 return f;
901 }
902
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700903 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700904 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700905 *
906 * @param categories The list in which to place the tiles categories.
907 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700908 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700909 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700910 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
911 updateTilesList(categories);
912 }
913
914 /**
915 * Parse the given XML file as a categories description, adding each
916 * parsed categories and tiles into the target list.
917 *
918 * @param resid The XML resource to load and parse.
919 * @param target The list in which the parsed categories and tiles should be placed.
920 */
921 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
922 XmlResourceParser parser = null;
923 try {
924 parser = getResources().getXml(resid);
925 AttributeSet attrs = Xml.asAttributeSet(parser);
926
927 int type;
928 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
929 && type != XmlPullParser.START_TAG) {
930 // Parse next until start tag is found
931 }
932
933 String nodeName = parser.getName();
934 if (!"dashboard-categories".equals(nodeName)) {
935 throw new RuntimeException(
936 "XML document must start with <preference-categories> tag; found"
937 + nodeName + " at " + parser.getPositionDescription());
938 }
939
940 Bundle curBundle = null;
941
942 final int outerDepth = parser.getDepth();
943 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
944 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
945 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
946 continue;
947 }
948
949 nodeName = parser.getName();
950 if ("dashboard-category".equals(nodeName)) {
951 DashboardCategory category = new DashboardCategory();
952
953 TypedArray sa = obtainStyledAttributes(
954 attrs, com.android.internal.R.styleable.PreferenceHeader);
955 category.id = sa.getResourceId(
956 com.android.internal.R.styleable.PreferenceHeader_id,
957 (int)DashboardCategory.CAT_ID_UNDEFINED);
958
959 TypedValue tv = sa.peekValue(
960 com.android.internal.R.styleable.PreferenceHeader_title);
961 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
962 if (tv.resourceId != 0) {
963 category.titleRes = tv.resourceId;
964 } else {
965 category.title = tv.string;
966 }
967 }
968 sa.recycle();
969
970 final int innerDepth = parser.getDepth();
971 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
972 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
973 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
974 continue;
975 }
976
977 String innerNodeName = parser.getName();
978 if (innerNodeName.equals("dashboard-tile")) {
979 DashboardTile tile = new DashboardTile();
980
981 sa = obtainStyledAttributes(
982 attrs, com.android.internal.R.styleable.PreferenceHeader);
983 tile.id = sa.getResourceId(
984 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700985 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700986 tv = sa.peekValue(
987 com.android.internal.R.styleable.PreferenceHeader_title);
988 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
989 if (tv.resourceId != 0) {
990 tile.titleRes = tv.resourceId;
991 } else {
992 tile.title = tv.string;
993 }
994 }
995 tv = sa.peekValue(
996 com.android.internal.R.styleable.PreferenceHeader_summary);
997 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
998 if (tv.resourceId != 0) {
999 tile.summaryRes = tv.resourceId;
1000 } else {
1001 tile.summary = tv.string;
1002 }
1003 }
1004 tile.iconRes = sa.getResourceId(
1005 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1006 tile.fragment = sa.getString(
1007 com.android.internal.R.styleable.PreferenceHeader_fragment);
1008 sa.recycle();
1009
1010 if (curBundle == null) {
1011 curBundle = new Bundle();
1012 }
1013
1014 final int innerDepth2 = parser.getDepth();
1015 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1016 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1017 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1018 continue;
1019 }
1020
1021 String innerNodeName2 = parser.getName();
1022 if (innerNodeName2.equals("extra")) {
1023 getResources().parseBundleExtra("extra", attrs, curBundle);
1024 XmlUtils.skipCurrentTag(parser);
1025
1026 } else if (innerNodeName2.equals("intent")) {
1027 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1028
1029 } else {
1030 XmlUtils.skipCurrentTag(parser);
1031 }
1032 }
1033
1034 if (curBundle.size() > 0) {
1035 tile.fragmentArguments = curBundle;
1036 curBundle = null;
1037 }
1038
1039 category.addTile(tile);
1040
1041 } else {
1042 XmlUtils.skipCurrentTag(parser);
1043 }
1044 }
1045
1046 target.add(category);
1047 } else {
1048 XmlUtils.skipCurrentTag(parser);
1049 }
1050 }
1051
1052 } catch (XmlPullParserException e) {
1053 throw new RuntimeException("Error parsing categories", e);
1054 } catch (IOException e) {
1055 throw new RuntimeException("Error parsing categories", e);
1056 } finally {
1057 if (parser != null) parser.close();
1058 }
1059 }
1060
1061 private void updateTilesList(List<DashboardCategory> target) {
1062 final boolean showDev = mDevelopmentPreferences.getBoolean(
1063 DevelopmentSettings.PREF_SHOW,
1064 android.os.Build.TYPE.equals("eng"));
1065
1066 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1067
1068 final int size = target.size();
1069 for (int i = 0; i < size; i++) {
1070
1071 DashboardCategory category = target.get(i);
1072
1073 // Ids are integers, so downcasting is ok
1074 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001075 int n = category.getTilesCount() - 1;
1076 while (n >= 0) {
1077
1078 DashboardTile tile = category.getTile(n);
1079
1080 id = (int) tile.id;
1081 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1082 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1083 } else if (id == R.id.wifi_settings) {
1084 // Remove WiFi Settings if WiFi service is not available.
1085 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1086 category.removeTile(n);
1087 }
1088 } else if (id == R.id.bluetooth_settings) {
1089 // Remove Bluetooth Settings if Bluetooth service is not available.
1090 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1091 category.removeTile(n);
1092 }
1093 } else if (id == R.id.data_usage_settings) {
1094 // Remove data usage when kernel module not enabled
1095 final INetworkManagementService netManager = INetworkManagementService.Stub
1096 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1097 try {
1098 if (!netManager.isBandwidthControlEnabled()) {
1099 category.removeTile(n);
1100 }
1101 } catch (RemoteException e) {
1102 // ignored
1103 }
1104 } else if (id == R.id.battery_settings) {
1105 // Remove battery settings when battery is not available. (e.g. TV)
1106
1107 if (!mBatteryPresent) {
1108 category.removeTile(n);
1109 }
1110 } else if (id == R.id.home_settings) {
1111 if (!updateHomeSettingTiles(tile)) {
1112 category.removeTile(n);
1113 }
1114 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001115 boolean hasMultipleUsers =
1116 ((UserManager) getSystemService(Context.USER_SERVICE))
1117 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001118 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001119 || (!UserManager.supportsMultipleUsers()
1120 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001121 || Utils.isMonkeyRunning()) {
1122 category.removeTile(n);
1123 }
1124 } else if (id == R.id.nfc_payment_settings) {
1125 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1126 category.removeTile(n);
1127 } else {
1128 // Only show if NFC is on and we have the HCE feature
1129 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1130 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1131 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1132 category.removeTile(n);
1133 }
1134 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001135 } else if (id == R.id.print_settings) {
1136 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1137 PackageManager.FEATURE_PRINTING);
1138 if (!hasPrintingSupport) {
1139 category.removeTile(n);
1140 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001141 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001142 if (!showDev || um.hasUserRestriction(
1143 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001144 category.removeTile(n);
1145 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001146 }
1147
1148 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001149 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1150 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001151 category.removeTile(n);
1152 }
1153
1154 n--;
1155 }
1156 }
1157 }
1158
1159 private boolean updateHomeSettingTiles(DashboardTile tile) {
1160 // Once we decide to show Home settings, keep showing it forever
1161 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1162 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1163 return true;
1164 }
1165
1166 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001167 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001168 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001169 // When there's only one available home app, omit this settings
1170 // category entirely at the top level UI. If the user just
1171 // uninstalled the penultimate home app candidiate, we also
1172 // now tell them about why they aren't seeing 'Home' in the list.
1173 if (sShowNoHomeNotice) {
1174 sShowNoHomeNotice = false;
1175 NoHomeDialogFragment.show(this);
1176 }
1177 return false;
1178 } else {
1179 // Okay, we're allowing the Home settings category. Tell it, when
1180 // invoked via this front door, that we'll need to be told about the
1181 // case when the user uninstalls all but one home app.
1182 if (tile.fragmentArguments == null) {
1183 tile.fragmentArguments = new Bundle();
1184 }
1185 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1186 }
1187 } catch (Exception e) {
1188 // Can't look up the home activity; bail on configuring the icon
1189 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1190 }
1191
1192 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1193 return true;
1194 }
1195
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001196 private void getMetaData() {
1197 try {
1198 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1199 PackageManager.GET_META_DATA);
1200 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001201 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1202 } catch (NameNotFoundException nnfe) {
1203 // No recovery
1204 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1205 }
1206 }
1207
1208 // give subclasses access to the Next button
1209 public boolean hasNextButton() {
1210 return mNextButton != null;
1211 }
1212
1213 public Button getNextButton() {
1214 return mNextButton;
1215 }
1216
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001217 @Override
1218 public boolean shouldUpRecreateTask(Intent targetIntent) {
1219 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1220 }
1221
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001222 public static void requestHomeNotice() {
1223 sShowNoHomeNotice = true;
1224 }
1225
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001226 @Override
1227 public boolean onQueryTextSubmit(String query) {
1228 switchToSearchResultsFragmentIfNeeded();
1229 mSearchQuery = query;
1230 return mSearchResultsFragment.onQueryTextSubmit(query);
1231 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001232
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001233 @Override
1234 public boolean onQueryTextChange(String newText) {
1235 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001236 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001237 return false;
1238 }
1239 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001240 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001241
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001242 @Override
1243 public boolean onClose() {
1244 return false;
1245 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001246
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001247 @Override
1248 public boolean onMenuItemActionExpand(MenuItem item) {
1249 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001250 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001251 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001252 return true;
1253 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001254
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001255 @Override
1256 public boolean onMenuItemActionCollapse(MenuItem item) {
1257 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001258 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001259 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001260 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001261 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001262 return true;
1263 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001264
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001265 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001266 if (mSearchResultsFragment != null) {
1267 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001268 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001269 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001270 if (current != null && current instanceof SearchResultsSummary) {
1271 mSearchResultsFragment = (SearchResultsSummary) current;
1272 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001273 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001274 SearchResultsSummary.class.getName(), null, false, true,
1275 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001276 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001277 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001278 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001279 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001280
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001281 public void needToRevertToInitialFragment() {
1282 mNeedToRevertToInitialFragment = true;
1283 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001284
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001285 private void revertToInitialFragment() {
1286 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001287 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001288 mSearchMenuItemExpanded = false;
1289 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1290 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001291 if (mSearchMenuItem != null) {
1292 mSearchMenuItem.collapseActionView();
1293 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001294 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001295}