blob: 575fed455c1c288bd6262093b881e6dcbefe246d [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080019import android.app.ActionBar;
20import android.app.Activity;
21import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.pm.ResolveInfo;
34import android.content.res.Configuration;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080035import android.content.res.TypedArray;
36import android.content.res.XmlResourceParser;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.nfc.NfcAdapter;
38import android.os.Bundle;
39import android.os.Handler;
40import android.os.INetworkManagementService;
41import android.os.Message;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.UserHandle;
45import android.os.UserManager;
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -080046import android.os.storage.StorageManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.preference.Preference;
48import android.preference.PreferenceFragment;
49import android.preference.PreferenceManager;
50import android.preference.PreferenceScreen;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070052import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import android.util.AttributeSet;
54import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import android.util.TypedValue;
56import android.util.Xml;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070057import android.view.Menu;
58import android.view.MenuInflater;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080059import android.view.MenuItem;
60import android.view.View;
61import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070062import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080063import android.widget.Button;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070064import android.widget.SearchView;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040065
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080066import com.android.internal.util.ArrayUtils;
67import com.android.internal.util.XmlUtils;
68import com.android.settings.accessibility.AccessibilitySettings;
69import com.android.settings.accessibility.CaptionPropertiesFragment;
Alexandra Gherghina6d839872014-07-22 12:04:58 +000070import com.android.settings.accounts.AccountSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080071import com.android.settings.accounts.AccountSyncSettings;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070072import com.android.settings.applications.InstalledAppDetails;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import com.android.settings.applications.ManageApplications;
74import com.android.settings.applications.ProcessStatsUi;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import com.android.settings.bluetooth.BluetoothSettings;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070076import com.android.settings.dashboard.DashboardCategory;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import com.android.settings.dashboard.DashboardSummary;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -070078import com.android.settings.dashboard.DashboardTile;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070079import com.android.settings.dashboard.NoHomeDialogFragment;
80import com.android.settings.dashboard.SearchResultsSummary;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import com.android.settings.deviceinfo.Memory;
82import com.android.settings.deviceinfo.UsbSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -040083import com.android.settings.fuelgauge.BatterySaverSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080084import com.android.settings.fuelgauge.PowerUsageSummary;
John Spurlock802ddf92014-07-18 11:51:13 -040085import com.android.settings.notification.NotificationAppList;
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -070086import com.android.settings.notification.OtherSoundSettings;
Fabrice Di Meglio7a6bfd12014-04-14 19:49:18 -070087import com.android.settings.search.DynamicIndexableContentMonitor;
Fabrice Di Megliofa7dc242014-03-12 19:24:43 -070088import com.android.settings.search.Index;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
90import com.android.settings.inputmethod.KeyboardLayoutPickerFragment;
91import com.android.settings.inputmethod.SpellCheckersSettings;
92import com.android.settings.inputmethod.UserDictionaryList;
93import com.android.settings.location.LocationSettings;
94import com.android.settings.nfc.AndroidBeam;
95import com.android.settings.nfc.PaymentSettings;
John Spurlock802ddf92014-07-18 11:51:13 -040096import com.android.settings.notification.AppNotificationSettings;
John Spurlockc9afadb2014-04-29 18:07:23 -040097import com.android.settings.notification.ConditionProviderSettings;
John Spurlock4a350512014-04-08 14:08:21 -040098import com.android.settings.notification.NotificationAccessSettings;
99import com.android.settings.notification.NotificationSettings;
100import com.android.settings.notification.NotificationStation;
101import com.android.settings.notification.ZenModeSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800102import com.android.settings.print.PrintJobSettingsFragment;
103import com.android.settings.print.PrintSettingsFragment;
PauloftheWest38155612014-06-30 10:02:36 -0700104import com.android.settings.sim.SimSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800105import com.android.settings.tts.TextToSpeechSettings;
106import com.android.settings.users.UserSettings;
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700107import com.android.settings.voice.VoiceInputSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800108import com.android.settings.vpn2.VpnSettings;
109import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700110import com.android.settings.widget.SwitchBar;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800111import com.android.settings.wifi.AdvancedWifiSettings;
PauloftheWest7837b992014-06-24 07:42:27 -0700112import com.android.settings.wifi.SavedAccessPointsWifiSettings;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113import com.android.settings.wifi.WifiSettings;
114import com.android.settings.wifi.p2p.WifiP2pSettings;
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800115import com.android.settings.WifiCallingSettings;
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400116
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117import org.xmlpull.v1.XmlPullParser;
118import org.xmlpull.v1.XmlPullParserException;
119
120import java.io.IOException;
121import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800122import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700123import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800124
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -0700125import static com.android.settings.dashboard.DashboardTile.TILE_ID_UNDEFINED;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700126
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800127public 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
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208 private static boolean sShowNoHomeNotice = false;
209
210 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800212 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700213 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800214
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800215 // Show only these settings for restricted users
216 private int[] SETTINGS_FOR_RESTRICTED = {
217 R.id.wireless_section,
218 R.id.wifi_settings,
219 R.id.bluetooth_settings,
220 R.id.data_usage_settings,
PauloftheWest38155612014-06-30 10:02:36 -0700221 R.id.sim_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800222 R.id.wireless_settings,
223 R.id.device_section,
John Spurlock4e4cdef2014-05-28 09:43:45 -0400224 R.id.notification_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800225 R.id.display_settings,
226 R.id.storage_settings,
227 R.id.application_settings,
228 R.id.battery_settings,
229 R.id.personal_section,
230 R.id.location_settings,
231 R.id.security_settings,
232 R.id.language_settings,
233 R.id.user_settings,
234 R.id.account_settings,
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800235 R.id.system_section,
236 R.id.date_time_settings,
237 R.id.about_settings,
238 R.id.accessibility_settings,
239 R.id.print_settings,
240 R.id.nfc_payment_settings,
Fabrice Di Meglio2858b792014-02-18 19:35:08 -0800241 R.id.home_settings,
242 R.id.dashboard
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243 };
244
245 private static final String[] ENTRY_FRAGMENTS = {
246 WirelessSettings.class.getName(),
247 WifiSettings.class.getName(),
248 AdvancedWifiSettings.class.getName(),
PauloftheWest7837b992014-06-24 07:42:27 -0700249 SavedAccessPointsWifiSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800250 BluetoothSettings.class.getName(),
PauloftheWest38155612014-06-30 10:02:36 -0700251 SimSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800252 TetherSettings.class.getName(),
253 WifiP2pSettings.class.getName(),
254 VpnSettings.class.getName(),
255 DateTimeSettings.class.getName(),
256 LocalePicker.class.getName(),
257 InputMethodAndLanguageSettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700258 VoiceInputSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800259 SpellCheckersSettings.class.getName(),
260 UserDictionaryList.class.getName(),
261 UserDictionarySettings.class.getName(),
Amith Yamasani3033ce02014-07-14 18:03:15 -0700262 HomeSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800263 DisplaySettings.class.getName(),
264 DeviceInfoSettings.class.getName(),
265 ManageApplications.class.getName(),
266 ProcessStatsUi.class.getName(),
267 NotificationStation.class.getName(),
268 LocationSettings.class.getName(),
269 SecuritySettings.class.getName(),
Dianne Hackbornb51253c2014-08-12 15:08:04 -0700270 UsageAccessSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271 PrivacySettings.class.getName(),
272 DeviceAdminSettings.class.getName(),
273 AccessibilitySettings.class.getName(),
274 CaptionPropertiesFragment.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800275 com.android.settings.accessibility.ToggleDaltonizerPreferenceFragment.class.getName(),
276 TextToSpeechSettings.class.getName(),
277 Memory.class.getName(),
278 DevelopmentSettings.class.getName(),
279 UsbSettings.class.getName(),
280 AndroidBeam.class.getName(),
281 WifiDisplaySettings.class.getName(),
282 PowerUsageSummary.class.getName(),
283 AccountSyncSettings.class.getName(),
Alexandra Gherghina6d839872014-07-22 12:04:58 +0000284 AccountSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800285 CryptKeeperSettings.class.getName(),
286 DataUsageSummary.class.getName(),
287 DreamSettings.class.getName(),
288 UserSettings.class.getName(),
289 NotificationAccessSettings.class.getName(),
John Spurlockc9afadb2014-04-29 18:07:23 -0400290 ConditionProviderSettings.class.getName(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 PrintSettingsFragment.class.getName(),
292 PrintJobSettingsFragment.class.getName(),
293 TrustedCredentialsSettings.class.getName(),
294 PaymentSettings.class.getName(),
295 KeyboardLayoutPickerFragment.class.getName(),
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700296 ZenModeSettings.class.getName(),
297 NotificationSettings.class.getName(),
298 ChooseLockPassword.ChooseLockPasswordFragment.class.getName(),
299 ChooseLockPattern.ChooseLockPatternFragment.class.getName(),
John Spurlockc7f8e8c2014-06-04 19:11:22 -0400300 InstalledAppDetails.class.getName(),
301 BatterySaverSettings.class.getName(),
John Spurlock802ddf92014-07-18 11:51:13 -0400302 NotificationAppList.class.getName(),
Fabrice Di Megliodcf59dd2014-07-21 11:22:20 -0700303 AppNotificationSettings.class.getName(),
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700304 OtherSoundSettings.class.getName(),
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700305 ApnSettings.class.getName()
Fabrice Di Meglioe3ff4d82014-07-23 20:03:20 -0700306 QuickLaunchSettings.class.getName(),
Pavel Zhamaitsiak4ab32562015-02-23 15:34:07 -0800307 WifiCallingSettings.class.getName()
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 };
309
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700310
311 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
312 "android.settings.APPLICATION_DETAILS_SETTINGS"
313 };
314
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800315 private SharedPreferences mDevelopmentPreferences;
316 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
317
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 private boolean mBatteryPresent = true;
319 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 @Override
321 public void onReceive(Context context, Intent intent) {
322 String action = intent.getAction();
323 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
324 boolean batteryPresent = Utils.isBatteryPresent(intent);
325
326 if (mBatteryPresent != batteryPresent) {
327 mBatteryPresent = batteryPresent;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700328 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800329 }
330 }
331 }
332 };
333
Svetoslav990159a2014-04-14 17:14:59 -0700334 private final DynamicIndexableContentMonitor mDynamicIndexableContentMonitor =
335 new DynamicIndexableContentMonitor();
Svetoslav853e4712014-04-14 10:10:25 -0700336
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700337 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700338 private SwitchBar mSwitchBar;
339
340 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700341
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700342 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700343 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700344
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700345 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700346 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700347
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700348 private ViewGroup mContent;
349
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700350 private SearchView mSearchView;
351 private MenuItem mSearchMenuItem;
352 private boolean mSearchMenuItemExpanded = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700353 private SearchResultsSummary mSearchResultsFragment;
354 private String mSearchQuery;
355
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700356 // Categories
357 private ArrayList<DashboardCategory> mCategories = new ArrayList<DashboardCategory>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700359 private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700360 private static final int MSG_BUILD_CATEGORIES = 1;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800361 private Handler mHandler = new Handler() {
362 @Override
363 public void handleMessage(Message msg) {
364 switch (msg.what) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700365 case MSG_BUILD_CATEGORIES: {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700366 final boolean forceRefresh = msg.getData().getBoolean(MSG_DATA_FORCE_REFRESH);
367 if (forceRefresh) {
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700368 buildDashboardCategories(mCategories);
369 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 } break;
371 }
372 }
373 };
374
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700375 private boolean mNeedToRevertToInitialFragment = false;
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700376 private int mHomeActivitiesCount = 1;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700377
Jim Miller0698a212014-10-16 19:49:07 -0700378 private Intent mResultIntentData;
379
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700380 public SwitchBar getSwitchBar() {
381 return mSwitchBar;
382 }
383
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700384 public List<DashboardCategory> getDashboardCategories(boolean forceRefresh) {
385 if (forceRefresh || mCategories.size() == 0) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700386 buildDashboardCategories(mCategories);
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700387 }
388 return mCategories;
389 }
390
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800391 @Override
392 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
393 // Override the fragment title for Wallpaper settings
394 int titleRes = pref.getTitleRes();
395 if (pref.getFragment().equals(WallpaperTypeSettings.class.getName())) {
396 titleRes = R.string.wallpaper_settings_fragment_title;
397 } else if (pref.getFragment().equals(OwnerInfoSettings.class.getName())
398 && UserHandle.myUserId() != UserHandle.USER_OWNER) {
399 if (UserManager.get(this).isLinkedUser()) {
400 titleRes = R.string.profile_info_settings_title;
401 } else {
402 titleRes = R.string.user_info_settings_title;
403 }
404 }
405 startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes, pref.getTitle(),
406 null, 0);
407 return true;
408 }
409
410 @Override
411 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
412 return false;
413 }
414
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700415 private void invalidateCategories(boolean forceRefresh) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700416 if (!mHandler.hasMessages(MSG_BUILD_CATEGORIES)) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700417 Message msg = new Message();
418 msg.what = MSG_BUILD_CATEGORIES;
419 msg.getData().putBoolean(MSG_DATA_FORCE_REFRESH, forceRefresh);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800420 }
421 }
422
423 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800424 public void onConfigurationChanged(Configuration newConfig) {
425 super.onConfigurationChanged(newConfig);
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800426 Index.getInstance(this).update();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800427 }
428
429 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700430 protected void onStart() {
431 super.onStart();
432
433 if (mNeedToRevertToInitialFragment) {
434 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800435 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800436 }
437
438 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700439 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700440 if (!mDisplaySearch) {
441 return false;
442 }
443
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700444 MenuInflater inflater = getMenuInflater();
445 inflater.inflate(R.menu.options_menu, menu);
446
447 // Cache the search query (can be overriden by the OnQueryTextListener)
448 final String query = mSearchQuery;
449
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700450 mSearchMenuItem = menu.findItem(R.id.search);
451 mSearchView = (SearchView) mSearchMenuItem.getActionView();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700452
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -0700453 if (mSearchMenuItem == null || mSearchView == null) {
454 return false;
455 }
456
Fabrice Di Meglio8c3b0ce2014-05-12 18:54:32 -0700457 if (mSearchResultsFragment != null) {
458 mSearchResultsFragment.setSearchView(mSearchView);
459 }
460
Fabrice Di Meglio95937822014-03-31 19:46:42 -0700461 mSearchMenuItem.setOnActionExpandListener(this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700462 mSearchView.setOnQueryTextListener(this);
463 mSearchView.setOnCloseListener(this);
464
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700465 if (mSearchMenuItemExpanded) {
466 mSearchMenuItem.expandActionView();
467 }
468 mSearchView.setQuery(query, true /* submit */);
469
470 return true;
471 }
472
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700473 private static boolean isShortCutIntent(final Intent intent) {
474 Set<String> categories = intent.getCategories();
475 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
476 }
477
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700478 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700479 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700480 if (action == null) {
481 return false;
482 }
483 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
484 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
485 }
486 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700487 }
488
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700489 @Override
490 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700491 super.onCreate(savedState);
492
493 // Should happen before any call to getIntent()
494 getMetaData();
495
496 final Intent intent = getIntent();
497 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
498 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800500
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800501 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
502 Context.MODE_PRIVATE);
503
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700504 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700505 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700506
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700507 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700508 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
509
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700510 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700511 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700512
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700513 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700514
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700515 // This is a "Sub Settings" when:
516 // - this is a real SubSettings
517 // - or :settings:show_fragment_as_subsetting is passed to the Intent
518 final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
519 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
520
521 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
522 if (isSubSettings) {
Fabrice Di Meglio712df6c2014-07-18 17:44:37 -0700523 // Check also that we are not a Theme Dialog as we don't want to override them
524 final int themeResId = getThemeResId();
525 if (themeResId != R.style.Theme_DialogWhenLarge &&
526 themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
527 setTheme(R.style.Theme_SubSettings);
528 }
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700529 }
530
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700531 setContentView(mIsShowingDashboard ?
532 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700534 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700535
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 getFragmentManager().addOnBackStackChangedListener(this);
537
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700538 if (mIsShowingDashboard) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800539 // Run the Index update only if we have some space
540 if (!Utils.isLowStorage(this)) {
541 Index.getInstance(getApplicationContext()).update();
542 } else {
543 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
544 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700545 }
546
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700547 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700548 // We are restarting from a previous saved state; used that to initialize, instead
549 // of starting fresh.
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700550 mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
551 mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700553 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800554
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700555 ArrayList<DashboardCategory> categories =
556 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
557 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700558 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700559 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700560 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800561 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700562
563 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700564 mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700565 mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
566 1 /* one home activity by default */);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 } else {
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700568 if (!mIsShowingDashboard) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700569 // Search is shown we are launched thru a Settings "shortcut". UP will be shown
570 // only if it is a sub settings
571 if (mIsShortcut) {
572 mDisplayHomeAsUpEnabled = isSubSettings;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700573 mDisplaySearch = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700574 } else if (isSubSettings) {
575 mDisplayHomeAsUpEnabled = true;
576 mDisplaySearch = true;
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700577 } else {
578 mDisplayHomeAsUpEnabled = false;
579 mDisplaySearch = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700580 }
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700581 setTitleFromIntent(intent);
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700582
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700583 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700584 switchToFragment(initialFragmentName, initialArguments, true, false,
585 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000586 } else {
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700587 // No UP affordance if we are displaying the main Dashboard
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700588 mDisplayHomeAsUpEnabled = false;
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700589 // Show Search affordance
590 mDisplaySearch = true;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700591 mInitialTitleResId = R.string.dashboard_title;
Fabrice Di Meglio42c4b0a2014-05-22 10:30:03 -0700592 switchToFragment(DashboardSummary.class.getName(), null, false, false,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700593 mInitialTitleResId, mInitialTitle, false);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 }
595 }
596
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700597 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700598 if (mActionBar != null) {
599 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
600 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
601 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700602 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
603
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800605 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
606
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700607 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800608 if (buttonBar != null) {
609 buttonBar.setVisibility(View.VISIBLE);
610
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700611 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800612 backButton.setOnClickListener(new OnClickListener() {
613 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700614 setResult(RESULT_CANCELED, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800615 finish();
616 }
617 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700618 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800619 skipButton.setOnClickListener(new OnClickListener() {
620 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700621 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800622 finish();
623 }
624 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700625 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800626 mNextButton.setOnClickListener(new OnClickListener() {
627 public void onClick(View v) {
Jim Miller0698a212014-10-16 19:49:07 -0700628 setResult(RESULT_OK, getResultIntentData());
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800629 finish();
630 }
631 });
632
633 // set our various button parameters
634 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
635 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
636 if (TextUtils.isEmpty(buttonText)) {
637 mNextButton.setVisibility(View.GONE);
638 }
639 else {
640 mNextButton.setText(buttonText);
641 }
642 }
643 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
644 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
645 if (TextUtils.isEmpty(buttonText)) {
646 backButton.setVisibility(View.GONE);
647 }
648 else {
649 backButton.setText(buttonText);
650 }
651 }
652 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
653 skipButton.setVisibility(View.VISIBLE);
654 }
655 }
656 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700657
658 mHomeActivitiesCount = getHomeActivitiesCount();
659 }
660
661 private int getHomeActivitiesCount() {
662 final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
663 getPackageManager().getHomeActivities(homeApps);
664 return homeApps.size();
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800665 }
666
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700667 private void setTitleFromIntent(Intent intent) {
668 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
669 if (initialTitleResId > 0) {
670 mInitialTitle = null;
671 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100672
673 final String initialTitleResPackageName = intent.getStringExtra(
674 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
675 if (initialTitleResPackageName != null) {
676 try {
677 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
678 0 /* flags */, new UserHandle(UserHandle.myUserId()));
679 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
680 setTitle(mInitialTitle);
681 mInitialTitleResId = -1;
682 return;
683 } catch (NameNotFoundException e) {
684 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
685 }
686 } else {
687 setTitle(mInitialTitleResId);
688 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700689 } else {
690 mInitialTitleResId = -1;
691 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
692 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
693 setTitle(mInitialTitle);
694 }
695 }
696
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800697 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800698 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700699 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800700 }
701
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700702 private int setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800703 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700704
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800705 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700706 if (mInitialTitleResId > 0) {
707 setTitle(mInitialTitleResId);
708 } else {
709 setTitle(mInitialTitle);
710 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700711 return 0;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800712 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700713
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800714 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
715 setTitleFromBackStackEntry(bse);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700716
717 return count;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800718 }
719
720 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
721 final CharSequence title;
722 final int titleRes = bse.getBreadCrumbTitleRes();
723 if (titleRes > 0) {
724 title = getText(titleRes);
725 } else {
726 title = bse.getBreadCrumbTitle();
727 }
728 if (title != null) {
729 setTitle(title);
730 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800731 }
732
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 @Override
734 protected void onSaveInstanceState(Bundle outState) {
735 super.onSaveInstanceState(outState);
736
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700737 if (mCategories.size() > 0) {
738 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800739 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700740
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700741 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700742 outState.putBoolean(SAVE_KEY_SHOW_SEARCH, mDisplaySearch);
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700743
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700744 if (mDisplaySearch) {
745 // The option menus are created if the ActionBar is visible and they are also created
746 // asynchronously. If you launch Settings with an Intent action like
747 // android.intent.action.POWER_USAGE_SUMMARY and at the same time your device is locked
748 // thru a LockScreen, onCreateOptionsMenu() is not yet called and references to the search
749 // menu item and search view are null.
750 boolean isExpanded = (mSearchMenuItem != null) && mSearchMenuItem.isActionViewExpanded();
751 outState.putBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED, isExpanded);
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700752
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700753 String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
754 outState.putString(SAVE_KEY_SEARCH_QUERY, query);
755 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700756
757 outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800758 }
759
760 @Override
761 public void onResume() {
762 super.onResume();
763
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700764 final int newHomeActivityCount = getHomeActivitiesCount();
765 if (newHomeActivityCount != mHomeActivitiesCount) {
766 mHomeActivitiesCount = newHomeActivityCount;
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700767 invalidateCategories(true);
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700768 }
769
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800770 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
771 @Override
772 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -0700773 invalidateCategories(true);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800774 }
775 };
776 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
777 mDevelopmentPreferencesListener);
778
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800779 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Svetoslav853e4712014-04-14 10:10:25 -0700780
Svetoslav990159a2014-04-14 17:14:59 -0700781 mDynamicIndexableContentMonitor.register(this);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700782
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700783 if(mDisplaySearch && !TextUtils.isEmpty(mSearchQuery)) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700784 onQueryTextSubmit(mSearchQuery);
785 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800786 }
787
788 @Override
789 public void onPause() {
790 super.onPause();
791
792 unregisterReceiver(mBatteryInfoReceiver);
Svetoslav990159a2014-04-14 17:14:59 -0700793 mDynamicIndexableContentMonitor.unregister();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800794 }
795
796 @Override
797 public void onDestroy() {
798 super.onDestroy();
Fabrice Di Meglio680b0642014-05-20 15:19:29 -0700799
800 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
801 mDevelopmentPreferencesListener);
802 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800803 }
804
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800805 protected boolean isValidFragment(String fragmentName) {
806 // Almost all fragments are wrapped in this,
807 // except for a few that have their own activities.
808 for (int i = 0; i < ENTRY_FRAGMENTS.length; i++) {
809 if (ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
810 }
811 return false;
812 }
813
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800814 @Override
815 public Intent getIntent() {
816 Intent superIntent = super.getIntent();
817 String startingFragment = getStartingFragmentClass(superIntent);
818 // This is called from super.onCreate, isMultiPane() is not yet reliable
819 // Do not use onIsHidingHeaders either, which relies itself on this method
820 if (startingFragment != null) {
821 Intent modIntent = new Intent(superIntent);
822 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
823 Bundle args = superIntent.getExtras();
824 if (args != null) {
825 args = new Bundle(args);
826 } else {
827 args = new Bundle();
828 }
829 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100830 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800831 return modIntent;
832 }
833 return superIntent;
834 }
835
836 /**
837 * Checks if the component name in the intent is different from the Settings class and
838 * returns the class name to load as a fragment.
839 */
840 private String getStartingFragmentClass(Intent intent) {
841 if (mFragmentClass != null) return mFragmentClass;
842
843 String intentClass = intent.getComponent().getClassName();
844 if (intentClass.equals(getClass().getName())) return null;
845
846 if ("com.android.settings.ManageApplications".equals(intentClass)
847 || "com.android.settings.RunningServices".equals(intentClass)
848 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
849 // Old names of manage apps.
850 intentClass = com.android.settings.applications.ManageApplications.class.getName();
851 }
852
853 return intentClass;
854 }
855
856 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000857 * Start a new fragment containing a preference panel. If the preferences
858 * are being displayed in multi-pane mode, the given fragment class will
859 * be instantiated and placed in the appropriate pane. If running in
860 * single-pane mode, a new activity will be launched in which to show the
861 * fragment.
862 *
863 * @param fragmentClass Full name of the class implementing the fragment.
864 * @param args Any desired arguments to supply to the fragment.
865 * @param titleRes Optional resource identifier of the title of this
866 * fragment.
867 * @param titleText Optional text of the title of this fragment.
868 * @param resultTo Optional fragment that result data should be sent to.
869 * If non-null, resultTo.onActivityResult() will be called when this
870 * preference panel is done. The launched panel must use
871 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
872 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700873 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000874 */
875 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700876 CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700877 String title = null;
878 if (titleRes < 0) {
879 if (titleText != null) {
880 title = titleText.toString();
881 } else {
882 // There not much we can do in that case
883 title = "";
884 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700885 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700886 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700887 titleRes, title, mIsShortcut);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000888 }
889
890 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100891 * Start a new fragment in a new activity containing a preference panel for a given user. If the
892 * preferences are being displayed in multi-pane mode, the given fragment class will be
893 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
894 * activity will be launched in which to show the 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 fragment.
899 * @param titleText Optional text of the title of this fragment.
900 * @param userHandle The user for which the panel has to be started.
901 */
902 public void startPreferencePanelAsUser(String fragmentClass, Bundle args, int titleRes,
903 CharSequence titleText, UserHandle userHandle) {
904 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 }
912 }
913 Utils.startWithFragmentAsUser(this, fragmentClass, args,
914 titleRes, title, mIsShortcut, userHandle);
915 }
916
917 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800918 * Called by a preference panel fragment to finish itself.
919 *
920 * @param caller The fragment that is asking to be finished.
921 * @param resultCode Optional result code to send back to the original
922 * launching fragment.
923 * @param resultData Optional result data to send back to the original
924 * launching fragment.
925 */
926 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
927 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700928 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800929 }
930
931 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000932 * Start a new fragment.
933 *
934 * @param fragment The fragment to start
935 * @param push If true, the current fragment will be pushed onto the back stack. If false,
936 * the current fragment will be replaced.
937 */
938 public void startPreferenceFragment(Fragment fragment, boolean push) {
939 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700940 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000941 if (push) {
942 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
943 transaction.addToBackStack(BACK_STACK_PREFS);
944 } else {
945 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
946 }
947 transaction.commitAllowingStateLoss();
948 }
949
950 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700951 * Switch to a specific Fragment with taking care of validation, Title and BackStack
952 */
953 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700954 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700955 if (validate && !isValidFragment(fragmentName)) {
956 throw new IllegalArgumentException("Invalid fragment for this activity: "
957 + fragmentName);
958 }
959 Fragment f = Fragment.instantiate(this, fragmentName, args);
960 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700961 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700962 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700963 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700964 }
965 if (addToBackStack) {
966 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
967 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700968 if (titleResId > 0) {
969 transaction.setBreadCrumbTitle(titleResId);
970 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700971 transaction.setBreadCrumbTitle(title);
972 }
973 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700974 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700975 return f;
976 }
977
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700978 /**
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700979 * Called when the activity needs its list of categories/tiles built.
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700980 *
981 * @param categories The list in which to place the tiles categories.
982 */
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700983 private void buildDashboardCategories(List<DashboardCategory> categories) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700984 categories.clear();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700985 loadCategoriesFromResource(R.xml.dashboard_categories, categories);
986 updateTilesList(categories);
987 }
988
989 /**
990 * Parse the given XML file as a categories description, adding each
991 * parsed categories and tiles into the target list.
992 *
993 * @param resid The XML resource to load and parse.
994 * @param target The list in which the parsed categories and tiles should be placed.
995 */
996 private void loadCategoriesFromResource(int resid, List<DashboardCategory> target) {
997 XmlResourceParser parser = null;
998 try {
999 parser = getResources().getXml(resid);
1000 AttributeSet attrs = Xml.asAttributeSet(parser);
1001
1002 int type;
1003 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1004 && type != XmlPullParser.START_TAG) {
1005 // Parse next until start tag is found
1006 }
1007
1008 String nodeName = parser.getName();
1009 if (!"dashboard-categories".equals(nodeName)) {
1010 throw new RuntimeException(
1011 "XML document must start with <preference-categories> tag; found"
1012 + nodeName + " at " + parser.getPositionDescription());
1013 }
1014
1015 Bundle curBundle = null;
1016
1017 final int outerDepth = parser.getDepth();
1018 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1019 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1020 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1021 continue;
1022 }
1023
1024 nodeName = parser.getName();
1025 if ("dashboard-category".equals(nodeName)) {
1026 DashboardCategory category = new DashboardCategory();
1027
1028 TypedArray sa = obtainStyledAttributes(
1029 attrs, com.android.internal.R.styleable.PreferenceHeader);
1030 category.id = sa.getResourceId(
1031 com.android.internal.R.styleable.PreferenceHeader_id,
1032 (int)DashboardCategory.CAT_ID_UNDEFINED);
1033
1034 TypedValue tv = sa.peekValue(
1035 com.android.internal.R.styleable.PreferenceHeader_title);
1036 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1037 if (tv.resourceId != 0) {
1038 category.titleRes = tv.resourceId;
1039 } else {
1040 category.title = tv.string;
1041 }
1042 }
1043 sa.recycle();
1044
1045 final int innerDepth = parser.getDepth();
1046 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1047 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1048 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1049 continue;
1050 }
1051
1052 String innerNodeName = parser.getName();
1053 if (innerNodeName.equals("dashboard-tile")) {
1054 DashboardTile tile = new DashboardTile();
1055
1056 sa = obtainStyledAttributes(
1057 attrs, com.android.internal.R.styleable.PreferenceHeader);
1058 tile.id = sa.getResourceId(
1059 com.android.internal.R.styleable.PreferenceHeader_id,
Fabrice Di Meglioe9326d22014-05-13 12:49:14 -07001060 (int)TILE_ID_UNDEFINED);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001061 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 tile.titleRes = tv.resourceId;
1066 } else {
1067 tile.title = tv.string;
1068 }
1069 }
1070 tv = sa.peekValue(
1071 com.android.internal.R.styleable.PreferenceHeader_summary);
1072 if (tv != null && tv.type == TypedValue.TYPE_STRING) {
1073 if (tv.resourceId != 0) {
1074 tile.summaryRes = tv.resourceId;
1075 } else {
1076 tile.summary = tv.string;
1077 }
1078 }
1079 tile.iconRes = sa.getResourceId(
1080 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
1081 tile.fragment = sa.getString(
1082 com.android.internal.R.styleable.PreferenceHeader_fragment);
1083 sa.recycle();
1084
1085 if (curBundle == null) {
1086 curBundle = new Bundle();
1087 }
1088
1089 final int innerDepth2 = parser.getDepth();
1090 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1091 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth2)) {
1092 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1093 continue;
1094 }
1095
1096 String innerNodeName2 = parser.getName();
1097 if (innerNodeName2.equals("extra")) {
1098 getResources().parseBundleExtra("extra", attrs, curBundle);
1099 XmlUtils.skipCurrentTag(parser);
1100
1101 } else if (innerNodeName2.equals("intent")) {
1102 tile.intent = Intent.parseIntent(getResources(), parser, attrs);
1103
1104 } else {
1105 XmlUtils.skipCurrentTag(parser);
1106 }
1107 }
1108
1109 if (curBundle.size() > 0) {
1110 tile.fragmentArguments = curBundle;
1111 curBundle = null;
1112 }
1113
PauloftheWest38155612014-06-30 10:02:36 -07001114 // Show the SIM Cards setting if there are more than 2 SIMs installed.
Fabrice Di Meglio22a2a492014-08-08 12:27:57 -07001115 if(tile.id != R.id.sim_settings || Utils.showSimCardTile(this)){
PauloftheWest38155612014-06-30 10:02:36 -07001116 category.addTile(tile);
1117 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001118
1119 } else {
1120 XmlUtils.skipCurrentTag(parser);
1121 }
1122 }
1123
1124 target.add(category);
1125 } else {
1126 XmlUtils.skipCurrentTag(parser);
1127 }
1128 }
1129
1130 } catch (XmlPullParserException e) {
1131 throw new RuntimeException("Error parsing categories", e);
1132 } catch (IOException e) {
1133 throw new RuntimeException("Error parsing categories", e);
1134 } finally {
1135 if (parser != null) parser.close();
1136 }
1137 }
1138
1139 private void updateTilesList(List<DashboardCategory> target) {
1140 final boolean showDev = mDevelopmentPreferences.getBoolean(
1141 DevelopmentSettings.PREF_SHOW,
1142 android.os.Build.TYPE.equals("eng"));
1143
1144 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
1145
1146 final int size = target.size();
1147 for (int i = 0; i < size; i++) {
1148
1149 DashboardCategory category = target.get(i);
1150
1151 // Ids are integers, so downcasting is ok
1152 int id = (int) category.id;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001153 int n = category.getTilesCount() - 1;
1154 while (n >= 0) {
1155
1156 DashboardTile tile = category.getTile(n);
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001157 boolean removeTile = false;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001158 id = (int) tile.id;
1159 if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001160 if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {
1161 removeTile = true;
1162 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001163 } else if (id == R.id.wifi_settings) {
1164 // Remove WiFi Settings if WiFi service is not available.
1165 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001166 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001167 }
1168 } else if (id == R.id.bluetooth_settings) {
1169 // Remove Bluetooth Settings if Bluetooth service is not available.
1170 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001171 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001172 }
1173 } else if (id == R.id.data_usage_settings) {
1174 // Remove data usage when kernel module not enabled
1175 final INetworkManagementService netManager = INetworkManagementService.Stub
1176 .asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
1177 try {
1178 if (!netManager.isBandwidthControlEnabled()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001179 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001180 }
1181 } catch (RemoteException e) {
1182 // ignored
1183 }
1184 } else if (id == R.id.battery_settings) {
1185 // Remove battery settings when battery is not available. (e.g. TV)
1186
1187 if (!mBatteryPresent) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001188 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001189 }
1190 } else if (id == R.id.home_settings) {
1191 if (!updateHomeSettingTiles(tile)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001192 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001193 }
1194 } else if (id == R.id.user_settings) {
Amith Yamasani4093e402014-06-06 14:31:37 -07001195 boolean hasMultipleUsers =
1196 ((UserManager) getSystemService(Context.USER_SERVICE))
1197 .getUserCount() > 1;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001198 if (!UserHandle.MU_ENABLED
Amith Yamasani4093e402014-06-06 14:31:37 -07001199 || (!UserManager.supportsMultipleUsers()
1200 && !hasMultipleUsers)
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001201 || Utils.isMonkeyRunning()) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001202 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001203 }
1204 } else if (id == R.id.nfc_payment_settings) {
1205 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001206 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001207 } else {
1208 // Only show if NFC is on and we have the HCE feature
1209 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Fabrice Di Megliob3c7a172014-09-23 11:36:44 -07001210 if (adapter == null || !adapter.isEnabled() ||
1211 !getPackageManager().hasSystemFeature(
1212 PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001213 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001214 }
1215 }
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001216 } else if (id == R.id.print_settings) {
1217 boolean hasPrintingSupport = getPackageManager().hasSystemFeature(
1218 PackageManager.FEATURE_PRINTING);
1219 if (!hasPrintingSupport) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001220 removeTile = true;
Fabrice Di Meglio488cae32014-05-13 11:26:34 -07001221 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001222 } else if (id == R.id.development_settings) {
Julia Reynolds6c088cb2014-05-08 09:29:41 -04001223 if (!showDev || um.hasUserRestriction(
1224 UserManager.DISALLOW_DEBUGGING_FEATURES)) {
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001225 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001226 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001227 }
1228
1229 if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001230 && !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {
1231 removeTile = true;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001232 }
1233
Amith Yamasani57fd5fd2014-08-06 15:48:10 -07001234 if (removeTile && n < category.getTilesCount()) {
1235 category.removeTile(n);
1236 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001237 n--;
1238 }
1239 }
1240 }
1241
1242 private boolean updateHomeSettingTiles(DashboardTile tile) {
1243 // Once we decide to show Home settings, keep showing it forever
1244 SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
1245 if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
1246 return true;
1247 }
1248
1249 try {
Fabrice Di Megliob0a464f2014-07-23 18:15:42 -07001250 mHomeActivitiesCount = getHomeActivitiesCount();
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -07001251 if (mHomeActivitiesCount < 2) {
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -07001252 // When there's only one available home app, omit this settings
1253 // category entirely at the top level UI. If the user just
1254 // uninstalled the penultimate home app candidiate, we also
1255 // now tell them about why they aren't seeing 'Home' in the list.
1256 if (sShowNoHomeNotice) {
1257 sShowNoHomeNotice = false;
1258 NoHomeDialogFragment.show(this);
1259 }
1260 return false;
1261 } else {
1262 // Okay, we're allowing the Home settings category. Tell it, when
1263 // invoked via this front door, that we'll need to be told about the
1264 // case when the user uninstalls all but one home app.
1265 if (tile.fragmentArguments == null) {
1266 tile.fragmentArguments = new Bundle();
1267 }
1268 tile.fragmentArguments.putBoolean(HomeSettings.HOME_SHOW_NOTICE, true);
1269 }
1270 } catch (Exception e) {
1271 // Can't look up the home activity; bail on configuring the icon
1272 Log.w(LOG_TAG, "Problem looking up home activity!", e);
1273 }
1274
1275 sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
1276 return true;
1277 }
1278
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001279 private void getMetaData() {
1280 try {
1281 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
1282 PackageManager.GET_META_DATA);
1283 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001284 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
1285 } catch (NameNotFoundException nnfe) {
1286 // No recovery
1287 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
1288 }
1289 }
1290
1291 // give subclasses access to the Next button
1292 public boolean hasNextButton() {
1293 return mNextButton != null;
1294 }
1295
1296 public Button getNextButton() {
1297 return mNextButton;
1298 }
1299
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001300 @Override
1301 public boolean shouldUpRecreateTask(Intent targetIntent) {
1302 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
1303 }
1304
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001305 public static void requestHomeNotice() {
1306 sShowNoHomeNotice = true;
1307 }
1308
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001309 @Override
1310 public boolean onQueryTextSubmit(String query) {
1311 switchToSearchResultsFragmentIfNeeded();
1312 mSearchQuery = query;
1313 return mSearchResultsFragment.onQueryTextSubmit(query);
1314 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001315
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001316 @Override
1317 public boolean onQueryTextChange(String newText) {
1318 mSearchQuery = newText;
Fabrice Di Meglio7e4855e2014-05-23 16:03:43 -07001319 if (mSearchResultsFragment == null) {
Fabrice Di Meglioa3270762014-04-16 16:54:56 -07001320 return false;
1321 }
1322 return mSearchResultsFragment.onQueryTextChange(newText);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001323 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001324
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001325 @Override
1326 public boolean onClose() {
1327 return false;
1328 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001329
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001330 @Override
1331 public boolean onMenuItemActionExpand(MenuItem item) {
1332 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001333 switchToSearchResultsFragmentIfNeeded();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001334 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001335 return true;
1336 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001337
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001338 @Override
1339 public boolean onMenuItemActionCollapse(MenuItem item) {
1340 if (item.getItemId() == mSearchMenuItem.getItemId()) {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001341 if (mSearchMenuItemExpanded) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001342 revertToInitialFragment();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001343 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001344 }
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001345 return true;
1346 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001347
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001348 private void switchToSearchResultsFragmentIfNeeded() {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001349 if (mSearchResultsFragment != null) {
1350 return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001351 }
Fabrice Di Megliod40dd452014-07-18 15:20:34 -07001352 Fragment current = getFragmentManager().findFragmentById(R.id.main_content);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001353 if (current != null && current instanceof SearchResultsSummary) {
1354 mSearchResultsFragment = (SearchResultsSummary) current;
1355 } else {
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001356 mSearchResultsFragment = (SearchResultsSummary) switchToFragment(
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -07001357 SearchResultsSummary.class.getName(), null, false, true,
1358 R.string.search_results_title, null, true);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001359 }
Fabrice Di Megliod297a582014-04-22 17:23:23 -07001360 mSearchResultsFragment.setSearchView(mSearchView);
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001361 mSearchMenuItemExpanded = true;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001362 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001363
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001364 public void needToRevertToInitialFragment() {
1365 mNeedToRevertToInitialFragment = true;
1366 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001367
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001368 private void revertToInitialFragment() {
1369 mNeedToRevertToInitialFragment = false;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -07001370 mSearchResultsFragment = null;
Fabrice Di Megliobb16fd82014-04-04 14:48:05 -07001371 mSearchMenuItemExpanded = false;
1372 getFragmentManager().popBackStackImmediate(SettingsActivity.BACK_STACK_PREFS,
1373 FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fabrice Di Meglio23ae00c2014-04-21 12:43:20 -07001374 if (mSearchMenuItem != null) {
1375 mSearchMenuItem.collapseActionView();
1376 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001377 }
Jim Miller0698a212014-10-16 19:49:07 -07001378
1379 public Intent getResultIntentData() {
1380 return mResultIntentData;
1381 }
1382
1383 public void setResultIntentData(Intent resultIntentData) {
1384 mResultIntentData = resultIntentData;
1385 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001386}