blob: 0f0c89775a468314741d8292c499bc2be13dd3f3 [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;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070084import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070085import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070086import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
88import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
89import com.android.settings.inputmethod.SpellCheckersSettings;
90import com.android.settings.inputmethod.UserDictionaryList;
91import com.android.settings.location.LocationSettings;
92import com.android.settings.nfc.AndroidBeam;
93import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040094import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040095import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040096import com.android.settings.notification.NotificationAccessSettings;
97import com.android.settings.notification.NotificationSettings;
98import com.android.settings.notification.NotificationStation;
99import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800100import com.android.settings.print.PrintJobSettingsFragment;
101import com.android.settings.print.PrintSettingsFragment;
PauloftheWest38155612014-06-30 10:02:36 -0700102import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800103import com.android.settings.tts.TextToSpeechSettings;
104import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700105import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800106import com.android.settings.vpn2.VpnSettings;
107import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700108import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800109import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700110import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.WifiSettings;
112import com.android.settings.wifi.p2p.WifiP2pSettings;
113import org.xmlpull.v1.XmlPullParser;
114import org.xmlpull.v1.XmlPullParserException;
115
116import java.io.IOException;
117import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700119import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800120
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700121import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700122
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123public class SettingsActivity extends Activity
124 implements PreferenceManager.OnPreferenceTreeClickListener,
125 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100126 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700127 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100128 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800129
130 private static final String LOG_TAG = "Settings";
131
132 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700133 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700134 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
135 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700136 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700137 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700138 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800139
140 /**
141 * When starting this activity, the invoking Intent can contain this extra
142 * string to specify which fragment should be initially displayed.
143 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
144 * will call isValidFragment() to confirm that the fragment class name is valid for this
145 * activity.
146 */
147 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
148
149 /**
150 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
151 * this extra can also be specified to supply a Bundle of arguments to pass
152 * to that fragment when it is instantiated during the initial creation
153 * of the activity.
154 */
155 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
156
157 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700158 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
159 */
160 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
161
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800162 public static final String BACK_STACK_PREFS = ":settings:prefs";
163
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164 // extras that allow any preference activity to be launched as part of a wizard
165
166 // show Back and Next buttons? takes boolean parameter
167 // Back will then return RESULT_CANCELED and Next RESULT_OK
168 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
169
170 // add a Skip button?
171 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
172
173 // specify custom text for the Back or Next buttons, or cause a button to not appear
174 // at all by setting it to null
175 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
176 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
177
178 /**
179 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700180 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800181 * that fragment.
182 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700183 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100184 /**
185 * The package name used to resolve the title resource id.
186 */
187 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
188 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700189 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
190 ":settings:show_fragment_title_resid";
191 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
192 ":settings:show_fragment_as_shortcut";
193
194 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
195 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800196
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800197 private static final String META_DATA_KEY_FRAGMENT_CLASS =
198 "com.android.settings.FRAGMENT_CLASS";
199
200 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
201
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700202 private static final String EMPTY_QUERY = "";
203
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204 private static boolean sShowNoHomeNotice = false;
205
206 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800207
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800208 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700209 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 // Show only these settings for restricted users
212 private int[] SETTINGS_FOR_RESTRICTED = {
213 R.id.wireless_section,
214 R.id.wifi_settings,
215 R.id.bluetooth_settings,
216 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700217 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800218 R.id.wireless_settings,
219 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400220 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800221 R.id.display_settings,
222 R.id.storage_settings,
223 R.id.application_settings,
224 R.id.battery_settings,
225 R.id.personal_section,
226 R.id.location_settings,
227 R.id.security_settings,
228 R.id.language_settings,
229 R.id.user_settings,
230 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800231 R.id.system_section,
232 R.id.date_time_settings,
233 R.id.about_settings,
234 R.id.accessibility_settings,
235 R.id.print_settings,
236 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800237 R.id.home_settings,
238 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800239 };
240
241 private static final String[] ENTRY_FRAGMENTS = {
242 WirelessSettings.class.getName(),
243 WifiSettings.class.getName(),
244 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700245 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700247 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248 TetherSettings.class.getName(),
249 WifiP2pSettings.class.getName(),
250 VpnSettings.class.getName(),
251 DateTimeSettings.class.getName(),
252 LocalePicker.class.getName(),
253 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700254 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800255 SpellCheckersSettings.class.getName(),
256 UserDictionaryList.class.getName(),
257 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700258 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 DisplaySettings.class.getName(),
260 DeviceInfoSettings.class.getName(),
261 ManageApplications.class.getName(),
262 ProcessStatsUi.class.getName(),
263 NotificationStation.class.getName(),
264 LocationSettings.class.getName(),
265 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700266 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800267 PrivacySettings.class.getName(),
268 DeviceAdminSettings.class.getName(),
269 AccessibilitySettings.class.getName(),
270 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
272 TextToSpeechSettings.class.getName(),
273 Memory.class.getName(),
274 DevelopmentSettings.class.getName(),
275 UsbSettings.class.getName(),
276 AndroidBeam.class.getName(),
277 WifiDisplaySettings.class.getName(),
278 PowerUsageSummary.class.getName(),
279 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000280 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800281 CryptKeeperSettings.class.getName(),
282 DataUsageSummary.class.getName(),
283 DreamSettings.class.getName(),
284 UserSettings.class.getName(),
285 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400286 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 PrintSettingsFragment.class.getName(),
288 PrintJobSettingsFragment.class.getName(),
289 TrustedCredentialsSettings.class.getName(),
290 PaymentSettings.class.getName(),
291 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700292 ZenModeSettings.class.getName(),
293 NotificationSettings.class.getName(),
294 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
295 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400296 InstalledAppDetails.class.getName(),
297 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700298 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700299 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000300 ApnSettings.class.getName(),
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800301 WifiCallingSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 };
303
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700304
305 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
306 "android.settings.APPLICATION_DETAILS_SETTINGS"
307 };
308
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 private SharedPreferences mDevelopmentPreferences;
310 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
311
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 private boolean mBatteryPresent = true;
313 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 @Override
315 public void onReceive(Context context, Intent intent) {
316 String action = intent.getAction();
317 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
318 boolean batteryPresent = Utils.isBatteryPresent(intent);
319
320 if (mBatteryPresent != batteryPresent) {
321 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700322 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 }
324 }
325 }
326 };
327
Svetoslav990159a2014-04-14 17:14:59 -0700328 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
329 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700330
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700331 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700332 private SwitchBar mSwitchBar;
333
334 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700335
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700336 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700337 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700338
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700339 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700340 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700341
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700342 private ViewGroup mContent;
343
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700344 private SearchView mSearchView;
345 private MenuItem mSearchMenuItem;
346 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700347 private SearchResultsSummary mSearchResultsFragment;
348 private String mSearchQuery;
349
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700350 // Categories
351 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700353 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700354 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 private Handler mHandler = new Handler() {
356 @Override
357 public void handleMessage(Message msg) {
358 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700359 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700360 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
361 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700362 buildDashboardCategories(mCategories);
363 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800364 } break;
365 }
366 }
367 };
368
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700369 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700370 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700371
Jim Miller0698a212014-10-16 19:49:07 -0700372 private Intent mResultIntentData;
373
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700374 public SwitchBar getSwitchBar() {
375 return mSwitchBar;
376 }
377
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700378 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
379 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700380 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700381 }
382 return mCategories;
383 }
384
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800385 @Override
386 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
387 // Override the fragment title for Wallpaper settings
388 int titleRes = pref.getTitleRes();
389 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
390 titleRes = R.string.wallpaper_settings_fragment_title;
391 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
392 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
393 if (UserManager.get(this).isLinkedUser()) {
394 titleRes = R.string.profile_info_settings_title;
395 } else {
396 titleRes = R.string.user_info_settings_title;
397 }
398 }
399 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
400 null, 0);
401 return true;
402 }
403
404 @Override
405 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
406 return false;
407 }
408
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700409 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700410 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700411 Message msg = new Message();
412 msg.what = MSG_BUILD_CATEGORIES;
413 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800414 }
415 }
416
417 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800418 public void onConfigurationChanged(Configuration newConfig) {
419 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800420 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800421 }
422
423 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700424 protected void onStart() {
425 super.onStart();
426
427 if (mNeedToRevertToInitialFragment) {
428 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800429 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800430 }
431
432 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700433 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700434 if (!mDisplaySearch) {
435 return false;
436 }
437
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700438 MenuInflater inflater = getMenuInflater();
439 inflater.inflate(R.menu.options_menu, menu);
440
441 // Cache the search query (can be overriden by the OnQueryTextListener)
442 final String query = mSearchQuery;
443
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700444 mSearchMenuItem = menu.findItem(R.id.search);
445 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700446
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700447 if (mSearchMenuItem == null || mSearchView == null) {
448 return false;
449 }
450
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700451 if (mSearchResultsFragment != null) {
452 mSearchResultsFragment.setSearchView(mSearchView);
453 }
454
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700455 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700456 mSearchView.setOnQueryTextListener(this);
457 mSearchView.setOnCloseListener(this);
458
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700459 if (mSearchMenuItemExpanded) {
460 mSearchMenuItem.expandActionView();
461 }
462 mSearchView.setQuery(query, true /* submit */);
463
464 return true;
465 }
466
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700467 private static boolean isShortCutIntent(final Intent intent) {
468 Set<String> categories = intent.getCategories();
469 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
470 }
471
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700472 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700473 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700474 if (action == null) {
475 return false;
476 }
477 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
478 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
479 }
480 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700481 }
482
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700483 @Override
484 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700485 super.onCreate(savedState);
486
487 // Should happen before any call to getIntent()
488 getMetaData();
489
490 final Intent intent = getIntent();
491 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
492 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800493 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800494
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800495 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
496 Context.MODE_PRIVATE);
497
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700498 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700499 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700500
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700501 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700502 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
503
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700504 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700505 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700506
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700507 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700508
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700509 // This is a "Sub Settings" when:
510 // - this is a real SubSettings
511 // - or :settings:show_fragment_as_subsetting is passed to the Intent
512 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
513 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
514
515 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
516 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700517 // Check also that we are not a Theme Dialog as we don't want to override them
518 final int themeResId = getThemeResId();
519 if (themeResId != R.style.Theme_DialogWhenLarge &&
520 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
521 setTheme(R.style.Theme_SubSettings);
522 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700523 }
524
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700525 setContentView(mIsShowingDashboard ?
526 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700528 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700529
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530 getFragmentManager().addOnBackStackChangedListener(this);
531
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700532 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800533 // Run the Index update only if we have some space
534 if (!Utils.isLowStorage(this)) {
535 Index.getInstance(getApplicationContext()).update();
536 } else {
537 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
538 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700539 }
540
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700541 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700542 // We are restarting from a previous saved state; used that to initialize, instead
543 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700544 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
545 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700547 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800548
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700549 ArrayList<DashboardCategory> categories =
550 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
551 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700552 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700553 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700554 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800555 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700556
557 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700558 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700559 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
560 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800561 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700562 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700563 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
564 // only if it is a sub settings
565 if (mIsShortcut) {
566 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700567 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700568 } else if (isSubSettings) {
569 mDisplayHomeAsUpEnabled = true;
570 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700571 } else {
572 mDisplayHomeAsUpEnabled = false;
573 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700574 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700575 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700576
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700577 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700578 switchToFragment(initialFragmentName, initialArguments, true, false,
579 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000580 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700581 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700582 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700583 // Show Search affordance
584 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700585 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700586 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700587 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 }
589 }
590
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700591 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700592 if (mActionBar != null) {
593 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
594 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
595 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700596 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
597
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800598 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
600
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700601 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800602 if (buttonBar != null) {
603 buttonBar.setVisibility(View.VISIBLE);
604
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700605 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800606 backButton.setOnClickListener(new OnClickListener() {
607 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700608 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800609 finish();
610 }
611 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700612 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800613 skipButton.setOnClickListener(new OnClickListener() {
614 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700615 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800616 finish();
617 }
618 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700619 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800620 mNextButton.setOnClickListener(new OnClickListener() {
621 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700622 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800623 finish();
624 }
625 });
626
627 // set our various button parameters
628 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
629 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
630 if (TextUtils.isEmpty(buttonText)) {
631 mNextButton.setVisibility(View.GONE);
632 }
633 else {
634 mNextButton.setText(buttonText);
635 }
636 }
637 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
638 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
639 if (TextUtils.isEmpty(buttonText)) {
640 backButton.setVisibility(View.GONE);
641 }
642 else {
643 backButton.setText(buttonText);
644 }
645 }
646 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
647 skipButton.setVisibility(View.VISIBLE);
648 }
649 }
650 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700651
652 mHomeActivitiesCount = getHomeActivitiesCount();
653 }
654
655 private int getHomeActivitiesCount() {
656 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
657 getPackageManager().getHomeActivities(homeApps);
658 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800659 }
660
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700661 private void setTitleFromIntent(Intent intent) {
662 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
663 if (initialTitleResId > 0) {
664 mInitialTitle = null;
665 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100666
667 final String initialTitleResPackageName = intent.getStringExtra(
668 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
669 if (initialTitleResPackageName != null) {
670 try {
671 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
672 0 /* flags */, new UserHandle(UserHandle.myUserId()));
673 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
674 setTitle(mInitialTitle);
675 mInitialTitleResId = -1;
676 return;
677 } catch (NameNotFoundException e) {
678 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
679 }
680 } else {
681 setTitle(mInitialTitleResId);
682 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700683 } else {
684 mInitialTitleResId = -1;
685 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
686 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
687 setTitle(mInitialTitle);
688 }
689 }
690
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800691 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800692 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700693 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800694 }
695
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700696 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800697 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700698
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800699 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700700 if (mInitialTitleResId > 0) {
701 setTitle(mInitialTitleResId);
702 } else {
703 setTitle(mInitialTitle);
704 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700705 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800706 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700707
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800708 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
709 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700710
711 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800712 }
713
714 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
715 final CharSequence title;
716 final int titleRes = bse.getBreadCrumbTitleRes();
717 if (titleRes > 0) {
718 title = getText(titleRes);
719 } else {
720 title = bse.getBreadCrumbTitle();
721 }
722 if (title != null) {
723 setTitle(title);
724 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800725 }
726
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800727 @Override
728 protected void onSaveInstanceState(Bundle outState) {
729 super.onSaveInstanceState(outState);
730
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700731 if (mCategories.size() > 0) {
732 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700734
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700735 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700736 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700737
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700738 if (mDisplaySearch) {
739 // The option menus are created if the ActionBar is visible and they are also created
740 // asynchronously. If you launch Settings with an Intent action like
741 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
742 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
743 // menu item and search view are null.
744 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
745 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700746
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700747 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
748 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
749 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700750
751 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800752 }
753
754 @Override
755 public void onResume() {
756 super.onResume();
757
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700758 final int newHomeActivityCount = getHomeActivitiesCount();
759 if (newHomeActivityCount != mHomeActivitiesCount) {
760 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700761 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700762 }
763
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800764 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
765 @Override
766 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700767 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800768 }
769 };
770 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
771 mDevelopmentPreferencesListener);
772
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800773 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700774
Svetoslav990159a2014-04-14 17:14:59 -0700775 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700776
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700777 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700778 onQueryTextSubmit(mSearchQuery);
779 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800780 }
781
782 @Override
783 public void onPause() {
784 super.onPause();
785
786 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700787 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800788 }
789
790 @Override
791 public void onDestroy() {
792 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700793
794 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
795 mDevelopmentPreferencesListener);
796 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800797 }
798
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800799 protected boolean isValidFragment(String fragmentName) {
800 // Almost all fragments are wrapped in this,
801 // except for a few that have their own activities.
802 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
803 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
804 }
805 return false;
806 }
807
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800808 @Override
809 public Intent getIntent() {
810 Intent superIntent = super.getIntent();
811 String startingFragment = getStartingFragmentClass(superIntent);
812 // This is called from super.onCreate, isMultiPane() is not yet reliable
813 // Do not use onIsHidingHeaders either, which relies itself on this method
814 if (startingFragment != null) {
815 Intent modIntent = new Intent(superIntent);
816 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
817 Bundle args = superIntent.getExtras();
818 if (args != null) {
819 args = new Bundle(args);
820 } else {
821 args = new Bundle();
822 }
823 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100824 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800825 return modIntent;
826 }
827 return superIntent;
828 }
829
830 /**
831 * Checks if the component name in the intent is different from the Settings class and
832 * returns the class name to load as a fragment.
833 */
834 private String getStartingFragmentClass(Intent intent) {
835 if (mFragmentClass != null) return mFragmentClass;
836
837 String intentClass = intent.getComponent().getClassName();
838 if (intentClass.equals(getClass().getName())) return null;
839
840 if ("com.android.settings.ManageApplications".equals(intentClass)
841 || "com.android.settings.RunningServices".equals(intentClass)
842 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
843 // Old names of manage apps.
844 intentClass = com.android.settings.applications.ManageApplications.class.getName();
845 }
846
847 return intentClass;
848 }
849
850 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000851 * Start a new fragment containing a preference panel. If the preferences
852 * are being displayed in multi-pane mode, the given fragment class will
853 * be instantiated and placed in the appropriate pane. If running in
854 * single-pane mode, a new activity will be launched in which to show the
855 * fragment.
856 *
857 * @param fragmentClass Full name of the class implementing the fragment.
858 * @param args Any desired arguments to supply to the fragment.
859 * @param titleRes Optional resource identifier of the title of this
860 * fragment.
861 * @param titleText Optional text of the title of this fragment.
862 * @param resultTo Optional fragment that result data should be sent to.
863 * If non-null, resultTo.onActivityResult() will be called when this
864 * preference panel is done. The launched panel must use
865 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
866 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700867 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000868 */
869 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700870 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700871 String title = null;
872 if (titleRes < 0) {
873 if (titleText != null) {
874 title = titleText.toString();
875 } else {
876 // There not much we can do in that case
877 title = "";
878 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700879 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700880 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700881 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000882 }
883
884 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100885 * Start a new fragment in a new activity containing a preference panel for a given user. If the
886 * preferences are being displayed in multi-pane mode, the given fragment class will be
887 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
888 * activity will be launched in which to show the fragment.
889 *
890 * @param fragmentClass Full name of the class implementing the fragment.
891 * @param args Any desired arguments to supply to the fragment.
892 * @param titleRes Optional resource identifier of the title of this fragment.
893 * @param titleText Optional text of the title of this fragment.
894 * @param userHandle The user for which the panel has to be started.
895 */
896 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
897 CharSequence titleText, UserHandle userHandle) {
898 String title = null;
899 if (titleRes < 0) {
900 if (titleText != null) {
901 title = titleText.toString();
902 } else {
903 // There not much we can do in that case
904 title = "";
905 }
906 }
907 Utils.startWithFragmentAsUser(this, fragmentClass, args,
908 titleRes, title, mIsShortcut, userHandle);
909 }
910
911 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800912 * Called by a preference panel fragment to finish itself.
913 *
914 * @param caller The fragment that is asking to be finished.
915 * @param resultCode Optional result code to send back to the original
916 * launching fragment.
917 * @param resultData Optional result data to send back to the original
918 * launching fragment.
919 */
920 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
921 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700922 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800923 }
924
925 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000926 * Start a new fragment.
927 *
928 * @param fragment The fragment to start
929 * @param push If true, the current fragment will be pushed onto the back stack. If false,
930 * the current fragment will be replaced.
931 */
932 public void startPreferenceFragment(Fragment fragment, boolean push) {
933 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700934 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000935 if (push) {
936 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
937 transaction.addToBackStack(BACK_STACK_PREFS);
938 } else {
939 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
940 }
941 transaction.commitAllowingStateLoss();
942 }
943
944 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700945 * Switch to a specific Fragment with taking care of validation, Title and BackStack
946 */
947 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700948 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700949 if (validate && !isValidFragment(fragmentName)) {
950 throw new IllegalArgumentException("Invalid fragment for this activity: "
951 + fragmentName);
952 }
953 Fragment f = Fragment.instantiate(this, fragmentName, args);
954 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700955 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700956 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700957 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700958 }
959 if (addToBackStack) {
960 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
961 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700962 if (titleResId > 0) {
963 transaction.setBreadCrumbTitle(titleResId);
964 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700965 transaction.setBreadCrumbTitle(title);
966 }
967 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700968 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700969 return f;
970 }
971
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700972 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700973 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700974 *
975 * @param categories The list in which to place the tiles categories.
976 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700977 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700978 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700979 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
980 updateTilesList(categories);
981 }
982
983 /**
984 * Parse the given XML file as a categories description, adding each
985 * parsed categories and tiles into the target list.
986 *
987 * @param resid The XML resource to load and parse.
988 * @param target The list in which the parsed categories and tiles should be placed.
989 */
990 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
991 XmlResourceParser parser = null;
992 try {
993 parser = getResources().getXml(resid);
994 AttributeSet attrs = Xml.asAttributeSet(parser);
995
996 int type;
997 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
998 && type != XmlPullParser.START_TAG) {
999 // Parse next until start tag is found
1000 }
1001
1002 String nodeName = parser.getName();
1003 if (!"dashboard-categories".equals(nodeName)) {
1004 throw new RuntimeException(
1005 "XML document must start with <preference-categories> tag; found"
1006 + nodeName + " at " + parser.getPositionDescription());
1007 }
1008
1009 Bundle curBundle = null;
1010
1011 final int outerDepth = parser.getDepth();
1012 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1013 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1014 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1015 continue;
1016 }
1017
1018 nodeName = parser.getName();
1019 if ("dashboard-category".equals(nodeName)) {
1020 DashboardCategory category = new DashboardCategory();
1021
1022 TypedArray sa = obtainStyledAttributes(
1023 attrs, com.android.internal.R.styleable.PreferenceHeader);
1024 category.id = sa.getResourceId(
1025 com.android.internal.R.styleable.PreferenceHeader_id,
1026 (int)DashboardCategory.CAT_ID_UNDEFINED);
1027
1028 TypedValue tv = sa.peekValue(
1029 com.android.internal.R.styleable.PreferenceHeader_title);
1030 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1031 if (tv.resourceId != 0) {
1032 category.titleRes = tv.resourceId;
1033 } else {
1034 category.title = tv.string;
1035 }
1036 }
1037 sa.recycle();
1038
1039 final int innerDepth = parser.getDepth();
1040 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1041 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1042 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1043 continue;
1044 }
1045
1046 String innerNodeName = parser.getName();
1047 if (innerNodeName.equals("dashboard-tile")) {
1048 DashboardTile tile = new DashboardTile();
1049
1050 sa = obtainStyledAttributes(
1051 attrs, com.android.internal.R.styleable.PreferenceHeader);
1052 tile.id = sa.getResourceId(
1053 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001054 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001055 tv = sa.peekValue(
1056 com.android.internal.R.styleable.PreferenceHeader_title);
1057 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1058 if (tv.resourceId != 0) {
1059 tile.titleRes = tv.resourceId;
1060 } else {
1061 tile.title = tv.string;
1062 }
1063 }
1064 tv = sa.peekValue(
1065 com.android.internal.R.styleable.PreferenceHeader_summary);
1066 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1067 if (tv.resourceId != 0) {
1068 tile.summaryRes = tv.resourceId;
1069 } else {
1070 tile.summary = tv.string;
1071 }
1072 }
1073 tile.iconRes = sa.getResourceId(
1074 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1075 tile.fragment = sa.getString(
1076 com.android.internal.R.styleable.PreferenceHeader_fragment);
1077 sa.recycle();
1078
1079 if (curBundle == null) {
1080 curBundle = new Bundle();
1081 }
1082
1083 final int innerDepth2 = parser.getDepth();
1084 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1085 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1086 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1087 continue;
1088 }
1089
1090 String innerNodeName2 = parser.getName();
1091 if (innerNodeName2.equals("extra")) {
1092 getResources().parseBundleExtra("extra", attrs, curBundle);
1093 XmlUtils.skipCurrentTag(parser);
1094
1095 } else if (innerNodeName2.equals("intent")) {
1096 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1097
1098 } else {
1099 XmlUtils.skipCurrentTag(parser);
1100 }
1101 }
1102
1103 if (curBundle.size() > 0) {
1104 tile.fragmentArguments = curBundle;
1105 curBundle = null;
1106 }
1107
PauloftheWest38155612014-06-30 10:02:36 -07001108 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001109 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001110 category.addTile(tile);
1111 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001112
1113 } else {
1114 XmlUtils.skipCurrentTag(parser);
1115 }
1116 }
1117
1118 target.add(category);
1119 } else {
1120 XmlUtils.skipCurrentTag(parser);
1121 }
1122 }
1123
1124 } catch (XmlPullParserException e) {
1125 throw new RuntimeException("Error parsing categories", e);
1126 } catch (IOException e) {
1127 throw new RuntimeException("Error parsing categories", e);
1128 } finally {
1129 if (parser != null) parser.close();
1130 }
1131 }
1132
1133 private void updateTilesList(List<DashboardCategory> target) {
1134 final boolean showDev = mDevelopmentPreferences.getBoolean(
1135 DevelopmentSettings.PREF_SHOW,
1136 android.os.Build.TYPE.equals("eng"));
1137
1138 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1139
1140 final int size = target.size();
1141 for (int i = 0; i < size; i++) {
1142
1143 DashboardCategory category = target.get(i);
1144
1145 // Ids are integers, so downcasting is ok
1146 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001147 int n = category.getTilesCount() - 1;
1148 while (n >= 0) {
1149
1150 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001151 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001152 id = (int) tile.id;
1153 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001154 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1155 removeTile = true;
1156 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001157 } else if (id == R.id.wifi_settings) {
1158 // Remove WiFi Settings if WiFi service is not available.
1159 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001160 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001161 }
1162 } else if (id == R.id.bluetooth_settings) {
1163 // Remove Bluetooth Settings if Bluetooth service is not available.
1164 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001165 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001166 }
1167 } else if (id == R.id.data_usage_settings) {
1168 // Remove data usage when kernel module not enabled
1169 final INetworkManagementService netManager = INetworkManagementService.Stub
1170 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1171 try {
1172 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001173 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001174 }
1175 } catch (RemoteException e) {
1176 // ignored
1177 }
1178 } else if (id == R.id.battery_settings) {
1179 // Remove battery settings when battery is not available. (e.g. TV)
1180
1181 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001182 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001183 }
1184 } else if (id == R.id.home_settings) {
1185 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001186 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001187 }
1188 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001189 boolean hasMultipleUsers =
1190 ((UserManager) getSystemService(Context.USER_SERVICE))
1191 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001192 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001193 || (!UserManager.supportsMultipleUsers()
1194 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001195 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001196 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001197 }
1198 } else if (id == R.id.nfc_payment_settings) {
1199 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001200 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001201 } else {
1202 // Only show if NFC is on and we have the HCE feature
1203 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001204 if (adapter == null || !adapter.isEnabled() ||
1205 !getPackageManager().hasSystemFeature(
1206 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001207 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001208 }
1209 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001210 } else if (id == R.id.print_settings) {
1211 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1212 PackageManager.FEATURE_PRINTING);
1213 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001214 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001215 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001216 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001217 if (!showDev || um.hasUserRestriction(
1218 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001219 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001220 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001221 }
1222
1223 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001224 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1225 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001226 }
1227
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001228 if (removeTile && n < category.getTilesCount()) {
1229 category.removeTile(n);
1230 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001231 n--;
1232 }
1233 }
1234 }
1235
1236 private boolean updateHomeSettingTiles(DashboardTile tile) {
1237 // Once we decide to show Home settings, keep showing it forever
1238 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1239 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1240 return true;
1241 }
1242
1243 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001244 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001245 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001246 // When there's only one available home app, omit this settings
1247 // category entirely at the top level UI. If the user just
1248 // uninstalled the penultimate home app candidiate, we also
1249 // now tell them about why they aren't seeing 'Home' in the list.
1250 if (sShowNoHomeNotice) {
1251 sShowNoHomeNotice = false;
1252 NoHomeDialogFragment.show(this);
1253 }
1254 return false;
1255 } else {
1256 // Okay, we're allowing the Home settings category. Tell it, when
1257 // invoked via this front door, that we'll need to be told about the
1258 // case when the user uninstalls all but one home app.
1259 if (tile.fragmentArguments == null) {
1260 tile.fragmentArguments = new Bundle();
1261 }
1262 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1263 }
1264 } catch (Exception e) {
1265 // Can't look up the home activity; bail on configuring the icon
1266 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1267 }
1268
1269 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1270 return true;
1271 }
1272
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001273 private void getMetaData() {
1274 try {
1275 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1276 PackageManager.GET_META_DATA);
1277 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001278 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1279 } catch (NameNotFoundException nnfe) {
1280 // No recovery
1281 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1282 }
1283 }
1284
1285 // give subclasses access to the Next button
1286 public boolean hasNextButton() {
1287 return mNextButton != null;
1288 }
1289
1290 public Button getNextButton() {
1291 return mNextButton;
1292 }
1293
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001294 @Override
1295 public boolean shouldUpRecreateTask(Intent targetIntent) {
1296 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1297 }
1298
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001299 public static void requestHomeNotice() {
1300 sShowNoHomeNotice = true;
1301 }
1302
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001303 @Override
1304 public boolean onQueryTextSubmit(String query) {
1305 switchToSearchResultsFragmentIfNeeded();
1306 mSearchQuery = query;
1307 return mSearchResultsFragment.onQueryTextSubmit(query);
1308 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001309
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001310 @Override
1311 public boolean onQueryTextChange(String newText) {
1312 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001313 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001314 return false;
1315 }
1316 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001317 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001318
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001319 @Override
1320 public boolean onClose() {
1321 return false;
1322 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001323
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001324 @Override
1325 public boolean onMenuItemActionExpand(MenuItem item) {
1326 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001327 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001328 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001329 return true;
1330 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001331
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001332 @Override
1333 public boolean onMenuItemActionCollapse(MenuItem item) {
1334 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001335 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001336 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001337 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001338 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001339 return true;
1340 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001341
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001342 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001343 if (mSearchResultsFragment != null) {
1344 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001345 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001346 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001347 if (current != null && current instanceof SearchResultsSummary) {
1348 mSearchResultsFragment = (SearchResultsSummary) current;
1349 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001350 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001351 SearchResultsSummary.class.getName(), null, false, true,
1352 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001353 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001354 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001355 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001356 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001357
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001358 public void needToRevertToInitialFragment() {
1359 mNeedToRevertToInitialFragment = true;
1360 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001361
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001362 private void revertToInitialFragment() {
1363 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001364 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001365 mSearchMenuItemExpanded = false;
1366 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1367 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001368 if (mSearchMenuItem != null) {
1369 mSearchMenuItem.collapseActionView();
1370 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001371 }
Jim Miller0698a212014-10-16 19:49:07 -07001372
1373 public Intent getResultIntentData() {
1374 return mResultIntentData;
1375 }
1376
1377 public void setResultIntentData(Intent resultIntentData) {
1378 mResultIntentData = resultIntentData;
1379 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001380}