blob: 62462b3492a3ef3946711338d1793e0320cd2029 [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;
Inseob Kima1aeeb22018-11-08 12:44:47 +090047import android.sysprop.VoldProperties;
qingxi072f2862017-04-11 18:28:40 -070048import android.telephony.euicc.EuiccManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080049import android.text.TextUtils;
Joe Onoratob51886d2010-11-08 18:25:51 -080050import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051import android.view.LayoutInflater;
52import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080053import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070054import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080055import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070057import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070058import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080059import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080060import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080061import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040062
Fan Zhang23f8d592018-08-28 15:11:40 -070063import androidx.annotation.VisibleForTesting;
64
Doris Ling72489722017-11-16 11:03:40 -080065import com.android.settings.core.InstrumentedFragment;
arangelove35badd2017-10-27 13:51:28 +010066import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Maurice Lam2eb170c2017-04-28 16:18:47 -070067import com.android.settings.password.ChooseLockSettingsHelper;
68import com.android.settings.password.ConfirmLockPattern;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070069import com.android.settingslib.RestrictedLockUtilsInternal;
Mill Chen1e607902021-02-25 15:03:25 +080070import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Taesu Lee39e4fbb2019-07-22 19:20:05 +090071import com.android.settingslib.development.DevelopmentSettingsEnabler;
Pasty Changc1f86002018-12-11 02:22:55 +000072
pastychang9bdb59a2019-01-21 09:49:15 +080073import com.google.android.setupcompat.template.FooterBarMixin;
74import com.google.android.setupcompat.template.FooterButton;
75import com.google.android.setupcompat.template.FooterButton.ButtonType;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000076import com.google.android.setupdesign.GlifLayout;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010078import java.util.List;
79
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080/**
81 * Confirm and execute a reset of the device to a clean "just out of the box"
82 * state. Multiple confirmations are required: first, a general "are you sure
83 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
84 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
85 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
86 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070087 *
88 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 */
Edgar Wang21f8cb32020-08-04 23:13:05 +080090public class MainClear extends InstrumentedFragment implements OnGlobalLayoutListener {
91 private static final String TAG = "MainClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092
felkachanga6cec472018-03-29 12:08:20 +080093 @VisibleForTesting
94 static final int KEYGUARD_REQUEST = 55;
95 @VisibleForTesting
96 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097
Edgar Wang21f8cb32020-08-04 23:13:05 +080098 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX =
99 "masterclear.allow_retain_esim_profiles_after_fdr";
Qingxi Lic8a2b042018-01-11 11:35:07 -0800100
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700101 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -0700102 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800103
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700104 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +0800105 @VisibleForTesting
pastychang103409b2018-12-13 23:00:17 +0800106 FooterButton mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700107 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800108 @VisibleForTesting
109 CheckBox mExternalStorage;
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800110 @VisibleForTesting
111 View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800112 @VisibleForTesting
113 CheckBox mEsimStorage;
114 @VisibleForTesting
115 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800116
Doris Ling1f497152018-02-22 16:33:07 -0800117 @Override
118 public void onGlobalLayout() {
119 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
120 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800121
felkachanga6cec472018-03-29 12:08:20 +0800122 private void setUpActionBarAndTitle() {
123 final Activity activity = getActivity();
124 if (activity == null) {
125 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
126 return;
127 }
128 final ActionBar actionBar = activity.getActionBar();
129 if (actionBar == null) {
130 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
131 return;
132 }
133 actionBar.hide();
134 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700135 }
136
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800137 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700138 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800140 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700141 * @param request the request code to be returned once confirmation finishes
142 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800143 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700144 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700145 Resources res = getActivity().getResources();
Kevin Chynb13bc502020-07-20 23:35:21 -0700146 final ChooseLockSettingsHelper.Builder builder =
147 new ChooseLockSettingsHelper.Builder(getActivity(), this);
148 return builder.setRequestCode(request)
Edgar Wang21f8cb32020-08-04 23:13:05 +0800149 .setTitle(res.getText(R.string.main_clear_short_title))
Kevin Chynb13bc502020-07-20 23:35:21 -0700150 .show();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800151 }
152
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800153 @VisibleForTesting
154 boolean isValidRequestCode(int requestCode) {
155 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
156 }
157
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800158 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700159 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800160 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800161 onActivityResultInternal(requestCode, resultCode, data);
162 }
163
164 /*
165 * Internal method that allows easy testing without dealing with super references.
166 */
167 @VisibleForTesting
168 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800169 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800170 return;
171 }
172
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800173 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800175 return;
176 }
177
178 Intent intent = null;
179 // If returning from a Keyguard request, try to show an account confirmation request if
180 // applciable.
181 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
182 && (intent = getAccountConfirmationIntent()) != null) {
183 showAccountCredentialConfirmation(intent);
184 } else {
185 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800186 }
187 }
188
qingxi072f2862017-04-11 18:28:40 -0700189 @VisibleForTesting
190 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800191 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700192 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Antony Sargent8d2baf62019-07-11 11:04:09 -0700193 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Mill Chen1e607902021-02-25 15:03:25 +0800194 final Intent intent = new Intent();
195 intent.setClass(getContext(),
196 com.android.settings.Settings.FactoryResetConfirmActivity.class);
197 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, MainClearConfirm.class.getName());
198 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
199 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID,
200 R.string.main_clear_confirm_title);
201 intent.putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, getMetricsCategory());
202 getContext().startActivity(intent);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700203 }
204
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800205 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800206 void showAccountCredentialConfirmation(Intent intent) {
207 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
208 }
209
210 @VisibleForTesting
211 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800212 final Context context = getActivity();
213 final String accountType = context.getString(R.string.account_type);
214 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800215 final String className = context.getString(R.string.account_confirmation_class);
216 if (TextUtils.isEmpty(accountType)
217 || TextUtils.isEmpty(packageName)
218 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800219 Log.i(TAG, "Resources not set for account confirmation.");
220 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800221 }
222 final AccountManager am = AccountManager.get(context);
223 Account[] accounts = am.getAccountsByType(accountType);
224 if (accounts != null && accounts.length > 0) {
225 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800226 .setPackage(packageName)
227 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800228 // Check to make sure that the intent is supported.
229 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800230 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
231 if (resolution != null
232 && resolution.activityInfo != null
233 && packageName.equals(resolution.activityInfo.packageName)) {
234 // Note that we need to check the packagename to make sure that an Activity resolver
235 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800236 return requestAccountConfirmation;
237 } else {
238 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800239 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800240 } else {
241 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800242 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800243 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800244 }
245
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800246 /**
247 * If the user clicks to begin the reset sequence, we next require a
248 * keyguard confirmation if the user has currently enabled one. If there
249 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700250 *
251 * 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 -0800252 */
Christine Franksa09eb382017-06-23 14:28:21 -0700253 @VisibleForTesting
254 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700255
Christine Franksa09eb382017-06-23 14:28:21 -0700256 public void onClick(View view) {
257 final Context context = view.getContext();
258 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700259 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
260 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700261 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700262 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700263 .setAction(Intent.ACTION_FACTORY_RESET);
264 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800265 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800266 return;
267 }
268
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800269 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
270 return;
271 }
272
273 Intent intent = getAccountConfirmationIntent();
274 if (intent != null) {
275 showAccountCredentialConfirmation(intent);
276 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700277 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800278 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800279 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700280 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800281
282 /**
283 * In its initial state, the activity presents a button for the user to
284 * click in order to initiate a confirmation sequence. This method is
285 * called from various other points in the code to reset the activity to
286 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800287 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800288 * <p>Reinflating views from resources is expensive and prevents us from
289 * caching widget pointers, so we use a single-inflate pattern: we lazy-
290 * inflate each view, caching all of the widget pointers we'll need at the
291 * time, then simply reuse the inflated views directly whenever we need
292 * to change contents.
293 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800294 @VisibleForTesting
295 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800296 setUpActionBarAndTitle();
297 setUpInitiateButton();
298
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700299 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800300 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800301 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800302 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
303 if (mScrollView != null) {
304 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
305 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800306 mScrollView = mContentView.findViewById(R.id.main_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800307
Kenny Root3785e392011-01-18 15:14:32 -0800308 /*
309 * If the external storage is emulated, it will be erased with a factory
310 * reset at any rate. There is no need to have a separate option until
311 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700312 * others. Likewise, if it's non-removable storage, it could potentially have been
313 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800314 */
Ben Komalo2a321922011-09-07 16:42:34 -0700315 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
316 if (isExtStorageEmulated
317 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800318 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700319
Kenny Root3785e392011-01-18 15:14:32 -0800320 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
321 externalOption.setVisibility(View.GONE);
322
323 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
324 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700325
326 // If it's not emulated, it is on a separate partition but it means we're doing
327 // a force wipe due to encryption.
328 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800329 } else {
330 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
331
332 @Override
333 public void onClick(View v) {
334 mExternalStorage.toggle();
335 }
336 });
337 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800338
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700339 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800340 if (showWipeEuiccCheckbox()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800341 mEsimStorageContainer.setVisibility(View.VISIBLE);
342 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
343 @Override
344 public void onClick(View v) {
345 mEsimStorage.toggle();
346 }
347 });
348 } else {
349 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
350 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700351
Qingxi Lic8a2b042018-01-11 11:35:07 -0800352 final View noCancelMobilePlan = mContentView.findViewById(
353 R.id.no_cancel_mobile_plan);
354 noCancelMobilePlan.setVisibility(View.VISIBLE);
355 mEsimStorage.setChecked(true /* checked */);
356 }
Edgar Wang514d7462020-03-25 16:17:40 +0800357 } else {
358 mEsimStorage.setChecked(false /* checked */);
qingxi072f2862017-04-11 18:28:40 -0700359 }
360
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100361 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
362 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800363 final StringBuffer contentDescription = new StringBuffer();
Edgar Wang21f8cb32020-08-04 23:13:05 +0800364 final View mainClearContainer = mContentView.findViewById(R.id.main_clear_container);
365 getContentDescription(mainClearContainer, contentDescription);
366 mainClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800367
368 // Set the status of initiateButton based on scrollview
369 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
370 @Override
371 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800372 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800373 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
374 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800375 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800376 }
377 }
378 });
379
380 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800381 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800382 }
383
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700384 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700385 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700386 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700387 * <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 -0700388 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700389 */
390 @VisibleForTesting
391 boolean showWipeEuicc() {
392 Context context = getContext();
393 if (!isEuiccEnabled(context)) {
394 return false;
395 }
396 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700397 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
Taesu Lee39e4fbb2019-07-22 19:20:05 +0900398 || DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context);
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700399 }
400
401 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800402 boolean showWipeEuiccCheckbox() {
403 return SystemProperties
404 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
405 }
406
407 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700408 protected boolean isEuiccEnabled(Context context) {
409 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
410 return euiccManager.isEnabled();
411 }
412
jackqdyulei0b3edc72016-12-13 17:07:08 -0800413 @VisibleForTesting
414 boolean hasReachedBottom(final ScrollView scrollView) {
415 if (scrollView.getChildCount() < 1) {
416 return true;
417 }
418
419 final View view = scrollView.getChildAt(0);
420 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
421
422 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400423 }
424
felkachanga6cec472018-03-29 12:08:20 +0800425 private void setUpInitiateButton() {
426 if (mInitiateButton != null) {
427 return;
428 }
429
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000430 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
pastychang9bdb59a2019-01-21 09:49:15 +0800431 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
432 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800433 new FooterButton.Builder(getActivity())
Edgar Wang21f8cb32020-08-04 23:13:05 +0800434 .setText(R.string.main_clear_button_text)
pastychang908501e2018-12-26 10:46:40 +0800435 .setListener(mInitiateListener)
436 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800437 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800438 .build()
pastychang103409b2018-12-13 23:00:17 +0800439 );
pastychang9bdb59a2019-01-21 09:49:15 +0800440 mInitiateButton = mixin.getPrimaryButton();
felkachanga6cec472018-03-29 12:08:20 +0800441 }
442
Julia Reynoldsce25af42015-07-08 16:56:31 -0400443 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800444 if (v.getVisibility() != View.VISIBLE) {
445 return;
446 }
447 if (v instanceof ViewGroup) {
448 ViewGroup vGroup = (ViewGroup) v;
449 for (int i = 0; i < vGroup.getChildCount(); i++) {
450 View nextChild = vGroup.getChildAt(i);
451 getContentDescription(nextChild, description);
452 }
453 } else if (v instanceof TextView) {
454 TextView vText = (TextView) v;
455 description.append(vText.getText());
456 description.append(","); // Allow Talkback to pause between sections.
457 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800458 }
459
Ben Komalo2a321922011-09-07 16:42:34 -0700460 private boolean isExtStorageEncrypted() {
Inseob Kima1aeeb22018-11-08 12:44:47 +0900461 String state = VoldProperties.decrypt().orElse("");
Ben Komalo2a321922011-09-07 16:42:34 -0700462 return !"".equals(state);
463 }
464
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100465 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800466 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
felkachanga6cec472018-03-29 12:08:20 +0800467 LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700468 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800469
470 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100471 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
472 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800473
474 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800475
felkachanga6cec472018-03-29 12:08:20 +0800476 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Joe Onoratob51886d2010-11-08 18:25:51 -0800477 Context.LAYOUT_INFLATER_SERVICE);
478
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100479 int accountsCount = 0;
480 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
481 final UserInfo userInfo = profiles.get(profileIndex);
482 final int profileId = userInfo.id;
483 final UserHandle userHandle = new UserHandle(profileId);
484 Account[] accounts = mgr.getAccountsAsUser(profileId);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800485 final int accountLength = accounts.length;
486 if (accountLength == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800487 continue;
488 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800489 accountsCount += accountLength;
Joe Onoratob51886d2010-11-08 18:25:51 -0800490
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100491 AuthenticatorDescription[] descs = AccountManager.get(context)
492 .getAuthenticatorTypesAsUser(profileId);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800493 final int descLength = descs.length;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100494
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200495 if (profilesSize > 1) {
496 View titleView = Utils.inflateCategoryHeader(inflater, contents);
497 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
Jonathan Scotte0d43942022-01-19 15:19:08 +0000498
499 DevicePolicyManager devicePolicyManager =
500 context.getSystemService(DevicePolicyManager.class);
501
502 if (userInfo.isManagedProfile()) {
kholoud mohamedde781492022-03-14 11:11:33 +0000503 titleText.setText(devicePolicyManager.getResources().getString(
Jonathan Scotte0d43942022-01-19 15:19:08 +0000504 WORK_CATEGORY_HEADER, () -> getString(R.string.category_work)));
505 } else {
kholoud mohamedde781492022-03-14 11:11:33 +0000506 titleText.setText(devicePolicyManager.getResources().getString(
Jonathan Scotte0d43942022-01-19 15:19:08 +0000507 PERSONAL_CATEGORY_HEADER, () -> getString(R.string.category_personal)));
508 }
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200509 contents.addView(titleView);
510 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100511
Edgar Wang21f8cb32020-08-04 23:13:05 +0800512 for (int i = 0; i < accountLength; i++) {
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100513 Account account = accounts[i];
514 AuthenticatorDescription desc = null;
Edgar Wang21f8cb32020-08-04 23:13:05 +0800515 for (int j = 0; j < descLength; j++) {
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100516 if (account.type.equals(descs[j].type)) {
517 desc = descs[j];
518 break;
519 }
520 }
521 if (desc == null) {
522 Log.w(TAG, "No descriptor for account name=" + account.name
523 + " type=" + account.type);
524 continue;
525 }
526 Drawable icon = null;
527 try {
528 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000529 Context authContext = context.createPackageContextAsUser(desc.packageName,
530 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100531 icon = context.getPackageManager().getUserBadgedIcon(
532 authContext.getDrawable(desc.iconId), userHandle);
533 }
534 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100535 Log.w(TAG, "Bad package name for account type " + desc.type);
536 } catch (Resources.NotFoundException e) {
537 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
538 }
539 if (icon == null) {
540 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100541 }
542
Edgar Wang21f8cb32020-08-04 23:13:05 +0800543 View child = inflater.inflate(R.layout.main_clear_account, contents, false);
Fan Zhang6b2bb392016-09-28 09:07:44 -0700544 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
545 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100546 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800547 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800548 }
549
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100550 if (accountsCount > 0) {
551 accountsLabel.setVisibility(View.VISIBLE);
552 contents.setVisibility(View.VISIBLE);
553 }
554 // Checking for all other users and their profiles if any.
555 View otherUsers = mContentView.findViewById(R.id.other_users_present);
556 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
557 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800558 }
559
560 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700561 public View onCreateView(LayoutInflater inflater, ViewGroup container,
562 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800563 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700564 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800565 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
566 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700567 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
568 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
569 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700570 if (disallow && !Utils.isDemoUser(context)) {
Edgar Wang21f8cb32020-08-04 23:13:05 +0800571 return inflater.inflate(R.layout.main_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000572 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100573 new ActionDisabledByAdminDialogHelper(getActivity())
574 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
575 .setOnDismissListener(__ -> getActivity().finish())
576 .show();
577 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400578 }
579
Edgar Wang21f8cb32020-08-04 23:13:05 +0800580 mContentView = inflater.inflate(R.layout.main_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800581
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800582 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700583 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800584 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400585
586 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700587 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800588 return SettingsEnums.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400589 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800590}