blob: 307bbc1425a2b30a55fc2b6c503dc865de3de8d0 [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;
felkachanga6cec472018-03-29 12:08:20 +080067import com.android.setupwizardlib.TemplateLayout;
68import com.android.setupwizardlib.template.ButtonFooterMixin;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080069
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010070import java.util.List;
71
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072/**
73 * Confirm and execute a reset of the device to a clean "just out of the box"
74 * state. Multiple confirmations are required: first, a general "are you sure
75 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
76 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
77 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
78 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070079 *
80 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 */
Doris Ling1f497152018-02-22 16:33:07 -080082public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener {
Joe Onoratob51886d2010-11-08 18:25:51 -080083 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084
felkachanga6cec472018-03-29 12:08:20 +080085 @VisibleForTesting
86 static final int KEYGUARD_REQUEST = 55;
87 @VisibleForTesting
88 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089
Qingxi Lic8a2b042018-01-11 11:35:07 -080090 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX
91 = "masterclear.allow_retain_esim_profiles_after_fdr";
92
Amith Yamasanib14e1e02010-11-02 09:52:29 -070093 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -070094 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080095
Amith Yamasanib14e1e02010-11-02 09:52:29 -070096 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +080097 @VisibleForTesting
98 Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070099 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800100 @VisibleForTesting
101 CheckBox mExternalStorage;
Qingxi Lic8a2b042018-01-11 11:35:07 -0800102 private View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800103 @VisibleForTesting
104 CheckBox mEsimStorage;
105 @VisibleForTesting
106 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800107
Doris Ling1f497152018-02-22 16:33:07 -0800108 @Override
109 public void onGlobalLayout() {
110 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
111 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112
felkachanga6cec472018-03-29 12:08:20 +0800113 private void setUpActionBarAndTitle() {
114 final Activity activity = getActivity();
115 if (activity == null) {
116 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
117 return;
118 }
119 final ActionBar actionBar = activity.getActionBar();
120 if (actionBar == null) {
121 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
122 return;
123 }
124 actionBar.hide();
125 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700126 }
127
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800128 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700129 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800130 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800131 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700132 * @param request the request code to be returned once confirmation finishes
133 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700135 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700136 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700137 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
Fan Zhangdc00efa2018-05-10 15:10:19 -0700138 request, res.getText(R.string.master_clear_short_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139 }
140
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800141 @VisibleForTesting
142 boolean isValidRequestCode(int requestCode) {
143 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
144 }
145
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800146 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700147 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800148 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800149 onActivityResultInternal(requestCode, resultCode, data);
150 }
151
152 /*
153 * Internal method that allows easy testing without dealing with super references.
154 */
155 @VisibleForTesting
156 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800157 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800158 return;
159 }
160
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800161 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800162 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800163 return;
164 }
165
166 Intent intent = null;
167 // If returning from a Keyguard request, try to show an account confirmation request if
168 // applciable.
169 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
170 && (intent = getAccountConfirmationIntent()) != null) {
171 showAccountCredentialConfirmation(intent);
172 } else {
173 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 }
175 }
176
qingxi072f2862017-04-11 18:28:40 -0700177 @VisibleForTesting
178 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800179 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700180 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Qingxi Lic8a2b042018-01-11 11:35:07 -0800181 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Fan Zhang7cf99f52018-02-16 10:37:37 -0800182 new SubSettingLauncher(getContext())
183 .setDestination(MasterClearConfirm.class.getName())
184 .setArguments(args)
hjchangliaoe86eec02018-05-02 13:01:07 +0800185 .setTitleRes(R.string.master_clear_confirm_title)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800186 .setSourceMetricsCategory(getMetricsCategory())
187 .launch();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700188 }
189
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800190 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800191 void showAccountCredentialConfirmation(Intent intent) {
192 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
193 }
194
195 @VisibleForTesting
196 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800197 final Context context = getActivity();
198 final String accountType = context.getString(R.string.account_type);
199 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800200 final String className = context.getString(R.string.account_confirmation_class);
201 if (TextUtils.isEmpty(accountType)
202 || TextUtils.isEmpty(packageName)
203 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800204 Log.i(TAG, "Resources not set for account confirmation.");
205 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800206 }
207 final AccountManager am = AccountManager.get(context);
208 Account[] accounts = am.getAccountsByType(accountType);
209 if (accounts != null && accounts.length > 0) {
210 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800211 .setPackage(packageName)
212 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800213 // Check to make sure that the intent is supported.
214 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800215 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
216 if (resolution != null
217 && resolution.activityInfo != null
218 && packageName.equals(resolution.activityInfo.packageName)) {
219 // Note that we need to check the packagename to make sure that an Activity resolver
220 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800221 return requestAccountConfirmation;
222 } else {
223 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800224 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800225 } else {
226 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800227 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800228 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800229 }
230
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800231 /**
232 * If the user clicks to begin the reset sequence, we next require a
233 * keyguard confirmation if the user has currently enabled one. If there
234 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700235 *
236 * 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 -0800237 */
Christine Franksa09eb382017-06-23 14:28:21 -0700238 @VisibleForTesting
239 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700240
Christine Franksa09eb382017-06-23 14:28:21 -0700241 public void onClick(View view) {
242 final Context context = view.getContext();
243 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700244 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
245 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700246 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700247 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700248 .setAction(Intent.ACTION_FACTORY_RESET);
249 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800250 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800251 return;
252 }
253
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800254 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
255 return;
256 }
257
258 Intent intent = getAccountConfirmationIntent();
259 if (intent != null) {
260 showAccountCredentialConfirmation(intent);
261 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700262 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800263 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800264 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700265 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800266
267 /**
268 * In its initial state, the activity presents a button for the user to
269 * click in order to initiate a confirmation sequence. This method is
270 * called from various other points in the code to reset the activity to
271 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800272 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800273 * <p>Reinflating views from resources is expensive and prevents us from
274 * caching widget pointers, so we use a single-inflate pattern: we lazy-
275 * inflate each view, caching all of the widget pointers we'll need at the
276 * time, then simply reuse the inflated views directly whenever we need
277 * to change contents.
278 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800279 @VisibleForTesting
280 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800281 setUpActionBarAndTitle();
282 setUpInitiateButton();
283
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700284 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800285 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800286 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800287 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
288 if (mScrollView != null) {
289 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
290 }
291 mScrollView = mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800292
Kenny Root3785e392011-01-18 15:14:32 -0800293 /*
294 * If the external storage is emulated, it will be erased with a factory
295 * reset at any rate. There is no need to have a separate option until
296 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700297 * others. Likewise, if it's non-removable storage, it could potentially have been
298 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800299 */
Ben Komalo2a321922011-09-07 16:42:34 -0700300 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
301 if (isExtStorageEmulated
302 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800303 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700304
Kenny Root3785e392011-01-18 15:14:32 -0800305 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
306 externalOption.setVisibility(View.GONE);
307
308 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
309 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700310
311 // If it's not emulated, it is on a separate partition but it means we're doing
312 // a force wipe due to encryption.
313 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800314 } else {
315 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
316
317 @Override
318 public void onClick(View v) {
319 mExternalStorage.toggle();
320 }
321 });
322 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800323
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700324 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800325 if (showWipeEuiccCheckbox()) {
326 TextView title = mContentView.findViewById(R.id.erase_esim_title);
327 title.setText(R.string.erase_esim_storage);
328 mEsimStorageContainer.setVisibility(View.VISIBLE);
329 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
330 @Override
331 public void onClick(View v) {
332 mEsimStorage.toggle();
333 }
334 });
335 } else {
336 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
337 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700338
Qingxi Lic8a2b042018-01-11 11:35:07 -0800339 final View noCancelMobilePlan = mContentView.findViewById(
340 R.id.no_cancel_mobile_plan);
341 noCancelMobilePlan.setVisibility(View.VISIBLE);
342 mEsimStorage.setChecked(true /* checked */);
343 }
qingxi072f2862017-04-11 18:28:40 -0700344 }
345
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100346 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
347 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800348 final StringBuffer contentDescription = new StringBuffer();
349 final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400350 getContentDescription(masterClearContainer, contentDescription);
351 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800352
353 // Set the status of initiateButton based on scrollview
354 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
355 @Override
356 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800357 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800358 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
359 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800360 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800361 }
362 }
363 });
364
365 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800366 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800367 }
368
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700369 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700370 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700371 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700372 * <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 -0700373 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700374 */
375 @VisibleForTesting
376 boolean showWipeEuicc() {
377 Context context = getContext();
378 if (!isEuiccEnabled(context)) {
379 return false;
380 }
381 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700382 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
felkachanga6cec472018-03-29 12:08:20 +0800383 || Settings.Global.getInt(
384 cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700385 }
386
387 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800388 boolean showWipeEuiccCheckbox() {
389 return SystemProperties
390 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
391 }
392
393 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700394 protected boolean isEuiccEnabled(Context context) {
395 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
396 return euiccManager.isEnabled();
397 }
398
jackqdyulei0b3edc72016-12-13 17:07:08 -0800399 @VisibleForTesting
400 boolean hasReachedBottom(final ScrollView scrollView) {
401 if (scrollView.getChildCount() < 1) {
402 return true;
403 }
404
405 final View view = scrollView.getChildAt(0);
406 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
407
408 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400409 }
410
felkachanga6cec472018-03-29 12:08:20 +0800411 private void setUpInitiateButton() {
412 if (mInitiateButton != null) {
413 return;
414 }
415
416 final TemplateLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
417 final ButtonFooterMixin buttonFooterMixin = layout.getMixin(ButtonFooterMixin.class);
418 buttonFooterMixin.removeAllViews();
419 buttonFooterMixin.addSpace();
420 buttonFooterMixin.addSpace();
421 mInitiateButton = buttonFooterMixin.addButton(R.string.master_clear_button_text,
422 R.style.SuwGlifButton_Primary);
423 mInitiateButton.setOnClickListener(mInitiateListener);
424 }
425
Julia Reynoldsce25af42015-07-08 16:56:31 -0400426 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800427 if (v.getVisibility() != View.VISIBLE) {
428 return;
429 }
430 if (v instanceof ViewGroup) {
431 ViewGroup vGroup = (ViewGroup) v;
432 for (int i = 0; i < vGroup.getChildCount(); i++) {
433 View nextChild = vGroup.getChildAt(i);
434 getContentDescription(nextChild, description);
435 }
436 } else if (v instanceof TextView) {
437 TextView vText = (TextView) v;
438 description.append(vText.getText());
439 description.append(","); // Allow Talkback to pause between sections.
440 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800441 }
442
Ben Komalo2a321922011-09-07 16:42:34 -0700443 private boolean isExtStorageEncrypted() {
Inseob Kima1aeeb22018-11-08 12:44:47 +0900444 String state = VoldProperties.decrypt().orElse("");
Ben Komalo2a321922011-09-07 16:42:34 -0700445 return !"".equals(state);
446 }
447
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100448 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800449 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
felkachanga6cec472018-03-29 12:08:20 +0800450 LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700451 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800452
453 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100454 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
455 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800456
457 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800458
felkachanga6cec472018-03-29 12:08:20 +0800459 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Joe Onoratob51886d2010-11-08 18:25:51 -0800460 Context.LAYOUT_INFLATER_SERVICE);
461
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100462 int accountsCount = 0;
463 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
464 final UserInfo userInfo = profiles.get(profileIndex);
465 final int profileId = userInfo.id;
466 final UserHandle userHandle = new UserHandle(profileId);
467 Account[] accounts = mgr.getAccountsAsUser(profileId);
468 final int N = accounts.length;
469 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800470 continue;
471 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100472 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800473
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100474 AuthenticatorDescription[] descs = AccountManager.get(context)
475 .getAuthenticatorTypesAsUser(profileId);
476 final int M = descs.length;
477
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200478 if (profilesSize > 1) {
479 View titleView = Utils.inflateCategoryHeader(inflater, contents);
480 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
481 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
482 : R.string.category_personal);
483 contents.addView(titleView);
484 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100485
486 for (int i = 0; i < N; i++) {
487 Account account = accounts[i];
488 AuthenticatorDescription desc = null;
489 for (int j = 0; j < M; j++) {
490 if (account.type.equals(descs[j].type)) {
491 desc = descs[j];
492 break;
493 }
494 }
495 if (desc == null) {
496 Log.w(TAG, "No descriptor for account name=" + account.name
497 + " type=" + account.type);
498 continue;
499 }
500 Drawable icon = null;
501 try {
502 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000503 Context authContext = context.createPackageContextAsUser(desc.packageName,
504 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100505 icon = context.getPackageManager().getUserBadgedIcon(
506 authContext.getDrawable(desc.iconId), userHandle);
507 }
508 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100509 Log.w(TAG, "Bad package name for account type " + desc.type);
510 } catch (Resources.NotFoundException e) {
511 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
512 }
513 if (icon == null) {
514 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100515 }
516
Fan Zhang6b2bb392016-09-28 09:07:44 -0700517 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
518 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
519 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100520 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800521 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800522 }
523
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100524 if (accountsCount > 0) {
525 accountsLabel.setVisibility(View.VISIBLE);
526 contents.setVisibility(View.VISIBLE);
527 }
528 // Checking for all other users and their profiles if any.
529 View otherUsers = mContentView.findViewById(R.id.other_users_present);
530 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
531 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800532 }
533
534 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700535 public View onCreateView(LayoutInflater inflater, ViewGroup container,
536 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800537 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700538 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800539 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
540 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700541 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
542 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
543 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700544 if (disallow && !Utils.isDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400545 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000546 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100547 new ActionDisabledByAdminDialogHelper(getActivity())
548 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
549 .setOnDismissListener(__ -> getActivity().finish())
550 .show();
551 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400552 }
553
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700554 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800555
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800556 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700557 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800558 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400559
560 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700561 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500562 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400563 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800564}