blob: 97942fec63e860838d04ed21e0c8fdbf0a692a7c [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;
qingxi072f2862017-04-11 18:28:40 -070042import android.telephony.euicc.EuiccManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080043import android.text.TextUtils;
Joe Onoratob51886d2010-11-08 18:25:51 -080044import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045import android.view.LayoutInflater;
46import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080047import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070048import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080049import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070051import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070052import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080053import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080054import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080055import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040056
Fan Zhang23f8d592018-08-28 15:11:40 -070057import androidx.annotation.VisibleForTesting;
58
Tamas Berghammer265d3c22016-06-22 15:34:45 +010059import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Doris Ling72489722017-11-16 11:03:40 -080060import com.android.settings.core.InstrumentedFragment;
Fan Zhang7cf99f52018-02-16 10:37:37 -080061import com.android.settings.core.SubSettingLauncher;
arangelove35badd2017-10-27 13:51:28 +010062import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Maurice Lam2eb170c2017-04-28 16:18:47 -070063import com.android.settings.password.ChooseLockSettingsHelper;
64import com.android.settings.password.ConfirmLockPattern;
Philip P. Moltmanne3f72112018-08-28 15:01:43 -070065import com.android.settingslib.RestrictedLockUtilsInternal;
felkachanga6cec472018-03-29 12:08:20 +080066import com.android.setupwizardlib.TemplateLayout;
67import com.android.setupwizardlib.template.ButtonFooterMixin;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010069import java.util.List;
70
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080071/**
72 * Confirm and execute a reset of the device to a clean "just out of the box"
73 * state. Multiple confirmations are required: first, a general "are you sure
74 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
75 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
76 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
77 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070078 *
79 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 */
Doris Ling1f497152018-02-22 16:33:07 -080081public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener {
Joe Onoratob51886d2010-11-08 18:25:51 -080082 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080083
felkachanga6cec472018-03-29 12:08:20 +080084 @VisibleForTesting
85 static final int KEYGUARD_REQUEST = 55;
86 @VisibleForTesting
87 static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088
Qingxi Lic8a2b042018-01-11 11:35:07 -080089 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX
90 = "masterclear.allow_retain_esim_profiles_after_fdr";
91
Amith Yamasanib14e1e02010-11-02 09:52:29 -070092 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -070093 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094
Amith Yamasanib14e1e02010-11-02 09:52:29 -070095 private View mContentView;
felkachanga6cec472018-03-29 12:08:20 +080096 @VisibleForTesting
97 Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070098 private View mExternalStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +080099 @VisibleForTesting
100 CheckBox mExternalStorage;
Qingxi Lic8a2b042018-01-11 11:35:07 -0800101 private View mEsimStorageContainer;
felkachanga6cec472018-03-29 12:08:20 +0800102 @VisibleForTesting
103 CheckBox mEsimStorage;
104 @VisibleForTesting
105 ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -0800106
Doris Ling1f497152018-02-22 16:33:07 -0800107 @Override
108 public void onGlobalLayout() {
109 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
110 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111
felkachanga6cec472018-03-29 12:08:20 +0800112 private void setUpActionBarAndTitle() {
113 final Activity activity = getActivity();
114 if (activity == null) {
115 Log.e(TAG, "No activity attached, skipping setUpActionBarAndTitle");
116 return;
117 }
118 final ActionBar actionBar = activity.getActionBar();
119 if (actionBar == null) {
120 Log.e(TAG, "No actionbar, skipping setUpActionBarAndTitle");
121 return;
122 }
123 actionBar.hide();
124 activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Doris Ling03a3b512017-10-18 14:25:01 -0700125 }
126
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800127 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700128 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 * component as a subactivity
felkachanga6cec472018-03-29 12:08:20 +0800130 *
Jim Miller2deec7e2010-04-13 17:43:36 -0700131 * @param request the request code to be returned once confirmation finishes
132 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800133 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700134 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700135 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700136 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
Fan Zhangdc00efa2018-05-10 15:10:19 -0700137 request, res.getText(R.string.master_clear_short_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138 }
139
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800140 @VisibleForTesting
141 boolean isValidRequestCode(int requestCode) {
142 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
143 }
144
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800145 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700146 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800147 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800148 onActivityResultInternal(requestCode, resultCode, data);
149 }
150
151 /*
152 * Internal method that allows easy testing without dealing with super references.
153 */
154 @VisibleForTesting
155 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800156 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800157 return;
158 }
159
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800160 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800161 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800162 return;
163 }
164
165 Intent intent = null;
166 // If returning from a Keyguard request, try to show an account confirmation request if
167 // applciable.
168 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
169 && (intent = getAccountConfirmationIntent()) != null) {
170 showAccountCredentialConfirmation(intent);
171 } else {
172 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 }
174 }
175
qingxi072f2862017-04-11 18:28:40 -0700176 @VisibleForTesting
177 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800178 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700179 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Qingxi Lic8a2b042018-01-11 11:35:07 -0800180 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Fan Zhang7cf99f52018-02-16 10:37:37 -0800181 new SubSettingLauncher(getContext())
182 .setDestination(MasterClearConfirm.class.getName())
183 .setArguments(args)
hjchangliaoe86eec02018-05-02 13:01:07 +0800184 .setTitleRes(R.string.master_clear_confirm_title)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800185 .setSourceMetricsCategory(getMetricsCategory())
186 .launch();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700187 }
188
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800189 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800190 void showAccountCredentialConfirmation(Intent intent) {
191 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
192 }
193
194 @VisibleForTesting
195 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800196 final Context context = getActivity();
197 final String accountType = context.getString(R.string.account_type);
198 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800199 final String className = context.getString(R.string.account_confirmation_class);
200 if (TextUtils.isEmpty(accountType)
201 || TextUtils.isEmpty(packageName)
202 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800203 Log.i(TAG, "Resources not set for account confirmation.");
204 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800205 }
206 final AccountManager am = AccountManager.get(context);
207 Account[] accounts = am.getAccountsByType(accountType);
208 if (accounts != null && accounts.length > 0) {
209 final Intent requestAccountConfirmation = new Intent()
felkachanga6cec472018-03-29 12:08:20 +0800210 .setPackage(packageName)
211 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800212 // Check to make sure that the intent is supported.
213 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800214 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
215 if (resolution != null
216 && resolution.activityInfo != null
217 && packageName.equals(resolution.activityInfo.packageName)) {
218 // Note that we need to check the packagename to make sure that an Activity resolver
219 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800220 return requestAccountConfirmation;
221 } else {
222 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800223 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800224 } else {
225 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800226 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800227 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800228 }
229
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800230 /**
231 * If the user clicks to begin the reset sequence, we next require a
232 * keyguard confirmation if the user has currently enabled one. If there
233 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700234 *
235 * 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 -0800236 */
Christine Franksa09eb382017-06-23 14:28:21 -0700237 @VisibleForTesting
238 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700239
Christine Franksa09eb382017-06-23 14:28:21 -0700240 public void onClick(View view) {
241 final Context context = view.getContext();
242 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700243 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
244 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700245 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700246 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700247 .setAction(Intent.ACTION_FACTORY_RESET);
248 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800249 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800250 return;
251 }
252
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800253 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
254 return;
255 }
256
257 Intent intent = getAccountConfirmationIntent();
258 if (intent != null) {
259 showAccountCredentialConfirmation(intent);
260 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700261 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800262 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800263 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700264 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800265
266 /**
267 * In its initial state, the activity presents a button for the user to
268 * click in order to initiate a confirmation sequence. This method is
269 * called from various other points in the code to reset the activity to
270 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800271 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800272 * <p>Reinflating views from resources is expensive and prevents us from
273 * caching widget pointers, so we use a single-inflate pattern: we lazy-
274 * inflate each view, caching all of the widget pointers we'll need at the
275 * time, then simply reuse the inflated views directly whenever we need
276 * to change contents.
277 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800278 @VisibleForTesting
279 void establishInitialState() {
felkachanga6cec472018-03-29 12:08:20 +0800280 setUpActionBarAndTitle();
281 setUpInitiateButton();
282
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700283 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800284 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800285 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800286 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
287 if (mScrollView != null) {
288 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
289 }
290 mScrollView = mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800291
Kenny Root3785e392011-01-18 15:14:32 -0800292 /*
293 * If the external storage is emulated, it will be erased with a factory
294 * reset at any rate. There is no need to have a separate option until
295 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700296 * others. Likewise, if it's non-removable storage, it could potentially have been
297 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800298 */
Ben Komalo2a321922011-09-07 16:42:34 -0700299 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
300 if (isExtStorageEmulated
301 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800302 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700303
Kenny Root3785e392011-01-18 15:14:32 -0800304 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
305 externalOption.setVisibility(View.GONE);
306
307 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
308 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700309
310 // If it's not emulated, it is on a separate partition but it means we're doing
311 // a force wipe due to encryption.
312 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800313 } else {
314 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
315
316 @Override
317 public void onClick(View v) {
318 mExternalStorage.toggle();
319 }
320 });
321 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800322
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700323 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800324 if (showWipeEuiccCheckbox()) {
325 TextView title = mContentView.findViewById(R.id.erase_esim_title);
326 title.setText(R.string.erase_esim_storage);
327 mEsimStorageContainer.setVisibility(View.VISIBLE);
328 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
329 @Override
330 public void onClick(View v) {
331 mEsimStorage.toggle();
332 }
333 });
334 } else {
335 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
336 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700337
Qingxi Lic8a2b042018-01-11 11:35:07 -0800338 final View noCancelMobilePlan = mContentView.findViewById(
339 R.id.no_cancel_mobile_plan);
340 noCancelMobilePlan.setVisibility(View.VISIBLE);
341 mEsimStorage.setChecked(true /* checked */);
342 }
qingxi072f2862017-04-11 18:28:40 -0700343 }
344
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100345 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
346 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800347 final StringBuffer contentDescription = new StringBuffer();
348 final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400349 getContentDescription(masterClearContainer, contentDescription);
350 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800351
352 // Set the status of initiateButton based on scrollview
353 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
354 @Override
355 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
felkachanga6cec472018-03-29 12:08:20 +0800356 int oldScrollY) {
jackqdyulei0b3edc72016-12-13 17:07:08 -0800357 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
358 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800359 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800360 }
361 }
362 });
363
364 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800365 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800366 }
367
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700368 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700369 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700370 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700371 * <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 -0700372 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700373 */
374 @VisibleForTesting
375 boolean showWipeEuicc() {
376 Context context = getContext();
377 if (!isEuiccEnabled(context)) {
378 return false;
379 }
380 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700381 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
felkachanga6cec472018-03-29 12:08:20 +0800382 || Settings.Global.getInt(
383 cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700384 }
385
386 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800387 boolean showWipeEuiccCheckbox() {
388 return SystemProperties
389 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
390 }
391
392 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700393 protected boolean isEuiccEnabled(Context context) {
394 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
395 return euiccManager.isEnabled();
396 }
397
jackqdyulei0b3edc72016-12-13 17:07:08 -0800398 @VisibleForTesting
399 boolean hasReachedBottom(final ScrollView scrollView) {
400 if (scrollView.getChildCount() < 1) {
401 return true;
402 }
403
404 final View view = scrollView.getChildAt(0);
405 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
406
407 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400408 }
409
felkachanga6cec472018-03-29 12:08:20 +0800410 private void setUpInitiateButton() {
411 if (mInitiateButton != null) {
412 return;
413 }
414
415 final TemplateLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
416 final ButtonFooterMixin buttonFooterMixin = layout.getMixin(ButtonFooterMixin.class);
417 buttonFooterMixin.removeAllViews();
418 buttonFooterMixin.addSpace();
419 buttonFooterMixin.addSpace();
420 mInitiateButton = buttonFooterMixin.addButton(R.string.master_clear_button_text,
421 R.style.SuwGlifButton_Primary);
422 mInitiateButton.setOnClickListener(mInitiateListener);
423 }
424
Julia Reynoldsce25af42015-07-08 16:56:31 -0400425 private void getContentDescription(View v, StringBuffer description) {
felkachanga6cec472018-03-29 12:08:20 +0800426 if (v.getVisibility() != View.VISIBLE) {
427 return;
428 }
429 if (v instanceof ViewGroup) {
430 ViewGroup vGroup = (ViewGroup) v;
431 for (int i = 0; i < vGroup.getChildCount(); i++) {
432 View nextChild = vGroup.getChildAt(i);
433 getContentDescription(nextChild, description);
434 }
435 } else if (v instanceof TextView) {
436 TextView vText = (TextView) v;
437 description.append(vText.getText());
438 description.append(","); // Allow Talkback to pause between sections.
439 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800440 }
441
Ben Komalo2a321922011-09-07 16:42:34 -0700442 private boolean isExtStorageEncrypted() {
443 String state = SystemProperties.get("vold.decrypt");
444 return !"".equals(state);
445 }
446
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100447 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800448 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
felkachanga6cec472018-03-29 12:08:20 +0800449 LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700450 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800451
452 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100453 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
454 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800455
456 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800457
felkachanga6cec472018-03-29 12:08:20 +0800458 LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Joe Onoratob51886d2010-11-08 18:25:51 -0800459 Context.LAYOUT_INFLATER_SERVICE);
460
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100461 int accountsCount = 0;
462 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
463 final UserInfo userInfo = profiles.get(profileIndex);
464 final int profileId = userInfo.id;
465 final UserHandle userHandle = new UserHandle(profileId);
466 Account[] accounts = mgr.getAccountsAsUser(profileId);
467 final int N = accounts.length;
468 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800469 continue;
470 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100471 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800472
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100473 AuthenticatorDescription[] descs = AccountManager.get(context)
474 .getAuthenticatorTypesAsUser(profileId);
475 final int M = descs.length;
476
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200477 if (profilesSize > 1) {
478 View titleView = Utils.inflateCategoryHeader(inflater, contents);
479 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
480 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
481 : R.string.category_personal);
482 contents.addView(titleView);
483 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100484
485 for (int i = 0; i < N; i++) {
486 Account account = accounts[i];
487 AuthenticatorDescription desc = null;
488 for (int j = 0; j < M; j++) {
489 if (account.type.equals(descs[j].type)) {
490 desc = descs[j];
491 break;
492 }
493 }
494 if (desc == null) {
495 Log.w(TAG, "No descriptor for account name=" + account.name
496 + " type=" + account.type);
497 continue;
498 }
499 Drawable icon = null;
500 try {
501 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000502 Context authContext = context.createPackageContextAsUser(desc.packageName,
503 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100504 icon = context.getPackageManager().getUserBadgedIcon(
505 authContext.getDrawable(desc.iconId), userHandle);
506 }
507 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100508 Log.w(TAG, "Bad package name for account type " + desc.type);
509 } catch (Resources.NotFoundException e) {
510 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
511 }
512 if (icon == null) {
513 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100514 }
515
Fan Zhang6b2bb392016-09-28 09:07:44 -0700516 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
517 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
518 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100519 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800520 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800521 }
522
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100523 if (accountsCount > 0) {
524 accountsLabel.setVisibility(View.VISIBLE);
525 contents.setVisibility(View.VISIBLE);
526 }
527 // Checking for all other users and their profiles if any.
528 View otherUsers = mContentView.findViewById(R.id.other_users_present);
529 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
530 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800531 }
532
533 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700534 public View onCreateView(LayoutInflater inflater, ViewGroup container,
535 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800536 final Context context = getContext();
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700537 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(context,
Christine Franks14782222017-01-23 16:44:02 -0800538 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
539 final UserManager um = UserManager.get(context);
Philip P. Moltmanne3f72112018-08-28 15:01:43 -0700540 final boolean disallow = !um.isAdminUser() || RestrictedLockUtilsInternal
541 .hasBaseUserRestriction(context, UserManager.DISALLOW_FACTORY_RESET,
542 UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700543 if (disallow && !Utils.isDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400544 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000545 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100546 new ActionDisabledByAdminDialogHelper(getActivity())
547 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
548 .setOnDismissListener(__ -> getActivity().finish())
549 .show();
550 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400551 }
552
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700553 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800554
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800555 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700556 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800557 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400558
559 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700560 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500561 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400562 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800563}