blob: 37d4a4e99eba20072264856fc83c660d652480e3 [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
Arc Wanga63e9c62021-09-28 20:39:15 +080019import static android.provider.Settings.ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY;
20import static android.provider.Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY;
21import static android.provider.Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI;
22
Vinit Nayakf7d423d2019-07-01 13:24:39 -070023import static com.android.settings.applications.appinfo.AppButtonsPreferenceController.KEY_REMOVE_TASK_WHEN_FINISHING;
24
Fan Zhang60f01072016-10-13 16:53:41 -070025import android.app.ActionBar;
Matthew Fritze7ac78f22016-10-03 13:26:07 -070026import android.app.ActivityManager;
Arc Wang9ba80e72022-02-25 12:35:20 +080027import android.content.ActivityNotFoundException;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080028import android.content.BroadcastReceiver;
Fabrice Di Megliod25314d2014-03-21 19:24:43 -070029import android.content.ComponentName;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080030import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.SharedPreferences;
34import android.content.pm.ActivityInfo;
35import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
Tsung-Mao Fang78267b22021-10-18 22:01:27 +080037import android.content.res.Resources;
pastychangfefc6682019-03-21 19:42:39 +080038import android.content.res.Resources.Theme;
Winson Chungf07554f2020-02-18 16:42:26 -080039import android.graphics.drawable.Icon;
Jason Monkfd2c7222015-12-02 15:38:38 -050040import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080041import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.UserHandle;
43import android.os.UserManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080044import android.text.TextUtils;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080046import android.view.View;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.widget.Button;
Fan Zhang4c26da92017-09-08 15:29:54 -070048
Fan Zhang60243e62018-08-03 09:13:53 -070049import androidx.annotation.Nullable;
50import androidx.annotation.VisibleForTesting;
51import androidx.fragment.app.Fragment;
52import androidx.fragment.app.FragmentManager;
53import androidx.fragment.app.FragmentTransaction;
54import androidx.localbroadcastmanager.content.LocalBroadcastManager;
55import androidx.preference.Preference;
56import androidx.preference.PreferenceFragmentCompat;
57import androidx.preference.PreferenceManager;
58
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080059import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040060import com.android.settings.Settings.WifiSettingsActivity;
Arc Wang3e816462021-09-27 14:16:14 +080061import com.android.settings.activityembedding.ActivityEmbeddingUtils;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070062import com.android.settings.applications.manageapplications.ManageApplications;
Fan Zhangaf32e722018-07-17 10:12:30 -070063import com.android.settings.core.OnActivityResultListener;
tmfangd97fba52018-06-14 14:48:35 +080064import com.android.settings.core.SettingsBaseActivity;
Fan Zhang615563d2018-02-15 15:19:33 -080065import com.android.settings.core.SubSettingLauncher;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080066import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070067import com.android.settings.dashboard.DashboardFeatureProvider;
Arc Wang22ce3922021-08-31 18:11:57 +080068import com.android.settings.homepage.SettingsHomepageActivity;
Arc Wanga63e9c62021-09-28 20:39:15 +080069import com.android.settings.homepage.SliceDeepLinkHomepageActivity;
Fan Zhang21526c12018-09-06 15:44:26 -070070import com.android.settings.homepage.TopLevelSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070071import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080072import com.android.settings.wfd.WifiDisplaySettings;
Stanley Wangb87ddba2020-12-25 16:27:32 +080073import com.android.settings.widget.SettingsMainSwitchBar;
Fan Zhang7cf99f52018-02-16 10:37:37 -080074import com.android.settingslib.core.instrumentation.Instrumentable;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080075import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070076import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040077import com.android.settingslib.drawer.DashboardCategory;
Fan Zhang4c26da92017-09-08 15:29:54 -070078
cnchen3eee5cf2019-05-06 10:19:10 +080079import com.google.android.setupcompat.util.WizardManagerHelper;
80
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080081import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080082import java.util.List;
83
tmfangd97fba52018-06-14 14:48:35 +080084
85public class SettingsActivity extends SettingsBaseActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080086 implements PreferenceManager.OnPreferenceTreeClickListener,
tmfang27c84de2018-06-28 11:39:05 +080087 PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080088 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080089
Fan Zhang22230502018-03-27 14:14:13 -070090 private static final String LOG_TAG = "SettingsActivity";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080091
92 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070093 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080094
95 /**
96 * When starting this activity, the invoking Intent can contain this extra
97 * string to specify which fragment should be initially displayed.
98 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
99 * will call isValidFragment() to confirm that the fragment class name is valid for this
100 * activity.
101 */
102 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
103
104 /**
105 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
106 * this extra can also be specified to supply a Bundle of arguments to pass
107 * to that fragment when it is instantiated during the initial creation
108 * of the activity.
109 */
110 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
111
112 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700113 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
114 */
115 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
116
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800117 // extras that allow any preference activity to be launched as part of a wizard
118
119 // show Back and Next buttons? takes boolean parameter
120 // Back will then return RESULT_CANCELED and Next RESULT_OK
121 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
122
123 // add a Skip button?
124 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
125
126 // specify custom text for the Back or Next buttons, or cause a button to not appear
127 // at all by setting it to null
128 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
129 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
130
131 /**
132 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700133 * 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 -0800134 * that fragment.
135 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700136 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100137 /**
138 * The package name used to resolve the title resource id.
139 */
140 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
141 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700142 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
143 ":settings:show_fragment_title_resid";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700144
145 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
146 ":settings:show_fragment_as_subsetting";
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800147 public static final String EXTRA_IS_SECONDARY_LAYER_PAGE =
148 ":settings:is_secondary_layer_page";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800149
Raff Tsai910ea662019-12-04 10:56:43 +0800150 /**
Arc Wanga63e9c62021-09-28 20:39:15 +0800151 * Additional extra of Settings#ACTION_SETTINGS_LARGE_SCREEN_DEEP_LINK.
152 * Set true when the deep link intent is from a slice
153 */
154 public static final String EXTRA_IS_FROM_SLICE = "is_from_slice";
155
156 /**
Raff Tsai910ea662019-12-04 10:56:43 +0800157 * Personal or Work profile tab of {@link ProfileSelectFragment}
158 * <p>0: Personal tab.
159 * <p>1: Work profile tab.
160 */
161 public static final String EXTRA_SHOW_FRAGMENT_TAB =
162 ":settings:show_fragment_tab";
163
Jason Monk30695812015-11-17 09:01:08 -0500164 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800165 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800166
Jason Chiu29a09e52021-10-12 17:24:34 +0800167 public static final String META_DATA_KEY_HIGHLIGHT_MENU_KEY =
168 "com.android.settings.HIGHLIGHT_MENU_KEY";
169
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800170 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
171
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 private String mFragmentClass;
Jason Chiu29a09e52021-10-12 17:24:34 +0800173 private String mHighlightMenuKey;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800175 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700176 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800177
Tony Mantler3d84d562017-07-31 10:48:55 -0700178 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800179
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800180 private boolean mBatteryPresent = true;
181 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 @Override
183 public void onReceive(Context context, Intent intent) {
184 String action = intent.getAction();
185 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
186 boolean batteryPresent = Utils.isBatteryPresent(intent);
187
188 if (mBatteryPresent != batteryPresent) {
189 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400190 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800191 }
192 }
193 }
194 };
195
Stanley Wangb87ddba2020-12-25 16:27:32 +0800196 private SettingsMainSwitchBar mMainSwitch;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700197
198 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700199
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700200 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800201 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800202
Fan Zhanga96a2d82016-09-27 17:51:11 -0700203 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700204
Stanley Wangb87ddba2020-12-25 16:27:32 +0800205 public SettingsMainSwitchBar getSwitchBar() {
206 return mMainSwitch;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700207 }
208
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800209 @Override
tmfang27c84de2018-06-28 11:39:05 +0800210 public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800211 new SubSettingLauncher(this)
212 .setDestination(pref.getFragment())
213 .setArguments(pref.getExtras())
214 .setSourceMetricsCategory(caller instanceof Instrumentable
215 ? ((Instrumentable) caller).getMetricsCategory()
216 : Instrumentable.METRICS_CATEGORY_UNKNOWN)
hjchangliaoe86eec02018-05-02 13:01:07 +0800217 .setTitleRes(-1)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800218 .launch();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800219 return true;
220 }
221
222 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400223 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800224 return false;
225 }
226
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800227 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500228 public SharedPreferences getSharedPreferences(String name, int mode) {
229 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800230 return new SharedPreferencesLogger(this, getMetricsTag(),
231 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500232 }
233 return super.getSharedPreferences(name, mode);
234 }
235
Jason Monke4ebcd12016-02-21 09:37:41 -0500236 private String getMetricsTag() {
Raff Tsai54a5c442019-07-22 10:13:11 +0800237 String tag = null;
Jason Monke4ebcd12016-02-21 09:37:41 -0500238 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100239 tag = getInitialFragmentName(getIntent());
Jason Monke4ebcd12016-02-21 09:37:41 -0500240 }
Raff Tsai54a5c442019-07-22 10:13:11 +0800241 if (TextUtils.isEmpty(tag)) {
242 Log.w(LOG_TAG, "MetricsTag is invalid " + tag);
243 tag = getClass().getName();
244 }
Jason Monke4ebcd12016-02-21 09:37:41 -0500245 if (tag.startsWith("com.android.settings.")) {
246 tag = tag.replace("com.android.settings.", "");
247 }
248 return tag;
249 }
250
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700251 @Override
252 protected void onCreate(Bundle savedState) {
Jason Chiu29a09e52021-10-12 17:24:34 +0800253 // Should happen before any call to getIntent()
254 getMetaData();
255 final Intent intent = getIntent();
Arc Wange80b74d2021-11-01 19:07:43 +0800256
Arc Wang9ba80e72022-02-25 12:35:20 +0800257 if (shouldShowTwoPaneDeepLink(intent) && tryStartTwoPaneDeepLink(intent)) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800258 finishAndRemoveTask();
259 super.onCreate(savedState);
260 return;
261 }
262
263 super.onCreate(savedState);
264 Log.d(LOG_TAG, "Starting onCreate");
265
266 long startTime = System.currentTimeMillis();
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800267
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700268 final FeatureFactory factory = FeatureFactory.getFactory(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700269 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700270
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700271 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
272 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800273 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800274
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700275 // Getting Intent properties can only be done after the super.onCreate(...)
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100276 final String initialFragmentName = getInitialFragmentName(intent);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700277
Fan Zhang92f1e942017-01-21 10:07:26 -0800278 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
cnchen3eee5cf2019-05-06 10:19:10 +0800279 // insets.
280 // If this is in setup flow, don't apply theme. Because light theme needs to be applied
281 // in SettingsBaseActivity#onCreate().
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800282 if (isSubSettings(intent) && !WizardManagerHelper.isAnySetupWizard(getIntent())) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800283 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700284 }
285
Fan Zhang4df9bfa2019-02-22 13:53:09 -0800286 setContentView(R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800287
tmfang27c84de2018-06-28 11:39:05 +0800288 getSupportFragmentManager().addOnBackStackChangedListener(this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700290 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700291 // We are restarting from a previous saved state; used that to initialize, instead
292 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700293 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800294
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700295 ArrayList<DashboardCategory> categories =
296 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
297 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700298 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700299 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700300 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800301 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800302 } else {
Fan Zhang8e14ca02018-11-27 13:06:44 -0800303 launchSettingFragment(initialFragmentName, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 }
305
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800306 final boolean isActionBarButtonEnabled = isActionBarButtonEnabled(intent);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700307
Fan Zhang4df9bfa2019-02-22 13:53:09 -0800308 final ActionBar actionBar = getActionBar();
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700309 if (actionBar != null) {
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800310 actionBar.setDisplayHomeAsUpEnabled(isActionBarButtonEnabled);
311 actionBar.setHomeButtonEnabled(isActionBarButtonEnabled);
Mill Chen7b163e22021-01-14 18:33:46 +0800312 actionBar.setDisplayShowTitleEnabled(true);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700313 }
Stanley Wangb87ddba2020-12-25 16:27:32 +0800314 mMainSwitch = findViewById(R.id.switch_bar);
315 if (mMainSwitch != null) {
316 mMainSwitch.setMetricsTag(getMetricsTag());
Stanley Wang3b70c6c2021-03-03 12:06:14 +0800317 mMainSwitch.setTranslationZ(findViewById(R.id.main_content).getTranslationZ() + 1);
Jason Monke4ebcd12016-02-21 09:37:41 -0500318 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700319
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800320 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800321 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
322
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700323 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800324 if (buttonBar != null) {
325 buttonBar.setVisibility(View.VISIBLE);
326
Fan Zhange994d462018-12-04 12:43:14 -0800327 Button backButton = findViewById(R.id.back_button);
328 backButton.setOnClickListener(v -> {
329 setResult(RESULT_CANCELED, null);
330 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800331 });
Fan Zhange994d462018-12-04 12:43:14 -0800332 Button skipButton = findViewById(R.id.skip_button);
333 skipButton.setOnClickListener(v -> {
334 setResult(RESULT_OK, null);
335 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800336 });
Fan Zhange994d462018-12-04 12:43:14 -0800337 mNextButton = findViewById(R.id.next_button);
338 mNextButton.setOnClickListener(v -> {
339 setResult(RESULT_OK, null);
340 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341 });
342
343 // set our various button parameters
344 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
345 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
346 if (TextUtils.isEmpty(buttonText)) {
347 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800348 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800349 mNextButton.setText(buttonText);
350 }
351 }
352 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
353 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
354 if (TextUtils.isEmpty(buttonText)) {
355 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800356 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800357 backButton.setText(buttonText);
358 }
359 }
360 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
361 skipButton.setVisibility(View.VISIBLE);
362 }
363 }
364 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700365
Fan Zhangefba6b42017-02-07 17:40:59 -0800366 if (DEBUG_TIMING) {
367 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
368 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700369 }
370
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800371 private boolean isActionBarButtonEnabled(Intent intent) {
372 if (WizardManagerHelper.isAnySetupWizard(intent)) {
373 return false;
374 }
375 final boolean isSecondaryLayerPage =
376 intent.getBooleanExtra(EXTRA_IS_SECONDARY_LAYER_PAGE, false);
377
378 // TODO: move Settings's ActivityEmbeddingUtils to SettingsLib.
379 return !com.android.settingslib.activityembedding.ActivityEmbeddingUtils
380 .shouldHideBackButton(this, isSecondaryLayerPage);
381 }
382
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800383 private boolean isSubSettings(Intent intent) {
384 return this instanceof SubSettings ||
385 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
386 }
387
Jason Chiue427cc62021-10-25 14:11:16 +0800388 /**
389 * Returns the deep link trampoline intent for large screen devices.
390 */
391 public static Intent getTrampolineIntent(Intent intent, String highlightMenuKey) {
Arc Wanga63e9c62021-09-28 20:39:15 +0800392 final Intent detailIntent = new Intent(intent);
Arc Wang3e816462021-09-27 14:16:14 +0800393 // It's a deep link intent, SettingsHomepageActivity will set SplitPairRule and start it.
Jason Chiu209df882021-12-09 16:51:59 +0800394 final Intent trampolineIntent = new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY)
395 .setPackage(Utils.SETTINGS_PACKAGE_NAME)
396 .replaceExtras(detailIntent);
Arc Wanga63e9c62021-09-28 20:39:15 +0800397
398 // Relay detail intent data to prevent failure of Intent#ParseUri.
399 // If Intent#getData() is not null, Intent#toUri will return an Uri which has the scheme of
400 // Intent#getData() and it may not be the scheme of an Intent.
Arc Wang3e816462021-09-27 14:16:14 +0800401 trampolineIntent.putExtra(
Arc Wanga63e9c62021-09-28 20:39:15 +0800402 SettingsHomepageActivity.EXTRA_SETTINGS_LARGE_SCREEN_DEEP_LINK_INTENT_DATA,
403 detailIntent.getData());
404 detailIntent.setData(null);
405
406 trampolineIntent.putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI,
407 detailIntent.toUri(Intent.URI_INTENT_SCHEME));
408
Jason Chiue427cc62021-10-25 14:11:16 +0800409 trampolineIntent.putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY,
410 highlightMenuKey);
411 trampolineIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
412 return trampolineIntent;
413 }
414
Arc Wang9ba80e72022-02-25 12:35:20 +0800415 private boolean tryStartTwoPaneDeepLink(Intent intent) {
Jason Chiue427cc62021-10-25 14:11:16 +0800416 final Intent trampolineIntent;
417 if (intent.getBooleanExtra(EXTRA_IS_FROM_SLICE, false)) {
Arc Wanga63e9c62021-09-28 20:39:15 +0800418 // Get menu key for slice deep link case.
Jason Chiue427cc62021-10-25 14:11:16 +0800419 final String highlightMenuKey = intent.getStringExtra(
Arc Wanga63e9c62021-09-28 20:39:15 +0800420 EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY);
421 if (!TextUtils.isEmpty(highlightMenuKey)) {
422 mHighlightMenuKey = highlightMenuKey;
423 }
Jason Chiue427cc62021-10-25 14:11:16 +0800424 trampolineIntent = getTrampolineIntent(intent, mHighlightMenuKey);
425 trampolineIntent.setClass(this, SliceDeepLinkHomepageActivity.class);
426 } else {
427 trampolineIntent = getTrampolineIntent(intent, mHighlightMenuKey);
Arc Wanga63e9c62021-09-28 20:39:15 +0800428 }
Arc Wang9ba80e72022-02-25 12:35:20 +0800429
430 try {
431 startActivity(trampolineIntent);
432 } catch (ActivityNotFoundException e) {
433 Log.e(LOG_TAG, "Deep link homepage is not available to show 2-pane UI");
434 return false;
435 }
436 return true;
Arc Wang3e816462021-09-27 14:16:14 +0800437 }
438
439 private boolean shouldShowTwoPaneDeepLink(Intent intent) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800440 if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
441 return false;
442 }
443
444 // If the activity is not the task root, it should not start trampoline for deep links.
445 if (!isTaskRoot()) {
446 return false;
447 }
448
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800449 // Only starts trampoline for deep links. Should return false for all the cases that
450 // Settings app starts SettingsActivity or SubSetting by itself.
451 if (intent.getAction() == null) {
452 // Other apps should send deep link intent which matches intent filter of the Activity.
453 return false;
454 }
455
Arc Wanga63e9c62021-09-28 20:39:15 +0800456 if (intent.getBooleanExtra(EXTRA_IS_FROM_SLICE, false)) {
457 // Slice deep link starts the Intent using SubSettingLauncher. Returns true to show
458 // 2-pane deep link.
459 return true;
460 }
461
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800462 if (isSubSettings(intent)) {
463 return false;
464 }
465
Arc Wang488f79c2021-11-05 11:07:33 +0800466 if (intent.getBooleanExtra(SettingsHomepageActivity.EXTRA_IS_FROM_SETTINGS_HOMEPAGE,
467 /* defaultValue */ false)) {
468 return false;
469 }
470
Arc Wang3e816462021-09-27 14:16:14 +0800471 if (TextUtils.equals(intent.getAction(), Intent.ACTION_CREATE_SHORTCUT)) {
472 // Returns false to show full screen for Intent.ACTION_CREATE_SHORTCUT because
473 // - Launcher startActivityForResult for Intent.ACTION_CREATE_SHORTCUT and activity
474 // stack starts from launcher, CreateShortcutActivity will not follows SplitPaitRule
475 // registered by Settings.
476 // - There is no CreateShortcutActivity entry point from Settings app UI.
477 return false;
478 }
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800479
480 return true;
Arc Wang22ce3922021-08-31 18:11:57 +0800481 }
482
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100483 /** Returns the initial fragment name that the activity will launch. */
484 @VisibleForTesting
485 public String getInitialFragmentName(Intent intent) {
486 return intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
487 }
488
Fan Zhangaf32e722018-07-17 10:12:30 -0700489 @Override
pastychangfefc6682019-03-21 19:42:39 +0800490 protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
491 theme.applyStyle(R.style.SetupWizardPartnerResource, true);
492 super.onApplyThemeResource(theme, resid, first);
493 }
494
495 @Override
Fan Zhangaf32e722018-07-17 10:12:30 -0700496 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
497 super.onActivityResult(requestCode, resultCode, data);
498 final List<Fragment> fragments = getSupportFragmentManager().getFragments();
499 if (fragments != null) {
500 for (Fragment fragment : fragments) {
501 if (fragment instanceof OnActivityResultListener) {
502 fragment.onActivityResult(requestCode, resultCode, data);
503 }
504 }
505 }
506 }
507
Doris Lingcb69baf2017-02-23 17:50:03 -0800508 @VisibleForTesting
Fan Zhang8e14ca02018-11-27 13:06:44 -0800509 void launchSettingFragment(String initialFragmentName, Intent intent) {
510 if (initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800511 setTitleFromIntent(intent);
512
513 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800514 switchToFragment(initialFragmentName, initialArguments, true,
515 mInitialTitleResId, mInitialTitle);
Doris Lingcb69baf2017-02-23 17:50:03 -0800516 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700517 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800518 mInitialTitleResId = R.string.dashboard_title;
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800519 switchToFragment(TopLevelSettings.class.getName(), null /* args */, false,
520 mInitialTitleResId, mInitialTitle);
Doris Lingcb69baf2017-02-23 17:50:03 -0800521 }
522 }
523
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700524 private void setTitleFromIntent(Intent intent) {
Fan Zhang22230502018-03-27 14:14:13 -0700525 Log.d(LOG_TAG, "Starting to set activity title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700526 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
527 if (initialTitleResId > 0) {
528 mInitialTitle = null;
529 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100530
531 final String initialTitleResPackageName = intent.getStringExtra(
532 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
533 if (initialTitleResPackageName != null) {
534 try {
535 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
536 0 /* flags */, new UserHandle(UserHandle.myUserId()));
537 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
538 setTitle(mInitialTitle);
539 mInitialTitleResId = -1;
540 return;
541 } catch (NameNotFoundException e) {
542 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
Tsung-Mao Fang78267b22021-10-18 22:01:27 +0800543 } catch (Resources.NotFoundException resourceNotFound) {
544 Log.w(LOG_TAG,
545 "Could not find title resource in " + initialTitleResPackageName);
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100546 }
547 } else {
548 setTitle(mInitialTitleResId);
549 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700550 } else {
551 mInitialTitleResId = -1;
552 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
553 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
554 setTitle(mInitialTitle);
555 }
Fan Zhang22230502018-03-27 14:14:13 -0700556 Log.d(LOG_TAG, "Done setting title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700557 }
558
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800559 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800560 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700561 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800562 }
563
Fan Zhang28691572016-03-23 15:31:08 -0700564 private void setTitleFromBackStack() {
tmfang27c84de2018-06-28 11:39:05 +0800565 final int count = getSupportFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700566
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800567 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700568 if (mInitialTitleResId > 0) {
569 setTitle(mInitialTitleResId);
570 } else {
571 setTitle(mInitialTitle);
572 }
Fan Zhang28691572016-03-23 15:31:08 -0700573 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800574 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700575
tmfang27c84de2018-06-28 11:39:05 +0800576 FragmentManager.BackStackEntry bse = getSupportFragmentManager().
577 getBackStackEntryAt(count - 1);
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800578 setTitleFromBackStackEntry(bse);
579 }
580
581 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
582 final CharSequence title;
583 final int titleRes = bse.getBreadCrumbTitleRes();
584 if (titleRes > 0) {
585 title = getText(titleRes);
586 } else {
587 title = bse.getBreadCrumbTitle();
588 }
589 if (title != null) {
590 setTitle(title);
591 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800592 }
593
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 @Override
595 protected void onSaveInstanceState(Bundle outState) {
596 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700597 saveState(outState);
598 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800599
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700600 /**
601 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
602 */
603 @VisibleForTesting
604 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700605 if (mCategories.size() > 0) {
606 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800607 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800608 }
609
610 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400611 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800612 super.onResume();
613
Tony Mantler3d84d562017-07-31 10:48:55 -0700614 mDevelopmentSettingsListener = new BroadcastReceiver() {
615 @Override
616 public void onReceive(Context context, Intent intent) {
617 updateTilesList();
618 }
619 };
620 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
621 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800622
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800623 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700624
Jason Monk4da79e02015-09-10 10:54:36 -0400625 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800626 }
627
628 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400629 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800630 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700631 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
632 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800633 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800634 }
635
636 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800637 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winson Chungf07554f2020-02-18 16:42:26 -0800638 taskDescription.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_settings));
jackqdyulei6c355f52017-03-01 17:37:23 -0800639 super.setTaskDescription(taskDescription);
640 }
641
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800642 protected boolean isValidFragment(String fragmentName) {
643 // Almost all fragments are wrapped in this,
644 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800645 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
646 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647 }
648 return false;
649 }
650
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 @Override
652 public Intent getIntent() {
653 Intent superIntent = super.getIntent();
654 String startingFragment = getStartingFragmentClass(superIntent);
655 // This is called from super.onCreate, isMultiPane() is not yet reliable
656 // Do not use onIsHidingHeaders either, which relies itself on this method
657 if (startingFragment != null) {
658 Intent modIntent = new Intent(superIntent);
659 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Fan Zhange8666c62018-03-02 11:01:15 -0800660 Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800661 if (args != null) {
662 args = new Bundle(args);
663 } else {
664 args = new Bundle();
665 }
666 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100667 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800668 return modIntent;
669 }
670 return superIntent;
671 }
672
673 /**
674 * Checks if the component name in the intent is different from the Settings class and
675 * returns the class name to load as a fragment.
676 */
677 private String getStartingFragmentClass(Intent intent) {
678 if (mFragmentClass != null) return mFragmentClass;
679
680 String intentClass = intent.getComponent().getClassName();
681 if (intentClass.equals(getClass().getName())) return null;
682
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700683 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800684 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
685 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700686 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800687 }
688
689 return intentClass;
690 }
691
692 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 * Called by a preference panel fragment to finish itself.
694 *
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800695 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800696 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800697 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800698 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800699 */
Fan Zhang47854c22018-02-19 13:54:09 -0800700 public void finishPreferencePanel(int resultCode, Intent resultData) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800701 setResult(resultCode, resultData);
Vinit Nayakf7d423d2019-07-01 13:24:39 -0700702 if (resultData != null &&
703 resultData.getBooleanExtra(KEY_REMOVE_TASK_WHEN_FINISHING, false)) {
704 finishAndRemoveTask();
705 } else {
706 finish();
707 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800708 }
709
710 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700711 * Switch to a specific Fragment with taking care of validation, Title and BackStack
712 */
Jason Chiuc38978f2021-06-29 21:10:32 +0800713 private void switchToFragment(String fragmentName, Bundle args, boolean validate,
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800714 int titleResId, CharSequence title) {
Fan Zhang22230502018-03-27 14:14:13 -0700715 Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700716 if (validate && !isValidFragment(fragmentName)) {
717 throw new IllegalArgumentException("Invalid fragment for this activity: "
718 + fragmentName);
719 }
Raff Tsaiafd494d2019-10-16 15:25:21 +0800720 Fragment f = Utils.getTargetFragment(this, fragmentName, args);
Jason Chiuc38978f2021-06-29 21:10:32 +0800721 if (f == null) {
722 return;
723 }
tmfang27c84de2018-06-28 11:39:05 +0800724 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700725 transaction.replace(R.id.main_content, f);
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();
tmfang27c84de2018-06-28 11:39:05 +0800732 getSupportFragmentManager().executePendingTransactions();
Fan Zhang22230502018-03-27 14:14:13 -0700733 Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700734 }
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
tmfangd97fba52018-06-14 14:48:35 +0800739 // SettingsBaseActivity will pick up on the updates automatically.
Jason Chiubac59872019-10-24 17:30:34 +0800740 AsyncTask.execute(() -> doUpdateTilesList());
Jason Monkfd2c7222015-12-02 15:38:38 -0500741 }
742
743 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400744 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700745 final UserManager um = UserManager.get(this);
746 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700747 boolean somethingChanged = false;
Fan Zhang22230502018-03-27 14:14:13 -0700748 final String packageName = getPackageName();
749 final StringBuilder changedList = new StringBuilder();
750 somethingChanged = setTileEnabled(changedList,
Fan Zhangaeb94f02017-07-05 13:46:04 -0700751 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
752 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700753
Fan Zhang22230502018-03-27 14:14:13 -0700754 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800755 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700756 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
757 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700758
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700759 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
760 // enable DataPlanUsageSummaryActivity.
Fan Zhang22230502018-03-27 14:14:13 -0700761 somethingChanged = setTileEnabled(changedList,
Fan Zhang3ed55b12018-02-07 13:50:09 -0800762 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700763 Utils.isBandwidthControlEnabled() /* enabled */,
764 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700765
Fan Zhang22230502018-03-27 14:14:13 -0700766 somethingChanged = setTileEnabled(changedList,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800767 new ComponentName(packageName,
768 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Jerry Zhang40bd5702018-02-23 14:43:58 -0800769 !UserManager.isDeviceInDemoMode(this) /* enabled */,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800770 isAdmin) || somethingChanged;
771
Fan Zhang22230502018-03-27 14:14:13 -0700772 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800773 Settings.PowerUsageSummaryActivity.class.getName()),
Lei Yu68ffee12018-03-26 14:31:57 -0700774 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400775
Fan Zhang22230502018-03-27 14:14:13 -0700776 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800777 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhangcd21cb82018-06-11 09:28:02 -0700778 Utils.isBandwidthControlEnabled(), isAdmin)
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800779 || somethingChanged;
780
Fan Zhang22230502018-03-27 14:14:13 -0700781 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800782 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400783 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700784 && !Utils.isMonkeyRunning(), isAdmin)
785 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400786
Tony Mantler3d84d562017-07-31 10:48:55 -0700787 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800788 && !Utils.isMonkeyRunning();
Fan Zhang22230502018-03-27 14:14:13 -0700789 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4c26da92017-09-08 15:29:54 -0700790 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
Christine Franks986d02f2018-04-23 11:19:57 -0700791 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700792 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400793
Chandan Nath4d626f62019-03-12 10:52:53 +0000794 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800795 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700796 WifiDisplaySettings.isAvailable(this), isAdmin)
797 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800798
Jason Monk4da79e02015-09-10 10:54:36 -0400799 if (UserHandle.MU_ENABLED && !isAdmin) {
800 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800801 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800802 synchronized (categories) {
803 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700804 final int tileCount = category.getTilesCount();
805 for (int i = 0; i < tileCount; i++) {
Fan Zhang3506b742018-08-07 13:52:52 -0700806 final ComponentName component = category.getTile(i)
807 .getIntent().getComponent();
Fan Zhanga6986e72017-03-08 09:24:45 -0800808 final String name = component.getClassName();
809 final boolean isEnabledForRestricted = ArrayUtils.contains(
Christine Franks986d02f2018-04-23 11:19:57 -0700810 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
Fan Zhanga6986e72017-03-08 09:24:45 -0800811 if (packageName.equals(component.getPackageName())
812 && !isEnabledForRestricted) {
Fan Zhang22230502018-03-27 14:14:13 -0700813 somethingChanged =
814 setTileEnabled(changedList, component, false, isAdmin)
815 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800816 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700817 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700818 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700819 }
820 }
Doris Ling26bf0032016-06-15 18:16:09 -0700821
Fan Zhang224caad2017-01-06 11:42:48 -0800822 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700823 if (somethingChanged) {
Fan Zhang22230502018-03-27 14:14:13 -0700824 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories "
825 + changedList.toString());
Jason Chiuc713c3e2021-06-17 17:48:02 +0800826 mCategoryMixin.updateCategories();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700827 } else {
828 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
829 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500830 }
831
Fan Zhangaeb94f02017-07-05 13:46:04 -0700832 /**
833 * @return whether or not the enabled state actually changed.
834 */
Fan Zhang22230502018-03-27 14:14:13 -0700835 private boolean setTileEnabled(StringBuilder changedList, ComponentName component,
836 boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400837 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800838 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
839 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400840 enabled = false;
841 }
Fan Zhang22230502018-03-27 14:14:13 -0700842 boolean changed = setTileEnabled(component, enabled);
843 if (changed) {
844 changedList.append(component.toShortString()).append(",");
845 }
846 return changed;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500847 }
848
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800849 private void getMetaData() {
850 try {
851 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
852 PackageManager.GET_META_DATA);
853 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800854 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Jason Chiu29a09e52021-10-12 17:24:34 +0800855 mHighlightMenuKey = ai.metaData.getString(META_DATA_KEY_HIGHLIGHT_MENU_KEY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800856 } catch (NameNotFoundException nnfe) {
857 // No recovery
858 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
859 }
860 }
861
862 // give subclasses access to the Next button
863 public boolean hasNextButton() {
864 return mNextButton != null;
865 }
866
867 public Button getNextButton() {
868 return mNextButton;
869 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700870}