blob: 09380e5c00b7db1682de86b059e65e12d6eb6d5d [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;
jackqdyulei6c355f52017-03-01 17:37:23 -080033import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080036import android.nfc.NfcAdapter;
Jason Monkfd2c7222015-12-02 15:38:38 -050037import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.UserHandle;
40import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080041import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040042import android.support.v14.preference.PreferenceFragment;
43import android.support.v7.preference.Preference;
44import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070046import android.transition.TransitionManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.util.Log;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070048import android.view.Menu;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.view.View;
50import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.widget.Button;
Fan Zhang28691572016-03-23 15:31:08 -070053
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040055import com.android.settings.Settings.WifiSettingsActivity;
Anton Philippovadfec552017-01-25 20:37:36 +000056import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080057import com.android.settings.core.gateway.SettingsGateway;
Fan Zhangc6ca3142017-02-14 15:02:35 -080058import com.android.settings.core.instrumentation.MetricsFeatureProvider;
Fan Zhang5f79ae92016-08-18 16:04:19 -070059import com.android.settings.core.instrumentation.SharedPreferencesLogger;
Fan Zhanga96a2d82016-09-27 17:51:11 -070060import com.android.settings.dashboard.DashboardFeatureProvider;
61import com.android.settings.dashboard.DashboardSummary;
Fan Zhang11d2d682017-03-17 16:19:26 -070062import com.android.settings.development.DevelopmentSettings;
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 Meglio263bcc82014-01-17 19:17:58 -080088
89 /**
90 * When starting this activity, the invoking Intent can contain this extra
91 * string to specify which fragment should be initially displayed.
92 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
93 * will call isValidFragment() to confirm that the fragment class name is valid for this
94 * activity.
95 */
96 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
97
98 /**
Fan Zhangc6ca3142017-02-14 15:02:35 -080099 * The metrics category constant for logging source when a setting fragment is opened.
100 */
101 public static final String EXTRA_SOURCE_METRICS_CATEGORY = ":settings:source_metrics";
102
103 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800104 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
105 * this extra can also be specified to supply a Bundle of arguments to pass
106 * to that fragment when it is instantiated during the initial creation
107 * of the activity.
108 */
109 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
110
111 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700112 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
113 */
114 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
115
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800116 public static final String BACK_STACK_PREFS = ":settings:prefs";
117
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118 // extras that allow any preference activity to be launched as part of a wizard
119
120 // show Back and Next buttons? takes boolean parameter
121 // Back will then return RESULT_CANCELED and Next RESULT_OK
122 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
123
124 // add a Skip button?
125 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
126
127 // specify custom text for the Back or Next buttons, or cause a button to not appear
128 // at all by setting it to null
129 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
130 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
131
132 /**
133 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700134 * 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 -0800135 * that fragment.
136 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100138 /**
139 * The package name used to resolve the title resource id.
140 */
141 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
142 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700143 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
144 ":settings:show_fragment_title_resid";
145 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
146 ":settings:show_fragment_as_shortcut";
147
148 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
149 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800150
Fan Zhang78ab9f92017-02-27 16:10:05 -0800151 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700152 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
153
Jason Monk30695812015-11-17 09:01:08 -0500154 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155 "com.android.settings.FRAGMENT_CLASS";
156
157 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
158
Jason Monkd4f03ec2016-01-07 16:25:34 -0500159 private static final int REQUEST_SUGGESTION = 42;
160
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800163 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700164 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800165
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700166 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
167 "android.settings.APPLICATION_DETAILS_SETTINGS"
168 };
169
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170 private SharedPreferences mDevelopmentPreferences;
171 private SharedPreferences.OnSharedPreferenceChangeListener mDevelopmentPreferencesListener;
172
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 private boolean mBatteryPresent = true;
174 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 @Override
176 public void onReceive(Context context, Intent intent) {
177 String action = intent.getAction();
178 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
179 boolean batteryPresent = Utils.isBatteryPresent(intent);
180
181 if (mBatteryPresent != batteryPresent) {
182 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400183 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800184 }
185 }
186 }
187 };
188
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800189 private DynamicIndexableContentMonitor mDynamicIndexableContentMonitor;
Svetoslav853e4712014-04-14 10:10:25 -0700190
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700191 private ActionBar mActionBar;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700192 private SwitchBar mSwitchBar;
193
194 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700195
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700196 private boolean mDisplayHomeAsUpEnabled;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700197 private boolean mDisplaySearch;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700198
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700199 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700200 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700201
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700202 private ViewGroup mContent;
203
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700204 private SearchFeatureProvider mSearchFeatureProvider;
Fan Zhangc6ca3142017-02-14 15:02:35 -0800205 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700206
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700207 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800208 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209
Fan Zhanga96a2d82016-09-27 17:51:11 -0700210 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500211 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700212
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700213 public SwitchBar getSwitchBar() {
214 return mSwitchBar;
215 }
216
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800217 @Override
218 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800219 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800220 null, 0);
221 return true;
222 }
223
224 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400225 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800226 return false;
227 }
228
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800229 @Override
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700230 public boolean onCreateOptionsMenu(Menu menu) {
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700231 if (!mDisplaySearch) {
232 return false;
233 }
234
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700235 mSearchFeatureProvider.setUpSearchMenu(menu, this);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700236 return true;
237 }
238
Jason Monk31c7c322016-01-20 14:41:52 -0500239 @Override
240 public SharedPreferences getSharedPreferences(String name, int mode) {
241 if (name.equals(getPackageName() + "_preferences")) {
Jason Monke4ebcd12016-02-21 09:37:41 -0500242 return new SharedPreferencesLogger(this, getMetricsTag());
Jason Monk31c7c322016-01-20 14:41:52 -0500243 }
244 return super.getSharedPreferences(name, mode);
245 }
246
Jason Monke4ebcd12016-02-21 09:37:41 -0500247 private String getMetricsTag() {
248 String tag = getClass().getName();
249 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
250 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
251 }
252 if (tag.startsWith("com.android.settings.")) {
253 tag = tag.replace("com.android.settings.", "");
254 }
255 return tag;
256 }
257
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700258 private static boolean isShortCutIntent(final Intent intent) {
259 Set<String> categories = intent.getCategories();
260 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
261 }
262
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700263 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700264 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700265 if (action == null) {
266 return false;
267 }
268 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
269 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
270 }
271 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700272 }
273
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700274 @Override
275 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700276 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500277 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700278
279 final FeatureFactory factory = FeatureFactory.getFactory(this);
280
281 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze9955db62016-12-20 09:37:21 -0800282 mSearchFeatureProvider = factory.getSearchFeatureProvider();
Fan Zhangc6ca3142017-02-14 15:02:35 -0800283 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700284
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700285 // Should happen before any call to getIntent()
286 getMetaData();
287
288 final Intent intent = getIntent();
289 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
290 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800292
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
294 Context.MODE_PRIVATE);
295
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700296 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700297 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700298
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700299 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700300 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
301
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700302 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700303 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700304
Doris Ling1694d4b2017-03-03 14:21:37 -0800305 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700306
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700307 // This is a "Sub Settings" when:
308 // - this is a real SubSettings
309 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700310 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700311 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
312
Fan Zhang92f1e942017-01-21 10:07:26 -0800313 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
314 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700315 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800316 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700317 }
318
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700319 setContentView(mIsShowingDashboard ?
320 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700322 mContent = (ViewGroup) findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700323
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 getFragmentManager().addOnBackStackChangedListener(this);
325
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700326 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700327 // We are restarting from a previous saved state; used that to initialize, instead
328 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700329 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800330
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700331 ArrayList<DashboardCategory> categories =
332 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
333 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700334 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700335 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700336 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800337 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700338
339 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700340
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800342 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343 }
344
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700345 mActionBar = getActionBar();
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700346 if (mActionBar != null) {
347 mActionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
348 mActionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
349 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700350 mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500351 if (mSwitchBar != null) {
352 mSwitchBar.setMetricsTag(getMetricsTag());
353 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700354
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
357
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700358 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 if (buttonBar != null) {
360 buttonBar.setVisibility(View.VISIBLE);
361
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700362 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 backButton.setOnClickListener(new OnClickListener() {
364 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700365 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 finish();
367 }
368 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700369 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 skipButton.setOnClickListener(new OnClickListener() {
371 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700372 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 finish();
374 }
375 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700376 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800377 mNextButton.setOnClickListener(new OnClickListener() {
378 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700379 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800380 finish();
381 }
382 });
383
384 // set our various button parameters
385 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
386 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
387 if (TextUtils.isEmpty(buttonText)) {
388 mNextButton.setVisibility(View.GONE);
389 }
390 else {
391 mNextButton.setText(buttonText);
392 }
393 }
394 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
395 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
396 if (TextUtils.isEmpty(buttonText)) {
397 backButton.setVisibility(View.GONE);
398 }
399 else {
400 backButton.setText(buttonText);
401 }
402 }
403 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
404 skipButton.setVisibility(View.VISIBLE);
405 }
406 }
407 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700408
Fan Zhangefba6b42017-02-07 17:40:59 -0800409 if (DEBUG_TIMING) {
410 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
411 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700412 }
413
Doris Lingcb69baf2017-02-23 17:50:03 -0800414 @VisibleForTesting
415 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
416 if (!mIsShowingDashboard && initialFragmentName != null) {
417 mDisplaySearch = false;
418 // UP will be shown only if it is a sub settings
419 if (mIsShortcut) {
420 mDisplayHomeAsUpEnabled = isSubSettings;
421 } else if (isSubSettings) {
422 mDisplayHomeAsUpEnabled = true;
423 } else {
424 mDisplayHomeAsUpEnabled = false;
425 }
426 setTitleFromIntent(intent);
427
428 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
429 switchToFragment(initialFragmentName, initialArguments, true, false,
430 mInitialTitleResId, mInitialTitle, false);
431 } else {
432 // No UP affordance if we are displaying the main Dashboard
433 mDisplayHomeAsUpEnabled = false;
434 // Show Search affordance
435 mDisplaySearch = true;
436 mInitialTitleResId = R.string.dashboard_title;
437
438 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
439 mInitialTitleResId, mInitialTitle, false);
440 }
441 }
442
Fan Zhangece8ff62016-06-30 13:20:12 -0700443 public void setDisplaySearchMenu(boolean displaySearch) {
444 if (displaySearch != mDisplaySearch) {
445 mDisplaySearch = displaySearch;
446 invalidateOptionsMenu();
447 }
448 }
449
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700450 private void setTitleFromIntent(Intent intent) {
451 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
452 if (initialTitleResId > 0) {
453 mInitialTitle = null;
454 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100455
456 final String initialTitleResPackageName = intent.getStringExtra(
457 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
458 if (initialTitleResPackageName != null) {
459 try {
460 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
461 0 /* flags */, new UserHandle(UserHandle.myUserId()));
462 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
463 setTitle(mInitialTitle);
464 mInitialTitleResId = -1;
465 return;
466 } catch (NameNotFoundException e) {
467 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
468 }
469 } else {
470 setTitle(mInitialTitleResId);
471 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700472 } else {
473 mInitialTitleResId = -1;
474 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
475 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
476 setTitle(mInitialTitle);
477 }
478 }
479
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800480 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700482 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800483 }
484
Fan Zhang28691572016-03-23 15:31:08 -0700485 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800486 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700487
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800488 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700489 if (mInitialTitleResId > 0) {
490 setTitle(mInitialTitleResId);
491 } else {
492 setTitle(mInitialTitle);
493 }
Fan Zhang28691572016-03-23 15:31:08 -0700494 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800495 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700496
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800497 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
498 setTitleFromBackStackEntry(bse);
499 }
500
501 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
502 final CharSequence title;
503 final int titleRes = bse.getBreadCrumbTitleRes();
504 if (titleRes > 0) {
505 title = getText(titleRes);
506 } else {
507 title = bse.getBreadCrumbTitle();
508 }
509 if (title != null) {
510 setTitle(title);
511 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800512 }
513
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800514 @Override
515 protected void onSaveInstanceState(Bundle outState) {
516 super.onSaveInstanceState(outState);
517
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700518 if (mCategories.size() > 0) {
519 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800520 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700521
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700522 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800523 }
524
525 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400526 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 super.onResume();
528
529 mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
530 @Override
531 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Jason Monk4da79e02015-09-10 10:54:36 -0400532 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 }
534 };
535 mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(
536 mDevelopmentPreferencesListener);
537
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800539 if (mDynamicIndexableContentMonitor == null) {
540 mDynamicIndexableContentMonitor = new DynamicIndexableContentMonitor();
541 }
Philip P. Moltmannd9779db2016-02-24 12:11:05 -0800542 mDynamicIndexableContentMonitor.register(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700543
Jason Monk4da79e02015-09-10 10:54:36 -0400544 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 }
546
547 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400548 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549 super.onPause();
Fan Zhanga6986e72017-03-08 09:24:45 -0800550 mDevelopmentPreferences.unregisterOnSharedPreferenceChangeListener(
551 mDevelopmentPreferencesListener);
552 mDevelopmentPreferencesListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 unregisterReceiver(mBatteryInfoReceiver);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800554 if (mDynamicIndexableContentMonitor != null) {
Tadashi G. Takaokaa034fa52016-11-21 14:20:19 +0900555 mDynamicIndexableContentMonitor.unregister(this, LOADER_ID_INDEXABLE_CONTENT_MONITOR);
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800556 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800557 }
558
559 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800560 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
561 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
562 taskDescription.setIcon(icon);
563 super.setTaskDescription(taskDescription);
564 }
565
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 protected boolean isValidFragment(String fragmentName) {
567 // Almost all fragments are wrapped in this,
568 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800569 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
570 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571 }
572 return false;
573 }
574
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 @Override
576 public Intent getIntent() {
577 Intent superIntent = super.getIntent();
578 String startingFragment = getStartingFragmentClass(superIntent);
579 // This is called from super.onCreate, isMultiPane() is not yet reliable
580 // Do not use onIsHidingHeaders either, which relies itself on this method
581 if (startingFragment != null) {
582 Intent modIntent = new Intent(superIntent);
583 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
584 Bundle args = superIntent.getExtras();
585 if (args != null) {
586 args = new Bundle(args);
587 } else {
588 args = new Bundle();
589 }
590 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100591 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800592 return modIntent;
593 }
594 return superIntent;
595 }
596
597 /**
598 * Checks if the component name in the intent is different from the Settings class and
599 * returns the class name to load as a fragment.
600 */
601 private String getStartingFragmentClass(Intent intent) {
602 if (mFragmentClass != null) return mFragmentClass;
603
604 String intentClass = intent.getComponent().getClassName();
605 if (intentClass.equals(getClass().getName())) return null;
606
607 if ("com.android.settings.ManageApplications".equals(intentClass)
608 || "com.android.settings.RunningServices".equals(intentClass)
609 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
610 // Old names of manage apps.
611 intentClass = com.android.settings.applications.ManageApplications.class.getName();
612 }
613
614 return intentClass;
615 }
616
617 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000618 * Start a new fragment containing a preference panel. If the preferences
619 * are being displayed in multi-pane mode, the given fragment class will
620 * be instantiated and placed in the appropriate pane. If running in
621 * single-pane mode, a new activity will be launched in which to show the
622 * fragment.
623 *
624 * @param fragmentClass Full name of the class implementing the fragment.
625 * @param args Any desired arguments to supply to the fragment.
626 * @param titleRes Optional resource identifier of the title of this
627 * fragment.
628 * @param titleText Optional text of the title of this fragment.
629 * @param resultTo Optional fragment that result data should be sent to.
630 * If non-null, resultTo.onActivityResult() will be called when this
631 * preference panel is done. The launched panel must use
632 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
633 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700634 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000635 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800636 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
637 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700638 String title = null;
639 if (titleRes < 0) {
640 if (titleText != null) {
641 title = titleText.toString();
642 } else {
643 // There not much we can do in that case
644 title = "";
645 }
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700646 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700647 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800648 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000649 }
650
651 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100652 * Start a new fragment in a new activity containing a preference panel for a given user. If the
653 * preferences are being displayed in multi-pane mode, the given fragment class will be
654 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
655 * activity will be launched in which to show the fragment.
656 *
657 * @param fragmentClass Full name of the class implementing the fragment.
658 * @param args Any desired arguments to supply to the fragment.
659 * @param titleRes Optional resource identifier of the title of this fragment.
660 * @param titleText Optional text of the title of this fragment.
661 * @param userHandle The user for which the panel has to be started.
662 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800663 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
664 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700665 // This is a workaround.
666 //
667 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
668 // starting the fragment could cause a native stack corruption. See b/17523189. However,
669 // adding that flag and start the preference panel with the same UserHandler will make it
670 // impossible to use back button to return to the previous screen. See b/20042570.
671 //
672 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
673 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
674 // when we're calling it as the same user.
675 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800676 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700677 } else {
678 String title = null;
679 if (titleRes < 0) {
680 if (titleText != null) {
681 title = titleText.toString();
682 } else {
683 // There not much we can do in that case
684 title = "";
685 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100686 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800687 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
688 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100689 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100690 }
691
692 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 * Called by a preference panel fragment to finish itself.
694 *
695 * @param caller The fragment that is asking to be finished.
696 * @param resultCode Optional result code to send back to the original
697 * launching fragment.
698 * @param resultData Optional result data to send back to the original
699 * launching fragment.
700 */
701 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
702 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700703 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800704 }
705
706 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000707 * Start a new fragment.
708 *
709 * @param fragment The fragment to start
710 * @param push If true, the current fragment will be pushed onto the back stack. If false,
711 * the current fragment will be replaced.
712 */
713 public void startPreferenceFragment(Fragment fragment, boolean push) {
714 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700715 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000716 if (push) {
717 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
718 transaction.addToBackStack(BACK_STACK_PREFS);
719 } else {
720 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
721 }
722 transaction.commitAllowingStateLoss();
723 }
724
725 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700726 * Switch to a specific Fragment with taking care of validation, Title and BackStack
727 */
728 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700729 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700730 if (validate && !isValidFragment(fragmentName)) {
731 throw new IllegalArgumentException("Invalid fragment for this activity: "
732 + fragmentName);
733 }
734 Fragment f = Fragment.instantiate(this, fragmentName, args);
735 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700736 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700737 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700738 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700739 }
740 if (addToBackStack) {
741 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
742 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700743 if (titleResId > 0) {
744 transaction.setBreadCrumbTitle(titleResId);
745 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700746 transaction.setBreadCrumbTitle(title);
747 }
748 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700749 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700750 return f;
751 }
752
Jason Monk4da79e02015-09-10 10:54:36 -0400753 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500754 // Generally the items that are will be changing from these updates will
755 // not be in the top list of tiles, so run it in the background and the
756 // SettingsDrawerActivity will pick up on the updates automatically.
757 AsyncTask.execute(new Runnable() {
758 @Override
759 public void run() {
760 doUpdateTilesList();
761 }
762 });
763 }
764
765 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400766 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700767 final UserManager um = UserManager.get(this);
768 final boolean isAdmin = um.isAdminUser();
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700769
Jason Monk4da79e02015-09-10 10:54:36 -0400770 String packageName = getPackageName();
771 setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800772 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700773
Jason Monk4da79e02015-09-10 10:54:36 -0400774 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800775 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800776 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700777
Jason Monk4da79e02015-09-10 10:54:36 -0400778 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800779 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800780 Utils.isBandwidthControlEnabled(), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700781
Jason Monk4da79e02015-09-10 10:54:36 -0400782 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800783 Settings.SimSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800784 Utils.showSimCardTile(this), isAdmin);
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700785
Jason Monk4da79e02015-09-10 10:54:36 -0400786 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800787 Settings.PowerUsageSummaryActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800788 mBatteryPresent, isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400789
790 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800791 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400792 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhanga6986e72017-03-08 09:24:45 -0800793 && !Utils.isMonkeyRunning(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400794
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700795 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800796 Settings.NetworkDashboardActivity.class.getName()),
Doris Ling26208222017-03-03 11:28:05 -0800797 !UserManager.isDeviceInDemoMode(this), isAdmin);
798
799 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800800 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800801 !UserManager.isDeviceInDemoMode(this), isAdmin);
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700802
803 setTileEnabled(new ComponentName(packageName,
804 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800805 !UserManager.isDeviceInDemoMode(this), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400806 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
807 setTileEnabled(new ComponentName(packageName,
808 Settings.PaymentSettingsActivity.class.getName()),
809 pm.hasSystemFeature(PackageManager.FEATURE_NFC)
810 && pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
Fan Zhang224caad2017-01-06 11:42:48 -0800811 && adapter != null && adapter.isEnabled(), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400812
813 setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800814 Settings.PrintSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800815 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin);
Jason Monk4da79e02015-09-10 10:54:36 -0400816
817 final boolean showDev = mDevelopmentPreferences.getBoolean(
Fan Zhanga6986e72017-03-08 09:24:45 -0800818 DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"))
Dan Sandler12c4ba42016-03-28 11:13:40 -0400819 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES);
Jason Monk4da79e02015-09-10 10:54:36 -0400820 setTileEnabled(new ComponentName(packageName,
821 Settings.DevelopmentSettingsActivity.class.getName()),
Fan Zhang224caad2017-01-06 11:42:48 -0800822 showDev, isAdmin);
Dan Sandler12c4ba42016-03-28 11:13:40 -0400823
Fan Zhanga6986e72017-03-08 09:24:45 -0800824 // Enable/disable backup settings depending on whether the user is admin.
825 setTileEnabled(new ComponentName(packageName,
826 BackupSettingsActivity.class.getName()), true,
827 isAdmin || Utils.isCarrierDemoUser(this));
828
829 setTileEnabled(new ComponentName(packageName,
830 Settings.EnterprisePrivacySettingsActivity.class.getName()),
Fan Zhangcabc5092017-03-14 12:38:43 -0700831 EnterprisePrivacySettings.isPageEnabled(this), isAdmin);
Fan Zhanga6986e72017-03-08 09:24:45 -0800832
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800833 setTileEnabled(new ComponentName(packageName,
834 Settings.WifiDisplaySettingsActivity.class.getName()),
835 WifiDisplaySettings.isAvailable(this), isAdmin);
836
Jason Monk4da79e02015-09-10 10:54:36 -0400837 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800838
Jason Monk4da79e02015-09-10 10:54:36 -0400839 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800840 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800841 synchronized (categories) {
842 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700843 final int tileCount = category.getTilesCount();
844 for (int i = 0; i < tileCount; i++) {
845 final ComponentName component = category.getTile(i).intent.getComponent();
846
Fan Zhanga6986e72017-03-08 09:24:45 -0800847 final String name = component.getClassName();
848 final boolean isEnabledForRestricted = ArrayUtils.contains(
849 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
850 if (packageName.equals(component.getPackageName())
851 && !isEnabledForRestricted) {
852 setTileEnabled(component, false, isAdmin);
853 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700854 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700855 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700856 }
857 }
Doris Ling26bf0032016-06-15 18:16:09 -0700858
Fan Zhang224caad2017-01-06 11:42:48 -0800859 // Final step, refresh categories.
860 updateCategories();
Jason Monk2ebc8a02015-02-13 15:23:19 -0500861 }
862
Fan Zhang224caad2017-01-06 11:42:48 -0800863 private void setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400864 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800865 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
866 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400867 enabled = false;
868 }
Jason Monk5862c1e2016-06-07 14:02:33 -0400869 setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500870 }
871
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800872 private void getMetaData() {
873 try {
874 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
875 PackageManager.GET_META_DATA);
876 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800877 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
878 } catch (NameNotFoundException nnfe) {
879 // No recovery
880 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
881 }
882 }
883
884 // give subclasses access to the Next button
885 public boolean hasNextButton() {
886 return mNextButton != null;
887 }
888
889 public Button getNextButton() {
890 return mNextButton;
891 }
892
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800893 @Override
894 public boolean shouldUpRecreateTask(Intent targetIntent) {
895 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
896 }
897
Jason Monkd4f03ec2016-01-07 16:25:34 -0500898 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700899 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700900 return;
901 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700902 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500903 startActivityForResult(intent, REQUEST_SUGGESTION);
904 }
905
906 @Override
907 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
908 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
909 && resultCode != RESULT_CANCELED) {
910 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
911 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
912 }
913 super.onActivityResult(requestCode, resultCode, data);
914 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800915
916 @VisibleForTesting
917 Bitmap getBitmapFromXmlResource(int drawableRes) {
918 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
919 Canvas canvas = new Canvas();
920 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
921 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
922 canvas.setBitmap(bitmap);
923 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
924 drawable.draw(canvas);
925
926 return bitmap;
927 }
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700928}