blob: 1a9bdc742fed3c2bf1b82bcad4d5d2cd3b6e8b62 [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;
Jason Chiu1b1c07a2022-05-13 16:42:07 +080037import android.content.pm.UserInfo;
Tsung-Mao Fang78267b22021-10-18 22:01:27 +080038import android.content.res.Resources;
pastychangfefc6682019-03-21 19:42:39 +080039import android.content.res.Resources.Theme;
Winson Chungf07554f2020-02-18 16:42:26 -080040import android.graphics.drawable.Icon;
Jason Monkfd2c7222015-12-02 15:38:38 -050041import android.os.AsyncTask;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080042import android.os.Bundle;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080043import android.os.UserHandle;
44import android.os.UserManager;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080045import android.text.TextUtils;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080046import android.util.Log;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080047import android.view.View;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080048import android.widget.Button;
Fan Zhang4c26da92017-09-08 15:29:54 -070049
Fan Zhang60243e62018-08-03 09:13:53 -070050import androidx.annotation.Nullable;
51import androidx.annotation.VisibleForTesting;
52import androidx.fragment.app.Fragment;
53import androidx.fragment.app.FragmentManager;
54import androidx.fragment.app.FragmentTransaction;
55import androidx.localbroadcastmanager.content.LocalBroadcastManager;
56import androidx.preference.Preference;
57import androidx.preference.PreferenceFragmentCompat;
58import androidx.preference.PreferenceManager;
59
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080060import com.android.internal.util.ArrayUtils;
Jason Monk4da79e02015-09-10 10:54:36 -040061import com.android.settings.Settings.WifiSettingsActivity;
Arc Wang3e816462021-09-27 14:16:14 +080062import com.android.settings.activityembedding.ActivityEmbeddingUtils;
Fan Zhanga8cac7a2017-10-11 16:44:41 -070063import com.android.settings.applications.manageapplications.ManageApplications;
Fan Zhangaf32e722018-07-17 10:12:30 -070064import com.android.settings.core.OnActivityResultListener;
tmfangd97fba52018-06-14 14:48:35 +080065import com.android.settings.core.SettingsBaseActivity;
Fan Zhang615563d2018-02-15 15:19:33 -080066import com.android.settings.core.SubSettingLauncher;
Fan Zhanga3e8f5c2016-12-01 09:52:10 -080067import com.android.settings.core.gateway.SettingsGateway;
Fan Zhanga96a2d82016-09-27 17:51:11 -070068import com.android.settings.dashboard.DashboardFeatureProvider;
Jason Chiu177573c2021-12-02 14:26:24 +080069import com.android.settings.homepage.DeepLinkHomepageActivityInternal;
Arc Wang22ce3922021-08-31 18:11:57 +080070import com.android.settings.homepage.SettingsHomepageActivity;
Fan Zhang21526c12018-09-06 15:44:26 -070071import com.android.settings.homepage.TopLevelSettings;
Fan Zhanga96a2d82016-09-27 17:51:11 -070072import com.android.settings.overlay.FeatureFactory;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -080073import com.android.settings.wfd.WifiDisplaySettings;
Stanley Wangb87ddba2020-12-25 16:27:32 +080074import com.android.settings.widget.SettingsMainSwitchBar;
Fan Zhang7cf99f52018-02-16 10:37:37 -080075import com.android.settingslib.core.instrumentation.Instrumentable;
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -080076import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
Tony Mantler3d84d562017-07-31 10:48:55 -070077import com.android.settingslib.development.DevelopmentSettingsEnabler;
Jason Monk4da79e02015-09-10 10:54:36 -040078import com.android.settingslib.drawer.DashboardCategory;
Fan Zhang4c26da92017-09-08 15:29:54 -070079
cnchen3eee5cf2019-05-06 10:19:10 +080080import com.google.android.setupcompat.util.WizardManagerHelper;
81
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080082import java.util.ArrayList;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080083import java.util.List;
84
tmfangd97fba52018-06-14 14:48:35 +080085
86public class SettingsActivity extends SettingsBaseActivity
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080087 implements PreferenceManager.OnPreferenceTreeClickListener,
tmfang27c84de2018-06-28 11:39:05 +080088 PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
Fan Zhangff4da232017-11-09 14:07:23 -080089 ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080090
Fan Zhang22230502018-03-27 14:14:13 -070091 private static final String LOG_TAG = "SettingsActivity";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080092
93 // Constants for state save/restore
Fabrice Di Meglio769630c2014-04-24 14:48:48 -070094 private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -080095
96 /**
97 * When starting this activity, the invoking Intent can contain this extra
98 * string to specify which fragment should be initially displayed.
99 * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
100 * will call isValidFragment() to confirm that the fragment class name is valid for this
101 * activity.
102 */
103 public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
104
105 /**
106 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
107 * this extra can also be specified to supply a Bundle of arguments to pass
108 * to that fragment when it is instantiated during the initial creation
109 * of the activity.
110 */
111 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
112
113 /**
Fabrice Di Meglioc1457322014-04-04 19:07:50 -0700114 * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
115 */
116 public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
117
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800118 // extras that allow any preference activity to be launched as part of a wizard
119
120 // show Back and Next buttons? takes boolean parameter
121 // Back will then return RESULT_CANCELED and Next RESULT_OK
122 protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
123
124 // add a Skip button?
125 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
126
127 // specify custom text for the Back or Next buttons, or cause a button to not appear
128 // at all by setting it to null
129 protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
130 protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
131
132 /**
133 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700134 * 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 -0800135 * that fragment.
136 */
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700137 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100138 /**
139 * The package name used to resolve the title resource id.
140 */
141 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
142 ":settings:show_fragment_title_res_package_name";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700143 public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
144 ":settings:show_fragment_title_resid";
Fabrice Di Meglio61a1fec2014-08-13 16:22:38 -0700145
146 public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
147 ":settings:show_fragment_as_subsetting";
Tsung-Mao Fang00cbd722022-02-17 20:02:29 +0800148 public static final String EXTRA_IS_SECOND_LAYER_PAGE = ":settings:is_second_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
Jason Chiu1b1c07a2022-05-13 16:42:07 +0800156 public static final String EXTRA_USER_HANDLE = "user_handle";
157
Arc Wanga63e9c62021-09-28 20:39:15 +0800158 /**
Raff Tsai910ea662019-12-04 10:56:43 +0800159 * Personal or Work profile tab of {@link ProfileSelectFragment}
160 * <p>0: Personal tab.
161 * <p>1: Work profile tab.
162 */
163 public static final String EXTRA_SHOW_FRAGMENT_TAB =
164 ":settings:show_fragment_tab";
165
Jason Monk30695812015-11-17 09:01:08 -0500166 public static final String META_DATA_KEY_FRAGMENT_CLASS =
Fan Zhangf11c8852018-02-06 14:21:01 -0800167 "com.android.settings.FRAGMENT_CLASS";
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800168
Jason Chiu29a09e52021-10-12 17:24:34 +0800169 public static final String META_DATA_KEY_HIGHLIGHT_MENU_KEY =
170 "com.android.settings.HIGHLIGHT_MENU_KEY";
171
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800172 private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
173
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800174 private String mFragmentClass;
Jason Chiu29a09e52021-10-12 17:24:34 +0800175 private String mHighlightMenuKey;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800176
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800177 private CharSequence mInitialTitle;
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700178 private int mInitialTitleResId;
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800179
Tony Mantler3d84d562017-07-31 10:48:55 -0700180 private BroadcastReceiver mDevelopmentSettingsListener;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800181
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800182 private boolean mBatteryPresent = true;
183 private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800184 @Override
185 public void onReceive(Context context, Intent intent) {
186 String action = intent.getAction();
187 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
188 boolean batteryPresent = Utils.isBatteryPresent(intent);
189
190 if (mBatteryPresent != batteryPresent) {
191 mBatteryPresent = batteryPresent;
Jason Monk4da79e02015-09-10 10:54:36 -0400192 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800193 }
194 }
195 }
196 };
197
Stanley Wangb87ddba2020-12-25 16:27:32 +0800198 private SettingsMainSwitchBar mMainSwitch;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700199
200 private Button mNextButton;
Fabrice Di Meglio3d35ec72014-06-06 12:13:29 -0700201
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700202 // Categories
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800203 private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800204
Fan Zhanga96a2d82016-09-27 17:51:11 -0700205 private DashboardFeatureProvider mDashboardFeatureProvider;
Jim Miller0698a212014-10-16 19:49:07 -0700206
Stanley Wangb87ddba2020-12-25 16:27:32 +0800207 public SettingsMainSwitchBar getSwitchBar() {
208 return mMainSwitch;
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700209 }
210
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800211 @Override
tmfang27c84de2018-06-28 11:39:05 +0800212 public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800213 new SubSettingLauncher(this)
214 .setDestination(pref.getFragment())
215 .setArguments(pref.getExtras())
216 .setSourceMetricsCategory(caller instanceof Instrumentable
217 ? ((Instrumentable) caller).getMetricsCategory()
218 : Instrumentable.METRICS_CATEGORY_UNKNOWN)
hjchangliaoe86eec02018-05-02 13:01:07 +0800219 .setTitleRes(-1)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800220 .launch();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800221 return true;
222 }
223
224 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400225 public boolean onPreferenceTreeClick(Preference preference) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800226 return false;
227 }
228
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800229 @Override
Jason Monk31c7c322016-01-20 14:41:52 -0500230 public SharedPreferences getSharedPreferences(String name, int mode) {
231 if (name.equals(getPackageName() + "_preferences")) {
Leif Hendrik Wilden28dee1f2018-01-11 10:15:36 -0800232 return new SharedPreferencesLogger(this, getMetricsTag(),
233 FeatureFactory.getFactory(this).getMetricsFeatureProvider());
Jason Monk31c7c322016-01-20 14:41:52 -0500234 }
235 return super.getSharedPreferences(name, mode);
236 }
237
Jason Monke4ebcd12016-02-21 09:37:41 -0500238 private String getMetricsTag() {
Raff Tsai54a5c442019-07-22 10:13:11 +0800239 String tag = null;
Jason Monke4ebcd12016-02-21 09:37:41 -0500240 if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100241 tag = getInitialFragmentName(getIntent());
Jason Monke4ebcd12016-02-21 09:37:41 -0500242 }
Raff Tsai54a5c442019-07-22 10:13:11 +0800243 if (TextUtils.isEmpty(tag)) {
244 Log.w(LOG_TAG, "MetricsTag is invalid " + tag);
245 tag = getClass().getName();
246 }
Jason Monke4ebcd12016-02-21 09:37:41 -0500247 if (tag.startsWith("com.android.settings.")) {
248 tag = tag.replace("com.android.settings.", "");
249 }
250 return tag;
251 }
252
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700253 @Override
254 protected void onCreate(Bundle savedState) {
Jason Chiu29a09e52021-10-12 17:24:34 +0800255 // Should happen before any call to getIntent()
256 getMetaData();
257 final Intent intent = getIntent();
Arc Wange80b74d2021-11-01 19:07:43 +0800258
Arc Wang9ba80e72022-02-25 12:35:20 +0800259 if (shouldShowTwoPaneDeepLink(intent) && tryStartTwoPaneDeepLink(intent)) {
Jason Chiu177573c2021-12-02 14:26:24 +0800260 finish();
Jason Chiua3b21cf2021-11-16 18:20:19 +0800261 super.onCreate(savedState);
262 return;
263 }
264
265 super.onCreate(savedState);
266 Log.d(LOG_TAG, "Starting onCreate");
267
268 long startTime = System.currentTimeMillis();
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800269
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700270 final FeatureFactory factory = FeatureFactory.getFactory(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700271 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700272
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700273 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
274 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800275 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800276
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700277 // Getting Intent properties can only be done after the super.onCreate(...)
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100278 final String initialFragmentName = getInitialFragmentName(intent);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700279
Fan Zhang92f1e942017-01-21 10:07:26 -0800280 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
cnchen3eee5cf2019-05-06 10:19:10 +0800281 // insets.
282 // If this is in setup flow, don't apply theme. Because light theme needs to be applied
283 // in SettingsBaseActivity#onCreate().
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800284 if (isSubSettings(intent) && !WizardManagerHelper.isAnySetupWizard(getIntent())) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800285 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700286 }
287
Fan Zhang4df9bfa2019-02-22 13:53:09 -0800288 setContentView(R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800289
tmfang27c84de2018-06-28 11:39:05 +0800290 getSupportFragmentManager().addOnBackStackChangedListener(this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800291
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700292 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700293 // We are restarting from a previous saved state; used that to initialize, instead
294 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700295 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800296
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700297 ArrayList<DashboardCategory> categories =
298 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
299 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700300 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700301 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700302 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800303 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800304 } else {
Fan Zhang8e14ca02018-11-27 13:06:44 -0800305 launchSettingFragment(initialFragmentName, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 }
307
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800308 final boolean isActionBarButtonEnabled = isActionBarButtonEnabled(intent);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700309
Fan Zhang4df9bfa2019-02-22 13:53:09 -0800310 final ActionBar actionBar = getActionBar();
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700311 if (actionBar != null) {
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800312 actionBar.setDisplayHomeAsUpEnabled(isActionBarButtonEnabled);
313 actionBar.setHomeButtonEnabled(isActionBarButtonEnabled);
Mill Chen7b163e22021-01-14 18:33:46 +0800314 actionBar.setDisplayShowTitleEnabled(true);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700315 }
Stanley Wangb87ddba2020-12-25 16:27:32 +0800316 mMainSwitch = findViewById(R.id.switch_bar);
317 if (mMainSwitch != null) {
318 mMainSwitch.setMetricsTag(getMetricsTag());
Stanley Wang3b70c6c2021-03-03 12:06:14 +0800319 mMainSwitch.setTranslationZ(findViewById(R.id.main_content).getTranslationZ() + 1);
Jason Monke4ebcd12016-02-21 09:37:41 -0500320 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700321
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800322 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800323 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
324
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700325 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 if (buttonBar != null) {
327 buttonBar.setVisibility(View.VISIBLE);
328
Fan Zhange994d462018-12-04 12:43:14 -0800329 Button backButton = findViewById(R.id.back_button);
330 backButton.setOnClickListener(v -> {
331 setResult(RESULT_CANCELED, null);
332 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800333 });
Fan Zhange994d462018-12-04 12:43:14 -0800334 Button skipButton = findViewById(R.id.skip_button);
335 skipButton.setOnClickListener(v -> {
336 setResult(RESULT_OK, null);
337 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800338 });
Fan Zhange994d462018-12-04 12:43:14 -0800339 mNextButton = findViewById(R.id.next_button);
340 mNextButton.setOnClickListener(v -> {
341 setResult(RESULT_OK, null);
342 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800343 });
344
345 // set our various button parameters
346 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
347 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
348 if (TextUtils.isEmpty(buttonText)) {
349 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800350 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800351 mNextButton.setText(buttonText);
352 }
353 }
354 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
355 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
356 if (TextUtils.isEmpty(buttonText)) {
357 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800358 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800359 backButton.setText(buttonText);
360 }
361 }
362 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
363 skipButton.setVisibility(View.VISIBLE);
364 }
365 }
366 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700367
Fan Zhangefba6b42017-02-07 17:40:59 -0800368 if (DEBUG_TIMING) {
369 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
370 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700371 }
372
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800373 private boolean isActionBarButtonEnabled(Intent intent) {
374 if (WizardManagerHelper.isAnySetupWizard(intent)) {
375 return false;
376 }
Tsung-Mao Fang00cbd722022-02-17 20:02:29 +0800377 final boolean isSecondLayerPage =
378 intent.getBooleanExtra(EXTRA_IS_SECOND_LAYER_PAGE, false);
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800379
380 // TODO: move Settings's ActivityEmbeddingUtils to SettingsLib.
381 return !com.android.settingslib.activityembedding.ActivityEmbeddingUtils
Tsung-Mao Fang00cbd722022-02-17 20:02:29 +0800382 .shouldHideNavigateUpButton(this, isSecondLayerPage);
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800383 }
384
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800385 private boolean isSubSettings(Intent intent) {
386 return this instanceof SubSettings ||
387 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
388 }
389
Jason Chiue427cc62021-10-25 14:11:16 +0800390 /**
391 * Returns the deep link trampoline intent for large screen devices.
392 */
393 public static Intent getTrampolineIntent(Intent intent, String highlightMenuKey) {
Arc Wanga63e9c62021-09-28 20:39:15 +0800394 final Intent detailIntent = new Intent(intent);
Arc Wang3e816462021-09-27 14:16:14 +0800395 // It's a deep link intent, SettingsHomepageActivity will set SplitPairRule and start it.
Jason Chiu209df882021-12-09 16:51:59 +0800396 final Intent trampolineIntent = new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY)
397 .setPackage(Utils.SETTINGS_PACKAGE_NAME)
398 .replaceExtras(detailIntent);
Arc Wanga63e9c62021-09-28 20:39:15 +0800399
400 // Relay detail intent data to prevent failure of Intent#ParseUri.
401 // If Intent#getData() is not null, Intent#toUri will return an Uri which has the scheme of
402 // Intent#getData() and it may not be the scheme of an Intent.
Arc Wang3e816462021-09-27 14:16:14 +0800403 trampolineIntent.putExtra(
Arc Wanga63e9c62021-09-28 20:39:15 +0800404 SettingsHomepageActivity.EXTRA_SETTINGS_LARGE_SCREEN_DEEP_LINK_INTENT_DATA,
405 detailIntent.getData());
406 detailIntent.setData(null);
407
408 trampolineIntent.putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI,
409 detailIntent.toUri(Intent.URI_INTENT_SCHEME));
410
Jason Chiue427cc62021-10-25 14:11:16 +0800411 trampolineIntent.putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY,
412 highlightMenuKey);
413 trampolineIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
414 return trampolineIntent;
415 }
416
Arc Wang9ba80e72022-02-25 12:35:20 +0800417 private boolean tryStartTwoPaneDeepLink(Intent intent) {
Jason Chiue427cc62021-10-25 14:11:16 +0800418 final Intent trampolineIntent;
419 if (intent.getBooleanExtra(EXTRA_IS_FROM_SLICE, false)) {
Arc Wanga63e9c62021-09-28 20:39:15 +0800420 // Get menu key for slice deep link case.
Jason Chiue427cc62021-10-25 14:11:16 +0800421 final String highlightMenuKey = intent.getStringExtra(
Arc Wanga63e9c62021-09-28 20:39:15 +0800422 EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY);
423 if (!TextUtils.isEmpty(highlightMenuKey)) {
424 mHighlightMenuKey = highlightMenuKey;
425 }
Jason Chiue427cc62021-10-25 14:11:16 +0800426 trampolineIntent = getTrampolineIntent(intent, mHighlightMenuKey);
Jason Chiu177573c2021-12-02 14:26:24 +0800427 trampolineIntent.setClass(this, DeepLinkHomepageActivityInternal.class);
Jason Chiue427cc62021-10-25 14:11:16 +0800428 } else {
429 trampolineIntent = getTrampolineIntent(intent, mHighlightMenuKey);
Arc Wanga63e9c62021-09-28 20:39:15 +0800430 }
Arc Wang9ba80e72022-02-25 12:35:20 +0800431
432 try {
Jason Chiu1b1c07a2022-05-13 16:42:07 +0800433 final UserManager um = getSystemService(UserManager.class);
434 final UserInfo userInfo = um.getUserInfo(getUser().getIdentifier());
435 if (userInfo.isManagedProfile()) {
Jason Chiu26588fc2022-05-20 12:05:32 +0800436 trampolineIntent.setClass(this, DeepLinkHomepageActivityInternal.class)
437 .putExtra(EXTRA_USER_HANDLE, getUser());
Jason Chiu1b1c07a2022-05-13 16:42:07 +0800438 startActivityAsUser(trampolineIntent, um.getPrimaryUser().getUserHandle());
439 } else {
440 startActivity(trampolineIntent);
441 }
Arc Wang9ba80e72022-02-25 12:35:20 +0800442 } catch (ActivityNotFoundException e) {
443 Log.e(LOG_TAG, "Deep link homepage is not available to show 2-pane UI");
444 return false;
445 }
446 return true;
Arc Wang3e816462021-09-27 14:16:14 +0800447 }
448
449 private boolean shouldShowTwoPaneDeepLink(Intent intent) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800450 if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
451 return false;
452 }
453
Jason Chiu177573c2021-12-02 14:26:24 +0800454 // If the activity is task root, starting trampoline is needed in order to show two-pane UI.
455 // If FLAG_ACTIVITY_NEW_TASK is set, the activity will become the start of a new task on
456 // this history stack, so starting trampoline is needed in order to notify the homepage that
457 // the highlight key is changed.
458 if (!isTaskRoot() && (intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800459 return false;
460 }
461
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800462 // Only starts trampoline for deep links. Should return false for all the cases that
463 // Settings app starts SettingsActivity or SubSetting by itself.
464 if (intent.getAction() == null) {
465 // Other apps should send deep link intent which matches intent filter of the Activity.
466 return false;
467 }
468
Jason Chiu0c732f22022-05-26 17:47:18 +0800469 // If the activity's launch mode is "singleInstance", it can't be embedded in Settings since
470 // it will be created in a new task.
471 ActivityInfo info = intent.resolveActivityInfo(getPackageManager(),
472 PackageManager.MATCH_DEFAULT_ONLY);
473 if (info.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
474 Log.w(LOG_TAG, "launchMode: singleInstance");
475 return false;
476 }
477
Arc Wanga63e9c62021-09-28 20:39:15 +0800478 if (intent.getBooleanExtra(EXTRA_IS_FROM_SLICE, false)) {
479 // Slice deep link starts the Intent using SubSettingLauncher. Returns true to show
480 // 2-pane deep link.
481 return true;
482 }
483
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800484 if (isSubSettings(intent)) {
485 return false;
486 }
487
Arc Wang488f79c2021-11-05 11:07:33 +0800488 if (intent.getBooleanExtra(SettingsHomepageActivity.EXTRA_IS_FROM_SETTINGS_HOMEPAGE,
489 /* defaultValue */ false)) {
490 return false;
491 }
492
Arc Wang3e816462021-09-27 14:16:14 +0800493 if (TextUtils.equals(intent.getAction(), Intent.ACTION_CREATE_SHORTCUT)) {
494 // Returns false to show full screen for Intent.ACTION_CREATE_SHORTCUT because
495 // - Launcher startActivityForResult for Intent.ACTION_CREATE_SHORTCUT and activity
496 // stack starts from launcher, CreateShortcutActivity will not follows SplitPaitRule
497 // registered by Settings.
498 // - There is no CreateShortcutActivity entry point from Settings app UI.
499 return false;
500 }
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800501
502 return true;
Arc Wang22ce3922021-08-31 18:11:57 +0800503 }
504
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100505 /** Returns the initial fragment name that the activity will launch. */
506 @VisibleForTesting
507 public String getInitialFragmentName(Intent intent) {
508 return intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
509 }
510
Fan Zhangaf32e722018-07-17 10:12:30 -0700511 @Override
pastychangfefc6682019-03-21 19:42:39 +0800512 protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
513 theme.applyStyle(R.style.SetupWizardPartnerResource, true);
514 super.onApplyThemeResource(theme, resid, first);
515 }
516
517 @Override
Fan Zhangaf32e722018-07-17 10:12:30 -0700518 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
519 super.onActivityResult(requestCode, resultCode, data);
520 final List<Fragment> fragments = getSupportFragmentManager().getFragments();
521 if (fragments != null) {
522 for (Fragment fragment : fragments) {
523 if (fragment instanceof OnActivityResultListener) {
524 fragment.onActivityResult(requestCode, resultCode, data);
525 }
526 }
527 }
528 }
529
Doris Lingcb69baf2017-02-23 17:50:03 -0800530 @VisibleForTesting
Fan Zhang8e14ca02018-11-27 13:06:44 -0800531 void launchSettingFragment(String initialFragmentName, Intent intent) {
532 if (initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800533 setTitleFromIntent(intent);
534
535 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800536 switchToFragment(initialFragmentName, initialArguments, true,
537 mInitialTitleResId, mInitialTitle);
Doris Lingcb69baf2017-02-23 17:50:03 -0800538 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700539 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800540 mInitialTitleResId = R.string.dashboard_title;
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800541 switchToFragment(TopLevelSettings.class.getName(), null /* args */, false,
542 mInitialTitleResId, mInitialTitle);
Doris Lingcb69baf2017-02-23 17:50:03 -0800543 }
544 }
545
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700546 private void setTitleFromIntent(Intent intent) {
Fan Zhang22230502018-03-27 14:14:13 -0700547 Log.d(LOG_TAG, "Starting to set activity title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700548 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
549 if (initialTitleResId > 0) {
550 mInitialTitle = null;
551 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100552
553 final String initialTitleResPackageName = intent.getStringExtra(
554 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
555 if (initialTitleResPackageName != null) {
556 try {
557 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
558 0 /* flags */, new UserHandle(UserHandle.myUserId()));
559 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
560 setTitle(mInitialTitle);
561 mInitialTitleResId = -1;
562 return;
563 } catch (NameNotFoundException e) {
564 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
Tsung-Mao Fang78267b22021-10-18 22:01:27 +0800565 } catch (Resources.NotFoundException resourceNotFound) {
566 Log.w(LOG_TAG,
567 "Could not find title resource in " + initialTitleResPackageName);
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100568 }
569 } else {
570 setTitle(mInitialTitleResId);
571 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700572 } else {
573 mInitialTitleResId = -1;
574 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
575 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
576 setTitle(mInitialTitle);
577 }
Fan Zhang22230502018-03-27 14:14:13 -0700578 Log.d(LOG_TAG, "Done setting title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700579 }
580
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800581 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800582 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700583 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800584 }
585
Fan Zhang28691572016-03-23 15:31:08 -0700586 private void setTitleFromBackStack() {
tmfang27c84de2018-06-28 11:39:05 +0800587 final int count = getSupportFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700588
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800589 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700590 if (mInitialTitleResId > 0) {
591 setTitle(mInitialTitleResId);
592 } else {
593 setTitle(mInitialTitle);
594 }
Fan Zhang28691572016-03-23 15:31:08 -0700595 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800596 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700597
tmfang27c84de2018-06-28 11:39:05 +0800598 FragmentManager.BackStackEntry bse = getSupportFragmentManager().
599 getBackStackEntryAt(count - 1);
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800600 setTitleFromBackStackEntry(bse);
601 }
602
603 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
604 final CharSequence title;
605 final int titleRes = bse.getBreadCrumbTitleRes();
606 if (titleRes > 0) {
607 title = getText(titleRes);
608 } else {
609 title = bse.getBreadCrumbTitle();
610 }
611 if (title != null) {
612 setTitle(title);
613 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800614 }
615
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800616 @Override
617 protected void onSaveInstanceState(Bundle outState) {
618 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700619 saveState(outState);
620 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800621
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700622 /**
623 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
624 */
625 @VisibleForTesting
626 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700627 if (mCategories.size() > 0) {
628 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800629 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800630 }
631
632 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400633 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800634 super.onResume();
635
Tony Mantler3d84d562017-07-31 10:48:55 -0700636 mDevelopmentSettingsListener = new BroadcastReceiver() {
637 @Override
638 public void onReceive(Context context, Intent intent) {
639 updateTilesList();
640 }
641 };
642 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
643 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800644
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800645 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700646
Jason Monk4da79e02015-09-10 10:54:36 -0400647 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 }
649
650 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400651 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700653 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
654 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 }
657
658 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800659 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winson Chungf07554f2020-02-18 16:42:26 -0800660 taskDescription.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_settings));
jackqdyulei6c355f52017-03-01 17:37:23 -0800661 super.setTaskDescription(taskDescription);
662 }
663
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800664 protected boolean isValidFragment(String fragmentName) {
665 // Almost all fragments are wrapped in this,
666 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800667 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
668 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800669 }
670 return false;
671 }
672
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800673 @Override
674 public Intent getIntent() {
675 Intent superIntent = super.getIntent();
676 String startingFragment = getStartingFragmentClass(superIntent);
677 // This is called from super.onCreate, isMultiPane() is not yet reliable
678 // Do not use onIsHidingHeaders either, which relies itself on this method
679 if (startingFragment != null) {
680 Intent modIntent = new Intent(superIntent);
681 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Fan Zhange8666c62018-03-02 11:01:15 -0800682 Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800683 if (args != null) {
684 args = new Bundle(args);
685 } else {
686 args = new Bundle();
687 }
688 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100689 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800690 return modIntent;
691 }
692 return superIntent;
693 }
694
695 /**
696 * Checks if the component name in the intent is different from the Settings class and
697 * returns the class name to load as a fragment.
698 */
699 private String getStartingFragmentClass(Intent intent) {
700 if (mFragmentClass != null) return mFragmentClass;
701
702 String intentClass = intent.getComponent().getClassName();
703 if (intentClass.equals(getClass().getName())) return null;
704
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700705 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800706 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
707 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700708 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800709 }
710
711 return intentClass;
712 }
713
714 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800715 * Called by a preference panel fragment to finish itself.
716 *
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800717 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800718 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800719 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800720 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800721 */
Fan Zhang47854c22018-02-19 13:54:09 -0800722 public void finishPreferencePanel(int resultCode, Intent resultData) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800723 setResult(resultCode, resultData);
Vinit Nayakf7d423d2019-07-01 13:24:39 -0700724 if (resultData != null &&
725 resultData.getBooleanExtra(KEY_REMOVE_TASK_WHEN_FINISHING, false)) {
726 finishAndRemoveTask();
727 } else {
728 finish();
729 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800730 }
731
732 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700733 * Switch to a specific Fragment with taking care of validation, Title and BackStack
734 */
Jason Chiuc38978f2021-06-29 21:10:32 +0800735 private void switchToFragment(String fragmentName, Bundle args, boolean validate,
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800736 int titleResId, CharSequence title) {
Fan Zhang22230502018-03-27 14:14:13 -0700737 Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700738 if (validate && !isValidFragment(fragmentName)) {
739 throw new IllegalArgumentException("Invalid fragment for this activity: "
740 + fragmentName);
741 }
Raff Tsaiafd494d2019-10-16 15:25:21 +0800742 Fragment f = Utils.getTargetFragment(this, fragmentName, args);
Jason Chiuc38978f2021-06-29 21:10:32 +0800743 if (f == null) {
744 return;
745 }
tmfang27c84de2018-06-28 11:39:05 +0800746 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700747 transaction.replace(R.id.main_content, f);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700748 if (titleResId > 0) {
749 transaction.setBreadCrumbTitle(titleResId);
750 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700751 transaction.setBreadCrumbTitle(title);
752 }
753 transaction.commitAllowingStateLoss();
tmfang27c84de2018-06-28 11:39:05 +0800754 getSupportFragmentManager().executePendingTransactions();
Fan Zhang22230502018-03-27 14:14:13 -0700755 Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700756 }
757
Jason Monk4da79e02015-09-10 10:54:36 -0400758 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500759 // Generally the items that are will be changing from these updates will
760 // not be in the top list of tiles, so run it in the background and the
tmfangd97fba52018-06-14 14:48:35 +0800761 // SettingsBaseActivity will pick up on the updates automatically.
Jason Chiubac59872019-10-24 17:30:34 +0800762 AsyncTask.execute(() -> doUpdateTilesList());
Jason Monkfd2c7222015-12-02 15:38:38 -0500763 }
764
765 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400766 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700767 final UserManager um = UserManager.get(this);
768 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700769 boolean somethingChanged = false;
Fan Zhang22230502018-03-27 14:14:13 -0700770 final String packageName = getPackageName();
771 final StringBuilder changedList = new StringBuilder();
772 somethingChanged = setTileEnabled(changedList,
Fan Zhangaeb94f02017-07-05 13:46:04 -0700773 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
774 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700775
Fan Zhang22230502018-03-27 14:14:13 -0700776 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800777 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700778 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
779 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700780
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700781 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
782 // enable DataPlanUsageSummaryActivity.
Fan Zhang22230502018-03-27 14:14:13 -0700783 somethingChanged = setTileEnabled(changedList,
Fan Zhang3ed55b12018-02-07 13:50:09 -0800784 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700785 Utils.isBandwidthControlEnabled() /* enabled */,
786 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700787
Fan Zhang22230502018-03-27 14:14:13 -0700788 somethingChanged = setTileEnabled(changedList,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800789 new ComponentName(packageName,
790 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Jerry Zhang40bd5702018-02-23 14:43:58 -0800791 !UserManager.isDeviceInDemoMode(this) /* enabled */,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800792 isAdmin) || somethingChanged;
793
Fan Zhang22230502018-03-27 14:14:13 -0700794 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800795 Settings.PowerUsageSummaryActivity.class.getName()),
Lei Yu68ffee12018-03-26 14:31:57 -0700796 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400797
Fan Zhang22230502018-03-27 14:14:13 -0700798 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800799 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhangcd21cb82018-06-11 09:28:02 -0700800 Utils.isBandwidthControlEnabled(), isAdmin)
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800801 || somethingChanged;
802
Fan Zhang22230502018-03-27 14:14:13 -0700803 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800804 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400805 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700806 && !Utils.isMonkeyRunning(), isAdmin)
807 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400808
Tony Mantler3d84d562017-07-31 10:48:55 -0700809 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800810 && !Utils.isMonkeyRunning();
Fan Zhang22230502018-03-27 14:14:13 -0700811 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4c26da92017-09-08 15:29:54 -0700812 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
Christine Franks986d02f2018-04-23 11:19:57 -0700813 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700814 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400815
Chandan Nath4d626f62019-03-12 10:52:53 +0000816 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800817 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700818 WifiDisplaySettings.isAvailable(this), isAdmin)
819 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800820
Jason Monk4da79e02015-09-10 10:54:36 -0400821 if (UserHandle.MU_ENABLED && !isAdmin) {
822 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800823 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800824 synchronized (categories) {
825 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700826 final int tileCount = category.getTilesCount();
827 for (int i = 0; i < tileCount; i++) {
Fan Zhang3506b742018-08-07 13:52:52 -0700828 final ComponentName component = category.getTile(i)
829 .getIntent().getComponent();
Fan Zhanga6986e72017-03-08 09:24:45 -0800830 final String name = component.getClassName();
831 final boolean isEnabledForRestricted = ArrayUtils.contains(
Christine Franks986d02f2018-04-23 11:19:57 -0700832 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
Fan Zhanga6986e72017-03-08 09:24:45 -0800833 if (packageName.equals(component.getPackageName())
834 && !isEnabledForRestricted) {
Fan Zhang22230502018-03-27 14:14:13 -0700835 somethingChanged =
836 setTileEnabled(changedList, component, false, isAdmin)
837 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800838 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700839 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700840 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700841 }
842 }
Doris Ling26bf0032016-06-15 18:16:09 -0700843
Fan Zhang224caad2017-01-06 11:42:48 -0800844 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700845 if (somethingChanged) {
Fan Zhang22230502018-03-27 14:14:13 -0700846 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories "
847 + changedList.toString());
Jason Chiuc713c3e2021-06-17 17:48:02 +0800848 mCategoryMixin.updateCategories();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700849 } else {
850 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
851 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500852 }
853
Fan Zhangaeb94f02017-07-05 13:46:04 -0700854 /**
855 * @return whether or not the enabled state actually changed.
856 */
Fan Zhang22230502018-03-27 14:14:13 -0700857 private boolean setTileEnabled(StringBuilder changedList, ComponentName component,
858 boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400859 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800860 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
861 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400862 enabled = false;
863 }
Fan Zhang22230502018-03-27 14:14:13 -0700864 boolean changed = setTileEnabled(component, enabled);
865 if (changed) {
866 changedList.append(component.toShortString()).append(",");
867 }
868 return changed;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500869 }
870
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800871 private void getMetaData() {
872 try {
873 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
874 PackageManager.GET_META_DATA);
875 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800876 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Jason Chiu29a09e52021-10-12 17:24:34 +0800877 mHighlightMenuKey = ai.metaData.getString(META_DATA_KEY_HIGHLIGHT_MENU_KEY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800878 } catch (NameNotFoundException nnfe) {
879 // No recovery
880 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
881 }
882 }
883
884 // give subclasses access to the Next button
885 public boolean hasNextButton() {
886 return mNextButton != null;
887 }
888
889 public Button getNextButton() {
890 return mNextButton;
891 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700892}