blob: dc5675c9090003db10495fdc84fed36c00b568e4 [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;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070075import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070077import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070078import com.android.settings.dashboard.NoHomeDialogFragment;
79import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.deviceinfo.Memory;
81import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040082import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083import com.android.settings.fuelgauge.PowerUsageSummary;
John Spurlock802ddf92014-07-18 11:51:13 -040084import com.android.settings.notification.NotificationAppList;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070085import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -070086import com.android.settings.quicklaunch.QuickLaunchSettings;
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 Spurlock802ddf92014-07-18 11:51:13 -040096import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040097import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
101import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800102import com.android.settings.print.PrintJobSettingsFragment;
103import com.android.settings.print.PrintSettingsFragment;
PauloftheWest38155612014-06-30 10:02:36 -0700104import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800105import com.android.settings.tts.TextToSpeechSettings;
106import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700107import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.vpn2.VpnSettings;
109import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700110import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700112import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.wifi.WifiSettings;
114import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400115
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import org.xmlpull.v1.XmlPullParser;
117import org.xmlpull.v1.XmlPullParserException;
118
119import java.io.IOException;
120import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700122import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700124import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700125
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126public class SettingsActivity extends Activity
127 implements PreferenceManager.OnPreferenceTreeClickListener,
128 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100129 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700130 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100131 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133 private static final String LOG_TAG = "Settings";
134
135 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700136 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
138 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700139 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700140 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700141 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
143 /**
144 * When starting this activity, the invoking Intent can contain this extra
145 * string to specify which fragment should be initially displayed.
146 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
147 * will call isValidFragment() to confirm that the fragment class name is valid for this
148 * activity.
149 */
150 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
151
152 /**
153 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
154 * this extra can also be specified to supply a Bundle of arguments to pass
155 * to that fragment when it is instantiated during the initial creation
156 * of the activity.
157 */
158 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
159
160 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700161 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
162 */
163 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
164
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800165 public static final String BACK_STACK_PREFS = ":settings:prefs";
166
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167 // extras that allow any preference activity to be launched as part of a wizard
168
169 // show Back and Next buttons? takes boolean parameter
170 // Back will then return RESULT_CANCELED and Next RESULT_OK
171 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
172
173 // add a Skip button?
174 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
175
176 // specify custom text for the Back or Next buttons, or cause a button to not appear
177 // at all by setting it to null
178 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
179 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
180
181 /**
182 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700183 * 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 -0800184 * that fragment.
185 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100187 /**
188 * The package name used to resolve the title resource id.
189 */
190 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
191 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700192 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
193 ":settings:show_fragment_title_resid";
194 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
195 ":settings:show_fragment_as_shortcut";
196
197 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
198 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800199
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200 private static final String META_DATA_KEY_FRAGMENT_CLASS =
201 "com.android.settings.FRAGMENT_CLASS";
202
203 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
204
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700205 private static final String EMPTY_QUERY = "";
206
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207 private static boolean sShowNoHomeNotice = false;
208
209 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800210
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800211 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700212 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800213
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800214 // Show only these settings for restricted users
215 private int[] SETTINGS_FOR_RESTRICTED = {
216 R.id.wireless_section,
217 R.id.wifi_settings,
218 R.id.bluetooth_settings,
219 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700220 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800221 R.id.wireless_settings,
222 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400223 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 R.id.display_settings,
225 R.id.storage_settings,
226 R.id.application_settings,
227 R.id.battery_settings,
228 R.id.personal_section,
229 R.id.location_settings,
230 R.id.security_settings,
231 R.id.language_settings,
232 R.id.user_settings,
233 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800234 R.id.system_section,
235 R.id.date_time_settings,
236 R.id.about_settings,
237 R.id.accessibility_settings,
238 R.id.print_settings,
239 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800240 R.id.home_settings,
241 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800242 };
243
244 private static final String[] ENTRY_FRAGMENTS = {
245 WirelessSettings.class.getName(),
246 WifiSettings.class.getName(),
247 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700248 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800249 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700250 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 TetherSettings.class.getName(),
252 WifiP2pSettings.class.getName(),
253 VpnSettings.class.getName(),
254 DateTimeSettings.class.getName(),
255 LocalePicker.class.getName(),
256 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700257 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800258 SpellCheckersSettings.class.getName(),
259 UserDictionaryList.class.getName(),
260 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700261 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800262 DisplaySettings.class.getName(),
263 DeviceInfoSettings.class.getName(),
264 ManageApplications.class.getName(),
265 ProcessStatsUi.class.getName(),
266 NotificationStation.class.getName(),
267 LocationSettings.class.getName(),
268 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700269 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270 PrivacySettings.class.getName(),
271 DeviceAdminSettings.class.getName(),
272 AccessibilitySettings.class.getName(),
273 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
275 TextToSpeechSettings.class.getName(),
276 Memory.class.getName(),
277 DevelopmentSettings.class.getName(),
278 UsbSettings.class.getName(),
279 AndroidBeam.class.getName(),
280 WifiDisplaySettings.class.getName(),
281 PowerUsageSummary.class.getName(),
282 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000283 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800284 CryptKeeperSettings.class.getName(),
285 DataUsageSummary.class.getName(),
286 DreamSettings.class.getName(),
287 UserSettings.class.getName(),
288 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400289 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 PrintSettingsFragment.class.getName(),
291 PrintJobSettingsFragment.class.getName(),
292 TrustedCredentialsSettings.class.getName(),
293 PaymentSettings.class.getName(),
294 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700295 ZenModeSettings.class.getName(),
296 NotificationSettings.class.getName(),
297 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
298 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400299 InstalledAppDetails.class.getName(),
300 BatterySaverSettings.class.getName(),
John Spurlock802ddf92014-07-18 11:51:13 -0400301 NotificationAppList.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700302 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700303 OtherSoundSettings.class.getName(),
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700304 QuickLaunchSettings.class.getName(),
305 ApnSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 };
307
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700308
309 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
310 "android.settings.APPLICATION_DETAILS_SETTINGS"
311 };
312
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 private SharedPreferences mDevelopmentPreferences;
314 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
315
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 private boolean mBatteryPresent = true;
317 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 @Override
319 public void onReceive(Context context, Intent intent) {
320 String action = intent.getAction();
321 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
322 boolean batteryPresent = Utils.isBatteryPresent(intent);
323
324 if (mBatteryPresent != batteryPresent) {
325 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700326 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800327 }
328 }
329 }
330 };
331
Svetoslav990159a2014-04-14 17:14:59 -0700332 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
333 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700334
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700335 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700336 private SwitchBar mSwitchBar;
337
338 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700339
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700340 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700341 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700342
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700343 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700344 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700345
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700346 private ViewGroup mContent;
347
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700348 private SearchView mSearchView;
349 private MenuItem mSearchMenuItem;
350 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700351 private SearchResultsSummary mSearchResultsFragment;
352 private String mSearchQuery;
353
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700354 // Categories
355 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700357 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700358 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 private Handler mHandler = new Handler() {
360 @Override
361 public void handleMessage(Message msg) {
362 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700363 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700364 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
365 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700366 buildDashboardCategories(mCategories);
367 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 } break;
369 }
370 }
371 };
372
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700373 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700374 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700375
Jim Miller0698a212014-10-16 19:49:07 -0700376 private Intent mResultIntentData;
377
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700378 public SwitchBar getSwitchBar() {
379 return mSwitchBar;
380 }
381
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700382 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
383 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700384 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700385 }
386 return mCategories;
387 }
388
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800389 @Override
390 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
391 // Override the fragment title for Wallpaper settings
392 int titleRes = pref.getTitleRes();
393 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
394 titleRes = R.string.wallpaper_settings_fragment_title;
395 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
396 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
397 if (UserManager.get(this).isLinkedUser()) {
398 titleRes = R.string.profile_info_settings_title;
399 } else {
400 titleRes = R.string.user_info_settings_title;
401 }
402 }
403 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
404 null, 0);
405 return true;
406 }
407
408 @Override
409 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
410 return false;
411 }
412
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700413 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700414 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700415 Message msg = new Message();
416 msg.what = MSG_BUILD_CATEGORIES;
417 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800418 }
419 }
420
421 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800422 public void onConfigurationChanged(Configuration newConfig) {
423 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800424 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800425 }
426
427 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700428 protected void onStart() {
429 super.onStart();
430
431 if (mNeedToRevertToInitialFragment) {
432 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800433 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800434 }
435
436 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700437 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700438 if (!mDisplaySearch) {
439 return false;
440 }
441
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700442 MenuInflater inflater = getMenuInflater();
443 inflater.inflate(R.menu.options_menu, menu);
444
445 // Cache the search query (can be overriden by the OnQueryTextListener)
446 final String query = mSearchQuery;
447
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700448 mSearchMenuItem = menu.findItem(R.id.search);
449 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700450
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700451 if (mSearchMenuItem == null || mSearchView == null) {
452 return false;
453 }
454
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700455 if (mSearchResultsFragment != null) {
456 mSearchResultsFragment.setSearchView(mSearchView);
457 }
458
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700459 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700460 mSearchView.setOnQueryTextListener(this);
461 mSearchView.setOnCloseListener(this);
462
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700463 if (mSearchMenuItemExpanded) {
464 mSearchMenuItem.expandActionView();
465 }
466 mSearchView.setQuery(query, true /* submit */);
467
468 return true;
469 }
470
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700471 private static boolean isShortCutIntent(final Intent intent) {
472 Set<String> categories = intent.getCategories();
473 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
474 }
475
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700476 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700477 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700478 if (action == null) {
479 return false;
480 }
481 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
482 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
483 }
484 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700485 }
486
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700487 @Override
488 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700489 super.onCreate(savedState);
490
491 // Should happen before any call to getIntent()
492 getMetaData();
493
494 final Intent intent = getIntent();
495 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
496 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800497 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800498
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
500 Context.MODE_PRIVATE);
501
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700502 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700503 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700504
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700505 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700506 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
507
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700508 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700509 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700510
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700511 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700512
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700513 // This is a "Sub Settings" when:
514 // - this is a real SubSettings
515 // - or :settings:show_fragment_as_subsetting is passed to the Intent
516 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
517 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
518
519 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
520 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700521 // Check also that we are not a Theme Dialog as we don't want to override them
522 final int themeResId = getThemeResId();
523 if (themeResId != R.style.Theme_DialogWhenLarge &&
524 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
525 setTheme(R.style.Theme_SubSettings);
526 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700527 }
528
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700529 setContentView(mIsShowingDashboard ?
530 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800531
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700532 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700533
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534 getFragmentManager().addOnBackStackChangedListener(this);
535
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700536 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700537 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700538 }
539
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700540 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700541 // We are restarting from a previous saved state; used that to initialize, instead
542 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700543 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
544 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700546 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800547
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700548 ArrayList<DashboardCategory> categories =
549 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
550 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700551 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700552 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700553 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800554 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700555
556 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700557 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700558 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
559 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700561 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700562 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
563 // only if it is a sub settings
564 if (mIsShortcut) {
565 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700566 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700567 } else if (isSubSettings) {
568 mDisplayHomeAsUpEnabled = true;
569 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700570 } else {
571 mDisplayHomeAsUpEnabled = false;
572 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700573 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700574 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700575
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700576 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700577 switchToFragment(initialFragmentName, initialArguments, true, false,
578 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000579 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700580 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700581 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700582 // Show Search affordance
583 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700584 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700585 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700586 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800587 }
588 }
589
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700590 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700591 if (mActionBar != null) {
592 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
593 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
594 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700595 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
596
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800598 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
599
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700600 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601 if (buttonBar != null) {
602 buttonBar.setVisibility(View.VISIBLE);
603
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700604 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800605 backButton.setOnClickListener(new OnClickListener() {
606 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700607 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800608 finish();
609 }
610 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700611 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800612 skipButton.setOnClickListener(new OnClickListener() {
613 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700614 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800615 finish();
616 }
617 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700618 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800619 mNextButton.setOnClickListener(new OnClickListener() {
620 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700621 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800622 finish();
623 }
624 });
625
626 // set our various button parameters
627 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
628 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
629 if (TextUtils.isEmpty(buttonText)) {
630 mNextButton.setVisibility(View.GONE);
631 }
632 else {
633 mNextButton.setText(buttonText);
634 }
635 }
636 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
637 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
638 if (TextUtils.isEmpty(buttonText)) {
639 backButton.setVisibility(View.GONE);
640 }
641 else {
642 backButton.setText(buttonText);
643 }
644 }
645 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
646 skipButton.setVisibility(View.VISIBLE);
647 }
648 }
649 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700650
651 mHomeActivitiesCount = getHomeActivitiesCount();
652 }
653
654 private int getHomeActivitiesCount() {
655 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
656 getPackageManager().getHomeActivities(homeApps);
657 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800658 }
659
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700660 private void setTitleFromIntent(Intent intent) {
661 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
662 if (initialTitleResId > 0) {
663 mInitialTitle = null;
664 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100665
666 final String initialTitleResPackageName = intent.getStringExtra(
667 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
668 if (initialTitleResPackageName != null) {
669 try {
670 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
671 0 /* flags */, new UserHandle(UserHandle.myUserId()));
672 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
673 setTitle(mInitialTitle);
674 mInitialTitleResId = -1;
675 return;
676 } catch (NameNotFoundException e) {
677 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
678 }
679 } else {
680 setTitle(mInitialTitleResId);
681 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700682 } else {
683 mInitialTitleResId = -1;
684 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
685 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
686 setTitle(mInitialTitle);
687 }
688 }
689
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800690 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800691 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700692 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 }
694
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700695 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800696 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700697
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800698 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700699 if (mInitialTitleResId > 0) {
700 setTitle(mInitialTitleResId);
701 } else {
702 setTitle(mInitialTitle);
703 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700704 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800705 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700706
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800707 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
708 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700709
710 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800711 }
712
713 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
714 final CharSequence title;
715 final int titleRes = bse.getBreadCrumbTitleRes();
716 if (titleRes > 0) {
717 title = getText(titleRes);
718 } else {
719 title = bse.getBreadCrumbTitle();
720 }
721 if (title != null) {
722 setTitle(title);
723 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800724 }
725
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800726 @Override
727 protected void onSaveInstanceState(Bundle outState) {
728 super.onSaveInstanceState(outState);
729
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700730 if (mCategories.size() > 0) {
731 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800732 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700733
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700734 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700735 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700736
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700737 if (mDisplaySearch) {
738 // The option menus are created if the ActionBar is visible and they are also created
739 // asynchronously. If you launch Settings with an Intent action like
740 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
741 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
742 // menu item and search view are null.
743 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
744 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700745
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700746 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
747 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
748 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700749
750 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800751 }
752
753 @Override
754 public void onResume() {
755 super.onResume();
756
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700757 final int newHomeActivityCount = getHomeActivitiesCount();
758 if (newHomeActivityCount != mHomeActivitiesCount) {
759 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700760 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700761 }
762
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800763 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
764 @Override
765 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700766 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800767 }
768 };
769 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
770 mDevelopmentPreferencesListener);
771
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800772 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700773
Svetoslav990159a2014-04-14 17:14:59 -0700774 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700775
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700776 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700777 onQueryTextSubmit(mSearchQuery);
778 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800779 }
780
781 @Override
782 public void onPause() {
783 super.onPause();
784
785 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700786 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800787 }
788
789 @Override
790 public void onDestroy() {
791 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700792
793 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
794 mDevelopmentPreferencesListener);
795 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800796 }
797
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800798 protected boolean isValidFragment(String fragmentName) {
799 // Almost all fragments are wrapped in this,
800 // except for a few that have their own activities.
801 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
802 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
803 }
804 return false;
805 }
806
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800807 @Override
808 public Intent getIntent() {
809 Intent superIntent = super.getIntent();
810 String startingFragment = getStartingFragmentClass(superIntent);
811 // This is called from super.onCreate, isMultiPane() is not yet reliable
812 // Do not use onIsHidingHeaders either, which relies itself on this method
813 if (startingFragment != null) {
814 Intent modIntent = new Intent(superIntent);
815 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
816 Bundle args = superIntent.getExtras();
817 if (args != null) {
818 args = new Bundle(args);
819 } else {
820 args = new Bundle();
821 }
822 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100823 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800824 return modIntent;
825 }
826 return superIntent;
827 }
828
829 /**
830 * Checks if the component name in the intent is different from the Settings class and
831 * returns the class name to load as a fragment.
832 */
833 private String getStartingFragmentClass(Intent intent) {
834 if (mFragmentClass != null) return mFragmentClass;
835
836 String intentClass = intent.getComponent().getClassName();
837 if (intentClass.equals(getClass().getName())) return null;
838
839 if ("com.android.settings.ManageApplications".equals(intentClass)
840 || "com.android.settings.RunningServices".equals(intentClass)
841 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
842 // Old names of manage apps.
843 intentClass = com.android.settings.applications.ManageApplications.class.getName();
844 }
845
846 return intentClass;
847 }
848
849 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000850 * Start a new fragment containing a preference panel. If the preferences
851 * are being displayed in multi-pane mode, the given fragment class will
852 * be instantiated and placed in the appropriate pane. If running in
853 * single-pane mode, a new activity will be launched in which to show the
854 * fragment.
855 *
856 * @param fragmentClass Full name of the class implementing the fragment.
857 * @param args Any desired arguments to supply to the fragment.
858 * @param titleRes Optional resource identifier of the title of this
859 * fragment.
860 * @param titleText Optional text of the title of this fragment.
861 * @param resultTo Optional fragment that result data should be sent to.
862 * If non-null, resultTo.onActivityResult() will be called when this
863 * preference panel is done. The launched panel must use
864 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
865 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700866 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000867 */
868 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700869 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700870 String title = null;
871 if (titleRes < 0) {
872 if (titleText != null) {
873 title = titleText.toString();
874 } else {
875 // There not much we can do in that case
876 title = "";
877 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700878 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700879 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700880 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000881 }
882
883 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100884 * Start a new fragment in a new activity containing a preference panel for a given user. If the
885 * preferences are being displayed in multi-pane mode, the given fragment class will be
886 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
887 * activity will be launched in which to show the fragment.
888 *
889 * @param fragmentClass Full name of the class implementing the fragment.
890 * @param args Any desired arguments to supply to the fragment.
891 * @param titleRes Optional resource identifier of the title of this fragment.
892 * @param titleText Optional text of the title of this fragment.
893 * @param userHandle The user for which the panel has to be started.
894 */
895 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
896 CharSequence titleText, UserHandle userHandle) {
897 String title = null;
898 if (titleRes < 0) {
899 if (titleText != null) {
900 title = titleText.toString();
901 } else {
902 // There not much we can do in that case
903 title = "";
904 }
905 }
906 Utils.startWithFragmentAsUser(this, fragmentClass, args,
907 titleRes, title, mIsShortcut, userHandle);
908 }
909
910 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800911 * Called by a preference panel fragment to finish itself.
912 *
913 * @param caller The fragment that is asking to be finished.
914 * @param resultCode Optional result code to send back to the original
915 * launching fragment.
916 * @param resultData Optional result data to send back to the original
917 * launching fragment.
918 */
919 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
920 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700921 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800922 }
923
924 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000925 * Start a new fragment.
926 *
927 * @param fragment The fragment to start
928 * @param push If true, the current fragment will be pushed onto the back stack. If false,
929 * the current fragment will be replaced.
930 */
931 public void startPreferenceFragment(Fragment fragment, boolean push) {
932 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700933 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000934 if (push) {
935 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
936 transaction.addToBackStack(BACK_STACK_PREFS);
937 } else {
938 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
939 }
940 transaction.commitAllowingStateLoss();
941 }
942
943 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700944 * Switch to a specific Fragment with taking care of validation, Title and BackStack
945 */
946 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700947 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700948 if (validate && !isValidFragment(fragmentName)) {
949 throw new IllegalArgumentException("Invalid fragment for this activity: "
950 + fragmentName);
951 }
952 Fragment f = Fragment.instantiate(this, fragmentName, args);
953 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700954 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700955 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700956 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700957 }
958 if (addToBackStack) {
959 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
960 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700961 if (titleResId > 0) {
962 transaction.setBreadCrumbTitle(titleResId);
963 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700964 transaction.setBreadCrumbTitle(title);
965 }
966 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700967 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700968 return f;
969 }
970
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700971 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700972 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700973 *
974 * @param categories The list in which to place the tiles categories.
975 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700976 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700977 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700978 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
979 updateTilesList(categories);
980 }
981
982 /**
983 * Parse the given XML file as a categories description, adding each
984 * parsed categories and tiles into the target list.
985 *
986 * @param resid The XML resource to load and parse.
987 * @param target The list in which the parsed categories and tiles should be placed.
988 */
989 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
990 XmlResourceParser parser = null;
991 try {
992 parser = getResources().getXml(resid);
993 AttributeSet attrs = Xml.asAttributeSet(parser);
994
995 int type;
996 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
997 && type != XmlPullParser.START_TAG) {
998 // Parse next until start tag is found
999 }
1000
1001 String nodeName = parser.getName();
1002 if (!"dashboard-categories".equals(nodeName)) {
1003 throw new RuntimeException(
1004 "XML document must start with <preference-categories> tag; found"
1005 + nodeName + " at " + parser.getPositionDescription());
1006 }
1007
1008 Bundle curBundle = null;
1009
1010 final int outerDepth = parser.getDepth();
1011 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1012 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1013 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1014 continue;
1015 }
1016
1017 nodeName = parser.getName();
1018 if ("dashboard-category".equals(nodeName)) {
1019 DashboardCategory category = new DashboardCategory();
1020
1021 TypedArray sa = obtainStyledAttributes(
1022 attrs, com.android.internal.R.styleable.PreferenceHeader);
1023 category.id = sa.getResourceId(
1024 com.android.internal.R.styleable.PreferenceHeader_id,
1025 (int)DashboardCategory.CAT_ID_UNDEFINED);
1026
1027 TypedValue tv = sa.peekValue(
1028 com.android.internal.R.styleable.PreferenceHeader_title);
1029 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1030 if (tv.resourceId != 0) {
1031 category.titleRes = tv.resourceId;
1032 } else {
1033 category.title = tv.string;
1034 }
1035 }
1036 sa.recycle();
1037
1038 final int innerDepth = parser.getDepth();
1039 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1040 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1041 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1042 continue;
1043 }
1044
1045 String innerNodeName = parser.getName();
1046 if (innerNodeName.equals("dashboard-tile")) {
1047 DashboardTile tile = new DashboardTile();
1048
1049 sa = obtainStyledAttributes(
1050 attrs, com.android.internal.R.styleable.PreferenceHeader);
1051 tile.id = sa.getResourceId(
1052 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001053 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001054 tv = sa.peekValue(
1055 com.android.internal.R.styleable.PreferenceHeader_title);
1056 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1057 if (tv.resourceId != 0) {
1058 tile.titleRes = tv.resourceId;
1059 } else {
1060 tile.title = tv.string;
1061 }
1062 }
1063 tv = sa.peekValue(
1064 com.android.internal.R.styleable.PreferenceHeader_summary);
1065 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1066 if (tv.resourceId != 0) {
1067 tile.summaryRes = tv.resourceId;
1068 } else {
1069 tile.summary = tv.string;
1070 }
1071 }
1072 tile.iconRes = sa.getResourceId(
1073 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1074 tile.fragment = sa.getString(
1075 com.android.internal.R.styleable.PreferenceHeader_fragment);
1076 sa.recycle();
1077
1078 if (curBundle == null) {
1079 curBundle = new Bundle();
1080 }
1081
1082 final int innerDepth2 = parser.getDepth();
1083 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1084 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1085 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1086 continue;
1087 }
1088
1089 String innerNodeName2 = parser.getName();
1090 if (innerNodeName2.equals("extra")) {
1091 getResources().parseBundleExtra("extra", attrs, curBundle);
1092 XmlUtils.skipCurrentTag(parser);
1093
1094 } else if (innerNodeName2.equals("intent")) {
1095 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1096
1097 } else {
1098 XmlUtils.skipCurrentTag(parser);
1099 }
1100 }
1101
1102 if (curBundle.size() > 0) {
1103 tile.fragmentArguments = curBundle;
1104 curBundle = null;
1105 }
1106
PauloftheWest38155612014-06-30 10:02:36 -07001107 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001108 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001109 category.addTile(tile);
1110 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001111
1112 } else {
1113 XmlUtils.skipCurrentTag(parser);
1114 }
1115 }
1116
1117 target.add(category);
1118 } else {
1119 XmlUtils.skipCurrentTag(parser);
1120 }
1121 }
1122
1123 } catch (XmlPullParserException e) {
1124 throw new RuntimeException("Error parsing categories", e);
1125 } catch (IOException e) {
1126 throw new RuntimeException("Error parsing categories", e);
1127 } finally {
1128 if (parser != null) parser.close();
1129 }
1130 }
1131
1132 private void updateTilesList(List<DashboardCategory> target) {
1133 final boolean showDev = mDevelopmentPreferences.getBoolean(
1134 DevelopmentSettings.PREF_SHOW,
1135 android.os.Build.TYPE.equals("eng"));
1136
1137 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1138
1139 final int size = target.size();
1140 for (int i = 0; i < size; i++) {
1141
1142 DashboardCategory category = target.get(i);
1143
1144 // Ids are integers, so downcasting is ok
1145 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001146 int n = category.getTilesCount() - 1;
1147 while (n >= 0) {
1148
1149 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001150 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001151 id = (int) tile.id;
1152 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001153 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1154 removeTile = true;
1155 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001156 } else if (id == R.id.wifi_settings) {
1157 // Remove WiFi Settings if WiFi service is not available.
1158 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001159 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001160 }
1161 } else if (id == R.id.bluetooth_settings) {
1162 // Remove Bluetooth Settings if Bluetooth service is not available.
1163 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001164 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001165 }
1166 } else if (id == R.id.data_usage_settings) {
1167 // Remove data usage when kernel module not enabled
1168 final INetworkManagementService netManager = INetworkManagementService.Stub
1169 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1170 try {
1171 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001172 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001173 }
1174 } catch (RemoteException e) {
1175 // ignored
1176 }
1177 } else if (id == R.id.battery_settings) {
1178 // Remove battery settings when battery is not available. (e.g. TV)
1179
1180 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001181 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001182 }
1183 } else if (id == R.id.home_settings) {
1184 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001185 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001186 }
1187 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001188 boolean hasMultipleUsers =
1189 ((UserManager) getSystemService(Context.USER_SERVICE))
1190 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001191 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001192 || (!UserManager.supportsMultipleUsers()
1193 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001194 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001195 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001196 }
1197 } else if (id == R.id.nfc_payment_settings) {
1198 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001199 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001200 } else {
1201 // Only show if NFC is on and we have the HCE feature
1202 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001203 if (adapter == null || !adapter.isEnabled() ||
1204 !getPackageManager().hasSystemFeature(
1205 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001206 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001207 }
1208 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001209 } else if (id == R.id.print_settings) {
1210 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1211 PackageManager.FEATURE_PRINTING);
1212 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001213 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001214 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001215 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001216 if (!showDev || um.hasUserRestriction(
1217 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001218 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001219 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001220 }
1221
1222 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001223 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1224 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001225 }
1226
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001227 if (removeTile && n < category.getTilesCount()) {
1228 category.removeTile(n);
1229 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001230 n--;
1231 }
1232 }
1233 }
1234
1235 private boolean updateHomeSettingTiles(DashboardTile tile) {
1236 // Once we decide to show Home settings, keep showing it forever
1237 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1238 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1239 return true;
1240 }
1241
1242 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001243 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001244 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001245 // When there's only one available home app, omit this settings
1246 // category entirely at the top level UI. If the user just
1247 // uninstalled the penultimate home app candidiate, we also
1248 // now tell them about why they aren't seeing 'Home' in the list.
1249 if (sShowNoHomeNotice) {
1250 sShowNoHomeNotice = false;
1251 NoHomeDialogFragment.show(this);
1252 }
1253 return false;
1254 } else {
1255 // Okay, we're allowing the Home settings category. Tell it, when
1256 // invoked via this front door, that we'll need to be told about the
1257 // case when the user uninstalls all but one home app.
1258 if (tile.fragmentArguments == null) {
1259 tile.fragmentArguments = new Bundle();
1260 }
1261 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1262 }
1263 } catch (Exception e) {
1264 // Can't look up the home activity; bail on configuring the icon
1265 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1266 }
1267
1268 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1269 return true;
1270 }
1271
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001272 private void getMetaData() {
1273 try {
1274 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1275 PackageManager.GET_META_DATA);
1276 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001277 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1278 } catch (NameNotFoundException nnfe) {
1279 // No recovery
1280 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1281 }
1282 }
1283
1284 // give subclasses access to the Next button
1285 public boolean hasNextButton() {
1286 return mNextButton != null;
1287 }
1288
1289 public Button getNextButton() {
1290 return mNextButton;
1291 }
1292
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001293 @Override
1294 public boolean shouldUpRecreateTask(Intent targetIntent) {
1295 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1296 }
1297
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001298 public static void requestHomeNotice() {
1299 sShowNoHomeNotice = true;
1300 }
1301
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001302 @Override
1303 public boolean onQueryTextSubmit(String query) {
1304 switchToSearchResultsFragmentIfNeeded();
1305 mSearchQuery = query;
1306 return mSearchResultsFragment.onQueryTextSubmit(query);
1307 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001308
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001309 @Override
1310 public boolean onQueryTextChange(String newText) {
1311 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001312 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001313 return false;
1314 }
1315 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001316 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001317
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001318 @Override
1319 public boolean onClose() {
1320 return false;
1321 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001322
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001323 @Override
1324 public boolean onMenuItemActionExpand(MenuItem item) {
1325 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001326 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001327 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001328 return true;
1329 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001330
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001331 @Override
1332 public boolean onMenuItemActionCollapse(MenuItem item) {
1333 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001334 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001335 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001336 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001337 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001338 return true;
1339 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001340
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001341 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001342 if (mSearchResultsFragment != null) {
1343 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001344 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001345 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001346 if (current != null && current instanceof SearchResultsSummary) {
1347 mSearchResultsFragment = (SearchResultsSummary) current;
1348 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001349 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001350 SearchResultsSummary.class.getName(), null, false, true,
1351 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001352 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001353 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001354 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001355 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001356
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001357 public void needToRevertToInitialFragment() {
1358 mNeedToRevertToInitialFragment = true;
1359 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001360
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001361 private void revertToInitialFragment() {
1362 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001363 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001364 mSearchMenuItemExpanded = false;
1365 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1366 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001367 if (mSearchMenuItem != null) {
1368 mSearchMenuItem.collapseActionView();
1369 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001370 }
Jim Miller0698a212014-10-16 19:49:07 -07001371
1372 public Intent getResultIntentData() {
1373 return mResultIntentData;
1374 }
1375
1376 public void setResultIntentData(Intent resultIntentData) {
1377 mResultIntentData = resultIntentData;
1378 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001379}