blob: 3144ae24440519030ff7c911317f7c4145a2f53c [file] [log] [blame]
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Fan Zhang6013a952018-02-06 11:36:07 -080019import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
20
Fan Zhang60f01072016-10-13 16:53:41 -070021import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070022import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080023import android.app.Fragment;
24import android.app.FragmentManager;
25import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070027import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.SharedPreferences;
32import android.content.pm.ActivityInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080035import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080036import android.graphics.Canvas;
37import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050038import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080039import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080040import android.os.UserHandle;
41import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080042import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070044import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040045import android.support.v7.preference.Preference;
46import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070048import android.transition.TransitionManager;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080049import android.util.FeatureFlagUtils;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080050import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080051import android.view.View;
52import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070053import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080054import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070055import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070056
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080057import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040058import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070059import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000060import com.android.settings.backup.BackupSettingsActivity;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080061import com.android.settings.core.FeatureFlags;
Fan Zhang615563d2018-02-15 15:19:33 -080062import com.android.settings.core.SubSettingLauncher;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080063import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070064import com.android.settings.dashboard.DashboardFeatureProvider;
65import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070066import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080067import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070068import com.android.settings.widget.SwitchBar;
Fan Zhang7cf99f52018-02-16 10:37:37 -080069import com.android.settingslib.core.instrumentation.Instrumentable;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080070import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070071import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040072import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040073import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070074
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080076import java.util.List;
77
Jason Monk4da79e02015-09-10 10:54:36 -040078public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080079 implements PreferenceManager.OnPreferenceTreeClickListener,
80 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080081 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080082
83 private static final String LOG_TAG = "Settings";
84
85 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070086 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087
88 /**
89 * When starting this activity, the invoking Intent can contain this extra
90 * string to specify which fragment should be initially displayed.
91 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
92 * will call isValidFragment() to confirm that the fragment class name is valid for this
93 * activity.
94 */
95 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
96
97 /**
98 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
99 * this extra can also be specified to supply a Bundle of arguments to pass
100 * to that fragment when it is instantiated during the initial creation
101 * of the activity.
102 */
103 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
104
105 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700106 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
107 */
108 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
109
Fabrice Di Meglio6f0739a2014-02-03 18:12:25 -0800110 public static final String BACK_STACK_PREFS = ":settings:prefs";
111
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800112 // extras that allow any preference activity to be launched as part of a wizard
113
114 // show Back and Next buttons? takes boolean parameter
115 // Back will then return RESULT_CANCELED and Next RESULT_OK
116 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
117
118 // add a Skip button?
119 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
120
121 // specify custom text for the Back or Next buttons, or cause a button to not appear
122 // at all by setting it to null
123 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
124 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
125
126 /**
127 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700128 * 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 -0800129 * that fragment.
130 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700131 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100132 /**
133 * The package name used to resolve the title resource id.
134 */
135 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
136 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700137 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
138 ":settings:show_fragment_title_resid";
139 public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
140 ":settings:show_fragment_as_shortcut";
141
142 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
143 ":settings:show_fragment_as_subsetting";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800144
Fan Zhang78ab9f92017-02-27 16:10:05 -0800145 @Deprecated
Udam Saini92779ce2016-03-28 14:29:48 -0700146 public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
147
Jason Monk30695812015-11-17 09:01:08 -0500148 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800149 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800150
151 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
152
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800153 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800154
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800155 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700156 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800157
Tony Mantler3d84d562017-07-31 10:48:55 -0700158 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800159
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800160 private boolean mBatteryPresent = true;
161 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800162 @Override
163 public void onReceive(Context context, Intent intent) {
164 String action = intent.getAction();
165 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
166 boolean batteryPresent = Utils.isBatteryPresent(intent);
167
168 if (mBatteryPresent != batteryPresent) {
169 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400170 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800171 }
172 }
173 }
174 };
175
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700176 private SwitchBar mSwitchBar;
177
178 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700179
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700180 private boolean mIsShowingDashboard;
181
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700182 private ViewGroup mContent;
183
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700184 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800185 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800186
Fan Zhanga96a2d82016-09-27 17:51:11 -0700187 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700188
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700189 public SwitchBar getSwitchBar() {
190 return mSwitchBar;
191 }
192
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800193 @Override
194 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800195 new SubSettingLauncher(this)
196 .setDestination(pref.getFragment())
197 .setArguments(pref.getExtras())
198 .setSourceMetricsCategory(caller instanceof Instrumentable
199 ? ((Instrumentable) caller).getMetricsCategory()
200 : Instrumentable.METRICS_CATEGORY_UNKNOWN)
201 .setTitle(-1)
202 .launch();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800203 return true;
204 }
205
206 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400207 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800208 return false;
209 }
210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500212 public SharedPreferences getSharedPreferences(String name, int mode) {
213 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800214 return new SharedPreferencesLogger(this, getMetricsTag(),
215 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500216 }
217 return super.getSharedPreferences(name, mode);
218 }
219
Jason Monke4ebcd12016-02-21 09:37:41 -0500220 private String getMetricsTag() {
221 String tag = getClass().getName();
222 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
223 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
224 }
225 if (tag.startsWith("com.android.settings.")) {
226 tag = tag.replace("com.android.settings.", "");
227 }
228 return tag;
229 }
230
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700231 @Override
232 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700233 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500234 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700235
236 final FeatureFactory factory = FeatureFactory.getFactory(this);
237
238 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700239
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700240 // Should happen before any call to getIntent()
241 getMetaData();
242
243 final Intent intent = getIntent();
244 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
245 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800246 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800247
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700248 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700249 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700250
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700251 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700252 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700253
Doris Ling1694d4b2017-03-03 14:21:37 -0800254 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700255
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700256 // This is a "Sub Settings" when:
257 // - this is a real SubSettings
258 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700259 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700260 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
261
Fan Zhang92f1e942017-01-21 10:07:26 -0800262 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
263 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700264 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800265 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700266 }
267
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700268 setContentView(mIsShowingDashboard ?
269 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700271 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700272
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 getFragmentManager().addOnBackStackChangedListener(this);
274
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700275 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700276 // We are restarting from a previous saved state; used that to initialize, instead
277 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700278 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800279
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700280 ArrayList<DashboardCategory> categories =
281 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
282 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700283 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700284 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700285 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800286 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800288 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289 }
290
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700291 if (mIsShowingDashboard) {
292 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
293 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800294 final Toolbar toolbar = findViewById(R.id.search_action_bar);
295 FeatureFactory.getFactory(this).getSearchFeatureProvider()
296 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700297 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700298
299 // Please forgive me for what I am about to do.
300 //
301 // Need to make the navigation icon non-clickable so that the entire card is clickable
302 // and goes to the search UI. Also set the background to null so there's no ripple.
303 View navView = toolbar.getNavigationView();
304 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800305 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700306 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700307 }
308
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700309 ActionBar actionBar = getActionBar();
310 if (actionBar != null) {
Fan Zhangf11c8852018-02-06 14:21:01 -0800311 actionBar.setDisplayHomeAsUpEnabled(true);
312 actionBar.setHomeButtonEnabled(true);
Fan Zhange096ae72017-11-30 14:41:04 -0800313 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700314 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700315 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500316 if (mSwitchBar != null) {
317 mSwitchBar.setMetricsTag(getMetricsTag());
318 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700319
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
322
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700323 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 if (buttonBar != null) {
325 buttonBar.setVisibility(View.VISIBLE);
326
Fan Zhangf11c8852018-02-06 14:21:01 -0800327 Button backButton = (Button) findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800328 backButton.setOnClickListener(new OnClickListener() {
329 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700330 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800331 finish();
332 }
333 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800334 Button skipButton = (Button) findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800335 skipButton.setOnClickListener(new OnClickListener() {
336 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700337 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800338 finish();
339 }
340 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800341 mNextButton = (Button) findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800342 mNextButton.setOnClickListener(new OnClickListener() {
343 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700344 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800345 finish();
346 }
347 });
348
349 // set our various button parameters
350 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
351 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
352 if (TextUtils.isEmpty(buttonText)) {
353 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800354 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 mNextButton.setText(buttonText);
356 }
357 }
358 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
359 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
360 if (TextUtils.isEmpty(buttonText)) {
361 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800362 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 backButton.setText(buttonText);
364 }
365 }
366 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
367 skipButton.setVisibility(View.VISIBLE);
368 }
369 }
370 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700371
Fan Zhangefba6b42017-02-07 17:40:59 -0800372 if (DEBUG_TIMING) {
373 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
374 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700375 }
376
Doris Lingcb69baf2017-02-23 17:50:03 -0800377 @VisibleForTesting
378 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
379 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800380 setTitleFromIntent(intent);
381
382 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
383 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800384 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800385 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700386 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800387 mInitialTitleResId = R.string.dashboard_title;
388
389 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800390 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800391 }
392 }
393
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700394 private void setTitleFromIntent(Intent intent) {
395 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
396 if (initialTitleResId > 0) {
397 mInitialTitle = null;
398 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100399
400 final String initialTitleResPackageName = intent.getStringExtra(
401 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
402 if (initialTitleResPackageName != null) {
403 try {
404 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
405 0 /* flags */, new UserHandle(UserHandle.myUserId()));
406 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
407 setTitle(mInitialTitle);
408 mInitialTitleResId = -1;
409 return;
410 } catch (NameNotFoundException e) {
411 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
412 }
413 } else {
414 setTitle(mInitialTitleResId);
415 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700416 } else {
417 mInitialTitleResId = -1;
418 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
419 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
420 setTitle(mInitialTitle);
421 }
422 }
423
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800424 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800425 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700426 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800427 }
428
Fan Zhang28691572016-03-23 15:31:08 -0700429 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800430 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700431
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800432 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700433 if (mInitialTitleResId > 0) {
434 setTitle(mInitialTitleResId);
435 } else {
436 setTitle(mInitialTitle);
437 }
Fan Zhang28691572016-03-23 15:31:08 -0700438 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800439 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700440
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800441 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
442 setTitleFromBackStackEntry(bse);
443 }
444
445 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
446 final CharSequence title;
447 final int titleRes = bse.getBreadCrumbTitleRes();
448 if (titleRes > 0) {
449 title = getText(titleRes);
450 } else {
451 title = bse.getBreadCrumbTitle();
452 }
453 if (title != null) {
454 setTitle(title);
455 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800456 }
457
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800458 @Override
459 protected void onSaveInstanceState(Bundle outState) {
460 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700461 saveState(outState);
462 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800463
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700464 /**
465 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
466 */
467 @VisibleForTesting
468 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700469 if (mCategories.size() > 0) {
470 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800471 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800472 }
473
474 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400475 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800476 super.onResume();
477
Tony Mantler3d84d562017-07-31 10:48:55 -0700478 mDevelopmentSettingsListener = new BroadcastReceiver() {
479 @Override
480 public void onReceive(Context context, Intent intent) {
481 updateTilesList();
482 }
483 };
484 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
485 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800486
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800487 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700488
Jason Monk4da79e02015-09-10 10:54:36 -0400489 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800490 }
491
492 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400493 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800494 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700495 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
496 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800497 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800498 }
499
500 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800501 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
502 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
503 taskDescription.setIcon(icon);
504 super.setTaskDescription(taskDescription);
505 }
506
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507 protected boolean isValidFragment(String fragmentName) {
508 // Almost all fragments are wrapped in this,
509 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800510 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
511 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800512 }
513 return false;
514 }
515
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800516 @Override
517 public Intent getIntent() {
518 Intent superIntent = super.getIntent();
519 String startingFragment = getStartingFragmentClass(superIntent);
520 // This is called from super.onCreate, isMultiPane() is not yet reliable
521 // Do not use onIsHidingHeaders either, which relies itself on this method
522 if (startingFragment != null) {
523 Intent modIntent = new Intent(superIntent);
524 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
525 Bundle args = superIntent.getExtras();
526 if (args != null) {
527 args = new Bundle(args);
528 } else {
529 args = new Bundle();
530 }
531 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100532 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800533 return modIntent;
534 }
535 return superIntent;
536 }
537
538 /**
539 * Checks if the component name in the intent is different from the Settings class and
540 * returns the class name to load as a fragment.
541 */
542 private String getStartingFragmentClass(Intent intent) {
543 if (mFragmentClass != null) return mFragmentClass;
544
545 String intentClass = intent.getComponent().getClassName();
546 if (intentClass.equals(getClass().getName())) return null;
547
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700548 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800549 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
550 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700551 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800552 }
553
554 return intentClass;
555 }
556
557 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800558 * Called by a preference panel fragment to finish itself.
559 *
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800561 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 * @param resultData Optional result data 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 */
Fan Zhang47854c22018-02-19 13:54:09 -0800565 public void finishPreferencePanel(int resultCode, Intent resultData) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700567 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800568 }
569
570 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700571 * Switch to a specific Fragment with taking care of validation, Title and BackStack
572 */
573 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700574 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700575 if (validate && !isValidFragment(fragmentName)) {
576 throw new IllegalArgumentException("Invalid fragment for this activity: "
577 + fragmentName);
578 }
579 Fragment f = Fragment.instantiate(this, fragmentName, args);
580 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700581 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700582 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700583 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700584 }
585 if (addToBackStack) {
586 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
587 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700588 if (titleResId > 0) {
589 transaction.setBreadCrumbTitle(titleResId);
590 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700591 transaction.setBreadCrumbTitle(title);
592 }
593 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700594 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700595 return f;
596 }
597
Jason Monk4da79e02015-09-10 10:54:36 -0400598 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500599 // Generally the items that are will be changing from these updates will
600 // not be in the top list of tiles, so run it in the background and the
601 // SettingsDrawerActivity will pick up on the updates automatically.
602 AsyncTask.execute(new Runnable() {
603 @Override
604 public void run() {
605 doUpdateTilesList();
606 }
607 });
608 }
609
610 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400611 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700612 final UserManager um = UserManager.get(this);
613 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800614 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700615 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400616 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700617 somethingChanged = setTileEnabled(
618 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
619 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700620
Fan Zhangaeb94f02017-07-05 13:46:04 -0700621 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800622 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700623 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
624 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700625
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700626
627 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
628 // enable DataPlanUsageSummaryActivity.
629 somethingChanged = setTileEnabled(
Fan Zhang3ed55b12018-02-07 13:50:09 -0800630 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700631 Utils.isBandwidthControlEnabled() /* enabled */,
632 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700633
jackqdyulei76ba0f72017-11-17 14:14:51 -0800634 final boolean isConnectedDeviceV2Enabled =
635 Settings.ConnectedDeviceDashboardActivity.isEnabled();
636 // Enable new connected page if v2 enabled
637 somethingChanged = setTileEnabled(
638 new ComponentName(packageName,
639 Settings.ConnectedDeviceDashboardActivity.class.getName()),
640 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
641 isAdmin) || somethingChanged;
642 // Enable old connected page if v2 disabled
643 somethingChanged = setTileEnabled(
644 new ComponentName(packageName,
645 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
646 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
647 isAdmin) || somethingChanged;
648
Fan Zhangaeb94f02017-07-05 13:46:04 -0700649 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800650 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700651 Utils.showSimCardTile(this), isAdmin)
652 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700653
Fan Zhangf3144942017-12-22 09:59:37 -0800654 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800655 .getPowerUsageFeatureProvider(this)
656 .isBatteryV2Enabled();
657 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700658 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800659 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800660 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
661 // Enable legacy battery page if v2 disabled
662 somethingChanged = setTileEnabled(new ComponentName(packageName,
663 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
664 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400665
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800666 final boolean isDataUsageSettingsV2Enabled =
667 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
668 // Enable new data usage page if v2 enabled
669 somethingChanged = setTileEnabled(new ComponentName(packageName,
670 Settings.DataUsageSummaryActivity.class.getName()),
671 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
672 || somethingChanged;
673 // Enable legacy data usage page if v2 disabled
674 somethingChanged = setTileEnabled(new ComponentName(packageName,
675 Settings.DataUsageSummaryLegacyActivity.class.getName()),
676 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
677 || somethingChanged;
678
Fan Zhangaeb94f02017-07-05 13:46:04 -0700679 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800680 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400681 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700682 && !Utils.isMonkeyRunning(), isAdmin)
683 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400684
Fan Zhangaeb94f02017-07-05 13:46:04 -0700685 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800686 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700687 !UserManager.isDeviceInDemoMode(this), isAdmin)
688 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800689
Fan Zhangaeb94f02017-07-05 13:46:04 -0700690 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700691 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700692 !UserManager.isDeviceInDemoMode(this), isAdmin)
693 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400694
Tony Mantler3d84d562017-07-31 10:48:55 -0700695 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800696 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800697
Fan Zhang4c26da92017-09-08 15:29:54 -0700698 somethingChanged = setTileEnabled(new ComponentName(packageName,
699 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800700 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700701 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400702
Fan Zhanga6986e72017-03-08 09:24:45 -0800703 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700704 somethingChanged = setTileEnabled(new ComponentName(packageName,
705 BackupSettingsActivity.class.getName()), true, isAdmin)
706 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800707
Fan Zhangaeb94f02017-07-05 13:46:04 -0700708 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800709 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700710 WifiDisplaySettings.isAvailable(this), isAdmin)
711 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800712
Daniel Nishi1e620952017-12-19 10:15:14 -0800713 // Enable/disable the Me Card page.
714 final boolean isMeCardEnabled = featureFactory
715 .getAccountFeatureProvider()
716 .isMeCardEnabled(this);
717 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800718 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800719 isMeCardEnabled, isAdmin)
720 || somethingChanged;
721 somethingChanged = setTileEnabled(new ComponentName(packageName,
722 Settings.DeviceInfoSettingsActivity.class.getName()),
723 !isMeCardEnabled, isAdmin)
724 || somethingChanged;
725
Jason Monk4da79e02015-09-10 10:54:36 -0400726 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800727
Jason Monk4da79e02015-09-10 10:54:36 -0400728 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800729 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800730 synchronized (categories) {
731 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700732 final int tileCount = category.getTilesCount();
733 for (int i = 0; i < tileCount; i++) {
734 final ComponentName component = category.getTile(i).intent.getComponent();
735
Fan Zhanga6986e72017-03-08 09:24:45 -0800736 final String name = component.getClassName();
737 final boolean isEnabledForRestricted = ArrayUtils.contains(
738 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
739 if (packageName.equals(component.getPackageName())
740 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700741 somethingChanged = setTileEnabled(component, false, isAdmin)
742 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800743 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700744 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700745 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700746 }
747 }
Doris Ling26bf0032016-06-15 18:16:09 -0700748
Fan Zhang224caad2017-01-06 11:42:48 -0800749 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700750 if (somethingChanged) {
751 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
752 updateCategories();
753 } else {
754 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
755 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500756 }
757
Fan Zhangaeb94f02017-07-05 13:46:04 -0700758 /**
759 * @return whether or not the enabled state actually changed.
760 */
761 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400762 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800763 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
764 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400765 enabled = false;
766 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700767 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500768 }
769
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800770 private void getMetaData() {
771 try {
772 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
773 PackageManager.GET_META_DATA);
774 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800775 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
776 } catch (NameNotFoundException nnfe) {
777 // No recovery
778 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
779 }
780 }
781
782 // give subclasses access to the Next button
783 public boolean hasNextButton() {
784 return mNextButton != null;
785 }
786
787 public Button getNextButton() {
788 return mNextButton;
789 }
790
jackqdyulei6c355f52017-03-01 17:37:23 -0800791 @VisibleForTesting
792 Bitmap getBitmapFromXmlResource(int drawableRes) {
793 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
794 Canvas canvas = new Canvas();
795 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
796 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
797 canvas.setBitmap(bitmap);
798 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
799 drawable.draw(canvas);
800
801 return bitmap;
802 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700803}