The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 19 | import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
| 20 | |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountManager; |
| 23 | import android.accounts.AuthenticatorDescription; |
Doris Ling | ed4685f | 2017-10-25 14:08:57 -0700 | [diff] [blame] | 24 | import android.annotation.Nullable; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 25 | import android.app.Activity; |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 28 | import android.content.Context; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 29 | import android.content.Intent; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 31 | import android.content.pm.ResolveInfo; |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 32 | import android.content.pm.UserInfo; |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 33 | import android.content.res.Resources; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 35 | import android.os.Bundle; |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 36 | import android.os.Environment; |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 37 | import android.os.SystemProperties; |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 38 | import android.os.UserHandle; |
Amith Yamasani | 7eedcf4 | 2013-07-02 14:15:29 -0700 | [diff] [blame] | 39 | import android.os.UserManager; |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 41 | import android.support.annotation.VisibleForTesting; |
qingxi | 072f286 | 2017-04-11 18:28:40 -0700 | [diff] [blame] | 42 | import android.telephony.euicc.EuiccManager; |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 43 | import android.text.TextUtils; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 44 | import android.util.Log; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 47 | import android.view.View.OnScrollChangeListener; |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 48 | import android.view.ViewGroup; |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 49 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 50 | import android.widget.Button; |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame] | 51 | import android.widget.CheckBox; |
Fan Zhang | 6b2bb39 | 2016-09-28 09:07:44 -0700 | [diff] [blame] | 52 | import android.widget.ImageView; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 53 | import android.widget.LinearLayout; |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 54 | import android.widget.ScrollView; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 55 | import android.widget.TextView; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 56 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 57 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Doris Ling | 7248972 | 2017-11-16 11:03:40 -0800 | [diff] [blame] | 58 | import com.android.settings.core.InstrumentedFragment; |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 59 | import com.android.settings.core.SubSettingLauncher; |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 60 | import com.android.settings.password.ChooseLockSettingsHelper; |
| 61 | import com.android.settings.password.ConfirmLockPattern; |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 62 | import com.android.settingslib.RestrictedLockUtils; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 63 | |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 64 | import java.util.List; |
| 65 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Confirm and execute a reset of the device to a clean "just out of the box" |
| 68 | * state. Multiple confirmations are required: first, a general "are you sure |
| 69 | * you want to do this?" prompt, followed by a keyguard pattern trace if the user |
| 70 | * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING |
| 71 | * ON THE PHONE" prompt. If at any time the phone is allowed to go to sleep, is |
| 72 | * locked, et cetera, then the confirmation sequence is abandoned. |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 73 | * |
| 74 | * This is the initial screen. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 75 | */ |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 76 | public class MasterClear extends InstrumentedFragment implements OnGlobalLayoutListener { |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 77 | private static final String TAG = "MasterClear"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 78 | |
Carlos Valdivia | 5dd6ed4 | 2018-01-23 09:12:59 -0800 | [diff] [blame] | 79 | @VisibleForTesting static final int KEYGUARD_REQUEST = 55; |
Carlos Valdivia | 08c8820 | 2018-01-21 18:57:28 -0800 | [diff] [blame] | 80 | @VisibleForTesting static final int CREDENTIAL_CONFIRM_REQUEST = 56; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 81 | |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 82 | private static final String KEY_SHOW_ESIM_RESET_CHECKBOX |
| 83 | = "masterclear.allow_retain_esim_profiles_after_fdr"; |
| 84 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 85 | static final String ERASE_EXTERNAL_EXTRA = "erase_sd"; |
qingxi | 072f286 | 2017-04-11 18:28:40 -0700 | [diff] [blame] | 86 | static final String ERASE_ESIMS_EXTRA = "erase_esim"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 87 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 88 | private View mContentView; |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 89 | @VisibleForTesting Button mInitiateButton; |
Dianne Hackborn | 1337d0f | 2010-10-14 11:58:30 -0700 | [diff] [blame] | 90 | private View mExternalStorageContainer; |
qingxi | 072f286 | 2017-04-11 18:28:40 -0700 | [diff] [blame] | 91 | @VisibleForTesting CheckBox mExternalStorage; |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 92 | private View mEsimStorageContainer; |
| 93 | @VisibleForTesting CheckBox mEsimStorage; |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 94 | @VisibleForTesting ScrollView mScrollView; |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 95 | |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 96 | @Override |
| 97 | public void onGlobalLayout() { |
| 98 | mInitiateButton.setEnabled(hasReachedBottom(mScrollView)); |
| 99 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 100 | |
Doris Ling | 03a3b51 | 2017-10-18 14:25:01 -0700 | [diff] [blame] | 101 | @Override |
Doris Ling | ed4685f | 2017-10-25 14:08:57 -0700 | [diff] [blame] | 102 | public void onCreate(@Nullable Bundle savedInstanceState) { |
| 103 | super.onCreate(savedInstanceState); |
Doris Ling | 4a01283 | 2017-11-13 17:58:13 -0800 | [diff] [blame] | 104 | getActivity().setTitle(R.string.master_clear_title); |
Doris Ling | 03a3b51 | 2017-10-18 14:25:01 -0700 | [diff] [blame] | 105 | } |
| 106 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 107 | /** |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 108 | * Keyguard validation is run using the standard {@link ConfirmLockPattern} |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 109 | * component as a subactivity |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 110 | * @param request the request code to be returned once confirmation finishes |
| 111 | * @return true if confirmation launched |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 112 | */ |
Jim Miller | 2deec7e | 2010-04-13 17:43:36 -0700 | [diff] [blame] | 113 | private boolean runKeyguardConfirmation(int request) { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 114 | Resources res = getActivity().getResources(); |
Jorim Jaggi | 8a09b61 | 2015-04-06 17:47:18 -0700 | [diff] [blame] | 115 | return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity( |
| 116 | request, res.getText(R.string.master_clear_title)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Carlos Valdivia | 5dd6ed4 | 2018-01-23 09:12:59 -0800 | [diff] [blame] | 119 | @VisibleForTesting |
| 120 | boolean isValidRequestCode(int requestCode) { |
| 121 | return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST)); |
| 122 | } |
| 123 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 124 | @Override |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 125 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 126 | super.onActivityResult(requestCode, resultCode, data); |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 127 | onActivityResultInternal(requestCode, resultCode, data); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Internal method that allows easy testing without dealing with super references. |
| 132 | */ |
| 133 | @VisibleForTesting |
| 134 | void onActivityResultInternal(int requestCode, int resultCode, Intent data) { |
Carlos Valdivia | 5dd6ed4 | 2018-01-23 09:12:59 -0800 | [diff] [blame] | 135 | if (!isValidRequestCode(requestCode)) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 139 | if (resultCode != Activity.RESULT_OK) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 140 | establishInitialState(); |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | Intent intent = null; |
| 145 | // If returning from a Keyguard request, try to show an account confirmation request if |
| 146 | // applciable. |
| 147 | if (CREDENTIAL_CONFIRM_REQUEST != requestCode |
| 148 | && (intent = getAccountConfirmationIntent()) != null) { |
| 149 | showAccountCredentialConfirmation(intent); |
| 150 | } else { |
| 151 | showFinalConfirmation(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
qingxi | 072f286 | 2017-04-11 18:28:40 -0700 | [diff] [blame] | 155 | @VisibleForTesting |
| 156 | void showFinalConfirmation() { |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 157 | final Bundle args = new Bundle(); |
Stuart Scott | be90341 | 2014-07-24 19:22:06 -0700 | [diff] [blame] | 158 | args.putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked()); |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 159 | args.putBoolean(ERASE_ESIMS_EXTRA, mEsimStorage.isChecked()); |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 160 | new SubSettingLauncher(getContext()) |
| 161 | .setDestination(MasterClearConfirm.class.getName()) |
| 162 | .setArguments(args) |
| 163 | .setTitle(R.string.master_clear_confirm_title) |
| 164 | .setSourceMetricsCategory(getMetricsCategory()) |
| 165 | .launch(); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 168 | @VisibleForTesting |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 169 | void showAccountCredentialConfirmation(Intent intent) { |
| 170 | startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST); |
| 171 | } |
| 172 | |
| 173 | @VisibleForTesting |
| 174 | Intent getAccountConfirmationIntent() { |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 175 | final Context context = getActivity(); |
| 176 | final String accountType = context.getString(R.string.account_type); |
| 177 | final String packageName = context.getString(R.string.account_confirmation_package); |
Carlos Valdivia | 5114604 | 2018-01-23 17:17:56 -0800 | [diff] [blame] | 178 | final String className = context.getString(R.string.account_confirmation_class); |
| 179 | if (TextUtils.isEmpty(accountType) |
| 180 | || TextUtils.isEmpty(packageName) |
| 181 | || TextUtils.isEmpty(className)) { |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 182 | Log.i(TAG, "Resources not set for account confirmation."); |
| 183 | return null; |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 184 | } |
| 185 | final AccountManager am = AccountManager.get(context); |
| 186 | Account[] accounts = am.getAccountsByType(accountType); |
| 187 | if (accounts != null && accounts.length > 0) { |
| 188 | final Intent requestAccountConfirmation = new Intent() |
| 189 | .setPackage(packageName) |
Carlos Valdivia | 5114604 | 2018-01-23 17:17:56 -0800 | [diff] [blame] | 190 | .setComponent(new ComponentName(packageName, className)); |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 191 | // Check to make sure that the intent is supported. |
| 192 | final PackageManager pm = context.getPackageManager(); |
Carlos Valdivia | 08c8820 | 2018-01-21 18:57:28 -0800 | [diff] [blame] | 193 | final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0); |
| 194 | if (resolution != null |
| 195 | && resolution.activityInfo != null |
| 196 | && packageName.equals(resolution.activityInfo.packageName)) { |
| 197 | // Note that we need to check the packagename to make sure that an Activity resolver |
| 198 | // wasn't returned. |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 199 | return requestAccountConfirmation; |
| 200 | } else { |
| 201 | Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className); |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 202 | } |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 203 | } else { |
| 204 | Log.d(TAG, "No " + accountType + " accounts installed!"); |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 205 | } |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 206 | return null; |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 207 | } |
| 208 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 209 | /** |
| 210 | * If the user clicks to begin the reset sequence, we next require a |
| 211 | * keyguard confirmation if the user has currently enabled one. If there |
| 212 | * is no keyguard available, we simply go to the final confirmation prompt. |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 213 | * |
| 214 | * If the user is in demo mode, route to the demo mode app for confirmation. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 215 | */ |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 216 | @VisibleForTesting |
| 217 | protected final Button.OnClickListener mInitiateListener = new Button.OnClickListener() { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 218 | |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 219 | public void onClick(View view) { |
| 220 | final Context context = view.getContext(); |
| 221 | if (Utils.isDemoUser(context)) { |
Justin Klaassen | 3025727 | 2017-08-08 21:58:05 -0700 | [diff] [blame] | 222 | final ComponentName componentName = Utils.getDeviceOwnerComponent(context); |
| 223 | if (componentName != null) { |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 224 | final Intent requestFactoryReset = new Intent() |
Justin Klaassen | 3025727 | 2017-08-08 21:58:05 -0700 | [diff] [blame] | 225 | .setPackage(componentName.getPackageName()) |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 226 | .setAction(Intent.ACTION_FACTORY_RESET); |
| 227 | context.startActivity(requestFactoryReset); |
Christine Franks | d7713c9 | 2017-01-20 11:24:14 -0800 | [diff] [blame] | 228 | } |
Carlos Valdivia | a9736ed | 2018-01-16 13:59:31 -0800 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 232 | if (runKeyguardConfirmation(KEYGUARD_REQUEST)) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | Intent intent = getAccountConfirmationIntent(); |
| 237 | if (intent != null) { |
| 238 | showAccountCredentialConfirmation(intent); |
| 239 | } else { |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 240 | showFinalConfirmation(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 241 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 242 | } |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 243 | }; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * In its initial state, the activity presents a button for the user to |
| 247 | * click in order to initiate a confirmation sequence. This method is |
| 248 | * called from various other points in the code to reset the activity to |
| 249 | * this base state. |
Jim Miller | 47d380f | 2010-01-20 13:37:14 -0800 | [diff] [blame] | 250 | * |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 251 | * <p>Reinflating views from resources is expensive and prevents us from |
| 252 | * caching widget pointers, so we use a single-inflate pattern: we lazy- |
| 253 | * inflate each view, caching all of the widget pointers we'll need at the |
| 254 | * time, then simply reuse the inflated views directly whenever we need |
| 255 | * to change contents. |
| 256 | */ |
Carlos Valdivia | 4b34dad | 2018-01-29 18:30:33 -0800 | [diff] [blame] | 257 | @VisibleForTesting |
| 258 | void establishInitialState() { |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 259 | mInitiateButton = mContentView.findViewById(R.id.initiate_master_clear); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 260 | mInitiateButton.setOnClickListener(mInitiateListener); |
| 261 | mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container); |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 262 | mExternalStorage = mContentView.findViewById(R.id.erase_external); |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 263 | mEsimStorageContainer = mContentView.findViewById(R.id.erase_esim_container); |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 264 | mEsimStorage = mContentView.findViewById(R.id.erase_esim); |
| 265 | if (mScrollView != null) { |
| 266 | mScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 267 | } |
| 268 | mScrollView = mContentView.findViewById(R.id.master_clear_scrollview); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 269 | |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 270 | /* |
| 271 | * If the external storage is emulated, it will be erased with a factory |
| 272 | * reset at any rate. There is no need to have a separate option until |
| 273 | * we have a factory reset that only erases some directories and not |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 274 | * others. Likewise, if it's non-removable storage, it could potentially have been |
| 275 | * encrypted, and will also need to be wiped. |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 276 | */ |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 277 | boolean isExtStorageEmulated = Environment.isExternalStorageEmulated(); |
| 278 | if (isExtStorageEmulated |
| 279 | || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) { |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 280 | mExternalStorageContainer.setVisibility(View.GONE); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 281 | |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 282 | final View externalOption = mContentView.findViewById(R.id.erase_external_option_text); |
| 283 | externalOption.setVisibility(View.GONE); |
| 284 | |
| 285 | final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external); |
| 286 | externalAlsoErased.setVisibility(View.VISIBLE); |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 287 | |
| 288 | // If it's not emulated, it is on a separate partition but it means we're doing |
| 289 | // a force wipe due to encryption. |
| 290 | mExternalStorage.setChecked(!isExtStorageEmulated); |
Kenny Root | 3785e39 | 2011-01-18 15:14:32 -0800 | [diff] [blame] | 291 | } else { |
| 292 | mExternalStorageContainer.setOnClickListener(new View.OnClickListener() { |
| 293 | |
| 294 | @Override |
| 295 | public void onClick(View v) { |
| 296 | mExternalStorage.toggle(); |
| 297 | } |
| 298 | }); |
| 299 | } |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 300 | |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 301 | if (showWipeEuicc()) { |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 302 | if (showWipeEuiccCheckbox()) { |
| 303 | TextView title = mContentView.findViewById(R.id.erase_esim_title); |
| 304 | title.setText(R.string.erase_esim_storage); |
| 305 | mEsimStorageContainer.setVisibility(View.VISIBLE); |
| 306 | mEsimStorageContainer.setOnClickListener(new View.OnClickListener() { |
| 307 | @Override |
| 308 | public void onClick(View v) { |
| 309 | mEsimStorage.toggle(); |
| 310 | } |
| 311 | }); |
| 312 | } else { |
| 313 | final View esimAlsoErased = mContentView.findViewById(R.id.also_erases_esim); |
| 314 | esimAlsoErased.setVisibility(View.VISIBLE); |
qingxi | 072f286 | 2017-04-11 18:28:40 -0700 | [diff] [blame] | 315 | |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 316 | final View noCancelMobilePlan = mContentView.findViewById( |
| 317 | R.id.no_cancel_mobile_plan); |
| 318 | noCancelMobilePlan.setVisibility(View.VISIBLE); |
| 319 | mEsimStorage.setChecked(true /* checked */); |
| 320 | } |
qingxi | 072f286 | 2017-04-11 18:28:40 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 323 | final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); |
| 324 | loadAccountList(um); |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 325 | final StringBuffer contentDescription = new StringBuffer(); |
| 326 | final View masterClearContainer = mContentView.findViewById(R.id.master_clear_container); |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 327 | getContentDescription(masterClearContainer, contentDescription); |
| 328 | masterClearContainer.setContentDescription(contentDescription); |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 329 | |
| 330 | // Set the status of initiateButton based on scrollview |
| 331 | mScrollView.setOnScrollChangeListener(new OnScrollChangeListener() { |
| 332 | @Override |
| 333 | public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, |
| 334 | int oldScrollY) { |
| 335 | if (v instanceof ScrollView && hasReachedBottom((ScrollView) v)) { |
| 336 | mInitiateButton.setEnabled(true); |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 337 | mScrollView.setOnScrollChangeListener(null); |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | }); |
| 341 | |
| 342 | // Set the initial state of the initiateButton |
Doris Ling | 1f49715 | 2018-02-22 16:33:07 -0800 | [diff] [blame] | 343 | mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this); |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 346 | /** |
Jeff Davidson | a0b8472 | 2017-07-26 17:38:41 -0700 | [diff] [blame] | 347 | * Whether to show strings indicating that the eUICC will be wiped. |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 348 | * |
Jeff Davidson | a0b8472 | 2017-07-26 17:38:41 -0700 | [diff] [blame] | 349 | * <p>We show the strings on any device which supports eUICC as long as the eUICC was ever |
Jeff Davidson | 4ba7478 | 2017-06-30 18:02:13 -0700 | [diff] [blame] | 350 | * provisioned (that is, at least one profile was ever downloaded onto it). |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 351 | */ |
| 352 | @VisibleForTesting |
| 353 | boolean showWipeEuicc() { |
| 354 | Context context = getContext(); |
| 355 | if (!isEuiccEnabled(context)) { |
| 356 | return false; |
| 357 | } |
| 358 | ContentResolver cr = context.getContentResolver(); |
Jeff Davidson | 4ba7478 | 2017-06-30 18:02:13 -0700 | [diff] [blame] | 359 | return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0; |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @VisibleForTesting |
Qingxi Li | c8a2b04 | 2018-01-11 11:35:07 -0800 | [diff] [blame] | 363 | boolean showWipeEuiccCheckbox() { |
| 364 | return SystemProperties |
| 365 | .getBoolean(KEY_SHOW_ESIM_RESET_CHECKBOX, false /* def */); |
| 366 | } |
| 367 | |
| 368 | @VisibleForTesting |
Jeff Davidson | f1c13fa | 2017-05-19 17:03:32 -0700 | [diff] [blame] | 369 | protected boolean isEuiccEnabled(Context context) { |
| 370 | EuiccManager euiccManager = (EuiccManager) context.getSystemService(Context.EUICC_SERVICE); |
| 371 | return euiccManager.isEnabled(); |
| 372 | } |
| 373 | |
jackqdyulei | 0b3edc7 | 2016-12-13 17:07:08 -0800 | [diff] [blame] | 374 | @VisibleForTesting |
| 375 | boolean hasReachedBottom(final ScrollView scrollView) { |
| 376 | if (scrollView.getChildCount() < 1) { |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | final View view = scrollView.getChildAt(0); |
| 381 | final int diff = view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()); |
| 382 | |
| 383 | return diff <= 0; |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | private void getContentDescription(View v, StringBuffer description) { |
Jason Monk | fda7741 | 2016-05-06 14:28:09 -0400 | [diff] [blame] | 387 | if (v.getVisibility() != View.VISIBLE) { |
| 388 | return; |
| 389 | } |
Julia Reynolds | ce25af4 | 2015-07-08 16:56:31 -0400 | [diff] [blame] | 390 | if (v instanceof ViewGroup) { |
| 391 | ViewGroup vGroup = (ViewGroup) v; |
| 392 | for (int i = 0; i < vGroup.getChildCount(); i++) { |
| 393 | View nextChild = vGroup.getChildAt(i); |
| 394 | getContentDescription(nextChild, description); |
| 395 | } |
| 396 | } else if (v instanceof TextView) { |
| 397 | TextView vText = (TextView) v; |
| 398 | description.append(vText.getText()); |
| 399 | description.append(","); // Allow Talkback to pause between sections. |
| 400 | } |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Ben Komalo | 2a32192 | 2011-09-07 16:42:34 -0700 | [diff] [blame] | 403 | private boolean isExtStorageEncrypted() { |
| 404 | String state = SystemProperties.get("vold.decrypt"); |
| 405 | return !"".equals(state); |
| 406 | } |
| 407 | |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 408 | private void loadAccountList(final UserManager um) { |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 409 | View accountsLabel = mContentView.findViewById(R.id.accounts_label); |
| 410 | LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts); |
Amith Yamasani | 3f45de5 | 2011-09-22 14:34:17 -0700 | [diff] [blame] | 411 | contents.removeAllViews(); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 412 | |
| 413 | Context context = getActivity(); |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 414 | final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId()); |
| 415 | final int profilesSize = profiles.size(); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 416 | |
| 417 | AccountManager mgr = AccountManager.get(context); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 418 | |
| 419 | LayoutInflater inflater = (LayoutInflater)context.getSystemService( |
| 420 | Context.LAYOUT_INFLATER_SERVICE); |
| 421 | |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 422 | int accountsCount = 0; |
| 423 | for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) { |
| 424 | final UserInfo userInfo = profiles.get(profileIndex); |
| 425 | final int profileId = userInfo.id; |
| 426 | final UserHandle userHandle = new UserHandle(profileId); |
| 427 | Account[] accounts = mgr.getAccountsAsUser(profileId); |
| 428 | final int N = accounts.length; |
| 429 | if (N == 0) { |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 430 | continue; |
| 431 | } |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 432 | accountsCount += N; |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 433 | |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 434 | AuthenticatorDescription[] descs = AccountManager.get(context) |
| 435 | .getAuthenticatorTypesAsUser(profileId); |
| 436 | final int M = descs.length; |
| 437 | |
Bartosz Fabianowski | 30ba868 | 2017-04-19 11:51:43 +0200 | [diff] [blame] | 438 | if (profilesSize > 1) { |
| 439 | View titleView = Utils.inflateCategoryHeader(inflater, contents); |
| 440 | final TextView titleText = (TextView) titleView.findViewById(android.R.id.title); |
| 441 | titleText.setText(userInfo.isManagedProfile() ? R.string.category_work |
| 442 | : R.string.category_personal); |
| 443 | contents.addView(titleView); |
| 444 | } |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 445 | |
| 446 | for (int i = 0; i < N; i++) { |
| 447 | Account account = accounts[i]; |
| 448 | AuthenticatorDescription desc = null; |
| 449 | for (int j = 0; j < M; j++) { |
| 450 | if (account.type.equals(descs[j].type)) { |
| 451 | desc = descs[j]; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | if (desc == null) { |
| 456 | Log.w(TAG, "No descriptor for account name=" + account.name |
| 457 | + " type=" + account.type); |
| 458 | continue; |
| 459 | } |
| 460 | Drawable icon = null; |
| 461 | try { |
| 462 | if (desc.iconId != 0) { |
Zoltan Szatmary-Ban | 546790c | 2014-12-02 17:22:10 +0000 | [diff] [blame] | 463 | Context authContext = context.createPackageContextAsUser(desc.packageName, |
| 464 | 0, userHandle); |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 465 | icon = context.getPackageManager().getUserBadgedIcon( |
| 466 | authContext.getDrawable(desc.iconId), userHandle); |
| 467 | } |
| 468 | } catch (PackageManager.NameNotFoundException e) { |
Rubin Xu | d1ce82a | 2015-06-08 17:21:19 +0100 | [diff] [blame] | 469 | Log.w(TAG, "Bad package name for account type " + desc.type); |
| 470 | } catch (Resources.NotFoundException e) { |
| 471 | Log.w(TAG, "Invalid icon id for account type " + desc.type, e); |
| 472 | } |
| 473 | if (icon == null) { |
| 474 | icon = context.getPackageManager().getDefaultActivityIcon(); |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Fan Zhang | 6b2bb39 | 2016-09-28 09:07:44 -0700 | [diff] [blame] | 477 | View child = inflater.inflate(R.layout.master_clear_account, contents, false); |
| 478 | ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon); |
| 479 | ((TextView) child.findViewById(android.R.id.title)).setText(account.name); |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 480 | contents.addView(child); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 481 | } |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Zoltan Szatmary-Ban | 7cc1b9e | 2014-10-24 18:03:18 +0100 | [diff] [blame] | 484 | if (accountsCount > 0) { |
| 485 | accountsLabel.setVisibility(View.VISIBLE); |
| 486 | contents.setVisibility(View.VISIBLE); |
| 487 | } |
| 488 | // Checking for all other users and their profiles if any. |
| 489 | View otherUsers = mContentView.findViewById(R.id.other_users_present); |
| 490 | final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0; |
| 491 | otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | @Override |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 495 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 496 | Bundle savedInstanceState) { |
Christine Franks | 1478222 | 2017-01-23 16:44:02 -0800 | [diff] [blame] | 497 | final Context context = getContext(); |
| 498 | final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(context, |
| 499 | UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId()); |
| 500 | final UserManager um = UserManager.get(context); |
| 501 | final boolean disallow = !um.isAdminUser() || RestrictedLockUtils.hasBaseUserRestriction( |
| 502 | context, UserManager.DISALLOW_FACTORY_RESET, UserHandle.myUserId()); |
Christine Franks | a09eb38 | 2017-06-23 14:28:21 -0700 | [diff] [blame] | 503 | if (disallow && !Utils.isDemoUser(context)) { |
Julia Reynolds | 2c53933 | 2014-06-11 12:56:02 -0400 | [diff] [blame] | 504 | return inflater.inflate(R.layout.master_clear_disallowed_screen, null); |
Sudheer Shanka | 7dbbe13 | 2016-02-16 14:19:32 +0000 | [diff] [blame] | 505 | } else if (admin != null) { |
| 506 | View view = inflater.inflate(R.layout.admin_support_details_empty_view, null); |
| 507 | ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false); |
| 508 | view.setVisibility(View.VISIBLE); |
| 509 | return view; |
Julia Reynolds | 2c53933 | 2014-06-11 12:56:02 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 512 | mContentView = inflater.inflate(R.layout.master_clear, null); |
Joe Onorato | b51886d | 2010-11-08 18:25:51 -0800 | [diff] [blame] | 513 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 514 | establishInitialState(); |
Amith Yamasani | b14e1e0 | 2010-11-02 09:52:29 -0700 | [diff] [blame] | 515 | return mContentView; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 516 | } |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 517 | |
| 518 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 519 | public int getMetricsCategory() { |
Chris Wren | 9d1bfd1 | 2016-01-26 18:04:01 -0500 | [diff] [blame] | 520 | return MetricsEvent.MASTER_CLEAR; |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 521 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 522 | } |