blob: bd53eb664788bd35e01df0c0becc02344ab34910 [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;
PauloftheWest38155612014-06-30 10:02:36 -0700103import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800104import com.android.settings.tts.TextToSpeechSettings;
105import com.android.settings.users.UserSettings;
106import com.android.settings.vpn2.VpnSettings;
107import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700108import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700110import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.WifiSettings;
112import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400113
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import org.xmlpull.v1.XmlPullParser;
115import org.xmlpull.v1.XmlPullParserException;
116
117import java.io.IOException;
118import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700120import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700122import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700123
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124public class SettingsActivity extends Activity
125 implements PreferenceManager.OnPreferenceTreeClickListener,
126 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100127 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700128 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100129 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800130
131 private static final String LOG_TAG = "Settings";
132
133 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700134 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700135 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
136 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700137 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700138 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700139 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800140
141 /**
142 * When starting this activity, the invoking Intent can contain this extra
143 * string to specify which fragment should be initially displayed.
144 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
145 * will call isValidFragment() to confirm that the fragment class name is valid for this
146 * activity.
147 */
148 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
149
150 /**
151 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
152 * this extra can also be specified to supply a Bundle of arguments to pass
153 * to that fragment when it is instantiated during the initial creation
154 * of the activity.
155 */
156 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
157
158 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700159 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
160 */
161 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
162
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800163 public static final String BACK_STACK_PREFS = ":settings:prefs";
164
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165 // extras that allow any preference activity to be launched as part of a wizard
166
167 // show Back and Next buttons? takes boolean parameter
168 // Back will then return RESULT_CANCELED and Next RESULT_OK
169 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
170
171 // add a Skip button?
172 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
173
174 // specify custom text for the Back or Next buttons, or cause a button to not appear
175 // at all by setting it to null
176 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
177 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
178
179 /**
180 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700181 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 * that fragment.
183 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700184 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID = ":settings:show_fragment_title_resid";
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700186 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT = ":settings:show_fragment_as_shortcut";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800187
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800188 private static final String META_DATA_KEY_FRAGMENT_CLASS =
189 "com.android.settings.FRAGMENT_CLASS";
190
191 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
192
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700193 private static final String EMPTY_QUERY = "";
194
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 private static boolean sShowNoHomeNotice = false;
196
197 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800199 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700200 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 // Show only these settings for restricted users
203 private int[] SETTINGS_FOR_RESTRICTED = {
204 R.id.wireless_section,
205 R.id.wifi_settings,
206 R.id.bluetooth_settings,
207 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700208 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 R.id.wireless_settings,
210 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400211 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 R.id.display_settings,
213 R.id.storage_settings,
214 R.id.application_settings,
215 R.id.battery_settings,
216 R.id.personal_section,
217 R.id.location_settings,
218 R.id.security_settings,
219 R.id.language_settings,
220 R.id.user_settings,
221 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800222 R.id.system_section,
223 R.id.date_time_settings,
224 R.id.about_settings,
225 R.id.accessibility_settings,
226 R.id.print_settings,
227 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800228 R.id.home_settings,
229 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800230 };
231
232 private static final String[] ENTRY_FRAGMENTS = {
233 WirelessSettings.class.getName(),
234 WifiSettings.class.getName(),
235 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700236 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800237 BluetoothSettings.class.getName(),
PauloftheWestc9dc66d2014-07-01 05:19:58 -0700238 MessageAccessSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700239 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 TetherSettings.class.getName(),
241 WifiP2pSettings.class.getName(),
242 VpnSettings.class.getName(),
243 DateTimeSettings.class.getName(),
244 LocalePicker.class.getName(),
245 InputMethodAndLanguageSettings.class.getName(),
246 SpellCheckersSettings.class.getName(),
247 UserDictionaryList.class.getName(),
248 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700249 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 DisplaySettings.class.getName(),
251 DeviceInfoSettings.class.getName(),
252 ManageApplications.class.getName(),
253 ProcessStatsUi.class.getName(),
254 NotificationStation.class.getName(),
255 LocationSettings.class.getName(),
256 SecuritySettings.class.getName(),
257 PrivacySettings.class.getName(),
258 DeviceAdminSettings.class.getName(),
259 AccessibilitySettings.class.getName(),
260 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
262 TextToSpeechSettings.class.getName(),
263 Memory.class.getName(),
264 DevelopmentSettings.class.getName(),
265 UsbSettings.class.getName(),
266 AndroidBeam.class.getName(),
267 WifiDisplaySettings.class.getName(),
268 PowerUsageSummary.class.getName(),
269 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000270 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 CryptKeeperSettings.class.getName(),
272 DataUsageSummary.class.getName(),
273 DreamSettings.class.getName(),
274 UserSettings.class.getName(),
275 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400276 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800277 PrintSettingsFragment.class.getName(),
278 PrintJobSettingsFragment.class.getName(),
279 TrustedCredentialsSettings.class.getName(),
280 PaymentSettings.class.getName(),
281 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700282 ZenModeSettings.class.getName(),
283 NotificationSettings.class.getName(),
284 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
285 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400286 InstalledAppDetails.class.getName(),
287 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700288 AppNotificationSettings.class.getName(),
289 OtherSoundSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 };
291
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700292
293 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
294 "android.settings.APPLICATION_DETAILS_SETTINGS"
295 };
296
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800297 private SharedPreferences mDevelopmentPreferences;
298 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
299
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 private boolean mBatteryPresent = true;
301 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 @Override
303 public void onReceive(Context context, Intent intent) {
304 String action = intent.getAction();
305 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
306 boolean batteryPresent = Utils.isBatteryPresent(intent);
307
308 if (mBatteryPresent != batteryPresent) {
309 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700310 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 }
312 }
313 }
314 };
315
Svetoslav990159a2014-04-14 17:14:59 -0700316 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
317 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700318
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700319 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700320 private SwitchBar mSwitchBar;
321
322 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700323
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700324 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700325 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700326
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700327 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700328 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700329
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700330 private ViewGroup mContent;
331
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700332 private SearchView mSearchView;
333 private MenuItem mSearchMenuItem;
334 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700335 private SearchResultsSummary mSearchResultsFragment;
336 private String mSearchQuery;
337
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700338 // Categories
339 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800340
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700341 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700342 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343 private Handler mHandler = new Handler() {
344 @Override
345 public void handleMessage(Message msg) {
346 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700347 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700348 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
349 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700350 buildDashboardCategories(mCategories);
351 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 } break;
353 }
354 }
355 };
356
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700357 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700358 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700359
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700360 public SwitchBar getSwitchBar() {
361 return mSwitchBar;
362 }
363
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700364 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
365 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700366 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700367 }
368 return mCategories;
369 }
370
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800371 @Override
372 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
373 // Override the fragment title for Wallpaper settings
374 int titleRes = pref.getTitleRes();
375 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
376 titleRes = R.string.wallpaper_settings_fragment_title;
377 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
378 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
379 if (UserManager.get(this).isLinkedUser()) {
380 titleRes = R.string.profile_info_settings_title;
381 } else {
382 titleRes = R.string.user_info_settings_title;
383 }
384 }
385 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
386 null, 0);
387 return true;
388 }
389
390 @Override
391 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
392 return false;
393 }
394
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700395 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700396 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700397 Message msg = new Message();
398 msg.what = MSG_BUILD_CATEGORIES;
399 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800400 }
401 }
402
403 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404 public void onConfigurationChanged(Configuration newConfig) {
405 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800406 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800407 }
408
409 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700410 protected void onStart() {
411 super.onStart();
412
413 if (mNeedToRevertToInitialFragment) {
414 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800415 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800416 }
417
418 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700419 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700420 if (!mDisplaySearch) {
421 return false;
422 }
423
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700424 MenuInflater inflater = getMenuInflater();
425 inflater.inflate(R.menu.options_menu, menu);
426
427 // Cache the search query (can be overriden by the OnQueryTextListener)
428 final String query = mSearchQuery;
429
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700430 mSearchMenuItem = menu.findItem(R.id.search);
431 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700432
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700433 if (mSearchMenuItem == null || mSearchView == null) {
434 return false;
435 }
436
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700437 if (mSearchResultsFragment != null) {
438 mSearchResultsFragment.setSearchView(mSearchView);
439 }
440
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700441 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700442 mSearchView.setOnQueryTextListener(this);
443 mSearchView.setOnCloseListener(this);
444
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700445 if (mSearchMenuItemExpanded) {
446 mSearchMenuItem.expandActionView();
447 }
448 mSearchView.setQuery(query, true /* submit */);
449
450 return true;
451 }
452
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700453 private static boolean isShortCutIntent(final Intent intent) {
454 Set<String> categories = intent.getCategories();
455 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
456 }
457
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700458 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700459 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700460 if (action == null) {
461 return false;
462 }
463 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
464 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
465 }
466 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700467 }
468
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700469 @Override
470 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700471 super.onCreate(savedState);
472
473 // Should happen before any call to getIntent()
474 getMetaData();
475
476 final Intent intent = getIntent();
477 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
478 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800479 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800480
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
482 Context.MODE_PRIVATE);
483
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700484 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700485 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700486
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700487 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700488 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
489
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700490 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700491 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700492
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700493 mIsShowingDashboard = className.equals(Settings.class.getName());
494 final boolean isSubSettings = className.equals(SubSettings.class.getName());
495
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700496 // If this is a sub settings or not the main Dashboard and not a Shortcut and an initial
497 // Fragment then apply the SubSettings theme for the ActionBar content insets
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700498 if (isSubSettings ||
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700499 (!mIsShowingDashboard && !mIsShortcut && (initialFragmentName != null))) {
500 // Check also that we are not a Theme Dialog as we don't want to override them
501 final int themeResId = getThemeResId();
502 if (themeResId != R.style.Theme_DialogWhenLarge &&
503 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
504 setTheme(R.style.Theme_SubSettings);
505 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700506 }
507
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700508 setContentView(mIsShowingDashboard ?
509 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800510
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700511 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700512
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800513 getFragmentManager().addOnBackStackChangedListener(this);
514
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700515 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700516 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700517 }
518
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700519 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700520 // We are restarting from a previous saved state; used that to initialize, instead
521 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700522 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
523 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800524
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700525 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800526
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700527 ArrayList<DashboardCategory> categories =
528 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
529 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700530 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700531 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700532 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700534
535 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700536 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700537 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
538 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800539 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700540 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700541 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
542 // only if it is a sub settings
543 if (mIsShortcut) {
544 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700545 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700546 } else if (isSubSettings) {
547 mDisplayHomeAsUpEnabled = true;
548 mDisplaySearch = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700549 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700550 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700551
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700552 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700553 switchToFragment(initialFragmentName, initialArguments, true, false,
554 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000555 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700556 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700557 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700558 // Show Search affordance
559 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700560 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700561 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700562 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800563 }
564 }
565
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700566 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700567 if (mActionBar != null) {
568 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
569 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
570 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700571 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
572
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800574 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
575
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700576 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 if (buttonBar != null) {
578 buttonBar.setVisibility(View.VISIBLE);
579
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700580 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 backButton.setOnClickListener(new OnClickListener() {
582 public void onClick(View v) {
583 setResult(RESULT_CANCELED);
584 finish();
585 }
586 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700587 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 skipButton.setOnClickListener(new OnClickListener() {
589 public void onClick(View v) {
590 setResult(RESULT_OK);
591 finish();
592 }
593 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700594 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 mNextButton.setOnClickListener(new OnClickListener() {
596 public void onClick(View v) {
597 setResult(RESULT_OK);
598 finish();
599 }
600 });
601
602 // set our various button parameters
603 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
604 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
605 if (TextUtils.isEmpty(buttonText)) {
606 mNextButton.setVisibility(View.GONE);
607 }
608 else {
609 mNextButton.setText(buttonText);
610 }
611 }
612 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
613 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
614 if (TextUtils.isEmpty(buttonText)) {
615 backButton.setVisibility(View.GONE);
616 }
617 else {
618 backButton.setText(buttonText);
619 }
620 }
621 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
622 skipButton.setVisibility(View.VISIBLE);
623 }
624 }
625 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700626
627 mHomeActivitiesCount = getHomeActivitiesCount();
628 }
629
630 private int getHomeActivitiesCount() {
631 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
632 getPackageManager().getHomeActivities(homeApps);
633 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800634 }
635
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700636 private void setTitleFromIntent(Intent intent) {
637 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
638 if (initialTitleResId > 0) {
639 mInitialTitle = null;
640 mInitialTitleResId = initialTitleResId;
641 setTitle(mInitialTitleResId);
642 } else {
643 mInitialTitleResId = -1;
644 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
645 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
646 setTitle(mInitialTitle);
647 }
648 }
649
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800650 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700652 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 }
654
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700655 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800656 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700657
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800658 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700659 if (mInitialTitleResId > 0) {
660 setTitle(mInitialTitleResId);
661 } else {
662 setTitle(mInitialTitle);
663 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700664 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800665 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700666
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800667 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
668 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700669
670 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800671 }
672
673 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
674 final CharSequence title;
675 final int titleRes = bse.getBreadCrumbTitleRes();
676 if (titleRes > 0) {
677 title = getText(titleRes);
678 } else {
679 title = bse.getBreadCrumbTitle();
680 }
681 if (title != null) {
682 setTitle(title);
683 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800684 }
685
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800686 @Override
687 protected void onSaveInstanceState(Bundle outState) {
688 super.onSaveInstanceState(outState);
689
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700690 if (mCategories.size() > 0) {
691 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800692 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700693
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700694 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700695 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700696
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700697 if (mDisplaySearch) {
698 // The option menus are created if the ActionBar is visible and they are also created
699 // asynchronously. If you launch Settings with an Intent action like
700 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
701 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
702 // menu item and search view are null.
703 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
704 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700705
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700706 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
707 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
708 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700709
710 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800711 }
712
713 @Override
714 public void onResume() {
715 super.onResume();
716
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700717 final int newHomeActivityCount = getHomeActivitiesCount();
718 if (newHomeActivityCount != mHomeActivitiesCount) {
719 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700720 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700721 }
722
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800723 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
724 @Override
725 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700726 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800727 }
728 };
729 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
730 mDevelopmentPreferencesListener);
731
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800732 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700733
Svetoslav990159a2014-04-14 17:14:59 -0700734 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700735
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700736 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700737 onQueryTextSubmit(mSearchQuery);
738 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800739 }
740
741 @Override
742 public void onPause() {
743 super.onPause();
744
745 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700746 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800747 }
748
749 @Override
750 public void onDestroy() {
751 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700752
753 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
754 mDevelopmentPreferencesListener);
755 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800756 }
757
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800758 protected boolean isValidFragment(String fragmentName) {
759 // Almost all fragments are wrapped in this,
760 // except for a few that have their own activities.
761 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
762 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
763 }
764 return false;
765 }
766
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800767 @Override
768 public Intent getIntent() {
769 Intent superIntent = super.getIntent();
770 String startingFragment = getStartingFragmentClass(superIntent);
771 // This is called from super.onCreate, isMultiPane() is not yet reliable
772 // Do not use onIsHidingHeaders either, which relies itself on this method
773 if (startingFragment != null) {
774 Intent modIntent = new Intent(superIntent);
775 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
776 Bundle args = superIntent.getExtras();
777 if (args != null) {
778 args = new Bundle(args);
779 } else {
780 args = new Bundle();
781 }
782 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100783 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800784 return modIntent;
785 }
786 return superIntent;
787 }
788
789 /**
790 * Checks if the component name in the intent is different from the Settings class and
791 * returns the class name to load as a fragment.
792 */
793 private String getStartingFragmentClass(Intent intent) {
794 if (mFragmentClass != null) return mFragmentClass;
795
796 String intentClass = intent.getComponent().getClassName();
797 if (intentClass.equals(getClass().getName())) return null;
798
799 if ("com.android.settings.ManageApplications".equals(intentClass)
800 || "com.android.settings.RunningServices".equals(intentClass)
801 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
802 // Old names of manage apps.
803 intentClass = com.android.settings.applications.ManageApplications.class.getName();
804 }
805
806 return intentClass;
807 }
808
809 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000810 * Start a new fragment containing a preference panel. If the preferences
811 * are being displayed in multi-pane mode, the given fragment class will
812 * be instantiated and placed in the appropriate pane. If running in
813 * single-pane mode, a new activity will be launched in which to show the
814 * fragment.
815 *
816 * @param fragmentClass Full name of the class implementing the fragment.
817 * @param args Any desired arguments to supply to the fragment.
818 * @param titleRes Optional resource identifier of the title of this
819 * fragment.
820 * @param titleText Optional text of the title of this fragment.
821 * @param resultTo Optional fragment that result data should be sent to.
822 * If non-null, resultTo.onActivityResult() will be called when this
823 * preference panel is done. The launched panel must use
824 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
825 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700826 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000827 */
828 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700829 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700830 String title = null;
831 if (titleRes < 0) {
832 if (titleText != null) {
833 title = titleText.toString();
834 } else {
835 // There not much we can do in that case
836 title = "";
837 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700838 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700839 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700840 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000841 }
842
843 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800844 * Called by a preference panel fragment to finish itself.
845 *
846 * @param caller The fragment that is asking to be finished.
847 * @param resultCode Optional result code to send back to the original
848 * launching fragment.
849 * @param resultData Optional result data to send back to the original
850 * launching fragment.
851 */
852 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
853 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700854 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800855 }
856
857 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000858 * Start a new fragment.
859 *
860 * @param fragment The fragment to start
861 * @param push If true, the current fragment will be pushed onto the back stack. If false,
862 * the current fragment will be replaced.
863 */
864 public void startPreferenceFragment(Fragment fragment, boolean push) {
865 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700866 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000867 if (push) {
868 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
869 transaction.addToBackStack(BACK_STACK_PREFS);
870 } else {
871 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
872 }
873 transaction.commitAllowingStateLoss();
874 }
875
876 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700877 * Switch to a specific Fragment with taking care of validation, Title and BackStack
878 */
879 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700880 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700881 if (validate && !isValidFragment(fragmentName)) {
882 throw new IllegalArgumentException("Invalid fragment for this activity: "
883 + fragmentName);
884 }
885 Fragment f = Fragment.instantiate(this, fragmentName, args);
886 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700887 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700888 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700889 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700890 }
891 if (addToBackStack) {
892 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
893 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700894 if (titleResId > 0) {
895 transaction.setBreadCrumbTitle(titleResId);
896 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700897 transaction.setBreadCrumbTitle(title);
898 }
899 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700900 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700901 return f;
902 }
903
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700904 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700905 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700906 *
907 * @param categories The list in which to place the tiles categories.
908 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700909 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700910 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700911 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
912 updateTilesList(categories);
913 }
914
915 /**
916 * Parse the given XML file as a categories description, adding each
917 * parsed categories and tiles into the target list.
918 *
919 * @param resid The XML resource to load and parse.
920 * @param target The list in which the parsed categories and tiles should be placed.
921 */
922 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
923 XmlResourceParser parser = null;
924 try {
925 parser = getResources().getXml(resid);
926 AttributeSet attrs = Xml.asAttributeSet(parser);
927
928 int type;
929 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
930 && type != XmlPullParser.START_TAG) {
931 // Parse next until start tag is found
932 }
933
934 String nodeName = parser.getName();
935 if (!"dashboard-categories".equals(nodeName)) {
936 throw new RuntimeException(
937 "XML document must start with <preference-categories> tag; found"
938 + nodeName + " at " + parser.getPositionDescription());
939 }
940
941 Bundle curBundle = null;
942
943 final int outerDepth = parser.getDepth();
944 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
945 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
946 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
947 continue;
948 }
949
950 nodeName = parser.getName();
951 if ("dashboard-category".equals(nodeName)) {
952 DashboardCategory category = new DashboardCategory();
953
954 TypedArray sa = obtainStyledAttributes(
955 attrs, com.android.internal.R.styleable.PreferenceHeader);
956 category.id = sa.getResourceId(
957 com.android.internal.R.styleable.PreferenceHeader_id,
958 (int)DashboardCategory.CAT_ID_UNDEFINED);
959
960 TypedValue tv = sa.peekValue(
961 com.android.internal.R.styleable.PreferenceHeader_title);
962 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
963 if (tv.resourceId != 0) {
964 category.titleRes = tv.resourceId;
965 } else {
966 category.title = tv.string;
967 }
968 }
969 sa.recycle();
970
971 final int innerDepth = parser.getDepth();
972 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
973 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
974 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
975 continue;
976 }
977
978 String innerNodeName = parser.getName();
979 if (innerNodeName.equals("dashboard-tile")) {
980 DashboardTile tile = new DashboardTile();
981
982 sa = obtainStyledAttributes(
983 attrs, com.android.internal.R.styleable.PreferenceHeader);
984 tile.id = sa.getResourceId(
985 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700986 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700987 tv = sa.peekValue(
988 com.android.internal.R.styleable.PreferenceHeader_title);
989 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
990 if (tv.resourceId != 0) {
991 tile.titleRes = tv.resourceId;
992 } else {
993 tile.title = tv.string;
994 }
995 }
996 tv = sa.peekValue(
997 com.android.internal.R.styleable.PreferenceHeader_summary);
998 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
999 if (tv.resourceId != 0) {
1000 tile.summaryRes = tv.resourceId;
1001 } else {
1002 tile.summary = tv.string;
1003 }
1004 }
1005 tile.iconRes = sa.getResourceId(
1006 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1007 tile.fragment = sa.getString(
1008 com.android.internal.R.styleable.PreferenceHeader_fragment);
1009 sa.recycle();
1010
1011 if (curBundle == null) {
1012 curBundle = new Bundle();
1013 }
1014
1015 final int innerDepth2 = parser.getDepth();
1016 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1017 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1018 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1019 continue;
1020 }
1021
1022 String innerNodeName2 = parser.getName();
1023 if (innerNodeName2.equals("extra")) {
1024 getResources().parseBundleExtra("extra", attrs, curBundle);
1025 XmlUtils.skipCurrentTag(parser);
1026
1027 } else if (innerNodeName2.equals("intent")) {
1028 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1029
1030 } else {
1031 XmlUtils.skipCurrentTag(parser);
1032 }
1033 }
1034
1035 if (curBundle.size() > 0) {
1036 tile.fragmentArguments = curBundle;
1037 curBundle = null;
1038 }
1039
PauloftheWest38155612014-06-30 10:02:36 -07001040 // Show the SIM Cards setting if there are more than 2 SIMs installed.
1041 if(tile.id != R.id.sim_settings || SimSettings.showSimCardScreen(this)){
1042 category.addTile(tile);
1043 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001044
1045 } else {
1046 XmlUtils.skipCurrentTag(parser);
1047 }
1048 }
1049
1050 target.add(category);
1051 } else {
1052 XmlUtils.skipCurrentTag(parser);
1053 }
1054 }
1055
1056 } catch (XmlPullParserException e) {
1057 throw new RuntimeException("Error parsing categories", e);
1058 } catch (IOException e) {
1059 throw new RuntimeException("Error parsing categories", e);
1060 } finally {
1061 if (parser != null) parser.close();
1062 }
1063 }
1064
1065 private void updateTilesList(List<DashboardCategory> target) {
1066 final boolean showDev = mDevelopmentPreferences.getBoolean(
1067 DevelopmentSettings.PREF_SHOW,
1068 android.os.Build.TYPE.equals("eng"));
1069
1070 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1071
1072 final int size = target.size();
1073 for (int i = 0; i < size; i++) {
1074
1075 DashboardCategory category = target.get(i);
1076
1077 // Ids are integers, so downcasting is ok
1078 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001079 int n = category.getTilesCount() - 1;
1080 while (n >= 0) {
1081
1082 DashboardTile tile = category.getTile(n);
1083
1084 id = (int) tile.id;
1085 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
1086 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, category, tile);
1087 } else if (id == R.id.wifi_settings) {
1088 // Remove WiFi Settings if WiFi service is not available.
1089 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
1090 category.removeTile(n);
1091 }
1092 } else if (id == R.id.bluetooth_settings) {
1093 // Remove Bluetooth Settings if Bluetooth service is not available.
1094 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
1095 category.removeTile(n);
1096 }
1097 } else if (id == R.id.data_usage_settings) {
1098 // Remove data usage when kernel module not enabled
1099 final INetworkManagementService netManager = INetworkManagementService.Stub
1100 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1101 try {
1102 if (!netManager.isBandwidthControlEnabled()) {
1103 category.removeTile(n);
1104 }
1105 } catch (RemoteException e) {
1106 // ignored
1107 }
1108 } else if (id == R.id.battery_settings) {
1109 // Remove battery settings when battery is not available. (e.g. TV)
1110
1111 if (!mBatteryPresent) {
1112 category.removeTile(n);
1113 }
1114 } else if (id == R.id.home_settings) {
1115 if (!updateHomeSettingTiles(tile)) {
1116 category.removeTile(n);
1117 }
1118 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001119 boolean hasMultipleUsers =
1120 ((UserManager) getSystemService(Context.USER_SERVICE))
1121 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001122 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001123 || (!UserManager.supportsMultipleUsers()
1124 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001125 || Utils.isMonkeyRunning()) {
1126 category.removeTile(n);
1127 }
1128 } else if (id == R.id.nfc_payment_settings) {
1129 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
1130 category.removeTile(n);
1131 } else {
1132 // Only show if NFC is on and we have the HCE feature
1133 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1134 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1135 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
1136 category.removeTile(n);
1137 }
1138 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001139 } else if (id == R.id.print_settings) {
1140 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1141 PackageManager.FEATURE_PRINTING);
1142 if (!hasPrintingSupport) {
1143 category.removeTile(n);
1144 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001145 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001146 if (!showDev || um.hasUserRestriction(
1147 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001148 category.removeTile(n);
1149 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001150 }
1151
1152 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasania97089d2014-04-30 10:58:09 -07001153 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)
1154 && n < category.getTilesCount()) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001155 category.removeTile(n);
1156 }
1157
1158 n--;
1159 }
1160 }
1161 }
1162
1163 private boolean updateHomeSettingTiles(DashboardTile tile) {
1164 // Once we decide to show Home settings, keep showing it forever
1165 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1166 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1167 return true;
1168 }
1169
1170 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001171 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001172 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001173 // When there's only one available home app, omit this settings
1174 // category entirely at the top level UI. If the user just
1175 // uninstalled the penultimate home app candidiate, we also
1176 // now tell them about why they aren't seeing 'Home' in the list.
1177 if (sShowNoHomeNotice) {
1178 sShowNoHomeNotice = false;
1179 NoHomeDialogFragment.show(this);
1180 }
1181 return false;
1182 } else {
1183 // Okay, we're allowing the Home settings category. Tell it, when
1184 // invoked via this front door, that we'll need to be told about the
1185 // case when the user uninstalls all but one home app.
1186 if (tile.fragmentArguments == null) {
1187 tile.fragmentArguments = new Bundle();
1188 }
1189 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1190 }
1191 } catch (Exception e) {
1192 // Can't look up the home activity; bail on configuring the icon
1193 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1194 }
1195
1196 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1197 return true;
1198 }
1199
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001200 private void getMetaData() {
1201 try {
1202 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1203 PackageManager.GET_META_DATA);
1204 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001205 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1206 } catch (NameNotFoundException nnfe) {
1207 // No recovery
1208 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1209 }
1210 }
1211
1212 // give subclasses access to the Next button
1213 public boolean hasNextButton() {
1214 return mNextButton != null;
1215 }
1216
1217 public Button getNextButton() {
1218 return mNextButton;
1219 }
1220
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001221 @Override
1222 public boolean shouldUpRecreateTask(Intent targetIntent) {
1223 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1224 }
1225
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001226 public static void requestHomeNotice() {
1227 sShowNoHomeNotice = true;
1228 }
1229
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001230 @Override
1231 public boolean onQueryTextSubmit(String query) {
1232 switchToSearchResultsFragmentIfNeeded();
1233 mSearchQuery = query;
1234 return mSearchResultsFragment.onQueryTextSubmit(query);
1235 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001236
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001237 @Override
1238 public boolean onQueryTextChange(String newText) {
1239 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001240 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001241 return false;
1242 }
1243 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001244 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001245
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001246 @Override
1247 public boolean onClose() {
1248 return false;
1249 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001250
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001251 @Override
1252 public boolean onMenuItemActionExpand(MenuItem item) {
1253 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001254 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001255 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001256 return true;
1257 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001258
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001259 @Override
1260 public boolean onMenuItemActionCollapse(MenuItem item) {
1261 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001262 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001263 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001264 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001265 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001266 return true;
1267 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001268
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001269 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001270 if (mSearchResultsFragment != null) {
1271 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001272 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001273 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001274 if (current != null && current instanceof SearchResultsSummary) {
1275 mSearchResultsFragment = (SearchResultsSummary) current;
1276 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001277 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001278 SearchResultsSummary.class.getName(), null, false, true,
1279 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001280 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001281 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001282 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001283 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001284
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001285 public void needToRevertToInitialFragment() {
1286 mNeedToRevertToInitialFragment = true;
1287 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001288
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001289 private void revertToInitialFragment() {
1290 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001291 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001292 mSearchMenuItemExpanded = false;
1293 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1294 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001295 if (mSearchMenuItem != null) {
1296 mSearchMenuItem.collapseActionView();
1297 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001298 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001299}