blob: 90e8fd4f536937e66468eeeb28ce3d721d0b9798 [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 Meglio7a6bfd12014-04-14 19:49:18 -070087import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070088import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
90import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
91import com.android.settings.inputmethod.SpellCheckersSettings;
92import com.android.settings.inputmethod.UserDictionaryList;
93import com.android.settings.location.LocationSettings;
94import com.android.settings.nfc.AndroidBeam;
95import com.android.settings.nfc.PaymentSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040096import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040097import com.android.settings.notification.NotificationAccessSettings;
98import com.android.settings.notification.NotificationSettings;
99import com.android.settings.notification.NotificationStation;
100import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800101import com.android.settings.print.PrintJobSettingsFragment;
102import com.android.settings.print.PrintSettingsFragment;
103import com.android.settings.tts.TextToSpeechSettings;
104import com.android.settings.users.UserSettings;
105import com.android.settings.vpn2.VpnSettings;
106import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700107import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700109import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.wifi.WifiSettings;
111import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400112
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import org.xmlpull.v1.XmlPullParser;
114import org.xmlpull.v1.XmlPullParserException;
115
116import java.io.IOException;
117import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700119import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700121import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700122
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123public class SettingsActivity extends Activity
124 implements PreferenceManager.OnPreferenceTreeClickListener,
125 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100126 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700127 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100128 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800129
130 private static final String LOG_TAG = "Settings";
131
132 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700133 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700134 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
135 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700136 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700137 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700138 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800139
140 /**
141 * When starting this activity, the invoking Intent can contain this extra
142 * string to specify which fragment should be initially displayed.
143 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
144 * will call isValidFragment() to confirm that the fragment class name is valid for this
145 * activity.
146 */
147 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
148
149 /**
150 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
151 * this extra can also be specified to supply a Bundle of arguments to pass
152 * to that fragment when it is instantiated during the initial creation
153 * of the activity.
154 */
155 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
156
157 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700158 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
159 */
160 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
161
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800162 public static final String BACK_STACK_PREFS = ":settings:prefs";
163
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164 // extras that allow any preference activity to be launched as part of a wizard
165
166 // show Back and Next buttons? takes boolean parameter
167 // Back will then return RESULT_CANCELED and Next RESULT_OK
168 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
169
170 // add a Skip button?
171 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
172
173 // specify custom text for the Back or Next buttons, or cause a button to not appear
174 // at all by setting it to null
175 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
176 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
177
178 /**
179 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700180 * 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 -0800181 * that fragment.
182 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700183 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700184 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700185 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800186
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800187 private static final String META_DATA_KEY_FRAGMENT_CLASS =
188 "com.android.settings.FRAGMENT_CLASS";
189
190 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
191
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700192 private static final String EMPTY_QUERY = "";
193
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194 private static boolean sShowNoHomeNotice = false;
195
196 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800197
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800198 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700199 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800200
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201 // Show only these settings for restricted users
202 private int[] SETTINGS_FOR_RESTRICTED = {
203 R.id.wireless_section,
204 R.id.wifi_settings,
205 R.id.bluetooth_settings,
206 R.id.data_usage_settings,
207 R.id.wireless_settings,
208 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400209 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210 R.id.display_settings,
211 R.id.storage_settings,
212 R.id.application_settings,
213 R.id.battery_settings,
214 R.id.personal_section,
215 R.id.location_settings,
216 R.id.security_settings,
217 R.id.language_settings,
218 R.id.user_settings,
219 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800220 R.id.system_section,
221 R.id.date_time_settings,
222 R.id.about_settings,
223 R.id.accessibility_settings,
224 R.id.print_settings,
225 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800226 R.id.home_settings,
227 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800228 };
229
230 private static final String[] ENTRY_FRAGMENTS = {
231 WirelessSettings.class.getName(),
232 WifiSettings.class.getName(),
233 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700234 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800235 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700236 MessageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800237 TetherSettings.class.getName(),
238 WifiP2pSettings.class.getName(),
239 VpnSettings.class.getName(),
240 DateTimeSettings.class.getName(),
241 LocalePicker.class.getName(),
242 InputMethodAndLanguageSettings.class.getName(),
243 SpellCheckersSettings.class.getName(),
244 UserDictionaryList.class.getName(),
245 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700246 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 DisplaySettings.class.getName(),
248 DeviceInfoSettings.class.getName(),
249 ManageApplications.class.getName(),
250 ProcessStatsUi.class.getName(),
251 NotificationStation.class.getName(),
252 LocationSettings.class.getName(),
253 SecuritySettings.class.getName(),
254 PrivacySettings.class.getName(),
255 DeviceAdminSettings.class.getName(),
256 AccessibilitySettings.class.getName(),
257 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
259 TextToSpeechSettings.class.getName(),
260 Memory.class.getName(),
261 DevelopmentSettings.class.getName(),
262 UsbSettings.class.getName(),
263 AndroidBeam.class.getName(),
264 WifiDisplaySettings.class.getName(),
265 PowerUsageSummary.class.getName(),
266 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000267 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800268 CryptKeeperSettings.class.getName(),
269 DataUsageSummary.class.getName(),
270 DreamSettings.class.getName(),
271 UserSettings.class.getName(),
272 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400273 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 PrintSettingsFragment.class.getName(),
275 PrintJobSettingsFragment.class.getName(),
276 TrustedCredentialsSettings.class.getName(),
277 PaymentSettings.class.getName(),
278 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700279 ZenModeSettings.class.getName(),
280 NotificationSettings.class.getName(),
281 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
282 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400283 InstalledAppDetails.class.getName(),
284 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700285 AppNotificationSettings.class.getName(),
286 OtherSoundSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 };
288
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700289
290 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
291 "android.settings.APPLICATION_DETAILS_SETTINGS"
292 };
293
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 private SharedPreferences mDevelopmentPreferences;
295 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
296
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 private boolean mBatteryPresent = true;
298 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 @Override
300 public void onReceive(Context context, Intent intent) {
301 String action = intent.getAction();
302 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
303 boolean batteryPresent = Utils.isBatteryPresent(intent);
304
305 if (mBatteryPresent != batteryPresent) {
306 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700307 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 }
309 }
310 }
311 };
312
Svetoslav990159a2014-04-14 17:14:59 -0700313 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
314 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700315
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700316 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700317 private SwitchBar mSwitchBar;
318
319 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700320
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700321 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700322 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700323
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700324 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700325 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700326
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700327 private ViewGroup mContent;
328
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700329 private SearchView mSearchView;
330 private MenuItem mSearchMenuItem;
331 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700332 private SearchResultsSummary mSearchResultsFragment;
333 private String mSearchQuery;
334
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700335 // Categories
336 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800337
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700338 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700339 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800340 private Handler mHandler = new Handler() {
341 @Override
342 public void handleMessage(Message msg) {
343 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700344 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700345 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
346 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700347 buildDashboardCategories(mCategories);
348 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 } break;
350 }
351 }
352 };
353
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700354 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700355 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700356
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700357 public SwitchBar getSwitchBar() {
358 return mSwitchBar;
359 }
360
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700361 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
362 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700363 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700364 }
365 return mCategories;
366 }
367
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 @Override
369 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
370 // Override the fragment title for Wallpaper settings
371 int titleRes = pref.getTitleRes();
372 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
373 titleRes = R.string.wallpaper_settings_fragment_title;
374 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
375 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
376 if (UserManager.get(this).isLinkedUser()) {
377 titleRes = R.string.profile_info_settings_title;
378 } else {
379 titleRes = R.string.user_info_settings_title;
380 }
381 }
382 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
383 null, 0);
384 return true;
385 }
386
387 @Override
388 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
389 return false;
390 }
391
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700392 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700393 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700394 Message msg = new Message();
395 msg.what = MSG_BUILD_CATEGORIES;
396 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800397 }
398 }
399
400 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401 public void onConfigurationChanged(Configuration newConfig) {
402 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800403 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404 }
405
406 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700407 protected void onStart() {
408 super.onStart();
409
410 if (mNeedToRevertToInitialFragment) {
411 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800412 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800413 }
414
415 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700416 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700417 if (!mDisplaySearch) {
418 return false;
419 }
420
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421 MenuInflater inflater = getMenuInflater();
422 inflater.inflate(R.menu.options_menu, menu);
423
424 // Cache the search query (can be overriden by the OnQueryTextListener)
425 final String query = mSearchQuery;
426
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700427 mSearchMenuItem = menu.findItem(R.id.search);
428 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700429
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700430 if (mSearchMenuItem == null || mSearchView == null) {
431 return false;
432 }
433
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700434 if (mSearchResultsFragment != null) {
435 mSearchResultsFragment.setSearchView(mSearchView);
436 }
437
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700438 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700439 mSearchView.setOnQueryTextListener(this);
440 mSearchView.setOnCloseListener(this);
441
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700442 if (mSearchMenuItemExpanded) {
443 mSearchMenuItem.expandActionView();
444 }
445 mSearchView.setQuery(query, true /* submit */);
446
447 return true;
448 }
449
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700450 private static boolean isShortCutIntent(final Intent intent) {
451 Set<String> categories = intent.getCategories();
452 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
453 }
454
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700455 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700456 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700457 if (action == null) {
458 return false;
459 }
460 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
461 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
462 }
463 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700464 }
465
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700466 @Override
467 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700468 super.onCreate(savedState);
469
470 // Should happen before any call to getIntent()
471 getMetaData();
472
473 final Intent intent = getIntent();
474 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
475 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800477
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
479 Context.MODE_PRIVATE);
480
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700481 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700482 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700483
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700484 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700485 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
486
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700487 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700488 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700489
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700490 mIsShowingDashboard = className.equals(Settings.class.getName());
491 final boolean isSubSettings = className.equals(SubSettings.class.getName());
492
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700493 // If this is a sub settings or not the main Dashboard and not a Shortcut and an initial
494 // Fragment then apply the SubSettings theme for the ActionBar content insets
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700495 if (isSubSettings ||
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700496 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName != null))) {
497 // Check also that we are not a Theme Dialog as we don't want to override them
498 final int themeResId = getThemeResId();
499 if (themeResId != R.style.Theme_DialogWhenLarge &&
500 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
501 setTheme(R.style.Theme_SubSettings);
502 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700503 }
504
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700505 setContentView(mIsShowingDashboard ?
506 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700508 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700509
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800510 getFragmentManager().addOnBackStackChangedListener(this);
511
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700512 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700513 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700514 }
515
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700516 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700517 // We are restarting from a previous saved state; used that to initialize, instead
518 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700519 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
520 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800521
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700522 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800523
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700524 ArrayList<DashboardCategory> categories =
525 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
526 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700527 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700528 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700529 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700531
532 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700533 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700534 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
535 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700537 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700538 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
539 // only if it is a sub settings
540 if (mIsShortcut) {
541 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700542 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700543 } else if (isSubSettings) {
544 mDisplayHomeAsUpEnabled = true;
545 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700546 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700547 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700548
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700549 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700550 switchToFragment(initialFragmentName, initialArguments, true, false,
551 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000552 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700553 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700554 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700555 // Show Search affordance
556 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700557 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700558 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700559 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 }
561 }
562
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700563 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700564 if (mActionBar != null) {
565 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
566 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
567 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700568 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
569
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
572
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700573 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 if (buttonBar != null) {
575 buttonBar.setVisibility(View.VISIBLE);
576
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700577 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800578 backButton.setOnClickListener(new OnClickListener() {
579 public void onClick(View v) {
580 setResult(RESULT_CANCELED);
581 finish();
582 }
583 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700584 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 skipButton.setOnClickListener(new OnClickListener() {
586 public void onClick(View v) {
587 setResult(RESULT_OK);
588 finish();
589 }
590 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700591 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800592 mNextButton.setOnClickListener(new OnClickListener() {
593 public void onClick(View v) {
594 setResult(RESULT_OK);
595 finish();
596 }
597 });
598
599 // set our various button parameters
600 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
601 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
602 if (TextUtils.isEmpty(buttonText)) {
603 mNextButton.setVisibility(View.GONE);
604 }
605 else {
606 mNextButton.setText(buttonText);
607 }
608 }
609 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
610 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
611 if (TextUtils.isEmpty(buttonText)) {
612 backButton.setVisibility(View.GONE);
613 }
614 else {
615 backButton.setText(buttonText);
616 }
617 }
618 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
619 skipButton.setVisibility(View.VISIBLE);
620 }
621 }
622 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700623
624 mHomeActivitiesCount = getHomeActivitiesCount();
625 }
626
627 private int getHomeActivitiesCount() {
628 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
629 getPackageManager().getHomeActivities(homeApps);
630 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800631 }
632
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700633 private void setTitleFromIntent(Intent intent) {
634 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
635 if (initialTitleResId > 0) {
636 mInitialTitle = null;
637 mInitialTitleResId = initialTitleResId;
638 setTitle(mInitialTitleResId);
639 } else {
640 mInitialTitleResId = -1;
641 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
642 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
643 setTitle(mInitialTitle);
644 }
645 }
646
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800647 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700649 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 }
651
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700652 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800653 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700654
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800655 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700656 if (mInitialTitleResId > 0) {
657 setTitle(mInitialTitleResId);
658 } else {
659 setTitle(mInitialTitle);
660 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700661 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800662 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700663
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800664 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
665 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700666
667 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800668 }
669
670 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
671 final CharSequence title;
672 final int titleRes = bse.getBreadCrumbTitleRes();
673 if (titleRes > 0) {
674 title = getText(titleRes);
675 } else {
676 title = bse.getBreadCrumbTitle();
677 }
678 if (title != null) {
679 setTitle(title);
680 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800681 }
682
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800683 @Override
684 protected void onSaveInstanceState(Bundle outState) {
685 super.onSaveInstanceState(outState);
686
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700687 if (mCategories.size() > 0) {
688 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800689 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700690
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700691 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700692 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700693
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700694 if (mDisplaySearch) {
695 // The option menus are created if the ActionBar is visible and they are also created
696 // asynchronously. If you launch Settings with an Intent action like
697 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
698 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
699 // menu item and search view are null.
700 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
701 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700702
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700703 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
704 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
705 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700706
707 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800708 }
709
710 @Override
711 public void onResume() {
712 super.onResume();
713
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700714 final int newHomeActivityCount = getHomeActivitiesCount();
715 if (newHomeActivityCount != mHomeActivitiesCount) {
716 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700717 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700718 }
719
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800720 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
721 @Override
722 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700723 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800724 }
725 };
726 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
727 mDevelopmentPreferencesListener);
728
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800729 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700730
Svetoslav990159a2014-04-14 17:14:59 -0700731 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700732
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700733 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700734 onQueryTextSubmit(mSearchQuery);
735 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800736 }
737
738 @Override
739 public void onPause() {
740 super.onPause();
741
742 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700743 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800744 }
745
746 @Override
747 public void onDestroy() {
748 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700749
750 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
751 mDevelopmentPreferencesListener);
752 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800753 }
754
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800755 protected boolean isValidFragment(String fragmentName) {
756 // Almost all fragments are wrapped in this,
757 // except for a few that have their own activities.
758 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
759 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
760 }
761 return false;
762 }
763
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800764 @Override
765 public Intent getIntent() {
766 Intent superIntent = super.getIntent();
767 String startingFragment = getStartingFragmentClass(superIntent);
768 // This is called from super.onCreate, isMultiPane() is not yet reliable
769 // Do not use onIsHidingHeaders either, which relies itself on this method
770 if (startingFragment != null) {
771 Intent modIntent = new Intent(superIntent);
772 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
773 Bundle args = superIntent.getExtras();
774 if (args != null) {
775 args = new Bundle(args);
776 } else {
777 args = new Bundle();
778 }
779 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100780 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800781 return modIntent;
782 }
783 return superIntent;
784 }
785
786 /**
787 * Checks if the component name in the intent is different from the Settings class and
788 * returns the class name to load as a fragment.
789 */
790 private String getStartingFragmentClass(Intent intent) {
791 if (mFragmentClass != null) return mFragmentClass;
792
793 String intentClass = intent.getComponent().getClassName();
794 if (intentClass.equals(getClass().getName())) return null;
795
796 if ("com.android.settings.ManageApplications".equals(intentClass)
797 || "com.android.settings.RunningServices".equals(intentClass)
798 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
799 // Old names of manage apps.
800 intentClass = com.android.settings.applications.ManageApplications.class.getName();
801 }
802
803 return intentClass;
804 }
805
806 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000807 * Start a new fragment containing a preference panel. If the preferences
808 * are being displayed in multi-pane mode, the given fragment class will
809 * be instantiated and placed in the appropriate pane. If running in
810 * single-pane mode, a new activity will be launched in which to show the
811 * fragment.
812 *
813 * @param fragmentClass Full name of the class implementing the fragment.
814 * @param args Any desired arguments to supply to the fragment.
815 * @param titleRes Optional resource identifier of the title of this
816 * fragment.
817 * @param titleText Optional text of the title of this fragment.
818 * @param resultTo Optional fragment that result data should be sent to.
819 * If non-null, resultTo.onActivityResult() will be called when this
820 * preference panel is done. The launched panel must use
821 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
822 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700823 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000824 */
825 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700826 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700827 String title = null;
828 if (titleRes < 0) {
829 if (titleText != null) {
830 title = titleText.toString();
831 } else {
832 // There not much we can do in that case
833 title = "";
834 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700835 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700836 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700837 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000838 }
839
840 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800841 * Called by a preference panel fragment to finish itself.
842 *
843 * @param caller The fragment that is asking to be finished.
844 * @param resultCode Optional result code to send back to the original
845 * launching fragment.
846 * @param resultData Optional result data to send back to the original
847 * launching fragment.
848 */
849 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
850 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700851 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800852 }
853
854 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000855 * Start a new fragment.
856 *
857 * @param fragment The fragment to start
858 * @param push If true, the current fragment will be pushed onto the back stack. If false,
859 * the current fragment will be replaced.
860 */
861 public void startPreferenceFragment(Fragment fragment, boolean push) {
862 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700863 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000864 if (push) {
865 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
866 transaction.addToBackStack(BACK_STACK_PREFS);
867 } else {
868 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
869 }
870 transaction.commitAllowingStateLoss();
871 }
872
873 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700874 * Switch to a specific Fragment with taking care of validation, Title and BackStack
875 */
876 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700877 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700878 if (validate && !isValidFragment(fragmentName)) {
879 throw new IllegalArgumentException("Invalid fragment for this activity: "
880 + fragmentName);
881 }
882 Fragment f = Fragment.instantiate(this, fragmentName, args);
883 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700884 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700885 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700886 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700887 }
888 if (addToBackStack) {
889 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
890 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700891 if (titleResId > 0) {
892 transaction.setBreadCrumbTitle(titleResId);
893 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700894 transaction.setBreadCrumbTitle(title);
895 }
896 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700897 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700898 return f;
899 }
900
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700901 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700902 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700903 *
904 * @param categories The list in which to place the tiles categories.
905 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700906 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700907 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700908 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
909 updateTilesList(categories);
910 }
911
912 /**
913 * Parse the given XML file as a categories description, adding each
914 * parsed categories and tiles into the target list.
915 *
916 * @param resid The XML resource to load and parse.
917 * @param target The list in which the parsed categories and tiles should be placed.
918 */
919 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
920 XmlResourceParser parser = null;
921 try {
922 parser = getResources().getXml(resid);
923 AttributeSet attrs = Xml.asAttributeSet(parser);
924
925 int type;
926 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
927 && type != XmlPullParser.START_TAG) {
928 // Parse next until start tag is found
929 }
930
931 String nodeName = parser.getName();
932 if (!"dashboard-categories".equals(nodeName)) {
933 throw new RuntimeException(
934 "XML document must start with <preference-categories> tag; found"
935 + nodeName + " at " + parser.getPositionDescription());
936 }
937
938 Bundle curBundle = null;
939
940 final int outerDepth = parser.getDepth();
941 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
942 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
943 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
944 continue;
945 }
946
947 nodeName = parser.getName();
948 if ("dashboard-category".equals(nodeName)) {
949 DashboardCategory category = new DashboardCategory();
950
951 TypedArray sa = obtainStyledAttributes(
952 attrs, com.android.internal.R.styleable.PreferenceHeader);
953 category.id = sa.getResourceId(
954 com.android.internal.R.styleable.PreferenceHeader_id,
955 (int)DashboardCategory.CAT_ID_UNDEFINED);
956
957 TypedValue tv = sa.peekValue(
958 com.android.internal.R.styleable.PreferenceHeader_title);
959 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
960 if (tv.resourceId != 0) {
961 category.titleRes = tv.resourceId;
962 } else {
963 category.title = tv.string;
964 }
965 }
966 sa.recycle();
967
968 final int innerDepth = parser.getDepth();
969 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
970 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
971 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
972 continue;
973 }
974
975 String innerNodeName = parser.getName();
976 if (innerNodeName.equals("dashboard-tile")) {
977 DashboardTile tile = new DashboardTile();
978
979 sa = obtainStyledAttributes(
980 attrs, com.android.internal.R.styleable.PreferenceHeader);
981 tile.id = sa.getResourceId(
982 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700983 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700984 tv = sa.peekValue(
985 com.android.internal.R.styleable.PreferenceHeader_title);
986 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
987 if (tv.resourceId != 0) {
988 tile.titleRes = tv.resourceId;
989 } else {
990 tile.title = tv.string;
991 }
992 }
993 tv = sa.peekValue(
994 com.android.internal.R.styleable.PreferenceHeader_summary);
995 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
996 if (tv.resourceId != 0) {
997 tile.summaryRes = tv.resourceId;
998 } else {
999 tile.summary = tv.string;
1000 }
1001 }
1002 tile.iconRes = sa.getResourceId(
1003 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1004 tile.fragment = sa.getString(
1005 com.android.internal.R.styleable.PreferenceHeader_fragment);
1006 sa.recycle();
1007
1008 if (curBundle == null) {
1009 curBundle = new Bundle();
1010 }
1011
1012 final int innerDepth2 = parser.getDepth();
1013 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1014 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1015 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1016 continue;
1017 }
1018
1019 String innerNodeName2 = parser.getName();
1020 if (innerNodeName2.equals("extra")) {
1021 getResources().parseBundleExtra("extra", attrs, curBundle);
1022 XmlUtils.skipCurrentTag(parser);
1023
1024 } else if (innerNodeName2.equals("intent")) {
1025 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1026
1027 } else {
1028 XmlUtils.skipCurrentTag(parser);
1029 }
1030 }
1031
1032 if (curBundle.size() > 0) {
1033 tile.fragmentArguments = curBundle;
1034 curBundle = null;
1035 }
1036
1037 category.addTile(tile);
1038
1039 } else {
1040 XmlUtils.skipCurrentTag(parser);
1041 }
1042 }
1043
1044 target.add(category);
1045 } else {
1046 XmlUtils.skipCurrentTag(parser);
1047 }
1048 }
1049
1050 } catch (XmlPullParserException e) {
1051 throw new RuntimeException("Error parsing categories", e);
1052 } catch (IOException e) {
1053 throw new RuntimeException("Error parsing categories", e);
1054 } finally {
1055 if (parser != null) parser.close();
1056 }
1057 }
1058
1059 private void updateTilesList(List<DashboardCategory> target) {
1060 final boolean showDev = mDevelopmentPreferences.getBoolean(
1061 DevelopmentSettings.PREF_SHOW,
1062 android.os.Build.TYPE.equals("eng"));
1063
1064 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1065
1066 final int size = target.size();
1067 for (int i = 0; i < size; i++) {
1068
1069 DashboardCategory category = target.get(i);
1070
1071 // Ids are integers, so downcasting is ok
1072 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001073 int n = category.getTilesCount() - 1;
1074 while (n >= 0) {
1075
1076 DashboardTile tile = category.getTile(n);
1077
1078 id = (int) tile.id;
1079 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1080 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1081 } else if (id == R.id.wifi_settings) {
1082 // Remove WiFi Settings if WiFi service is not available.
1083 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1084 category.removeTile(n);
1085 }
1086 } else if (id == R.id.bluetooth_settings) {
1087 // Remove Bluetooth Settings if Bluetooth service is not available.
1088 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1089 category.removeTile(n);
1090 }
1091 } else if (id == R.id.data_usage_settings) {
1092 // Remove data usage when kernel module not enabled
1093 final INetworkManagementService netManager = INetworkManagementService.Stub
1094 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1095 try {
1096 if (!netManager.isBandwidthControlEnabled()) {
1097 category.removeTile(n);
1098 }
1099 } catch (RemoteException e) {
1100 // ignored
1101 }
1102 } else if (id == R.id.battery_settings) {
1103 // Remove battery settings when battery is not available. (e.g. TV)
1104
1105 if (!mBatteryPresent) {
1106 category.removeTile(n);
1107 }
1108 } else if (id == R.id.home_settings) {
1109 if (!updateHomeSettingTiles(tile)) {
1110 category.removeTile(n);
1111 }
1112 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001113 boolean hasMultipleUsers =
1114 ((UserManager) getSystemService(Context.USER_SERVICE))
1115 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001116 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001117 || (!UserManager.supportsMultipleUsers()
1118 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001119 || Utils.isMonkeyRunning()) {
1120 category.removeTile(n);
1121 }
1122 } else if (id == R.id.nfc_payment_settings) {
1123 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1124 category.removeTile(n);
1125 } else {
1126 // Only show if NFC is on and we have the HCE feature
1127 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1128 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1129 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1130 category.removeTile(n);
1131 }
1132 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001133 } else if (id == R.id.print_settings) {
1134 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1135 PackageManager.FEATURE_PRINTING);
1136 if (!hasPrintingSupport) {
1137 category.removeTile(n);
1138 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001139 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001140 if (!showDev || um.hasUserRestriction(
1141 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001142 category.removeTile(n);
1143 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001144 }
1145
1146 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001147 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1148 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001149 category.removeTile(n);
1150 }
1151
1152 n--;
1153 }
1154 }
1155 }
1156
1157 private boolean updateHomeSettingTiles(DashboardTile tile) {
1158 // Once we decide to show Home settings, keep showing it forever
1159 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1160 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1161 return true;
1162 }
1163
1164 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001165 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001166 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001167 // When there's only one available home app, omit this settings
1168 // category entirely at the top level UI. If the user just
1169 // uninstalled the penultimate home app candidiate, we also
1170 // now tell them about why they aren't seeing 'Home' in the list.
1171 if (sShowNoHomeNotice) {
1172 sShowNoHomeNotice = false;
1173 NoHomeDialogFragment.show(this);
1174 }
1175 return false;
1176 } else {
1177 // Okay, we're allowing the Home settings category. Tell it, when
1178 // invoked via this front door, that we'll need to be told about the
1179 // case when the user uninstalls all but one home app.
1180 if (tile.fragmentArguments == null) {
1181 tile.fragmentArguments = new Bundle();
1182 }
1183 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1184 }
1185 } catch (Exception e) {
1186 // Can't look up the home activity; bail on configuring the icon
1187 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1188 }
1189
1190 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1191 return true;
1192 }
1193
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001194 private void getMetaData() {
1195 try {
1196 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1197 PackageManager.GET_META_DATA);
1198 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001199 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1200 } catch (NameNotFoundException nnfe) {
1201 // No recovery
1202 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1203 }
1204 }
1205
1206 // give subclasses access to the Next button
1207 public boolean hasNextButton() {
1208 return mNextButton != null;
1209 }
1210
1211 public Button getNextButton() {
1212 return mNextButton;
1213 }
1214
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001215 @Override
1216 public boolean shouldUpRecreateTask(Intent targetIntent) {
1217 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1218 }
1219
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001220 public static void requestHomeNotice() {
1221 sShowNoHomeNotice = true;
1222 }
1223
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001224 @Override
1225 public boolean onQueryTextSubmit(String query) {
1226 switchToSearchResultsFragmentIfNeeded();
1227 mSearchQuery = query;
1228 return mSearchResultsFragment.onQueryTextSubmit(query);
1229 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001230
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001231 @Override
1232 public boolean onQueryTextChange(String newText) {
1233 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001234 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001235 return false;
1236 }
1237 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001238 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001239
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001240 @Override
1241 public boolean onClose() {
1242 return false;
1243 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001244
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001245 @Override
1246 public boolean onMenuItemActionExpand(MenuItem item) {
1247 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001248 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001249 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001250 return true;
1251 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001252
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001253 @Override
1254 public boolean onMenuItemActionCollapse(MenuItem item) {
1255 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001256 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001257 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001258 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001259 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001260 return true;
1261 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001262
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001263 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001264 if (mSearchResultsFragment != null) {
1265 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001266 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001267 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001268 if (current != null && current instanceof SearchResultsSummary) {
1269 mSearchResultsFragment = (SearchResultsSummary) current;
1270 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001271 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001272 SearchResultsSummary.class.getName(), null, false, true,
1273 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001274 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001275 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001276 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001277 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001278
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001279 public void needToRevertToInitialFragment() {
1280 mNeedToRevertToInitialFragment = true;
1281 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001282
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001283 private void revertToInitialFragment() {
1284 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001285 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001286 mSearchMenuItemExpanded = false;
1287 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1288 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001289 if (mSearchMenuItem != null) {
1290 mSearchMenuItem.collapseActionView();
1291 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001292 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001293}