blob: 14a6aed9e7997a7a7fbf1846fc7d30f3b157c578 [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;
Fan Zhang7cf99f52018-02-16 10:37:37 -080062import com.android.settings.core.SubSettingLauncher;
arangelove35badd2017-10-27 13:51:28 +010063import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Maurice Lam2eb170c2017-04-28 16:18:47 -070064import com.android.settings.password.ChooseLockSettingsHelper;
65import com.android.settings.password.ConfirmLockPattern;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070066import com.android.settingslib.RestrictedLockUtilsInternal;
Pasty Changc1f86002018-12-11 02:22:55 +000067
pastychang9bdb59a2019-01-21 09:49:15 +080068import com.google.android.setupcompat.template.FooterBarMixin;
69import com.google.android.setupcompat.template.FooterButton;
70import com.google.android.setupcompat.template.FooterButton.ButtonType;
Maurice Lam59f1c1a2019-02-14 22:04:30 +000071import com.google.android.setupdesign.GlifLayout;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010073import java.util.List;
74
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080075/**
76 * Confirm and execute a reset of the device to a clean "just out of the box"
77 * state. Multiple confirmations are required: first, a general "are you sure
78 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
79 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
80 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
81 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070082 *
83 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 */
Doris Ling1f497152018-02-22 16:33:07 -080085public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener {
Joe Onoratob51886d2010-11-08 18:25:51 -080086 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080087
felkachanga6cec472018-03-29 12:08:20 +080088 @VisibleForTesting
89 static final int KEYGUARD_REQUEST = 55;
90 @VisibleForTesting
91 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092
Qingxi Lic8a2b042018-01-11 11:35:07 -080093 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX
94 = "masterclear.allow_retain_esim_profiles_after_fdr";
95
Amith Yamasanib14e1e02010-11-02 09:52:29 -070096 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -070097 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098
Amith Yamasanib14e1e02010-11-02 09:52:29 -070099 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +0800100 @VisibleForTesting
pastychang103409b2018-12-13 23:00:17 +0800101 FooterButton mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700102 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800103 @VisibleForTesting
104 CheckBox mExternalStorage;
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800105 @VisibleForTesting
106 View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800107 @VisibleForTesting
108 CheckBox mEsimStorage;
109 @VisibleForTesting
110 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800111
Doris Ling1f497152018-02-22 16:33:07 -0800112 @Override
113 public void onGlobalLayout() {
114 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
115 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800116
felkachanga6cec472018-03-29 12:08:20 +0800117 private void setUpActionBarAndTitle() {
118 final Activity activity = getActivity();
119 if (activity == null) {
120 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
121 return;
122 }
123 final ActionBar actionBar = activity.getActionBar();
124 if (actionBar == null) {
125 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
126 return;
127 }
128 actionBar.hide();
129 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700130 }
131
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800132 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700133 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800135 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700136 * @param request the request code to be returned once confirmation finishes
137 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700139 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700140 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700141 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
Fan Zhangdc00efa2018-05-10 15:10:19 -0700142 request, res.getText(R.string.master_clear_short_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800143 }
144
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800145 @VisibleForTesting
146 boolean isValidRequestCode(int requestCode) {
147 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
148 }
149
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700151 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800152 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800153 onActivityResultInternal(requestCode, resultCode, data);
154 }
155
156 /*
157 * Internal method that allows easy testing without dealing with super references.
158 */
159 @VisibleForTesting
160 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800161 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800162 return;
163 }
164
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800165 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800166 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800167 return;
168 }
169
170 Intent intent = null;
171 // If returning from a Keyguard request, try to show an account confirmation request if
172 // applciable.
173 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
174 && (intent = getAccountConfirmationIntent()) != null) {
175 showAccountCredentialConfirmation(intent);
176 } else {
177 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800178 }
179 }
180
qingxi072f2862017-04-11 18:28:40 -0700181 @VisibleForTesting
182 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800183 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700184 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Andrew Sapperstein88930e32019-03-07 14:18:15 -0800185 args.putBoolean(ERASE_ESIMS_EXTRA,
186 mEsimStorageContainer.getVisibility() == View.VISIBLE && mEsimStorage.isChecked());
Fan Zhang7cf99f52018-02-16 10:37:37 -0800187 new SubSettingLauncher(getContext())
188 .setDestination(MasterClearConfirm.class.getName())
189 .setArguments(args)
hjchangliaoe86eec02018-05-02 13:01:07 +0800190 .setTitleRes(R.string.master_clear_confirm_title)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800191 .setSourceMetricsCategory(getMetricsCategory())
192 .launch();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700193 }
194
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800195 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800196 void showAccountCredentialConfirmation(Intent intent) {
197 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
198 }
199
200 @VisibleForTesting
201 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800202 final Context context = getActivity();
203 final String accountType = context.getString(R.string.account_type);
204 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800205 final String className = context.getString(R.string.account_confirmation_class);
206 if (TextUtils.isEmpty(accountType)
207 || TextUtils.isEmpty(packageName)
208 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800209 Log.i(TAG, "Resources not set for account confirmation.");
210 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800211 }
212 final AccountManager am = AccountManager.get(context);
213 Account[] accounts = am.getAccountsByType(accountType);
214 if (accounts != null && accounts.length > 0) {
215 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800216 .setPackage(packageName)
217 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800218 // Check to make sure that the intent is supported.
219 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800220 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
221 if (resolution != null
222 && resolution.activityInfo != null
223 && packageName.equals(resolution.activityInfo.packageName)) {
224 // Note that we need to check the packagename to make sure that an Activity resolver
225 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800226 return requestAccountConfirmation;
227 } else {
228 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800229 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800230 } else {
231 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800232 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800233 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800234 }
235
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800236 /**
237 * If the user clicks to begin the reset sequence, we next require a
238 * keyguard confirmation if the user has currently enabled one. If there
239 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700240 *
241 * 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 -0800242 */
Christine Franksa09eb382017-06-23 14:28:21 -0700243 @VisibleForTesting
244 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700245
Christine Franksa09eb382017-06-23 14:28:21 -0700246 public void onClick(View view) {
247 final Context context = view.getContext();
248 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700249 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
250 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700251 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700252 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700253 .setAction(Intent.ACTION_FACTORY_RESET);
254 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800255 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800256 return;
257 }
258
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800259 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
260 return;
261 }
262
263 Intent intent = getAccountConfirmationIntent();
264 if (intent != null) {
265 showAccountCredentialConfirmation(intent);
266 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700267 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800268 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800269 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700270 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800271
272 /**
273 * In its initial state, the activity presents a button for the user to
274 * click in order to initiate a confirmation sequence. This method is
275 * called from various other points in the code to reset the activity to
276 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800277 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800278 * <p>Reinflating views from resources is expensive and prevents us from
279 * caching widget pointers, so we use a single-inflate pattern: we lazy-
280 * inflate each view, caching all of the widget pointers we'll need at the
281 * time, then simply reuse the inflated views directly whenever we need
282 * to change contents.
283 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800284 @VisibleForTesting
285 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800286 setUpActionBarAndTitle();
287 setUpInitiateButton();
288
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700289 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800290 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800291 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800292 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
293 if (mScrollView != null) {
294 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
295 }
296 mScrollView = mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800297
Kenny Root3785e392011-01-18 15:14:32 -0800298 /*
299 * If the external storage is emulated, it will be erased with a factory
300 * reset at any rate. There is no need to have a separate option until
301 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700302 * others. Likewise, if it's non-removable storage, it could potentially have been
303 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800304 */
Ben Komalo2a321922011-09-07 16:42:34 -0700305 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
306 if (isExtStorageEmulated
307 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800308 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700309
Kenny Root3785e392011-01-18 15:14:32 -0800310 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
311 externalOption.setVisibility(View.GONE);
312
313 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
314 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700315
316 // If it's not emulated, it is on a separate partition but it means we're doing
317 // a force wipe due to encryption.
318 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800319 } else {
320 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
321
322 @Override
323 public void onClick(View v) {
324 mExternalStorage.toggle();
325 }
326 });
327 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800328
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700329 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800330 if (showWipeEuiccCheckbox()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800331 mEsimStorageContainer.setVisibility(View.VISIBLE);
332 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
333 @Override
334 public void onClick(View v) {
335 mEsimStorage.toggle();
336 }
337 });
338 } else {
339 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
340 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700341
Qingxi Lic8a2b042018-01-11 11:35:07 -0800342 final View noCancelMobilePlan = mContentView.findViewById(
343 R.id.no_cancel_mobile_plan);
344 noCancelMobilePlan.setVisibility(View.VISIBLE);
345 mEsimStorage.setChecked(true /* checked */);
346 }
qingxi072f2862017-04-11 18:28:40 -0700347 }
348
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100349 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
350 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800351 final StringBuffer contentDescription = new StringBuffer();
352 final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400353 getContentDescription(masterClearContainer, contentDescription);
354 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800355
356 // Set the status of initiateButton based on scrollview
357 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
358 @Override
359 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800360 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800361 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
362 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800363 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800364 }
365 }
366 });
367
368 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800369 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800370 }
371
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700372 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700373 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700374 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700375 * <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 -0700376 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700377 */
378 @VisibleForTesting
379 boolean showWipeEuicc() {
380 Context context = getContext();
381 if (!isEuiccEnabled(context)) {
382 return false;
383 }
384 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700385 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
felkachanga6cec472018-03-29 12:08:20 +0800386 || Settings.Global.getInt(
387 cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700388 }
389
390 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800391 boolean showWipeEuiccCheckbox() {
392 return SystemProperties
393 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
394 }
395
396 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700397 protected boolean isEuiccEnabled(Context context) {
398 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
399 return euiccManager.isEnabled();
400 }
401
jackqdyulei0b3edc72016-12-13 17:07:08 -0800402 @VisibleForTesting
403 boolean hasReachedBottom(final ScrollView scrollView) {
404 if (scrollView.getChildCount() < 1) {
405 return true;
406 }
407
408 final View view = scrollView.getChildAt(0);
409 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
410
411 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400412 }
413
felkachanga6cec472018-03-29 12:08:20 +0800414 private void setUpInitiateButton() {
415 if (mInitiateButton != null) {
416 return;
417 }
418
Maurice Lam59f1c1a2019-02-14 22:04:30 +0000419 final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
pastychang9bdb59a2019-01-21 09:49:15 +0800420 final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
421 mixin.setPrimaryButton(
pastychang908501e2018-12-26 10:46:40 +0800422 new FooterButton.Builder(getActivity())
423 .setText(R.string.master_clear_button_text)
424 .setListener(mInitiateListener)
425 .setButtonType(ButtonType.OTHER)
pastychang79614822019-01-03 10:12:54 +0800426 .setTheme(R.style.SudGlifButton_Primary)
pastychang908501e2018-12-26 10:46:40 +0800427 .build()
pastychang103409b2018-12-13 23:00:17 +0800428 );
pastychang9bdb59a2019-01-21 09:49:15 +0800429 mInitiateButton = mixin.getPrimaryButton();
felkachanga6cec472018-03-29 12:08:20 +0800430 }
431
Julia Reynoldsce25af42015-07-08 16:56:31 -0400432 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800433 if (v.getVisibility() != View.VISIBLE) {
434 return;
435 }
436 if (v instanceof ViewGroup) {
437 ViewGroup vGroup = (ViewGroup) v;
438 for (int i = 0; i < vGroup.getChildCount(); i++) {
439 View nextChild = vGroup.getChildAt(i);
440 getContentDescription(nextChild, description);
441 }
442 } else if (v instanceof TextView) {
443 TextView vText = (TextView) v;
444 description.append(vText.getText());
445 description.append(","); // Allow Talkback to pause between sections.
446 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800447 }
448
Ben Komalo2a321922011-09-07 16:42:34 -0700449 private boolean isExtStorageEncrypted() {
Inseob Kima1aeeb22018-11-08 12:44:47 +0900450 String state = VoldProperties.decrypt().orElse("");
Ben Komalo2a321922011-09-07 16:42:34 -0700451 return !"".equals(state);
452 }
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);
474 final int N = accounts.length;
475 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800476 continue;
477 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100478 accountsCount += N;
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);
482 final int M = descs.length;
483
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200484 if (profilesSize > 1) {
485 View titleView = Utils.inflateCategoryHeader(inflater, contents);
486 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
487 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
488 : R.string.category_personal);
489 contents.addView(titleView);
490 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100491
492 for (int i = 0; i < N; i++) {
493 Account account = accounts[i];
494 AuthenticatorDescription desc = null;
495 for (int j = 0; j < M; j++) {
496 if (account.type.equals(descs[j].type)) {
497 desc = descs[j];
498 break;
499 }
500 }
501 if (desc == null) {
502 Log.w(TAG, "No descriptor for account name=" + account.name
503 + " type=" + account.type);
504 continue;
505 }
506 Drawable icon = null;
507 try {
508 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000509 Context authContext = context.createPackageContextAsUser(desc.packageName,
510 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100511 icon = context.getPackageManager().getUserBadgedIcon(
512 authContext.getDrawable(desc.iconId), userHandle);
513 }
514 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100515 Log.w(TAG, "Bad package name for account type " + desc.type);
516 } catch (Resources.NotFoundException e) {
517 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
518 }
519 if (icon == null) {
520 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100521 }
522
Fan Zhang6b2bb392016-09-28 09:07:44 -0700523 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
524 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
525 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100526 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800527 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800528 }
529
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100530 if (accountsCount > 0) {
531 accountsLabel.setVisibility(View.VISIBLE);
532 contents.setVisibility(View.VISIBLE);
533 }
534 // Checking for all other users and their profiles if any.
535 View otherUsers = mContentView.findViewById(R.id.other_users_present);
536 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
537 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800538 }
539
540 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700541 public View onCreateView(LayoutInflater inflater, ViewGroup container,
542 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800543 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700544 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800545 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
546 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700547 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
548 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
549 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700550 if (disallow && !Utils.isDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400551 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000552 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100553 new ActionDisabledByAdminDialogHelper(getActivity())
554 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
555 .setOnDismissListener(__ -> getActivity().finish())
556 .show();
557 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400558 }
559
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700560 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800561
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800562 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700563 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800564 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400565
566 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700567 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800568 return SettingsEnums.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400569 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800570}