blob: 520bfba06218d80ec7f5c2ab0e80137e383adcac [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;
PauloftheWest38155612014-06-30 10:02:36 -0700104import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800105import com.android.settings.tts.TextToSpeechSettings;
106import com.android.settings.users.UserSettings;
107import com.android.settings.vpn2.VpnSettings;
108import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700109import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700111import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.wifi.WifiSettings;
113import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400114
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import org.xmlpull.v1.XmlPullParser;
116import org.xmlpull.v1.XmlPullParserException;
117
118import java.io.IOException;
119import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700121import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700123import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700124
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125public class SettingsActivity extends Activity
126 implements PreferenceManager.OnPreferenceTreeClickListener,
127 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100128 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700129 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100130 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131
132 private static final String LOG_TAG = "Settings";
133
134 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700135 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
137 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700138 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700139 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700140 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 /**
143 * When starting this activity, the invoking Intent can contain this extra
144 * string to specify which fragment should be initially displayed.
145 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
146 * will call isValidFragment() to confirm that the fragment class name is valid for this
147 * activity.
148 */
149 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
150
151 /**
152 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
153 * this extra can also be specified to supply a Bundle of arguments to pass
154 * to that fragment when it is instantiated during the initial creation
155 * of the activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
158
159 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700160 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
161 */
162 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
163
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800164 public static final String BACK_STACK_PREFS = ":settings:prefs";
165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 // extras that allow any preference activity to be launched as part of a wizard
167
168 // show Back and Next buttons? takes boolean parameter
169 // Back will then return RESULT_CANCELED and Next RESULT_OK
170 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
171
172 // add a Skip button?
173 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
174
175 // specify custom text for the Back or Next buttons, or cause a button to not appear
176 // at all by setting it to null
177 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
178 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
179
180 /**
181 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700182 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800183 * that fragment.
184 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700187 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800189 private static final String META_DATA_KEY_FRAGMENT_CLASS =
190 "com.android.settings.FRAGMENT_CLASS";
191
192 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
193
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700194 private static final String EMPTY_QUERY = "";
195
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196 private static boolean sShowNoHomeNotice = false;
197
198 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800199
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800200 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700201 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800202
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203 // Show only these settings for restricted users
204 private int[] SETTINGS_FOR_RESTRICTED = {
205 R.id.wireless_section,
206 R.id.wifi_settings,
207 R.id.bluetooth_settings,
208 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700209 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210 R.id.wireless_settings,
211 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400212 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800213 R.id.display_settings,
214 R.id.storage_settings,
215 R.id.application_settings,
216 R.id.battery_settings,
217 R.id.personal_section,
218 R.id.location_settings,
219 R.id.security_settings,
220 R.id.language_settings,
221 R.id.user_settings,
222 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800223 R.id.system_section,
224 R.id.date_time_settings,
225 R.id.about_settings,
226 R.id.accessibility_settings,
227 R.id.print_settings,
228 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800229 R.id.home_settings,
230 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800231 };
232
233 private static final String[] ENTRY_FRAGMENTS = {
234 WirelessSettings.class.getName(),
235 WifiSettings.class.getName(),
236 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700237 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800238 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700239 MessageAccessSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700240 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800241 TetherSettings.class.getName(),
242 WifiP2pSettings.class.getName(),
243 VpnSettings.class.getName(),
244 DateTimeSettings.class.getName(),
245 LocalePicker.class.getName(),
246 InputMethodAndLanguageSettings.class.getName(),
247 SpellCheckersSettings.class.getName(),
248 UserDictionaryList.class.getName(),
249 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700250 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 DisplaySettings.class.getName(),
252 DeviceInfoSettings.class.getName(),
253 ManageApplications.class.getName(),
254 ProcessStatsUi.class.getName(),
255 NotificationStation.class.getName(),
256 LocationSettings.class.getName(),
257 SecuritySettings.class.getName(),
258 PrivacySettings.class.getName(),
259 DeviceAdminSettings.class.getName(),
260 AccessibilitySettings.class.getName(),
261 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800262 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
263 TextToSpeechSettings.class.getName(),
264 Memory.class.getName(),
265 DevelopmentSettings.class.getName(),
266 UsbSettings.class.getName(),
267 AndroidBeam.class.getName(),
268 WifiDisplaySettings.class.getName(),
269 PowerUsageSummary.class.getName(),
270 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000271 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272 CryptKeeperSettings.class.getName(),
273 DataUsageSummary.class.getName(),
274 DreamSettings.class.getName(),
275 UserSettings.class.getName(),
276 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400277 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 PrintSettingsFragment.class.getName(),
279 PrintJobSettingsFragment.class.getName(),
280 TrustedCredentialsSettings.class.getName(),
281 PaymentSettings.class.getName(),
282 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700283 ZenModeSettings.class.getName(),
284 NotificationSettings.class.getName(),
285 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
286 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400287 InstalledAppDetails.class.getName(),
288 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700289 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700290 OtherSoundSettings.class.getName(),
291 QuickLaunchSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800292 };
293
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700294
295 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
296 "android.settings.APPLICATION_DETAILS_SETTINGS"
297 };
298
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 private SharedPreferences mDevelopmentPreferences;
300 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
301
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 private boolean mBatteryPresent = true;
303 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 @Override
305 public void onReceive(Context context, Intent intent) {
306 String action = intent.getAction();
307 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
308 boolean batteryPresent = Utils.isBatteryPresent(intent);
309
310 if (mBatteryPresent != batteryPresent) {
311 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700312 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 }
314 }
315 }
316 };
317
Svetoslav990159a2014-04-14 17:14:59 -0700318 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
319 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700320
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700321 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700322 private SwitchBar mSwitchBar;
323
324 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700325
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700326 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700327 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700328
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700329 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700330 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700331
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700332 private ViewGroup mContent;
333
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700334 private SearchView mSearchView;
335 private MenuItem mSearchMenuItem;
336 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700337 private SearchResultsSummary mSearchResultsFragment;
338 private String mSearchQuery;
339
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700340 // Categories
341 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700343 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700344 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 private Handler mHandler = new Handler() {
346 @Override
347 public void handleMessage(Message msg) {
348 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700349 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700350 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
351 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700352 buildDashboardCategories(mCategories);
353 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 } break;
355 }
356 }
357 };
358
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700359 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700360 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700361
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700362 public SwitchBar getSwitchBar() {
363 return mSwitchBar;
364 }
365
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700366 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
367 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700368 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700369 }
370 return mCategories;
371 }
372
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 @Override
374 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
375 // Override the fragment title for Wallpaper settings
376 int titleRes = pref.getTitleRes();
377 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
378 titleRes = R.string.wallpaper_settings_fragment_title;
379 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
380 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
381 if (UserManager.get(this).isLinkedUser()) {
382 titleRes = R.string.profile_info_settings_title;
383 } else {
384 titleRes = R.string.user_info_settings_title;
385 }
386 }
387 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
388 null, 0);
389 return true;
390 }
391
392 @Override
393 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
394 return false;
395 }
396
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700397 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700398 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700399 Message msg = new Message();
400 msg.what = MSG_BUILD_CATEGORIES;
401 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800402 }
403 }
404
405 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800406 public void onConfigurationChanged(Configuration newConfig) {
407 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800408 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800409 }
410
411 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700412 protected void onStart() {
413 super.onStart();
414
415 if (mNeedToRevertToInitialFragment) {
416 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800417 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800418 }
419
420 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700422 if (!mDisplaySearch) {
423 return false;
424 }
425
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700426 MenuInflater inflater = getMenuInflater();
427 inflater.inflate(R.menu.options_menu, menu);
428
429 // Cache the search query (can be overriden by the OnQueryTextListener)
430 final String query = mSearchQuery;
431
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700432 mSearchMenuItem = menu.findItem(R.id.search);
433 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700434
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700435 if (mSearchMenuItem == null || mSearchView == null) {
436 return false;
437 }
438
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700439 if (mSearchResultsFragment != null) {
440 mSearchResultsFragment.setSearchView(mSearchView);
441 }
442
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700443 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700444 mSearchView.setOnQueryTextListener(this);
445 mSearchView.setOnCloseListener(this);
446
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700447 if (mSearchMenuItemExpanded) {
448 mSearchMenuItem.expandActionView();
449 }
450 mSearchView.setQuery(query, true /* submit */);
451
452 return true;
453 }
454
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700455 private static boolean isShortCutIntent(final Intent intent) {
456 Set<String> categories = intent.getCategories();
457 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
458 }
459
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700460 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700461 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700462 if (action == null) {
463 return false;
464 }
465 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
466 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
467 }
468 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700469 }
470
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700471 @Override
472 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700473 super.onCreate(savedState);
474
475 // Should happen before any call to getIntent()
476 getMetaData();
477
478 final Intent intent = getIntent();
479 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
480 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800482
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800483 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
484 Context.MODE_PRIVATE);
485
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700486 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700487 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700488
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700489 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700490 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
491
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700492 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700493 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700494
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700495 mIsShowingDashboard = className.equals(Settings.class.getName());
496 final boolean isSubSettings = className.equals(SubSettings.class.getName());
497
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700498 // If this is a sub settings or not the main Dashboard and not a Shortcut and an initial
499 // Fragment then apply the SubSettings theme for the ActionBar content insets
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700500 if (isSubSettings ||
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700501 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName != null))) {
502 // Check also that we are not a Theme Dialog as we don't want to override them
503 final int themeResId = getThemeResId();
504 if (themeResId != R.style.Theme_DialogWhenLarge &&
505 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
506 setTheme(R.style.Theme_SubSettings);
507 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700508 }
509
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700510 setContentView(mIsShowingDashboard ?
511 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700513 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700514
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515 getFragmentManager().addOnBackStackChangedListener(this);
516
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700517 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700518 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700519 }
520
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700521 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700522 // We are restarting from a previous saved state; used that to initialize, instead
523 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700524 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
525 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700527 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800528
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700529 ArrayList<DashboardCategory> categories =
530 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
531 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700532 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700533 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700534 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700536
537 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700538 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700539 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
540 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800541 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700542 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700543 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
544 // only if it is a sub settings
545 if (mIsShortcut) {
546 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700547 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700548 } else if (isSubSettings) {
549 mDisplayHomeAsUpEnabled = true;
550 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700551 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700552 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700553
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700554 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700555 switchToFragment(initialFragmentName, initialArguments, true, false,
556 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000557 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700558 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700559 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700560 // Show Search affordance
561 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700562 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700563 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700564 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800565 }
566 }
567
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700568 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700569 if (mActionBar != null) {
570 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
571 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
572 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700573 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
574
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800576 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
577
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700578 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579 if (buttonBar != null) {
580 buttonBar.setVisibility(View.VISIBLE);
581
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700582 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800583 backButton.setOnClickListener(new OnClickListener() {
584 public void onClick(View v) {
585 setResult(RESULT_CANCELED);
586 finish();
587 }
588 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700589 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590 skipButton.setOnClickListener(new OnClickListener() {
591 public void onClick(View v) {
592 setResult(RESULT_OK);
593 finish();
594 }
595 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700596 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 mNextButton.setOnClickListener(new OnClickListener() {
598 public void onClick(View v) {
599 setResult(RESULT_OK);
600 finish();
601 }
602 });
603
604 // set our various button parameters
605 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
606 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
607 if (TextUtils.isEmpty(buttonText)) {
608 mNextButton.setVisibility(View.GONE);
609 }
610 else {
611 mNextButton.setText(buttonText);
612 }
613 }
614 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
615 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
616 if (TextUtils.isEmpty(buttonText)) {
617 backButton.setVisibility(View.GONE);
618 }
619 else {
620 backButton.setText(buttonText);
621 }
622 }
623 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
624 skipButton.setVisibility(View.VISIBLE);
625 }
626 }
627 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700628
629 mHomeActivitiesCount = getHomeActivitiesCount();
630 }
631
632 private int getHomeActivitiesCount() {
633 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
634 getPackageManager().getHomeActivities(homeApps);
635 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800636 }
637
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700638 private void setTitleFromIntent(Intent intent) {
639 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
640 if (initialTitleResId > 0) {
641 mInitialTitle = null;
642 mInitialTitleResId = initialTitleResId;
643 setTitle(mInitialTitleResId);
644 } else {
645 mInitialTitleResId = -1;
646 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
647 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
648 setTitle(mInitialTitle);
649 }
650 }
651
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800652 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700654 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 }
656
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700657 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800658 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700659
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800660 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700661 if (mInitialTitleResId > 0) {
662 setTitle(mInitialTitleResId);
663 } else {
664 setTitle(mInitialTitle);
665 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700666 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800667 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700668
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800669 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
670 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700671
672 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800673 }
674
675 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
676 final CharSequence title;
677 final int titleRes = bse.getBreadCrumbTitleRes();
678 if (titleRes > 0) {
679 title = getText(titleRes);
680 } else {
681 title = bse.getBreadCrumbTitle();
682 }
683 if (title != null) {
684 setTitle(title);
685 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800686 }
687
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800688 @Override
689 protected void onSaveInstanceState(Bundle outState) {
690 super.onSaveInstanceState(outState);
691
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700692 if (mCategories.size() > 0) {
693 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800694 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700695
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700696 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700697 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700698
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700699 if (mDisplaySearch) {
700 // The option menus are created if the ActionBar is visible and they are also created
701 // asynchronously. If you launch Settings with an Intent action like
702 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
703 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
704 // menu item and search view are null.
705 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
706 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700707
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700708 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
709 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
710 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700711
712 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800713 }
714
715 @Override
716 public void onResume() {
717 super.onResume();
718
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700719 final int newHomeActivityCount = getHomeActivitiesCount();
720 if (newHomeActivityCount != mHomeActivitiesCount) {
721 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700722 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700723 }
724
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800725 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
726 @Override
727 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700728 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800729 }
730 };
731 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
732 mDevelopmentPreferencesListener);
733
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800734 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700735
Svetoslav990159a2014-04-14 17:14:59 -0700736 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700737
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700738 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700739 onQueryTextSubmit(mSearchQuery);
740 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800741 }
742
743 @Override
744 public void onPause() {
745 super.onPause();
746
747 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700748 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800749 }
750
751 @Override
752 public void onDestroy() {
753 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700754
755 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
756 mDevelopmentPreferencesListener);
757 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800758 }
759
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800760 protected boolean isValidFragment(String fragmentName) {
761 // Almost all fragments are wrapped in this,
762 // except for a few that have their own activities.
763 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
764 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
765 }
766 return false;
767 }
768
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800769 @Override
770 public Intent getIntent() {
771 Intent superIntent = super.getIntent();
772 String startingFragment = getStartingFragmentClass(superIntent);
773 // This is called from super.onCreate, isMultiPane() is not yet reliable
774 // Do not use onIsHidingHeaders either, which relies itself on this method
775 if (startingFragment != null) {
776 Intent modIntent = new Intent(superIntent);
777 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
778 Bundle args = superIntent.getExtras();
779 if (args != null) {
780 args = new Bundle(args);
781 } else {
782 args = new Bundle();
783 }
784 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100785 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800786 return modIntent;
787 }
788 return superIntent;
789 }
790
791 /**
792 * Checks if the component name in the intent is different from the Settings class and
793 * returns the class name to load as a fragment.
794 */
795 private String getStartingFragmentClass(Intent intent) {
796 if (mFragmentClass != null) return mFragmentClass;
797
798 String intentClass = intent.getComponent().getClassName();
799 if (intentClass.equals(getClass().getName())) return null;
800
801 if ("com.android.settings.ManageApplications".equals(intentClass)
802 || "com.android.settings.RunningServices".equals(intentClass)
803 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
804 // Old names of manage apps.
805 intentClass = com.android.settings.applications.ManageApplications.class.getName();
806 }
807
808 return intentClass;
809 }
810
811 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000812 * Start a new fragment containing a preference panel. If the preferences
813 * are being displayed in multi-pane mode, the given fragment class will
814 * be instantiated and placed in the appropriate pane. If running in
815 * single-pane mode, a new activity will be launched in which to show the
816 * fragment.
817 *
818 * @param fragmentClass Full name of the class implementing the fragment.
819 * @param args Any desired arguments to supply to the fragment.
820 * @param titleRes Optional resource identifier of the title of this
821 * fragment.
822 * @param titleText Optional text of the title of this fragment.
823 * @param resultTo Optional fragment that result data should be sent to.
824 * If non-null, resultTo.onActivityResult() will be called when this
825 * preference panel is done. The launched panel must use
826 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
827 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700828 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000829 */
830 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700831 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700832 String title = null;
833 if (titleRes < 0) {
834 if (titleText != null) {
835 title = titleText.toString();
836 } else {
837 // There not much we can do in that case
838 title = "";
839 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700840 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700841 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700842 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000843 }
844
845 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800846 * Called by a preference panel fragment to finish itself.
847 *
848 * @param caller The fragment that is asking to be finished.
849 * @param resultCode Optional result code to send back to the original
850 * launching fragment.
851 * @param resultData Optional result data to send back to the original
852 * launching fragment.
853 */
854 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
855 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700856 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800857 }
858
859 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000860 * Start a new fragment.
861 *
862 * @param fragment The fragment to start
863 * @param push If true, the current fragment will be pushed onto the back stack. If false,
864 * the current fragment will be replaced.
865 */
866 public void startPreferenceFragment(Fragment fragment, boolean push) {
867 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700868 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000869 if (push) {
870 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
871 transaction.addToBackStack(BACK_STACK_PREFS);
872 } else {
873 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
874 }
875 transaction.commitAllowingStateLoss();
876 }
877
878 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700879 * Switch to a specific Fragment with taking care of validation, Title and BackStack
880 */
881 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700882 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700883 if (validate && !isValidFragment(fragmentName)) {
884 throw new IllegalArgumentException("Invalid fragment for this activity: "
885 + fragmentName);
886 }
887 Fragment f = Fragment.instantiate(this, fragmentName, args);
888 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700889 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700890 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700891 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700892 }
893 if (addToBackStack) {
894 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
895 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700896 if (titleResId > 0) {
897 transaction.setBreadCrumbTitle(titleResId);
898 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700899 transaction.setBreadCrumbTitle(title);
900 }
901 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700902 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700903 return f;
904 }
905
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700906 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700907 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700908 *
909 * @param categories The list in which to place the tiles categories.
910 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700911 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700912 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700913 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
914 updateTilesList(categories);
915 }
916
917 /**
918 * Parse the given XML file as a categories description, adding each
919 * parsed categories and tiles into the target list.
920 *
921 * @param resid The XML resource to load and parse.
922 * @param target The list in which the parsed categories and tiles should be placed.
923 */
924 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
925 XmlResourceParser parser = null;
926 try {
927 parser = getResources().getXml(resid);
928 AttributeSet attrs = Xml.asAttributeSet(parser);
929
930 int type;
931 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
932 && type != XmlPullParser.START_TAG) {
933 // Parse next until start tag is found
934 }
935
936 String nodeName = parser.getName();
937 if (!"dashboard-categories".equals(nodeName)) {
938 throw new RuntimeException(
939 "XML document must start with <preference-categories> tag; found"
940 + nodeName + " at " + parser.getPositionDescription());
941 }
942
943 Bundle curBundle = null;
944
945 final int outerDepth = parser.getDepth();
946 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
947 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
948 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
949 continue;
950 }
951
952 nodeName = parser.getName();
953 if ("dashboard-category".equals(nodeName)) {
954 DashboardCategory category = new DashboardCategory();
955
956 TypedArray sa = obtainStyledAttributes(
957 attrs, com.android.internal.R.styleable.PreferenceHeader);
958 category.id = sa.getResourceId(
959 com.android.internal.R.styleable.PreferenceHeader_id,
960 (int)DashboardCategory.CAT_ID_UNDEFINED);
961
962 TypedValue tv = sa.peekValue(
963 com.android.internal.R.styleable.PreferenceHeader_title);
964 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
965 if (tv.resourceId != 0) {
966 category.titleRes = tv.resourceId;
967 } else {
968 category.title = tv.string;
969 }
970 }
971 sa.recycle();
972
973 final int innerDepth = parser.getDepth();
974 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
975 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
976 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
977 continue;
978 }
979
980 String innerNodeName = parser.getName();
981 if (innerNodeName.equals("dashboard-tile")) {
982 DashboardTile tile = new DashboardTile();
983
984 sa = obtainStyledAttributes(
985 attrs, com.android.internal.R.styleable.PreferenceHeader);
986 tile.id = sa.getResourceId(
987 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700988 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700989 tv = sa.peekValue(
990 com.android.internal.R.styleable.PreferenceHeader_title);
991 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
992 if (tv.resourceId != 0) {
993 tile.titleRes = tv.resourceId;
994 } else {
995 tile.title = tv.string;
996 }
997 }
998 tv = sa.peekValue(
999 com.android.internal.R.styleable.PreferenceHeader_summary);
1000 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1001 if (tv.resourceId != 0) {
1002 tile.summaryRes = tv.resourceId;
1003 } else {
1004 tile.summary = tv.string;
1005 }
1006 }
1007 tile.iconRes = sa.getResourceId(
1008 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1009 tile.fragment = sa.getString(
1010 com.android.internal.R.styleable.PreferenceHeader_fragment);
1011 sa.recycle();
1012
1013 if (curBundle == null) {
1014 curBundle = new Bundle();
1015 }
1016
1017 final int innerDepth2 = parser.getDepth();
1018 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1019 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1020 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1021 continue;
1022 }
1023
1024 String innerNodeName2 = parser.getName();
1025 if (innerNodeName2.equals("extra")) {
1026 getResources().parseBundleExtra("extra", attrs, curBundle);
1027 XmlUtils.skipCurrentTag(parser);
1028
1029 } else if (innerNodeName2.equals("intent")) {
1030 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1031
1032 } else {
1033 XmlUtils.skipCurrentTag(parser);
1034 }
1035 }
1036
1037 if (curBundle.size() > 0) {
1038 tile.fragmentArguments = curBundle;
1039 curBundle = null;
1040 }
1041
PauloftheWest38155612014-06-30 10:02:36 -07001042 // Show the SIM Cards setting if there are more than 2 SIMs installed.
1043 if(tile.id != R.id.sim_settings || SimSettings.showSimCardScreen(this)){
1044 category.addTile(tile);
1045 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001046
1047 } else {
1048 XmlUtils.skipCurrentTag(parser);
1049 }
1050 }
1051
1052 target.add(category);
1053 } else {
1054 XmlUtils.skipCurrentTag(parser);
1055 }
1056 }
1057
1058 } catch (XmlPullParserException e) {
1059 throw new RuntimeException("Error parsing categories", e);
1060 } catch (IOException e) {
1061 throw new RuntimeException("Error parsing categories", e);
1062 } finally {
1063 if (parser != null) parser.close();
1064 }
1065 }
1066
1067 private void updateTilesList(List<DashboardCategory> target) {
1068 final boolean showDev = mDevelopmentPreferences.getBoolean(
1069 DevelopmentSettings.PREF_SHOW,
1070 android.os.Build.TYPE.equals("eng"));
1071
1072 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1073
1074 final int size = target.size();
1075 for (int i = 0; i < size; i++) {
1076
1077 DashboardCategory category = target.get(i);
1078
1079 // Ids are integers, so downcasting is ok
1080 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001081 int n = category.getTilesCount() - 1;
1082 while (n >= 0) {
1083
1084 DashboardTile tile = category.getTile(n);
1085
1086 id = (int) tile.id;
1087 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1088 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1089 } else if (id == R.id.wifi_settings) {
1090 // Remove WiFi Settings if WiFi service is not available.
1091 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1092 category.removeTile(n);
1093 }
1094 } else if (id == R.id.bluetooth_settings) {
1095 // Remove Bluetooth Settings if Bluetooth service is not available.
1096 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1097 category.removeTile(n);
1098 }
1099 } else if (id == R.id.data_usage_settings) {
1100 // Remove data usage when kernel module not enabled
1101 final INetworkManagementService netManager = INetworkManagementService.Stub
1102 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1103 try {
1104 if (!netManager.isBandwidthControlEnabled()) {
1105 category.removeTile(n);
1106 }
1107 } catch (RemoteException e) {
1108 // ignored
1109 }
1110 } else if (id == R.id.battery_settings) {
1111 // Remove battery settings when battery is not available. (e.g. TV)
1112
1113 if (!mBatteryPresent) {
1114 category.removeTile(n);
1115 }
1116 } else if (id == R.id.home_settings) {
1117 if (!updateHomeSettingTiles(tile)) {
1118 category.removeTile(n);
1119 }
1120 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001121 boolean hasMultipleUsers =
1122 ((UserManager) getSystemService(Context.USER_SERVICE))
1123 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001124 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001125 || (!UserManager.supportsMultipleUsers()
1126 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001127 || Utils.isMonkeyRunning()) {
1128 category.removeTile(n);
1129 }
1130 } else if (id == R.id.nfc_payment_settings) {
1131 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1132 category.removeTile(n);
1133 } else {
1134 // Only show if NFC is on and we have the HCE feature
1135 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1136 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1137 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1138 category.removeTile(n);
1139 }
1140 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001141 } else if (id == R.id.print_settings) {
1142 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1143 PackageManager.FEATURE_PRINTING);
1144 if (!hasPrintingSupport) {
1145 category.removeTile(n);
1146 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001147 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001148 if (!showDev || um.hasUserRestriction(
1149 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001150 category.removeTile(n);
1151 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001152 }
1153
1154 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001155 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1156 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001157 category.removeTile(n);
1158 }
1159
1160 n--;
1161 }
1162 }
1163 }
1164
1165 private boolean updateHomeSettingTiles(DashboardTile tile) {
1166 // Once we decide to show Home settings, keep showing it forever
1167 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1168 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1169 return true;
1170 }
1171
1172 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001173 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001174 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001175 // When there's only one available home app, omit this settings
1176 // category entirely at the top level UI. If the user just
1177 // uninstalled the penultimate home app candidiate, we also
1178 // now tell them about why they aren't seeing 'Home' in the list.
1179 if (sShowNoHomeNotice) {
1180 sShowNoHomeNotice = false;
1181 NoHomeDialogFragment.show(this);
1182 }
1183 return false;
1184 } else {
1185 // Okay, we're allowing the Home settings category. Tell it, when
1186 // invoked via this front door, that we'll need to be told about the
1187 // case when the user uninstalls all but one home app.
1188 if (tile.fragmentArguments == null) {
1189 tile.fragmentArguments = new Bundle();
1190 }
1191 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1192 }
1193 } catch (Exception e) {
1194 // Can't look up the home activity; bail on configuring the icon
1195 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1196 }
1197
1198 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1199 return true;
1200 }
1201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001202 private void getMetaData() {
1203 try {
1204 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1205 PackageManager.GET_META_DATA);
1206 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001207 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1208 } catch (NameNotFoundException nnfe) {
1209 // No recovery
1210 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1211 }
1212 }
1213
1214 // give subclasses access to the Next button
1215 public boolean hasNextButton() {
1216 return mNextButton != null;
1217 }
1218
1219 public Button getNextButton() {
1220 return mNextButton;
1221 }
1222
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001223 @Override
1224 public boolean shouldUpRecreateTask(Intent targetIntent) {
1225 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1226 }
1227
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001228 public static void requestHomeNotice() {
1229 sShowNoHomeNotice = true;
1230 }
1231
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001232 @Override
1233 public boolean onQueryTextSubmit(String query) {
1234 switchToSearchResultsFragmentIfNeeded();
1235 mSearchQuery = query;
1236 return mSearchResultsFragment.onQueryTextSubmit(query);
1237 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001238
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001239 @Override
1240 public boolean onQueryTextChange(String newText) {
1241 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001242 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001243 return false;
1244 }
1245 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001246 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001247
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001248 @Override
1249 public boolean onClose() {
1250 return false;
1251 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001252
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001253 @Override
1254 public boolean onMenuItemActionExpand(MenuItem item) {
1255 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001256 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001257 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001258 return true;
1259 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001260
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001261 @Override
1262 public boolean onMenuItemActionCollapse(MenuItem item) {
1263 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001264 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001265 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001266 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001267 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001268 return true;
1269 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001270
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001271 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001272 if (mSearchResultsFragment != null) {
1273 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001274 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001275 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001276 if (current != null && current instanceof SearchResultsSummary) {
1277 mSearchResultsFragment = (SearchResultsSummary) current;
1278 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001279 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001280 SearchResultsSummary.class.getName(), null, false, true,
1281 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001282 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001283 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001284 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001285 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001286
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001287 public void needToRevertToInitialFragment() {
1288 mNeedToRevertToInitialFragment = true;
1289 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001290
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001291 private void revertToInitialFragment() {
1292 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001293 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001294 mSearchMenuItemExpanded = false;
1295 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1296 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001297 if (mSearchMenuItem != null) {
1298 mSearchMenuItem.collapseActionView();
1299 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001300 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001301}