blob: eecf7a207b498dd9433a64f17878347347fd31de [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
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080069import com.android.internal.util.ArrayUtils;
70import com.android.internal.util.XmlUtils;
71import com.android.settings.accessibility.AccessibilitySettings;
72import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000073import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070075import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import com.android.settings.applications.ManageApplications;
77import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070079import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070081import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070082import com.android.settings.dashboard.NoHomeDialogFragment;
83import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084import com.android.settings.deviceinfo.Memory;
85import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040086import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087import com.android.settings.fuelgauge.PowerUsageSummary;
88import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
89import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
90import com.android.settings.inputmethod.SpellCheckersSettings;
91import com.android.settings.inputmethod.UserDictionaryList;
92import com.android.settings.location.LocationSettings;
93import com.android.settings.nfc.AndroidBeam;
94import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040095import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040096import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040097import com.android.settings.notification.NotificationAccessSettings;
98import com.android.settings.notification.NotificationSettings;
99import com.android.settings.notification.NotificationStation;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500100import com.android.settings.notification.OtherSoundSettings;
John Spurlock4a350512014-04-08 14:08:21 -0400101import 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;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500104import com.android.settings.search.DynamicIndexableContentMonitor;
105import com.android.settings.search.Index;
PauloftheWest38155612014-06-30 10:02:36 -0700106import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800107import com.android.settings.tts.TextToSpeechSettings;
108import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700109import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110import com.android.settings.vpn2.VpnSettings;
111import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700112import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700114import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800115import com.android.settings.wifi.WifiSettings;
116import com.android.settings.wifi.p2p.WifiP2pSettings;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500117
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118import org.xmlpull.v1.XmlPullParser;
119import org.xmlpull.v1.XmlPullParserException;
120
121import java.io.IOException;
122import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800123import java.util.List;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500124import java.util.Map;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700125import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800126
127public class SettingsActivity extends Activity
128 implements PreferenceManager.OnPreferenceTreeClickListener,
129 PreferenceFragment.OnPreferenceStartFragmentCallback,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100130 ButtonBarHandler, FragmentManager.OnBackStackChangedListener,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700131 SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Alexandra Gherghinacfc7f9d2014-07-04 12:25:38 +0100132 MenuItem.OnActionExpandListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800133
134 private static final String LOG_TAG = "Settings";
135
136 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700137 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 private static final String SAVE_KEY_SEARCH_MENU_EXPANDED = ":settings:search_menu_expanded";
139 private static final String SAVE_KEY_SEARCH_QUERY = ":settings:search_query";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700140 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700141 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700142 private static final String SAVE_KEY_HOME_ACTIVITIES_COUNT = ":settings:home_activities_count";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800143
144 /**
145 * When starting this activity, the invoking Intent can contain this extra
146 * string to specify which fragment should be initially displayed.
147 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
148 * will call isValidFragment() to confirm that the fragment class name is valid for this
149 * activity.
150 */
151 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
152
153 /**
154 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
155 * this extra can also be specified to supply a Bundle of arguments to pass
156 * to that fragment when it is instantiated during the initial creation
157 * of the activity.
158 */
159 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
160
161 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700162 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
163 */
164 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
165
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800166 public static final String BACK_STACK_PREFS = ":settings:prefs";
167
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 // extras that allow any preference activity to be launched as part of a wizard
169
170 // show Back and Next buttons? takes boolean parameter
171 // Back will then return RESULT_CANCELED and Next RESULT_OK
172 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
173
174 // add a Skip button?
175 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
176
177 // specify custom text for the Back or Next buttons, or cause a button to not appear
178 // at all by setting it to null
179 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
180 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
181
182 /**
183 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700184 * 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 -0800185 * that fragment.
186 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700187 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100188 /**
189 * The package name used to resolve the title resource id.
190 */
191 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
192 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700193 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
194 ":settings:show_fragment_title_resid";
195 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
196 ":settings:show_fragment_as_shortcut";
197
198 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
199 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201 private static final String META_DATA_KEY_FRAGMENT_CLASS =
202 "com.android.settings.FRAGMENT_CLASS";
203
204 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
205
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700206 private static final String EMPTY_QUERY = "";
207
Jason Monk2ebc8a02015-02-13 15:23:19 -0500208 /**
209 * Settings will search for system activities of this action and add them as a top level
210 * settings tile using the following parameters.
211 *
212 * <p>A category must be specified in the meta-data for the activity named
213 * {@link #EXTRA_CATEGORY_KEY}
214 *
215 * <p>The title may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_TITLE}
216 * otherwise the label for the activity will be used.
217 *
218 * <p>The icon may be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_ICON}
219 * otherwise the icon for the activity will be used.
220 *
221 * <p>A summary my be defined by meta-data named {@link Utils#META_DATA_PREFERENCE_SUMMARY}
222 */
223 private static final String EXTRA_SETTINGS_ACTION =
224 "com.android.settings.action.EXTRA_SETTINGS";
225
226 /**
227 * The key used to get the category from metadata of activities of action
228 * {@link #EXTRA_SETTINGS_ACTION}
229 * The value must be one of:
230 * <li>com.android.settings.category.wireless</li>
231 * <li>com.android.settings.category.device</li>
232 * <li>com.android.settings.category.personal</li>
233 * <li>com.android.settings.category.system</li>
234 */
235 private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
236
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800237 private static boolean sShowNoHomeNotice = false;
238
239 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800241 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700242 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800243
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244 // Show only these settings for restricted users
245 private int[] SETTINGS_FOR_RESTRICTED = {
246 R.id.wireless_section,
247 R.id.wifi_settings,
248 R.id.bluetooth_settings,
249 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700250 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800251 R.id.wireless_settings,
252 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400253 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800254 R.id.display_settings,
255 R.id.storage_settings,
256 R.id.application_settings,
257 R.id.battery_settings,
258 R.id.personal_section,
259 R.id.location_settings,
260 R.id.security_settings,
261 R.id.language_settings,
262 R.id.user_settings,
263 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800264 R.id.system_section,
265 R.id.date_time_settings,
266 R.id.about_settings,
267 R.id.accessibility_settings,
268 R.id.print_settings,
269 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800270 R.id.home_settings,
271 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800272 };
273
274 private static final String[] ENTRY_FRAGMENTS = {
275 WirelessSettings.class.getName(),
276 WifiSettings.class.getName(),
277 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700278 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700280 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800281 TetherSettings.class.getName(),
282 WifiP2pSettings.class.getName(),
283 VpnSettings.class.getName(),
284 DateTimeSettings.class.getName(),
285 LocalePicker.class.getName(),
286 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700287 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 SpellCheckersSettings.class.getName(),
289 UserDictionaryList.class.getName(),
290 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700291 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800292 DisplaySettings.class.getName(),
293 DeviceInfoSettings.class.getName(),
294 ManageApplications.class.getName(),
295 ProcessStatsUi.class.getName(),
296 NotificationStation.class.getName(),
297 LocationSettings.class.getName(),
298 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700299 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800300 PrivacySettings.class.getName(),
301 DeviceAdminSettings.class.getName(),
302 AccessibilitySettings.class.getName(),
303 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
305 TextToSpeechSettings.class.getName(),
306 Memory.class.getName(),
307 DevelopmentSettings.class.getName(),
308 UsbSettings.class.getName(),
309 AndroidBeam.class.getName(),
310 WifiDisplaySettings.class.getName(),
311 PowerUsageSummary.class.getName(),
312 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000313 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314 CryptKeeperSettings.class.getName(),
315 DataUsageSummary.class.getName(),
316 DreamSettings.class.getName(),
317 UserSettings.class.getName(),
318 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400319 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 PrintSettingsFragment.class.getName(),
321 PrintJobSettingsFragment.class.getName(),
322 TrustedCredentialsSettings.class.getName(),
323 PaymentSettings.class.getName(),
324 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700325 ZenModeSettings.class.getName(),
326 NotificationSettings.class.getName(),
327 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
328 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400329 InstalledAppDetails.class.getName(),
330 BatterySaverSettings.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700331 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700332 OtherSoundSettings.class.getName(),
Narayan Kamath33acb152015-03-16 12:48:05 +0000333 ApnSettings.class.getName(),
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800334 WifiCallingSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800335 };
336
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700337
338 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
339 "android.settings.APPLICATION_DETAILS_SETTINGS"
340 };
341
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 private SharedPreferences mDevelopmentPreferences;
343 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
344
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 private boolean mBatteryPresent = true;
346 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 @Override
348 public void onReceive(Context context, Intent intent) {
349 String action = intent.getAction();
350 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
351 boolean batteryPresent = Utils.isBatteryPresent(intent);
352
353 if (mBatteryPresent != batteryPresent) {
354 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700355 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 }
357 }
358 }
359 };
360
Svetoslav990159a2014-04-14 17:14:59 -0700361 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
362 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700363
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700364 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700365 private SwitchBar mSwitchBar;
366
367 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700368
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700369 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700370 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700371
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700372 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700373 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700374
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700375 private ViewGroup mContent;
376
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700377 private SearchView mSearchView;
378 private MenuItem mSearchMenuItem;
379 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700380 private SearchResultsSummary mSearchResultsFragment;
381 private String mSearchQuery;
382
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700383 // Categories
384 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800385
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700386 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700387 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800388 private Handler mHandler = new Handler() {
389 @Override
390 public void handleMessage(Message msg) {
391 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700392 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700393 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
394 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700395 buildDashboardCategories(mCategories);
396 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800397 } break;
398 }
399 }
400 };
401
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700402 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700403 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700404
Jim Miller0698a212014-10-16 19:49:07 -0700405 private Intent mResultIntentData;
406
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700407 public SwitchBar getSwitchBar() {
408 return mSwitchBar;
409 }
410
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700411 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
412 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700413 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700414 }
415 return mCategories;
416 }
417
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800418 @Override
419 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
420 // Override the fragment title for Wallpaper settings
421 int titleRes = pref.getTitleRes();
422 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
423 titleRes = R.string.wallpaper_settings_fragment_title;
424 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
425 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
426 if (UserManager.get(this).isLinkedUser()) {
427 titleRes = R.string.profile_info_settings_title;
428 } else {
429 titleRes = R.string.user_info_settings_title;
430 }
431 }
432 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
433 null, 0);
434 return true;
435 }
436
437 @Override
438 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
439 return false;
440 }
441
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700442 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700443 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700444 Message msg = new Message();
445 msg.what = MSG_BUILD_CATEGORIES;
446 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800447 }
448 }
449
450 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800451 public void onConfigurationChanged(Configuration newConfig) {
452 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800453 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800454 }
455
456 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700457 protected void onStart() {
458 super.onStart();
459
460 if (mNeedToRevertToInitialFragment) {
461 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800462 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463 }
464
465 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700466 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700467 if (!mDisplaySearch) {
468 return false;
469 }
470
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700471 MenuInflater inflater = getMenuInflater();
472 inflater.inflate(R.menu.options_menu, menu);
473
474 // Cache the search query (can be overriden by the OnQueryTextListener)
475 final String query = mSearchQuery;
476
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700477 mSearchMenuItem = menu.findItem(R.id.search);
478 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700479
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700480 if (mSearchMenuItem == null || mSearchView == null) {
481 return false;
482 }
483
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700484 if (mSearchResultsFragment != null) {
485 mSearchResultsFragment.setSearchView(mSearchView);
486 }
487
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700488 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700489 mSearchView.setOnQueryTextListener(this);
490 mSearchView.setOnCloseListener(this);
491
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700492 if (mSearchMenuItemExpanded) {
493 mSearchMenuItem.expandActionView();
494 }
495 mSearchView.setQuery(query, true /* submit */);
496
497 return true;
498 }
499
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700500 private static boolean isShortCutIntent(final Intent intent) {
501 Set<String> categories = intent.getCategories();
502 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
503 }
504
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700505 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700506 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700507 if (action == null) {
508 return false;
509 }
510 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
511 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
512 }
513 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700514 }
515
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700516 @Override
517 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700518 super.onCreate(savedState);
519
520 // Should happen before any call to getIntent()
521 getMetaData();
522
523 final Intent intent = getIntent();
524 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
525 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800526 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800528 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
529 Context.MODE_PRIVATE);
530
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700531 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700532 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700533
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700534 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700535 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
536
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700537 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700538 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700539
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700540 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700541
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700542 // This is a "Sub Settings" when:
543 // - this is a real SubSettings
544 // - or :settings:show_fragment_as_subsetting is passed to the Intent
545 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
546 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
547
548 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
549 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700550 // Check also that we are not a Theme Dialog as we don't want to override them
551 final int themeResId = getThemeResId();
552 if (themeResId != R.style.Theme_DialogWhenLarge &&
553 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
554 setTheme(R.style.Theme_SubSettings);
555 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700556 }
557
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700558 setContentView(mIsShowingDashboard ?
559 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700561 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700562
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800563 getFragmentManager().addOnBackStackChangedListener(this);
564
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700565 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800566 // Run the Index update only if we have some space
567 if (!Utils.isLowStorage(this)) {
568 Index.getInstance(getApplicationContext()).update();
569 } else {
570 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
571 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700572 }
573
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700574 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700575 // We are restarting from a previous saved state; used that to initialize, instead
576 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700577 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
578 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800579
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700580 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800581
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700582 ArrayList<DashboardCategory> categories =
583 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
584 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700585 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700586 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700587 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700589
590 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700591 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700592 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
593 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700595 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700596 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
597 // only if it is a sub settings
598 if (mIsShortcut) {
599 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700600 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700601 } else if (isSubSettings) {
602 mDisplayHomeAsUpEnabled = true;
603 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700604 } else {
605 mDisplayHomeAsUpEnabled = false;
606 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700607 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700608 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700609
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700610 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700611 switchToFragment(initialFragmentName, initialArguments, true, false,
612 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000613 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700614 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700615 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700616 // Show Search affordance
617 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700618 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700619 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700620 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800621 }
622 }
623
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700624 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700625 if (mActionBar != null) {
626 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
627 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
628 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700629 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
630
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800631 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800632 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
633
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700634 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800635 if (buttonBar != null) {
636 buttonBar.setVisibility(View.VISIBLE);
637
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700638 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800639 backButton.setOnClickListener(new OnClickListener() {
640 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700641 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 finish();
643 }
644 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700645 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 skipButton.setOnClickListener(new OnClickListener() {
647 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700648 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 finish();
650 }
651 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700652 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 mNextButton.setOnClickListener(new OnClickListener() {
654 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700655 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 finish();
657 }
658 });
659
660 // set our various button parameters
661 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
662 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
663 if (TextUtils.isEmpty(buttonText)) {
664 mNextButton.setVisibility(View.GONE);
665 }
666 else {
667 mNextButton.setText(buttonText);
668 }
669 }
670 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
671 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
672 if (TextUtils.isEmpty(buttonText)) {
673 backButton.setVisibility(View.GONE);
674 }
675 else {
676 backButton.setText(buttonText);
677 }
678 }
679 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
680 skipButton.setVisibility(View.VISIBLE);
681 }
682 }
683 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700684
685 mHomeActivitiesCount = getHomeActivitiesCount();
686 }
687
688 private int getHomeActivitiesCount() {
689 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
690 getPackageManager().getHomeActivities(homeApps);
691 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800692 }
693
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700694 private void setTitleFromIntent(Intent intent) {
695 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
696 if (initialTitleResId > 0) {
697 mInitialTitle = null;
698 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100699
700 final String initialTitleResPackageName = intent.getStringExtra(
701 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
702 if (initialTitleResPackageName != null) {
703 try {
704 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
705 0 /* flags */, new UserHandle(UserHandle.myUserId()));
706 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
707 setTitle(mInitialTitle);
708 mInitialTitleResId = -1;
709 return;
710 } catch (NameNotFoundException e) {
711 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
712 }
713 } else {
714 setTitle(mInitialTitleResId);
715 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700716 } else {
717 mInitialTitleResId = -1;
718 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
719 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
720 setTitle(mInitialTitle);
721 }
722 }
723
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800724 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800725 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700726 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800727 }
728
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700729 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800730 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700731
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800732 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700733 if (mInitialTitleResId > 0) {
734 setTitle(mInitialTitleResId);
735 } else {
736 setTitle(mInitialTitle);
737 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700738 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800739 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700740
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800741 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
742 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700743
744 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800745 }
746
747 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
748 final CharSequence title;
749 final int titleRes = bse.getBreadCrumbTitleRes();
750 if (titleRes > 0) {
751 title = getText(titleRes);
752 } else {
753 title = bse.getBreadCrumbTitle();
754 }
755 if (title != null) {
756 setTitle(title);
757 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800758 }
759
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800760 @Override
761 protected void onSaveInstanceState(Bundle outState) {
762 super.onSaveInstanceState(outState);
763
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700764 if (mCategories.size() > 0) {
765 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800766 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700767
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700768 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700769 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700770
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700771 if (mDisplaySearch) {
772 // The option menus are created if the ActionBar is visible and they are also created
773 // asynchronously. If you launch Settings with an Intent action like
774 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
775 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
776 // menu item and search view are null.
777 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
778 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700779
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700780 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
781 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
782 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700783
784 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800785 }
786
787 @Override
788 public void onResume() {
789 super.onResume();
790
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700791 final int newHomeActivityCount = getHomeActivitiesCount();
792 if (newHomeActivityCount != mHomeActivitiesCount) {
793 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700794 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700795 }
796
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800797 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
798 @Override
799 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700800 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800801 }
802 };
803 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
804 mDevelopmentPreferencesListener);
805
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800806 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700807
Svetoslav990159a2014-04-14 17:14:59 -0700808 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700809
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700810 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700811 onQueryTextSubmit(mSearchQuery);
812 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800813 }
814
815 @Override
816 public void onPause() {
817 super.onPause();
818
819 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700820 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800821 }
822
823 @Override
824 public void onDestroy() {
825 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700826
827 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
828 mDevelopmentPreferencesListener);
829 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800830 }
831
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800832 protected boolean isValidFragment(String fragmentName) {
833 // Almost all fragments are wrapped in this,
834 // except for a few that have their own activities.
835 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
836 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
837 }
838 return false;
839 }
840
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800841 @Override
842 public Intent getIntent() {
843 Intent superIntent = super.getIntent();
844 String startingFragment = getStartingFragmentClass(superIntent);
845 // This is called from super.onCreate, isMultiPane() is not yet reliable
846 // Do not use onIsHidingHeaders either, which relies itself on this method
847 if (startingFragment != null) {
848 Intent modIntent = new Intent(superIntent);
849 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
850 Bundle args = superIntent.getExtras();
851 if (args != null) {
852 args = new Bundle(args);
853 } else {
854 args = new Bundle();
855 }
856 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100857 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800858 return modIntent;
859 }
860 return superIntent;
861 }
862
863 /**
864 * Checks if the component name in the intent is different from the Settings class and
865 * returns the class name to load as a fragment.
866 */
867 private String getStartingFragmentClass(Intent intent) {
868 if (mFragmentClass != null) return mFragmentClass;
869
870 String intentClass = intent.getComponent().getClassName();
871 if (intentClass.equals(getClass().getName())) return null;
872
873 if ("com.android.settings.ManageApplications".equals(intentClass)
874 || "com.android.settings.RunningServices".equals(intentClass)
875 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
876 // Old names of manage apps.
877 intentClass = com.android.settings.applications.ManageApplications.class.getName();
878 }
879
880 return intentClass;
881 }
882
883 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000884 * Start a new fragment containing a preference panel. If the preferences
885 * are being displayed in multi-pane mode, the given fragment class will
886 * be instantiated and placed in the appropriate pane. If running in
887 * single-pane mode, a new activity will be launched in which to show the
888 * fragment.
889 *
890 * @param fragmentClass Full name of the class implementing the fragment.
891 * @param args Any desired arguments to supply to the fragment.
892 * @param titleRes Optional resource identifier of the title of this
893 * fragment.
894 * @param titleText Optional text of the title of this fragment.
895 * @param resultTo Optional fragment that result data should be sent to.
896 * If non-null, resultTo.onActivityResult() will be called when this
897 * preference panel is done. The launched panel must use
898 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
899 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700900 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000901 */
902 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700903 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700904 String title = null;
905 if (titleRes < 0) {
906 if (titleText != null) {
907 title = titleText.toString();
908 } else {
909 // There not much we can do in that case
910 title = "";
911 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700912 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700913 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700914 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000915 }
916
917 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100918 * Start a new fragment in a new activity containing a preference panel for a given user. If the
919 * preferences are being displayed in multi-pane mode, the given fragment class will be
920 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
921 * activity will be launched in which to show the fragment.
922 *
923 * @param fragmentClass Full name of the class implementing the fragment.
924 * @param args Any desired arguments to supply to the fragment.
925 * @param titleRes Optional resource identifier of the title of this fragment.
926 * @param titleText Optional text of the title of this fragment.
927 * @param userHandle The user for which the panel has to be started.
928 */
929 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
930 CharSequence titleText, UserHandle userHandle) {
931 String title = null;
932 if (titleRes < 0) {
933 if (titleText != null) {
934 title = titleText.toString();
935 } else {
936 // There not much we can do in that case
937 title = "";
938 }
939 }
940 Utils.startWithFragmentAsUser(this, fragmentClass, args,
941 titleRes, title, mIsShortcut, userHandle);
942 }
943
944 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800945 * Called by a preference panel fragment to finish itself.
946 *
947 * @param caller The fragment that is asking to be finished.
948 * @param resultCode Optional result code to send back to the original
949 * launching fragment.
950 * @param resultData Optional result data to send back to the original
951 * launching fragment.
952 */
953 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
954 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700955 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800956 }
957
958 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000959 * Start a new fragment.
960 *
961 * @param fragment The fragment to start
962 * @param push If true, the current fragment will be pushed onto the back stack. If false,
963 * the current fragment will be replaced.
964 */
965 public void startPreferenceFragment(Fragment fragment, boolean push) {
966 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700967 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000968 if (push) {
969 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
970 transaction.addToBackStack(BACK_STACK_PREFS);
971 } else {
972 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
973 }
974 transaction.commitAllowingStateLoss();
975 }
976
977 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700978 * Switch to a specific Fragment with taking care of validation, Title and BackStack
979 */
980 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700981 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700982 if (validate && !isValidFragment(fragmentName)) {
983 throw new IllegalArgumentException("Invalid fragment for this activity: "
984 + fragmentName);
985 }
986 Fragment f = Fragment.instantiate(this, fragmentName, args);
987 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700988 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700989 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700990 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700991 }
992 if (addToBackStack) {
993 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
994 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700995 if (titleResId > 0) {
996 transaction.setBreadCrumbTitle(titleResId);
997 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700998 transaction.setBreadCrumbTitle(title);
999 }
1000 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -07001001 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001002 return f;
1003 }
1004
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001005 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001006 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001007 *
1008 * @param categories The list in which to place the tiles categories.
1009 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -07001010 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -07001011 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001012 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
1013 updateTilesList(categories);
1014 }
1015
1016 /**
1017 * Parse the given XML file as a categories description, adding each
1018 * parsed categories and tiles into the target list.
1019 *
1020 * @param resid The XML resource to load and parse.
1021 * @param target The list in which the parsed categories and tiles should be placed.
1022 */
1023 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
1024 XmlResourceParser parser = null;
1025 try {
1026 parser = getResources().getXml(resid);
1027 AttributeSet attrs = Xml.asAttributeSet(parser);
1028
1029 int type;
1030 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1031 && type != XmlPullParser.START_TAG) {
1032 // Parse next until start tag is found
1033 }
1034
1035 String nodeName = parser.getName();
1036 if (!"dashboard-categories".equals(nodeName)) {
1037 throw new RuntimeException(
1038 "XML document must start with <preference-categories> tag; found"
1039 + nodeName + " at " + parser.getPositionDescription());
1040 }
1041
1042 Bundle curBundle = null;
1043
1044 final int outerDepth = parser.getDepth();
1045 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1046 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1047 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1048 continue;
1049 }
1050
1051 nodeName = parser.getName();
1052 if ("dashboard-category".equals(nodeName)) {
1053 DashboardCategory category = new DashboardCategory();
1054
1055 TypedArray sa = obtainStyledAttributes(
1056 attrs, com.android.internal.R.styleable.PreferenceHeader);
1057 category.id = sa.getResourceId(
1058 com.android.internal.R.styleable.PreferenceHeader_id,
1059 (int)DashboardCategory.CAT_ID_UNDEFINED);
1060
1061 TypedValue tv = sa.peekValue(
1062 com.android.internal.R.styleable.PreferenceHeader_title);
1063 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1064 if (tv.resourceId != 0) {
1065 category.titleRes = tv.resourceId;
1066 } else {
1067 category.title = tv.string;
1068 }
1069 }
1070 sa.recycle();
Jason Monk2ebc8a02015-02-13 15:23:19 -05001071 sa = obtainStyledAttributes(attrs, com.android.internal.R.styleable.Preference);
1072 tv = sa.peekValue(
1073 com.android.internal.R.styleable.Preference_key);
1074 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1075 if (tv.resourceId != 0) {
1076 category.key = getString(tv.resourceId);
1077 } else {
1078 category.key = tv.string.toString();
1079 }
1080 }
1081 sa.recycle();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001082
1083 final int innerDepth = parser.getDepth();
1084 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1085 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1086 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1087 continue;
1088 }
1089
1090 String innerNodeName = parser.getName();
1091 if (innerNodeName.equals("dashboard-tile")) {
1092 DashboardTile tile = new DashboardTile();
1093
1094 sa = obtainStyledAttributes(
1095 attrs, com.android.internal.R.styleable.PreferenceHeader);
1096 tile.id = sa.getResourceId(
1097 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001098 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001099 tv = sa.peekValue(
1100 com.android.internal.R.styleable.PreferenceHeader_title);
1101 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1102 if (tv.resourceId != 0) {
1103 tile.titleRes = tv.resourceId;
1104 } else {
1105 tile.title = tv.string;
1106 }
1107 }
1108 tv = sa.peekValue(
1109 com.android.internal.R.styleable.PreferenceHeader_summary);
1110 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1111 if (tv.resourceId != 0) {
1112 tile.summaryRes = tv.resourceId;
1113 } else {
1114 tile.summary = tv.string;
1115 }
1116 }
1117 tile.iconRes = sa.getResourceId(
1118 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1119 tile.fragment = sa.getString(
1120 com.android.internal.R.styleable.PreferenceHeader_fragment);
1121 sa.recycle();
1122
1123 if (curBundle == null) {
1124 curBundle = new Bundle();
1125 }
1126
1127 final int innerDepth2 = parser.getDepth();
1128 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1129 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1130 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1131 continue;
1132 }
1133
1134 String innerNodeName2 = parser.getName();
1135 if (innerNodeName2.equals("extra")) {
1136 getResources().parseBundleExtra("extra", attrs, curBundle);
1137 XmlUtils.skipCurrentTag(parser);
1138
1139 } else if (innerNodeName2.equals("intent")) {
1140 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1141
1142 } else {
1143 XmlUtils.skipCurrentTag(parser);
1144 }
1145 }
1146
1147 if (curBundle.size() > 0) {
1148 tile.fragmentArguments = curBundle;
1149 curBundle = null;
1150 }
1151
PauloftheWest38155612014-06-30 10:02:36 -07001152 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001153 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001154 category.addTile(tile);
1155 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001156
Jason Monk2ebc8a02015-02-13 15:23:19 -05001157 } else if (innerNodeName.equals("external-tiles")) {
1158 category.externalIndex = category.getTilesCount();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001159 } else {
1160 XmlUtils.skipCurrentTag(parser);
1161 }
1162 }
1163
1164 target.add(category);
1165 } else {
1166 XmlUtils.skipCurrentTag(parser);
1167 }
1168 }
1169
1170 } catch (XmlPullParserException e) {
1171 throw new RuntimeException("Error parsing categories", e);
1172 } catch (IOException e) {
1173 throw new RuntimeException("Error parsing categories", e);
1174 } finally {
1175 if (parser != null) parser.close();
1176 }
1177 }
1178
1179 private void updateTilesList(List<DashboardCategory> target) {
1180 final boolean showDev = mDevelopmentPreferences.getBoolean(
1181 DevelopmentSettings.PREF_SHOW,
1182 android.os.Build.TYPE.equals("eng"));
1183
1184 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1185
1186 final int size = target.size();
1187 for (int i = 0; i < size; i++) {
1188
1189 DashboardCategory category = target.get(i);
1190
1191 // Ids are integers, so downcasting is ok
1192 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001193 int n = category.getTilesCount() - 1;
1194 while (n >= 0) {
1195
1196 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001197 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001198 id = (int) tile.id;
1199 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001200 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1201 removeTile = true;
1202 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001203 } else if (id == R.id.wifi_settings) {
1204 // Remove WiFi Settings if WiFi service is not available.
1205 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001206 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001207 }
1208 } else if (id == R.id.bluetooth_settings) {
1209 // Remove Bluetooth Settings if Bluetooth service is not available.
1210 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001211 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001212 }
1213 } else if (id == R.id.data_usage_settings) {
1214 // Remove data usage when kernel module not enabled
1215 final INetworkManagementService netManager = INetworkManagementService.Stub
1216 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1217 try {
1218 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001219 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001220 }
1221 } catch (RemoteException e) {
1222 // ignored
1223 }
1224 } else if (id == R.id.battery_settings) {
1225 // Remove battery settings when battery is not available. (e.g. TV)
1226
1227 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001228 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001229 }
1230 } else if (id == R.id.home_settings) {
1231 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001232 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001233 }
1234 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001235 boolean hasMultipleUsers =
1236 ((UserManager) getSystemService(Context.USER_SERVICE))
1237 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001238 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001239 || (!UserManager.supportsMultipleUsers()
1240 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001241 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001242 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001243 }
1244 } else if (id == R.id.nfc_payment_settings) {
1245 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001246 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001247 } else {
1248 // Only show if NFC is on and we have the HCE feature
1249 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001250 if (adapter == null || !adapter.isEnabled() ||
1251 !getPackageManager().hasSystemFeature(
1252 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001253 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001254 }
1255 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001256 } else if (id == R.id.print_settings) {
1257 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1258 PackageManager.FEATURE_PRINTING);
1259 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001260 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001261 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001262 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001263 if (!showDev || um.hasUserRestriction(
1264 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001265 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001266 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001267 }
1268
1269 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001270 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1271 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001272 }
1273
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001274 if (removeTile && n < category.getTilesCount()) {
1275 category.removeTile(n);
1276 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001277 n--;
1278 }
1279 }
Jason Monk2ebc8a02015-02-13 15:23:19 -05001280 addExternalTiles(target);
1281 }
1282
1283 private void addExternalTiles(List<DashboardCategory> target) {
1284 Map<Pair<String, String>, DashboardTile> addedCache =
1285 new ArrayMap<Pair<String, String>, DashboardTile>();
1286 UserManager userManager = UserManager.get(this);
1287 for (UserHandle user : userManager.getUserProfiles()) {
1288 addExternalTiles(target, user, addedCache);
1289 }
1290 }
1291
1292 private void addExternalTiles(List<DashboardCategory> target, UserHandle user,
1293 Map<Pair<String, String>, DashboardTile> addedCache) {
1294 PackageManager pm = getPackageManager();
1295 Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
1296 List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
1297 PackageManager.GET_META_DATA, user.getIdentifier());
1298 for (ResolveInfo resolved : results) {
1299 if (!resolved.system) {
1300 // Do not allow any app to add to settings, only system ones.
1301 continue;
1302 }
1303 ActivityInfo activityInfo = resolved.activityInfo;
1304 Bundle metaData = activityInfo.metaData;
1305 if ((metaData == null) || !metaData.containsKey(EXTRA_CATEGORY_KEY)) {
1306 Log.w(LOG_TAG, "Found " + resolved.activityInfo.name + " for action "
1307 + EXTRA_SETTINGS_ACTION + " missing metadata " +
1308 (metaData == null ? "" : EXTRA_CATEGORY_KEY));
1309 continue;
1310 }
1311 String categoryKey = metaData.getString(EXTRA_CATEGORY_KEY);
1312 DashboardCategory category = getCategory(target, categoryKey);
1313 if (category == null) {
1314 Log.w(LOG_TAG, "Activity " + resolved.activityInfo.name + " has unknown "
1315 + "category key " + categoryKey);
1316 continue;
1317 }
1318 Pair<String, String> key = new Pair<String, String>(activityInfo.packageName,
1319 activityInfo.name);
1320 DashboardTile tile = addedCache.get(key);
1321 if (tile == null) {
1322 tile = new DashboardTile();
1323 tile.intent = new Intent().setClassName(
1324 activityInfo.packageName, activityInfo.name);
1325 Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile);
1326
1327 if (category.externalIndex == -1) {
1328 // If no location for external tiles has been specified for this category,
1329 // then just put them at the end.
1330 category.addTile(tile);
1331 } else {
1332 category.addTile(category.externalIndex, tile);
1333 }
1334 addedCache.put(key, tile);
1335 }
1336 tile.userHandle.add(user);
1337 }
1338 }
1339
1340 private DashboardCategory getCategory(List<DashboardCategory> target, String categoryKey) {
1341 for (DashboardCategory category : target) {
1342 if (categoryKey.equals(category.key)) {
1343 return category;
1344 }
1345 }
1346 return null;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001347 }
1348
1349 private boolean updateHomeSettingTiles(DashboardTile tile) {
1350 // Once we decide to show Home settings, keep showing it forever
1351 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1352 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1353 return true;
1354 }
1355
1356 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001357 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001358 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001359 // When there's only one available home app, omit this settings
1360 // category entirely at the top level UI. If the user just
1361 // uninstalled the penultimate home app candidiate, we also
1362 // now tell them about why they aren't seeing 'Home' in the list.
1363 if (sShowNoHomeNotice) {
1364 sShowNoHomeNotice = false;
1365 NoHomeDialogFragment.show(this);
1366 }
1367 return false;
1368 } else {
1369 // Okay, we're allowing the Home settings category. Tell it, when
1370 // invoked via this front door, that we'll need to be told about the
1371 // case when the user uninstalls all but one home app.
1372 if (tile.fragmentArguments == null) {
1373 tile.fragmentArguments = new Bundle();
1374 }
1375 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1376 }
1377 } catch (Exception e) {
1378 // Can't look up the home activity; bail on configuring the icon
1379 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1380 }
1381
1382 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1383 return true;
1384 }
1385
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001386 private void getMetaData() {
1387 try {
1388 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1389 PackageManager.GET_META_DATA);
1390 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001391 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1392 } catch (NameNotFoundException nnfe) {
1393 // No recovery
1394 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1395 }
1396 }
1397
1398 // give subclasses access to the Next button
1399 public boolean hasNextButton() {
1400 return mNextButton != null;
1401 }
1402
1403 public Button getNextButton() {
1404 return mNextButton;
1405 }
1406
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001407 @Override
1408 public boolean shouldUpRecreateTask(Intent targetIntent) {
1409 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1410 }
1411
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001412 public static void requestHomeNotice() {
1413 sShowNoHomeNotice = true;
1414 }
1415
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001416 @Override
1417 public boolean onQueryTextSubmit(String query) {
1418 switchToSearchResultsFragmentIfNeeded();
1419 mSearchQuery = query;
1420 return mSearchResultsFragment.onQueryTextSubmit(query);
1421 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001422
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001423 @Override
1424 public boolean onQueryTextChange(String newText) {
1425 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001426 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001427 return false;
1428 }
1429 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001430 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001431
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001432 @Override
1433 public boolean onClose() {
1434 return false;
1435 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001436
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001437 @Override
1438 public boolean onMenuItemActionExpand(MenuItem item) {
1439 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001440 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001441 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001442 return true;
1443 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001444
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001445 @Override
1446 public boolean onMenuItemActionCollapse(MenuItem item) {
1447 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001448 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001449 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001450 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001451 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001452 return true;
1453 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001454
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001455 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001456 if (mSearchResultsFragment != null) {
1457 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001458 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001459 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001460 if (current != null && current instanceof SearchResultsSummary) {
1461 mSearchResultsFragment = (SearchResultsSummary) current;
1462 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001463 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001464 SearchResultsSummary.class.getName(), null, false, true,
1465 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001466 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001467 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001468 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001469 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001470
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001471 public void needToRevertToInitialFragment() {
1472 mNeedToRevertToInitialFragment = true;
1473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001474
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001475 private void revertToInitialFragment() {
1476 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001477 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001478 mSearchMenuItemExpanded = false;
1479 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1480 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001481 if (mSearchMenuItem != null) {
1482 mSearchMenuItem.collapseActionView();
1483 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001484 }
Jim Miller0698a212014-10-16 19:49:07 -07001485
1486 public Intent getResultIntentData() {
1487 return mResultIntentData;
1488 }
1489
1490 public void setResultIntentData(Intent resultIntentData) {
1491 mResultIntentData = resultIntentData;
1492 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001493}