blob: 415de5b2978aed8e84d92740200f1e3aa2a1e9c8 [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 Meglio7a6bfd12014-04-14 19:49:18 -070086import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070087import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
89import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
90import com.android.settings.inputmethod.SpellCheckersSettings;
91import com.android.settings.inputmethod.UserDictionaryList;
92import com.android.settings.location.LocationSettings;
93import com.android.settings.nfc.AndroidBeam;
94import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040095import com.android.settings.notification.AppNotificationSettings;
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;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700106import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800107import com.android.settings.vpn2.VpnSettings;
108import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700109import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700111import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112import com.android.settings.wifi.WifiSettings;
113import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400114
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import org.xmlpull.v1.XmlPullParser;
116import org.xmlpull.v1.XmlPullParserException;
117
118import java.io.IOException;
119import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700121import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700123import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700124
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800125public class SettingsActivity extends Activity
126 implements PreferenceManager.OnPreferenceTreeClickListener,
127 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100128 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700129 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100130 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800131
132 private static final String LOG_TAG = "Settings";
133
134 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700135 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
137 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700138 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700139 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700140 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800141
142 /**
143 * When starting this activity, the invoking Intent can contain this extra
144 * string to specify which fragment should be initially displayed.
145 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
146 * will call isValidFragment() to confirm that the fragment class name is valid for this
147 * activity.
148 */
149 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
150
151 /**
152 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
153 * this extra can also be specified to supply a Bundle of arguments to pass
154 * to that fragment when it is instantiated during the initial creation
155 * of the activity.
156 */
157 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
158
159 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700160 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
161 */
162 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
163
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800164 public static final String BACK_STACK_PREFS = ":settings:prefs";
165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 // extras that allow any preference activity to be launched as part of a wizard
167
168 // show Back and Next buttons? takes boolean parameter
169 // Back will then return RESULT_CANCELED and Next RESULT_OK
170 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
171
172 // add a Skip button?
173 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
174
175 // specify custom text for the Back or Next buttons, or cause a button to not appear
176 // at all by setting it to null
177 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
178 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
179
180 /**
181 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700182 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800183 * that fragment.
184 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700185 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100186 /**
187 * The package name used to resolve the title resource id.
188 */
189 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
190 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700191 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
192 ":settings:show_fragment_title_resid";
193 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
194 ":settings:show_fragment_as_shortcut";
195
196 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
197 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800199 private static final String META_DATA_KEY_FRAGMENT_CLASS =
200 "com.android.settings.FRAGMENT_CLASS";
201
202 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
203
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700204 private static final String EMPTY_QUERY = "";
205
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206 private static boolean sShowNoHomeNotice = false;
207
208 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800210 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700211 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800212
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800213 // Show only these settings for restricted users
214 private int[] SETTINGS_FOR_RESTRICTED = {
215 R.id.wireless_section,
216 R.id.wifi_settings,
217 R.id.bluetooth_settings,
218 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700219 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800220 R.id.wireless_settings,
221 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400222 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800223 R.id.display_settings,
224 R.id.storage_settings,
225 R.id.application_settings,
226 R.id.battery_settings,
227 R.id.personal_section,
228 R.id.location_settings,
229 R.id.security_settings,
230 R.id.language_settings,
231 R.id.user_settings,
232 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800233 R.id.system_section,
234 R.id.date_time_settings,
235 R.id.about_settings,
236 R.id.accessibility_settings,
237 R.id.print_settings,
238 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800239 R.id.home_settings,
240 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800241 };
242
243 private static final String[] ENTRY_FRAGMENTS = {
244 WirelessSettings.class.getName(),
245 WifiSettings.class.getName(),
246 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700247 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700249 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 TetherSettings.class.getName(),
251 WifiP2pSettings.class.getName(),
252 VpnSettings.class.getName(),
253 DateTimeSettings.class.getName(),
254 LocalePicker.class.getName(),
255 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700256 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 SpellCheckersSettings.class.getName(),
258 UserDictionaryList.class.getName(),
259 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700260 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 DisplaySettings.class.getName(),
262 DeviceInfoSettings.class.getName(),
263 ManageApplications.class.getName(),
264 ProcessStatsUi.class.getName(),
265 NotificationStation.class.getName(),
266 LocationSettings.class.getName(),
267 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700268 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 PrivacySettings.class.getName(),
270 DeviceAdminSettings.class.getName(),
271 AccessibilitySettings.class.getName(),
272 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
274 TextToSpeechSettings.class.getName(),
275 Memory.class.getName(),
276 DevelopmentSettings.class.getName(),
277 UsbSettings.class.getName(),
278 AndroidBeam.class.getName(),
279 WifiDisplaySettings.class.getName(),
280 PowerUsageSummary.class.getName(),
281 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000282 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800283 CryptKeeperSettings.class.getName(),
284 DataUsageSummary.class.getName(),
285 DreamSettings.class.getName(),
286 UserSettings.class.getName(),
287 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400288 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 PrintSettingsFragment.class.getName(),
290 PrintJobSettingsFragment.class.getName(),
291 TrustedCredentialsSettings.class.getName(),
292 PaymentSettings.class.getName(),
293 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700294 ZenModeSettings.class.getName(),
295 NotificationSettings.class.getName(),
296 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
297 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400298 InstalledAppDetails.class.getName(),
299 BatterySaverSettings.class.getName(),
John Spurlock802ddf92014-07-18 11:51:13 -0400300 NotificationAppList.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700301 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700302 OtherSoundSettings.class.getName(),
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700303 ApnSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 };
305
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700306
307 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
308 "android.settings.APPLICATION_DETAILS_SETTINGS"
309 };
310
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 private SharedPreferences mDevelopmentPreferences;
312 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
313
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 private boolean mBatteryPresent = true;
315 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 @Override
317 public void onReceive(Context context, Intent intent) {
318 String action = intent.getAction();
319 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
320 boolean batteryPresent = Utils.isBatteryPresent(intent);
321
322 if (mBatteryPresent != batteryPresent) {
323 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700324 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 }
326 }
327 }
328 };
329
Svetoslav990159a2014-04-14 17:14:59 -0700330 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
331 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700332
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700333 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700334 private SwitchBar mSwitchBar;
335
336 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700337
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700338 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700339 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700340
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700341 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700342 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700343
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700344 private ViewGroup mContent;
345
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700346 private SearchView mSearchView;
347 private MenuItem mSearchMenuItem;
348 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700349 private SearchResultsSummary mSearchResultsFragment;
350 private String mSearchQuery;
351
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700352 // Categories
353 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700355 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700356 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 private Handler mHandler = new Handler() {
358 @Override
359 public void handleMessage(Message msg) {
360 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700361 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700362 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
363 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700364 buildDashboardCategories(mCategories);
365 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 } break;
367 }
368 }
369 };
370
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700371 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700372 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700373
Jim Miller0698a212014-10-16 19:49:07 -0700374 private Intent mResultIntentData;
375
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700376 public SwitchBar getSwitchBar() {
377 return mSwitchBar;
378 }
379
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700380 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
381 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700382 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700383 }
384 return mCategories;
385 }
386
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800387 @Override
388 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
389 // Override the fragment title for Wallpaper settings
390 int titleRes = pref.getTitleRes();
391 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
392 titleRes = R.string.wallpaper_settings_fragment_title;
393 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
394 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
395 if (UserManager.get(this).isLinkedUser()) {
396 titleRes = R.string.profile_info_settings_title;
397 } else {
398 titleRes = R.string.user_info_settings_title;
399 }
400 }
401 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
402 null, 0);
403 return true;
404 }
405
406 @Override
407 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
408 return false;
409 }
410
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700411 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700412 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700413 Message msg = new Message();
414 msg.what = MSG_BUILD_CATEGORIES;
415 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800416 }
417 }
418
419 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800420 public void onConfigurationChanged(Configuration newConfig) {
421 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800422 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800423 }
424
425 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700426 protected void onStart() {
427 super.onStart();
428
429 if (mNeedToRevertToInitialFragment) {
430 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800431 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800432 }
433
434 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700435 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700436 if (!mDisplaySearch) {
437 return false;
438 }
439
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700440 MenuInflater inflater = getMenuInflater();
441 inflater.inflate(R.menu.options_menu, menu);
442
443 // Cache the search query (can be overriden by the OnQueryTextListener)
444 final String query = mSearchQuery;
445
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700446 mSearchMenuItem = menu.findItem(R.id.search);
447 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700448
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700449 if (mSearchMenuItem == null || mSearchView == null) {
450 return false;
451 }
452
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700453 if (mSearchResultsFragment != null) {
454 mSearchResultsFragment.setSearchView(mSearchView);
455 }
456
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700457 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700458 mSearchView.setOnQueryTextListener(this);
459 mSearchView.setOnCloseListener(this);
460
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700461 if (mSearchMenuItemExpanded) {
462 mSearchMenuItem.expandActionView();
463 }
464 mSearchView.setQuery(query, true /* submit */);
465
466 return true;
467 }
468
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700469 private static boolean isShortCutIntent(final Intent intent) {
470 Set<String> categories = intent.getCategories();
471 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
472 }
473
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700474 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700475 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700476 if (action == null) {
477 return false;
478 }
479 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
480 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
481 }
482 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700483 }
484
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700485 @Override
486 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700487 super.onCreate(savedState);
488
489 // Should happen before any call to getIntent()
490 getMetaData();
491
492 final Intent intent = getIntent();
493 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
494 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800496
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800497 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
498 Context.MODE_PRIVATE);
499
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700500 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700501 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700502
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700503 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700504 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
505
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700506 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700507 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700508
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700509 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700510
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700511 // This is a "Sub Settings" when:
512 // - this is a real SubSettings
513 // - or :settings:show_fragment_as_subsetting is passed to the Intent
514 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
515 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
516
517 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
518 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700519 // Check also that we are not a Theme Dialog as we don't want to override them
520 final int themeResId = getThemeResId();
521 if (themeResId != R.style.Theme_DialogWhenLarge &&
522 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
523 setTheme(R.style.Theme_SubSettings);
524 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700525 }
526
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700527 setContentView(mIsShowingDashboard ?
528 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800529
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700530 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700531
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532 getFragmentManager().addOnBackStackChangedListener(this);
533
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700534 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700535 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700536 }
537
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700538 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700539 // We are restarting from a previous saved state; used that to initialize, instead
540 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700541 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
542 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800543
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700544 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800545
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700546 ArrayList<DashboardCategory> categories =
547 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
548 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700549 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700550 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700551 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700553
554 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700555 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700556 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
557 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700559 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700560 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
561 // only if it is a sub settings
562 if (mIsShortcut) {
563 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700564 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700565 } else if (isSubSettings) {
566 mDisplayHomeAsUpEnabled = true;
567 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700568 } else {
569 mDisplayHomeAsUpEnabled = false;
570 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700571 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700572 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700573
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700574 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700575 switchToFragment(initialFragmentName, initialArguments, true, false,
576 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000577 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700578 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700579 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700580 // Show Search affordance
581 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700582 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700583 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700584 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 }
586 }
587
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700588 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700589 if (mActionBar != null) {
590 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
591 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
592 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700593 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
594
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800596 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
597
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700598 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 if (buttonBar != null) {
600 buttonBar.setVisibility(View.VISIBLE);
601
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700602 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800603 backButton.setOnClickListener(new OnClickListener() {
604 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700605 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800606 finish();
607 }
608 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700609 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800610 skipButton.setOnClickListener(new OnClickListener() {
611 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700612 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800613 finish();
614 }
615 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700616 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800617 mNextButton.setOnClickListener(new OnClickListener() {
618 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700619 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800620 finish();
621 }
622 });
623
624 // set our various button parameters
625 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
626 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
627 if (TextUtils.isEmpty(buttonText)) {
628 mNextButton.setVisibility(View.GONE);
629 }
630 else {
631 mNextButton.setText(buttonText);
632 }
633 }
634 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
635 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
636 if (TextUtils.isEmpty(buttonText)) {
637 backButton.setVisibility(View.GONE);
638 }
639 else {
640 backButton.setText(buttonText);
641 }
642 }
643 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
644 skipButton.setVisibility(View.VISIBLE);
645 }
646 }
647 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700648
649 mHomeActivitiesCount = getHomeActivitiesCount();
650 }
651
652 private int getHomeActivitiesCount() {
653 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
654 getPackageManager().getHomeActivities(homeApps);
655 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800656 }
657
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700658 private void setTitleFromIntent(Intent intent) {
659 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
660 if (initialTitleResId > 0) {
661 mInitialTitle = null;
662 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100663
664 final String initialTitleResPackageName = intent.getStringExtra(
665 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
666 if (initialTitleResPackageName != null) {
667 try {
668 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
669 0 /* flags */, new UserHandle(UserHandle.myUserId()));
670 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
671 setTitle(mInitialTitle);
672 mInitialTitleResId = -1;
673 return;
674 } catch (NameNotFoundException e) {
675 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
676 }
677 } else {
678 setTitle(mInitialTitleResId);
679 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700680 } else {
681 mInitialTitleResId = -1;
682 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
683 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
684 setTitle(mInitialTitle);
685 }
686 }
687
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800688 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800689 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700690 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800691 }
692
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700693 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800694 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700695
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800696 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700697 if (mInitialTitleResId > 0) {
698 setTitle(mInitialTitleResId);
699 } else {
700 setTitle(mInitialTitle);
701 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700702 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800703 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700704
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800705 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
706 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700707
708 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800709 }
710
711 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
712 final CharSequence title;
713 final int titleRes = bse.getBreadCrumbTitleRes();
714 if (titleRes > 0) {
715 title = getText(titleRes);
716 } else {
717 title = bse.getBreadCrumbTitle();
718 }
719 if (title != null) {
720 setTitle(title);
721 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800722 }
723
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800724 @Override
725 protected void onSaveInstanceState(Bundle outState) {
726 super.onSaveInstanceState(outState);
727
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700728 if (mCategories.size() > 0) {
729 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800730 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700731
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700732 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700733 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700734
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700735 if (mDisplaySearch) {
736 // The option menus are created if the ActionBar is visible and they are also created
737 // asynchronously. If you launch Settings with an Intent action like
738 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
739 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
740 // menu item and search view are null.
741 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
742 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700743
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700744 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
745 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
746 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700747
748 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800749 }
750
751 @Override
752 public void onResume() {
753 super.onResume();
754
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700755 final int newHomeActivityCount = getHomeActivitiesCount();
756 if (newHomeActivityCount != mHomeActivitiesCount) {
757 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700758 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700759 }
760
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800761 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
762 @Override
763 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700764 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800765 }
766 };
767 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
768 mDevelopmentPreferencesListener);
769
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800770 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700771
Svetoslav990159a2014-04-14 17:14:59 -0700772 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700773
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700774 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700775 onQueryTextSubmit(mSearchQuery);
776 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800777 }
778
779 @Override
780 public void onPause() {
781 super.onPause();
782
783 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700784 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800785 }
786
787 @Override
788 public void onDestroy() {
789 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700790
791 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
792 mDevelopmentPreferencesListener);
793 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800794 }
795
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800796 protected boolean isValidFragment(String fragmentName) {
797 // Almost all fragments are wrapped in this,
798 // except for a few that have their own activities.
799 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
800 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
801 }
802 return false;
803 }
804
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800805 @Override
806 public Intent getIntent() {
807 Intent superIntent = super.getIntent();
808 String startingFragment = getStartingFragmentClass(superIntent);
809 // This is called from super.onCreate, isMultiPane() is not yet reliable
810 // Do not use onIsHidingHeaders either, which relies itself on this method
811 if (startingFragment != null) {
812 Intent modIntent = new Intent(superIntent);
813 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
814 Bundle args = superIntent.getExtras();
815 if (args != null) {
816 args = new Bundle(args);
817 } else {
818 args = new Bundle();
819 }
820 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100821 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800822 return modIntent;
823 }
824 return superIntent;
825 }
826
827 /**
828 * Checks if the component name in the intent is different from the Settings class and
829 * returns the class name to load as a fragment.
830 */
831 private String getStartingFragmentClass(Intent intent) {
832 if (mFragmentClass != null) return mFragmentClass;
833
834 String intentClass = intent.getComponent().getClassName();
835 if (intentClass.equals(getClass().getName())) return null;
836
837 if ("com.android.settings.ManageApplications".equals(intentClass)
838 || "com.android.settings.RunningServices".equals(intentClass)
839 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
840 // Old names of manage apps.
841 intentClass = com.android.settings.applications.ManageApplications.class.getName();
842 }
843
844 return intentClass;
845 }
846
847 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000848 * Start a new fragment containing a preference panel. If the preferences
849 * are being displayed in multi-pane mode, the given fragment class will
850 * be instantiated and placed in the appropriate pane. If running in
851 * single-pane mode, a new activity will be launched in which to show the
852 * fragment.
853 *
854 * @param fragmentClass Full name of the class implementing the fragment.
855 * @param args Any desired arguments to supply to the fragment.
856 * @param titleRes Optional resource identifier of the title of this
857 * fragment.
858 * @param titleText Optional text of the title of this fragment.
859 * @param resultTo Optional fragment that result data should be sent to.
860 * If non-null, resultTo.onActivityResult() will be called when this
861 * preference panel is done. The launched panel must use
862 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
863 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700864 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000865 */
866 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700867 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700868 String title = null;
869 if (titleRes < 0) {
870 if (titleText != null) {
871 title = titleText.toString();
872 } else {
873 // There not much we can do in that case
874 title = "";
875 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700876 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700877 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700878 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000879 }
880
881 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100882 * Start a new fragment in a new activity containing a preference panel for a given user. If the
883 * preferences are being displayed in multi-pane mode, the given fragment class will be
884 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
885 * activity will be launched in which to show the fragment.
886 *
887 * @param fragmentClass Full name of the class implementing the fragment.
888 * @param args Any desired arguments to supply to the fragment.
889 * @param titleRes Optional resource identifier of the title of this fragment.
890 * @param titleText Optional text of the title of this fragment.
891 * @param userHandle The user for which the panel has to be started.
892 */
893 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
894 CharSequence titleText, UserHandle userHandle) {
895 String title = null;
896 if (titleRes < 0) {
897 if (titleText != null) {
898 title = titleText.toString();
899 } else {
900 // There not much we can do in that case
901 title = "";
902 }
903 }
904 Utils.startWithFragmentAsUser(this, fragmentClass, args,
905 titleRes, title, mIsShortcut, userHandle);
906 }
907
908 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800909 * Called by a preference panel fragment to finish itself.
910 *
911 * @param caller The fragment that is asking to be finished.
912 * @param resultCode Optional result code to send back to the original
913 * launching fragment.
914 * @param resultData Optional result data to send back to the original
915 * launching fragment.
916 */
917 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
918 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700919 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800920 }
921
922 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000923 * Start a new fragment.
924 *
925 * @param fragment The fragment to start
926 * @param push If true, the current fragment will be pushed onto the back stack. If false,
927 * the current fragment will be replaced.
928 */
929 public void startPreferenceFragment(Fragment fragment, boolean push) {
930 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700931 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000932 if (push) {
933 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
934 transaction.addToBackStack(BACK_STACK_PREFS);
935 } else {
936 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
937 }
938 transaction.commitAllowingStateLoss();
939 }
940
941 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700942 * Switch to a specific Fragment with taking care of validation, Title and BackStack
943 */
944 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700945 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700946 if (validate && !isValidFragment(fragmentName)) {
947 throw new IllegalArgumentException("Invalid fragment for this activity: "
948 + fragmentName);
949 }
950 Fragment f = Fragment.instantiate(this, fragmentName, args);
951 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700952 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700953 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700954 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700955 }
956 if (addToBackStack) {
957 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
958 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700959 if (titleResId > 0) {
960 transaction.setBreadCrumbTitle(titleResId);
961 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700962 transaction.setBreadCrumbTitle(title);
963 }
964 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700965 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700966 return f;
967 }
968
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700969 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700970 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700971 *
972 * @param categories The list in which to place the tiles categories.
973 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700974 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700975 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700976 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
977 updateTilesList(categories);
978 }
979
980 /**
981 * Parse the given XML file as a categories description, adding each
982 * parsed categories and tiles into the target list.
983 *
984 * @param resid The XML resource to load and parse.
985 * @param target The list in which the parsed categories and tiles should be placed.
986 */
987 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
988 XmlResourceParser parser = null;
989 try {
990 parser = getResources().getXml(resid);
991 AttributeSet attrs = Xml.asAttributeSet(parser);
992
993 int type;
994 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
995 && type != XmlPullParser.START_TAG) {
996 // Parse next until start tag is found
997 }
998
999 String nodeName = parser.getName();
1000 if (!"dashboard-categories".equals(nodeName)) {
1001 throw new RuntimeException(
1002 "XML document must start with <preference-categories> tag; found"
1003 + nodeName + " at " + parser.getPositionDescription());
1004 }
1005
1006 Bundle curBundle = null;
1007
1008 final int outerDepth = parser.getDepth();
1009 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1010 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1011 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1012 continue;
1013 }
1014
1015 nodeName = parser.getName();
1016 if ("dashboard-category".equals(nodeName)) {
1017 DashboardCategory category = new DashboardCategory();
1018
1019 TypedArray sa = obtainStyledAttributes(
1020 attrs, com.android.internal.R.styleable.PreferenceHeader);
1021 category.id = sa.getResourceId(
1022 com.android.internal.R.styleable.PreferenceHeader_id,
1023 (int)DashboardCategory.CAT_ID_UNDEFINED);
1024
1025 TypedValue tv = sa.peekValue(
1026 com.android.internal.R.styleable.PreferenceHeader_title);
1027 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1028 if (tv.resourceId != 0) {
1029 category.titleRes = tv.resourceId;
1030 } else {
1031 category.title = tv.string;
1032 }
1033 }
1034 sa.recycle();
1035
1036 final int innerDepth = parser.getDepth();
1037 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1038 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1039 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1040 continue;
1041 }
1042
1043 String innerNodeName = parser.getName();
1044 if (innerNodeName.equals("dashboard-tile")) {
1045 DashboardTile tile = new DashboardTile();
1046
1047 sa = obtainStyledAttributes(
1048 attrs, com.android.internal.R.styleable.PreferenceHeader);
1049 tile.id = sa.getResourceId(
1050 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001051 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001052 tv = sa.peekValue(
1053 com.android.internal.R.styleable.PreferenceHeader_title);
1054 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1055 if (tv.resourceId != 0) {
1056 tile.titleRes = tv.resourceId;
1057 } else {
1058 tile.title = tv.string;
1059 }
1060 }
1061 tv = sa.peekValue(
1062 com.android.internal.R.styleable.PreferenceHeader_summary);
1063 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1064 if (tv.resourceId != 0) {
1065 tile.summaryRes = tv.resourceId;
1066 } else {
1067 tile.summary = tv.string;
1068 }
1069 }
1070 tile.iconRes = sa.getResourceId(
1071 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1072 tile.fragment = sa.getString(
1073 com.android.internal.R.styleable.PreferenceHeader_fragment);
1074 sa.recycle();
1075
1076 if (curBundle == null) {
1077 curBundle = new Bundle();
1078 }
1079
1080 final int innerDepth2 = parser.getDepth();
1081 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1082 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1083 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1084 continue;
1085 }
1086
1087 String innerNodeName2 = parser.getName();
1088 if (innerNodeName2.equals("extra")) {
1089 getResources().parseBundleExtra("extra", attrs, curBundle);
1090 XmlUtils.skipCurrentTag(parser);
1091
1092 } else if (innerNodeName2.equals("intent")) {
1093 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1094
1095 } else {
1096 XmlUtils.skipCurrentTag(parser);
1097 }
1098 }
1099
1100 if (curBundle.size() > 0) {
1101 tile.fragmentArguments = curBundle;
1102 curBundle = null;
1103 }
1104
PauloftheWest38155612014-06-30 10:02:36 -07001105 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001106 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001107 category.addTile(tile);
1108 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001109
1110 } else {
1111 XmlUtils.skipCurrentTag(parser);
1112 }
1113 }
1114
1115 target.add(category);
1116 } else {
1117 XmlUtils.skipCurrentTag(parser);
1118 }
1119 }
1120
1121 } catch (XmlPullParserException e) {
1122 throw new RuntimeException("Error parsing categories", e);
1123 } catch (IOException e) {
1124 throw new RuntimeException("Error parsing categories", e);
1125 } finally {
1126 if (parser != null) parser.close();
1127 }
1128 }
1129
1130 private void updateTilesList(List<DashboardCategory> target) {
1131 final boolean showDev = mDevelopmentPreferences.getBoolean(
1132 DevelopmentSettings.PREF_SHOW,
1133 android.os.Build.TYPE.equals("eng"));
1134
1135 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1136
1137 final int size = target.size();
1138 for (int i = 0; i < size; i++) {
1139
1140 DashboardCategory category = target.get(i);
1141
1142 // Ids are integers, so downcasting is ok
1143 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001144 int n = category.getTilesCount() - 1;
1145 while (n >= 0) {
1146
1147 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001148 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001149 id = (int) tile.id;
1150 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001151 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1152 removeTile = true;
1153 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001154 } else if (id == R.id.wifi_settings) {
1155 // Remove WiFi Settings if WiFi service is not available.
1156 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001157 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001158 }
1159 } else if (id == R.id.bluetooth_settings) {
1160 // Remove Bluetooth Settings if Bluetooth service is not available.
1161 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001162 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001163 }
1164 } else if (id == R.id.data_usage_settings) {
1165 // Remove data usage when kernel module not enabled
1166 final INetworkManagementService netManager = INetworkManagementService.Stub
1167 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1168 try {
1169 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001170 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001171 }
1172 } catch (RemoteException e) {
1173 // ignored
1174 }
1175 } else if (id == R.id.battery_settings) {
1176 // Remove battery settings when battery is not available. (e.g. TV)
1177
1178 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001179 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001180 }
1181 } else if (id == R.id.home_settings) {
1182 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001183 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001184 }
1185 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001186 boolean hasMultipleUsers =
1187 ((UserManager) getSystemService(Context.USER_SERVICE))
1188 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001189 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001190 || (!UserManager.supportsMultipleUsers()
1191 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001192 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001193 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001194 }
1195 } else if (id == R.id.nfc_payment_settings) {
1196 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001197 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001198 } else {
1199 // Only show if NFC is on and we have the HCE feature
1200 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001201 if (adapter == null || !adapter.isEnabled() ||
1202 !getPackageManager().hasSystemFeature(
1203 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001204 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001205 }
1206 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001207 } else if (id == R.id.print_settings) {
1208 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1209 PackageManager.FEATURE_PRINTING);
1210 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001211 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001212 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001213 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001214 if (!showDev || um.hasUserRestriction(
1215 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001216 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001217 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001218 }
1219
1220 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001221 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1222 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001223 }
1224
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001225 if (removeTile && n < category.getTilesCount()) {
1226 category.removeTile(n);
1227 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001228 n--;
1229 }
1230 }
1231 }
1232
1233 private boolean updateHomeSettingTiles(DashboardTile tile) {
1234 // Once we decide to show Home settings, keep showing it forever
1235 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1236 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1237 return true;
1238 }
1239
1240 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001241 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001242 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001243 // When there's only one available home app, omit this settings
1244 // category entirely at the top level UI. If the user just
1245 // uninstalled the penultimate home app candidiate, we also
1246 // now tell them about why they aren't seeing 'Home' in the list.
1247 if (sShowNoHomeNotice) {
1248 sShowNoHomeNotice = false;
1249 NoHomeDialogFragment.show(this);
1250 }
1251 return false;
1252 } else {
1253 // Okay, we're allowing the Home settings category. Tell it, when
1254 // invoked via this front door, that we'll need to be told about the
1255 // case when the user uninstalls all but one home app.
1256 if (tile.fragmentArguments == null) {
1257 tile.fragmentArguments = new Bundle();
1258 }
1259 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1260 }
1261 } catch (Exception e) {
1262 // Can't look up the home activity; bail on configuring the icon
1263 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1264 }
1265
1266 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1267 return true;
1268 }
1269
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001270 private void getMetaData() {
1271 try {
1272 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1273 PackageManager.GET_META_DATA);
1274 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001275 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1276 } catch (NameNotFoundException nnfe) {
1277 // No recovery
1278 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1279 }
1280 }
1281
1282 // give subclasses access to the Next button
1283 public boolean hasNextButton() {
1284 return mNextButton != null;
1285 }
1286
1287 public Button getNextButton() {
1288 return mNextButton;
1289 }
1290
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001291 @Override
1292 public boolean shouldUpRecreateTask(Intent targetIntent) {
1293 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1294 }
1295
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001296 public static void requestHomeNotice() {
1297 sShowNoHomeNotice = true;
1298 }
1299
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001300 @Override
1301 public boolean onQueryTextSubmit(String query) {
1302 switchToSearchResultsFragmentIfNeeded();
1303 mSearchQuery = query;
1304 return mSearchResultsFragment.onQueryTextSubmit(query);
1305 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001306
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001307 @Override
1308 public boolean onQueryTextChange(String newText) {
1309 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001310 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001311 return false;
1312 }
1313 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001314 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001315
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001316 @Override
1317 public boolean onClose() {
1318 return false;
1319 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001320
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001321 @Override
1322 public boolean onMenuItemActionExpand(MenuItem item) {
1323 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001324 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001325 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001326 return true;
1327 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001328
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001329 @Override
1330 public boolean onMenuItemActionCollapse(MenuItem item) {
1331 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001332 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001333 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001334 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001335 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001336 return true;
1337 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001338
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001339 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001340 if (mSearchResultsFragment != null) {
1341 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001342 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001343 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001344 if (current != null && current instanceof SearchResultsSummary) {
1345 mSearchResultsFragment = (SearchResultsSummary) current;
1346 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001347 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001348 SearchResultsSummary.class.getName(), null, false, true,
1349 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001350 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001351 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001352 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001353 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001354
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001355 public void needToRevertToInitialFragment() {
1356 mNeedToRevertToInitialFragment = true;
1357 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001358
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001359 private void revertToInitialFragment() {
1360 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001361 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001362 mSearchMenuItemExpanded = false;
1363 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1364 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001365 if (mSearchMenuItem != null) {
1366 mSearchMenuItem.collapseActionView();
1367 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001368 }
Jim Miller0698a212014-10-16 19:49:07 -07001369
1370 public Intent getResultIntentData() {
1371 return mResultIntentData;
1372 }
1373
1374 public void setResultIntentData(Intent resultIntentData) {
1375 mResultIntentData = resultIntentData;
1376 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001377}