blob: 705dfa723a35c0e4ec9e81dda2643a1dcdf192c3 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2008 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
Jonathan Scotte0d43942022-01-19 15:19:08 +000019import static android.app.admin.DevicePolicyResources.Strings.Settings.PERSONAL_CATEGORY_HEADER;
20import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_CATEGORY_HEADER;
21
Maurice Lam2eb170c2017-04-28 16:18:47 -070022import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
23
Joe Onoratob51886d2010-11-08 18:25:51 -080024import android.accounts.Account;
25import android.accounts.AccountManager;
26import android.accounts.AuthenticatorDescription;
felkachanga6cec472018-03-29 12:08:20 +080027import android.app.ActionBar;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.app.Activity;
Jonathan Scotte0d43942022-01-19 15:19:08 +000029import android.app.admin.DevicePolicyManager;
Fan Zhang31b21002019-01-16 13:49:47 -080030import android.app.settings.SettingsEnums;
Christine Franksa09eb382017-06-23 14:28:21 -070031import android.content.ComponentName;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070032import android.content.ContentResolver;
Joe Onoratob51886d2010-11-08 18:25:51 -080033import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080034import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080035import android.content.pm.PackageManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080036import android.content.pm.ResolveInfo;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010037import android.content.pm.UserInfo;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070038import android.content.res.Resources;
felkachanga6cec472018-03-29 12:08:20 +080039import android.graphics.Color;
Joe Onoratob51886d2010-11-08 18:25:51 -080040import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080042import android.os.Environment;
Ben Komalo2a321922011-09-07 16:42:34 -070043import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010044import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070045import android.os.UserManager;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070046import android.provider.Settings;
qingxi072f2862017-04-11 18:28:40 -070047import android.telephony.euicc.EuiccManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080048import android.text.TextUtils;
Joe Onoratob51886d2010-11-08 18:25:51 -080049import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050import android.view.LayoutInflater;
51import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080052import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070053import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080054import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070056import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070057import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080058import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080059import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080060import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040061
Fan Zhang23f8d592018-08-28 15:11:40 -070062import androidx.annotation.VisibleForTesting;
63
Doris Ling72489722017-11-16 11:03:40 -080064import com.android.settings.core.InstrumentedFragment;
arangelove35badd2017-10-27 13:51:28 +010065import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Maurice Lam2eb170c2017-04-28 16:18:47 -070066import com.android.settings.password.ChooseLockSettingsHelper;
67import com.android.settings.password.ConfirmLockPattern;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070068import com.android.settingslib.RestrictedLockUtilsInternal;
Mill Chen1e607902021-02-25 15:03:25 +080069import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Taesu Lee39e4fbb2019-07-22 19:20:05 +090070import com.android.settingslib.development.DevelopmentSettingsEnabler;
Pasty Changc1f86002018-12-11 02:22:55 +000071
pastychang9bdb59a2019-01-21 09:49:15 +080072import com.google.android.setupcompat.template.FooterBarMixin;
73import com.google.android.setupcompat.template.FooterButton;
74import com.google.android.setupcompat.template.FooterButton.ButtonType;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000075import com.google.android.setupdesign.GlifLayout;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010077import java.util.List;
78
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080079/**
80 * Confirm and execute a reset of the device to a clean "just out of the box"
81 * state. Multiple confirmations are required: first, a general "are you sure
82 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
83 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
84 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
85 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070086 *
87 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 */
Edgar Wang21f8cb32020-08-04 23:13:05 +080089public class MainClear extends InstrumentedFragment implements OnGlobalLayoutListener {
90 private static final String TAG = "MainClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091
felkachanga6cec472018-03-29 12:08:20 +080092 @VisibleForTesting
93 static final int KEYGUARD_REQUEST = 55;
94 @VisibleForTesting
95 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096
Edgar Wang21f8cb32020-08-04 23:13:05 +080097 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX =
98 "masterclear.allow_retain_esim_profiles_after_fdr";
Qingxi Lic8a2b042018-01-11 11:35:07 -080099
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700100 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -0700101 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700103 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +0800104 @VisibleForTesting
pastychang103409b2018-12-13 23:00:17 +0800105 FooterButton mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700106 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800107 @VisibleForTesting
108 CheckBox mExternalStorage;
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800109 @VisibleForTesting
110 View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800111 @VisibleForTesting
112 CheckBox mEsimStorage;
113 @VisibleForTesting
114 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800115
Doris Ling1f497152018-02-22 16:33:07 -0800116 @Override
117 public void onGlobalLayout() {
118 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
119 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800120
felkachanga6cec472018-03-29 12:08:20 +0800121 private void setUpActionBarAndTitle() {
122 final Activity activity = getActivity();
123 if (activity == null) {
124 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
125 return;
126 }
127 final ActionBar actionBar = activity.getActionBar();
128 if (actionBar == null) {
129 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
130 return;
131 }
132 actionBar.hide();
133 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700134 }
135
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700137 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800139 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700140 * @param request the request code to be returned once confirmation finishes
141 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700143 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700144 Resources res = getActivity().getResources();
Kevin Chynb13bc502020-07-20 23:35:21 -0700145 final ChooseLockSettingsHelper.Builder builder =
146 new ChooseLockSettingsHelper.Builder(getActivity(), this);
147 return builder.setRequestCode(request)
Edgar Wang21f8cb32020-08-04 23:13:05 +0800148 .setTitle(res.getText(R.string.main_clear_short_title))
Kevin Chynb13bc502020-07-20 23:35:21 -0700149 .show();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 }
151
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800152 @VisibleForTesting
153 boolean isValidRequestCode(int requestCode) {
154 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
155 }
156
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800157 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700158 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800159 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800160 onActivityResultInternal(requestCode, resultCode, data);
161 }
162
163 /*
164 * Internal method that allows easy testing without dealing with super references.
165 */
166 @VisibleForTesting
167 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800168 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800169 return;
170 }
171
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800172 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800174 return;
175 }
176
177 Intent intent = null;
178 // If returning from a Keyguard request, try to show an account confirmation request if
179 // applciable.
180 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
181 && (intent = getAccountConfirmationIntent()) != null) {
182 showAccountCredentialConfirmation(intent);
183 } else {
184 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800185 }
186 }
187
qingxi072f2862017-04-11 18:28:40 -0700188 @VisibleForTesting
189 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800190 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700191 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Antony Sargent8d2baf62019-07-11 11:04:09 -0700192 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Mill Chen1e607902021-02-25 15:03:25 +0800193 final Intent intent = new Intent();
194 intent.setClass(getContext(),
195 com.android.settings.Settings.FactoryResetConfirmActivity.class);
196 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, MainClearConfirm.class.getName());
197 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
198 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID,
199 R.string.main_clear_confirm_title);
200 intent.putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, getMetricsCategory());
201 getContext().startActivity(intent);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700202 }
203
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800204 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800205 void showAccountCredentialConfirmation(Intent intent) {
206 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
207 }
208
209 @VisibleForTesting
210 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800211 final Context context = getActivity();
212 final String accountType = context.getString(R.string.account_type);
213 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800214 final String className = context.getString(R.string.account_confirmation_class);
215 if (TextUtils.isEmpty(accountType)
216 || TextUtils.isEmpty(packageName)
217 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800218 Log.i(TAG, "Resources not set for account confirmation.");
219 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800220 }
221 final AccountManager am = AccountManager.get(context);
222 Account[] accounts = am.getAccountsByType(accountType);
223 if (accounts != null && accounts.length > 0) {
224 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800225 .setPackage(packageName)
226 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800227 // Check to make sure that the intent is supported.
228 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800229 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
230 if (resolution != null
231 && resolution.activityInfo != null
232 && packageName.equals(resolution.activityInfo.packageName)) {
233 // Note that we need to check the packagename to make sure that an Activity resolver
234 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800235 return requestAccountConfirmation;
236 } else {
237 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800238 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800239 } else {
240 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800241 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800242 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800243 }
244
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800245 /**
246 * If the user clicks to begin the reset sequence, we next require a
247 * keyguard confirmation if the user has currently enabled one. If there
248 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700249 *
250 * If the user is in demo mode, route to the demo mode app for confirmation.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800251 */
Christine Franksa09eb382017-06-23 14:28:21 -0700252 @VisibleForTesting
253 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700254
Christine Franksa09eb382017-06-23 14:28:21 -0700255 public void onClick(View view) {
256 final Context context = view.getContext();
257 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700258 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
259 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700260 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700261 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700262 .setAction(Intent.ACTION_FACTORY_RESET);
263 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800264 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800265 return;
266 }
267
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800268 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
269 return;
270 }
271
272 Intent intent = getAccountConfirmationIntent();
273 if (intent != null) {
274 showAccountCredentialConfirmation(intent);
275 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700276 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800277 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800278 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700279 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800280
281 /**
282 * In its initial state, the activity presents a button for the user to
283 * click in order to initiate a confirmation sequence. This method is
284 * called from various other points in the code to reset the activity to
285 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800286 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800287 * <p>Reinflating views from resources is expensive and prevents us from
288 * caching widget pointers, so we use a single-inflate pattern: we lazy-
289 * inflate each view, caching all of the widget pointers we'll need at the
290 * time, then simply reuse the inflated views directly whenever we need
291 * to change contents.
292 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800293 @VisibleForTesting
294 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800295 setUpActionBarAndTitle();
296 setUpInitiateButton();
297
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700298 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800299 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800300 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800301 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
302 if (mScrollView != null) {
303 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
304 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800305 mScrollView = mContentView.findViewById(R.id.main_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800306
Kenny Root3785e392011-01-18 15:14:32 -0800307 /*
308 * If the external storage is emulated, it will be erased with a factory
309 * reset at any rate. There is no need to have a separate option until
310 * we have a factory reset that only erases some directories and not
Eric Biggers8c254152022-04-25 21:27:03 +0000311 * others.
Kenny Root3785e392011-01-18 15:14:32 -0800312 */
Eric Biggers8c254152022-04-25 21:27:03 +0000313 if (Environment.isExternalStorageEmulated()) {
Kenny Root3785e392011-01-18 15:14:32 -0800314 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700315
Kenny Root3785e392011-01-18 15:14:32 -0800316 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
317 externalOption.setVisibility(View.GONE);
318
319 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
320 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700321
Eric Biggers8c254152022-04-25 21:27:03 +0000322 mExternalStorage.setChecked(false);
Kenny Root3785e392011-01-18 15:14:32 -0800323 } else {
324 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
325
326 @Override
327 public void onClick(View v) {
328 mExternalStorage.toggle();
329 }
330 });
331 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800332
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700333 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800334 if (showWipeEuiccCheckbox()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800335 mEsimStorageContainer.setVisibility(View.VISIBLE);
336 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
337 @Override
338 public void onClick(View v) {
339 mEsimStorage.toggle();
340 }
341 });
342 } else {
343 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
344 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700345
Qingxi Lic8a2b042018-01-11 11:35:07 -0800346 final View noCancelMobilePlan = mContentView.findViewById(
347 R.id.no_cancel_mobile_plan);
348 noCancelMobilePlan.setVisibility(View.VISIBLE);
349 mEsimStorage.setChecked(true /* checked */);
350 }
Edgar Wang514d7462020-03-25 16:17:40 +0800351 } else {
352 mEsimStorage.setChecked(false /* checked */);
qingxi072f2862017-04-11 18:28:40 -0700353 }
354
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100355 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
356 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800357 final StringBuffer contentDescription = new StringBuffer();
Edgar Wang21f8cb32020-08-04 23:13:05 +0800358 final View mainClearContainer = mContentView.findViewById(R.id.main_clear_container);
359 getContentDescription(mainClearContainer, contentDescription);
360 mainClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800361
362 // Set the status of initiateButton based on scrollview
363 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
364 @Override
365 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800366 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800367 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
368 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800369 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800370 }
371 }
372 });
373
374 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800375 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800376 }
377
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700378 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700379 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700380 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700381 * <p>We show the strings on any device which supports eUICC as long as the eUICC was ever
Jeff Davidson4ba74782017-06-30 18:02:13 -0700382 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700383 */
384 @VisibleForTesting
385 boolean showWipeEuicc() {
386 Context context = getContext();
387 if (!isEuiccEnabled(context)) {
388 return false;
389 }
390 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700391 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
Taesu Lee39e4fbb2019-07-22 19:20:05 +0900392 || DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context);
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700393 }
394
395 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800396 boolean showWipeEuiccCheckbox() {
397 return SystemProperties
398 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
399 }
400
401 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700402 protected boolean isEuiccEnabled(Context context) {
403 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
404 return euiccManager.isEnabled();
405 }
406
jackqdyulei0b3edc72016-12-13 17:07:08 -0800407 @VisibleForTesting
408 boolean hasReachedBottom(final ScrollView scrollView) {
409 if (scrollView.getChildCount() < 1) {
410 return true;
411 }
412
413 final View view = scrollView.getChildAt(0);
414 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
415
416 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400417 }
418
felkachanga6cec472018-03-29 12:08:20 +0800419 private void setUpInitiateButton() {
420 if (mInitiateButton != null) {
421 return;
422 }
423
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000424 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
pastychang9bdb59a2019-01-21 09:49:15 +0800425 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
426 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800427 new FooterButton.Builder(getActivity())
Edgar Wang21f8cb32020-08-04 23:13:05 +0800428 .setText(R.string.main_clear_button_text)
pastychang908501e2018-12-26 10:46:40 +0800429 .setListener(mInitiateListener)
430 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800431 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800432 .build()
pastychang103409b2018-12-13 23:00:17 +0800433 );
pastychang9bdb59a2019-01-21 09:49:15 +0800434 mInitiateButton = mixin.getPrimaryButton();
felkachanga6cec472018-03-29 12:08:20 +0800435 }
436
Julia Reynoldsce25af42015-07-08 16:56:31 -0400437 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800438 if (v.getVisibility() != View.VISIBLE) {
439 return;
440 }
441 if (v instanceof ViewGroup) {
442 ViewGroup vGroup = (ViewGroup) v;
443 for (int i = 0; i < vGroup.getChildCount(); i++) {
444 View nextChild = vGroup.getChildAt(i);
445 getContentDescription(nextChild, description);
446 }
447 } else if (v instanceof TextView) {
448 TextView vText = (TextView) v;
449 description.append(vText.getText());
450 description.append(","); // Allow Talkback to pause between sections.
451 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800452 }
453
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100454 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800455 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
felkachanga6cec472018-03-29 12:08:20 +0800456 LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700457 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800458
459 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100460 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
461 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800462
463 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800464
felkachanga6cec472018-03-29 12:08:20 +0800465 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Joe Onoratob51886d2010-11-08 18:25:51 -0800466 Context.LAYOUT_INFLATER_SERVICE);
467
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100468 int accountsCount = 0;
469 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
470 final UserInfo userInfo = profiles.get(profileIndex);
471 final int profileId = userInfo.id;
472 final UserHandle userHandle = new UserHandle(profileId);
473 Account[] accounts = mgr.getAccountsAsUser(profileId);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800474 final int accountLength = accounts.length;
475 if (accountLength == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800476 continue;
477 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800478 accountsCount += accountLength;
Joe Onoratob51886d2010-11-08 18:25:51 -0800479
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100480 AuthenticatorDescription[] descs = AccountManager.get(context)
481 .getAuthenticatorTypesAsUser(profileId);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800482 final int descLength = descs.length;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100483
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200484 if (profilesSize > 1) {
485 View titleView = Utils.inflateCategoryHeader(inflater, contents);
Edgar Wang0e92c7e2022-05-02 13:39:28 +0800486 titleView.setPadding(0 /* left */, titleView.getPaddingTop(),
487 0 /* right */, titleView.getPaddingBottom());
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200488 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
Jonathan Scotte0d43942022-01-19 15:19:08 +0000489
490 DevicePolicyManager devicePolicyManager =
491 context.getSystemService(DevicePolicyManager.class);
492
493 if (userInfo.isManagedProfile()) {
kholoud mohamed90afe192022-03-14 11:11:33 +0000494 titleText.setText(devicePolicyManager.getResources().getString(
Jonathan Scotte0d43942022-01-19 15:19:08 +0000495 WORK_CATEGORY_HEADER, () -> getString(R.string.category_work)));
496 } else {
kholoud mohamed90afe192022-03-14 11:11:33 +0000497 titleText.setText(devicePolicyManager.getResources().getString(
Jonathan Scotte0d43942022-01-19 15:19:08 +0000498 PERSONAL_CATEGORY_HEADER, () -> getString(R.string.category_personal)));
499 }
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200500 contents.addView(titleView);
501 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100502
Edgar Wang21f8cb32020-08-04 23:13:05 +0800503 for (int i = 0; i < accountLength; i++) {
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100504 Account account = accounts[i];
505 AuthenticatorDescription desc = null;
Edgar Wang21f8cb32020-08-04 23:13:05 +0800506 for (int j = 0; j < descLength; j++) {
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100507 if (account.type.equals(descs[j].type)) {
508 desc = descs[j];
509 break;
510 }
511 }
512 if (desc == null) {
513 Log.w(TAG, "No descriptor for account name=" + account.name
514 + " type=" + account.type);
515 continue;
516 }
517 Drawable icon = null;
518 try {
519 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000520 Context authContext = context.createPackageContextAsUser(desc.packageName,
521 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100522 icon = context.getPackageManager().getUserBadgedIcon(
523 authContext.getDrawable(desc.iconId), userHandle);
524 }
525 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100526 Log.w(TAG, "Bad package name for account type " + desc.type);
527 } catch (Resources.NotFoundException e) {
528 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
529 }
530 if (icon == null) {
531 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100532 }
533
Edgar Wang21f8cb32020-08-04 23:13:05 +0800534 View child = inflater.inflate(R.layout.main_clear_account, contents, false);
Fan Zhang6b2bb392016-09-28 09:07:44 -0700535 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
536 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100537 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800538 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800539 }
540
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100541 if (accountsCount > 0) {
542 accountsLabel.setVisibility(View.VISIBLE);
543 contents.setVisibility(View.VISIBLE);
544 }
545 // Checking for all other users and their profiles if any.
546 View otherUsers = mContentView.findViewById(R.id.other_users_present);
547 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
548 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800549 }
550
551 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700552 public View onCreateView(LayoutInflater inflater, ViewGroup container,
553 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800554 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700555 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800556 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
557 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700558 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
559 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
560 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700561 if (disallow && !Utils.isDemoUser(context)) {
Edgar Wang21f8cb32020-08-04 23:13:05 +0800562 return inflater.inflate(R.layout.main_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000563 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100564 new ActionDisabledByAdminDialogHelper(getActivity())
565 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
566 .setOnDismissListener(__ -> getActivity().finish())
567 .show();
568 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400569 }
570
Edgar Wang21f8cb32020-08-04 23:13:05 +0800571 mContentView = inflater.inflate(R.layout.main_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800572
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800573 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700574 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800575 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400576
577 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700578 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800579 return SettingsEnums.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400580 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800581}