blob: 66e587ff839d3dedce020550375e88a451a11e4a [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 Zhang60f01072016-10-13 16:53:41 -070019import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070020import android.app.ActivityManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080021import android.app.Fragment;
22import android.app.FragmentManager;
23import android.app.FragmentTransaction;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080024import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070025import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.SharedPreferences;
30import android.content.pm.ActivityInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.PackageManager.NameNotFoundException;
jackqdyulei6c355f52017-03-01 17:37:23 -080033import android.graphics.Bitmap;
jackqdyulei6c355f52017-03-01 17:37:23 -080034import android.graphics.Canvas;
35import android.graphics.drawable.Drawable;
Jason Monkfd2c7222015-12-02 15:38:38 -050036import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080037import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080038import android.os.UserHandle;
39import android.os.UserManager;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080040import android.support.annotation.VisibleForTesting;
Jason Monk39b46742015-09-10 15:52:51 -040041import android.support.v14.preference.PreferenceFragment;
Tony Mantler3d84d562017-07-31 10:48:55 -070042import android.support.v4.content.LocalBroadcastManager;
Jason Monk39b46742015-09-10 15:52:51 -040043import android.support.v7.preference.Preference;
44import android.support.v7.preference.PreferenceManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070046import android.transition.TransitionManager;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080047import android.util.FeatureFlagUtils;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080049import android.view.View;
50import android.view.View.OnClickListener;
Fabrice Di Meglio59a40552014-05-23 16:46:50 -070051import android.view.ViewGroup;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080052import android.widget.Button;
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -070053import android.widget.Toolbar;
Fan Zhang4c26da92017-09-08 15:29:54 -070054
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080055import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040056import com.android.settings.Settings.WifiSettingsActivity;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070057import com.android.settings.applications.manageapplications.ManageApplications;
Anton Philippovadfec552017-01-25 20:37:36 +000058import com.android.settings.backup.BackupSettingsActivity;
Eric Schwarzenbach98998a22018-02-06 15:57:01 -080059import com.android.settings.core.FeatureFlags;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080060import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070061import com.android.settings.dashboard.DashboardFeatureProvider;
62import com.android.settings.dashboard.DashboardSummary;
Fan Zhanga96a2d82016-09-27 17:51:11 -070063import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080064import com.android.settings.wfd.WifiDisplaySettings;
Fabrice Di Meglio41937762014-05-13 19:51:59 -070065import com.android.settings.widget.SwitchBar;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080066import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
67import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070068import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040069import com.android.settingslib.drawer.DashboardCategory;
Jason Monk4da79e02015-09-10 10:54:36 -040070import com.android.settingslib.drawer.SettingsDrawerActivity;
Fan Zhang4c26da92017-09-08 15:29:54 -070071
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080072import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080073import java.util.List;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -070074import java.util.Set;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080075
Jason Monk4da79e02015-09-10 10:54:36 -040076public class SettingsActivity extends SettingsDrawerActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080077 implements PreferenceManager.OnPreferenceTreeClickListener,
78 PreferenceFragment.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080079 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080080
81 private static final String LOG_TAG = "Settings";
82
83 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070084 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Matthew Fritze7d48ae82017-03-23 08:59:15 -070085 @VisibleForTesting
86 static final String SAVE_KEY_SHOW_HOME_AS_UP = ":settings:show_home_as_up";
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 =
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149 "com.android.settings.FRAGMENT_CLASS";
150
151 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
152
Jason Monkd4f03ec2016-01-07 16:25:34 -0500153 private static final int REQUEST_SUGGESTION = 42;
154
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800155 private String mFragmentClass;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800156
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800157 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700158 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800159
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700160 private static final String[] LIKE_SHORTCUT_INTENT_ACTION_ARRAY = {
161 "android.settings.APPLICATION_DETAILS_SETTINGS"
162 };
163
Tony Mantler3d84d562017-07-31 10:48:55 -0700164 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800165
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166 private boolean mBatteryPresent = true;
167 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168 @Override
169 public void onReceive(Context context, Intent intent) {
170 String action = intent.getAction();
171 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
172 boolean batteryPresent = Utils.isBatteryPresent(intent);
173
174 if (mBatteryPresent != batteryPresent) {
175 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400176 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800177 }
178 }
179 }
180 };
181
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700182 private SwitchBar mSwitchBar;
183
184 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700185
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700186 @VisibleForTesting
187 boolean mDisplayHomeAsUpEnabled;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700188
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700189 private boolean mIsShowingDashboard;
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700190 private boolean mIsShortcut;
Fabrice Di Meglio35062d62014-05-13 14:39:41 -0700191
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700192 private ViewGroup mContent;
193
Fan Zhangc6ca3142017-02-14 15:02:35 -0800194 private MetricsFeatureProvider mMetricsFeatureProvider;
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700195
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700196 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800197 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800198
Fan Zhanga96a2d82016-09-27 17:51:11 -0700199 private DashboardFeatureProvider mDashboardFeatureProvider;
Jason Monkd4f03ec2016-01-07 16:25:34 -0500200 private ComponentName mCurrentSuggestion;
Jim Miller0698a212014-10-16 19:49:07 -0700201
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700202 public SwitchBar getSwitchBar() {
203 return mSwitchBar;
204 }
205
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800206 @Override
207 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Doris Ling4a012832017-11-13 17:58:13 -0800208 startPreferencePanel(caller, pref.getFragment(), pref.getExtras(), -1, null, null, 0);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 return true;
210 }
211
212 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400213 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800214 return false;
215 }
216
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800217 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500218 public SharedPreferences getSharedPreferences(String name, int mode) {
219 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800220 return new SharedPreferencesLogger(this, getMetricsTag(),
221 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500222 }
223 return super.getSharedPreferences(name, mode);
224 }
225
Jason Monke4ebcd12016-02-21 09:37:41 -0500226 private String getMetricsTag() {
227 String tag = getClass().getName();
228 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
229 tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
230 }
231 if (tag.startsWith("com.android.settings.")) {
232 tag = tag.replace("com.android.settings.", "");
233 }
234 return tag;
235 }
236
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700237 private static boolean isShortCutIntent(final Intent intent) {
238 Set<String> categories = intent.getCategories();
239 return (categories != null) && categories.contains("com.android.settings.SHORTCUT");
240 }
241
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700242 private static boolean isLikeShortCutIntent(final Intent intent) {
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700243 String action = intent.getAction();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700244 if (action == null) {
245 return false;
246 }
247 for (int i = 0; i < LIKE_SHORTCUT_INTENT_ACTION_ARRAY.length; i++) {
248 if (LIKE_SHORTCUT_INTENT_ACTION_ARRAY[i].equals(action)) return true;
249 }
250 return false;
Fabrice Di Meglio5b7a1002014-06-23 17:54:40 -0700251 }
252
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700253 @Override
254 protected void onCreate(Bundle savedState) {
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700255 super.onCreate(savedState);
Jason Monkfd2c7222015-12-02 15:38:38 -0500256 long startTime = System.currentTimeMillis();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700257
258 final FeatureFactory factory = FeatureFactory.getFactory(this);
259
260 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Fan Zhangc6ca3142017-02-14 15:02:35 -0800261 mMetricsFeatureProvider = factory.getMetricsFeatureProvider();
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700262
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700263 // Should happen before any call to getIntent()
264 getMetaData();
265
266 final Intent intent = getIntent();
267 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
268 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800269 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800270
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700271 // Getting Intent properties can only be done after the super.onCreate(...)
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700272 final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700273
Fabrice Di Meglio5b3c3c02014-06-30 15:18:08 -0700274 mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700275 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
276
Fabrice Di Meglioe817a662014-07-16 19:51:06 -0700277 final ComponentName cn = intent.getComponent();
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700278 final String className = cn.getClassName();
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700279
Doris Ling1694d4b2017-03-03 14:21:37 -0800280 mIsShowingDashboard = className.equals(Settings.class.getName());
Fabrice Di Meglio5a62d942014-07-01 17:18:40 -0700281
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700282 // This is a "Sub Settings" when:
283 // - this is a real SubSettings
284 // - or :settings:show_fragment_as_subsetting is passed to the Intent
Jorim Jaggi4dfcb822015-04-29 17:21:32 -0700285 final boolean isSubSettings = this instanceof SubSettings ||
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700286 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
287
Fan Zhang92f1e942017-01-21 10:07:26 -0800288 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
289 // insets
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700290 if (isSubSettings) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800291 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700292 }
293
Fabrice Di Megliod40dd452014-07-18 15:20:34 -0700294 setContentView(mIsShowingDashboard ?
295 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800296
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700297 mContent = findViewById(R.id.main_content);
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700298
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800299 getFragmentManager().addOnBackStackChangedListener(this);
300
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700301 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700302 // We are restarting from a previous saved state; used that to initialize, instead
303 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700304 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800305
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700306 ArrayList<DashboardCategory> categories =
307 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
308 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700309 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700310 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700311 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800312 }
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700313
314 mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Salvador Martinez2368e542016-07-25 18:16:47 -0700315
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800316 } else {
Doris Lingcb69baf2017-02-23 17:50:03 -0800317 launchSettingFragment(initialFragmentName, isSubSettings, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800318 }
319
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700320 if (mIsShowingDashboard) {
321 findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
322 findViewById(R.id.action_bar).setVisibility(View.GONE);
Fan Zhangff4da232017-11-09 14:07:23 -0800323 final Toolbar toolbar = findViewById(R.id.search_action_bar);
324 FeatureFactory.getFactory(this).getSearchFeatureProvider()
325 .initSearchToolbar(this, toolbar);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700326 setActionBar(toolbar);
Andrew Sappersteinc2174642017-06-25 15:18:46 -0700327
328 // Please forgive me for what I am about to do.
329 //
330 // Need to make the navigation icon non-clickable so that the entire card is clickable
331 // and goes to the search UI. Also set the background to null so there's no ripple.
332 View navView = toolbar.getNavigationView();
333 navView.setClickable(false);
334 navView.setBackground(null);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700335 }
336
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700337 ActionBar actionBar = getActionBar();
338 if (actionBar != null) {
339 actionBar.setDisplayHomeAsUpEnabled(mDisplayHomeAsUpEnabled);
340 actionBar.setHomeButtonEnabled(mDisplayHomeAsUpEnabled);
Fan Zhange096ae72017-11-30 14:41:04 -0800341 actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700342 }
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700343 mSwitchBar = findViewById(R.id.switch_bar);
Jason Monke4ebcd12016-02-21 09:37:41 -0500344 if (mSwitchBar != null) {
345 mSwitchBar.setMetricsTag(getMetricsTag());
346 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700347
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800348 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
350
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700351 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800352 if (buttonBar != null) {
353 buttonBar.setVisibility(View.VISIBLE);
354
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700355 Button backButton = (Button)findViewById(R.id.back_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800356 backButton.setOnClickListener(new OnClickListener() {
357 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700358 setResult(RESULT_CANCELED, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 finish();
360 }
361 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700362 Button skipButton = (Button)findViewById(R.id.skip_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800363 skipButton.setOnClickListener(new OnClickListener() {
364 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700365 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800366 finish();
367 }
368 });
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700369 mNextButton = (Button)findViewById(R.id.next_button);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800370 mNextButton.setOnClickListener(new OnClickListener() {
371 public void onClick(View v) {
Matthew Fritze0b431fc2017-03-16 13:19:20 -0700372 setResult(RESULT_OK, null);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800373 finish();
374 }
375 });
376
377 // set our various button parameters
378 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
379 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
380 if (TextUtils.isEmpty(buttonText)) {
381 mNextButton.setVisibility(View.GONE);
382 }
383 else {
384 mNextButton.setText(buttonText);
385 }
386 }
387 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
388 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
389 if (TextUtils.isEmpty(buttonText)) {
390 backButton.setVisibility(View.GONE);
391 }
392 else {
393 backButton.setText(buttonText);
394 }
395 }
396 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
397 skipButton.setVisibility(View.VISIBLE);
398 }
399 }
400 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700401
Fan Zhangefba6b42017-02-07 17:40:59 -0800402 if (DEBUG_TIMING) {
403 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
404 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700405 }
406
Doris Lingcb69baf2017-02-23 17:50:03 -0800407 @VisibleForTesting
408 void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
409 if (!mIsShowingDashboard && initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800410 // UP will be shown only if it is a sub settings
411 if (mIsShortcut) {
412 mDisplayHomeAsUpEnabled = isSubSettings;
413 } else if (isSubSettings) {
414 mDisplayHomeAsUpEnabled = true;
415 } else {
416 mDisplayHomeAsUpEnabled = false;
417 }
418 setTitleFromIntent(intent);
419
420 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
421 switchToFragment(initialFragmentName, initialArguments, true, false,
422 mInitialTitleResId, mInitialTitle, false);
423 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700424 // Show search icon as up affordance if we are displaying the main Dashboard
425 mDisplayHomeAsUpEnabled = true;
Doris Lingcb69baf2017-02-23 17:50:03 -0800426 mInitialTitleResId = R.string.dashboard_title;
427
428 switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
429 mInitialTitleResId, mInitialTitle, false);
430 }
431 }
432
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700433 private void setTitleFromIntent(Intent intent) {
434 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
435 if (initialTitleResId > 0) {
436 mInitialTitle = null;
437 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100438
439 final String initialTitleResPackageName = intent.getStringExtra(
440 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
441 if (initialTitleResPackageName != null) {
442 try {
443 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
444 0 /* flags */, new UserHandle(UserHandle.myUserId()));
445 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
446 setTitle(mInitialTitle);
447 mInitialTitleResId = -1;
448 return;
449 } catch (NameNotFoundException e) {
450 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
451 }
452 } else {
453 setTitle(mInitialTitleResId);
454 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700455 } else {
456 mInitialTitleResId = -1;
457 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
458 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
459 setTitle(mInitialTitle);
460 }
461 }
462
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800463 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800464 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700465 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800466 }
467
Fan Zhang28691572016-03-23 15:31:08 -0700468 private void setTitleFromBackStack() {
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800469 final int count = getFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700470
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800471 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700472 if (mInitialTitleResId > 0) {
473 setTitle(mInitialTitleResId);
474 } else {
475 setTitle(mInitialTitle);
476 }
Fan Zhang28691572016-03-23 15:31:08 -0700477 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800478 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700479
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800480 FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
481 setTitleFromBackStackEntry(bse);
482 }
483
484 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
485 final CharSequence title;
486 final int titleRes = bse.getBreadCrumbTitleRes();
487 if (titleRes > 0) {
488 title = getText(titleRes);
489 } else {
490 title = bse.getBreadCrumbTitle();
491 }
492 if (title != null) {
493 setTitle(title);
494 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800495 }
496
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800497 @Override
498 protected void onSaveInstanceState(Bundle outState) {
499 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700500 saveState(outState);
501 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800502
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700503 /**
504 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
505 */
506 @VisibleForTesting
507 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700508 if (mCategories.size() > 0) {
509 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800510 }
Fabrice Di Megliod6985df2014-04-03 16:43:26 -0700511
Fabrice Di Megliob731dd02014-04-03 18:40:38 -0700512 outState.putBoolean(SAVE_KEY_SHOW_HOME_AS_UP, mDisplayHomeAsUpEnabled);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700513 }
514
515 @Override
516 protected void onRestoreInstanceState(Bundle savedInstanceState) {
517 super.onRestoreInstanceState(savedInstanceState);
518
519 mDisplayHomeAsUpEnabled = savedInstanceState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800520 }
521
522 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400523 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800524 super.onResume();
525
Tony Mantler3d84d562017-07-31 10:48:55 -0700526 mDevelopmentSettingsListener = new BroadcastReceiver() {
527 @Override
528 public void onReceive(Context context, Intent intent) {
529 updateTilesList();
530 }
531 };
532 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
533 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800534
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800535 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700536
Jason Monk4da79e02015-09-10 10:54:36 -0400537 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800538 }
539
540 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400541 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800542 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700543 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
544 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800545 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800546 }
547
548 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800549 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
550 final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
551 taskDescription.setIcon(icon);
552 super.setTaskDescription(taskDescription);
553 }
554
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800555 protected boolean isValidFragment(String fragmentName) {
556 // Almost all fragments are wrapped in this,
557 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800558 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
559 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 }
561 return false;
562 }
563
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800564 @Override
565 public Intent getIntent() {
566 Intent superIntent = super.getIntent();
567 String startingFragment = getStartingFragmentClass(superIntent);
568 // This is called from super.onCreate, isMultiPane() is not yet reliable
569 // Do not use onIsHidingHeaders either, which relies itself on this method
570 if (startingFragment != null) {
571 Intent modIntent = new Intent(superIntent);
572 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
573 Bundle args = superIntent.getExtras();
574 if (args != null) {
575 args = new Bundle(args);
576 } else {
577 args = new Bundle();
578 }
579 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100580 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800581 return modIntent;
582 }
583 return superIntent;
584 }
585
586 /**
587 * Checks if the component name in the intent is different from the Settings class and
588 * returns the class name to load as a fragment.
589 */
590 private String getStartingFragmentClass(Intent intent) {
591 if (mFragmentClass != null) return mFragmentClass;
592
593 String intentClass = intent.getComponent().getClassName();
594 if (intentClass.equals(getClass().getName())) return null;
595
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700596 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800597 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
598 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700599 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800600 }
601
602 return intentClass;
603 }
604
605 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000606 * Start a new fragment containing a preference panel. If the preferences
607 * are being displayed in multi-pane mode, the given fragment class will
608 * be instantiated and placed in the appropriate pane. If running in
609 * single-pane mode, a new activity will be launched in which to show the
610 * fragment.
611 *
612 * @param fragmentClass Full name of the class implementing the fragment.
613 * @param args Any desired arguments to supply to the fragment.
614 * @param titleRes Optional resource identifier of the title of this
615 * fragment.
616 * @param titleText Optional text of the title of this fragment.
617 * @param resultTo Optional fragment that result data should be sent to.
618 * If non-null, resultTo.onActivityResult() will be called when this
619 * preference panel is done. The launched panel must use
620 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
621 * @param resultRequestCode If resultTo is non-null, this is the caller's
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700622 * request code to be received with the result.
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000623 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800624 public void startPreferencePanel(Fragment caller, String fragmentClass, Bundle args,
625 int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700626 String title = null;
Doris Ling4a012832017-11-13 17:58:13 -0800627 if (titleRes < 0 && titleText != null) {
628 title = titleText.toString();
Fabrice Di Meglio911fb2a2014-04-04 17:55:57 -0700629 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700630 Utils.startWithFragment(this, fragmentClass, args, resultTo, resultRequestCode,
Fan Zhangc6ca3142017-02-14 15:02:35 -0800631 titleRes, title, mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller));
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000632 }
633
634 /**
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100635 * Start a new fragment in a new activity containing a preference panel for a given user. If the
636 * preferences are being displayed in multi-pane mode, the given fragment class will be
637 * instantiated and placed in the appropriate pane. If running in single-pane mode, a new
638 * activity will be launched in which to show the fragment.
639 *
640 * @param fragmentClass Full name of the class implementing the fragment.
641 * @param args Any desired arguments to supply to the fragment.
642 * @param titleRes Optional resource identifier of the title of this fragment.
643 * @param titleText Optional text of the title of this fragment.
644 * @param userHandle The user for which the panel has to be started.
645 */
Fan Zhangc6ca3142017-02-14 15:02:35 -0800646 public void startPreferencePanelAsUser(Fragment caller, String fragmentClass,
647 Bundle args, int titleRes, CharSequence titleText, UserHandle userHandle) {
Lifu Tangd0332852015-04-02 12:05:46 -0700648 // This is a workaround.
649 //
650 // Calling startWithFragmentAsUser() without specifying FLAG_ACTIVITY_NEW_TASK to the intent
651 // starting the fragment could cause a native stack corruption. See b/17523189. However,
652 // adding that flag and start the preference panel with the same UserHandler will make it
653 // impossible to use back button to return to the previous screen. See b/20042570.
654 //
655 // We work around this issue by adding FLAG_ACTIVITY_NEW_TASK to the intent, while doing
656 // another check here to call startPreferencePanel() instead of startWithFragmentAsUser()
657 // when we're calling it as the same user.
658 if (userHandle.getIdentifier() == UserHandle.myUserId()) {
Fan Zhangc6ca3142017-02-14 15:02:35 -0800659 startPreferencePanel(caller, fragmentClass, args, titleRes, titleText, null, 0);
Lifu Tangd0332852015-04-02 12:05:46 -0700660 } else {
661 String title = null;
662 if (titleRes < 0) {
663 if (titleText != null) {
664 title = titleText.toString();
665 } else {
666 // There not much we can do in that case
667 title = "";
668 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100669 }
Fan Zhangc6ca3142017-02-14 15:02:35 -0800670 Utils.startWithFragmentAsUser(this, fragmentClass, args, titleRes, title,
671 mIsShortcut, mMetricsFeatureProvider.getMetricsCategory(caller), userHandle);
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100672 }
Zoltan Szatmary-Ban7a2ccf22014-09-18 10:26:11 +0100673 }
674
675 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800676 * Called by a preference panel fragment to finish itself.
677 *
678 * @param caller The fragment that is asking to be finished.
679 * @param resultCode Optional result code to send back to the original
680 * launching fragment.
681 * @param resultData Optional result data to send back to the original
682 * launching fragment.
683 */
684 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
685 setResult(resultCode, resultData);
Fabrice Di Meglio58146c22014-06-26 16:20:26 -0700686 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800687 }
688
689 /**
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000690 * Start a new fragment.
691 *
692 * @param fragment The fragment to start
693 * @param push If true, the current fragment will be pushed onto the back stack. If false,
694 * the current fragment will be replaced.
695 */
696 public void startPreferenceFragment(Fragment fragment, boolean push) {
697 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700698 transaction.replace(R.id.main_content, fragment);
Fabrice Di Meglio10afdb82014-02-11 19:50:56 +0000699 if (push) {
700 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
701 transaction.addToBackStack(BACK_STACK_PREFS);
702 } else {
703 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
704 }
705 transaction.commitAllowingStateLoss();
706 }
707
708 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700709 * Switch to a specific Fragment with taking care of validation, Title and BackStack
710 */
711 private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700712 boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700713 if (validate && !isValidFragment(fragmentName)) {
714 throw new IllegalArgumentException("Invalid fragment for this activity: "
715 + fragmentName);
716 }
717 Fragment f = Fragment.instantiate(this, fragmentName, args);
718 FragmentTransaction transaction = getFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700719 transaction.replace(R.id.main_content, f);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700720 if (withTransition) {
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700721 TransitionManager.beginDelayedTransition(mContent);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700722 }
723 if (addToBackStack) {
724 transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
725 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700726 if (titleResId > 0) {
727 transaction.setBreadCrumbTitle(titleResId);
728 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700729 transaction.setBreadCrumbTitle(title);
730 }
731 transaction.commitAllowingStateLoss();
Fabrice Di Meglio59a40552014-05-23 16:46:50 -0700732 getFragmentManager().executePendingTransactions();
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700733 return f;
734 }
735
Jason Monk4da79e02015-09-10 10:54:36 -0400736 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500737 // Generally the items that are will be changing from these updates will
738 // not be in the top list of tiles, so run it in the background and the
739 // SettingsDrawerActivity will pick up on the updates automatically.
740 AsyncTask.execute(new Runnable() {
741 @Override
742 public void run() {
743 doUpdateTilesList();
744 }
745 });
746 }
747
748 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400749 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700750 final UserManager um = UserManager.get(this);
751 final boolean isAdmin = um.isAdminUser();
Fan Zhangf3144942017-12-22 09:59:37 -0800752 final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
Fan Zhangaeb94f02017-07-05 13:46:04 -0700753 boolean somethingChanged = false;
Jason Monk4da79e02015-09-10 10:54:36 -0400754 String packageName = getPackageName();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700755 somethingChanged = setTileEnabled(
756 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
757 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700758
Fan Zhangaeb94f02017-07-05 13:46:04 -0700759 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800760 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700761 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
762 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700763
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700764 boolean isDataPlanFeatureEnabled = FeatureFactory.getFactory(this)
765 .getDataPlanFeatureProvider()
766 .isEnabled();
767
768 // When the data plan feature flag is turned on we disable DataUsageSummaryActivity
769 // and enable DataPlanUsageSummaryActivity. When the feature flag is turned off we do the
770 // reverse.
771
772 // Disable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
773 // disable DataPlanUsageSummaryActivity.
774 somethingChanged = setTileEnabled(
775 new ComponentName(packageName,
776 isDataPlanFeatureEnabled
777 ? Settings.DataUsageSummaryActivity.class.getName()
778 : Settings.DataPlanUsageSummaryActivity.class.getName()),
779 false /* enabled */,
780 isAdmin) || somethingChanged;
781
782 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
783 // enable DataPlanUsageSummaryActivity.
784 somethingChanged = setTileEnabled(
785 new ComponentName(packageName,
786 isDataPlanFeatureEnabled
787 ? Settings.DataPlanUsageSummaryActivity.class.getName()
788 : Settings.DataUsageSummaryActivity.class.getName()),
789 Utils.isBandwidthControlEnabled() /* enabled */,
790 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700791
jackqdyulei76ba0f72017-11-17 14:14:51 -0800792 final boolean isConnectedDeviceV2Enabled =
793 Settings.ConnectedDeviceDashboardActivity.isEnabled();
794 // Enable new connected page if v2 enabled
795 somethingChanged = setTileEnabled(
796 new ComponentName(packageName,
797 Settings.ConnectedDeviceDashboardActivity.class.getName()),
798 isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
799 isAdmin) || somethingChanged;
800 // Enable old connected page if v2 disabled
801 somethingChanged = setTileEnabled(
802 new ComponentName(packageName,
803 Settings.ConnectedDeviceDashboardActivityOld.class.getName()),
804 !isConnectedDeviceV2Enabled && !UserManager.isDeviceInDemoMode(this) /* enabled */,
805 isAdmin) || somethingChanged;
806
Fan Zhangaeb94f02017-07-05 13:46:04 -0700807 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 Settings.SimSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 Utils.showSimCardTile(this), isAdmin)
810 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700811
Fan Zhangf3144942017-12-22 09:59:37 -0800812 final boolean isBatterySettingsV2Enabled = featureFactory
jackqdyulei166102c2017-11-27 13:49:19 -0800813 .getPowerUsageFeatureProvider(this)
814 .isBatteryV2Enabled();
815 // Enable new battery page if v2 enabled
Fan Zhangaeb94f02017-07-05 13:46:04 -0700816 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800817 Settings.PowerUsageSummaryActivity.class.getName()),
jackqdyulei166102c2017-11-27 13:49:19 -0800818 mBatteryPresent && isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
819 // Enable legacy battery page if v2 disabled
820 somethingChanged = setTileEnabled(new ComponentName(packageName,
821 Settings.PowerUsageSummaryLegacyActivity.class.getName()),
822 mBatteryPresent && !isBatterySettingsV2Enabled, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400823
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800824 final boolean isDataUsageSettingsV2Enabled =
825 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
826 // Enable new data usage page if v2 enabled
827 somethingChanged = setTileEnabled(new ComponentName(packageName,
828 Settings.DataUsageSummaryActivity.class.getName()),
829 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
830 || somethingChanged;
831 // Enable legacy data usage page if v2 disabled
832 somethingChanged = setTileEnabled(new ComponentName(packageName,
833 Settings.DataUsageSummaryLegacyActivity.class.getName()),
834 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
835 || somethingChanged;
836
Fan Zhangaeb94f02017-07-05 13:46:04 -0700837 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800838 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400839 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700840 && !Utils.isMonkeyRunning(), isAdmin)
841 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400842
Fan Zhangaeb94f02017-07-05 13:46:04 -0700843 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800844 Settings.NetworkDashboardActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700845 !UserManager.isDeviceInDemoMode(this), isAdmin)
846 || somethingChanged;
Doris Ling26208222017-03-03 11:28:05 -0800847
Fan Zhangaeb94f02017-07-05 13:46:04 -0700848 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fyodor Kupolovca060e32016-06-24 13:01:11 -0700849 Settings.DateTimeSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700850 !UserManager.isDeviceInDemoMode(this), isAdmin)
851 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400852
Fan Zhangaeb94f02017-07-05 13:46:04 -0700853 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800854 Settings.PrintSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700855 pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
856 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400857
Tony Mantler3d84d562017-07-31 10:48:55 -0700858 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800859 && !um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
860 && !Utils.isMonkeyRunning();
jeffreyhuangcb823d52017-11-17 11:19:44 -0800861
Fan Zhang4c26da92017-09-08 15:29:54 -0700862 somethingChanged = setTileEnabled(new ComponentName(packageName,
863 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
jeffreyhuangcb823d52017-11-17 11:19:44 -0800864 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700865 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400866
Fan Zhanga6986e72017-03-08 09:24:45 -0800867 // Enable/disable backup settings depending on whether the user is admin.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700868 somethingChanged = setTileEnabled(new ComponentName(packageName,
869 BackupSettingsActivity.class.getName()), true, isAdmin)
870 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800871
Fan Zhangaeb94f02017-07-05 13:46:04 -0700872 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800873 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700874 WifiDisplaySettings.isAvailable(this), isAdmin)
875 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800876
Daniel Nishi1e620952017-12-19 10:15:14 -0800877 // Enable/disable the Me Card page.
878 final boolean isMeCardEnabled = featureFactory
879 .getAccountFeatureProvider()
880 .isMeCardEnabled(this);
881 somethingChanged = setTileEnabled(new ComponentName(packageName,
Fan Zhang881d5792018-01-26 10:15:56 -0800882 Settings.MyDeviceInfoActivity.class.getName()),
Daniel Nishi1e620952017-12-19 10:15:14 -0800883 isMeCardEnabled, isAdmin)
884 || somethingChanged;
885 somethingChanged = setTileEnabled(new ComponentName(packageName,
886 Settings.DeviceInfoSettingsActivity.class.getName()),
887 !isMeCardEnabled, isAdmin)
888 || somethingChanged;
889
Jason Monk4da79e02015-09-10 10:54:36 -0400890 if (UserHandle.MU_ENABLED && !isAdmin) {
Fan Zhanga6986e72017-03-08 09:24:45 -0800891
Jason Monk4da79e02015-09-10 10:54:36 -0400892 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800893 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800894 synchronized (categories) {
895 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700896 final int tileCount = category.getTilesCount();
897 for (int i = 0; i < tileCount; i++) {
898 final ComponentName component = category.getTile(i).intent.getComponent();
899
Fan Zhanga6986e72017-03-08 09:24:45 -0800900 final String name = component.getClassName();
901 final boolean isEnabledForRestricted = ArrayUtils.contains(
902 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
903 if (packageName.equals(component.getPackageName())
904 && !isEnabledForRestricted) {
Fan Zhangaeb94f02017-07-05 13:46:04 -0700905 somethingChanged = setTileEnabled(component, false, isAdmin)
906 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800907 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700908 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700909 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700910 }
911 }
Doris Ling26bf0032016-06-15 18:16:09 -0700912
Fan Zhang224caad2017-01-06 11:42:48 -0800913 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700914 if (somethingChanged) {
915 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories");
916 updateCategories();
917 } else {
918 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
919 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500920 }
921
Fan Zhangaeb94f02017-07-05 13:46:04 -0700922 /**
923 * @return whether or not the enabled state actually changed.
924 */
925 private boolean setTileEnabled(ComponentName component, boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400926 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800927 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
928 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400929 enabled = false;
930 }
Fan Zhangaeb94f02017-07-05 13:46:04 -0700931 return setTileEnabled(component, enabled);
Jason Monk2ebc8a02015-02-13 15:23:19 -0500932 }
933
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800934 private void getMetaData() {
935 try {
936 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
937 PackageManager.GET_META_DATA);
938 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800939 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
940 } catch (NameNotFoundException nnfe) {
941 // No recovery
942 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
943 }
944 }
945
946 // give subclasses access to the Next button
947 public boolean hasNextButton() {
948 return mNextButton != null;
949 }
950
951 public Button getNextButton() {
952 return mNextButton;
953 }
954
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800955 @Override
956 public boolean shouldUpRecreateTask(Intent targetIntent) {
957 return super.shouldUpRecreateTask(new Intent(this, SettingsActivity.class));
958 }
959
Jason Monkd4f03ec2016-01-07 16:25:34 -0500960 public void startSuggestion(Intent intent) {
Matthew Fritze7ac78f22016-10-03 13:26:07 -0700961 if (intent == null || ActivityManager.isUserAMonkey()) {
Matthew Fritze703dc602016-09-30 12:36:27 -0700962 return;
963 }
Matthew Fritzef265dbc2016-10-04 13:39:27 -0700964 mCurrentSuggestion = intent.getComponent();
Jason Monkd4f03ec2016-01-07 16:25:34 -0500965 startActivityForResult(intent, REQUEST_SUGGESTION);
966 }
967
968 @Override
969 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
970 if (requestCode == REQUEST_SUGGESTION && mCurrentSuggestion != null
971 && resultCode != RESULT_CANCELED) {
972 getPackageManager().setComponentEnabledSetting(mCurrentSuggestion,
973 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
974 }
975 super.onActivityResult(requestCode, resultCode, data);
976 }
jackqdyulei6c355f52017-03-01 17:37:23 -0800977
978 @VisibleForTesting
979 Bitmap getBitmapFromXmlResource(int drawableRes) {
980 Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
981 Canvas canvas = new Canvas();
982 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
983 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
984 canvas.setBitmap(bitmap);
985 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
986 drawable.draw(canvas);
987
988 return bitmap;
989 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700990}