blob: f9515017406ee788c3e8fbdbac0cff9f12897da8 [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");
Bonian Chend10618d2022-05-30 19:06:10 +0800267 createUiFromIntent(savedState, intent);
268 }
Jason Chiua3b21cf2021-11-16 18:20:19 +0800269
Bonian Chend10618d2022-05-30 19:06:10 +0800270 protected void createUiFromIntent(Bundle savedState, Intent intent) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800271 long startTime = System.currentTimeMillis();
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800272
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700273 final FeatureFactory factory = FeatureFactory.getFactory(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700274 mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
Matthew Fritze0ed37c32016-10-24 10:12:49 -0700275
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700276 if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
277 getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800278 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800279
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700280 // Getting Intent properties can only be done after the super.onCreate(...)
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100281 final String initialFragmentName = getInitialFragmentName(intent);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700282
Fan Zhang92f1e942017-01-21 10:07:26 -0800283 // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
cnchen3eee5cf2019-05-06 10:19:10 +0800284 // insets.
285 // If this is in setup flow, don't apply theme. Because light theme needs to be applied
286 // in SettingsBaseActivity#onCreate().
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800287 if (isSubSettings(intent) && !WizardManagerHelper.isAnySetupWizard(getIntent())) {
Fan Zhang92f1e942017-01-21 10:07:26 -0800288 setTheme(R.style.Theme_SubSettings);
Fabrice Di Meglioda8baba2014-06-10 17:12:51 -0700289 }
290
Fan Zhang4df9bfa2019-02-22 13:53:09 -0800291 setContentView(R.layout.settings_main_prefs);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800292
tmfang27c84de2018-06-28 11:39:05 +0800293 getSupportFragmentManager().addOnBackStackChangedListener(this);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800294
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700295 if (savedState != null) {
Fabrice Di Meglio1800a9f2014-04-03 19:31:07 -0700296 // We are restarting from a previous saved state; used that to initialize, instead
297 // of starting fresh.
Fabrice Di Meglio0d643fd2014-06-16 20:11:27 -0700298 setTitleFromIntent(intent);
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800299
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700300 ArrayList<DashboardCategory> categories =
301 savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
302 if (categories != null) {
Fabrice Di Meglio5f995722014-05-19 19:51:31 -0700303 mCategories.clear();
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700304 mCategories.addAll(categories);
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700305 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800306 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800307 } else {
Fan Zhang8e14ca02018-11-27 13:06:44 -0800308 launchSettingFragment(initialFragmentName, intent);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800309 }
310
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800311 final boolean isActionBarButtonEnabled = isActionBarButtonEnabled(intent);
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700312
Fan Zhang4df9bfa2019-02-22 13:53:09 -0800313 final ActionBar actionBar = getActionBar();
Andrew Sappersteina2dd9972017-06-25 13:51:08 -0700314 if (actionBar != null) {
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800315 actionBar.setDisplayHomeAsUpEnabled(isActionBarButtonEnabled);
316 actionBar.setHomeButtonEnabled(isActionBarButtonEnabled);
Mill Chen7b163e22021-01-14 18:33:46 +0800317 actionBar.setDisplayShowTitleEnabled(true);
Fabrice Di Megliod8aec082014-05-20 10:49:50 -0700318 }
Stanley Wangb87ddba2020-12-25 16:27:32 +0800319 mMainSwitch = findViewById(R.id.switch_bar);
320 if (mMainSwitch != null) {
321 mMainSwitch.setMetricsTag(getMetricsTag());
Stanley Wang3b70c6c2021-03-03 12:06:14 +0800322 mMainSwitch.setTranslationZ(findViewById(R.id.main_content).getTranslationZ() + 1);
Jason Monke4ebcd12016-02-21 09:37:41 -0500323 }
Fabrice Di Meglio41937762014-05-13 19:51:59 -0700324
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800325 // see if we should show Back/Next buttons
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800326 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
327
Fabrice Di Megliod2b64f32014-05-20 12:55:15 -0700328 View buttonBar = findViewById(R.id.button_bar);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800329 if (buttonBar != null) {
330 buttonBar.setVisibility(View.VISIBLE);
331
Fan Zhange994d462018-12-04 12:43:14 -0800332 Button backButton = findViewById(R.id.back_button);
333 backButton.setOnClickListener(v -> {
334 setResult(RESULT_CANCELED, null);
335 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800336 });
Fan Zhange994d462018-12-04 12:43:14 -0800337 Button skipButton = findViewById(R.id.skip_button);
338 skipButton.setOnClickListener(v -> {
339 setResult(RESULT_OK, null);
340 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800341 });
Fan Zhange994d462018-12-04 12:43:14 -0800342 mNextButton = findViewById(R.id.next_button);
343 mNextButton.setOnClickListener(v -> {
344 setResult(RESULT_OK, null);
345 finish();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800346 });
347
348 // set our various button parameters
349 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
350 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
351 if (TextUtils.isEmpty(buttonText)) {
352 mNextButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800353 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800354 mNextButton.setText(buttonText);
355 }
356 }
357 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
358 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
359 if (TextUtils.isEmpty(buttonText)) {
360 backButton.setVisibility(View.GONE);
Fan Zhangf11c8852018-02-06 14:21:01 -0800361 } else {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800362 backButton.setText(buttonText);
363 }
364 }
365 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
366 skipButton.setVisibility(View.VISIBLE);
367 }
368 }
369 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700370
Fan Zhangefba6b42017-02-07 17:40:59 -0800371 if (DEBUG_TIMING) {
372 Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
373 }
Fabrice Di Meglioceb335f2014-07-23 16:25:30 -0700374 }
375
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800376 private boolean isActionBarButtonEnabled(Intent intent) {
377 if (WizardManagerHelper.isAnySetupWizard(intent)) {
378 return false;
379 }
Tsung-Mao Fang00cbd722022-02-17 20:02:29 +0800380 final boolean isSecondLayerPage =
381 intent.getBooleanExtra(EXTRA_IS_SECOND_LAYER_PAGE, false);
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800382
383 // TODO: move Settings's ActivityEmbeddingUtils to SettingsLib.
384 return !com.android.settingslib.activityembedding.ActivityEmbeddingUtils
Tsung-Mao Fang00cbd722022-02-17 20:02:29 +0800385 .shouldHideNavigateUpButton(this, isSecondLayerPage);
Tsung-Mao Fang2df79642022-01-21 18:14:53 +0800386 }
387
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800388 private boolean isSubSettings(Intent intent) {
389 return this instanceof SubSettings ||
390 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
391 }
392
Jason Chiue427cc62021-10-25 14:11:16 +0800393 /**
394 * Returns the deep link trampoline intent for large screen devices.
395 */
396 public static Intent getTrampolineIntent(Intent intent, String highlightMenuKey) {
Arc Wanga63e9c62021-09-28 20:39:15 +0800397 final Intent detailIntent = new Intent(intent);
Arc Wang3e816462021-09-27 14:16:14 +0800398 // It's a deep link intent, SettingsHomepageActivity will set SplitPairRule and start it.
Jason Chiu209df882021-12-09 16:51:59 +0800399 final Intent trampolineIntent = new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY)
400 .setPackage(Utils.SETTINGS_PACKAGE_NAME)
401 .replaceExtras(detailIntent);
Arc Wanga63e9c62021-09-28 20:39:15 +0800402
403 // Relay detail intent data to prevent failure of Intent#ParseUri.
404 // If Intent#getData() is not null, Intent#toUri will return an Uri which has the scheme of
405 // Intent#getData() and it may not be the scheme of an Intent.
Arc Wang3e816462021-09-27 14:16:14 +0800406 trampolineIntent.putExtra(
Arc Wanga63e9c62021-09-28 20:39:15 +0800407 SettingsHomepageActivity.EXTRA_SETTINGS_LARGE_SCREEN_DEEP_LINK_INTENT_DATA,
408 detailIntent.getData());
409 detailIntent.setData(null);
410
411 trampolineIntent.putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI,
412 detailIntent.toUri(Intent.URI_INTENT_SCHEME));
413
Jason Chiue427cc62021-10-25 14:11:16 +0800414 trampolineIntent.putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY,
415 highlightMenuKey);
416 trampolineIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
417 return trampolineIntent;
418 }
419
Arc Wang9ba80e72022-02-25 12:35:20 +0800420 private boolean tryStartTwoPaneDeepLink(Intent intent) {
Jason Chiue427cc62021-10-25 14:11:16 +0800421 final Intent trampolineIntent;
422 if (intent.getBooleanExtra(EXTRA_IS_FROM_SLICE, false)) {
Arc Wanga63e9c62021-09-28 20:39:15 +0800423 // Get menu key for slice deep link case.
Jason Chiue427cc62021-10-25 14:11:16 +0800424 final String highlightMenuKey = intent.getStringExtra(
Arc Wanga63e9c62021-09-28 20:39:15 +0800425 EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY);
426 if (!TextUtils.isEmpty(highlightMenuKey)) {
427 mHighlightMenuKey = highlightMenuKey;
428 }
Jason Chiue427cc62021-10-25 14:11:16 +0800429 trampolineIntent = getTrampolineIntent(intent, mHighlightMenuKey);
Jason Chiu177573c2021-12-02 14:26:24 +0800430 trampolineIntent.setClass(this, DeepLinkHomepageActivityInternal.class);
Jason Chiue427cc62021-10-25 14:11:16 +0800431 } else {
432 trampolineIntent = getTrampolineIntent(intent, mHighlightMenuKey);
Arc Wanga63e9c62021-09-28 20:39:15 +0800433 }
Arc Wang9ba80e72022-02-25 12:35:20 +0800434
435 try {
Jason Chiu1b1c07a2022-05-13 16:42:07 +0800436 final UserManager um = getSystemService(UserManager.class);
437 final UserInfo userInfo = um.getUserInfo(getUser().getIdentifier());
438 if (userInfo.isManagedProfile()) {
Jason Chiu26588fc2022-05-20 12:05:32 +0800439 trampolineIntent.setClass(this, DeepLinkHomepageActivityInternal.class)
440 .putExtra(EXTRA_USER_HANDLE, getUser());
Jason Chiu1b1c07a2022-05-13 16:42:07 +0800441 startActivityAsUser(trampolineIntent, um.getPrimaryUser().getUserHandle());
442 } else {
443 startActivity(trampolineIntent);
444 }
Arc Wang9ba80e72022-02-25 12:35:20 +0800445 } catch (ActivityNotFoundException e) {
446 Log.e(LOG_TAG, "Deep link homepage is not available to show 2-pane UI");
447 return false;
448 }
449 return true;
Arc Wang3e816462021-09-27 14:16:14 +0800450 }
451
452 private boolean shouldShowTwoPaneDeepLink(Intent intent) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800453 if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
454 return false;
455 }
456
Jason Chiu177573c2021-12-02 14:26:24 +0800457 // If the activity is task root, starting trampoline is needed in order to show two-pane UI.
458 // If FLAG_ACTIVITY_NEW_TASK is set, the activity will become the start of a new task on
459 // this history stack, so starting trampoline is needed in order to notify the homepage that
460 // the highlight key is changed.
461 if (!isTaskRoot() && (intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Jason Chiua3b21cf2021-11-16 18:20:19 +0800462 return false;
463 }
464
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800465 // Only starts trampoline for deep links. Should return false for all the cases that
466 // Settings app starts SettingsActivity or SubSetting by itself.
467 if (intent.getAction() == null) {
468 // Other apps should send deep link intent which matches intent filter of the Activity.
469 return false;
470 }
471
Jason Chiu0c732f22022-05-26 17:47:18 +0800472 // If the activity's launch mode is "singleInstance", it can't be embedded in Settings since
473 // it will be created in a new task.
474 ActivityInfo info = intent.resolveActivityInfo(getPackageManager(),
475 PackageManager.MATCH_DEFAULT_ONLY);
476 if (info.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
477 Log.w(LOG_TAG, "launchMode: singleInstance");
478 return false;
479 }
480
Arc Wanga63e9c62021-09-28 20:39:15 +0800481 if (intent.getBooleanExtra(EXTRA_IS_FROM_SLICE, false)) {
482 // Slice deep link starts the Intent using SubSettingLauncher. Returns true to show
483 // 2-pane deep link.
484 return true;
485 }
486
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800487 if (isSubSettings(intent)) {
488 return false;
489 }
490
Arc Wang488f79c2021-11-05 11:07:33 +0800491 if (intent.getBooleanExtra(SettingsHomepageActivity.EXTRA_IS_FROM_SETTINGS_HOMEPAGE,
492 /* defaultValue */ false)) {
493 return false;
494 }
495
Arc Wang3e816462021-09-27 14:16:14 +0800496 if (TextUtils.equals(intent.getAction(), Intent.ACTION_CREATE_SHORTCUT)) {
497 // Returns false to show full screen for Intent.ACTION_CREATE_SHORTCUT because
498 // - Launcher startActivityForResult for Intent.ACTION_CREATE_SHORTCUT and activity
499 // stack starts from launcher, CreateShortcutActivity will not follows SplitPaitRule
500 // registered by Settings.
501 // - There is no CreateShortcutActivity entry point from Settings app UI.
502 return false;
503 }
Arc Wangf6a8f6c2021-09-10 14:25:12 +0800504
505 return true;
Arc Wang22ce3922021-08-31 18:11:57 +0800506 }
507
Jan Tomljanovic07bffff2021-04-27 20:58:12 +0100508 /** Returns the initial fragment name that the activity will launch. */
509 @VisibleForTesting
510 public String getInitialFragmentName(Intent intent) {
511 return intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
512 }
513
Fan Zhangaf32e722018-07-17 10:12:30 -0700514 @Override
pastychangfefc6682019-03-21 19:42:39 +0800515 protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
516 theme.applyStyle(R.style.SetupWizardPartnerResource, true);
517 super.onApplyThemeResource(theme, resid, first);
518 }
519
520 @Override
Fan Zhangaf32e722018-07-17 10:12:30 -0700521 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
522 super.onActivityResult(requestCode, resultCode, data);
523 final List<Fragment> fragments = getSupportFragmentManager().getFragments();
524 if (fragments != null) {
525 for (Fragment fragment : fragments) {
526 if (fragment instanceof OnActivityResultListener) {
527 fragment.onActivityResult(requestCode, resultCode, data);
528 }
529 }
530 }
531 }
532
Doris Lingcb69baf2017-02-23 17:50:03 -0800533 @VisibleForTesting
Fan Zhang8e14ca02018-11-27 13:06:44 -0800534 void launchSettingFragment(String initialFragmentName, Intent intent) {
535 if (initialFragmentName != null) {
Doris Lingcb69baf2017-02-23 17:50:03 -0800536 setTitleFromIntent(intent);
537
538 Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800539 switchToFragment(initialFragmentName, initialArguments, true,
540 mInitialTitleResId, mInitialTitle);
Doris Lingcb69baf2017-02-23 17:50:03 -0800541 } else {
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700542 // Show search icon as up affordance if we are displaying the main Dashboard
Doris Lingcb69baf2017-02-23 17:50:03 -0800543 mInitialTitleResId = R.string.dashboard_title;
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800544 switchToFragment(TopLevelSettings.class.getName(), null /* args */, false,
545 mInitialTitleResId, mInitialTitle);
Doris Lingcb69baf2017-02-23 17:50:03 -0800546 }
547 }
548
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700549 private void setTitleFromIntent(Intent intent) {
Fan Zhang22230502018-03-27 14:14:13 -0700550 Log.d(LOG_TAG, "Starting to set activity title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700551 final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
552 if (initialTitleResId > 0) {
553 mInitialTitle = null;
554 mInitialTitleResId = initialTitleResId;
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100555
556 final String initialTitleResPackageName = intent.getStringExtra(
557 EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
558 if (initialTitleResPackageName != null) {
559 try {
560 Context authContext = createPackageContextAsUser(initialTitleResPackageName,
561 0 /* flags */, new UserHandle(UserHandle.myUserId()));
562 mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
563 setTitle(mInitialTitle);
564 mInitialTitleResId = -1;
565 return;
566 } catch (NameNotFoundException e) {
567 Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
Tsung-Mao Fang78267b22021-10-18 22:01:27 +0800568 } catch (Resources.NotFoundException resourceNotFound) {
569 Log.w(LOG_TAG,
570 "Could not find title resource in " + initialTitleResPackageName);
Alexandra Gherghina62464b82014-08-11 12:40:13 +0100571 }
572 } else {
573 setTitle(mInitialTitleResId);
574 }
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700575 } else {
576 mInitialTitleResId = -1;
577 final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
578 mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
579 setTitle(mInitialTitle);
580 }
Fan Zhang22230502018-03-27 14:14:13 -0700581 Log.d(LOG_TAG, "Done setting title");
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700582 }
583
Fabrice Di Meglioc95be4f2014-03-07 12:57:38 -0800584 @Override
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800585 public void onBackStackChanged() {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700586 setTitleFromBackStack();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800587 }
588
Fan Zhang28691572016-03-23 15:31:08 -0700589 private void setTitleFromBackStack() {
tmfang27c84de2018-06-28 11:39:05 +0800590 final int count = getSupportFragmentManager().getBackStackEntryCount();
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700591
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800592 if (count == 0) {
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700593 if (mInitialTitleResId > 0) {
594 setTitle(mInitialTitleResId);
595 } else {
596 setTitle(mInitialTitle);
597 }
Fan Zhang28691572016-03-23 15:31:08 -0700598 return;
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800599 }
Fabrice Di Megliob643cbf2014-03-10 12:18:39 -0700600
tmfang27c84de2018-06-28 11:39:05 +0800601 FragmentManager.BackStackEntry bse = getSupportFragmentManager().
602 getBackStackEntryAt(count - 1);
Fabrice Di Meglio8eb3f0f2014-02-27 15:51:46 -0800603 setTitleFromBackStackEntry(bse);
604 }
605
606 private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
607 final CharSequence title;
608 final int titleRes = bse.getBreadCrumbTitleRes();
609 if (titleRes > 0) {
610 title = getText(titleRes);
611 } else {
612 title = bse.getBreadCrumbTitle();
613 }
614 if (title != null) {
615 setTitle(title);
616 }
Fabrice Di Meglio5529d292014-02-11 19:52:28 -0800617 }
618
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800619 @Override
620 protected void onSaveInstanceState(Bundle outState) {
621 super.onSaveInstanceState(outState);
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700622 saveState(outState);
623 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800624
Matthew Fritze7d48ae82017-03-23 08:59:15 -0700625 /**
626 * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
627 */
628 @VisibleForTesting
629 void saveState(Bundle outState) {
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700630 if (mCategories.size() > 0) {
631 outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800632 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800633 }
634
635 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400636 protected void onResume() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800637 super.onResume();
638
Tony Mantler3d84d562017-07-31 10:48:55 -0700639 mDevelopmentSettingsListener = new BroadcastReceiver() {
640 @Override
641 public void onReceive(Context context, Intent intent) {
642 updateTilesList();
643 }
644 };
645 LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
646 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800647
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800648 registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Fabrice Di Meglioa3270762014-04-16 16:54:56 -0700649
Jason Monk4da79e02015-09-10 10:54:36 -0400650 updateTilesList();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800651 }
652
653 @Override
Jason Monk39b46742015-09-10 15:52:51 -0400654 protected void onPause() {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800655 super.onPause();
Tony Mantler3d84d562017-07-31 10:48:55 -0700656 LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
657 mDevelopmentSettingsListener = null;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 unregisterReceiver(mBatteryInfoReceiver);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800659 }
660
661 @Override
jackqdyulei6c355f52017-03-01 17:37:23 -0800662 public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
Winson Chungf07554f2020-02-18 16:42:26 -0800663 taskDescription.setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_settings));
jackqdyulei6c355f52017-03-01 17:37:23 -0800664 super.setTaskDescription(taskDescription);
665 }
666
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800667 protected boolean isValidFragment(String fragmentName) {
668 // Almost all fragments are wrapped in this,
669 // except for a few that have their own activities.
Fan Zhanga3e8f5c2016-12-01 09:52:10 -0800670 for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
671 if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800672 }
673 return false;
674 }
675
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800676 @Override
677 public Intent getIntent() {
678 Intent superIntent = super.getIntent();
679 String startingFragment = getStartingFragmentClass(superIntent);
680 // This is called from super.onCreate, isMultiPane() is not yet reliable
681 // Do not use onIsHidingHeaders either, which relies itself on this method
682 if (startingFragment != null) {
683 Intent modIntent = new Intent(superIntent);
684 modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
Fan Zhange8666c62018-03-02 11:01:15 -0800685 Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800686 if (args != null) {
687 args = new Bundle(args);
688 } else {
689 args = new Bundle();
690 }
691 args.putParcelable("intent", superIntent);
Kenny Guyac1e20e2014-06-24 14:34:14 +0100692 modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800693 return modIntent;
694 }
695 return superIntent;
696 }
697
698 /**
699 * Checks if the component name in the intent is different from the Settings class and
700 * returns the class name to load as a fragment.
701 */
702 private String getStartingFragmentClass(Intent intent) {
703 if (mFragmentClass != null) return mFragmentClass;
704
705 String intentClass = intent.getComponent().getClassName();
706 if (intentClass.equals(getClass().getName())) return null;
707
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700708 if ("com.android.settings.RunningServices".equals(intentClass)
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800709 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
710 // Old names of manage apps.
Fan Zhanga8cac7a2017-10-11 16:44:41 -0700711 intentClass = ManageApplications.class.getName();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800712 }
713
714 return intentClass;
715 }
716
717 /**
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800718 * Called by a preference panel fragment to finish itself.
719 *
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800720 * @param resultCode Optional result code to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800721 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800722 * @param resultData Optional result data to send back to the original
Fan Zhangf11c8852018-02-06 14:21:01 -0800723 * launching fragment.
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800724 */
Fan Zhang47854c22018-02-19 13:54:09 -0800725 public void finishPreferencePanel(int resultCode, Intent resultData) {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800726 setResult(resultCode, resultData);
Vinit Nayakf7d423d2019-07-01 13:24:39 -0700727 if (resultData != null &&
728 resultData.getBooleanExtra(KEY_REMOVE_TASK_WHEN_FINISHING, false)) {
729 finishAndRemoveTask();
730 } else {
731 finish();
732 }
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800733 }
734
735 /**
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700736 * Switch to a specific Fragment with taking care of validation, Title and BackStack
737 */
Jason Chiuc38978f2021-06-29 21:10:32 +0800738 private void switchToFragment(String fragmentName, Bundle args, boolean validate,
Fan Zhang39d9d7c2019-01-23 16:38:24 -0800739 int titleResId, CharSequence title) {
Fan Zhang22230502018-03-27 14:14:13 -0700740 Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700741 if (validate && !isValidFragment(fragmentName)) {
742 throw new IllegalArgumentException("Invalid fragment for this activity: "
743 + fragmentName);
744 }
Raff Tsaiafd494d2019-10-16 15:25:21 +0800745 Fragment f = Utils.getTargetFragment(this, fragmentName, args);
Jason Chiuc38978f2021-06-29 21:10:32 +0800746 if (f == null) {
747 return;
748 }
tmfang27c84de2018-06-28 11:39:05 +0800749 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Anna Galusza6b1e9db2016-03-30 17:25:36 -0700750 transaction.replace(R.id.main_content, f);
Fabrice Di Meglioa9e77992014-06-09 12:52:24 -0700751 if (titleResId > 0) {
752 transaction.setBreadCrumbTitle(titleResId);
753 } else if (title != null) {
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700754 transaction.setBreadCrumbTitle(title);
755 }
756 transaction.commitAllowingStateLoss();
tmfang27c84de2018-06-28 11:39:05 +0800757 getSupportFragmentManager().executePendingTransactions();
Fan Zhang22230502018-03-27 14:14:13 -0700758 Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
Fabrice Di Megliod25314d2014-03-21 19:24:43 -0700759 }
760
Jason Monk4da79e02015-09-10 10:54:36 -0400761 private void updateTilesList() {
Jason Monkfd2c7222015-12-02 15:38:38 -0500762 // Generally the items that are will be changing from these updates will
763 // not be in the top list of tiles, so run it in the background and the
tmfangd97fba52018-06-14 14:48:35 +0800764 // SettingsBaseActivity will pick up on the updates automatically.
Jason Chiubac59872019-10-24 17:30:34 +0800765 AsyncTask.execute(() -> doUpdateTilesList());
Jason Monkfd2c7222015-12-02 15:38:38 -0500766 }
767
768 private void doUpdateTilesList() {
Jason Monk4da79e02015-09-10 10:54:36 -0400769 PackageManager pm = getPackageManager();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700770 final UserManager um = UserManager.get(this);
771 final boolean isAdmin = um.isAdminUser();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700772 boolean somethingChanged = false;
Fan Zhang22230502018-03-27 14:14:13 -0700773 final String packageName = getPackageName();
774 final StringBuilder changedList = new StringBuilder();
775 somethingChanged = setTileEnabled(changedList,
Fan Zhangaeb94f02017-07-05 13:46:04 -0700776 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
777 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700778
Fan Zhang22230502018-03-27 14:14:13 -0700779 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800780 Settings.BluetoothSettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700781 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
782 || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700783
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700784 // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
785 // enable DataPlanUsageSummaryActivity.
Fan Zhang22230502018-03-27 14:14:13 -0700786 somethingChanged = setTileEnabled(changedList,
Fan Zhang3ed55b12018-02-07 13:50:09 -0800787 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
Rajeev Kumar4a5e3dc2017-07-18 19:21:34 -0700788 Utils.isBandwidthControlEnabled() /* enabled */,
789 isAdmin) || somethingChanged;
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700790
Fan Zhang22230502018-03-27 14:14:13 -0700791 somethingChanged = setTileEnabled(changedList,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800792 new ComponentName(packageName,
793 Settings.ConnectedDeviceDashboardActivity.class.getName()),
Jerry Zhang40bd5702018-02-23 14:43:58 -0800794 !UserManager.isDeviceInDemoMode(this) /* enabled */,
jackqdyulei76ba0f72017-11-17 14:14:51 -0800795 isAdmin) || somethingChanged;
796
Fan Zhang22230502018-03-27 14:14:13 -0700797 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800798 Settings.PowerUsageSummaryActivity.class.getName()),
Lei Yu68ffee12018-03-26 14:31:57 -0700799 mBatteryPresent, isAdmin) || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400800
Fan Zhang22230502018-03-27 14:14:13 -0700801 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800802 Settings.DataUsageSummaryActivity.class.getName()),
Fan Zhangcd21cb82018-06-11 09:28:02 -0700803 Utils.isBandwidthControlEnabled(), isAdmin)
Eric Schwarzenbach98998a22018-02-06 15:57:01 -0800804 || somethingChanged;
805
Fan Zhang22230502018-03-27 14:14:13 -0700806 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhanga6986e72017-03-08 09:24:45 -0800807 Settings.UserSettingsActivity.class.getName()),
Jason Monk4da79e02015-09-10 10:54:36 -0400808 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
Fan Zhangaeb94f02017-07-05 13:46:04 -0700809 && !Utils.isMonkeyRunning(), isAdmin)
810 || somethingChanged;
Jason Monk4da79e02015-09-10 10:54:36 -0400811
Tony Mantler3d84d562017-07-31 10:48:55 -0700812 final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
Fan Zhang831f6302017-11-22 13:07:50 -0800813 && !Utils.isMonkeyRunning();
Fan Zhang22230502018-03-27 14:14:13 -0700814 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4c26da92017-09-08 15:29:54 -0700815 Settings.DevelopmentSettingsDashboardActivity.class.getName()),
Christine Franks986d02f2018-04-23 11:19:57 -0700816 showDev, isAdmin)
Fan Zhangaeb94f02017-07-05 13:46:04 -0700817 || somethingChanged;
Dan Sandler12c4ba42016-03-28 11:13:40 -0400818
Chandan Nath4d626f62019-03-12 10:52:53 +0000819 somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800820 Settings.WifiDisplaySettingsActivity.class.getName()),
Fan Zhangaeb94f02017-07-05 13:46:04 -0700821 WifiDisplaySettings.isAvailable(this), isAdmin)
822 || somethingChanged;
Fan Zhang4fcd5ed2017-03-10 12:25:35 -0800823
Jason Monk4da79e02015-09-10 10:54:36 -0400824 if (UserHandle.MU_ENABLED && !isAdmin) {
825 // When on restricted users, disable all extra categories (but only the settings ones).
Fan Zhang78ab9f92017-02-27 16:10:05 -0800826 final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
Fan Zhanga6986e72017-03-08 09:24:45 -0800827 synchronized (categories) {
828 for (DashboardCategory category : categories) {
Fan Zhang7c8f8a02017-03-17 12:54:24 -0700829 final int tileCount = category.getTilesCount();
830 for (int i = 0; i < tileCount; i++) {
Fan Zhang3506b742018-08-07 13:52:52 -0700831 final ComponentName component = category.getTile(i)
832 .getIntent().getComponent();
Fan Zhanga6986e72017-03-08 09:24:45 -0800833 final String name = component.getClassName();
834 final boolean isEnabledForRestricted = ArrayUtils.contains(
Christine Franks986d02f2018-04-23 11:19:57 -0700835 SettingsGateway.SETTINGS_FOR_RESTRICTED, name);
Fan Zhanga6986e72017-03-08 09:24:45 -0800836 if (packageName.equals(component.getPackageName())
837 && !isEnabledForRestricted) {
Fan Zhang22230502018-03-27 14:14:13 -0700838 somethingChanged =
839 setTileEnabled(changedList, component, false, isAdmin)
840 || somethingChanged;
Fan Zhanga6986e72017-03-08 09:24:45 -0800841 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700842 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700843 }
Fabrice Di Meglio63bbb8e2014-04-23 16:44:30 -0700844 }
845 }
Doris Ling26bf0032016-06-15 18:16:09 -0700846
Fan Zhang224caad2017-01-06 11:42:48 -0800847 // Final step, refresh categories.
Fan Zhangaeb94f02017-07-05 13:46:04 -0700848 if (somethingChanged) {
Fan Zhang22230502018-03-27 14:14:13 -0700849 Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories "
850 + changedList.toString());
Jason Chiuc713c3e2021-06-17 17:48:02 +0800851 mCategoryMixin.updateCategories();
Fan Zhangaeb94f02017-07-05 13:46:04 -0700852 } else {
853 Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
854 }
Jason Monk2ebc8a02015-02-13 15:23:19 -0500855 }
856
Fan Zhangaeb94f02017-07-05 13:46:04 -0700857 /**
858 * @return whether or not the enabled state actually changed.
859 */
Fan Zhang22230502018-03-27 14:14:13 -0700860 private boolean setTileEnabled(StringBuilder changedList, ComponentName component,
861 boolean enabled, boolean isAdmin) {
Jason Monk5862c1e2016-06-07 14:02:33 -0400862 if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
Fan Zhang9ec45362017-01-10 11:55:45 -0800863 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
864 component.getClassName())) {
Jason Monk4da79e02015-09-10 10:54:36 -0400865 enabled = false;
866 }
Fan Zhang22230502018-03-27 14:14:13 -0700867 boolean changed = setTileEnabled(component, enabled);
868 if (changed) {
869 changedList.append(component.toShortString()).append(",");
870 }
871 return changed;
Jason Monk2ebc8a02015-02-13 15:23:19 -0500872 }
873
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800874 private void getMetaData() {
875 try {
876 ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
877 PackageManager.GET_META_DATA);
878 if (ai == null || ai.metaData == null) return;
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800879 mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
Jason Chiu29a09e52021-10-12 17:24:34 +0800880 mHighlightMenuKey = ai.metaData.getString(META_DATA_KEY_HIGHLIGHT_MENU_KEY);
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800881 } catch (NameNotFoundException nnfe) {
882 // No recovery
883 Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
884 }
885 }
886
887 // give subclasses access to the Next button
888 public boolean hasNextButton() {
889 return mNextButton != null;
890 }
891
892 public Button getNextButton() {
893 return mNextButton;
894 }
Andrew Sapperstein048f6fb2017-05-28 12:20:10 -0700895}