blob: 73561a66d2807be49a078535566b3af00b00d448 [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
Fan Zhang60f01072016-10-13 16:53:41 -070019import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070020import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import 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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080033import android.content.res.Configuration;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080035import android.graphics.Canvas;
36import android.graphics.drawable.Drawable;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.nfc.NfcAdapter;
Jason Monkfd2c7222015-12-02 15:38:38 -050038import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.os.UserHandle;
41import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080042import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v14.preference.PreferenceFragment;
44import android.support.v7.preference.Preference;
45import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080046import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070047import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.util.Log;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070049import android.view.Menu;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.view.View;
51import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070052import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import android.widget.Button;
Fan Zhang28691572016-03-23 15:31:08 -070054
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040056import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000057import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080058import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080059import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070060import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan Zhangcabc5092017-03-14 12:38:43 -070063import com.android.settings.enterprise.EnterprisePrivacySettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.overlay.FeatureFactory;
Jason Monk2ebc8a02015-02-13 15:23:19 -050065import com.android.settings.search.DynamicIndexableContentMonitor;
Matthew Fritze0ed37c32016-10-24 10:12:49 -070066import com.android.settings.search2.SearchFeatureProvider;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080067import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070068import com.android.settings.widget.SwitchBar;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.SettingsDrawerActivity;
Jason Monk2ebc8a02015-02-13 15:23:19 -050071
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070074import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075
Jason Monk4da79e02015-09-10 10:54:36 -040076public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077 implements PreferenceManager.OnPreferenceTreeClickListener,
78 PreferenceFragment.OnPreferenceStartFragmentCallback,
Matthew Fritze0b431fc2017-03-16 13:19:20 -070079 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 private static final String LOG_TAG = "Settings";
82
Philip P. Moltmannd9779db2016-02-24 12:11:05 -080083 private static final int LOADER_ID_INDEXABLE_CONTENT_MONITOR = 1;
84
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070086 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Megliob731dd02014-04-03 18:40:38 -070087 private static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -070088 private static final String SAVE_KEY_SHOW_SEARCH = ":settings:show_search";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089
90 /**
91 * When starting this activity, the invoking Intent can contain this extra
92 * string to specify which fragment should be initially displayed.
93 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
94 * will call isValidFragment() to confirm that the fragment class name is valid for this
95 * activity.
96 */
97 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
98
99 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -0800100 * The metrics category constant for logging source when a setting fragment is opened.
101 */
102 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
103
104 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800105 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
106 * this extra can also be specified to supply a Bundle of arguments to pass
107 * to that fragment when it is instantiated during the initial creation
108 * of the activity.
109 */
110 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
111
112 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700113 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
114 */
115 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
116
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800117 public static final String BACK_STACK_PREFS = ":settings:prefs";
118
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800119 // extras that allow any preference activity to be launched as part of a wizard
120
121 // show Back and Next buttons? takes boolean parameter
122 // Back will then return RESULT_CANCELED and Next RESULT_OK
123 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
124
125 // add a Skip button?
126 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
127
128 // specify custom text for the Back or Next buttons, or cause a button to not appear
129 // at all by setting it to null
130 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
131 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
132
133 /**
134 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700135 * 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 -0800136 * that fragment.
137 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700138 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100139 /**
140 * The package name used to resolve the title resource id.
141 */
142 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
143 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700144 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
145 ":settings:show_fragment_title_resid";
146 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
147 ":settings:show_fragment_as_shortcut";
148
149 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
150 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151
Fan Zhang78ab9f92017-02-27 16:10:05 -0800152 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700153 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
154
Jason Monk30695812015-11-17 09:01:08 -0500155 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800156 "com.android.settings.FRAGMENT_CLASS";
157
158 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
159
Jason Monkd4f03ec2016-01-07 16:25:34 -0500160 private static final int REQUEST_SUGGESTION = 42;
161
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800164 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700165 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800166
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700167 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
168 "android.settings.APPLICATION_DETAILS_SETTINGS"
169 };
170
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800171 private SharedPreferences mDevelopmentPreferences;
172 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
173
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174 private boolean mBatteryPresent = true;
175 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800176 @Override
177 public void onReceive(Context context, Intent intent) {
178 String action = intent.getAction();
179 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
180 boolean batteryPresent = Utils.isBatteryPresent(intent);
181
182 if (mBatteryPresent != batteryPresent) {
183 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400184 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800185 }
186 }
187 }
188 };
189
Clara Bayarri6934a042015-10-14 11:07:35 +0100190 private final BroadcastReceiver mUserAddRemoveReceiver = new BroadcastReceiver() {
191 @Override
192 public void onReceive(Context context, Intent intent) {
Matthew Fritze34c27602017-01-18 18:24:56 -0800193 if (mSearchFeatureProvider != null && !mSearchFeatureProvider.isEnabled(context)) {
194 String action = intent.getAction();
195 if (action.equals(Intent.ACTION_USER_ADDED)
196 || action.equals(Intent.ACTION_USER_REMOVED)) {
197 mSearchFeatureProvider.updateIndex(getApplicationContext());
198 }
Clara Bayarri6934a042015-10-14 11:07:35 +0100199 }
200 }
201 };
202
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800203 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700204
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700205 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700206 private SwitchBar mSwitchBar;
207
208 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700209
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700210 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700211 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700212
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700213 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700214 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700215
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700216 private ViewGroup mContent;
217
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700218 private SearchFeatureProvider mSearchFeatureProvider;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800219 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700220
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700221 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800222 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800223
Fan Zhanga96a2d82016-09-27 17:51:11 -0700224 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500225 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700226
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700227 public SwitchBar getSwitchBar() {
228 return mSwitchBar;
229 }
230
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800231 @Override
232 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800233 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800234 null, 0);
235 return true;
236 }
237
238 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400239 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800240 return false;
241 }
242
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800243 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800244 public void onConfigurationChanged(Configuration newConfig) {
245 super.onConfigurationChanged(newConfig);
Matthew Fritze34c27602017-01-18 18:24:56 -0800246 if (!mSearchFeatureProvider.isEnabled(this)) {
247 mSearchFeatureProvider.updateIndex(getApplicationContext());
248 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800249 }
250
251 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700252 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700253 if (!mDisplaySearch) {
254 return false;
255 }
256
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700257 mSearchFeatureProvider.setUpSearchMenu(menu, this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700258 return true;
259 }
260
Jason Monk31c7c322016-01-20 14:41:52 -0500261 @Override
262 public SharedPreferences getSharedPreferences(String name, int mode) {
263 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500264 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500265 }
266 return super.getSharedPreferences(name, mode);
267 }
268
Jason Monke4ebcd12016-02-21 09:37:41 -0500269 private String getMetricsTag() {
270 String tag = getClass().getName();
271 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
272 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
273 }
274 if (tag.startsWith("com.android.settings.")) {
275 tag = tag.replace("com.android.settings.", "");
276 }
277 return tag;
278 }
279
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700280 private static boolean isShortCutIntent(final Intent intent) {
281 Set<String> categories = intent.getCategories();
282 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
283 }
284
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700285 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700286 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700287 if (action == null) {
288 return false;
289 }
290 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
291 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
292 }
293 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700294 }
295
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700296 @Override
297 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700298 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500299 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700300
301 final FeatureFactory factory = FeatureFactory.getFactory(this);
302
303 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800304 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800305 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700306
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700307 // Should happen before any call to getIntent()
308 getMetaData();
309
310 final Intent intent = getIntent();
311 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
312 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800313 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800314
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800315 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
316 Context.MODE_PRIVATE);
317
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700318 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700319 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700320
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700321 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700322 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
323
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700324 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700325 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700326
Doris Ling1694d4b2017-03-03 14:21:37 -0800327 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700328
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700329 // This is a "Sub Settings" when:
330 // - this is a real SubSettings
331 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700332 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700333 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
334
Fan Zhang92f1e942017-01-21 10:07:26 -0800335 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
336 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700337 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800338 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700339 }
340
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700341 setContentView(mIsShowingDashboard ?
342 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700344 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700345
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 getFragmentManager().addOnBackStackChangedListener(this);
347
Matthew Fritze34c27602017-01-18 18:24:56 -0800348 if (mIsShowingDashboard && !mSearchFeatureProvider.isEnabled(this)) {
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800349 // Run the Index update only if we have some space
350 if (!Utils.isLowStorage(this)) {
Fan Zhanga8759242017-01-06 09:18:59 -0800351 mSearchFeatureProvider.updateIndex(getApplicationContext());
Fabrice Di Megliodff3faa2015-02-27 11:14:11 -0800352 } else {
353 Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
354 }
Fabrice Di Meglio5cda21b2014-04-21 10:14:28 -0700355 }
356
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700357 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700358 // We are restarting from a previous saved state; used that to initialize, instead
359 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700360 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800361
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700362 ArrayList<DashboardCategory> categories =
363 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
364 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700365 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700366 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700367 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800368 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700369
370 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700371
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800372 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800373 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800374 }
375
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700376 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700377 if (mActionBar != null) {
378 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
379 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
380 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700381 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500382 if (mSwitchBar != null) {
383 mSwitchBar.setMetricsTag(getMetricsTag());
384 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700385
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800386 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800387 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
388
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700389 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800390 if (buttonBar != null) {
391 buttonBar.setVisibility(View.VISIBLE);
392
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700393 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800394 backButton.setOnClickListener(new OnClickListener() {
395 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700396 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800397 finish();
398 }
399 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700400 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800401 skipButton.setOnClickListener(new OnClickListener() {
402 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700403 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800404 finish();
405 }
406 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700407 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800408 mNextButton.setOnClickListener(new OnClickListener() {
409 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700410 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800411 finish();
412 }
413 });
414
415 // set our various button parameters
416 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
417 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
418 if (TextUtils.isEmpty(buttonText)) {
419 mNextButton.setVisibility(View.GONE);
420 }
421 else {
422 mNextButton.setText(buttonText);
423 }
424 }
425 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
426 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
427 if (TextUtils.isEmpty(buttonText)) {
428 backButton.setVisibility(View.GONE);
429 }
430 else {
431 backButton.setText(buttonText);
432 }
433 }
434 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
435 skipButton.setVisibility(View.VISIBLE);
436 }
437 }
438 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700439
Fan Zhangefba6b42017-02-07 17:40:59 -0800440 if (DEBUG_TIMING) {
441 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
442 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700443 }
444
Doris Lingcb69baf2017-02-23 17:50:03 -0800445 @VisibleForTesting
446 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
447 if (!mIsShowingDashboard && initialFragmentName != null) {
448 mDisplaySearch = false;
449 // UP will be shown only if it is a sub settings
450 if (mIsShortcut) {
451 mDisplayHomeAsUpEnabled = isSubSettings;
452 } else if (isSubSettings) {
453 mDisplayHomeAsUpEnabled = true;
454 } else {
455 mDisplayHomeAsUpEnabled = false;
456 }
457 setTitleFromIntent(intent);
458
459 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
460 switchToFragment(initialFragmentName, initialArguments, true, false,
461 mInitialTitleResId, mInitialTitle, false);
462 } else {
463 // No UP affordance if we are displaying the main Dashboard
464 mDisplayHomeAsUpEnabled = false;
465 // Show Search affordance
466 mDisplaySearch = true;
467 mInitialTitleResId = R.string.dashboard_title;
468
469 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
470 mInitialTitleResId, mInitialTitle, false);
471 }
472 }
473
Fan Zhangece8ff62016-06-30 13:20:12 -0700474 public void setDisplaySearchMenu(boolean displaySearch) {
475 if (displaySearch != mDisplaySearch) {
476 mDisplaySearch = displaySearch;
477 invalidateOptionsMenu();
478 }
479 }
480
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700481 private void setTitleFromIntent(Intent intent) {
482 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
483 if (initialTitleResId > 0) {
484 mInitialTitle = null;
485 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100486
487 final String initialTitleResPackageName = intent.getStringExtra(
488 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
489 if (initialTitleResPackageName != null) {
490 try {
491 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
492 0 /* flags */, new UserHandle(UserHandle.myUserId()));
493 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
494 setTitle(mInitialTitle);
495 mInitialTitleResId = -1;
496 return;
497 } catch (NameNotFoundException e) {
498 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
499 }
500 } else {
501 setTitle(mInitialTitleResId);
502 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700503 } else {
504 mInitialTitleResId = -1;
505 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
506 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
507 setTitle(mInitialTitle);
508 }
509 }
510
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800511 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700513 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800514 }
515
Fan Zhang28691572016-03-23 15:31:08 -0700516 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800517 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700518
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800519 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700520 if (mInitialTitleResId > 0) {
521 setTitle(mInitialTitleResId);
522 } else {
523 setTitle(mInitialTitle);
524 }
Fan Zhang28691572016-03-23 15:31:08 -0700525 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800526 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700527
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800528 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
529 setTitleFromBackStackEntry(bse);
530 }
531
532 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
533 final CharSequence title;
534 final int titleRes = bse.getBreadCrumbTitleRes();
535 if (titleRes > 0) {
536 title = getText(titleRes);
537 } else {
538 title = bse.getBreadCrumbTitle();
539 }
540 if (title != null) {
541 setTitle(title);
542 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800543 }
544
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 @Override
546 protected void onSaveInstanceState(Bundle outState) {
547 super.onSaveInstanceState(outState);
548
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700549 if (mCategories.size() > 0) {
550 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800551 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700552
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700553 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800554 }
555
556 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400557 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 super.onResume();
559
560 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
561 @Override
562 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400563 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800564 }
565 };
566 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
567 mDevelopmentPreferencesListener);
568
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800569 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Matthew Fritze34c27602017-01-18 18:24:56 -0800570 if (!mSearchFeatureProvider.isEnabled(this)) {
571 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_ADDED));
572 registerReceiver(mUserAddRemoveReceiver, new IntentFilter(Intent.ACTION_USER_REMOVED));
573 }
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800574 if (mDynamicIndexableContentMonitor == null) {
575 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
576 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800577 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700578
Jason Monk4da79e02015-09-10 10:54:36 -0400579 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800580 }
581
582 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400583 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800584 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800585 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
586 mDevelopmentPreferencesListener);
587 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800588 unregisterReceiver(mBatteryInfoReceiver);
Matthew Fritze34c27602017-01-18 18:24:56 -0800589 if (!mSearchFeatureProvider.isEnabled(this)) {
590 unregisterReceiver(mUserAddRemoveReceiver);
591 }
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800592 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900593 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800594 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800595 }
596
597 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800598 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
599 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
600 taskDescription.setIcon(icon);
601 super.setTaskDescription(taskDescription);
602 }
603
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800604 protected boolean isValidFragment(String fragmentName) {
605 // Almost all fragments are wrapped in this,
606 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800607 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
608 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800609 }
610 return false;
611 }
612
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800613 @Override
614 public Intent getIntent() {
615 Intent superIntent = super.getIntent();
616 String startingFragment = getStartingFragmentClass(superIntent);
617 // This is called from super.onCreate, isMultiPane() is not yet reliable
618 // Do not use onIsHidingHeaders either, which relies itself on this method
619 if (startingFragment != null) {
620 Intent modIntent = new Intent(superIntent);
621 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
622 Bundle args = superIntent.getExtras();
623 if (args != null) {
624 args = new Bundle(args);
625 } else {
626 args = new Bundle();
627 }
628 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100629 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800630 return modIntent;
631 }
632 return superIntent;
633 }
634
635 /**
636 * Checks if the component name in the intent is different from the Settings class and
637 * returns the class name to load as a fragment.
638 */
639 private String getStartingFragmentClass(Intent intent) {
640 if (mFragmentClass != null) return mFragmentClass;
641
642 String intentClass = intent.getComponent().getClassName();
643 if (intentClass.equals(getClass().getName())) return null;
644
645 if ("com.android.settings.ManageApplications".equals(intentClass)
646 || "com.android.settings.RunningServices".equals(intentClass)
647 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
648 // Old names of manage apps.
649 intentClass = com.android.settings.applications.ManageApplications.class.getName();
650 }
651
652 return intentClass;
653 }
654
655 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000656 * Start a new fragment containing a preference panel. If the preferences
657 * are being displayed in multi-pane mode, the given fragment class will
658 * be instantiated and placed in the appropriate pane. If running in
659 * single-pane mode, a new activity will be launched in which to show the
660 * fragment.
661 *
662 * @param fragmentClass Full name of the class implementing the fragment.
663 * @param args Any desired arguments to supply to the fragment.
664 * @param titleRes Optional resource identifier of the title of this
665 * fragment.
666 * @param titleText Optional text of the title of this fragment.
667 * @param resultTo Optional fragment that result data should be sent to.
668 * If non-null, resultTo.onActivityResult() will be called when this
669 * preference panel is done. The launched panel must use
670 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
671 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700672 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000673 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800674 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
675 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700676 String title = null;
677 if (titleRes < 0) {
678 if (titleText != null) {
679 title = titleText.toString();
680 } else {
681 // There not much we can do in that case
682 title = "";
683 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700684 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700685 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800686 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000687 }
688
689 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100690 * Start a new fragment in a new activity containing a preference panel for a given user. If the
691 * preferences are being displayed in multi-pane mode, the given fragment class will be
692 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
693 * activity will be launched in which to show the fragment.
694 *
695 * @param fragmentClass Full name of the class implementing the fragment.
696 * @param args Any desired arguments to supply to the fragment.
697 * @param titleRes Optional resource identifier of the title of this fragment.
698 * @param titleText Optional text of the title of this fragment.
699 * @param userHandle The user for which the panel has to be started.
700 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800701 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
702 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700703 // This is a workaround.
704 //
705 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
706 // starting the fragment could cause a native stack corruption. See b/17523189. However,
707 // adding that flag and start the preference panel with the same UserHandler will make it
708 // impossible to use back button to return to the previous screen. See b/20042570.
709 //
710 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
711 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
712 // when we're calling it as the same user.
713 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800714 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700715 } else {
716 String title = null;
717 if (titleRes < 0) {
718 if (titleText != null) {
719 title = titleText.toString();
720 } else {
721 // There not much we can do in that case
722 title = "";
723 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100724 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800725 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
726 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100727 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100728 }
729
730 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800731 * Called by a preference panel fragment to finish itself.
732 *
733 * @param caller The fragment that is asking to be finished.
734 * @param resultCode Optional result code to send back to the original
735 * launching fragment.
736 * @param resultData Optional result data to send back to the original
737 * launching fragment.
738 */
739 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
740 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700741 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800742 }
743
744 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000745 * Start a new fragment.
746 *
747 * @param fragment The fragment to start
748 * @param push If true, the current fragment will be pushed onto the back stack. If false,
749 * the current fragment will be replaced.
750 */
751 public void startPreferenceFragment(Fragment fragment, boolean push) {
752 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700753 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000754 if (push) {
755 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
756 transaction.addToBackStack(BACK_STACK_PREFS);
757 } else {
758 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
759 }
760 transaction.commitAllowingStateLoss();
761 }
762
763 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700764 * Switch to a specific Fragment with taking care of validation, Title and BackStack
765 */
766 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700767 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700768 if (validate && !isValidFragment(fragmentName)) {
769 throw new IllegalArgumentException("Invalid fragment for this activity: "
770 + fragmentName);
771 }
772 Fragment f = Fragment.instantiate(this, fragmentName, args);
773 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700774 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700775 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700776 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700777 }
778 if (addToBackStack) {
779 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
780 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700781 if (titleResId > 0) {
782 transaction.setBreadCrumbTitle(titleResId);
783 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700784 transaction.setBreadCrumbTitle(title);
785 }
786 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700787 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700788 return f;
789 }
790
Jason Monk4da79e02015-09-10 10:54:36 -0400791 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500792 // Generally the items that are will be changing from these updates will
793 // not be in the top list of tiles, so run it in the background and the
794 // SettingsDrawerActivity will pick up on the updates automatically.
795 AsyncTask.execute(new Runnable() {
796 @Override
797 public void run() {
798 doUpdateTilesList();
799 }
800 });
801 }
802
803 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400804 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700805 final UserManager um = UserManager.get(this);
806 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700807
Jason Monk4da79e02015-09-10 10:54:36 -0400808 String packageName = getPackageName();
809 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800810 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700811
Jason Monk4da79e02015-09-10 10:54:36 -0400812 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800814 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700815
Jason Monk4da79e02015-09-10 10:54:36 -0400816 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800817 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800818 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700819
Jason Monk4da79e02015-09-10 10:54:36 -0400820 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800821 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800822 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700823
Jason Monk4da79e02015-09-10 10:54:36 -0400824 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800825 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800826 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400827
828 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800829 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400830 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhanga6986e72017-03-08 09:24:45 -0800831 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400832
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700833 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800834 Settings.NetworkDashboardActivity.class.getName()),
Doris Ling26208222017-03-03 11:28:05 -0800835 !UserManager.isDeviceInDemoMode(this), isAdmin);
836
837 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800838 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800839 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700840
841 setTileEnabled(new ComponentName(packageName,
842 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800843 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400844 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
845 setTileEnabled(new ComponentName(packageName,
846 Settings.PaymentSettingsActivity.class.getName()),
847 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
848 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800849 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400850
851 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800852 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800853 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400854
855 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800856 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400857 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400858 setTileEnabled(new ComponentName(packageName,
859 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800860 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400861
Fan Zhanga6986e72017-03-08 09:24:45 -0800862 // Enable/disable backup settings depending on whether the user is admin.
863 setTileEnabled(new ComponentName(packageName,
864 BackupSettingsActivity.class.getName()), true,
865 isAdmin || Utils.isCarrierDemoUser(this));
866
867 setTileEnabled(new ComponentName(packageName,
868 Settings.EnterprisePrivacySettingsActivity.class.getName()),
Fan Zhangcabc5092017-03-14 12:38:43 -0700869 EnterprisePrivacySettings.isPageEnabled(this), isAdmin);
Fan Zhanga6986e72017-03-08 09:24:45 -0800870
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800871 setTileEnabled(new ComponentName(packageName,
872 Settings.WifiDisplaySettingsActivity.class.getName()),
873 WifiDisplaySettings.isAvailable(this), isAdmin);
874
Jason Monk4da79e02015-09-10 10:54:36 -0400875 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800876
Jason Monk4da79e02015-09-10 10:54:36 -0400877 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800878 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800879 synchronized (categories) {
880 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700881 final int tileCount = category.getTilesCount();
882 for (int i = 0; i < tileCount; i++) {
883 final ComponentName component = category.getTile(i).intent.getComponent();
884
Fan Zhanga6986e72017-03-08 09:24:45 -0800885 final String name = component.getClassName();
886 final boolean isEnabledForRestricted = ArrayUtils.contains(
887 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
888 if (packageName.equals(component.getPackageName())
889 && !isEnabledForRestricted) {
890 setTileEnabled(component, false, isAdmin);
891 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700892 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700893 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700894 }
895 }
Doris Ling26bf0032016-06-15 18:16:09 -0700896
Fan Zhang224caad2017-01-06 11:42:48 -0800897 // Final step, refresh categories.
898 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500899 }
900
Fan Zhang224caad2017-01-06 11:42:48 -0800901 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400902 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800903 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
904 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400905 enabled = false;
906 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400907 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500908 }
909
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800910 private void getMetaData() {
911 try {
912 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
913 PackageManager.GET_META_DATA);
914 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800915 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
916 } catch (NameNotFoundException nnfe) {
917 // No recovery
918 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
919 }
920 }
921
922 // give subclasses access to the Next button
923 public boolean hasNextButton() {
924 return mNextButton != null;
925 }
926
927 public Button getNextButton() {
928 return mNextButton;
929 }
930
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800931 @Override
932 public boolean shouldUpRecreateTask(Intent targetIntent) {
933 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
934 }
935
Jason Monkd4f03ec2016-01-07 16:25:34 -0500936 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700937 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700938 return;
939 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700940 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500941 startActivityForResult(intent, REQUEST_SUGGESTION);
942 }
943
944 @Override
945 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
946 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
947 && resultCode != RESULT_CANCELED) {
948 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
949 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
950 }
951 super.onActivityResult(requestCode, resultCode, data);
952 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800953
954 @VisibleForTesting
955 Bitmap getBitmapFromXmlResource(int drawableRes) {
956 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
957 Canvas canvas = new Canvas();
958 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
959 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
960 canvas.setBitmap(bitmap);
961 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
962 drawable.draw(canvas);
963
964 return bitmap;
965 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700966}