Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.systemui.biometrics; |
| 18 | |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.app.admin.DevicePolicyManager; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 22 | import android.content.pm.UserInfo; |
Alex Johnston | 5179ac6 | 2020-01-07 14:23:26 +0000 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 24 | import android.hardware.biometrics.BiometricPrompt; |
| 25 | import android.os.AsyncTask; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.CountDownTimer; |
| 28 | import android.os.Handler; |
| 29 | import android.os.Looper; |
| 30 | import android.os.SystemClock; |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 31 | import android.os.UserManager; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
| 33 | import android.util.AttributeSet; |
| 34 | import android.view.View; |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 35 | import android.view.WindowManager; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityManager; |
Alex Johnston | 5179ac6 | 2020-01-07 14:23:26 +0000 | [diff] [blame] | 37 | import android.widget.ImageView; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 38 | import android.widget.LinearLayout; |
| 39 | import android.widget.TextView; |
| 40 | |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 41 | import androidx.annotation.IntDef; |
| 42 | import androidx.annotation.NonNull; |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 43 | import androidx.annotation.Nullable; |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 44 | import androidx.annotation.StringRes; |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 45 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 46 | import com.android.internal.widget.LockPatternUtils; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 47 | import com.android.systemui.Interpolators; |
| 48 | import com.android.systemui.R; |
| 49 | |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 50 | import java.lang.annotation.Retention; |
| 51 | import java.lang.annotation.RetentionPolicy; |
| 52 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 53 | /** |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 54 | * Abstract base class for Pin, Pattern, or Password authentication, for |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 55 | * {@link BiometricPrompt.Builder#setAllowedAuthenticators(int)}} |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 56 | */ |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 57 | public abstract class AuthCredentialView extends LinearLayout { |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 58 | private static final String TAG = "BiometricPrompt/AuthCredentialView"; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 59 | private static final int ERROR_DURATION_MS = 3000; |
| 60 | |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 61 | static final int USER_TYPE_PRIMARY = 1; |
| 62 | static final int USER_TYPE_MANAGED_PROFILE = 2; |
| 63 | static final int USER_TYPE_SECONDARY = 3; |
| 64 | @Retention(RetentionPolicy.SOURCE) |
| 65 | @IntDef({USER_TYPE_PRIMARY, USER_TYPE_MANAGED_PROFILE, USER_TYPE_SECONDARY}) |
| 66 | private @interface UserType {} |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 67 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 68 | protected final Handler mHandler; |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 69 | protected final LockPatternUtils mLockPatternUtils; |
| 70 | |
| 71 | private final AccessibilityManager mAccessibilityManager; |
| 72 | private final UserManager mUserManager; |
| 73 | private final DevicePolicyManager mDevicePolicyManager; |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 74 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 75 | private Bundle mBiometricPromptBundle; |
Kevin Chyn | 8cbb488 | 2019-09-19 16:49:02 -0700 | [diff] [blame] | 76 | private AuthPanelController mPanelController; |
| 77 | private boolean mShouldAnimatePanel; |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 78 | private boolean mShouldAnimateContents; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 79 | |
| 80 | private TextView mTitleView; |
| 81 | private TextView mSubtitleView; |
| 82 | private TextView mDescriptionView; |
Alex Johnston | 5179ac6 | 2020-01-07 14:23:26 +0000 | [diff] [blame] | 83 | private ImageView mIconView; |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 84 | protected TextView mErrorView; |
| 85 | |
| 86 | protected @Utils.CredentialType int mCredentialType; |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 87 | protected AuthContainerView mContainerView; |
| 88 | protected Callback mCallback; |
| 89 | protected AsyncTask<?, ?, ?> mPendingLockCheck; |
Kevin Chyn | bc3347f | 2020-02-20 16:45:59 -0800 | [diff] [blame] | 90 | protected int mUserId; |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 91 | protected long mOperationId; |
Kevin Chyn | b54bdfc | 2020-01-17 16:07:00 -0800 | [diff] [blame] | 92 | protected int mEffectiveUserId; |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 93 | protected ErrorTimer mErrorTimer; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 94 | |
| 95 | interface Callback { |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 96 | void onCredentialMatched(byte[] attestation); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 99 | protected static class ErrorTimer extends CountDownTimer { |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 100 | private final TextView mErrorView; |
| 101 | private final Context mContext; |
| 102 | |
| 103 | /** |
| 104 | * @param millisInFuture The number of millis in the future from the call |
| 105 | * to {@link #start()} until the countdown is done and {@link |
| 106 | * #onFinish()} |
| 107 | * is called. |
| 108 | * @param countDownInterval The interval along the way to receive |
| 109 | * {@link #onTick(long)} callbacks. |
| 110 | */ |
| 111 | public ErrorTimer(Context context, long millisInFuture, long countDownInterval, |
| 112 | TextView errorView) { |
| 113 | super(millisInFuture, countDownInterval); |
| 114 | mErrorView = errorView; |
| 115 | mContext = context; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onTick(long millisUntilFinished) { |
| 120 | final int secondsCountdown = (int) (millisUntilFinished / 1000); |
| 121 | mErrorView.setText(mContext.getString( |
| 122 | R.string.biometric_dialog_credential_too_many_attempts, secondsCountdown)); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onFinish() { |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 127 | if (mErrorView != null) { |
| 128 | mErrorView.setText(""); |
| 129 | } |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 133 | protected final Runnable mClearErrorRunnable = new Runnable() { |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public void run() { |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 136 | if (mErrorView != null) { |
| 137 | mErrorView.setText(""); |
| 138 | } |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | |
| 142 | public AuthCredentialView(Context context, AttributeSet attrs) { |
| 143 | super(context, attrs); |
| 144 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 145 | mLockPatternUtils = new LockPatternUtils(mContext); |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 146 | mHandler = new Handler(Looper.getMainLooper()); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 147 | mAccessibilityManager = mContext.getSystemService(AccessibilityManager.class); |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 148 | mUserManager = mContext.getSystemService(UserManager.class); |
| 149 | mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 152 | protected void showError(String error) { |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 153 | if (mHandler != null) { |
| 154 | mHandler.removeCallbacks(mClearErrorRunnable); |
| 155 | mHandler.postDelayed(mClearErrorRunnable, ERROR_DURATION_MS); |
| 156 | } |
| 157 | if (mErrorView != null) { |
| 158 | mErrorView.setText(error); |
| 159 | } |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 162 | private void setTextOrHide(TextView view, CharSequence text) { |
| 163 | if (TextUtils.isEmpty(text)) { |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 164 | view.setVisibility(View.GONE); |
| 165 | } else { |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 166 | view.setText(text); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | Utils.notifyAccessibilityContentChanged(mAccessibilityManager, this); |
| 170 | } |
| 171 | |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 172 | private void setText(TextView view, CharSequence text) { |
| 173 | view.setText(text); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Kevin Chyn | bc3347f | 2020-02-20 16:45:59 -0800 | [diff] [blame] | 176 | void setUserId(int userId) { |
| 177 | mUserId = userId; |
| 178 | } |
| 179 | |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 180 | void setOperationId(long operationId) { |
| 181 | mOperationId = operationId; |
| 182 | } |
| 183 | |
Kevin Chyn | b54bdfc | 2020-01-17 16:07:00 -0800 | [diff] [blame] | 184 | void setEffectiveUserId(int effectiveUserId) { |
| 185 | mEffectiveUserId = effectiveUserId; |
| 186 | } |
| 187 | |
| 188 | void setCredentialType(@Utils.CredentialType int credentialType) { |
| 189 | mCredentialType = credentialType; |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void setCallback(Callback callback) { |
| 193 | mCallback = callback; |
| 194 | } |
| 195 | |
| 196 | void setBiometricPromptBundle(Bundle bundle) { |
| 197 | mBiometricPromptBundle = bundle; |
| 198 | } |
| 199 | |
Kevin Chyn | 8cbb488 | 2019-09-19 16:49:02 -0700 | [diff] [blame] | 200 | void setPanelController(AuthPanelController panelController, boolean animatePanel) { |
| 201 | mPanelController = panelController; |
| 202 | mShouldAnimatePanel = animatePanel; |
| 203 | } |
| 204 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 205 | void setShouldAnimateContents(boolean animateContents) { |
| 206 | mShouldAnimateContents = animateContents; |
| 207 | } |
| 208 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 209 | void setContainerView(AuthContainerView containerView) { |
| 210 | mContainerView = containerView; |
| 211 | } |
| 212 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 213 | @Override |
| 214 | protected void onAttachedToWindow() { |
| 215 | super.onAttachedToWindow(); |
| 216 | |
Curtis Belmonte | da9204a | 2020-04-03 11:51:10 -0700 | [diff] [blame] | 217 | final CharSequence title = getTitle(mBiometricPromptBundle); |
| 218 | setText(mTitleView, title); |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 219 | setTextOrHide(mSubtitleView, getSubtitle(mBiometricPromptBundle)); |
| 220 | setTextOrHide(mDescriptionView, getDescription(mBiometricPromptBundle)); |
Curtis Belmonte | da9204a | 2020-04-03 11:51:10 -0700 | [diff] [blame] | 221 | announceForAccessibility(title); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 222 | |
Alex Johnston | 5179ac6 | 2020-01-07 14:23:26 +0000 | [diff] [blame] | 223 | final boolean isManagedProfile = Utils.isManagedProfile(mContext, mEffectiveUserId); |
| 224 | final Drawable image; |
| 225 | if (isManagedProfile) { |
| 226 | image = getResources().getDrawable(R.drawable.auth_dialog_enterprise, |
| 227 | mContext.getTheme()); |
| 228 | } else { |
| 229 | image = getResources().getDrawable(R.drawable.auth_dialog_lock, mContext.getTheme()); |
| 230 | } |
| 231 | mIconView.setImageDrawable(image); |
| 232 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 233 | // Only animate this if we're transitioning from a biometric view. |
| 234 | if (mShouldAnimateContents) { |
| 235 | setTranslationY(getResources() |
| 236 | .getDimension(R.dimen.biometric_dialog_credential_translation_offset)); |
| 237 | setAlpha(0); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 238 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 239 | postOnAnimation(() -> { |
| 240 | animate().translationY(0) |
| 241 | .setDuration(AuthDialog.ANIMATE_CREDENTIAL_INITIAL_DURATION_MS) |
| 242 | .alpha(1.f) |
| 243 | .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN) |
| 244 | .withLayer() |
| 245 | .start(); |
| 246 | }); |
| 247 | } |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
| 251 | protected void onDetachedFromWindow() { |
| 252 | super.onDetachedFromWindow(); |
| 253 | if (mErrorTimer != null) { |
| 254 | mErrorTimer.cancel(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | protected void onFinishInflate() { |
| 260 | super.onFinishInflate(); |
| 261 | mTitleView = findViewById(R.id.title); |
| 262 | mSubtitleView = findViewById(R.id.subtitle); |
| 263 | mDescriptionView = findViewById(R.id.description); |
Alex Johnston | 5179ac6 | 2020-01-07 14:23:26 +0000 | [diff] [blame] | 264 | mIconView = findViewById(R.id.icon); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 265 | mErrorView = findViewById(R.id.error); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Kevin Chyn | 8cbb488 | 2019-09-19 16:49:02 -0700 | [diff] [blame] | 268 | @Override |
| 269 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 270 | super.onLayout(changed, left, top, right, bottom); |
| 271 | |
| 272 | if (mShouldAnimatePanel) { |
| 273 | // Credential view is always full screen. |
| 274 | mPanelController.setUseFullScreen(true); |
| 275 | mPanelController.updateForContentDimensions(mPanelController.getContainerWidth(), |
| 276 | mPanelController.getContainerHeight(), 0 /* animateDurationMs */); |
| 277 | mShouldAnimatePanel = false; |
| 278 | } |
| 279 | } |
| 280 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 281 | protected void onErrorTimeoutFinish() {} |
| 282 | |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 283 | protected void onCredentialVerified(byte[] attestation, int timeoutMs) { |
| 284 | |
| 285 | final boolean matched = attestation != null; |
| 286 | |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 287 | if (matched) { |
| 288 | mClearErrorRunnable.run(); |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 289 | mCallback.onCredentialMatched(attestation); |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 290 | } else { |
| 291 | if (timeoutMs > 0) { |
| 292 | mHandler.removeCallbacks(mClearErrorRunnable); |
Kevin Chyn | b54bdfc | 2020-01-17 16:07:00 -0800 | [diff] [blame] | 293 | long deadline = mLockPatternUtils.setLockoutAttemptDeadline( |
| 294 | mEffectiveUserId, timeoutMs); |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 295 | mErrorTimer = new ErrorTimer(mContext, |
| 296 | deadline - SystemClock.elapsedRealtime(), |
| 297 | LockPatternUtils.FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS, |
| 298 | mErrorView) { |
| 299 | @Override |
| 300 | public void onFinish() { |
| 301 | onErrorTimeoutFinish(); |
| 302 | mClearErrorRunnable.run(); |
| 303 | } |
| 304 | }; |
| 305 | mErrorTimer.start(); |
| 306 | } else { |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 307 | final boolean didUpdateErrorText = reportFailedAttempt(); |
| 308 | if (!didUpdateErrorText) { |
| 309 | final @StringRes int errorRes; |
| 310 | switch (mCredentialType) { |
| 311 | case Utils.CREDENTIAL_PIN: |
| 312 | errorRes = R.string.biometric_dialog_wrong_pin; |
| 313 | break; |
| 314 | case Utils.CREDENTIAL_PATTERN: |
| 315 | errorRes = R.string.biometric_dialog_wrong_pattern; |
| 316 | break; |
| 317 | case Utils.CREDENTIAL_PASSWORD: |
| 318 | default: |
| 319 | errorRes = R.string.biometric_dialog_wrong_password; |
| 320 | break; |
| 321 | } |
| 322 | showError(getResources().getString(errorRes)); |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 323 | } |
Kevin Chyn | 8110ebb | 2019-10-02 11:16:31 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 327 | |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 328 | private boolean reportFailedAttempt() { |
| 329 | boolean result = updateErrorMessage( |
| 330 | mLockPatternUtils.getCurrentFailedPasswordAttempts(mEffectiveUserId) + 1); |
| 331 | mLockPatternUtils.reportFailedPasswordAttempt(mEffectiveUserId); |
| 332 | return result; |
| 333 | } |
| 334 | |
| 335 | private boolean updateErrorMessage(int numAttempts) { |
| 336 | // Don't show any message if there's no maximum number of attempts. |
| 337 | final int maxAttempts = mLockPatternUtils.getMaximumFailedPasswordsForWipe( |
| 338 | mEffectiveUserId); |
| 339 | if (maxAttempts <= 0 || numAttempts <= 0) { |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | // Update the on-screen error string. |
| 344 | if (mErrorView != null) { |
| 345 | final String message = getResources().getString( |
| 346 | R.string.biometric_dialog_credential_attempts_before_wipe, |
| 347 | numAttempts, |
| 348 | maxAttempts); |
| 349 | showError(message); |
| 350 | } |
| 351 | |
Curtis Belmonte | 1e6a356 | 2020-03-24 18:07:08 -0700 | [diff] [blame] | 352 | // Only show dialog if <=1 attempts are left before wiping. |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 353 | final int remainingAttempts = maxAttempts - numAttempts; |
Curtis Belmonte | 1e6a356 | 2020-03-24 18:07:08 -0700 | [diff] [blame] | 354 | if (remainingAttempts == 1) { |
| 355 | showLastAttemptBeforeWipeDialog(); |
| 356 | } else if (remainingAttempts <= 0) { |
| 357 | showNowWipingDialog(); |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
Curtis Belmonte | 1e6a356 | 2020-03-24 18:07:08 -0700 | [diff] [blame] | 362 | private void showLastAttemptBeforeWipeDialog() { |
| 363 | final AlertDialog alertDialog = new AlertDialog.Builder(mContext) |
| 364 | .setTitle(R.string.biometric_dialog_last_attempt_before_wipe_dialog_title) |
| 365 | .setMessage( |
| 366 | getLastAttemptBeforeWipeMessageRes(getUserTypeForWipe(), mCredentialType)) |
| 367 | .setPositiveButton(android.R.string.ok, null) |
| 368 | .create(); |
| 369 | alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL); |
| 370 | alertDialog.show(); |
| 371 | } |
| 372 | |
| 373 | private void showNowWipingDialog() { |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 374 | final AlertDialog alertDialog = new AlertDialog.Builder(mContext) |
| 375 | .setMessage(getNowWipingMessageRes(getUserTypeForWipe())) |
| 376 | .setPositiveButton(R.string.biometric_dialog_now_wiping_dialog_dismiss, null) |
Curtis Belmonte | 1e6a356 | 2020-03-24 18:07:08 -0700 | [diff] [blame] | 377 | .setOnDismissListener( |
| 378 | dialog -> mContainerView.animateAway(AuthDialogCallback.DISMISSED_ERROR)) |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 379 | .create(); |
Curtis Belmonte | 1e6a356 | 2020-03-24 18:07:08 -0700 | [diff] [blame] | 380 | alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL); |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 381 | alertDialog.show(); |
| 382 | } |
| 383 | |
| 384 | private @UserType int getUserTypeForWipe() { |
| 385 | final UserInfo userToBeWiped = mUserManager.getUserInfo( |
| 386 | mDevicePolicyManager.getProfileWithMinimumFailedPasswordsForWipe(mEffectiveUserId)); |
| 387 | if (userToBeWiped == null || userToBeWiped.isPrimary()) { |
| 388 | return USER_TYPE_PRIMARY; |
| 389 | } else if (userToBeWiped.isManagedProfile()) { |
| 390 | return USER_TYPE_MANAGED_PROFILE; |
| 391 | } else { |
| 392 | return USER_TYPE_SECONDARY; |
| 393 | } |
| 394 | } |
| 395 | |
Curtis Belmonte | 1e6a356 | 2020-03-24 18:07:08 -0700 | [diff] [blame] | 396 | private static @StringRes int getLastAttemptBeforeWipeMessageRes( |
| 397 | @UserType int userType, @Utils.CredentialType int credentialType) { |
| 398 | switch (userType) { |
| 399 | case USER_TYPE_PRIMARY: |
| 400 | return getLastAttemptBeforeWipeDeviceMessageRes(credentialType); |
| 401 | case USER_TYPE_MANAGED_PROFILE: |
| 402 | return getLastAttemptBeforeWipeProfileMessageRes(credentialType); |
| 403 | case USER_TYPE_SECONDARY: |
| 404 | return getLastAttemptBeforeWipeUserMessageRes(credentialType); |
| 405 | default: |
| 406 | throw new IllegalArgumentException("Unrecognized user type:" + userType); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private static @StringRes int getLastAttemptBeforeWipeDeviceMessageRes( |
| 411 | @Utils.CredentialType int credentialType) { |
| 412 | switch (credentialType) { |
| 413 | case Utils.CREDENTIAL_PIN: |
| 414 | return R.string.biometric_dialog_last_pin_attempt_before_wipe_device; |
| 415 | case Utils.CREDENTIAL_PATTERN: |
| 416 | return R.string.biometric_dialog_last_pattern_attempt_before_wipe_device; |
| 417 | case Utils.CREDENTIAL_PASSWORD: |
| 418 | default: |
| 419 | return R.string.biometric_dialog_last_password_attempt_before_wipe_device; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | private static @StringRes int getLastAttemptBeforeWipeProfileMessageRes( |
| 424 | @Utils.CredentialType int credentialType) { |
| 425 | switch (credentialType) { |
| 426 | case Utils.CREDENTIAL_PIN: |
| 427 | return R.string.biometric_dialog_last_pin_attempt_before_wipe_profile; |
| 428 | case Utils.CREDENTIAL_PATTERN: |
| 429 | return R.string.biometric_dialog_last_pattern_attempt_before_wipe_profile; |
| 430 | case Utils.CREDENTIAL_PASSWORD: |
| 431 | default: |
| 432 | return R.string.biometric_dialog_last_password_attempt_before_wipe_profile; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | private static @StringRes int getLastAttemptBeforeWipeUserMessageRes( |
| 437 | @Utils.CredentialType int credentialType) { |
| 438 | switch (credentialType) { |
| 439 | case Utils.CREDENTIAL_PIN: |
| 440 | return R.string.biometric_dialog_last_pin_attempt_before_wipe_user; |
| 441 | case Utils.CREDENTIAL_PATTERN: |
| 442 | return R.string.biometric_dialog_last_pattern_attempt_before_wipe_user; |
| 443 | case Utils.CREDENTIAL_PASSWORD: |
| 444 | default: |
| 445 | return R.string.biometric_dialog_last_password_attempt_before_wipe_user; |
| 446 | } |
| 447 | } |
| 448 | |
Curtis Belmonte | becc0a7 | 2020-03-16 16:13:56 -0700 | [diff] [blame] | 449 | private static @StringRes int getNowWipingMessageRes(@UserType int userType) { |
| 450 | switch (userType) { |
| 451 | case USER_TYPE_PRIMARY: |
| 452 | return R.string.biometric_dialog_failed_attempts_now_wiping_device; |
| 453 | case USER_TYPE_MANAGED_PROFILE: |
| 454 | return R.string.biometric_dialog_failed_attempts_now_wiping_profile; |
| 455 | case USER_TYPE_SECONDARY: |
| 456 | return R.string.biometric_dialog_failed_attempts_now_wiping_user; |
| 457 | default: |
| 458 | throw new IllegalArgumentException("Unrecognized user type:" + userType); |
| 459 | } |
| 460 | } |
| 461 | |
Curtis Belmonte | 4584d88 | 2020-02-11 13:12:36 -0800 | [diff] [blame] | 462 | @Nullable |
| 463 | private static CharSequence getTitle(@NonNull Bundle bundle) { |
| 464 | final CharSequence credentialTitle = |
| 465 | bundle.getCharSequence(BiometricPrompt.KEY_DEVICE_CREDENTIAL_TITLE); |
| 466 | return credentialTitle != null ? credentialTitle |
| 467 | : bundle.getCharSequence(BiometricPrompt.KEY_TITLE); |
| 468 | } |
| 469 | |
| 470 | @Nullable |
| 471 | private static CharSequence getSubtitle(@NonNull Bundle bundle) { |
| 472 | final CharSequence credentialSubtitle = |
| 473 | bundle.getCharSequence(BiometricPrompt.KEY_DEVICE_CREDENTIAL_SUBTITLE); |
| 474 | return credentialSubtitle != null ? credentialSubtitle |
| 475 | : bundle.getCharSequence(BiometricPrompt.KEY_SUBTITLE); |
| 476 | } |
| 477 | |
| 478 | @Nullable |
| 479 | private static CharSequence getDescription(@NonNull Bundle bundle) { |
| 480 | final CharSequence credentialDescription = |
| 481 | bundle.getCharSequence(BiometricPrompt.KEY_DEVICE_CREDENTIAL_DESCRIPTION); |
| 482 | return credentialDescription != null ? credentialDescription |
| 483 | : bundle.getCharSequence(BiometricPrompt.KEY_DESCRIPTION); |
| 484 | } |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 485 | } |