blob: ffb55d6e49db69aaf13ae817015f81973f654509 [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080019import android.app.ActionBar;
20import android.app.Activity;
21import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.pm.ResolveInfo;
34import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080035import android.content.res.TypedArray;
36import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.nfc.NfcAdapter;
38import android.os.Bundle;
39import android.os.Handler;
40import android.os.INetworkManagementService;
41import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.UserHandle;
45import android.os.UserManager;
46import android.preference.Preference;
47import android.preference.PreferenceFragment;
48import android.preference.PreferenceManager;
49import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.util.AttributeSet;
53import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import android.util.TypedValue;
55import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070056import android.view.Menu;
57import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.view.MenuItem;
59import android.view.View;
60import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070061import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070063import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040064
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080065import com.android.internal.util.ArrayUtils;
66import com.android.internal.util.XmlUtils;
67import com.android.settings.accessibility.AccessibilitySettings;
68import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000069import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070071import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import com.android.settings.applications.ManageApplications;
73import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070075import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070077import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070078import com.android.settings.dashboard.NoHomeDialogFragment;
79import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.deviceinfo.Memory;
81import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040082import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083import com.android.settings.fuelgauge.PowerUsageSummary;
John Spurlock802ddf92014-07-18 11:51:13 -040084import com.android.settings.notification.NotificationAppList;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070085import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -070086import com.android.settings.quicklaunch.QuickLaunchSettings;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070087import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070088import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
90import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
91import com.android.settings.inputmethod.SpellCheckersSettings;
92import com.android.settings.inputmethod.UserDictionaryList;
93import com.android.settings.location.LocationSettings;
94import com.android.settings.nfc.AndroidBeam;
95import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040096import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040097import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
101import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800102import com.android.settings.print.PrintJobSettingsFragment;
103import com.android.settings.print.PrintSettingsFragment;
PauloftheWest38155612014-06-30 10:02:36 -0700104import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800105import com.android.settings.tts.TextToSpeechSettings;
106import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700107import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.vpn2.VpnSettings;
109import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700110import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700112import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.wifi.WifiSettings;
114import com.android.settings.wifi.p2p.WifiP2pSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400115
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import org.xmlpull.v1.XmlPullParser;
117import org.xmlpull.v1.XmlPullParserException;
118
119import java.io.IOException;
120import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800121import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700122import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700124import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700125
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126public class SettingsActivity extends Activity
127 implements PreferenceManager.OnPreferenceTreeClickListener,
128 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100129 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700130 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100131 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800132
133 private static final String LOG_TAG = "Settings";
134
135 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700136 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
138 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700139 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700140 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700141 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
143 /**
144 * When starting this activity, the invoking Intent can contain this extra
145 * string to specify which fragment should be initially displayed.
146 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
147 * will call isValidFragment() to confirm that the fragment class name is valid for this
148 * activity.
149 */
150 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
151
152 /**
153 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
154 * this extra can also be specified to supply a Bundle of arguments to pass
155 * to that fragment when it is instantiated during the initial creation
156 * of the activity.
157 */
158 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
159
160 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700161 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
162 */
163 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
164
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800165 public static final String BACK_STACK_PREFS = ":settings:prefs";
166
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167 // extras that allow any preference activity to be launched as part of a wizard
168
169 // show Back and Next buttons? takes boolean parameter
170 // Back will then return RESULT_CANCELED and Next RESULT_OK
171 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
172
173 // add a Skip button?
174 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
175
176 // specify custom text for the Back or Next buttons, or cause a button to not appear
177 // at all by setting it to null
178 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
179 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
180
181 /**
182 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700183 * those extra can also be specify to supply the title or title res id to be shown for
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800184 * that fragment.
185 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700186 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700187 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
188 ":settings:show_fragment_title_resid";
189 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
190 ":settings:show_fragment_as_shortcut";
191
192 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
193 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800195 private static final String META_DATA_KEY_FRAGMENT_CLASS =
196 "com.android.settings.FRAGMENT_CLASS";
197
198 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
199
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700200 private static final String EMPTY_QUERY = "";
201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 private static boolean sShowNoHomeNotice = false;
203
204 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800205
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800206 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700207 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 // Show only these settings for restricted users
210 private int[] SETTINGS_FOR_RESTRICTED = {
211 R.id.wireless_section,
212 R.id.wifi_settings,
213 R.id.bluetooth_settings,
214 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700215 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800216 R.id.wireless_settings,
217 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400218 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800219 R.id.display_settings,
220 R.id.storage_settings,
221 R.id.application_settings,
222 R.id.battery_settings,
223 R.id.personal_section,
224 R.id.location_settings,
225 R.id.security_settings,
226 R.id.language_settings,
227 R.id.user_settings,
228 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800229 R.id.system_section,
230 R.id.date_time_settings,
231 R.id.about_settings,
232 R.id.accessibility_settings,
233 R.id.print_settings,
234 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800235 R.id.home_settings,
236 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800237 };
238
239 private static final String[] ENTRY_FRAGMENTS = {
240 WirelessSettings.class.getName(),
241 WifiSettings.class.getName(),
242 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700243 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700245 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 TetherSettings.class.getName(),
247 WifiP2pSettings.class.getName(),
248 VpnSettings.class.getName(),
249 DateTimeSettings.class.getName(),
250 LocalePicker.class.getName(),
251 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700252 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800253 SpellCheckersSettings.class.getName(),
254 UserDictionaryList.class.getName(),
255 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700256 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800257 DisplaySettings.class.getName(),
258 DeviceInfoSettings.class.getName(),
259 ManageApplications.class.getName(),
260 ProcessStatsUi.class.getName(),
261 NotificationStation.class.getName(),
262 LocationSettings.class.getName(),
263 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700264 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800265 PrivacySettings.class.getName(),
266 DeviceAdminSettings.class.getName(),
267 AccessibilitySettings.class.getName(),
268 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
270 TextToSpeechSettings.class.getName(),
271 Memory.class.getName(),
272 DevelopmentSettings.class.getName(),
273 UsbSettings.class.getName(),
274 AndroidBeam.class.getName(),
275 WifiDisplaySettings.class.getName(),
276 PowerUsageSummary.class.getName(),
277 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000278 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279 CryptKeeperSettings.class.getName(),
280 DataUsageSummary.class.getName(),
281 DreamSettings.class.getName(),
282 UserSettings.class.getName(),
283 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400284 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 PrintSettingsFragment.class.getName(),
286 PrintJobSettingsFragment.class.getName(),
287 TrustedCredentialsSettings.class.getName(),
288 PaymentSettings.class.getName(),
289 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700290 ZenModeSettings.class.getName(),
291 NotificationSettings.class.getName(),
292 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
293 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400294 InstalledAppDetails.class.getName(),
295 BatterySaverSettings.class.getName(),
John Spurlock802ddf92014-07-18 11:51:13 -0400296 NotificationAppList.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700297 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700298 OtherSoundSettings.class.getName(),
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700299 QuickLaunchSettings.class.getName(),
300 ApnSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 };
302
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700303
304 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
305 "android.settings.APPLICATION_DETAILS_SETTINGS"
306 };
307
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 private SharedPreferences mDevelopmentPreferences;
309 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
310
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800311 private boolean mBatteryPresent = true;
312 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 @Override
314 public void onReceive(Context context, Intent intent) {
315 String action = intent.getAction();
316 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
317 boolean batteryPresent = Utils.isBatteryPresent(intent);
318
319 if (mBatteryPresent != batteryPresent) {
320 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700321 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 }
323 }
324 }
325 };
326
Svetoslav990159a2014-04-14 17:14:59 -0700327 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
328 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700329
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700330 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700331 private SwitchBar mSwitchBar;
332
333 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700334
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700335 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700336 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700337
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700338 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700339 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700340
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700341 private ViewGroup mContent;
342
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700343 private SearchView mSearchView;
344 private MenuItem mSearchMenuItem;
345 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700346 private SearchResultsSummary mSearchResultsFragment;
347 private String mSearchQuery;
348
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700349 // Categories
350 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700352 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700353 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 private Handler mHandler = new Handler() {
355 @Override
356 public void handleMessage(Message msg) {
357 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700358 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700359 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
360 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700361 buildDashboardCategories(mCategories);
362 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 } break;
364 }
365 }
366 };
367
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700368 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700369 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700370
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700371 public SwitchBar getSwitchBar() {
372 return mSwitchBar;
373 }
374
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700375 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
376 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700377 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700378 }
379 return mCategories;
380 }
381
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800382 @Override
383 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
384 // Override the fragment title for Wallpaper settings
385 int titleRes = pref.getTitleRes();
386 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
387 titleRes = R.string.wallpaper_settings_fragment_title;
388 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
389 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
390 if (UserManager.get(this).isLinkedUser()) {
391 titleRes = R.string.profile_info_settings_title;
392 } else {
393 titleRes = R.string.user_info_settings_title;
394 }
395 }
396 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
397 null, 0);
398 return true;
399 }
400
401 @Override
402 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
403 return false;
404 }
405
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700406 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700407 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700408 Message msg = new Message();
409 msg.what = MSG_BUILD_CATEGORIES;
410 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 }
412 }
413
414 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800415 public void onConfigurationChanged(Configuration newConfig) {
416 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800417 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800418 }
419
420 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700421 protected void onStart() {
422 super.onStart();
423
424 if (mNeedToRevertToInitialFragment) {
425 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800426 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800427 }
428
429 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700430 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700431 if (!mDisplaySearch) {
432 return false;
433 }
434
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700435 MenuInflater inflater = getMenuInflater();
436 inflater.inflate(R.menu.options_menu, menu);
437
438 // Cache the search query (can be overriden by the OnQueryTextListener)
439 final String query = mSearchQuery;
440
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700441 mSearchMenuItem = menu.findItem(R.id.search);
442 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700443
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700444 if (mSearchMenuItem == null || mSearchView == null) {
445 return false;
446 }
447
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700448 if (mSearchResultsFragment != null) {
449 mSearchResultsFragment.setSearchView(mSearchView);
450 }
451
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700452 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700453 mSearchView.setOnQueryTextListener(this);
454 mSearchView.setOnCloseListener(this);
455
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700456 if (mSearchMenuItemExpanded) {
457 mSearchMenuItem.expandActionView();
458 }
459 mSearchView.setQuery(query, true /* submit */);
460
461 return true;
462 }
463
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700464 private static boolean isShortCutIntent(final Intent intent) {
465 Set<String> categories = intent.getCategories();
466 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
467 }
468
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700469 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700470 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700471 if (action == null) {
472 return false;
473 }
474 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
475 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
476 }
477 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700478 }
479
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700480 @Override
481 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700482 super.onCreate(savedState);
483
484 // Should happen before any call to getIntent()
485 getMetaData();
486
487 final Intent intent = getIntent();
488 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
489 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800491
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
493 Context.MODE_PRIVATE);
494
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700495 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700496 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700497
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700498 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700499 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
500
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700501 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700502 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700503
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700504 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700505
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700506 // This is a "Sub Settings" when:
507 // - this is a real SubSettings
508 // - or :settings:show_fragment_as_subsetting is passed to the Intent
509 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
510 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
511
512 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
513 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700514 // Check also that we are not a Theme Dialog as we don't want to override them
515 final int themeResId = getThemeResId();
516 if (themeResId != R.style.Theme_DialogWhenLarge &&
517 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
518 setTheme(R.style.Theme_SubSettings);
519 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700520 }
521
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700522 setContentView(mIsShowingDashboard ?
523 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800524
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700525 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700526
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 getFragmentManager().addOnBackStackChangedListener(this);
528
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700529 if (mIsShowingDashboard) {
Fabrice Di Megliodba577f2014-06-06 16:31:45 -0700530 Index.getInstance(getApplicationContext()).update();
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700531 }
532
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700533 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700534 // We are restarting from a previous saved state; used that to initialize, instead
535 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700536 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
537 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700539 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800540
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700541 ArrayList<DashboardCategory> categories =
542 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
543 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700544 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700545 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700546 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800547 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700548
549 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700550 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700551 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
552 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700554 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700555 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
556 // only if it is a sub settings
557 if (mIsShortcut) {
558 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700559 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700560 } else if (isSubSettings) {
561 mDisplayHomeAsUpEnabled = true;
562 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700563 } else {
564 mDisplayHomeAsUpEnabled = false;
565 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700566 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700567 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700568
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700569 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700570 switchToFragment(initialFragmentName, initialArguments, true, false,
571 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000572 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700573 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700574 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700575 // Show Search affordance
576 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700577 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700578 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700579 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580 }
581 }
582
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700583 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700584 if (mActionBar != null) {
585 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
586 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
587 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700588 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
589
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800590 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800591 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
592
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700593 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 if (buttonBar != null) {
595 buttonBar.setVisibility(View.VISIBLE);
596
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700597 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800598 backButton.setOnClickListener(new OnClickListener() {
599 public void onClick(View v) {
600 setResult(RESULT_CANCELED);
601 finish();
602 }
603 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700604 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800605 skipButton.setOnClickListener(new OnClickListener() {
606 public void onClick(View v) {
607 setResult(RESULT_OK);
608 finish();
609 }
610 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700611 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800612 mNextButton.setOnClickListener(new OnClickListener() {
613 public void onClick(View v) {
614 setResult(RESULT_OK);
615 finish();
616 }
617 });
618
619 // set our various button parameters
620 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
621 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
622 if (TextUtils.isEmpty(buttonText)) {
623 mNextButton.setVisibility(View.GONE);
624 }
625 else {
626 mNextButton.setText(buttonText);
627 }
628 }
629 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
630 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
631 if (TextUtils.isEmpty(buttonText)) {
632 backButton.setVisibility(View.GONE);
633 }
634 else {
635 backButton.setText(buttonText);
636 }
637 }
638 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
639 skipButton.setVisibility(View.VISIBLE);
640 }
641 }
642 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700643
644 mHomeActivitiesCount = getHomeActivitiesCount();
645 }
646
647 private int getHomeActivitiesCount() {
648 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
649 getPackageManager().getHomeActivities(homeApps);
650 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800651 }
652
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700653 private void setTitleFromIntent(Intent intent) {
654 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
655 if (initialTitleResId > 0) {
656 mInitialTitle = null;
657 mInitialTitleResId = initialTitleResId;
658 setTitle(mInitialTitleResId);
659 } else {
660 mInitialTitleResId = -1;
661 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
662 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
663 setTitle(mInitialTitle);
664 }
665 }
666
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800667 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800668 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700669 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800670 }
671
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700672 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800673 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700674
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800675 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700676 if (mInitialTitleResId > 0) {
677 setTitle(mInitialTitleResId);
678 } else {
679 setTitle(mInitialTitle);
680 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700681 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800682 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700683
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800684 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
685 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700686
687 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800688 }
689
690 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
691 final CharSequence title;
692 final int titleRes = bse.getBreadCrumbTitleRes();
693 if (titleRes > 0) {
694 title = getText(titleRes);
695 } else {
696 title = bse.getBreadCrumbTitle();
697 }
698 if (title != null) {
699 setTitle(title);
700 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800701 }
702
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800703 @Override
704 protected void onSaveInstanceState(Bundle outState) {
705 super.onSaveInstanceState(outState);
706
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700707 if (mCategories.size() > 0) {
708 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800709 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700710
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700711 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700712 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700713
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700714 if (mDisplaySearch) {
715 // The option menus are created if the ActionBar is visible and they are also created
716 // asynchronously. If you launch Settings with an Intent action like
717 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
718 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
719 // menu item and search view are null.
720 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
721 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700722
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700723 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
724 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
725 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700726
727 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800728 }
729
730 @Override
731 public void onResume() {
732 super.onResume();
733
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700734 final int newHomeActivityCount = getHomeActivitiesCount();
735 if (newHomeActivityCount != mHomeActivitiesCount) {
736 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700737 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700738 }
739
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800740 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
741 @Override
742 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700743 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800744 }
745 };
746 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
747 mDevelopmentPreferencesListener);
748
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800749 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700750
Svetoslav990159a2014-04-14 17:14:59 -0700751 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700752
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700753 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700754 onQueryTextSubmit(mSearchQuery);
755 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800756 }
757
758 @Override
759 public void onPause() {
760 super.onPause();
761
762 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700763 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800764 }
765
766 @Override
767 public void onDestroy() {
768 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700769
770 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
771 mDevelopmentPreferencesListener);
772 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800773 }
774
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800775 protected boolean isValidFragment(String fragmentName) {
776 // Almost all fragments are wrapped in this,
777 // except for a few that have their own activities.
778 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
779 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
780 }
781 return false;
782 }
783
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800784 @Override
785 public Intent getIntent() {
786 Intent superIntent = super.getIntent();
787 String startingFragment = getStartingFragmentClass(superIntent);
788 // This is called from super.onCreate, isMultiPane() is not yet reliable
789 // Do not use onIsHidingHeaders either, which relies itself on this method
790 if (startingFragment != null) {
791 Intent modIntent = new Intent(superIntent);
792 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
793 Bundle args = superIntent.getExtras();
794 if (args != null) {
795 args = new Bundle(args);
796 } else {
797 args = new Bundle();
798 }
799 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100800 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800801 return modIntent;
802 }
803 return superIntent;
804 }
805
806 /**
807 * Checks if the component name in the intent is different from the Settings class and
808 * returns the class name to load as a fragment.
809 */
810 private String getStartingFragmentClass(Intent intent) {
811 if (mFragmentClass != null) return mFragmentClass;
812
813 String intentClass = intent.getComponent().getClassName();
814 if (intentClass.equals(getClass().getName())) return null;
815
816 if ("com.android.settings.ManageApplications".equals(intentClass)
817 || "com.android.settings.RunningServices".equals(intentClass)
818 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
819 // Old names of manage apps.
820 intentClass = com.android.settings.applications.ManageApplications.class.getName();
821 }
822
823 return intentClass;
824 }
825
826 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000827 * Start a new fragment containing a preference panel. If the preferences
828 * are being displayed in multi-pane mode, the given fragment class will
829 * be instantiated and placed in the appropriate pane. If running in
830 * single-pane mode, a new activity will be launched in which to show the
831 * fragment.
832 *
833 * @param fragmentClass Full name of the class implementing the fragment.
834 * @param args Any desired arguments to supply to the fragment.
835 * @param titleRes Optional resource identifier of the title of this
836 * fragment.
837 * @param titleText Optional text of the title of this fragment.
838 * @param resultTo Optional fragment that result data should be sent to.
839 * If non-null, resultTo.onActivityResult() will be called when this
840 * preference panel is done. The launched panel must use
841 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
842 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700843 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000844 */
845 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700846 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700847 String title = null;
848 if (titleRes < 0) {
849 if (titleText != null) {
850 title = titleText.toString();
851 } else {
852 // There not much we can do in that case
853 title = "";
854 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700855 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700856 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700857 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000858 }
859
860 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100861 * Start a new fragment in a new activity containing a preference panel for a given user. If the
862 * preferences are being displayed in multi-pane mode, the given fragment class will be
863 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
864 * activity will be launched in which to show the fragment.
865 *
866 * @param fragmentClass Full name of the class implementing the fragment.
867 * @param args Any desired arguments to supply to the fragment.
868 * @param titleRes Optional resource identifier of the title of this fragment.
869 * @param titleText Optional text of the title of this fragment.
870 * @param userHandle The user for which the panel has to be started.
871 */
872 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
873 CharSequence titleText, UserHandle userHandle) {
874 String title = null;
875 if (titleRes < 0) {
876 if (titleText != null) {
877 title = titleText.toString();
878 } else {
879 // There not much we can do in that case
880 title = "";
881 }
882 }
883 Utils.startWithFragmentAsUser(this, fragmentClass, args,
884 titleRes, title, mIsShortcut, userHandle);
885 }
886
887 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800888 * Called by a preference panel fragment to finish itself.
889 *
890 * @param caller The fragment that is asking to be finished.
891 * @param resultCode Optional result code to send back to the original
892 * launching fragment.
893 * @param resultData Optional result data to send back to the original
894 * launching fragment.
895 */
896 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
897 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700898 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800899 }
900
901 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000902 * Start a new fragment.
903 *
904 * @param fragment The fragment to start
905 * @param push If true, the current fragment will be pushed onto the back stack. If false,
906 * the current fragment will be replaced.
907 */
908 public void startPreferenceFragment(Fragment fragment, boolean push) {
909 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700910 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000911 if (push) {
912 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
913 transaction.addToBackStack(BACK_STACK_PREFS);
914 } else {
915 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
916 }
917 transaction.commitAllowingStateLoss();
918 }
919
920 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700921 * Switch to a specific Fragment with taking care of validation, Title and BackStack
922 */
923 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700924 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700925 if (validate && !isValidFragment(fragmentName)) {
926 throw new IllegalArgumentException("Invalid fragment for this activity: "
927 + fragmentName);
928 }
929 Fragment f = Fragment.instantiate(this, fragmentName, args);
930 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700931 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700932 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700933 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700934 }
935 if (addToBackStack) {
936 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
937 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700938 if (titleResId > 0) {
939 transaction.setBreadCrumbTitle(titleResId);
940 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700941 transaction.setBreadCrumbTitle(title);
942 }
943 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700944 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700945 return f;
946 }
947
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700948 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700949 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700950 *
951 * @param categories The list in which to place the tiles categories.
952 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700953 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700954 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700955 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
956 updateTilesList(categories);
957 }
958
959 /**
960 * Parse the given XML file as a categories description, adding each
961 * parsed categories and tiles into the target list.
962 *
963 * @param resid The XML resource to load and parse.
964 * @param target The list in which the parsed categories and tiles should be placed.
965 */
966 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
967 XmlResourceParser parser = null;
968 try {
969 parser = getResources().getXml(resid);
970 AttributeSet attrs = Xml.asAttributeSet(parser);
971
972 int type;
973 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
974 && type != XmlPullParser.START_TAG) {
975 // Parse next until start tag is found
976 }
977
978 String nodeName = parser.getName();
979 if (!"dashboard-categories".equals(nodeName)) {
980 throw new RuntimeException(
981 "XML document must start with <preference-categories> tag; found"
982 + nodeName + " at " + parser.getPositionDescription());
983 }
984
985 Bundle curBundle = null;
986
987 final int outerDepth = parser.getDepth();
988 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
989 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
990 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
991 continue;
992 }
993
994 nodeName = parser.getName();
995 if ("dashboard-category".equals(nodeName)) {
996 DashboardCategory category = new DashboardCategory();
997
998 TypedArray sa = obtainStyledAttributes(
999 attrs, com.android.internal.R.styleable.PreferenceHeader);
1000 category.id = sa.getResourceId(
1001 com.android.internal.R.styleable.PreferenceHeader_id,
1002 (int)DashboardCategory.CAT_ID_UNDEFINED);
1003
1004 TypedValue tv = sa.peekValue(
1005 com.android.internal.R.styleable.PreferenceHeader_title);
1006 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1007 if (tv.resourceId != 0) {
1008 category.titleRes = tv.resourceId;
1009 } else {
1010 category.title = tv.string;
1011 }
1012 }
1013 sa.recycle();
1014
1015 final int innerDepth = parser.getDepth();
1016 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1017 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1018 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1019 continue;
1020 }
1021
1022 String innerNodeName = parser.getName();
1023 if (innerNodeName.equals("dashboard-tile")) {
1024 DashboardTile tile = new DashboardTile();
1025
1026 sa = obtainStyledAttributes(
1027 attrs, com.android.internal.R.styleable.PreferenceHeader);
1028 tile.id = sa.getResourceId(
1029 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001030 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001031 tv = sa.peekValue(
1032 com.android.internal.R.styleable.PreferenceHeader_title);
1033 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1034 if (tv.resourceId != 0) {
1035 tile.titleRes = tv.resourceId;
1036 } else {
1037 tile.title = tv.string;
1038 }
1039 }
1040 tv = sa.peekValue(
1041 com.android.internal.R.styleable.PreferenceHeader_summary);
1042 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1043 if (tv.resourceId != 0) {
1044 tile.summaryRes = tv.resourceId;
1045 } else {
1046 tile.summary = tv.string;
1047 }
1048 }
1049 tile.iconRes = sa.getResourceId(
1050 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1051 tile.fragment = sa.getString(
1052 com.android.internal.R.styleable.PreferenceHeader_fragment);
1053 sa.recycle();
1054
1055 if (curBundle == null) {
1056 curBundle = new Bundle();
1057 }
1058
1059 final int innerDepth2 = parser.getDepth();
1060 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1061 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1062 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1063 continue;
1064 }
1065
1066 String innerNodeName2 = parser.getName();
1067 if (innerNodeName2.equals("extra")) {
1068 getResources().parseBundleExtra("extra", attrs, curBundle);
1069 XmlUtils.skipCurrentTag(parser);
1070
1071 } else if (innerNodeName2.equals("intent")) {
1072 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1073
1074 } else {
1075 XmlUtils.skipCurrentTag(parser);
1076 }
1077 }
1078
1079 if (curBundle.size() > 0) {
1080 tile.fragmentArguments = curBundle;
1081 curBundle = null;
1082 }
1083
PauloftheWest38155612014-06-30 10:02:36 -07001084 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001085 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001086 category.addTile(tile);
1087 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001088
1089 } else {
1090 XmlUtils.skipCurrentTag(parser);
1091 }
1092 }
1093
1094 target.add(category);
1095 } else {
1096 XmlUtils.skipCurrentTag(parser);
1097 }
1098 }
1099
1100 } catch (XmlPullParserException e) {
1101 throw new RuntimeException("Error parsing categories", e);
1102 } catch (IOException e) {
1103 throw new RuntimeException("Error parsing categories", e);
1104 } finally {
1105 if (parser != null) parser.close();
1106 }
1107 }
1108
1109 private void updateTilesList(List<DashboardCategory> target) {
1110 final boolean showDev = mDevelopmentPreferences.getBoolean(
1111 DevelopmentSettings.PREF_SHOW,
1112 android.os.Build.TYPE.equals("eng"));
1113
1114 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1115
1116 final int size = target.size();
1117 for (int i = 0; i < size; i++) {
1118
1119 DashboardCategory category = target.get(i);
1120
1121 // Ids are integers, so downcasting is ok
1122 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001123 int n = category.getTilesCount() - 1;
1124 while (n >= 0) {
1125
1126 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001127 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001128 id = (int) tile.id;
1129 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001130 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1131 removeTile = true;
1132 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001133 } else if (id == R.id.wifi_settings) {
1134 // Remove WiFi Settings if WiFi service is not available.
1135 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001136 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001137 }
1138 } else if (id == R.id.bluetooth_settings) {
1139 // Remove Bluetooth Settings if Bluetooth service is not available.
1140 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001141 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001142 }
1143 } else if (id == R.id.data_usage_settings) {
1144 // Remove data usage when kernel module not enabled
1145 final INetworkManagementService netManager = INetworkManagementService.Stub
1146 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1147 try {
1148 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001149 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001150 }
1151 } catch (RemoteException e) {
1152 // ignored
1153 }
1154 } else if (id == R.id.battery_settings) {
1155 // Remove battery settings when battery is not available. (e.g. TV)
1156
1157 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001158 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001159 }
1160 } else if (id == R.id.home_settings) {
1161 if (!updateHomeSettingTiles(tile)) {
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.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001165 boolean hasMultipleUsers =
1166 ((UserManager) getSystemService(Context.USER_SERVICE))
1167 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001168 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001169 || (!UserManager.supportsMultipleUsers()
1170 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001171 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001172 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001173 }
1174 } else if (id == R.id.nfc_payment_settings) {
1175 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001176 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001177 } else {
1178 // Only show if NFC is on and we have the HCE feature
1179 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
1180 if (!adapter.isEnabled() || !getPackageManager().hasSystemFeature(
1181 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001182 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001183 }
1184 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001185 } else if (id == R.id.print_settings) {
1186 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1187 PackageManager.FEATURE_PRINTING);
1188 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001189 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001190 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001191 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001192 if (!showDev || um.hasUserRestriction(
1193 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001194 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001195 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001196 }
1197
1198 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001199 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1200 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001201 }
1202
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001203 if (removeTile && n < category.getTilesCount()) {
1204 category.removeTile(n);
1205 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001206 n--;
1207 }
1208 }
1209 }
1210
1211 private boolean updateHomeSettingTiles(DashboardTile tile) {
1212 // Once we decide to show Home settings, keep showing it forever
1213 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1214 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1215 return true;
1216 }
1217
1218 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001219 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001220 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001221 // When there's only one available home app, omit this settings
1222 // category entirely at the top level UI. If the user just
1223 // uninstalled the penultimate home app candidiate, we also
1224 // now tell them about why they aren't seeing 'Home' in the list.
1225 if (sShowNoHomeNotice) {
1226 sShowNoHomeNotice = false;
1227 NoHomeDialogFragment.show(this);
1228 }
1229 return false;
1230 } else {
1231 // Okay, we're allowing the Home settings category. Tell it, when
1232 // invoked via this front door, that we'll need to be told about the
1233 // case when the user uninstalls all but one home app.
1234 if (tile.fragmentArguments == null) {
1235 tile.fragmentArguments = new Bundle();
1236 }
1237 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1238 }
1239 } catch (Exception e) {
1240 // Can't look up the home activity; bail on configuring the icon
1241 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1242 }
1243
1244 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1245 return true;
1246 }
1247
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001248 private void getMetaData() {
1249 try {
1250 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1251 PackageManager.GET_META_DATA);
1252 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001253 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1254 } catch (NameNotFoundException nnfe) {
1255 // No recovery
1256 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1257 }
1258 }
1259
1260 // give subclasses access to the Next button
1261 public boolean hasNextButton() {
1262 return mNextButton != null;
1263 }
1264
1265 public Button getNextButton() {
1266 return mNextButton;
1267 }
1268
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001269 @Override
1270 public boolean shouldUpRecreateTask(Intent targetIntent) {
1271 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1272 }
1273
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001274 public static void requestHomeNotice() {
1275 sShowNoHomeNotice = true;
1276 }
1277
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001278 @Override
1279 public boolean onQueryTextSubmit(String query) {
1280 switchToSearchResultsFragmentIfNeeded();
1281 mSearchQuery = query;
1282 return mSearchResultsFragment.onQueryTextSubmit(query);
1283 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001284
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001285 @Override
1286 public boolean onQueryTextChange(String newText) {
1287 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001288 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001289 return false;
1290 }
1291 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001292 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001293
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001294 @Override
1295 public boolean onClose() {
1296 return false;
1297 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001298
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001299 @Override
1300 public boolean onMenuItemActionExpand(MenuItem item) {
1301 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001302 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001303 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001304 return true;
1305 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001306
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001307 @Override
1308 public boolean onMenuItemActionCollapse(MenuItem item) {
1309 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001310 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001311 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001312 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001313 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001314 return true;
1315 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001316
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001317 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001318 if (mSearchResultsFragment != null) {
1319 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001320 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001321 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001322 if (current != null && current instanceof SearchResultsSummary) {
1323 mSearchResultsFragment = (SearchResultsSummary) current;
1324 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001325 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001326 SearchResultsSummary.class.getName(), null, false, true,
1327 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001328 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001329 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001330 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001331 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001332
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001333 public void needToRevertToInitialFragment() {
1334 mNeedToRevertToInitialFragment = true;
1335 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001336
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001337 private void revertToInitialFragment() {
1338 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001339 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001340 mSearchMenuItemExpanded = false;
1341 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1342 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001343 if (mSearchMenuItem != null) {
1344 mSearchMenuItem.collapseActionView();
1345 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001346 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001347}