blob: 8e745a3823f057ce14c3eace753915508ce6e591 [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;
Christine Franksa09eb382017-06-23 14:28:21 -070026import android.content.ComponentName;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070027import android.content.ContentResolver;
Joe Onoratob51886d2010-11-08 18:25:51 -080028import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029import android.content.Intent;
Joe Onoratob51886d2010-11-08 18:25:51 -080030import android.content.pm.PackageManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080031import android.content.pm.ResolveInfo;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010032import android.content.pm.UserInfo;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070033import android.content.res.Resources;
felkachanga6cec472018-03-29 12:08:20 +080034import android.graphics.Color;
Joe Onoratob51886d2010-11-08 18:25:51 -080035import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080037import android.os.Environment;
Ben Komalo2a321922011-09-07 16:42:34 -070038import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010039import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070040import android.os.UserManager;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070041import android.provider.Settings;
Inseob Kima1aeeb22018-11-08 12:44:47 +090042import android.sysprop.VoldProperties;
qingxi072f2862017-04-11 18:28:40 -070043import android.telephony.euicc.EuiccManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080044import android.text.TextUtils;
Joe Onoratob51886d2010-11-08 18:25:51 -080045import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080046import android.view.LayoutInflater;
47import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080048import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070049import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080050import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070052import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070053import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080054import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080055import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080056import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040057
Fan Zhang23f8d592018-08-28 15:11:40 -070058import androidx.annotation.VisibleForTesting;
59
Tamas Berghammer265d3c22016-06-22 15:34:45 +010060import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
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
68import com.google.android.setupcompat.TemplateLayout;
69import com.google.android.setupdesign.template.ButtonFooterMixin;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010071import java.util.List;
72
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080073/**
74 * Confirm and execute a reset of the device to a clean "just out of the box"
75 * state. Multiple confirmations are required: first, a general "are you sure
76 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
77 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
78 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
79 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070080 *
81 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 */
Doris Ling1f497152018-02-22 16:33:07 -080083public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener {
Joe Onoratob51886d2010-11-08 18:25:51 -080084 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080085
felkachanga6cec472018-03-29 12:08:20 +080086 @VisibleForTesting
87 static final int KEYGUARD_REQUEST = 55;
88 @VisibleForTesting
89 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090
Qingxi Lic8a2b042018-01-11 11:35:07 -080091 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX
92 = "masterclear.allow_retain_esim_profiles_after_fdr";
93
Amith Yamasanib14e1e02010-11-02 09:52:29 -070094 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -070095 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096
Amith Yamasanib14e1e02010-11-02 09:52:29 -070097 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +080098 @VisibleForTesting
99 Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -0700100 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800101 @VisibleForTesting
102 CheckBox mExternalStorage;
Qingxi Lic8a2b042018-01-11 11:35:07 -0800103 private View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800104 @VisibleForTesting
105 CheckBox mEsimStorage;
106 @VisibleForTesting
107 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800108
Doris Ling1f497152018-02-22 16:33:07 -0800109 @Override
110 public void onGlobalLayout() {
111 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
112 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800113
felkachanga6cec472018-03-29 12:08:20 +0800114 private void setUpActionBarAndTitle() {
115 final Activity activity = getActivity();
116 if (activity == null) {
117 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
118 return;
119 }
120 final ActionBar actionBar = activity.getActionBar();
121 if (actionBar == null) {
122 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
123 return;
124 }
125 actionBar.hide();
126 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700127 }
128
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700130 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800131 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800132 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700133 * @param request the request code to be returned once confirmation finishes
134 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700136 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700137 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700138 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
Fan Zhangdc00efa2018-05-10 15:10:19 -0700139 request, res.getText(R.string.master_clear_short_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800140 }
141
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800142 @VisibleForTesting
143 boolean isValidRequestCode(int requestCode) {
144 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
145 }
146
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800147 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700148 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800149 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800150 onActivityResultInternal(requestCode, resultCode, data);
151 }
152
153 /*
154 * Internal method that allows easy testing without dealing with super references.
155 */
156 @VisibleForTesting
157 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800158 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800159 return;
160 }
161
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800162 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800163 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800164 return;
165 }
166
167 Intent intent = null;
168 // If returning from a Keyguard request, try to show an account confirmation request if
169 // applciable.
170 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
171 && (intent = getAccountConfirmationIntent()) != null) {
172 showAccountCredentialConfirmation(intent);
173 } else {
174 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800175 }
176 }
177
qingxi072f2862017-04-11 18:28:40 -0700178 @VisibleForTesting
179 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800180 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700181 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Qingxi Lic8a2b042018-01-11 11:35:07 -0800182 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Fan Zhang7cf99f52018-02-16 10:37:37 -0800183 new SubSettingLauncher(getContext())
184 .setDestination(MasterClearConfirm.class.getName())
185 .setArguments(args)
hjchangliaoe86eec02018-05-02 13:01:07 +0800186 .setTitleRes(R.string.master_clear_confirm_title)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800187 .setSourceMetricsCategory(getMetricsCategory())
188 .launch();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700189 }
190
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800191 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800192 void showAccountCredentialConfirmation(Intent intent) {
193 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
194 }
195
196 @VisibleForTesting
197 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800198 final Context context = getActivity();
199 final String accountType = context.getString(R.string.account_type);
200 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800201 final String className = context.getString(R.string.account_confirmation_class);
202 if (TextUtils.isEmpty(accountType)
203 || TextUtils.isEmpty(packageName)
204 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800205 Log.i(TAG, "Resources not set for account confirmation.");
206 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800207 }
208 final AccountManager am = AccountManager.get(context);
209 Account[] accounts = am.getAccountsByType(accountType);
210 if (accounts != null && accounts.length > 0) {
211 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800212 .setPackage(packageName)
213 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800214 // Check to make sure that the intent is supported.
215 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800216 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
217 if (resolution != null
218 && resolution.activityInfo != null
219 && packageName.equals(resolution.activityInfo.packageName)) {
220 // Note that we need to check the packagename to make sure that an Activity resolver
221 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800222 return requestAccountConfirmation;
223 } else {
224 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800225 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800226 } else {
227 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800228 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800229 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800230 }
231
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800232 /**
233 * If the user clicks to begin the reset sequence, we next require a
234 * keyguard confirmation if the user has currently enabled one. If there
235 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700236 *
237 * 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 -0800238 */
Christine Franksa09eb382017-06-23 14:28:21 -0700239 @VisibleForTesting
240 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700241
Christine Franksa09eb382017-06-23 14:28:21 -0700242 public void onClick(View view) {
243 final Context context = view.getContext();
244 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700245 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
246 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700247 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700248 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700249 .setAction(Intent.ACTION_FACTORY_RESET);
250 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800251 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800252 return;
253 }
254
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800255 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
256 return;
257 }
258
259 Intent intent = getAccountConfirmationIntent();
260 if (intent != null) {
261 showAccountCredentialConfirmation(intent);
262 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700263 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800264 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800265 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700266 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800267
268 /**
269 * In its initial state, the activity presents a button for the user to
270 * click in order to initiate a confirmation sequence. This method is
271 * called from various other points in the code to reset the activity to
272 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800273 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800274 * <p>Reinflating views from resources is expensive and prevents us from
275 * caching widget pointers, so we use a single-inflate pattern: we lazy-
276 * inflate each view, caching all of the widget pointers we'll need at the
277 * time, then simply reuse the inflated views directly whenever we need
278 * to change contents.
279 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800280 @VisibleForTesting
281 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800282 setUpActionBarAndTitle();
283 setUpInitiateButton();
284
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700285 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800286 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800287 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800288 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
289 if (mScrollView != null) {
290 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
291 }
292 mScrollView = mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800293
Kenny Root3785e392011-01-18 15:14:32 -0800294 /*
295 * If the external storage is emulated, it will be erased with a factory
296 * reset at any rate. There is no need to have a separate option until
297 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700298 * others. Likewise, if it's non-removable storage, it could potentially have been
299 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800300 */
Ben Komalo2a321922011-09-07 16:42:34 -0700301 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
302 if (isExtStorageEmulated
303 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800304 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700305
Kenny Root3785e392011-01-18 15:14:32 -0800306 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
307 externalOption.setVisibility(View.GONE);
308
309 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
310 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700311
312 // If it's not emulated, it is on a separate partition but it means we're doing
313 // a force wipe due to encryption.
314 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800315 } else {
316 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
317
318 @Override
319 public void onClick(View v) {
320 mExternalStorage.toggle();
321 }
322 });
323 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800324
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700325 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800326 if (showWipeEuiccCheckbox()) {
327 TextView title = mContentView.findViewById(R.id.erase_esim_title);
328 title.setText(R.string.erase_esim_storage);
329 mEsimStorageContainer.setVisibility(View.VISIBLE);
330 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
331 @Override
332 public void onClick(View v) {
333 mEsimStorage.toggle();
334 }
335 });
336 } else {
337 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
338 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700339
Qingxi Lic8a2b042018-01-11 11:35:07 -0800340 final View noCancelMobilePlan = mContentView.findViewById(
341 R.id.no_cancel_mobile_plan);
342 noCancelMobilePlan.setVisibility(View.VISIBLE);
343 mEsimStorage.setChecked(true /* checked */);
344 }
qingxi072f2862017-04-11 18:28:40 -0700345 }
346
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100347 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
348 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800349 final StringBuffer contentDescription = new StringBuffer();
350 final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400351 getContentDescription(masterClearContainer, contentDescription);
352 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800353
354 // Set the status of initiateButton based on scrollview
355 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
356 @Override
357 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800358 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800359 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
360 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800361 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800362 }
363 }
364 });
365
366 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800367 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800368 }
369
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700370 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700371 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700372 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700373 * <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 -0700374 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700375 */
376 @VisibleForTesting
377 boolean showWipeEuicc() {
378 Context context = getContext();
379 if (!isEuiccEnabled(context)) {
380 return false;
381 }
382 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700383 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
felkachanga6cec472018-03-29 12:08:20 +0800384 || Settings.Global.getInt(
385 cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700386 }
387
388 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800389 boolean showWipeEuiccCheckbox() {
390 return SystemProperties
391 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
392 }
393
394 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700395 protected boolean isEuiccEnabled(Context context) {
396 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
397 return euiccManager.isEnabled();
398 }
399
jackqdyulei0b3edc72016-12-13 17:07:08 -0800400 @VisibleForTesting
401 boolean hasReachedBottom(final ScrollView scrollView) {
402 if (scrollView.getChildCount() < 1) {
403 return true;
404 }
405
406 final View view = scrollView.getChildAt(0);
407 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
408
409 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400410 }
411
felkachanga6cec472018-03-29 12:08:20 +0800412 private void setUpInitiateButton() {
413 if (mInitiateButton != null) {
414 return;
415 }
416
417 final TemplateLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
418 final ButtonFooterMixin buttonFooterMixin = layout.getMixin(ButtonFooterMixin.class);
419 buttonFooterMixin.removeAllViews();
420 buttonFooterMixin.addSpace();
421 buttonFooterMixin.addSpace();
422 mInitiateButton = buttonFooterMixin.addButton(R.string.master_clear_button_text,
423 R.style.SuwGlifButton_Primary);
424 mInitiateButton.setOnClickListener(mInitiateListener);
425 }
426
Julia Reynoldsce25af42015-07-08 16:56:31 -0400427 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800428 if (v.getVisibility() != View.VISIBLE) {
429 return;
430 }
431 if (v instanceof ViewGroup) {
432 ViewGroup vGroup = (ViewGroup) v;
433 for (int i = 0; i < vGroup.getChildCount(); i++) {
434 View nextChild = vGroup.getChildAt(i);
435 getContentDescription(nextChild, description);
436 }
437 } else if (v instanceof TextView) {
438 TextView vText = (TextView) v;
439 description.append(vText.getText());
440 description.append(","); // Allow Talkback to pause between sections.
441 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800442 }
443
Ben Komalo2a321922011-09-07 16:42:34 -0700444 private boolean isExtStorageEncrypted() {
Inseob Kima1aeeb22018-11-08 12:44:47 +0900445 String state = VoldProperties.decrypt().orElse("");
Ben Komalo2a321922011-09-07 16:42:34 -0700446 return !"".equals(state);
447 }
448
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100449 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800450 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
felkachanga6cec472018-03-29 12:08:20 +0800451 LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700452 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800453
454 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100455 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
456 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800457
458 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800459
felkachanga6cec472018-03-29 12:08:20 +0800460 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Joe Onoratob51886d2010-11-08 18:25:51 -0800461 Context.LAYOUT_INFLATER_SERVICE);
462
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100463 int accountsCount = 0;
464 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
465 final UserInfo userInfo = profiles.get(profileIndex);
466 final int profileId = userInfo.id;
467 final UserHandle userHandle = new UserHandle(profileId);
468 Account[] accounts = mgr.getAccountsAsUser(profileId);
469 final int N = accounts.length;
470 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800471 continue;
472 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100473 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800474
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100475 AuthenticatorDescription[] descs = AccountManager.get(context)
476 .getAuthenticatorTypesAsUser(profileId);
477 final int M = descs.length;
478
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200479 if (profilesSize > 1) {
480 View titleView = Utils.inflateCategoryHeader(inflater, contents);
481 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
482 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
483 : R.string.category_personal);
484 contents.addView(titleView);
485 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100486
487 for (int i = 0; i < N; i++) {
488 Account account = accounts[i];
489 AuthenticatorDescription desc = null;
490 for (int j = 0; j < M; j++) {
491 if (account.type.equals(descs[j].type)) {
492 desc = descs[j];
493 break;
494 }
495 }
496 if (desc == null) {
497 Log.w(TAG, "No descriptor for account name=" + account.name
498 + " type=" + account.type);
499 continue;
500 }
501 Drawable icon = null;
502 try {
503 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000504 Context authContext = context.createPackageContextAsUser(desc.packageName,
505 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100506 icon = context.getPackageManager().getUserBadgedIcon(
507 authContext.getDrawable(desc.iconId), userHandle);
508 }
509 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100510 Log.w(TAG, "Bad package name for account type " + desc.type);
511 } catch (Resources.NotFoundException e) {
512 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
513 }
514 if (icon == null) {
515 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100516 }
517
Fan Zhang6b2bb392016-09-28 09:07:44 -0700518 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
519 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
520 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100521 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800522 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800523 }
524
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100525 if (accountsCount > 0) {
526 accountsLabel.setVisibility(View.VISIBLE);
527 contents.setVisibility(View.VISIBLE);
528 }
529 // Checking for all other users and their profiles if any.
530 View otherUsers = mContentView.findViewById(R.id.other_users_present);
531 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
532 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800533 }
534
535 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700536 public View onCreateView(LayoutInflater inflater, ViewGroup container,
537 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800538 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700539 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800540 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
541 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700542 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
543 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
544 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700545 if (disallow && !Utils.isDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400546 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000547 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100548 new ActionDisabledByAdminDialogHelper(getActivity())
549 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
550 .setOnDismissListener(__ -> getActivity().finish())
551 .show();
552 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400553 }
554
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700555 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800556
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800557 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700558 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800559 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400560
561 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700562 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500563 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400564 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800565}