blob: 2983dba7fca474912bd6a61cf2de7873da8974ea [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;
Doris Linged4685f2017-10-25 14:08:57 -070024import android.annotation.Nullable;
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;
Joe Onoratob51886d2010-11-08 18:25:51 -080034import android.graphics.drawable.Drawable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035import android.os.Bundle;
Kenny Root3785e392011-01-18 15:14:32 -080036import android.os.Environment;
Ben Komalo2a321922011-09-07 16:42:34 -070037import android.os.SystemProperties;
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010038import android.os.UserHandle;
Amith Yamasani7eedcf42013-07-02 14:15:29 -070039import android.os.UserManager;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -070040import android.provider.Settings;
qingxi072f2862017-04-11 18:28:40 -070041import android.telephony.euicc.EuiccManager;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -080042import android.text.TextUtils;
Joe Onoratob51886d2010-11-08 18:25:51 -080043import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080044import android.view.LayoutInflater;
45import android.view.View;
jackqdyulei0b3edc72016-12-13 17:07:08 -080046import android.view.View.OnScrollChangeListener;
Amith Yamasanib14e1e02010-11-02 09:52:29 -070047import android.view.ViewGroup;
jackqdyulei0b3edc72016-12-13 17:07:08 -080048import android.view.ViewTreeObserver.OnGlobalLayoutListener;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049import android.widget.Button;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070050import android.widget.CheckBox;
Fan Zhang6b2bb392016-09-28 09:07:44 -070051import android.widget.ImageView;
Joe Onoratob51886d2010-11-08 18:25:51 -080052import android.widget.LinearLayout;
jackqdyulei0b3edc72016-12-13 17:07:08 -080053import android.widget.ScrollView;
Joe Onoratob51886d2010-11-08 18:25:51 -080054import android.widget.TextView;
Jason Monk39b46742015-09-10 15:52:51 -040055
Tamas Berghammer265d3c22016-06-22 15:34:45 +010056import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Doris Ling72489722017-11-16 11:03:40 -080057import com.android.settings.core.InstrumentedFragment;
Fan Zhang7cf99f52018-02-16 10:37:37 -080058import com.android.settings.core.SubSettingLauncher;
arangelove35badd2017-10-27 13:51:28 +010059import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
Maurice Lam2eb170c2017-04-28 16:18:47 -070060import com.android.settings.password.ChooseLockSettingsHelper;
61import com.android.settings.password.ConfirmLockPattern;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000062import com.android.settingslib.RestrictedLockUtils;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +010064import java.util.List;
65
Fan Zhangc7162cd2018-06-18 15:21:41 -070066import androidx.annotation.VisibleForTesting;
67
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068/**
69 * Confirm and execute a reset of the device to a clean "just out of the box"
70 * state. Multiple confirmations are required: first, a general "are you sure
71 * you want to do this?" prompt, followed by a keyguard pattern trace if the user
72 * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
73 * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is
74 * locked, et cetera, then the confirmation sequence is abandoned.
Amith Yamasanib14e1e02010-11-02 09:52:29 -070075 *
76 * This is the initial screen.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080077 */
Doris Ling1f497152018-02-22 16:33:07 -080078public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener {
Joe Onoratob51886d2010-11-08 18:25:51 -080079 private static final String TAG = "MasterClear";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -080081 @VisibleForTesting static final int KEYGUARD_REQUEST = 55;
Carlos Valdivia08c88202018-01-21 18:57:28 -080082 @VisibleForTesting static final int CREDENTIAL_CONFIRM_REQUEST = 56;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080083
Qingxi Lic8a2b042018-01-11 11:35:07 -080084 private static final String KEY_SHOW_ESIM_RESET_CHECKBOX
85 = "masterclear.allow_retain_esim_profiles_after_fdr";
86
Amith Yamasanib14e1e02010-11-02 09:52:29 -070087 static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
qingxi072f2862017-04-11 18:28:40 -070088 static final String ERASE_ESIMS_EXTRA = "erase_esim";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089
Amith Yamasanib14e1e02010-11-02 09:52:29 -070090 private View mContentView;
Doris Ling1f497152018-02-22 16:33:07 -080091 @VisibleForTesting Button mInitiateButton;
Dianne Hackborn1337d0f2010-10-14 11:58:30 -070092 private View mExternalStorageContainer;
qingxi072f2862017-04-11 18:28:40 -070093 @VisibleForTesting CheckBox mExternalStorage;
Qingxi Lic8a2b042018-01-11 11:35:07 -080094 private View mEsimStorageContainer;
95 @VisibleForTesting CheckBox mEsimStorage;
Doris Ling1f497152018-02-22 16:33:07 -080096 @VisibleForTesting ScrollView mScrollView;
jackqdyulei0b3edc72016-12-13 17:07:08 -080097
Doris Ling1f497152018-02-22 16:33:07 -080098 @Override
99 public void onGlobalLayout() {
100 mInitiateButton.setEnabled(hasReachedBottom(mScrollView));
101 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102
Doris Ling03a3b512017-10-18 14:25:01 -0700103 @Override
Doris Linged4685f2017-10-25 14:08:57 -0700104 public void onCreate(@Nullable Bundle savedInstanceState) {
105 super.onCreate(savedInstanceState);
Fan Zhangdc00efa2018-05-10 15:10:19 -0700106 getActivity().setTitle(R.string.master_clear_short_title);
Doris Ling03a3b512017-10-18 14:25:01 -0700107 }
108
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800109 /**
Jim Miller2deec7e2010-04-13 17:43:36 -0700110 * Keyguard validation is run using the standard {@link ConfirmLockPattern}
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800111 * component as a subactivity
Jim Miller2deec7e2010-04-13 17:43:36 -0700112 * @param request the request code to be returned once confirmation finishes
113 * @return true if confirmation launched
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800114 */
Jim Miller2deec7e2010-04-13 17:43:36 -0700115 private boolean runKeyguardConfirmation(int request) {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700116 Resources res = getActivity().getResources();
Jorim Jaggi8a09b612015-04-06 17:47:18 -0700117 return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
Fan Zhangdc00efa2018-05-10 15:10:19 -0700118 request, res.getText(R.string.master_clear_short_title));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800119 }
120
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800121 @VisibleForTesting
122 boolean isValidRequestCode(int requestCode) {
123 return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
124 }
125
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800126 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700127 public void onActivityResult(int requestCode, int resultCode, Intent data) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800128 super.onActivityResult(requestCode, resultCode, data);
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800129 onActivityResultInternal(requestCode, resultCode, data);
130 }
131
132 /*
133 * Internal method that allows easy testing without dealing with super references.
134 */
135 @VisibleForTesting
136 void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
Carlos Valdivia5dd6ed42018-01-23 09:12:59 -0800137 if (!isValidRequestCode(requestCode)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138 return;
139 }
140
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800141 if (resultCode != Activity.RESULT_OK) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 establishInitialState();
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800143 return;
144 }
145
146 Intent intent = null;
147 // If returning from a Keyguard request, try to show an account confirmation request if
148 // applciable.
149 if (CREDENTIAL_CONFIRM_REQUEST != requestCode
150 && (intent = getAccountConfirmationIntent()) != null) {
151 showAccountCredentialConfirmation(intent);
152 } else {
153 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800154 }
155 }
156
qingxi072f2862017-04-11 18:28:40 -0700157 @VisibleForTesting
158 void showFinalConfirmation() {
Fan Zhang7cf99f52018-02-16 10:37:37 -0800159 final Bundle args = new Bundle();
Stuart Scottbe903412014-07-24 19:22:06 -0700160 args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
Qingxi Lic8a2b042018-01-11 11:35:07 -0800161 args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked());
Fan Zhang7cf99f52018-02-16 10:37:37 -0800162 new SubSettingLauncher(getContext())
163 .setDestination(MasterClearConfirm.class.getName())
164 .setArguments(args)
hjchangliaoe86eec02018-05-02 13:01:07 +0800165 .setTitleRes(R.string.master_clear_confirm_title)
Fan Zhang7cf99f52018-02-16 10:37:37 -0800166 .setSourceMetricsCategory(getMetricsCategory())
167 .launch();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700168 }
169
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800170 @VisibleForTesting
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800171 void showAccountCredentialConfirmation(Intent intent) {
172 startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
173 }
174
175 @VisibleForTesting
176 Intent getAccountConfirmationIntent() {
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800177 final Context context = getActivity();
178 final String accountType = context.getString(R.string.account_type);
179 final String packageName = context.getString(R.string.account_confirmation_package);
Carlos Valdivia51146042018-01-23 17:17:56 -0800180 final String className = context.getString(R.string.account_confirmation_class);
181 if (TextUtils.isEmpty(accountType)
182 || TextUtils.isEmpty(packageName)
183 || TextUtils.isEmpty(className)) {
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800184 Log.i(TAG, "Resources not set for account confirmation.");
185 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800186 }
187 final AccountManager am = AccountManager.get(context);
188 Account[] accounts = am.getAccountsByType(accountType);
189 if (accounts != null && accounts.length > 0) {
190 final Intent requestAccountConfirmation = new Intent()
191 .setPackage(packageName)
Carlos Valdivia51146042018-01-23 17:17:56 -0800192 .setComponent(new ComponentName(packageName, className));
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800193 // Check to make sure that the intent is supported.
194 final PackageManager pm = context.getPackageManager();
Carlos Valdivia08c88202018-01-21 18:57:28 -0800195 final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
196 if (resolution != null
197 && resolution.activityInfo != null
198 && packageName.equals(resolution.activityInfo.packageName)) {
199 // Note that we need to check the packagename to make sure that an Activity resolver
200 // wasn't returned.
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800201 return requestAccountConfirmation;
202 } else {
203 Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800204 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800205 } else {
206 Log.d(TAG, "No " + accountType + " accounts installed!");
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800207 }
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800208 return null;
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800209 }
210
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800211 /**
212 * If the user clicks to begin the reset sequence, we next require a
213 * keyguard confirmation if the user has currently enabled one. If there
214 * is no keyguard available, we simply go to the final confirmation prompt.
Christine Franksa09eb382017-06-23 14:28:21 -0700215 *
216 * 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 -0800217 */
Christine Franksa09eb382017-06-23 14:28:21 -0700218 @VisibleForTesting
219 protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700220
Christine Franksa09eb382017-06-23 14:28:21 -0700221 public void onClick(View view) {
222 final Context context = view.getContext();
223 if (Utils.isDemoUser(context)) {
Justin Klaassen30257272017-08-08 21:58:05 -0700224 final ComponentName componentName = Utils.getDeviceOwnerComponent(context);
225 if (componentName != null) {
Christine Franksa09eb382017-06-23 14:28:21 -0700226 final Intent requestFactoryReset = new Intent()
Justin Klaassen30257272017-08-08 21:58:05 -0700227 .setPackage(componentName.getPackageName())
Christine Franksa09eb382017-06-23 14:28:21 -0700228 .setAction(Intent.ACTION_FACTORY_RESET);
229 context.startActivity(requestFactoryReset);
Christine Franksd7713c92017-01-20 11:24:14 -0800230 }
Carlos Valdiviaa9736ed2018-01-16 13:59:31 -0800231 return;
232 }
233
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800234 if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
235 return;
236 }
237
238 Intent intent = getAccountConfirmationIntent();
239 if (intent != null) {
240 showAccountCredentialConfirmation(intent);
241 } else {
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700242 showFinalConfirmation();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800243 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800244 }
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700245 };
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800246
247 /**
248 * In its initial state, the activity presents a button for the user to
249 * click in order to initiate a confirmation sequence. This method is
250 * called from various other points in the code to reset the activity to
251 * this base state.
Jim Miller47d380f2010-01-20 13:37:14 -0800252 *
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800253 * <p>Reinflating views from resources is expensive and prevents us from
254 * caching widget pointers, so we use a single-inflate pattern: we lazy-
255 * inflate each view, caching all of the widget pointers we'll need at the
256 * time, then simply reuse the inflated views directly whenever we need
257 * to change contents.
258 */
Carlos Valdivia4b34dad2018-01-29 18:30:33 -0800259 @VisibleForTesting
260 void establishInitialState() {
Doris Ling1f497152018-02-22 16:33:07 -0800261 mInitiateButton = mContentView.findViewById(R.id.initiate_master_clear);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700262 mInitiateButton.setOnClickListener(mInitiateListener);
263 mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
Doris Ling1f497152018-02-22 16:33:07 -0800264 mExternalStorage = mContentView.findViewById(R.id.erase_external);
Qingxi Lic8a2b042018-01-11 11:35:07 -0800265 mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container);
Doris Ling1f497152018-02-22 16:33:07 -0800266 mEsimStorage = mContentView.findViewById(R.id.erase_esim);
267 if (mScrollView != null) {
268 mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
269 }
270 mScrollView = mContentView.findViewById(R.id.master_clear_scrollview);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800271
Kenny Root3785e392011-01-18 15:14:32 -0800272 /*
273 * If the external storage is emulated, it will be erased with a factory
274 * reset at any rate. There is no need to have a separate option until
275 * we have a factory reset that only erases some directories and not
Ben Komalo2a321922011-09-07 16:42:34 -0700276 * others. Likewise, if it's non-removable storage, it could potentially have been
277 * encrypted, and will also need to be wiped.
Kenny Root3785e392011-01-18 15:14:32 -0800278 */
Ben Komalo2a321922011-09-07 16:42:34 -0700279 boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
280 if (isExtStorageEmulated
281 || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
Kenny Root3785e392011-01-18 15:14:32 -0800282 mExternalStorageContainer.setVisibility(View.GONE);
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700283
Kenny Root3785e392011-01-18 15:14:32 -0800284 final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
285 externalOption.setVisibility(View.GONE);
286
287 final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
288 externalAlsoErased.setVisibility(View.VISIBLE);
Ben Komalo2a321922011-09-07 16:42:34 -0700289
290 // If it's not emulated, it is on a separate partition but it means we're doing
291 // a force wipe due to encryption.
292 mExternalStorage.setChecked(!isExtStorageEmulated);
Kenny Root3785e392011-01-18 15:14:32 -0800293 } else {
294 mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
295
296 @Override
297 public void onClick(View v) {
298 mExternalStorage.toggle();
299 }
300 });
301 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800302
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700303 if (showWipeEuicc()) {
Qingxi Lic8a2b042018-01-11 11:35:07 -0800304 if (showWipeEuiccCheckbox()) {
305 TextView title = mContentView.findViewById(R.id.erase_esim_title);
306 title.setText(R.string.erase_esim_storage);
307 mEsimStorageContainer.setVisibility(View.VISIBLE);
308 mEsimStorageContainer.setOnClickListener(new View.OnClickListener() {
309 @Override
310 public void onClick(View v) {
311 mEsimStorage.toggle();
312 }
313 });
314 } else {
315 final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim);
316 esimAlsoErased.setVisibility(View.VISIBLE);
qingxi072f2862017-04-11 18:28:40 -0700317
Qingxi Lic8a2b042018-01-11 11:35:07 -0800318 final View noCancelMobilePlan = mContentView.findViewById(
319 R.id.no_cancel_mobile_plan);
320 noCancelMobilePlan.setVisibility(View.VISIBLE);
321 mEsimStorage.setChecked(true /* checked */);
322 }
qingxi072f2862017-04-11 18:28:40 -0700323 }
324
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100325 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
326 loadAccountList(um);
Doris Ling1f497152018-02-22 16:33:07 -0800327 final StringBuffer contentDescription = new StringBuffer();
328 final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container);
Julia Reynoldsce25af42015-07-08 16:56:31 -0400329 getContentDescription(masterClearContainer, contentDescription);
330 masterClearContainer.setContentDescription(contentDescription);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800331
332 // Set the status of initiateButton based on scrollview
333 mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() {
334 @Override
335 public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX,
336 int oldScrollY) {
337 if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) {
338 mInitiateButton.setEnabled(true);
Doris Ling1f497152018-02-22 16:33:07 -0800339 mScrollView.setOnScrollChangeListener(null);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800340 }
341 }
342 });
343
344 // Set the initial state of the initiateButton
Doris Ling1f497152018-02-22 16:33:07 -0800345 mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this);
jackqdyulei0b3edc72016-12-13 17:07:08 -0800346 }
347
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700348 /**
Jeff Davidsona0b84722017-07-26 17:38:41 -0700349 * Whether to show strings indicating that the eUICC will be wiped.
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700350 *
Jeff Davidsona0b84722017-07-26 17:38:41 -0700351 * <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 -0700352 * provisioned (that is, at least one profile was ever downloaded onto it).
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700353 */
354 @VisibleForTesting
355 boolean showWipeEuicc() {
356 Context context = getContext();
357 if (!isEuiccEnabled(context)) {
358 return false;
359 }
360 ContentResolver cr = context.getContentResolver();
Qingxi Lie0681db2018-03-14 16:00:47 -0700361 return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
362 || Settings.Global.getInt(
363 cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700364 }
365
366 @VisibleForTesting
Qingxi Lic8a2b042018-01-11 11:35:07 -0800367 boolean showWipeEuiccCheckbox() {
368 return SystemProperties
369 .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */);
370 }
371
372 @VisibleForTesting
Jeff Davidsonf1c13fa2017-05-19 17:03:32 -0700373 protected boolean isEuiccEnabled(Context context) {
374 EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
375 return euiccManager.isEnabled();
376 }
377
jackqdyulei0b3edc72016-12-13 17:07:08 -0800378 @VisibleForTesting
379 boolean hasReachedBottom(final ScrollView scrollView) {
380 if (scrollView.getChildCount() < 1) {
381 return true;
382 }
383
384 final View view = scrollView.getChildAt(0);
385 final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY());
386
387 return diff <= 0;
Julia Reynoldsce25af42015-07-08 16:56:31 -0400388 }
389
390 private void getContentDescription(View v, StringBuffer description) {
Jason Monkfda77412016-05-06 14:28:09 -0400391 if (v.getVisibility() != View.VISIBLE) {
392 return;
393 }
Julia Reynoldsce25af42015-07-08 16:56:31 -0400394 if (v instanceof ViewGroup) {
395 ViewGroup vGroup = (ViewGroup) v;
396 for (int i = 0; i < vGroup.getChildCount(); i++) {
397 View nextChild = vGroup.getChildAt(i);
398 getContentDescription(nextChild, description);
399 }
400 } else if (v instanceof TextView) {
401 TextView vText = (TextView) v;
402 description.append(vText.getText());
403 description.append(","); // Allow Talkback to pause between sections.
404 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800405 }
406
Ben Komalo2a321922011-09-07 16:42:34 -0700407 private boolean isExtStorageEncrypted() {
408 String state = SystemProperties.get("vold.decrypt");
409 return !"".equals(state);
410 }
411
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100412 private void loadAccountList(final UserManager um) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800413 View accountsLabel = mContentView.findViewById(R.id.accounts_label);
414 LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
Amith Yamasani3f45de52011-09-22 14:34:17 -0700415 contents.removeAllViews();
Joe Onoratob51886d2010-11-08 18:25:51 -0800416
417 Context context = getActivity();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100418 final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
419 final int profilesSize = profiles.size();
Joe Onoratob51886d2010-11-08 18:25:51 -0800420
421 AccountManager mgr = AccountManager.get(context);
Joe Onoratob51886d2010-11-08 18:25:51 -0800422
423 LayoutInflater inflater = (LayoutInflater)context.getSystemService(
424 Context.LAYOUT_INFLATER_SERVICE);
425
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100426 int accountsCount = 0;
427 for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
428 final UserInfo userInfo = profiles.get(profileIndex);
429 final int profileId = userInfo.id;
430 final UserHandle userHandle = new UserHandle(profileId);
431 Account[] accounts = mgr.getAccountsAsUser(profileId);
432 final int N = accounts.length;
433 if (N == 0) {
Joe Onoratob51886d2010-11-08 18:25:51 -0800434 continue;
435 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100436 accountsCount += N;
Joe Onoratob51886d2010-11-08 18:25:51 -0800437
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100438 AuthenticatorDescription[] descs = AccountManager.get(context)
439 .getAuthenticatorTypesAsUser(profileId);
440 final int M = descs.length;
441
Bartosz Fabianowski30ba8682017-04-19 11:51:43 +0200442 if (profilesSize > 1) {
443 View titleView = Utils.inflateCategoryHeader(inflater, contents);
444 final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
445 titleText.setText(userInfo.isManagedProfile() ? R.string.category_work
446 : R.string.category_personal);
447 contents.addView(titleView);
448 }
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100449
450 for (int i = 0; i < N; i++) {
451 Account account = accounts[i];
452 AuthenticatorDescription desc = null;
453 for (int j = 0; j < M; j++) {
454 if (account.type.equals(descs[j].type)) {
455 desc = descs[j];
456 break;
457 }
458 }
459 if (desc == null) {
460 Log.w(TAG, "No descriptor for account name=" + account.name
461 + " type=" + account.type);
462 continue;
463 }
464 Drawable icon = null;
465 try {
466 if (desc.iconId != 0) {
Zoltan Szatmary-Ban546790c2014-12-02 17:22:10 +0000467 Context authContext = context.createPackageContextAsUser(desc.packageName,
468 0, userHandle);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100469 icon = context.getPackageManager().getUserBadgedIcon(
470 authContext.getDrawable(desc.iconId), userHandle);
471 }
472 } catch (PackageManager.NameNotFoundException e) {
Rubin Xud1ce82a2015-06-08 17:21:19 +0100473 Log.w(TAG, "Bad package name for account type " + desc.type);
474 } catch (Resources.NotFoundException e) {
475 Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
476 }
477 if (icon == null) {
478 icon = context.getPackageManager().getDefaultActivityIcon();
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100479 }
480
Fan Zhang6b2bb392016-09-28 09:07:44 -0700481 View child = inflater.inflate(R.layout.master_clear_account, contents, false);
482 ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
483 ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100484 contents.addView(child);
Joe Onoratob51886d2010-11-08 18:25:51 -0800485 }
Joe Onoratob51886d2010-11-08 18:25:51 -0800486 }
487
Zoltan Szatmary-Ban7cc1b9e2014-10-24 18:03:18 +0100488 if (accountsCount > 0) {
489 accountsLabel.setVisibility(View.VISIBLE);
490 contents.setVisibility(View.VISIBLE);
491 }
492 // Checking for all other users and their profiles if any.
493 View otherUsers = mContentView.findViewById(R.id.other_users_present);
494 final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
495 otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800496 }
497
498 @Override
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700499 public View onCreateView(LayoutInflater inflater, ViewGroup container,
500 Bundle savedInstanceState) {
Christine Franks14782222017-01-23 16:44:02 -0800501 final Context context = getContext();
502 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
503 UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
504 final UserManager um = UserManager.get(context);
505 final boolean disallow = !um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction(
506 context, UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId());
Christine Franksa09eb382017-06-23 14:28:21 -0700507 if (disallow && !Utils.isDemoUser(context)) {
Julia Reynolds2c539332014-06-11 12:56:02 -0400508 return inflater.inflate(R.layout.master_clear_disallowed_screen, null);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000509 } else if (admin != null) {
arangelove35badd2017-10-27 13:51:28 +0100510 new ActionDisabledByAdminDialogHelper(getActivity())
511 .prepareDialogBuilder(UserManager.DISALLOW_FACTORY_RESET, admin)
512 .setOnDismissListener(__ -> getActivity().finish())
513 .show();
514 return new View(getContext());
Julia Reynolds2c539332014-06-11 12:56:02 -0400515 }
516
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700517 mContentView = inflater.inflate(R.layout.master_clear, null);
Joe Onoratob51886d2010-11-08 18:25:51 -0800518
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800519 establishInitialState();
Amith Yamasanib14e1e02010-11-02 09:52:29 -0700520 return mContentView;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800521 }
Chris Wren8a963ba2015-03-20 10:29:14 -0400522
523 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700524 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500525 return MetricsEvent.MASTER_CLEAR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400526 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800527}