blob: 84b107191af582796b42ef4289265a0f742156b2 [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 Zhang6013a952018-02-06 11:36:07 -080019import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
20
Fan Zhang60f01072016-10-13 16:53:41 -070021import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070022import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080023import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080035import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080036import android.graphics.Canvas;
37import android.graphics.drawable.Drawable;
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;
Tony Mantler3d84d562017-07-31 10:48:55 -070044import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040045import android.support.v7.preference.Preference;
46import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070048import android.transition.TransitionManager;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080049import android.util.FeatureFlagUtils;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.view.View;
52import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070055import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070056
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080057import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040058import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070059import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000060import com.android.settings.backup.BackupSettingsActivity;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080061import com.android.settings.core.FeatureFlags;
Fan Zhang615563d2018-02-15 15:19:33 -080062import com.android.settings.core.SubSettingLauncher;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080063import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.dashboard.DashboardFeatureProvider;
65import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070066import com.android.settings.overlay.FeatureFactory;
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;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080069import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
70import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070071import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040072import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040073import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070074
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070077import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080078
Jason Monk4da79e02015-09-10 10:54:36 -040079public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080 implements PreferenceManager.OnPreferenceTreeClickListener,
81 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080082 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083
84 private static final String LOG_TAG = "Settings";
85
86 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070087 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
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 /**
99 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
100 * this extra can also be specified to supply a Bundle of arguments to pass
101 * to that fragment when it is instantiated during the initial creation
102 * of the activity.
103 */
104 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
105
106 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700107 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
108 */
109 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
110
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800111 public static final String BACK_STACK_PREFS = ":settings:prefs";
112
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800113 // extras that allow any preference activity to be launched as part of a wizard
114
115 // show Back and Next buttons? takes boolean parameter
116 // Back will then return RESULT_CANCELED and Next RESULT_OK
117 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
118
119 // add a Skip button?
120 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
121
122 // specify custom text for the Back or Next buttons, or cause a button to not appear
123 // at all by setting it to null
124 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
125 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
126
127 /**
128 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700129 * 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 -0800130 * that fragment.
131 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700132 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100133 /**
134 * The package name used to resolve the title resource id.
135 */
136 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
137 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700138 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
139 ":settings:show_fragment_title_resid";
140 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
141 ":settings:show_fragment_as_shortcut";
142
143 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
144 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800145
Fan Zhang78ab9f92017-02-27 16:10:05 -0800146 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700147 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
148
Jason Monk30695812015-11-17 09:01:08 -0500149 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800150 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151
152 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
153
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800154 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800156 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700157 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800158
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700159 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
160 "android.settings.APPLICATION_DETAILS_SETTINGS"
161 };
162
Tony Mantler3d84d562017-07-31 10:48:55 -0700163 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165 private boolean mBatteryPresent = true;
166 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800167 @Override
168 public void onReceive(Context context, Intent intent) {
169 String action = intent.getAction();
170 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
171 boolean batteryPresent = Utils.isBatteryPresent(intent);
172
173 if (mBatteryPresent != batteryPresent) {
174 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400175 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800176 }
177 }
178 }
179 };
180
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700181 private SwitchBar mSwitchBar;
182
183 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700184
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700185 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700186 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700187
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700188 private ViewGroup mContent;
189
Fan Zhangc6ca3142017-02-14 15:02:35 -0800190 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700191
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700192 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800193 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194
Fan Zhanga96a2d82016-09-27 17:51:11 -0700195 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700196
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700197 public SwitchBar getSwitchBar() {
198 return mSwitchBar;
199 }
200
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201 @Override
202 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800203 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204 return true;
205 }
206
207 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400208 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 return false;
210 }
211
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800212 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500213 public SharedPreferences getSharedPreferences(String name, int mode) {
214 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800215 return new SharedPreferencesLogger(this, getMetricsTag(),
216 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500217 }
218 return super.getSharedPreferences(name, mode);
219 }
220
Jason Monke4ebcd12016-02-21 09:37:41 -0500221 private String getMetricsTag() {
222 String tag = getClass().getName();
223 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
224 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
225 }
226 if (tag.startsWith("com.android.settings.")) {
227 tag = tag.replace("com.android.settings.", "");
228 }
229 return tag;
230 }
231
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700232 private static boolean isShortCutIntent(final Intent intent) {
233 Set<String> categories = intent.getCategories();
234 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
235 }
236
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700237 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700238 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700239 if (action == null) {
240 return false;
241 }
242 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
243 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
244 }
245 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700246 }
247
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700248 @Override
249 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700250 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500251 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700252
253 final FeatureFactory factory = FeatureFactory.getFactory(this);
254
255 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800256 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700257
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700258 // Should happen before any call to getIntent()
259 getMetaData();
260
261 final Intent intent = getIntent();
262 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
263 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800264 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800265
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700266 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700267 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700268
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700269 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700270 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
271
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700272 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700273 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700274
Doris Ling1694d4b2017-03-03 14:21:37 -0800275 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700276
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700277 // This is a "Sub Settings" when:
278 // - this is a real SubSettings
279 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700280 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700281 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
282
Fan Zhang92f1e942017-01-21 10:07:26 -0800283 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
284 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700285 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800286 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700287 }
288
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700289 setContentView(mIsShowingDashboard ?
290 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700292 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700293
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294 getFragmentManager().addOnBackStackChangedListener(this);
295
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700296 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700297 // We are restarting from a previous saved state; used that to initialize, instead
298 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700299 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800300
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700301 ArrayList<DashboardCategory> categories =
302 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
303 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700304 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700305 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700306 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800308 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800309 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800310 }
311
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700312 if (mIsShowingDashboard) {
313 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
314 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800315 final Toolbar toolbar = findViewById(R.id.search_action_bar);
316 FeatureFactory.getFactory(this).getSearchFeatureProvider()
317 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700318 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700319
320 // Please forgive me for what I am about to do.
321 //
322 // Need to make the navigation icon non-clickable so that the entire card is clickable
323 // and goes to the search UI. Also set the background to null so there's no ripple.
324 View navView = toolbar.getNavigationView();
325 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800326 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700327 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700328 }
329
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700330 ActionBar actionBar = getActionBar();
331 if (actionBar != null) {
Fan Zhangf11c8852018-02-06 14:21:01 -0800332 actionBar.setDisplayHomeAsUpEnabled(true);
333 actionBar.setHomeButtonEnabled(true);
Fan Zhange096ae72017-11-30 14:41:04 -0800334 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700335 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700336 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500337 if (mSwitchBar != null) {
338 mSwitchBar.setMetricsTag(getMetricsTag());
339 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700340
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
343
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700344 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 if (buttonBar != null) {
346 buttonBar.setVisibility(View.VISIBLE);
347
Fan Zhangf11c8852018-02-06 14:21:01 -0800348 Button backButton = (Button) findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 backButton.setOnClickListener(new OnClickListener() {
350 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700351 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 finish();
353 }
354 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800355 Button skipButton = (Button) findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 skipButton.setOnClickListener(new OnClickListener() {
357 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700358 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 finish();
360 }
361 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800362 mNextButton = (Button) findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 mNextButton.setOnClickListener(new OnClickListener() {
364 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700365 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 finish();
367 }
368 });
369
370 // set our various button parameters
371 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
372 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
373 if (TextUtils.isEmpty(buttonText)) {
374 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800375 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800376 mNextButton.setText(buttonText);
377 }
378 }
379 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
380 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
381 if (TextUtils.isEmpty(buttonText)) {
382 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800383 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800384 backButton.setText(buttonText);
385 }
386 }
387 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
388 skipButton.setVisibility(View.VISIBLE);
389 }
390 }
391 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700392
Fan Zhangefba6b42017-02-07 17:40:59 -0800393 if (DEBUG_TIMING) {
394 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
395 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700396 }
397
Doris Lingcb69baf2017-02-23 17:50:03 -0800398 @VisibleForTesting
399 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
400 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800401 setTitleFromIntent(intent);
402
403 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
404 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800405 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800406 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700407 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800408 mInitialTitleResId = R.string.dashboard_title;
409
410 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800411 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800412 }
413 }
414
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700415 private void setTitleFromIntent(Intent intent) {
416 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
417 if (initialTitleResId > 0) {
418 mInitialTitle = null;
419 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100420
421 final String initialTitleResPackageName = intent.getStringExtra(
422 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
423 if (initialTitleResPackageName != null) {
424 try {
425 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
426 0 /* flags */, new UserHandle(UserHandle.myUserId()));
427 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
428 setTitle(mInitialTitle);
429 mInitialTitleResId = -1;
430 return;
431 } catch (NameNotFoundException e) {
432 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
433 }
434 } else {
435 setTitle(mInitialTitleResId);
436 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700437 } else {
438 mInitialTitleResId = -1;
439 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
440 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
441 setTitle(mInitialTitle);
442 }
443 }
444
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800445 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800446 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700447 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800448 }
449
Fan Zhang28691572016-03-23 15:31:08 -0700450 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800451 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700452
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800453 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700454 if (mInitialTitleResId > 0) {
455 setTitle(mInitialTitleResId);
456 } else {
457 setTitle(mInitialTitle);
458 }
Fan Zhang28691572016-03-23 15:31:08 -0700459 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800460 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700461
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800462 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
463 setTitleFromBackStackEntry(bse);
464 }
465
466 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
467 final CharSequence title;
468 final int titleRes = bse.getBreadCrumbTitleRes();
469 if (titleRes > 0) {
470 title = getText(titleRes);
471 } else {
472 title = bse.getBreadCrumbTitle();
473 }
474 if (title != null) {
475 setTitle(title);
476 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800477 }
478
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800479 @Override
480 protected void onSaveInstanceState(Bundle outState) {
481 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700482 saveState(outState);
483 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800484
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700485 /**
486 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
487 */
488 @VisibleForTesting
489 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700490 if (mCategories.size() > 0) {
491 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800493 }
494
495 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400496 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800497 super.onResume();
498
Tony Mantler3d84d562017-07-31 10:48:55 -0700499 mDevelopmentSettingsListener = new BroadcastReceiver() {
500 @Override
501 public void onReceive(Context context, Intent intent) {
502 updateTilesList();
503 }
504 };
505 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
506 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800508 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700509
Jason Monk4da79e02015-09-10 10:54:36 -0400510 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800511 }
512
513 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400514 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700516 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
517 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800518 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800519 }
520
521 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800522 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
523 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
524 taskDescription.setIcon(icon);
525 super.setTaskDescription(taskDescription);
526 }
527
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800528 protected boolean isValidFragment(String fragmentName) {
529 // Almost all fragments are wrapped in this,
530 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800531 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
532 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 }
534 return false;
535 }
536
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800537 @Override
538 public Intent getIntent() {
539 Intent superIntent = super.getIntent();
540 String startingFragment = getStartingFragmentClass(superIntent);
541 // This is called from super.onCreate, isMultiPane() is not yet reliable
542 // Do not use onIsHidingHeaders either, which relies itself on this method
543 if (startingFragment != null) {
544 Intent modIntent = new Intent(superIntent);
545 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
546 Bundle args = superIntent.getExtras();
547 if (args != null) {
548 args = new Bundle(args);
549 } else {
550 args = new Bundle();
551 }
552 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100553 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800554 return modIntent;
555 }
556 return superIntent;
557 }
558
559 /**
560 * Checks if the component name in the intent is different from the Settings class and
561 * returns the class name to load as a fragment.
562 */
563 private String getStartingFragmentClass(Intent intent) {
564 if (mFragmentClass != null) return mFragmentClass;
565
566 String intentClass = intent.getComponent().getClassName();
567 if (intentClass.equals(getClass().getName())) return null;
568
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700569 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
571 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700572 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800573 }
574
575 return intentClass;
576 }
577
578 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000579 * Start a new fragment containing a preference panel. If the preferences
580 * are being displayed in multi-pane mode, the given fragment class will
581 * be instantiated and placed in the appropriate pane. If running in
582 * single-pane mode, a new activity will be launched in which to show the
583 * fragment.
584 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800585 * @param fragmentClass Full name of the class implementing the fragment.
586 * @param args Any desired arguments to supply to the fragment.
587 * @param titleRes Optional resource identifier of the title of this
588 * fragment.
589 * @param titleText Optional text of the title of this fragment.
590 * @param resultTo Optional fragment that result data should be sent to.
591 * If non-null, resultTo.onActivityResult() will be called when this
592 * preference panel is done. The launched panel must use
593 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000594 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fan Zhangf11c8852018-02-06 14:21:01 -0800595 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000596 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800597 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
598 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700599 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800600 if (titleRes < 0 && titleText != null) {
601 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700602 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700603 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800604 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000605 }
606
607 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100608 * Start a new fragment in a new activity containing a preference panel for a given user. If the
609 * preferences are being displayed in multi-pane mode, the given fragment class will be
610 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
611 * activity will be launched in which to show the fragment.
612 *
613 * @param fragmentClass Full name of the class implementing the fragment.
Fan Zhangf11c8852018-02-06 14:21:01 -0800614 * @param args Any desired arguments to supply to the fragment.
615 * @param titleRes Optional resource identifier of the title of this fragment.
Fan Zhangf11c8852018-02-06 14:21:01 -0800616 * @param userHandle The user for which the panel has to be started.
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100617 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800618 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
Fan Zhang615563d2018-02-15 15:19:33 -0800619 Bundle args, int titleRes, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700620 // This is a workaround.
621 //
622 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
623 // starting the fragment could cause a native stack corruption. See b/17523189. However,
624 // adding that flag and start the preference panel with the same UserHandler will make it
625 // impossible to use back button to return to the previous screen. See b/20042570.
626 //
627 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
628 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
629 // when we're calling it as the same user.
630 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhang615563d2018-02-15 15:19:33 -0800631 startPreferencePanel(caller, fragmentClass, args, titleRes, null /* titleText */,
632 null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700633 } else {
Fan Zhang615563d2018-02-15 15:19:33 -0800634 new SubSettingLauncher(this)
635 .setDestination(fragmentClass)
636 .setArguments(args)
637 .setTitle(titleRes)
638 .setIsShortCut(mIsShortcut)
639 .setSourceMetricsCategory(mMetricsFeatureProvider.getMetricsCategory(caller))
640 .setUserHandle(userHandle)
641 .launch();
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100642 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100643 }
644
645 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800646 * Called by a preference panel fragment to finish itself.
647 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800648 * @param caller The fragment that is asking to be finished.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800649 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800650 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800652 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800653 */
654 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
655 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700656 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800657 }
658
659 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000660 * Start a new fragment.
661 *
662 * @param fragment The fragment to start
Fan Zhangf11c8852018-02-06 14:21:01 -0800663 * @param push If true, the current fragment will be pushed onto the back stack. If false,
664 * the current fragment will be replaced.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000665 */
666 public void startPreferenceFragment(Fragment fragment, boolean push) {
667 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700668 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000669 if (push) {
670 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
671 transaction.addToBackStack(BACK_STACK_PREFS);
672 } else {
673 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
674 }
675 transaction.commitAllowingStateLoss();
676 }
677
678 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700679 * Switch to a specific Fragment with taking care of validation, Title and BackStack
680 */
681 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700682 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700683 if (validate && !isValidFragment(fragmentName)) {
684 throw new IllegalArgumentException("Invalid fragment for this activity: "
685 + fragmentName);
686 }
687 Fragment f = Fragment.instantiate(this, fragmentName, args);
688 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700689 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700690 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700691 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700692 }
693 if (addToBackStack) {
694 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
695 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700696 if (titleResId > 0) {
697 transaction.setBreadCrumbTitle(titleResId);
698 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700699 transaction.setBreadCrumbTitle(title);
700 }
701 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700702 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700703 return f;
704 }
705
Jason Monk4da79e02015-09-10 10:54:36 -0400706 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500707 // Generally the items that are will be changing from these updates will
708 // not be in the top list of tiles, so run it in the background and the
709 // SettingsDrawerActivity will pick up on the updates automatically.
710 AsyncTask.execute(new Runnable() {
711 @Override
712 public void run() {
713 doUpdateTilesList();
714 }
715 });
716 }
717
718 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400719 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700720 final UserManager um = UserManager.get(this);
721 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800722 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700723 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400724 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700725 somethingChanged = setTileEnabled(
726 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
727 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700728
Fan Zhangaeb94f02017-07-05 13:46:04 -0700729 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800730 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700731 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
732 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700733
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700734
735 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
736 // enable DataPlanUsageSummaryActivity.
737 somethingChanged = setTileEnabled(
Fan Zhang3ed55b12018-02-07 13:50:09 -0800738 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700739 Utils.isBandwidthControlEnabled() /* enabled */,
740 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700741
jackqdyulei76ba0f72017-11-17 14:14:51 -0800742 final boolean isConnectedDeviceV2Enabled =
743 Settings.ConnectedDeviceDashboardActivity.isEnabled();
744 // Enable new connected page if v2 enabled
745 somethingChanged = setTileEnabled(
746 new ComponentName(packageName,
747 Settings.ConnectedDeviceDashboardActivity.class.getName()),
748 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
749 isAdmin) || somethingChanged;
750 // Enable old connected page if v2 disabled
751 somethingChanged = setTileEnabled(
752 new ComponentName(packageName,
753 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
754 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
755 isAdmin) || somethingChanged;
756
Fan Zhangaeb94f02017-07-05 13:46:04 -0700757 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800758 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700759 Utils.showSimCardTile(this), isAdmin)
760 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700761
Fan Zhangf3144942017-12-22 09:59:37 -0800762 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800763 .getPowerUsageFeatureProvider(this)
764 .isBatteryV2Enabled();
765 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700766 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800767 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800768 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
769 // Enable legacy battery page if v2 disabled
770 somethingChanged = setTileEnabled(new ComponentName(packageName,
771 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
772 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400773
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800774 final boolean isDataUsageSettingsV2Enabled =
775 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
776 // Enable new data usage page if v2 enabled
777 somethingChanged = setTileEnabled(new ComponentName(packageName,
778 Settings.DataUsageSummaryActivity.class.getName()),
779 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
780 || somethingChanged;
781 // Enable legacy data usage page if v2 disabled
782 somethingChanged = setTileEnabled(new ComponentName(packageName,
783 Settings.DataUsageSummaryLegacyActivity.class.getName()),
784 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
785 || somethingChanged;
786
Fan Zhangaeb94f02017-07-05 13:46:04 -0700787 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800788 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400789 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700790 && !Utils.isMonkeyRunning(), isAdmin)
791 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400792
Fan Zhangaeb94f02017-07-05 13:46:04 -0700793 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800794 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700795 !UserManager.isDeviceInDemoMode(this), isAdmin)
796 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800797
Fan Zhangaeb94f02017-07-05 13:46:04 -0700798 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700799 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700800 !UserManager.isDeviceInDemoMode(this), isAdmin)
801 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400802
Tony Mantler3d84d562017-07-31 10:48:55 -0700803 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800804 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800805
Fan Zhang4c26da92017-09-08 15:29:54 -0700806 somethingChanged = setTileEnabled(new ComponentName(packageName,
807 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800808 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400810
Fan Zhanga6986e72017-03-08 09:24:45 -0800811 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 somethingChanged = setTileEnabled(new ComponentName(packageName,
813 BackupSettingsActivity.class.getName()), true, isAdmin)
814 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800815
Fan Zhangaeb94f02017-07-05 13:46:04 -0700816 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800817 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 WifiDisplaySettings.isAvailable(this), isAdmin)
819 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800820
Daniel Nishi1e620952017-12-19 10:15:14 -0800821 // Enable/disable the Me Card page.
822 final boolean isMeCardEnabled = featureFactory
823 .getAccountFeatureProvider()
824 .isMeCardEnabled(this);
825 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800826 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800827 isMeCardEnabled, isAdmin)
828 || somethingChanged;
829 somethingChanged = setTileEnabled(new ComponentName(packageName,
830 Settings.DeviceInfoSettingsActivity.class.getName()),
831 !isMeCardEnabled, isAdmin)
832 || somethingChanged;
833
Jason Monk4da79e02015-09-10 10:54:36 -0400834 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800835
Jason Monk4da79e02015-09-10 10:54:36 -0400836 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800837 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800838 synchronized (categories) {
839 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700840 final int tileCount = category.getTilesCount();
841 for (int i = 0; i < tileCount; i++) {
842 final ComponentName component = category.getTile(i).intent.getComponent();
843
Fan Zhanga6986e72017-03-08 09:24:45 -0800844 final String name = component.getClassName();
845 final boolean isEnabledForRestricted = ArrayUtils.contains(
846 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
847 if (packageName.equals(component.getPackageName())
848 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700849 somethingChanged = setTileEnabled(component, false, isAdmin)
850 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800851 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700852 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700853 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700854 }
855 }
Doris Ling26bf0032016-06-15 18:16:09 -0700856
Fan Zhang224caad2017-01-06 11:42:48 -0800857 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700858 if (somethingChanged) {
859 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
860 updateCategories();
861 } else {
862 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
863 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500864 }
865
Fan Zhangaeb94f02017-07-05 13:46:04 -0700866 /**
867 * @return whether or not the enabled state actually changed.
868 */
869 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400870 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800871 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
872 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400873 enabled = false;
874 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700875 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500876 }
877
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800878 private void getMetaData() {
879 try {
880 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
881 PackageManager.GET_META_DATA);
882 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800883 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
884 } catch (NameNotFoundException nnfe) {
885 // No recovery
886 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
887 }
888 }
889
890 // give subclasses access to the Next button
891 public boolean hasNextButton() {
892 return mNextButton != null;
893 }
894
895 public Button getNextButton() {
896 return mNextButton;
897 }
898
jackqdyulei6c355f52017-03-01 17:37:23 -0800899 @VisibleForTesting
900 Bitmap getBitmapFromXmlResource(int drawableRes) {
901 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
902 Canvas canvas = new Canvas();
903 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
904 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
905 canvas.setBitmap(bitmap);
906 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
907 drawable.draw(canvas);
908
909 return bitmap;
910 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700911}