blob: a7aa8eacf92cb06bda05779712982616e4266f27 [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
Maurice Lam2eb170c2017-04-28 16:18:47 -070019import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20
Joe Onoratob51886d2010-11-08 18:25:51 -080021import android.accounts.Account;
22import android.accounts.AccountManager;
23import android.accounts.AuthenticatorDescription;
felkachanga6cec472018-03-29 12:08:20 +080024import android.app.ActionBar;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.app.Activity;
Fan Zhang31b21002019-01-16 13:49:47 -080026import android.app.settings.SettingsEnums;
Christine Franksa09eb382017-06-23 14:28:21 -070027import android.content.ComponentName;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070028import android.content.ContentResolver;
Joe Onoratob51886d2010-11-08 18:25:51 -080029import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080030import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080031import android.content.pm.PackageManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080032import android.content.pm.ResolveInfo;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010033import android.content.pm.UserInfo;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070034import android.content.res.Resources;
felkachanga6cec472018-03-29 12:08:20 +080035import android.graphics.Color;
Joe Onoratob51886d2010-11-08 18:25:51 -080036import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080038import android.os.Environment;
Ben Komalo2a321922011-09-07 16:42:34 -070039import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010040import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070041import android.os.UserManager;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070042import android.provider.Settings;
Inseob Kima1aeeb22018-11-08 12:44:47 +090043import android.sysprop.VoldProperties;
qingxi072f2862017-04-11 18:28:40 -070044import android.telephony.euicc.EuiccManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080045import android.text.TextUtils;
Joe Onoratob51886d2010-11-08 18:25:51 -080046import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047import android.view.LayoutInflater;
48import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080049import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070050import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080051import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070053import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070054import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080055import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080056import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080057import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040058
Fan Zhang23f8d592018-08-28 15:11:40 -070059import androidx.annotation.VisibleForTesting;
60
Doris Ling72489722017-11-16 11:03:40 -080061import com.android.settings.core.InstrumentedFragment;
arangelove35badd2017-10-27 13:51:28 +010062import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Maurice Lam2eb170c2017-04-28 16:18:47 -070063import com.android.settings.password.ChooseLockSettingsHelper;
64import com.android.settings.password.ConfirmLockPattern;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070065import com.android.settingslib.RestrictedLockUtilsInternal;
Mill Chen1e607902021-02-25 15:03:25 +080066import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
Taesu Lee39e4fbb2019-07-22 19:20:05 +090067import com.android.settingslib.development.DevelopmentSettingsEnabler;
Pasty Changc1f86002018-12-11 02:22:55 +000068
pastychang9bdb59a2019-01-21 09:49:15 +080069import com.google.android.setupcompat.template.FooterBarMixin;
70import com.google.android.setupcompat.template.FooterButton;
71import com.google.android.setupcompat.template.FooterButton.ButtonType;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000072import com.google.android.setupdesign.GlifLayout;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010074import java.util.List;
75
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076/**
77 * Confirm and execute a reset of the device to a clean "just out of the box"
78 * state. Multiple confirmations are required: first, a general "are you sure
79 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
80 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
81 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
82 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070083 *
84 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080085 */
Edgar Wang21f8cb32020-08-04 23:13:05 +080086public class MainClear extends InstrumentedFragment implements OnGlobalLayoutListener {
87 private static final String TAG = "MainClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088
felkachanga6cec472018-03-29 12:08:20 +080089 @VisibleForTesting
90 static final int KEYGUARD_REQUEST = 55;
91 @VisibleForTesting
92 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093
Edgar Wang21f8cb32020-08-04 23:13:05 +080094 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX =
95 "masterclear.allow_retain_esim_profiles_after_fdr";
Qingxi Lic8a2b042018-01-11 11:35:07 -080096
Amith Yamasanib14e1e02010-11-02 09:52:29 -070097 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -070098 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080099
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700100 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +0800101 @VisibleForTesting
pastychang103409b2018-12-13 23:00:17 +0800102 FooterButton mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700103 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800104 @VisibleForTesting
105 CheckBox mExternalStorage;
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800106 @VisibleForTesting
107 View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800108 @VisibleForTesting
109 CheckBox mEsimStorage;
110 @VisibleForTesting
111 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800112
Doris Ling1f497152018-02-22 16:33:07 -0800113 @Override
114 public void onGlobalLayout() {
115 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
116 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800117
felkachanga6cec472018-03-29 12:08:20 +0800118 private void setUpActionBarAndTitle() {
119 final Activity activity = getActivity();
120 if (activity == null) {
121 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
122 return;
123 }
124 final ActionBar actionBar = activity.getActionBar();
125 if (actionBar == null) {
126 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
127 return;
128 }
129 actionBar.hide();
130 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700131 }
132
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700134 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800136 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700137 * @param request the request code to be returned once confirmation finishes
138 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700140 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700141 Resources res = getActivity().getResources();
Kevin Chynb13bc502020-07-20 23:35:21 -0700142 final ChooseLockSettingsHelper.Builder builder =
143 new ChooseLockSettingsHelper.Builder(getActivity(), this);
144 return builder.setRequestCode(request)
Edgar Wang21f8cb32020-08-04 23:13:05 +0800145 .setTitle(res.getText(R.string.main_clear_short_title))
Kevin Chynb13bc502020-07-20 23:35:21 -0700146 .show();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800147 }
148
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800149 @VisibleForTesting
150 boolean isValidRequestCode(int requestCode) {
151 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
152 }
153
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800154 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700155 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800156 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800157 onActivityResultInternal(requestCode, resultCode, data);
158 }
159
160 /*
161 * Internal method that allows easy testing without dealing with super references.
162 */
163 @VisibleForTesting
164 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800165 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800166 return;
167 }
168
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800169 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800170 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800171 return;
172 }
173
174 Intent intent = null;
175 // If returning from a Keyguard request, try to show an account confirmation request if
176 // applciable.
177 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
178 && (intent = getAccountConfirmationIntent()) != null) {
179 showAccountCredentialConfirmation(intent);
180 } else {
181 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182 }
183 }
184
qingxi072f2862017-04-11 18:28:40 -0700185 @VisibleForTesting
186 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800187 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700188 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Antony Sargent8d2baf62019-07-11 11:04:09 -0700189 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Mill Chen1e607902021-02-25 15:03:25 +0800190 final Intent intent = new Intent();
191 intent.setClass(getContext(),
192 com.android.settings.Settings.FactoryResetConfirmActivity.class);
193 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, MainClearConfirm.class.getName());
194 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
195 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID,
196 R.string.main_clear_confirm_title);
197 intent.putExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, getMetricsCategory());
198 getContext().startActivity(intent);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700199 }
200
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800201 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800202 void showAccountCredentialConfirmation(Intent intent) {
203 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
204 }
205
206 @VisibleForTesting
207 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800208 final Context context = getActivity();
209 final String accountType = context.getString(R.string.account_type);
210 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800211 final String className = context.getString(R.string.account_confirmation_class);
212 if (TextUtils.isEmpty(accountType)
213 || TextUtils.isEmpty(packageName)
214 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800215 Log.i(TAG, "Resources not set for account confirmation.");
216 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800217 }
218 final AccountManager am = AccountManager.get(context);
219 Account[] accounts = am.getAccountsByType(accountType);
220 if (accounts != null && accounts.length > 0) {
221 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800222 .setPackage(packageName)
223 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800224 // Check to make sure that the intent is supported.
225 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800226 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
227 if (resolution != null
228 && resolution.activityInfo != null
229 && packageName.equals(resolution.activityInfo.packageName)) {
230 // Note that we need to check the packagename to make sure that an Activity resolver
231 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800232 return requestAccountConfirmation;
233 } else {
234 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800235 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800236 } else {
237 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800238 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800239 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800240 }
241
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800242 /**
243 * If the user clicks to begin the reset sequence, we next require a
244 * keyguard confirmation if the user has currently enabled one. If there
245 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700246 *
247 * 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 -0800248 */
Christine Franksa09eb382017-06-23 14:28:21 -0700249 @VisibleForTesting
250 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700251
Christine Franksa09eb382017-06-23 14:28:21 -0700252 public void onClick(View view) {
253 final Context context = view.getContext();
254 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700255 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
256 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700257 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700258 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700259 .setAction(Intent.ACTION_FACTORY_RESET);
260 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800261 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800262 return;
263 }
264
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800265 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
266 return;
267 }
268
269 Intent intent = getAccountConfirmationIntent();
270 if (intent != null) {
271 showAccountCredentialConfirmation(intent);
272 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700273 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800274 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800275 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700276 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800277
278 /**
279 * In its initial state, the activity presents a button for the user to
280 * click in order to initiate a confirmation sequence. This method is
281 * called from various other points in the code to reset the activity to
282 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800283 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800284 * <p>Reinflating views from resources is expensive and prevents us from
285 * caching widget pointers, so we use a single-inflate pattern: we lazy-
286 * inflate each view, caching all of the widget pointers we'll need at the
287 * time, then simply reuse the inflated views directly whenever we need
288 * to change contents.
289 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800290 @VisibleForTesting
291 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800292 setUpActionBarAndTitle();
293 setUpInitiateButton();
294
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700295 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800296 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800297 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800298 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
299 if (mScrollView != null) {
300 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
301 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800302 mScrollView = mContentView.findViewById(R.id.main_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800303
Kenny Root3785e392011-01-18 15:14:32 -0800304 /*
305 * If the external storage is emulated, it will be erased with a factory
306 * reset at any rate. There is no need to have a separate option until
307 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700308 * others. Likewise, if it's non-removable storage, it could potentially have been
309 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800310 */
Ben Komalo2a321922011-09-07 16:42:34 -0700311 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
312 if (isExtStorageEmulated
313 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
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
322 // If it's not emulated, it is on a separate partition but it means we're doing
323 // a force wipe due to encryption.
324 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800325 } else {
326 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
327
328 @Override
329 public void onClick(View v) {
330 mExternalStorage.toggle();
331 }
332 });
333 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800334
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700335 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800336 if (showWipeEuiccCheckbox()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800337 mEsimStorageContainer.setVisibility(View.VISIBLE);
338 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
339 @Override
340 public void onClick(View v) {
341 mEsimStorage.toggle();
342 }
343 });
344 } else {
345 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
346 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700347
Qingxi Lic8a2b042018-01-11 11:35:07 -0800348 final View noCancelMobilePlan = mContentView.findViewById(
349 R.id.no_cancel_mobile_plan);
350 noCancelMobilePlan.setVisibility(View.VISIBLE);
351 mEsimStorage.setChecked(true /* checked */);
352 }
Edgar Wang514d7462020-03-25 16:17:40 +0800353 } else {
354 mEsimStorage.setChecked(false /* checked */);
qingxi072f2862017-04-11 18:28:40 -0700355 }
356
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100357 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
358 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800359 final StringBuffer contentDescription = new StringBuffer();
Edgar Wang21f8cb32020-08-04 23:13:05 +0800360 final View mainClearContainer = mContentView.findViewById(R.id.main_clear_container);
361 getContentDescription(mainClearContainer, contentDescription);
362 mainClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800363
364 // Set the status of initiateButton based on scrollview
365 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
366 @Override
367 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800368 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800369 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
370 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800371 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800372 }
373 }
374 });
375
376 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800377 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800378 }
379
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700380 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700381 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700382 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700383 * <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 -0700384 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700385 */
386 @VisibleForTesting
387 boolean showWipeEuicc() {
388 Context context = getContext();
389 if (!isEuiccEnabled(context)) {
390 return false;
391 }
392 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700393 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
Taesu Lee39e4fbb2019-07-22 19:20:05 +0900394 || DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context);
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700395 }
396
397 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800398 boolean showWipeEuiccCheckbox() {
399 return SystemProperties
400 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
401 }
402
403 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700404 protected boolean isEuiccEnabled(Context context) {
405 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
406 return euiccManager.isEnabled();
407 }
408
jackqdyulei0b3edc72016-12-13 17:07:08 -0800409 @VisibleForTesting
410 boolean hasReachedBottom(final ScrollView scrollView) {
411 if (scrollView.getChildCount() < 1) {
412 return true;
413 }
414
415 final View view = scrollView.getChildAt(0);
416 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
417
418 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400419 }
420
felkachanga6cec472018-03-29 12:08:20 +0800421 private void setUpInitiateButton() {
422 if (mInitiateButton != null) {
423 return;
424 }
425
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000426 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
pastychang9bdb59a2019-01-21 09:49:15 +0800427 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
428 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800429 new FooterButton.Builder(getActivity())
Edgar Wang21f8cb32020-08-04 23:13:05 +0800430 .setText(R.string.main_clear_button_text)
pastychang908501e2018-12-26 10:46:40 +0800431 .setListener(mInitiateListener)
432 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800433 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800434 .build()
pastychang103409b2018-12-13 23:00:17 +0800435 );
pastychang9bdb59a2019-01-21 09:49:15 +0800436 mInitiateButton = mixin.getPrimaryButton();
felkachanga6cec472018-03-29 12:08:20 +0800437 }
438
Julia Reynoldsce25af42015-07-08 16:56:31 -0400439 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800440 if (v.getVisibility() != View.VISIBLE) {
441 return;
442 }
443 if (v instanceof ViewGroup) {
444 ViewGroup vGroup = (ViewGroup) v;
445 for (int i = 0; i < vGroup.getChildCount(); i++) {
446 View nextChild = vGroup.getChildAt(i);
447 getContentDescription(nextChild, description);
448 }
449 } else if (v instanceof TextView) {
450 TextView vText = (TextView) v;
451 description.append(vText.getText());
452 description.append(","); // Allow Talkback to pause between sections.
453 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800454 }
455
Ben Komalo2a321922011-09-07 16:42:34 -0700456 private boolean isExtStorageEncrypted() {
Inseob Kima1aeeb22018-11-08 12:44:47 +0900457 String state = VoldProperties.decrypt().orElse("");
Ben Komalo2a321922011-09-07 16:42:34 -0700458 return !"".equals(state);
459 }
460
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100461 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800462 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
felkachanga6cec472018-03-29 12:08:20 +0800463 LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700464 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800465
466 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100467 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
468 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800469
470 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800471
felkachanga6cec472018-03-29 12:08:20 +0800472 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Joe Onoratob51886d2010-11-08 18:25:51 -0800473 Context.LAYOUT_INFLATER_SERVICE);
474
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100475 int accountsCount = 0;
476 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
477 final UserInfo userInfo = profiles.get(profileIndex);
478 final int profileId = userInfo.id;
479 final UserHandle userHandle = new UserHandle(profileId);
480 Account[] accounts = mgr.getAccountsAsUser(profileId);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800481 final int accountLength = accounts.length;
482 if (accountLength == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800483 continue;
484 }
Edgar Wang21f8cb32020-08-04 23:13:05 +0800485 accountsCount += accountLength;
Joe Onoratob51886d2010-11-08 18:25:51 -0800486
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100487 AuthenticatorDescription[] descs = AccountManager.get(context)
488 .getAuthenticatorTypesAsUser(profileId);
Edgar Wang21f8cb32020-08-04 23:13:05 +0800489 final int descLength = descs.length;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100490
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200491 if (profilesSize > 1) {
492 View titleView = Utils.inflateCategoryHeader(inflater, contents);
493 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
494 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
495 : R.string.category_personal);
496 contents.addView(titleView);
497 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100498
Edgar Wang21f8cb32020-08-04 23:13:05 +0800499 for (int i = 0; i < accountLength; i++) {
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100500 Account account = accounts[i];
501 AuthenticatorDescription desc = null;
Edgar Wang21f8cb32020-08-04 23:13:05 +0800502 for (int j = 0; j < descLength; j++) {
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100503 if (account.type.equals(descs[j].type)) {
504 desc = descs[j];
505 break;
506 }
507 }
508 if (desc == null) {
509 Log.w(TAG, "No descriptor for account name=" + account.name
510 + " type=" + account.type);
511 continue;
512 }
513 Drawable icon = null;
514 try {
515 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000516 Context authContext = context.createPackageContextAsUser(desc.packageName,
517 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100518 icon = context.getPackageManager().getUserBadgedIcon(
519 authContext.getDrawable(desc.iconId), userHandle);
520 }
521 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100522 Log.w(TAG, "Bad package name for account type " + desc.type);
523 } catch (Resources.NotFoundException e) {
524 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
525 }
526 if (icon == null) {
527 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100528 }
529
Edgar Wang21f8cb32020-08-04 23:13:05 +0800530 View child = inflater.inflate(R.layout.main_clear_account, contents, false);
Fan Zhang6b2bb392016-09-28 09:07:44 -0700531 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
532 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100533 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800534 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800535 }
536
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100537 if (accountsCount > 0) {
538 accountsLabel.setVisibility(View.VISIBLE);
539 contents.setVisibility(View.VISIBLE);
540 }
541 // Checking for all other users and their profiles if any.
542 View otherUsers = mContentView.findViewById(R.id.other_users_present);
543 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
544 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800545 }
546
547 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700548 public View onCreateView(LayoutInflater inflater, ViewGroup container,
549 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800550 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700551 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800552 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
553 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700554 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
555 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
556 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700557 if (disallow && !Utils.isDemoUser(context)) {
Edgar Wang21f8cb32020-08-04 23:13:05 +0800558 return inflater.inflate(R.layout.main_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000559 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100560 new ActionDisabledByAdminDialogHelper(getActivity())
561 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
562 .setOnDismissListener(__ -> getActivity().finish())
563 .show();
564 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400565 }
566
Edgar Wang21f8cb32020-08-04 23:13:05 +0800567 mContentView = inflater.inflate(R.layout.main_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800568
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800569 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700570 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800571 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400572
573 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700574 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800575 return SettingsEnums.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400576 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800577}