blob: 76b48d1f271ba9cb83f10f0e38250c9c7ddafba1 [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 Zhanga3e8f5c2016-12-01 09:52:10 -080062import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.dashboard.DashboardFeatureProvider;
64import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070065import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080066import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070067import com.android.settings.widget.SwitchBar;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080068import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
69import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070070import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040071import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040072import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070073
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080074import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070076import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077
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
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700158 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
159 "android.settings.APPLICATION_DETAILS_SETTINGS"
160 };
161
Tony Mantler3d84d562017-07-31 10:48:55 -0700162 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800163
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800164 private boolean mBatteryPresent = true;
165 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 @Override
167 public void onReceive(Context context, Intent intent) {
168 String action = intent.getAction();
169 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
170 boolean batteryPresent = Utils.isBatteryPresent(intent);
171
172 if (mBatteryPresent != batteryPresent) {
173 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400174 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800175 }
176 }
177 }
178 };
179
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700180 private SwitchBar mSwitchBar;
181
182 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700183
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700184 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700185 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700186
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700187 private ViewGroup mContent;
188
Fan Zhangc6ca3142017-02-14 15:02:35 -0800189 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700190
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700191 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800192 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800193
Fan Zhanga96a2d82016-09-27 17:51:11 -0700194 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700195
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700196 public SwitchBar getSwitchBar() {
197 return mSwitchBar;
198 }
199
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800200 @Override
201 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800202 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
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 Meglio0d643fd2014-06-16 20:11:27 -0700231 private static boolean isShortCutIntent(final Intent intent) {
232 Set<String> categories = intent.getCategories();
233 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
234 }
235
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700236 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700237 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700238 if (action == null) {
239 return false;
240 }
241 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
242 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
243 }
244 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700245 }
246
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700247 @Override
248 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700249 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500250 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700251
252 final FeatureFactory factory = FeatureFactory.getFactory(this);
253
254 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800255 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700256
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700257 // Should happen before any call to getIntent()
258 getMetaData();
259
260 final Intent intent = getIntent();
261 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
262 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800263 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800264
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700265 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700266 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700267
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700268 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700269 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
270
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700271 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700272 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700273
Doris Ling1694d4b2017-03-03 14:21:37 -0800274 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700275
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700276 // This is a "Sub Settings" when:
277 // - this is a real SubSettings
278 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700279 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700280 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
281
Fan Zhang92f1e942017-01-21 10:07:26 -0800282 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
283 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700284 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800285 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700286 }
287
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700288 setContentView(mIsShowingDashboard ?
289 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800290
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700291 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700292
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800293 getFragmentManager().addOnBackStackChangedListener(this);
294
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700295 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700296 // We are restarting from a previous saved state; used that to initialize, instead
297 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700298 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800299
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700300 ArrayList<DashboardCategory> categories =
301 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
302 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700303 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700304 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700305 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800308 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 }
310
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700311 if (mIsShowingDashboard) {
312 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
313 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800314 final Toolbar toolbar = findViewById(R.id.search_action_bar);
315 FeatureFactory.getFactory(this).getSearchFeatureProvider()
316 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700317 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700318
319 // Please forgive me for what I am about to do.
320 //
321 // Need to make the navigation icon non-clickable so that the entire card is clickable
322 // and goes to the search UI. Also set the background to null so there's no ripple.
323 View navView = toolbar.getNavigationView();
324 navView.setClickable(false);
Fan Zhang6013a952018-02-06 11:36:07 -0800325 navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700326 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700327 }
328
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700329 ActionBar actionBar = getActionBar();
330 if (actionBar != null) {
Fan Zhangf11c8852018-02-06 14:21:01 -0800331 actionBar.setDisplayHomeAsUpEnabled(true);
332 actionBar.setHomeButtonEnabled(true);
Fan Zhange096ae72017-11-30 14:41:04 -0800333 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700334 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700335 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500336 if (mSwitchBar != null) {
337 mSwitchBar.setMetricsTag(getMetricsTag());
338 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700339
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800340 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
342
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700343 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800344 if (buttonBar != null) {
345 buttonBar.setVisibility(View.VISIBLE);
346
Fan Zhangf11c8852018-02-06 14:21:01 -0800347 Button backButton = (Button) findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 backButton.setOnClickListener(new OnClickListener() {
349 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700350 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351 finish();
352 }
353 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800354 Button skipButton = (Button) findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800355 skipButton.setOnClickListener(new OnClickListener() {
356 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700357 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800358 finish();
359 }
360 });
Fan Zhangf11c8852018-02-06 14:21:01 -0800361 mNextButton = (Button) findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 mNextButton.setOnClickListener(new OnClickListener() {
363 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700364 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800365 finish();
366 }
367 });
368
369 // set our various button parameters
370 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
371 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
372 if (TextUtils.isEmpty(buttonText)) {
373 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800374 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800375 mNextButton.setText(buttonText);
376 }
377 }
378 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
379 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
380 if (TextUtils.isEmpty(buttonText)) {
381 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800382 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800383 backButton.setText(buttonText);
384 }
385 }
386 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
387 skipButton.setVisibility(View.VISIBLE);
388 }
389 }
390 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700391
Fan Zhangefba6b42017-02-07 17:40:59 -0800392 if (DEBUG_TIMING) {
393 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
394 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700395 }
396
Doris Lingcb69baf2017-02-23 17:50:03 -0800397 @VisibleForTesting
398 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
399 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800400 setTitleFromIntent(intent);
401
402 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
403 switchToFragment(initialFragmentName, initialArguments, true, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800404 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800405 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700406 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800407 mInitialTitleResId = R.string.dashboard_title;
408
409 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
Fan Zhangf11c8852018-02-06 14:21:01 -0800410 mInitialTitleResId, mInitialTitle, false);
Doris Lingcb69baf2017-02-23 17:50:03 -0800411 }
412 }
413
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700414 private void setTitleFromIntent(Intent intent) {
415 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
416 if (initialTitleResId > 0) {
417 mInitialTitle = null;
418 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100419
420 final String initialTitleResPackageName = intent.getStringExtra(
421 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
422 if (initialTitleResPackageName != null) {
423 try {
424 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
425 0 /* flags */, new UserHandle(UserHandle.myUserId()));
426 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
427 setTitle(mInitialTitle);
428 mInitialTitleResId = -1;
429 return;
430 } catch (NameNotFoundException e) {
431 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
432 }
433 } else {
434 setTitle(mInitialTitleResId);
435 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700436 } else {
437 mInitialTitleResId = -1;
438 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
439 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
440 setTitle(mInitialTitle);
441 }
442 }
443
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800444 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800445 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700446 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800447 }
448
Fan Zhang28691572016-03-23 15:31:08 -0700449 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800450 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700451
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800452 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700453 if (mInitialTitleResId > 0) {
454 setTitle(mInitialTitleResId);
455 } else {
456 setTitle(mInitialTitle);
457 }
Fan Zhang28691572016-03-23 15:31:08 -0700458 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800459 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700460
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800461 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
462 setTitleFromBackStackEntry(bse);
463 }
464
465 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
466 final CharSequence title;
467 final int titleRes = bse.getBreadCrumbTitleRes();
468 if (titleRes > 0) {
469 title = getText(titleRes);
470 } else {
471 title = bse.getBreadCrumbTitle();
472 }
473 if (title != null) {
474 setTitle(title);
475 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800476 }
477
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800478 @Override
479 protected void onSaveInstanceState(Bundle outState) {
480 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700481 saveState(outState);
482 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800483
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700484 /**
485 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
486 */
487 @VisibleForTesting
488 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700489 if (mCategories.size() > 0) {
490 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800491 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800492 }
493
494 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400495 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800496 super.onResume();
497
Tony Mantler3d84d562017-07-31 10:48:55 -0700498 mDevelopmentSettingsListener = new BroadcastReceiver() {
499 @Override
500 public void onReceive(Context context, Intent intent) {
501 updateTilesList();
502 }
503 };
504 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
505 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800506
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800507 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700508
Jason Monk4da79e02015-09-10 10:54:36 -0400509 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800510 }
511
512 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400513 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800514 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700515 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
516 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800517 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800518 }
519
520 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800521 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
522 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
523 taskDescription.setIcon(icon);
524 super.setTaskDescription(taskDescription);
525 }
526
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800527 protected boolean isValidFragment(String fragmentName) {
528 // Almost all fragments are wrapped in this,
529 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800530 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
531 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800532 }
533 return false;
534 }
535
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800536 @Override
537 public Intent getIntent() {
538 Intent superIntent = super.getIntent();
539 String startingFragment = getStartingFragmentClass(superIntent);
540 // This is called from super.onCreate, isMultiPane() is not yet reliable
541 // Do not use onIsHidingHeaders either, which relies itself on this method
542 if (startingFragment != null) {
543 Intent modIntent = new Intent(superIntent);
544 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
545 Bundle args = superIntent.getExtras();
546 if (args != null) {
547 args = new Bundle(args);
548 } else {
549 args = new Bundle();
550 }
551 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100552 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800553 return modIntent;
554 }
555 return superIntent;
556 }
557
558 /**
559 * Checks if the component name in the intent is different from the Settings class and
560 * returns the class name to load as a fragment.
561 */
562 private String getStartingFragmentClass(Intent intent) {
563 if (mFragmentClass != null) return mFragmentClass;
564
565 String intentClass = intent.getComponent().getClassName();
566 if (intentClass.equals(getClass().getName())) return null;
567
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700568 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800569 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
570 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700571 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800572 }
573
574 return intentClass;
575 }
576
577 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000578 * Start a new fragment containing a preference panel. If the preferences
579 * are being displayed in multi-pane mode, the given fragment class will
580 * be instantiated and placed in the appropriate pane. If running in
581 * single-pane mode, a new activity will be launched in which to show the
582 * fragment.
583 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800584 * @param fragmentClass Full name of the class implementing the fragment.
585 * @param args Any desired arguments to supply to the fragment.
586 * @param titleRes Optional resource identifier of the title of this
587 * fragment.
588 * @param titleText Optional text of the title of this fragment.
589 * @param resultTo Optional fragment that result data should be sent to.
590 * If non-null, resultTo.onActivityResult() will be called when this
591 * preference panel is done. The launched panel must use
592 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000593 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fan Zhangf11c8852018-02-06 14:21:01 -0800594 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000595 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800596 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
597 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700598 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800599 if (titleRes < 0 && titleText != null) {
600 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700601 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700602 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800603 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000604 }
605
606 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100607 * Start a new fragment in a new activity containing a preference panel for a given user. If the
608 * preferences are being displayed in multi-pane mode, the given fragment class will be
609 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
610 * activity will be launched in which to show the fragment.
611 *
612 * @param fragmentClass Full name of the class implementing the fragment.
Fan Zhangf11c8852018-02-06 14:21:01 -0800613 * @param args Any desired arguments to supply to the fragment.
614 * @param titleRes Optional resource identifier of the title of this fragment.
615 * @param titleText Optional text of the title of this fragment.
616 * @param userHandle The user for which the panel has to be started.
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100617 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800618 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
619 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700620 // This is a workaround.
621 //
622 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
623 // starting the fragment could cause a native stack corruption. See b/17523189. However,
624 // adding that flag and start the preference panel with the same UserHandler will make it
625 // impossible to use back button to return to the previous screen. See b/20042570.
626 //
627 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
628 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
629 // when we're calling it as the same user.
630 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800631 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700632 } else {
633 String title = null;
634 if (titleRes < 0) {
635 if (titleText != null) {
636 title = titleText.toString();
637 } else {
638 // There not much we can do in that case
639 title = "";
640 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100641 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800642 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
643 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100644 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100645 }
646
647 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 * Called by a preference panel fragment to finish itself.
649 *
Fan Zhangf11c8852018-02-06 14:21:01 -0800650 * @param caller The fragment that is asking to be finished.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 * @param resultCode Optional result code 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 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800654 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 */
656 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
657 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700658 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 }
660
661 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000662 * Start a new fragment.
663 *
664 * @param fragment The fragment to start
Fan Zhangf11c8852018-02-06 14:21:01 -0800665 * @param push If true, the current fragment will be pushed onto the back stack. If false,
666 * the current fragment will be replaced.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000667 */
668 public void startPreferenceFragment(Fragment fragment, boolean push) {
669 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700670 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000671 if (push) {
672 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
673 transaction.addToBackStack(BACK_STACK_PREFS);
674 } else {
675 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
676 }
677 transaction.commitAllowingStateLoss();
678 }
679
680 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700681 * Switch to a specific Fragment with taking care of validation, Title and BackStack
682 */
683 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700684 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700685 if (validate && !isValidFragment(fragmentName)) {
686 throw new IllegalArgumentException("Invalid fragment for this activity: "
687 + fragmentName);
688 }
689 Fragment f = Fragment.instantiate(this, fragmentName, args);
690 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700691 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700692 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700693 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700694 }
695 if (addToBackStack) {
696 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
697 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700698 if (titleResId > 0) {
699 transaction.setBreadCrumbTitle(titleResId);
700 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700701 transaction.setBreadCrumbTitle(title);
702 }
703 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700704 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700705 return f;
706 }
707
Jason Monk4da79e02015-09-10 10:54:36 -0400708 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500709 // Generally the items that are will be changing from these updates will
710 // not be in the top list of tiles, so run it in the background and the
711 // SettingsDrawerActivity will pick up on the updates automatically.
712 AsyncTask.execute(new Runnable() {
713 @Override
714 public void run() {
715 doUpdateTilesList();
716 }
717 });
718 }
719
720 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400721 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700722 final UserManager um = UserManager.get(this);
723 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800724 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700725 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400726 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700727 somethingChanged = setTileEnabled(
728 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
729 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700730
Fan Zhangaeb94f02017-07-05 13:46:04 -0700731 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800732 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700733 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
734 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700735
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700736
737 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
738 // enable DataPlanUsageSummaryActivity.
739 somethingChanged = setTileEnabled(
Fan Zhang3ed55b12018-02-07 13:50:09 -0800740 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700741 Utils.isBandwidthControlEnabled() /* enabled */,
742 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700743
jackqdyulei76ba0f72017-11-17 14:14:51 -0800744 final boolean isConnectedDeviceV2Enabled =
745 Settings.ConnectedDeviceDashboardActivity.isEnabled();
746 // Enable new connected page if v2 enabled
747 somethingChanged = setTileEnabled(
748 new ComponentName(packageName,
749 Settings.ConnectedDeviceDashboardActivity.class.getName()),
750 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
751 isAdmin) || somethingChanged;
752 // Enable old connected page if v2 disabled
753 somethingChanged = setTileEnabled(
754 new ComponentName(packageName,
755 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
756 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
757 isAdmin) || somethingChanged;
758
Fan Zhangaeb94f02017-07-05 13:46:04 -0700759 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800760 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700761 Utils.showSimCardTile(this), isAdmin)
762 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700763
Fan Zhangf3144942017-12-22 09:59:37 -0800764 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800765 .getPowerUsageFeatureProvider(this)
766 .isBatteryV2Enabled();
767 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700768 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800769 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800770 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
771 // Enable legacy battery page if v2 disabled
772 somethingChanged = setTileEnabled(new ComponentName(packageName,
773 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
774 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400775
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800776 final boolean isDataUsageSettingsV2Enabled =
777 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
778 // Enable new data usage page if v2 enabled
779 somethingChanged = setTileEnabled(new ComponentName(packageName,
780 Settings.DataUsageSummaryActivity.class.getName()),
781 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
782 || somethingChanged;
783 // Enable legacy data usage page if v2 disabled
784 somethingChanged = setTileEnabled(new ComponentName(packageName,
785 Settings.DataUsageSummaryLegacyActivity.class.getName()),
786 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
787 || somethingChanged;
788
Fan Zhangaeb94f02017-07-05 13:46:04 -0700789 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800790 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400791 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700792 && !Utils.isMonkeyRunning(), isAdmin)
793 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400794
Fan Zhangaeb94f02017-07-05 13:46:04 -0700795 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800796 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700797 !UserManager.isDeviceInDemoMode(this), isAdmin)
798 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800799
Fan Zhangaeb94f02017-07-05 13:46:04 -0700800 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700801 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700802 !UserManager.isDeviceInDemoMode(this), isAdmin)
803 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400804
Tony Mantler3d84d562017-07-31 10:48:55 -0700805 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800806 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800807
Fan Zhang4c26da92017-09-08 15:29:54 -0700808 somethingChanged = setTileEnabled(new ComponentName(packageName,
809 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800810 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700811 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400812
Fan Zhanga6986e72017-03-08 09:24:45 -0800813 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 somethingChanged = setTileEnabled(new ComponentName(packageName,
815 BackupSettingsActivity.class.getName()), true, isAdmin)
816 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800817
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800819 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700820 WifiDisplaySettings.isAvailable(this), isAdmin)
821 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800822
Daniel Nishi1e620952017-12-19 10:15:14 -0800823 // Enable/disable the Me Card page.
824 final boolean isMeCardEnabled = featureFactory
825 .getAccountFeatureProvider()
826 .isMeCardEnabled(this);
827 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800828 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800829 isMeCardEnabled, isAdmin)
830 || somethingChanged;
831 somethingChanged = setTileEnabled(new ComponentName(packageName,
832 Settings.DeviceInfoSettingsActivity.class.getName()),
833 !isMeCardEnabled, isAdmin)
834 || somethingChanged;
835
Jason Monk4da79e02015-09-10 10:54:36 -0400836 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800837
Jason Monk4da79e02015-09-10 10:54:36 -0400838 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800839 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800840 synchronized (categories) {
841 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700842 final int tileCount = category.getTilesCount();
843 for (int i = 0; i < tileCount; i++) {
844 final ComponentName component = category.getTile(i).intent.getComponent();
845
Fan Zhanga6986e72017-03-08 09:24:45 -0800846 final String name = component.getClassName();
847 final boolean isEnabledForRestricted = ArrayUtils.contains(
848 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
849 if (packageName.equals(component.getPackageName())
850 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700851 somethingChanged = setTileEnabled(component, false, isAdmin)
852 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800853 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700854 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700855 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700856 }
857 }
Doris Ling26bf0032016-06-15 18:16:09 -0700858
Fan Zhang224caad2017-01-06 11:42:48 -0800859 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700860 if (somethingChanged) {
861 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
862 updateCategories();
863 } else {
864 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
865 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500866 }
867
Fan Zhangaeb94f02017-07-05 13:46:04 -0700868 /**
869 * @return whether or not the enabled state actually changed.
870 */
871 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400872 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800873 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
874 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400875 enabled = false;
876 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700877 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500878 }
879
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800880 private void getMetaData() {
881 try {
882 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
883 PackageManager.GET_META_DATA);
884 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800885 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
886 } catch (NameNotFoundException nnfe) {
887 // No recovery
888 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
889 }
890 }
891
892 // give subclasses access to the Next button
893 public boolean hasNextButton() {
894 return mNextButton != null;
895 }
896
897 public Button getNextButton() {
898 return mNextButton;
899 }
900
jackqdyulei6c355f52017-03-01 17:37:23 -0800901 @VisibleForTesting
902 Bitmap getBitmapFromXmlResource(int drawableRes) {
903 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
904 Canvas canvas = new Canvas();
905 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
906 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
907 canvas.setBitmap(bitmap);
908 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
909 drawable.draw(canvas);
910
911 return bitmap;
912 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700913}