blob: 7328c76497b2d6dd285f6b2c68ad54a12feb07ff [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;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.view.View;
51import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070052import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080053import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070054import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070055
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080056import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040057import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070058import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000059import com.android.settings.backup.BackupSettingsActivity;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080060import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080064import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070065import com.android.settings.widget.SwitchBar;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080066import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
67import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070068import com.android.settingslib.development.DevelopmentSettingsEnabler;
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;
Fan Zhang4c26da92017-09-08 15:29:54 -070071
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,
Fan Zhangff4da232017-11-09 14:07:23 -080079 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 private static final String LOG_TAG = "Settings";
82
83 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070084 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080085
86 /**
87 * When starting this activity, the invoking Intent can contain this extra
88 * string to specify which fragment should be initially displayed.
89 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
90 * will call isValidFragment() to confirm that the fragment class name is valid for this
91 * activity.
92 */
93 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
94
95 /**
96 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
97 * this extra can also be specified to supply a Bundle of arguments to pass
98 * to that fragment when it is instantiated during the initial creation
99 * of the activity.
100 */
101 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
102
103 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700104 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
105 */
106 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
107
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800108 public static final String BACK_STACK_PREFS = ":settings:prefs";
109
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800110 // extras that allow any preference activity to be launched as part of a wizard
111
112 // show Back and Next buttons? takes boolean parameter
113 // Back will then return RESULT_CANCELED and Next RESULT_OK
114 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
115
116 // add a Skip button?
117 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
118
119 // specify custom text for the Back or Next buttons, or cause a button to not appear
120 // at all by setting it to null
121 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
122 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
123
124 /**
125 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700126 * 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 -0800127 * that fragment.
128 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700129 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100130 /**
131 * The package name used to resolve the title resource id.
132 */
133 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
134 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700135 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
136 ":settings:show_fragment_title_resid";
137 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
138 ":settings:show_fragment_as_shortcut";
139
140 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
141 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800142
Fan Zhang78ab9f92017-02-27 16:10:05 -0800143 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700144 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
145
Jason Monk30695812015-11-17 09:01:08 -0500146 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800147 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800148
149 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
150
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800151 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800152
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800153 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700154 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800155
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700156 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
157 "android.settings.APPLICATION_DETAILS_SETTINGS"
158 };
159
Tony Mantler3d84d562017-07-31 10:48:55 -0700160 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162 private boolean mBatteryPresent = true;
163 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164 @Override
165 public void onReceive(Context context, Intent intent) {
166 String action = intent.getAction();
167 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
168 boolean batteryPresent = Utils.isBatteryPresent(intent);
169
170 if (mBatteryPresent != batteryPresent) {
171 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400172 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800173 }
174 }
175 }
176 };
177
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700178 private SwitchBar mSwitchBar;
179
180 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700181
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700182 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700183 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700184
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700185 private ViewGroup mContent;
186
Fan Zhangc6ca3142017-02-14 15:02:35 -0800187 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700188
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700189 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800190 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191
Fan Zhanga96a2d82016-09-27 17:51:11 -0700192 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700193
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700194 public SwitchBar getSwitchBar() {
195 return mSwitchBar;
196 }
197
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198 @Override
199 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800200 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800201 return true;
202 }
203
204 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400205 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206 return false;
207 }
208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500210 public SharedPreferences getSharedPreferences(String name, int mode) {
211 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800212 return new SharedPreferencesLogger(this, getMetricsTag(),
213 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500214 }
215 return super.getSharedPreferences(name, mode);
216 }
217
Jason Monke4ebcd12016-02-21 09:37:41 -0500218 private String getMetricsTag() {
219 String tag = getClass().getName();
220 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
221 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
222 }
223 if (tag.startsWith("com.android.settings.")) {
224 tag = tag.replace("com.android.settings.", "");
225 }
226 return tag;
227 }
228
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700229 private static boolean isShortCutIntent(final Intent intent) {
230 Set<String> categories = intent.getCategories();
231 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
232 }
233
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700234 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700235 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700236 if (action == null) {
237 return false;
238 }
239 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
240 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
241 }
242 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700243 }
244
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700245 @Override
246 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700247 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500248 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700249
250 final FeatureFactory factory = FeatureFactory.getFactory(this);
251
252 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800253 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700254
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700255 // Should happen before any call to getIntent()
256 getMetaData();
257
258 final Intent intent = getIntent();
259 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
260 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800261 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800262
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700263 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700264 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700265
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700266 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700267 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
268
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700269 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700270 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700271
Doris Ling1694d4b2017-03-03 14:21:37 -0800272 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700273
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700274 // This is a "Sub Settings" when:
275 // - this is a real SubSettings
276 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700277 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700278 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
279
Fan Zhang92f1e942017-01-21 10:07:26 -0800280 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
281 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700282 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800283 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700284 }
285
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700286 setContentView(mIsShowingDashboard ?
287 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700289 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700290
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291 getFragmentManager().addOnBackStackChangedListener(this);
292
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700293 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700294 // We are restarting from a previous saved state; used that to initialize, instead
295 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700296 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800297
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700298 ArrayList<DashboardCategory> categories =
299 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
300 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700301 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700302 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700303 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800305 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800306 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 }
308
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700309 if (mIsShowingDashboard) {
310 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
311 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800312 final Toolbar toolbar = findViewById(R.id.search_action_bar);
313 FeatureFactory.getFactory(this).getSearchFeatureProvider()
314 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700315 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700316
317 // Please forgive me for what I am about to do.
318 //
319 // Need to make the navigation icon non-clickable so that the entire card is clickable
320 // and goes to the search UI. Also set the background to null so there's no ripple.
321 View navView = toolbar.getNavigationView();
322 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800323 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700324 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700325 }
326
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700327 ActionBar actionBar = getActionBar();
328 if (actionBar != null) {
Fan Zhangf11c8852018-02-06 14:21:01 -0800329 actionBar.setDisplayHomeAsUpEnabled(true);
330 actionBar.setHomeButtonEnabled(true);
Fan Zhange096ae72017-11-30 14:41:04 -0800331 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700332 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700333 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500334 if (mSwitchBar != null) {
335 mSwitchBar.setMetricsTag(getMetricsTag());
336 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700337
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800338 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800339 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
340
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700341 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 if (buttonBar != null) {
343 buttonBar.setVisibility(View.VISIBLE);
344
Fan Zhangf11c8852018-02-06 14:21:01 -0800345 Button backButton = (Button) findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 backButton.setOnClickListener(new OnClickListener() {
347 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700348 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 finish();
350 }
351 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800352 Button skipButton = (Button) findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800353 skipButton.setOnClickListener(new OnClickListener() {
354 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700355 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 finish();
357 }
358 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800359 mNextButton = (Button) findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800360 mNextButton.setOnClickListener(new OnClickListener() {
361 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700362 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 finish();
364 }
365 });
366
367 // set our various button parameters
368 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
369 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
370 if (TextUtils.isEmpty(buttonText)) {
371 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800372 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 mNextButton.setText(buttonText);
374 }
375 }
376 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
377 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
378 if (TextUtils.isEmpty(buttonText)) {
379 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800380 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800381 backButton.setText(buttonText);
382 }
383 }
384 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
385 skipButton.setVisibility(View.VISIBLE);
386 }
387 }
388 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700389
Fan Zhangefba6b42017-02-07 17:40:59 -0800390 if (DEBUG_TIMING) {
391 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
392 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700393 }
394
Doris Lingcb69baf2017-02-23 17:50:03 -0800395 @VisibleForTesting
396 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
397 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800398 setTitleFromIntent(intent);
399
400 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
401 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800402 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800403 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700404 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800405 mInitialTitleResId = R.string.dashboard_title;
406
407 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800408 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800409 }
410 }
411
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700412 private void setTitleFromIntent(Intent intent) {
413 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
414 if (initialTitleResId > 0) {
415 mInitialTitle = null;
416 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100417
418 final String initialTitleResPackageName = intent.getStringExtra(
419 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
420 if (initialTitleResPackageName != null) {
421 try {
422 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
423 0 /* flags */, new UserHandle(UserHandle.myUserId()));
424 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
425 setTitle(mInitialTitle);
426 mInitialTitleResId = -1;
427 return;
428 } catch (NameNotFoundException e) {
429 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
430 }
431 } else {
432 setTitle(mInitialTitleResId);
433 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700434 } else {
435 mInitialTitleResId = -1;
436 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
437 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
438 setTitle(mInitialTitle);
439 }
440 }
441
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800442 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800443 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700444 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800445 }
446
Fan Zhang28691572016-03-23 15:31:08 -0700447 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800448 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700449
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800450 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700451 if (mInitialTitleResId > 0) {
452 setTitle(mInitialTitleResId);
453 } else {
454 setTitle(mInitialTitle);
455 }
Fan Zhang28691572016-03-23 15:31:08 -0700456 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800457 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700458
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800459 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
460 setTitleFromBackStackEntry(bse);
461 }
462
463 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
464 final CharSequence title;
465 final int titleRes = bse.getBreadCrumbTitleRes();
466 if (titleRes > 0) {
467 title = getText(titleRes);
468 } else {
469 title = bse.getBreadCrumbTitle();
470 }
471 if (title != null) {
472 setTitle(title);
473 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800474 }
475
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 @Override
477 protected void onSaveInstanceState(Bundle outState) {
478 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700479 saveState(outState);
480 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800481
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700482 /**
483 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
484 */
485 @VisibleForTesting
486 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700487 if (mCategories.size() > 0) {
488 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800489 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490 }
491
492 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400493 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800494 super.onResume();
495
Tony Mantler3d84d562017-07-31 10:48:55 -0700496 mDevelopmentSettingsListener = new BroadcastReceiver() {
497 @Override
498 public void onReceive(Context context, Intent intent) {
499 updateTilesList();
500 }
501 };
502 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
503 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800504
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800505 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700506
Jason Monk4da79e02015-09-10 10:54:36 -0400507 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800508 }
509
510 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400511 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700513 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
514 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800515 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516 }
517
518 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800519 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
520 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
521 taskDescription.setIcon(icon);
522 super.setTaskDescription(taskDescription);
523 }
524
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800525 protected boolean isValidFragment(String fragmentName) {
526 // Almost all fragments are wrapped in this,
527 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800528 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
529 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800530 }
531 return false;
532 }
533
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534 @Override
535 public Intent getIntent() {
536 Intent superIntent = super.getIntent();
537 String startingFragment = getStartingFragmentClass(superIntent);
538 // This is called from super.onCreate, isMultiPane() is not yet reliable
539 // Do not use onIsHidingHeaders either, which relies itself on this method
540 if (startingFragment != null) {
541 Intent modIntent = new Intent(superIntent);
542 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
543 Bundle args = superIntent.getExtras();
544 if (args != null) {
545 args = new Bundle(args);
546 } else {
547 args = new Bundle();
548 }
549 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100550 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800551 return modIntent;
552 }
553 return superIntent;
554 }
555
556 /**
557 * Checks if the component name in the intent is different from the Settings class and
558 * returns the class name to load as a fragment.
559 */
560 private String getStartingFragmentClass(Intent intent) {
561 if (mFragmentClass != null) return mFragmentClass;
562
563 String intentClass = intent.getComponent().getClassName();
564 if (intentClass.equals(getClass().getName())) return null;
565
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700566 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800567 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
568 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700569 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 }
571
572 return intentClass;
573 }
574
575 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000576 * Start a new fragment containing a preference panel. If the preferences
577 * are being displayed in multi-pane mode, the given fragment class will
578 * be instantiated and placed in the appropriate pane. If running in
579 * single-pane mode, a new activity will be launched in which to show the
580 * fragment.
581 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800582 * @param fragmentClass Full name of the class implementing the fragment.
583 * @param args Any desired arguments to supply to the fragment.
584 * @param titleRes Optional resource identifier of the title of this
585 * fragment.
586 * @param titleText Optional text of the title of this fragment.
587 * @param resultTo Optional fragment that result data should be sent to.
588 * If non-null, resultTo.onActivityResult() will be called when this
589 * preference panel is done. The launched panel must use
590 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000591 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fan Zhangf11c8852018-02-06 14:21:01 -0800592 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000593 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800594 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
595 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700596 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800597 if (titleRes < 0 && titleText != null) {
598 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700599 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700600 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800601 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000602 }
603
604 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100605 * Start a new fragment in a new activity containing a preference panel for a given user. If the
606 * preferences are being displayed in multi-pane mode, the given fragment class will be
607 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
608 * activity will be launched in which to show the fragment.
609 *
610 * @param fragmentClass Full name of the class implementing the fragment.
Fan Zhangf11c8852018-02-06 14:21:01 -0800611 * @param args Any desired arguments to supply to the fragment.
612 * @param titleRes Optional resource identifier of the title of this fragment.
613 * @param titleText Optional text of the title of this fragment.
614 * @param userHandle The user for which the panel has to be started.
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100615 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800616 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
617 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700618 // This is a workaround.
619 //
620 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
621 // starting the fragment could cause a native stack corruption. See b/17523189. However,
622 // adding that flag and start the preference panel with the same UserHandler will make it
623 // impossible to use back button to return to the previous screen. See b/20042570.
624 //
625 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
626 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
627 // when we're calling it as the same user.
628 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800629 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700630 } else {
631 String title = null;
632 if (titleRes < 0) {
633 if (titleText != null) {
634 title = titleText.toString();
635 } else {
636 // There not much we can do in that case
637 title = "";
638 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100639 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800640 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
641 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
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 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
735 .getDataPlanFeatureProvider()
736 .isEnabled();
737
738 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
739 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
740 // reverse.
741
742 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
743 // disable DataPlanUsageSummaryActivity.
744 somethingChanged = setTileEnabled(
745 new ComponentName(packageName,
746 isDataPlanFeatureEnabled
747 ? Settings.DataUsageSummaryActivity.class.getName()
748 : Settings.DataPlanUsageSummaryActivity.class.getName()),
749 false /* enabled */,
750 isAdmin) || somethingChanged;
751
752 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
753 // enable DataPlanUsageSummaryActivity.
754 somethingChanged = setTileEnabled(
755 new ComponentName(packageName,
756 isDataPlanFeatureEnabled
757 ? Settings.DataPlanUsageSummaryActivity.class.getName()
758 : Settings.DataUsageSummaryActivity.class.getName()),
759 Utils.isBandwidthControlEnabled() /* enabled */,
760 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700761
jackqdyulei76ba0f72017-11-17 14:14:51 -0800762 final boolean isConnectedDeviceV2Enabled =
763 Settings.ConnectedDeviceDashboardActivity.isEnabled();
764 // Enable new connected page if v2 enabled
765 somethingChanged = setTileEnabled(
766 new ComponentName(packageName,
767 Settings.ConnectedDeviceDashboardActivity.class.getName()),
768 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
769 isAdmin) || somethingChanged;
770 // Enable old connected page if v2 disabled
771 somethingChanged = setTileEnabled(
772 new ComponentName(packageName,
773 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
774 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
775 isAdmin) || somethingChanged;
776
Fan Zhangaeb94f02017-07-05 13:46:04 -0700777 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800778 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700779 Utils.showSimCardTile(this), isAdmin)
780 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700781
Fan Zhangf3144942017-12-22 09:59:37 -0800782 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800783 .getPowerUsageFeatureProvider(this)
784 .isBatteryV2Enabled();
785 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700786 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800787 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800788 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
789 // Enable legacy battery page if v2 disabled
790 somethingChanged = setTileEnabled(new ComponentName(packageName,
791 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
792 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400793
Fan Zhangaeb94f02017-07-05 13:46:04 -0700794 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800795 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400796 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700797 && !Utils.isMonkeyRunning(), isAdmin)
798 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400799
Fan Zhangaeb94f02017-07-05 13:46:04 -0700800 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800801 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700802 !UserManager.isDeviceInDemoMode(this), isAdmin)
803 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800804
Fan Zhangaeb94f02017-07-05 13:46:04 -0700805 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700806 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 !UserManager.isDeviceInDemoMode(this), isAdmin)
808 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400809
Fan Zhangaeb94f02017-07-05 13:46:04 -0700810 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800811 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700812 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
813 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400814
Tony Mantler3d84d562017-07-31 10:48:55 -0700815 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800816 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
817 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800818
Fan Zhang4c26da92017-09-08 15:29:54 -0700819 somethingChanged = setTileEnabled(new ComponentName(packageName,
820 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800821 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700822 || somethingChanged;
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.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700825 somethingChanged = setTileEnabled(new ComponentName(packageName,
826 BackupSettingsActivity.class.getName()), true, isAdmin)
827 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800828
Fan Zhangaeb94f02017-07-05 13:46:04 -0700829 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800830 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700831 WifiDisplaySettings.isAvailable(this), isAdmin)
832 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800833
Daniel Nishi1e620952017-12-19 10:15:14 -0800834 // Enable/disable the Me Card page.
835 final boolean isMeCardEnabled = featureFactory
836 .getAccountFeatureProvider()
837 .isMeCardEnabled(this);
838 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800839 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800840 isMeCardEnabled, isAdmin)
841 || somethingChanged;
842 somethingChanged = setTileEnabled(new ComponentName(packageName,
843 Settings.DeviceInfoSettingsActivity.class.getName()),
844 !isMeCardEnabled, isAdmin)
845 || somethingChanged;
846
Jason Monk4da79e02015-09-10 10:54:36 -0400847 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800848
Jason Monk4da79e02015-09-10 10:54:36 -0400849 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800850 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800851 synchronized (categories) {
852 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700853 final int tileCount = category.getTilesCount();
854 for (int i = 0; i < tileCount; i++) {
855 final ComponentName component = category.getTile(i).intent.getComponent();
856
Fan Zhanga6986e72017-03-08 09:24:45 -0800857 final String name = component.getClassName();
858 final boolean isEnabledForRestricted = ArrayUtils.contains(
859 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
860 if (packageName.equals(component.getPackageName())
861 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700862 somethingChanged = setTileEnabled(component, false, isAdmin)
863 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800864 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700865 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700866 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700867 }
868 }
Doris Ling26bf0032016-06-15 18:16:09 -0700869
Fan Zhang224caad2017-01-06 11:42:48 -0800870 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700871 if (somethingChanged) {
872 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
873 updateCategories();
874 } else {
875 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
876 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500877 }
878
Fan Zhangaeb94f02017-07-05 13:46:04 -0700879 /**
880 * @return whether or not the enabled state actually changed.
881 */
882 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400883 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800884 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
885 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400886 enabled = false;
887 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700888 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500889 }
890
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800891 private void getMetaData() {
892 try {
893 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
894 PackageManager.GET_META_DATA);
895 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800896 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
897 } catch (NameNotFoundException nnfe) {
898 // No recovery
899 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
900 }
901 }
902
903 // give subclasses access to the Next button
904 public boolean hasNextButton() {
905 return mNextButton != null;
906 }
907
908 public Button getNextButton() {
909 return mNextButton;
910 }
911
jackqdyulei6c355f52017-03-01 17:37:23 -0800912 @VisibleForTesting
913 Bitmap getBitmapFromXmlResource(int drawableRes) {
914 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
915 Canvas canvas = new Canvas();
916 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
917 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
918 canvas.setBitmap(bitmap);
919 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
920 drawable.draw(canvas);
921
922 return bitmap;
923 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700924}