blob: 83bb082ffa50e7e907045e2db5d4c8974f49e096 [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;
Christine Franks4c3c7fb2018-02-20 11:52:44 -080066import com.android.settings.development.DevelopmentSettingsDashboardFragment;
Fan Zhanga96a2d82016-09-27 17:51:11 -070067import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080068import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070069import com.android.settings.widget.SwitchBar;
Fan Zhang7cf99f52018-02-16 10:37:37 -080070import com.android.settingslib.core.instrumentation.Instrumentable;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080071import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070072import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040073import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040074import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070075
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077import java.util.List;
78
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
Tony Mantler3d84d562017-07-31 10:48:55 -0700159 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800160
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800161 private boolean mBatteryPresent = true;
162 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163 @Override
164 public void onReceive(Context context, Intent intent) {
165 String action = intent.getAction();
166 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
167 boolean batteryPresent = Utils.isBatteryPresent(intent);
168
169 if (mBatteryPresent != batteryPresent) {
170 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400171 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 }
173 }
174 }
175 };
176
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700177 private SwitchBar mSwitchBar;
178
179 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700180
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700181 private boolean mIsShowingDashboard;
182
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700183 private ViewGroup mContent;
184
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700185 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800186 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800187
Fan Zhanga96a2d82016-09-27 17:51:11 -0700188 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700189
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700190 public SwitchBar getSwitchBar() {
191 return mSwitchBar;
192 }
193
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800194 @Override
195 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800196 new SubSettingLauncher(this)
197 .setDestination(pref.getFragment())
198 .setArguments(pref.getExtras())
199 .setSourceMetricsCategory(caller instanceof Instrumentable
200 ? ((Instrumentable) caller).getMetricsCategory()
201 : Instrumentable.METRICS_CATEGORY_UNKNOWN)
202 .setTitle(-1)
203 .launch();
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 Megliod25314d2014-03-21 19:24:43 -0700232 @Override
233 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700234 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500235 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700236
237 final FeatureFactory factory = FeatureFactory.getFactory(this);
238
239 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700240
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700241 // Should happen before any call to getIntent()
242 getMetaData();
243
244 final Intent intent = getIntent();
245 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
246 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800248
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700249 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700250 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700251
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700252 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700253 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700254
Doris Ling1694d4b2017-03-03 14:21:37 -0800255 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700256
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700257 // This is a "Sub Settings" when:
258 // - this is a real SubSettings
259 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700260 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700261 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
262
Fan Zhang92f1e942017-01-21 10:07:26 -0800263 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
264 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700265 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800266 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700267 }
268
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700269 setContentView(mIsShowingDashboard ?
270 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800271
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700272 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700273
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274 getFragmentManager().addOnBackStackChangedListener(this);
275
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700276 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700277 // We are restarting from a previous saved state; used that to initialize, instead
278 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700279 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800280
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700281 ArrayList<DashboardCategory> categories =
282 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
283 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700284 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700285 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700286 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800288 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800289 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290 }
291
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700292 if (mIsShowingDashboard) {
293 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
294 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800295 final Toolbar toolbar = findViewById(R.id.search_action_bar);
296 FeatureFactory.getFactory(this).getSearchFeatureProvider()
297 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700298 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700299
300 // Please forgive me for what I am about to do.
301 //
302 // Need to make the navigation icon non-clickable so that the entire card is clickable
303 // and goes to the search UI. Also set the background to null so there's no ripple.
304 View navView = toolbar.getNavigationView();
305 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800306 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700307 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700308 }
309
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700310 ActionBar actionBar = getActionBar();
311 if (actionBar != null) {
Tony Make226b382018-03-02 18:23:35 +0000312 boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
313 actionBar.setDisplayHomeAsUpEnabled(deviceProvisioned);
314 actionBar.setHomeButtonEnabled(deviceProvisioned);
Fan Zhange096ae72017-11-30 14:41:04 -0800315 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700316 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700317 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500318 if (mSwitchBar != null) {
319 mSwitchBar.setMetricsTag(getMetricsTag());
320 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700321
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
324
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700325 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 if (buttonBar != null) {
327 buttonBar.setVisibility(View.VISIBLE);
328
Fan Zhangf11c8852018-02-06 14:21:01 -0800329 Button backButton = (Button) findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800330 backButton.setOnClickListener(new OnClickListener() {
331 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700332 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800333 finish();
334 }
335 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800336 Button skipButton = (Button) findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800337 skipButton.setOnClickListener(new OnClickListener() {
338 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700339 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800340 finish();
341 }
342 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800343 mNextButton = (Button) findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800344 mNextButton.setOnClickListener(new OnClickListener() {
345 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700346 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800347 finish();
348 }
349 });
350
351 // set our various button parameters
352 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
353 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
354 if (TextUtils.isEmpty(buttonText)) {
355 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800356 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 mNextButton.setText(buttonText);
358 }
359 }
360 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
361 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
362 if (TextUtils.isEmpty(buttonText)) {
363 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800364 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800365 backButton.setText(buttonText);
366 }
367 }
368 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
369 skipButton.setVisibility(View.VISIBLE);
370 }
371 }
372 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700373
Fan Zhangefba6b42017-02-07 17:40:59 -0800374 if (DEBUG_TIMING) {
375 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
376 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700377 }
378
Doris Lingcb69baf2017-02-23 17:50:03 -0800379 @VisibleForTesting
380 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
381 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800382 setTitleFromIntent(intent);
383
384 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
385 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800386 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800387 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700388 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800389 mInitialTitleResId = R.string.dashboard_title;
390
391 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800392 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800393 }
394 }
395
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700396 private void setTitleFromIntent(Intent intent) {
397 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
398 if (initialTitleResId > 0) {
399 mInitialTitle = null;
400 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100401
402 final String initialTitleResPackageName = intent.getStringExtra(
403 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
404 if (initialTitleResPackageName != null) {
405 try {
406 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
407 0 /* flags */, new UserHandle(UserHandle.myUserId()));
408 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
409 setTitle(mInitialTitle);
410 mInitialTitleResId = -1;
411 return;
412 } catch (NameNotFoundException e) {
413 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
414 }
415 } else {
416 setTitle(mInitialTitleResId);
417 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700418 } else {
419 mInitialTitleResId = -1;
420 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
421 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
422 setTitle(mInitialTitle);
423 }
424 }
425
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800426 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800427 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700428 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800429 }
430
Fan Zhang28691572016-03-23 15:31:08 -0700431 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800432 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700433
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800434 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700435 if (mInitialTitleResId > 0) {
436 setTitle(mInitialTitleResId);
437 } else {
438 setTitle(mInitialTitle);
439 }
Fan Zhang28691572016-03-23 15:31:08 -0700440 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800441 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700442
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800443 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
444 setTitleFromBackStackEntry(bse);
445 }
446
447 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
448 final CharSequence title;
449 final int titleRes = bse.getBreadCrumbTitleRes();
450 if (titleRes > 0) {
451 title = getText(titleRes);
452 } else {
453 title = bse.getBreadCrumbTitle();
454 }
455 if (title != null) {
456 setTitle(title);
457 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800458 }
459
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800460 @Override
461 protected void onSaveInstanceState(Bundle outState) {
462 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700463 saveState(outState);
464 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800465
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700466 /**
467 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
468 */
469 @VisibleForTesting
470 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700471 if (mCategories.size() > 0) {
472 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800473 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800474 }
475
476 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400477 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 super.onResume();
479
Tony Mantler3d84d562017-07-31 10:48:55 -0700480 mDevelopmentSettingsListener = new BroadcastReceiver() {
481 @Override
482 public void onReceive(Context context, Intent intent) {
483 updateTilesList();
484 }
485 };
486 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
487 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800488
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800489 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700490
Jason Monk4da79e02015-09-10 10:54:36 -0400491 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 }
493
494 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400495 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800496 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700497 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
498 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800499 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800500 }
501
502 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800503 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
504 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
505 taskDescription.setIcon(icon);
506 super.setTaskDescription(taskDescription);
507 }
508
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800509 protected boolean isValidFragment(String fragmentName) {
510 // Almost all fragments are wrapped in this,
511 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800512 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
513 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800514 }
515 return false;
516 }
517
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800518 @Override
519 public Intent getIntent() {
520 Intent superIntent = super.getIntent();
521 String startingFragment = getStartingFragmentClass(superIntent);
522 // This is called from super.onCreate, isMultiPane() is not yet reliable
523 // Do not use onIsHidingHeaders either, which relies itself on this method
524 if (startingFragment != null) {
525 Intent modIntent = new Intent(superIntent);
526 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Fan Zhange8666c62018-03-02 11:01:15 -0800527 Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800528 if (args != null) {
529 args = new Bundle(args);
530 } else {
531 args = new Bundle();
532 }
533 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100534 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 return modIntent;
536 }
537 return superIntent;
538 }
539
540 /**
541 * Checks if the component name in the intent is different from the Settings class and
542 * returns the class name to load as a fragment.
543 */
544 private String getStartingFragmentClass(Intent intent) {
545 if (mFragmentClass != null) return mFragmentClass;
546
547 String intentClass = intent.getComponent().getClassName();
548 if (intentClass.equals(getClass().getName())) return null;
549
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700550 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800551 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
552 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700553 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800554 }
555
556 return intentClass;
557 }
558
559 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 * Called by a preference panel fragment to finish itself.
561 *
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800563 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800564 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800565 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 */
Fan Zhang47854c22018-02-19 13:54:09 -0800567 public void finishPreferencePanel(int resultCode, Intent resultData) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700569 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800570 }
571
572 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700573 * Switch to a specific Fragment with taking care of validation, Title and BackStack
574 */
575 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700576 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700577 if (validate && !isValidFragment(fragmentName)) {
578 throw new IllegalArgumentException("Invalid fragment for this activity: "
579 + fragmentName);
580 }
581 Fragment f = Fragment.instantiate(this, fragmentName, args);
582 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700583 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700584 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700585 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700586 }
587 if (addToBackStack) {
588 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
589 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700590 if (titleResId > 0) {
591 transaction.setBreadCrumbTitle(titleResId);
592 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700593 transaction.setBreadCrumbTitle(title);
594 }
595 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700596 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700597 return f;
598 }
599
Jason Monk4da79e02015-09-10 10:54:36 -0400600 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500601 // Generally the items that are will be changing from these updates will
602 // not be in the top list of tiles, so run it in the background and the
603 // SettingsDrawerActivity will pick up on the updates automatically.
604 AsyncTask.execute(new Runnable() {
605 @Override
606 public void run() {
607 doUpdateTilesList();
608 }
609 });
610 }
611
612 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400613 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700614 final UserManager um = UserManager.get(this);
615 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800616 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700617 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400618 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700619 somethingChanged = setTileEnabled(
620 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
621 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700622
Fan Zhangaeb94f02017-07-05 13:46:04 -0700623 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800624 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700625 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
626 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700627
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700628
629 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
630 // enable DataPlanUsageSummaryActivity.
631 somethingChanged = setTileEnabled(
Fan Zhang3ed55b12018-02-07 13:50:09 -0800632 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700633 Utils.isBandwidthControlEnabled() /* enabled */,
634 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700635
jackqdyulei76ba0f72017-11-17 14:14:51 -0800636 somethingChanged = setTileEnabled(
637 new ComponentName(packageName,
638 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Jerry Zhang40bd5702018-02-23 14:43:58 -0800639 !UserManager.isDeviceInDemoMode(this) /* enabled */,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800640 isAdmin) || somethingChanged;
641
Fan Zhangaeb94f02017-07-05 13:46:04 -0700642 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800643 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700644 Utils.showSimCardTile(this), isAdmin)
645 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700646
Fan Zhangf3144942017-12-22 09:59:37 -0800647 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800648 .getPowerUsageFeatureProvider(this)
649 .isBatteryV2Enabled();
650 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700651 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800652 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800653 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
654 // Enable legacy battery page if v2 disabled
655 somethingChanged = setTileEnabled(new ComponentName(packageName,
656 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
657 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400658
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800659 final boolean isDataUsageSettingsV2Enabled =
660 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
661 // Enable new data usage page if v2 enabled
662 somethingChanged = setTileEnabled(new ComponentName(packageName,
663 Settings.DataUsageSummaryActivity.class.getName()),
664 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
665 || somethingChanged;
666 // Enable legacy data usage page if v2 disabled
667 somethingChanged = setTileEnabled(new ComponentName(packageName,
668 Settings.DataUsageSummaryLegacyActivity.class.getName()),
669 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
670 || somethingChanged;
671
Fan Zhangaeb94f02017-07-05 13:46:04 -0700672 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800673 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400674 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700675 && !Utils.isMonkeyRunning(), isAdmin)
676 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400677
Fan Zhangaeb94f02017-07-05 13:46:04 -0700678 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800679 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700680 !UserManager.isDeviceInDemoMode(this), isAdmin)
681 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800682
Fan Zhangaeb94f02017-07-05 13:46:04 -0700683 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700684 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700685 !UserManager.isDeviceInDemoMode(this), isAdmin)
686 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400687
Tony Mantler3d84d562017-07-31 10:48:55 -0700688 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800689 && !Utils.isMonkeyRunning();
Christine Franks4c3c7fb2018-02-20 11:52:44 -0800690 final boolean isAdminOrDemo = um.isAdminUser() || um.isDemoUser();
Fan Zhang4c26da92017-09-08 15:29:54 -0700691 somethingChanged = setTileEnabled(new ComponentName(packageName,
692 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
Christine Franks4c3c7fb2018-02-20 11:52:44 -0800693 showDev, isAdminOrDemo)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700694 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400695
Fan Zhanga6986e72017-03-08 09:24:45 -0800696 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700697 somethingChanged = setTileEnabled(new ComponentName(packageName,
698 BackupSettingsActivity.class.getName()), true, isAdmin)
699 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800700
Fan Zhangaeb94f02017-07-05 13:46:04 -0700701 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800702 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700703 WifiDisplaySettings.isAvailable(this), isAdmin)
704 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800705
Daniel Nishi1e620952017-12-19 10:15:14 -0800706 // Enable/disable the Me Card page.
Fan Zhangcfd79a92018-02-28 10:03:43 -0800707 final boolean aboutPhoneV2Enabled = featureFactory
Daniel Nishi1e620952017-12-19 10:15:14 -0800708 .getAccountFeatureProvider()
Fan Zhangcfd79a92018-02-28 10:03:43 -0800709 .isAboutPhoneV2Enabled(this);
Daniel Nishi1e620952017-12-19 10:15:14 -0800710 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800711 Settings.MyDeviceInfoActivity.class.getName()),
Fan Zhangcfd79a92018-02-28 10:03:43 -0800712 aboutPhoneV2Enabled, isAdmin)
Daniel Nishi1e620952017-12-19 10:15:14 -0800713 || somethingChanged;
714 somethingChanged = setTileEnabled(new ComponentName(packageName,
715 Settings.DeviceInfoSettingsActivity.class.getName()),
Fan Zhangcfd79a92018-02-28 10:03:43 -0800716 !aboutPhoneV2Enabled, isAdmin)
Daniel Nishi1e620952017-12-19 10:15:14 -0800717 || somethingChanged;
718
Jason Monk4da79e02015-09-10 10:54:36 -0400719 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800720
Jason Monk4da79e02015-09-10 10:54:36 -0400721 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800722 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800723 synchronized (categories) {
724 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700725 final int tileCount = category.getTilesCount();
726 for (int i = 0; i < tileCount; i++) {
727 final ComponentName component = category.getTile(i).intent.getComponent();
Fan Zhanga6986e72017-03-08 09:24:45 -0800728 final String name = component.getClassName();
729 final boolean isEnabledForRestricted = ArrayUtils.contains(
Christine Franks4c3c7fb2018-02-20 11:52:44 -0800730 SettingsGateway.SETTINGS_FOR_RESTRICTED, name) || (isAdminOrDemo
731 && Settings.DevelopmentSettingsDashboardActivity.class.getName()
732 .equals(name));
Fan Zhanga6986e72017-03-08 09:24:45 -0800733 if (packageName.equals(component.getPackageName())
734 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700735 somethingChanged = setTileEnabled(component, false, isAdmin)
736 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800737 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700738 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700739 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700740 }
741 }
Doris Ling26bf0032016-06-15 18:16:09 -0700742
Fan Zhang224caad2017-01-06 11:42:48 -0800743 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700744 if (somethingChanged) {
745 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
746 updateCategories();
747 } else {
748 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
749 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500750 }
751
Fan Zhangaeb94f02017-07-05 13:46:04 -0700752 /**
753 * @return whether or not the enabled state actually changed.
754 */
755 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400756 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800757 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
758 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400759 enabled = false;
760 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700761 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500762 }
763
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800764 private void getMetaData() {
765 try {
766 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
767 PackageManager.GET_META_DATA);
768 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800769 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
770 } catch (NameNotFoundException nnfe) {
771 // No recovery
772 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
773 }
774 }
775
776 // give subclasses access to the Next button
777 public boolean hasNextButton() {
778 return mNextButton != null;
779 }
780
781 public Button getNextButton() {
782 return mNextButton;
783 }
784
jackqdyulei6c355f52017-03-01 17:37:23 -0800785 @VisibleForTesting
786 Bitmap getBitmapFromXmlResource(int drawableRes) {
787 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
788 Canvas canvas = new Canvas();
789 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
790 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
791 canvas.setBitmap(bitmap);
792 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
793 drawable.draw(canvas);
794
795 return bitmap;
796 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700797}