blob: 4794608b4c362b1b33da8cae4641f674f48de0c3 [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
Jason Monk2ebc8a02015-02-13 15:23:19 -050019import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
20
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.ActionBar;
22import android.app.Activity;
23import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.nfc.NfcAdapter;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.INetworkManagementService;
43import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080044import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.preference.Preference;
49import android.preference.PreferenceFragment;
50import android.preference.PreferenceManager;
51import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.transition.TransitionManager;
Jason Monk2ebc8a02015-02-13 15:23:19 -050054import android.util.ArrayMap;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.AttributeSet;
56import android.util.Log;
Jason Monk2ebc8a02015-02-13 15:23:19 -050057import android.util.Pair;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080058import android.util.TypedValue;
59import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070060import android.view.Menu;
61import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080062import android.view.MenuItem;
63import android.view.View;
64import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070065import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080066import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070067import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040068
Chris Wren8a963ba2015-03-20 10:29:14 -040069import com.android.internal.logging.MetricsLogger;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080070import com.android.internal.util.ArrayUtils;
71import com.android.internal.util.XmlUtils;
72import com.android.settings.accessibility.AccessibilitySettings;
73import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000074import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070076import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.applications.ManageApplications;
78import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070080import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070082import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070083import com.android.settings.dashboard.NoHomeDialogFragment;
84import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085import com.android.settings.deviceinfo.Memory;
86import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040087import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080088import com.android.settings.fuelgauge.PowerUsageSummary;
89import 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;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500101import com.android.settings.notification.OtherSoundSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400102import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800103import com.android.settings.print.PrintJobSettingsFragment;
104import com.android.settings.print.PrintSettingsFragment;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500105import com.android.settings.search.DynamicIndexableContentMonitor;
106import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700107import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.tts.TextToSpeechSettings;
109import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700110import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.vpn2.VpnSettings;
112import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700113import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800114import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700115import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800116import com.android.settings.wifi.WifiSettings;
117import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500118
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119import org.xmlpull.v1.XmlPullParser;
120import org.xmlpull.v1.XmlPullParserException;
121
122import java.io.IOException;
123import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500125import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700126import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127
128public class SettingsActivity extends Activity
129 implements PreferenceManager.OnPreferenceTreeClickListener,
130 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100131 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700132 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100133 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800134
135 private static final String LOG_TAG = "Settings";
136
137 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700138 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700139 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
140 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700141 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700142 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700143 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800144
145 /**
146 * When starting this activity, the invoking Intent can contain this extra
147 * string to specify which fragment should be initially displayed.
148 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
149 * will call isValidFragment() to confirm that the fragment class name is valid for this
150 * activity.
151 */
152 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
153
154 /**
155 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
156 * this extra can also be specified to supply a Bundle of arguments to pass
157 * to that fragment when it is instantiated during the initial creation
158 * of the activity.
159 */
160 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
161
162 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700163 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
164 */
165 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
166
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800167 public static final String BACK_STACK_PREFS = ":settings:prefs";
168
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800169 // extras that allow any preference activity to be launched as part of a wizard
170
171 // show Back and Next buttons? takes boolean parameter
172 // Back will then return RESULT_CANCELED and Next RESULT_OK
173 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
174
175 // add a Skip button?
176 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
177
178 // specify custom text for the Back or Next buttons, or cause a button to not appear
179 // at all by setting it to null
180 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
181 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
182
183 /**
184 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700185 * 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 -0800186 * that fragment.
187 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700188 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100189 /**
190 * The package name used to resolve the title resource id.
191 */
192 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
193 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700194 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
195 ":settings:show_fragment_title_resid";
196 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
197 ":settings:show_fragment_as_shortcut";
198
199 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
200 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202 private static final String META_DATA_KEY_FRAGMENT_CLASS =
203 "com.android.settings.FRAGMENT_CLASS";
204
205 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
206
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700207 private static final String EMPTY_QUERY = "";
208
Jason Monk2ebc8a02015-02-13 15:23:19 -0500209 /**
210 * Settings will search for system activities of this action and add them as a top level
211 * settings tile using the following parameters.
212 *
213 * <p>A category must be specified in the meta-data for the activity named
214 * {@link #EXTRA_CATEGORY_KEY}
215 *
216 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
217 * otherwise the label for the activity will be used.
218 *
219 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
220 * otherwise the icon for the activity will be used.
221 *
222 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
223 */
224 private static final String EXTRA_SETTINGS_ACTION =
225 "com.android.settings.action.EXTRA_SETTINGS";
226
227 /**
228 * The key used to get the category from metadata of activities of action
229 * {@link #EXTRA_SETTINGS_ACTION}
230 * The value must be one of:
231 * <li>com.android.settings.category.wireless</li>
232 * <li>com.android.settings.category.device</li>
233 * <li>com.android.settings.category.personal</li>
234 * <li>com.android.settings.category.system</li>
235 */
236 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
237
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800238 private static boolean sShowNoHomeNotice = false;
239
240 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800241
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800242 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700243 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800244
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800245 // Show only these settings for restricted users
246 private int[] SETTINGS_FOR_RESTRICTED = {
247 R.id.wireless_section,
248 R.id.wifi_settings,
249 R.id.bluetooth_settings,
250 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700251 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800252 R.id.wireless_settings,
253 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400254 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800255 R.id.display_settings,
256 R.id.storage_settings,
257 R.id.application_settings,
258 R.id.battery_settings,
259 R.id.personal_section,
260 R.id.location_settings,
261 R.id.security_settings,
262 R.id.language_settings,
263 R.id.user_settings,
264 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800265 R.id.system_section,
266 R.id.date_time_settings,
267 R.id.about_settings,
268 R.id.accessibility_settings,
269 R.id.print_settings,
270 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800271 R.id.home_settings,
272 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 };
274
275 private static final String[] ENTRY_FRAGMENTS = {
276 WirelessSettings.class.getName(),
277 WifiSettings.class.getName(),
278 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700279 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800280 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700281 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800282 TetherSettings.class.getName(),
283 WifiP2pSettings.class.getName(),
284 VpnSettings.class.getName(),
285 DateTimeSettings.class.getName(),
286 LocalePicker.class.getName(),
287 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700288 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 SpellCheckersSettings.class.getName(),
290 UserDictionaryList.class.getName(),
291 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700292 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 DisplaySettings.class.getName(),
294 DeviceInfoSettings.class.getName(),
295 ManageApplications.class.getName(),
296 ProcessStatsUi.class.getName(),
297 NotificationStation.class.getName(),
298 LocationSettings.class.getName(),
299 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700300 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 PrivacySettings.class.getName(),
302 DeviceAdminSettings.class.getName(),
303 AccessibilitySettings.class.getName(),
304 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
306 TextToSpeechSettings.class.getName(),
307 Memory.class.getName(),
308 DevelopmentSettings.class.getName(),
309 UsbSettings.class.getName(),
310 AndroidBeam.class.getName(),
311 WifiDisplaySettings.class.getName(),
312 PowerUsageSummary.class.getName(),
313 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000314 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800315 CryptKeeperSettings.class.getName(),
316 DataUsageSummary.class.getName(),
317 DreamSettings.class.getName(),
318 UserSettings.class.getName(),
319 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400320 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 PrintSettingsFragment.class.getName(),
322 PrintJobSettingsFragment.class.getName(),
323 TrustedCredentialsSettings.class.getName(),
324 PaymentSettings.class.getName(),
325 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700326 ZenModeSettings.class.getName(),
327 NotificationSettings.class.getName(),
328 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
329 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400330 InstalledAppDetails.class.getName(),
331 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700332 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700333 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000334 ApnSettings.class.getName(),
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800335 WifiCallingSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800336 };
337
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700338
339 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
340 "android.settings.APPLICATION_DETAILS_SETTINGS"
341 };
342
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343 private SharedPreferences mDevelopmentPreferences;
344 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
345
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 private boolean mBatteryPresent = true;
347 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 @Override
349 public void onReceive(Context context, Intent intent) {
350 String action = intent.getAction();
351 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
352 boolean batteryPresent = Utils.isBatteryPresent(intent);
353
354 if (mBatteryPresent != batteryPresent) {
355 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700356 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 }
358 }
359 }
360 };
361
Svetoslav990159a2014-04-14 17:14:59 -0700362 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
363 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700364
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700365 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700366 private SwitchBar mSwitchBar;
367
368 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700369
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700370 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700371 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700372
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700373 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700374 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700375
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700376 private ViewGroup mContent;
377
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700378 private SearchView mSearchView;
379 private MenuItem mSearchMenuItem;
380 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700381 private SearchResultsSummary mSearchResultsFragment;
382 private String mSearchQuery;
383
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700384 // Categories
385 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800386
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700387 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700388 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800389 private Handler mHandler = new Handler() {
390 @Override
391 public void handleMessage(Message msg) {
392 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700393 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700394 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
395 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700396 buildDashboardCategories(mCategories);
397 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800398 } break;
399 }
400 }
401 };
402
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700403 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700404 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700405
Jim Miller0698a212014-10-16 19:49:07 -0700406 private Intent mResultIntentData;
407
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700408 public SwitchBar getSwitchBar() {
409 return mSwitchBar;
410 }
411
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700412 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
413 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700414 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700415 }
416 return mCategories;
417 }
418
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800419 @Override
420 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
421 // Override the fragment title for Wallpaper settings
422 int titleRes = pref.getTitleRes();
423 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
424 titleRes = R.string.wallpaper_settings_fragment_title;
425 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
426 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
427 if (UserManager.get(this).isLinkedUser()) {
428 titleRes = R.string.profile_info_settings_title;
429 } else {
430 titleRes = R.string.user_info_settings_title;
431 }
432 }
433 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
434 null, 0);
435 return true;
436 }
437
438 @Override
439 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
440 return false;
441 }
442
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700443 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700444 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700445 Message msg = new Message();
446 msg.what = MSG_BUILD_CATEGORIES;
447 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800448 }
449 }
450
451 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800452 public void onConfigurationChanged(Configuration newConfig) {
453 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800454 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800455 }
456
457 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700458 protected void onStart() {
459 super.onStart();
460
461 if (mNeedToRevertToInitialFragment) {
462 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 }
465
466 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700467 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700468 if (!mDisplaySearch) {
469 return false;
470 }
471
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700472 MenuInflater inflater = getMenuInflater();
473 inflater.inflate(R.menu.options_menu, menu);
474
475 // Cache the search query (can be overriden by the OnQueryTextListener)
476 final String query = mSearchQuery;
477
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700478 mSearchMenuItem = menu.findItem(R.id.search);
479 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700480
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700481 if (mSearchMenuItem == null || mSearchView == null) {
482 return false;
483 }
484
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700485 if (mSearchResultsFragment != null) {
486 mSearchResultsFragment.setSearchView(mSearchView);
487 }
488
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700489 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700490 mSearchView.setOnQueryTextListener(this);
491 mSearchView.setOnCloseListener(this);
492
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700493 if (mSearchMenuItemExpanded) {
494 mSearchMenuItem.expandActionView();
495 }
496 mSearchView.setQuery(query, true /* submit */);
497
498 return true;
499 }
500
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700501 private static boolean isShortCutIntent(final Intent intent) {
502 Set<String> categories = intent.getCategories();
503 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
504 }
505
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700506 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700507 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700508 if (action == null) {
509 return false;
510 }
511 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
512 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
513 }
514 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700515 }
516
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700517 @Override
518 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700519 super.onCreate(savedState);
520
521 // Should happen before any call to getIntent()
522 getMetaData();
523
524 final Intent intent = getIntent();
525 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
526 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800528
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800529 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
530 Context.MODE_PRIVATE);
531
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700532 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700533 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700534
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700535 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700536 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
537
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700538 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700539 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700540
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700541 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700542
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700543 // This is a "Sub Settings" when:
544 // - this is a real SubSettings
545 // - or :settings:show_fragment_as_subsetting is passed to the Intent
546 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
547 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
548
549 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
550 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700551 // Check also that we are not a Theme Dialog as we don't want to override them
552 final int themeResId = getThemeResId();
553 if (themeResId != R.style.Theme_DialogWhenLarge &&
554 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
555 setTheme(R.style.Theme_SubSettings);
556 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700557 }
558
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700559 setContentView(mIsShowingDashboard ?
560 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800561
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700562 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700563
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800564 getFragmentManager().addOnBackStackChangedListener(this);
565
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700566 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800567 // Run the Index update only if we have some space
568 if (!Utils.isLowStorage(this)) {
569 Index.getInstance(getApplicationContext()).update();
570 } else {
571 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
572 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700573 }
574
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700575 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700576 // We are restarting from a previous saved state; used that to initialize, instead
577 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700578 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
579 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700581 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800582
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700583 ArrayList<DashboardCategory> categories =
584 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
585 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700586 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700587 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700588 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800589 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700590
591 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700592 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700593 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
594 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700596 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700597 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
598 // only if it is a sub settings
599 if (mIsShortcut) {
600 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700601 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700602 } else if (isSubSettings) {
603 mDisplayHomeAsUpEnabled = true;
604 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700605 } else {
606 mDisplayHomeAsUpEnabled = false;
607 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700608 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700609 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700610
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700611 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700612 switchToFragment(initialFragmentName, initialArguments, true, false,
613 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000614 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700615 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700616 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700617 // Show Search affordance
618 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700619 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700620 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700621 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800622 }
623 }
624
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700625 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700626 if (mActionBar != null) {
627 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
628 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
629 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700630 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
631
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800632 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800633 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
634
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700635 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800636 if (buttonBar != null) {
637 buttonBar.setVisibility(View.VISIBLE);
638
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700639 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800640 backButton.setOnClickListener(new OnClickListener() {
641 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700642 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800643 finish();
644 }
645 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700646 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 skipButton.setOnClickListener(new OnClickListener() {
648 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700649 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 finish();
651 }
652 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700653 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800654 mNextButton.setOnClickListener(new OnClickListener() {
655 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700656 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 finish();
658 }
659 });
660
661 // set our various button parameters
662 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
663 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
664 if (TextUtils.isEmpty(buttonText)) {
665 mNextButton.setVisibility(View.GONE);
666 }
667 else {
668 mNextButton.setText(buttonText);
669 }
670 }
671 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
672 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
673 if (TextUtils.isEmpty(buttonText)) {
674 backButton.setVisibility(View.GONE);
675 }
676 else {
677 backButton.setText(buttonText);
678 }
679 }
680 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
681 skipButton.setVisibility(View.VISIBLE);
682 }
683 }
684 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700685
686 mHomeActivitiesCount = getHomeActivitiesCount();
687 }
688
689 private int getHomeActivitiesCount() {
690 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
691 getPackageManager().getHomeActivities(homeApps);
692 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800693 }
694
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700695 private void setTitleFromIntent(Intent intent) {
696 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
697 if (initialTitleResId > 0) {
698 mInitialTitle = null;
699 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100700
701 final String initialTitleResPackageName = intent.getStringExtra(
702 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
703 if (initialTitleResPackageName != null) {
704 try {
705 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
706 0 /* flags */, new UserHandle(UserHandle.myUserId()));
707 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
708 setTitle(mInitialTitle);
709 mInitialTitleResId = -1;
710 return;
711 } catch (NameNotFoundException e) {
712 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
713 }
714 } else {
715 setTitle(mInitialTitleResId);
716 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700717 } else {
718 mInitialTitleResId = -1;
719 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
720 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
721 setTitle(mInitialTitle);
722 }
723 }
724
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800725 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800726 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700727 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800728 }
729
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700730 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800731 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700732
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800733 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700734 if (mInitialTitleResId > 0) {
735 setTitle(mInitialTitleResId);
736 } else {
737 setTitle(mInitialTitle);
738 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700739 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800740 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700741
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800742 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
743 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700744
745 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800746 }
747
748 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
749 final CharSequence title;
750 final int titleRes = bse.getBreadCrumbTitleRes();
751 if (titleRes > 0) {
752 title = getText(titleRes);
753 } else {
754 title = bse.getBreadCrumbTitle();
755 }
756 if (title != null) {
757 setTitle(title);
758 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800759 }
760
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800761 @Override
762 protected void onSaveInstanceState(Bundle outState) {
763 super.onSaveInstanceState(outState);
764
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700765 if (mCategories.size() > 0) {
766 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800767 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700768
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700769 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700770 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700771
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700772 if (mDisplaySearch) {
773 // The option menus are created if the ActionBar is visible and they are also created
774 // asynchronously. If you launch Settings with an Intent action like
775 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
776 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
777 // menu item and search view are null.
778 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
779 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700780
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700781 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
782 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
783 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700784
785 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800786 }
787
788 @Override
789 public void onResume() {
790 super.onResume();
Chris Wren8a963ba2015-03-20 10:29:14 -0400791 if (mIsShowingDashboard) {
792 MetricsLogger.visible(this, MetricsLogger.MAIN_SETTINGS);
793 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800794
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700795 final int newHomeActivityCount = getHomeActivitiesCount();
796 if (newHomeActivityCount != mHomeActivitiesCount) {
797 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700798 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700799 }
800
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800801 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
802 @Override
803 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700804 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800805 }
806 };
807 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
808 mDevelopmentPreferencesListener);
809
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800810 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700811
Svetoslav990159a2014-04-14 17:14:59 -0700812 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700813
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700814 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700815 onQueryTextSubmit(mSearchQuery);
816 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800817 }
818
819 @Override
820 public void onPause() {
821 super.onPause();
Chris Wren8a963ba2015-03-20 10:29:14 -0400822 if (mIsShowingDashboard) {
823 MetricsLogger.hidden(this, MetricsLogger.MAIN_SETTINGS);
824 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800825 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700826 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800827 }
828
829 @Override
830 public void onDestroy() {
831 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700832
833 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
834 mDevelopmentPreferencesListener);
835 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800836 }
837
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800838 protected boolean isValidFragment(String fragmentName) {
839 // Almost all fragments are wrapped in this,
840 // except for a few that have their own activities.
841 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
842 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
843 }
844 return false;
845 }
846
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800847 @Override
848 public Intent getIntent() {
849 Intent superIntent = super.getIntent();
850 String startingFragment = getStartingFragmentClass(superIntent);
851 // This is called from super.onCreate, isMultiPane() is not yet reliable
852 // Do not use onIsHidingHeaders either, which relies itself on this method
853 if (startingFragment != null) {
854 Intent modIntent = new Intent(superIntent);
855 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
856 Bundle args = superIntent.getExtras();
857 if (args != null) {
858 args = new Bundle(args);
859 } else {
860 args = new Bundle();
861 }
862 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100863 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800864 return modIntent;
865 }
866 return superIntent;
867 }
868
869 /**
870 * Checks if the component name in the intent is different from the Settings class and
871 * returns the class name to load as a fragment.
872 */
873 private String getStartingFragmentClass(Intent intent) {
874 if (mFragmentClass != null) return mFragmentClass;
875
876 String intentClass = intent.getComponent().getClassName();
877 if (intentClass.equals(getClass().getName())) return null;
878
879 if ("com.android.settings.ManageApplications".equals(intentClass)
880 || "com.android.settings.RunningServices".equals(intentClass)
881 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
882 // Old names of manage apps.
883 intentClass = com.android.settings.applications.ManageApplications.class.getName();
884 }
885
886 return intentClass;
887 }
888
889 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000890 * Start a new fragment containing a preference panel. If the preferences
891 * are being displayed in multi-pane mode, the given fragment class will
892 * be instantiated and placed in the appropriate pane. If running in
893 * single-pane mode, a new activity will be launched in which to show the
894 * fragment.
895 *
896 * @param fragmentClass Full name of the class implementing the fragment.
897 * @param args Any desired arguments to supply to the fragment.
898 * @param titleRes Optional resource identifier of the title of this
899 * fragment.
900 * @param titleText Optional text of the title of this fragment.
901 * @param resultTo Optional fragment that result data should be sent to.
902 * If non-null, resultTo.onActivityResult() will be called when this
903 * preference panel is done. The launched panel must use
904 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
905 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700906 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000907 */
908 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700909 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700910 String title = null;
911 if (titleRes < 0) {
912 if (titleText != null) {
913 title = titleText.toString();
914 } else {
915 // There not much we can do in that case
916 title = "";
917 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700918 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700919 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700920 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000921 }
922
923 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100924 * Start a new fragment in a new activity containing a preference panel for a given user. If the
925 * preferences are being displayed in multi-pane mode, the given fragment class will be
926 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
927 * activity will be launched in which to show the fragment.
928 *
929 * @param fragmentClass Full name of the class implementing the fragment.
930 * @param args Any desired arguments to supply to the fragment.
931 * @param titleRes Optional resource identifier of the title of this fragment.
932 * @param titleText Optional text of the title of this fragment.
933 * @param userHandle The user for which the panel has to be started.
934 */
935 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
936 CharSequence titleText, UserHandle userHandle) {
937 String title = null;
938 if (titleRes < 0) {
939 if (titleText != null) {
940 title = titleText.toString();
941 } else {
942 // There not much we can do in that case
943 title = "";
944 }
945 }
946 Utils.startWithFragmentAsUser(this, fragmentClass, args,
947 titleRes, title, mIsShortcut, userHandle);
948 }
949
950 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800951 * Called by a preference panel fragment to finish itself.
952 *
953 * @param caller The fragment that is asking to be finished.
954 * @param resultCode Optional result code to send back to the original
955 * launching fragment.
956 * @param resultData Optional result data to send back to the original
957 * launching fragment.
958 */
959 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
960 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700961 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800962 }
963
964 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000965 * Start a new fragment.
966 *
967 * @param fragment The fragment to start
968 * @param push If true, the current fragment will be pushed onto the back stack. If false,
969 * the current fragment will be replaced.
970 */
971 public void startPreferenceFragment(Fragment fragment, boolean push) {
972 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700973 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000974 if (push) {
975 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
976 transaction.addToBackStack(BACK_STACK_PREFS);
977 } else {
978 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
979 }
980 transaction.commitAllowingStateLoss();
981 }
982
983 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700984 * Switch to a specific Fragment with taking care of validation, Title and BackStack
985 */
986 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700987 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700988 if (validate && !isValidFragment(fragmentName)) {
989 throw new IllegalArgumentException("Invalid fragment for this activity: "
990 + fragmentName);
991 }
992 Fragment f = Fragment.instantiate(this, fragmentName, args);
993 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700994 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700995 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700996 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700997 }
998 if (addToBackStack) {
999 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
1000 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001001 if (titleResId > 0) {
1002 transaction.setBreadCrumbTitle(titleResId);
1003 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001004 transaction.setBreadCrumbTitle(title);
1005 }
1006 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001007 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001008 return f;
1009 }
1010
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001011 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001012 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001013 *
1014 * @param categories The list in which to place the tiles categories.
1015 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001016 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001017 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001018 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1019 updateTilesList(categories);
1020 }
1021
1022 /**
1023 * Parse the given XML file as a categories description, adding each
1024 * parsed categories and tiles into the target list.
1025 *
1026 * @param resid The XML resource to load and parse.
1027 * @param target The list in which the parsed categories and tiles should be placed.
1028 */
1029 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1030 XmlResourceParser parser = null;
1031 try {
1032 parser = getResources().getXml(resid);
1033 AttributeSet attrs = Xml.asAttributeSet(parser);
1034
1035 int type;
1036 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1037 && type != XmlPullParser.START_TAG) {
1038 // Parse next until start tag is found
1039 }
1040
1041 String nodeName = parser.getName();
1042 if (!"dashboard-categories".equals(nodeName)) {
1043 throw new RuntimeException(
1044 "XML document must start with <preference-categories> tag; found"
1045 + nodeName + " at " + parser.getPositionDescription());
1046 }
1047
1048 Bundle curBundle = null;
1049
1050 final int outerDepth = parser.getDepth();
1051 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1052 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1053 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1054 continue;
1055 }
1056
1057 nodeName = parser.getName();
1058 if ("dashboard-category".equals(nodeName)) {
1059 DashboardCategory category = new DashboardCategory();
1060
1061 TypedArray sa = obtainStyledAttributes(
1062 attrs, com.android.internal.R.styleable.PreferenceHeader);
1063 category.id = sa.getResourceId(
1064 com.android.internal.R.styleable.PreferenceHeader_id,
1065 (int)DashboardCategory.CAT_ID_UNDEFINED);
1066
1067 TypedValue tv = sa.peekValue(
1068 com.android.internal.R.styleable.PreferenceHeader_title);
1069 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1070 if (tv.resourceId != 0) {
1071 category.titleRes = tv.resourceId;
1072 } else {
1073 category.title = tv.string;
1074 }
1075 }
1076 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001077 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1078 tv = sa.peekValue(
1079 com.android.internal.R.styleable.Preference_key);
1080 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1081 if (tv.resourceId != 0) {
1082 category.key = getString(tv.resourceId);
1083 } else {
1084 category.key = tv.string.toString();
1085 }
1086 }
1087 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001088
1089 final int innerDepth = parser.getDepth();
1090 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1091 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1092 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1093 continue;
1094 }
1095
1096 String innerNodeName = parser.getName();
1097 if (innerNodeName.equals("dashboard-tile")) {
1098 DashboardTile tile = new DashboardTile();
1099
1100 sa = obtainStyledAttributes(
1101 attrs, com.android.internal.R.styleable.PreferenceHeader);
1102 tile.id = sa.getResourceId(
1103 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001104 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001105 tv = sa.peekValue(
1106 com.android.internal.R.styleable.PreferenceHeader_title);
1107 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1108 if (tv.resourceId != 0) {
1109 tile.titleRes = tv.resourceId;
1110 } else {
1111 tile.title = tv.string;
1112 }
1113 }
1114 tv = sa.peekValue(
1115 com.android.internal.R.styleable.PreferenceHeader_summary);
1116 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1117 if (tv.resourceId != 0) {
1118 tile.summaryRes = tv.resourceId;
1119 } else {
1120 tile.summary = tv.string;
1121 }
1122 }
1123 tile.iconRes = sa.getResourceId(
1124 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1125 tile.fragment = sa.getString(
1126 com.android.internal.R.styleable.PreferenceHeader_fragment);
1127 sa.recycle();
1128
1129 if (curBundle == null) {
1130 curBundle = new Bundle();
1131 }
1132
1133 final int innerDepth2 = parser.getDepth();
1134 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1135 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1136 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1137 continue;
1138 }
1139
1140 String innerNodeName2 = parser.getName();
1141 if (innerNodeName2.equals("extra")) {
1142 getResources().parseBundleExtra("extra", attrs, curBundle);
1143 XmlUtils.skipCurrentTag(parser);
1144
1145 } else if (innerNodeName2.equals("intent")) {
1146 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1147
1148 } else {
1149 XmlUtils.skipCurrentTag(parser);
1150 }
1151 }
1152
1153 if (curBundle.size() > 0) {
1154 tile.fragmentArguments = curBundle;
1155 curBundle = null;
1156 }
1157
PauloftheWest38155612014-06-30 10:02:36 -07001158 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001159 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001160 category.addTile(tile);
1161 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001162
Jason Monk2ebc8a02015-02-13 15:23:19 -05001163 } else if (innerNodeName.equals("external-tiles")) {
1164 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001165 } else {
1166 XmlUtils.skipCurrentTag(parser);
1167 }
1168 }
1169
1170 target.add(category);
1171 } else {
1172 XmlUtils.skipCurrentTag(parser);
1173 }
1174 }
1175
1176 } catch (XmlPullParserException e) {
1177 throw new RuntimeException("Error parsing categories", e);
1178 } catch (IOException e) {
1179 throw new RuntimeException("Error parsing categories", e);
1180 } finally {
1181 if (parser != null) parser.close();
1182 }
1183 }
1184
1185 private void updateTilesList(List<DashboardCategory> target) {
1186 final boolean showDev = mDevelopmentPreferences.getBoolean(
1187 DevelopmentSettings.PREF_SHOW,
1188 android.os.Build.TYPE.equals("eng"));
1189
1190 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1191
1192 final int size = target.size();
1193 for (int i = 0; i < size; i++) {
1194
1195 DashboardCategory category = target.get(i);
1196
1197 // Ids are integers, so downcasting is ok
1198 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001199 int n = category.getTilesCount() - 1;
1200 while (n >= 0) {
1201
1202 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001203 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001204 id = (int) tile.id;
1205 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001206 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1207 removeTile = true;
1208 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001209 } else if (id == R.id.wifi_settings) {
1210 // Remove WiFi Settings if WiFi service is not available.
1211 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001212 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001213 }
1214 } else if (id == R.id.bluetooth_settings) {
1215 // Remove Bluetooth Settings if Bluetooth service is not available.
1216 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001217 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001218 }
1219 } else if (id == R.id.data_usage_settings) {
1220 // Remove data usage when kernel module not enabled
1221 final INetworkManagementService netManager = INetworkManagementService.Stub
1222 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1223 try {
1224 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001225 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001226 }
1227 } catch (RemoteException e) {
1228 // ignored
1229 }
1230 } else if (id == R.id.battery_settings) {
1231 // Remove battery settings when battery is not available. (e.g. TV)
1232
1233 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001234 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001235 }
1236 } else if (id == R.id.home_settings) {
1237 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001238 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001239 }
1240 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001241 boolean hasMultipleUsers =
1242 ((UserManager) getSystemService(Context.USER_SERVICE))
1243 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001244 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001245 || (!UserManager.supportsMultipleUsers()
1246 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001247 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001248 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001249 }
1250 } else if (id == R.id.nfc_payment_settings) {
1251 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001252 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001253 } else {
1254 // Only show if NFC is on and we have the HCE feature
1255 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001256 if (adapter == null || !adapter.isEnabled() ||
1257 !getPackageManager().hasSystemFeature(
1258 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001259 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001260 }
1261 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001262 } else if (id == R.id.print_settings) {
1263 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1264 PackageManager.FEATURE_PRINTING);
1265 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001266 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001267 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001268 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001269 if (!showDev || um.hasUserRestriction(
1270 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001271 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001272 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001273 }
1274
1275 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001276 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1277 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001278 }
1279
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001280 if (removeTile && n < category.getTilesCount()) {
1281 category.removeTile(n);
1282 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001283 n--;
1284 }
1285 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001286 addExternalTiles(target);
1287 }
1288
1289 private void addExternalTiles(List<DashboardCategory> target) {
1290 Map<Pair<String, String>, DashboardTile> addedCache =
1291 new ArrayMap<Pair<String, String>, DashboardTile>();
1292 UserManager userManager = UserManager.get(this);
1293 for (UserHandle user : userManager.getUserProfiles()) {
1294 addExternalTiles(target, user, addedCache);
1295 }
1296 }
1297
1298 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1299 Map<Pair<String, String>, DashboardTile> addedCache) {
1300 PackageManager pm = getPackageManager();
1301 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1302 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1303 PackageManager.GET_META_DATA, user.getIdentifier());
1304 for (ResolveInfo resolved : results) {
1305 if (!resolved.system) {
1306 // Do not allow any app to add to settings, only system ones.
1307 continue;
1308 }
1309 ActivityInfo activityInfo = resolved.activityInfo;
1310 Bundle metaData = activityInfo.metaData;
1311 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1312 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1313 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1314 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1315 continue;
1316 }
1317 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1318 DashboardCategory category = getCategory(target, categoryKey);
1319 if (category == null) {
1320 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1321 + "category key " + categoryKey);
1322 continue;
1323 }
1324 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1325 activityInfo.name);
1326 DashboardTile tile = addedCache.get(key);
1327 if (tile == null) {
1328 tile = new DashboardTile();
1329 tile.intent = new Intent().setClassName(
1330 activityInfo.packageName, activityInfo.name);
1331 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1332
1333 if (category.externalIndex == -1) {
1334 // If no location for external tiles has been specified for this category,
1335 // then just put them at the end.
1336 category.addTile(tile);
1337 } else {
1338 category.addTile(category.externalIndex, tile);
1339 }
1340 addedCache.put(key, tile);
1341 }
1342 tile.userHandle.add(user);
1343 }
1344 }
1345
1346 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1347 for (DashboardCategory category : target) {
1348 if (categoryKey.equals(category.key)) {
1349 return category;
1350 }
1351 }
1352 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001353 }
1354
1355 private boolean updateHomeSettingTiles(DashboardTile tile) {
1356 // Once we decide to show Home settings, keep showing it forever
1357 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1358 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1359 return true;
1360 }
1361
1362 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001363 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001364 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001365 // When there's only one available home app, omit this settings
1366 // category entirely at the top level UI. If the user just
1367 // uninstalled the penultimate home app candidiate, we also
1368 // now tell them about why they aren't seeing 'Home' in the list.
1369 if (sShowNoHomeNotice) {
1370 sShowNoHomeNotice = false;
1371 NoHomeDialogFragment.show(this);
1372 }
1373 return false;
1374 } else {
1375 // Okay, we're allowing the Home settings category. Tell it, when
1376 // invoked via this front door, that we'll need to be told about the
1377 // case when the user uninstalls all but one home app.
1378 if (tile.fragmentArguments == null) {
1379 tile.fragmentArguments = new Bundle();
1380 }
1381 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1382 }
1383 } catch (Exception e) {
1384 // Can't look up the home activity; bail on configuring the icon
1385 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1386 }
1387
1388 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1389 return true;
1390 }
1391
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001392 private void getMetaData() {
1393 try {
1394 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1395 PackageManager.GET_META_DATA);
1396 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001397 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1398 } catch (NameNotFoundException nnfe) {
1399 // No recovery
1400 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1401 }
1402 }
1403
1404 // give subclasses access to the Next button
1405 public boolean hasNextButton() {
1406 return mNextButton != null;
1407 }
1408
1409 public Button getNextButton() {
1410 return mNextButton;
1411 }
1412
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001413 @Override
1414 public boolean shouldUpRecreateTask(Intent targetIntent) {
1415 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1416 }
1417
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001418 public static void requestHomeNotice() {
1419 sShowNoHomeNotice = true;
1420 }
1421
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001422 @Override
1423 public boolean onQueryTextSubmit(String query) {
1424 switchToSearchResultsFragmentIfNeeded();
1425 mSearchQuery = query;
1426 return mSearchResultsFragment.onQueryTextSubmit(query);
1427 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001428
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001429 @Override
1430 public boolean onQueryTextChange(String newText) {
1431 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001432 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001433 return false;
1434 }
1435 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001436 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001437
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001438 @Override
1439 public boolean onClose() {
1440 return false;
1441 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001442
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001443 @Override
1444 public boolean onMenuItemActionExpand(MenuItem item) {
1445 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001446 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001447 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001448 return true;
1449 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001450
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001451 @Override
1452 public boolean onMenuItemActionCollapse(MenuItem item) {
1453 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001454 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001455 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001456 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001457 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001458 return true;
1459 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001460
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001461 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001462 if (mSearchResultsFragment != null) {
1463 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001464 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001465 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001466 if (current != null && current instanceof SearchResultsSummary) {
1467 mSearchResultsFragment = (SearchResultsSummary) current;
1468 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001469 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001470 SearchResultsSummary.class.getName(), null, false, true,
1471 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001472 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001473 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001474 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001476
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001477 public void needToRevertToInitialFragment() {
1478 mNeedToRevertToInitialFragment = true;
1479 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001480
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001481 private void revertToInitialFragment() {
1482 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001483 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001484 mSearchMenuItemExpanded = false;
1485 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1486 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001487 if (mSearchMenuItem != null) {
1488 mSearchMenuItem.collapseActionView();
1489 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001490 }
Jim Miller0698a212014-10-16 19:49:07 -07001491
1492 public Intent getResultIntentData() {
1493 return mResultIntentData;
1494 }
1495
1496 public void setResultIntentData(Intent resultIntentData) {
1497 mResultIntentData = resultIntentData;
1498 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001499}