blob: 69630c4e90e81b60307c53dafd23722db6f02faf [file] [log] [blame]
Jim Miller5a8daad2014-01-14 18:57:03 -08001/*
2 * Copyright (C) 2014 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 */
Jim Miller5ecd8112013-01-09 18:50:26 -080016package com.android.keyguard;
Jim Miller838906b2012-10-19 18:41:25 -070017
Jim Miller7751ff62014-01-14 18:57:03 -080018import android.app.Activity;
19import android.app.AlertDialog;
Amith Yamasani3a3d2122014-10-29 11:41:31 -070020import android.app.admin.DevicePolicyManager;
Jim Miller838906b2012-10-19 18:41:25 -070021import android.content.Context;
Jason Chang1e4a4bd2018-05-22 17:30:19 +080022import android.content.res.ColorStateList;
Lucas Dupin3ac28ac2019-05-13 16:33:32 -070023import android.graphics.Rect;
Steven Wu6ef24b22019-03-11 17:14:11 -040024import android.metrics.LogMaker;
Amith Yamasani3a3d2122014-10-29 11:41:31 -070025import android.os.UserHandle;
Jim Miller838906b2012-10-19 18:41:25 -070026import android.util.AttributeSet;
Jim Miller7751ff62014-01-14 18:57:03 -080027import android.util.Log;
28import android.util.Slog;
Tej Singhdd7bd352018-02-09 19:33:15 -080029import android.util.StatsLog;
Lucas Dupin7156bc72019-05-03 19:37:39 -070030import android.util.TypedValue;
Jim Miller7751ff62014-01-14 18:57:03 -080031import android.view.LayoutInflater;
Lucas Dupin7156bc72019-05-03 19:37:39 -070032import android.view.MotionEvent;
33import android.view.VelocityTracker;
Chris Wren052999f2012-11-02 14:36:56 -040034import android.view.View;
Lucas Dupin7156bc72019-05-03 19:37:39 -070035import android.view.ViewConfiguration;
Jim Miller7751ff62014-01-14 18:57:03 -080036import android.view.WindowManager;
Jim Miller838906b2012-10-19 18:41:25 -070037import android.widget.FrameLayout;
38
Gus Prevasab336792018-11-14 13:52:20 -050039import androidx.annotation.VisibleForTesting;
Lucas Dupin7156bc72019-05-03 19:37:39 -070040import androidx.dynamicanimation.animation.DynamicAnimation;
41import androidx.dynamicanimation.animation.SpringAnimation;
Gus Prevasab336792018-11-14 13:52:20 -050042
Steven Wu6ef24b22019-03-11 17:14:11 -040043import com.android.internal.logging.MetricsLogger;
44import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jim Miller7751ff62014-01-14 18:57:03 -080045import com.android.internal.widget.LockPatternUtils;
46import com.android.keyguard.KeyguardSecurityModel.SecurityMode;
Steven Wu6ef24b22019-03-11 17:14:11 -040047import com.android.systemui.Dependency;
Priyank Singh051353e2019-04-25 10:14:57 -070048import com.android.systemui.SystemUIFactory;
Lucas Dupin206fe562019-05-31 14:36:42 -070049import com.android.systemui.statusbar.phone.UnlockMethodCache;
Priyank Singh051353e2019-04-25 10:14:57 -070050import com.android.systemui.util.InjectionInflationController;
Jim Miller7751ff62014-01-14 18:57:03 -080051
52public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSecurityView {
Jorim Jaggi5cf17872014-03-26 18:31:48 +010053 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Miller7751ff62014-01-14 18:57:03 -080054 private static final String TAG = "KeyguardSecurityView";
Amith Yamasani3a3d2122014-10-29 11:41:31 -070055
56 private static final int USER_TYPE_PRIMARY = 1;
57 private static final int USER_TYPE_WORK_PROFILE = 2;
58 private static final int USER_TYPE_SECONDARY_USER = 3;
59
Steven Wucfe398d2019-03-21 11:32:15 -040060 // Bouncer is dismissed due to no security.
61 private static final int BOUNCER_DISMISS_NONE_SECURITY = 0;
62 // Bouncer is dismissed due to pin, password or pattern entered.
63 private static final int BOUNCER_DISMISS_PASSWORD = 1;
64 // Bouncer is dismissed due to biometric (face, fingerprint or iris) authenticated.
65 private static final int BOUNCER_DISMISS_BIOMETRIC = 2;
66 // Bouncer is dismissed due to extended access granted.
67 private static final int BOUNCER_DISMISS_EXTENDED_ACCESS = 3;
68 // Bouncer is dismissed due to sim card unlock code entered.
69 private static final int BOUNCER_DISMISS_SIM = 4;
70
Lucas Dupin7156bc72019-05-03 19:37:39 -070071 // Make the view move slower than the finger, as if the spring were applying force.
72 private static final float TOUCH_Y_MULTIPLIER = 0.25f;
73 // How much you need to drag the bouncer to trigger an auth retry (in dps.)
74 private static final float MIN_DRAG_SIZE = 10;
Lucas Dupinabb18f42019-05-20 19:10:22 -070075 // How much to scale the default slop by, to avoid accidental drags.
76 private static final float SLOP_SCALE = 2f;
Lucas Dupin7156bc72019-05-03 19:37:39 -070077
Jim Miller7751ff62014-01-14 18:57:03 -080078 private KeyguardSecurityModel mSecurityModel;
Jim Miller7751ff62014-01-14 18:57:03 -080079 private LockPatternUtils mLockPatternUtils;
80
81 private KeyguardSecurityViewFlipper mSecurityViewFlipper;
82 private boolean mIsVerifyUnlockOnly;
83 private SecurityMode mCurrentSecuritySelection = SecurityMode.Invalid;
Lucas Dupin27321c42019-03-20 16:22:24 -070084 private KeyguardSecurityView mCurrentSecurityView;
Jim Miller7751ff62014-01-14 18:57:03 -080085 private SecurityCallback mSecurityCallback;
Lucas Dupin7f729822017-12-13 15:27:10 -080086 private AlertDialog mAlertDialog;
Priyank Singh051353e2019-04-25 10:14:57 -070087 private InjectionInflationController mInjectionInflationController;
Lucas Dupin7156bc72019-05-03 19:37:39 -070088 private boolean mSwipeUpToRetry;
Jim Miller7751ff62014-01-14 18:57:03 -080089
Lucas Dupin7156bc72019-05-03 19:37:39 -070090 private final ViewConfiguration mViewConfiguration;
91 private final SpringAnimation mSpringAnimation;
92 private final VelocityTracker mVelocityTracker = VelocityTracker.obtain();
Adrian Roos336be7f2014-05-19 17:11:18 +020093 private final KeyguardUpdateMonitor mUpdateMonitor;
Lucas Dupin206fe562019-05-31 14:36:42 -070094 private final UnlockMethodCache mUnlockMethodCache;
Adrian Roos336be7f2014-05-19 17:11:18 +020095
Steven Wu6ef24b22019-03-11 17:14:11 -040096 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Lucas Dupin7156bc72019-05-03 19:37:39 -070097 private float mLastTouchY = -1;
98 private int mActivePointerId = -1;
99 private boolean mIsDragging;
100 private float mStartTouchY = -1;
Steven Wu6ef24b22019-03-11 17:14:11 -0400101
Jim Miller7751ff62014-01-14 18:57:03 -0800102 // Used to notify the container when something interesting happens.
103 public interface SecurityCallback {
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700104 public boolean dismiss(boolean authenticated, int targetUserId);
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200105 public void userActivity();
Jim Millerba7d94b2014-02-05 17:30:50 -0800106 public void onSecurityModeChanged(SecurityMode securityMode, boolean needsInput);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700107
108 /**
109 * @param strongAuth wheher the user has authenticated with strong authentication like
110 * pattern, password or PIN but not by trust agents or fingerprint
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700111 * @param targetUserId a user that needs to be the foreground user at the finish completion.
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700112 */
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700113 public void finish(boolean strongAuth, int targetUserId);
Andrew Lee72b46d42015-01-30 13:23:21 -0800114 public void reset();
Aarthi Balachander0a427ef2018-07-13 15:00:58 -0700115 public void onCancelClicked();
Jim Miller7751ff62014-01-14 18:57:03 -0800116 }
117
Jim Miller838906b2012-10-19 18:41:25 -0700118 public KeyguardSecurityContainer(Context context, AttributeSet attrs) {
119 this(context, attrs, 0);
120 }
121
122 public KeyguardSecurityContainer(Context context) {
Esteban Talaverafe0f24c2014-08-06 16:20:56 +0100123 this(context, null, 0);
Jim Miller838906b2012-10-19 18:41:25 -0700124 }
125
126 public KeyguardSecurityContainer(Context context, AttributeSet attrs, int defStyle) {
Lucas Dupin987f1932017-05-13 21:02:52 -0700127 super(context, attrs, defStyle);
Jim Miller7751ff62014-01-14 18:57:03 -0800128 mSecurityModel = new KeyguardSecurityModel(context);
129 mLockPatternUtils = new LockPatternUtils(context);
Adrian Roos336be7f2014-05-19 17:11:18 +0200130 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Lucas Dupin7156bc72019-05-03 19:37:39 -0700131 mSpringAnimation = new SpringAnimation(this, DynamicAnimation.Y);
Priyank Singh051353e2019-04-25 10:14:57 -0700132 mInjectionInflationController = new InjectionInflationController(
133 SystemUIFactory.getInstance().getRootComponent());
Lucas Dupin206fe562019-05-31 14:36:42 -0700134 mUnlockMethodCache = UnlockMethodCache.getInstance(context);
Lucas Dupin7156bc72019-05-03 19:37:39 -0700135 mViewConfiguration = ViewConfiguration.get(context);
Chris Wrenc3451462012-10-30 11:22:58 -0400136 }
137
Jim Miller7751ff62014-01-14 18:57:03 -0800138 public void setSecurityCallback(SecurityCallback callback) {
139 mSecurityCallback = callback;
140 }
141
142 @Override
143 public void onResume(int reason) {
Jorim Jaggif4797922014-08-04 22:49:41 +0200144 if (mCurrentSecuritySelection != SecurityMode.None) {
145 getSecurityView(mCurrentSecuritySelection).onResume(reason);
146 }
Lucas Dupin7156bc72019-05-03 19:37:39 -0700147 updateBiometricRetry();
Jim Miller7751ff62014-01-14 18:57:03 -0800148 }
149
150 @Override
151 public void onPause() {
Lucas Dupin7f729822017-12-13 15:27:10 -0800152 if (mAlertDialog != null) {
153 mAlertDialog.dismiss();
154 mAlertDialog = null;
155 }
Jorim Jaggif4797922014-08-04 22:49:41 +0200156 if (mCurrentSecuritySelection != SecurityMode.None) {
157 getSecurityView(mCurrentSecuritySelection).onPause();
158 }
Jim Miller7751ff62014-01-14 18:57:03 -0800159 }
160
Lucas Dupin7156bc72019-05-03 19:37:39 -0700161 @Override
162 public boolean shouldDelayChildPressedState() {
163 return true;
164 }
165
166 @Override
167 public boolean onInterceptTouchEvent(MotionEvent event) {
168 switch (event.getActionMasked()) {
169 case MotionEvent.ACTION_DOWN:
170 int pointerIndex = event.getActionIndex();
171 mStartTouchY = event.getY(pointerIndex);
172 mActivePointerId = event.getPointerId(pointerIndex);
173 mVelocityTracker.clear();
174 break;
175 case MotionEvent.ACTION_MOVE:
176 if (mIsDragging) {
177 return true;
178 }
179 if (!mSwipeUpToRetry) {
180 return false;
181 }
182 // Avoid dragging the pattern view
183 if (mCurrentSecurityView.disallowInterceptTouch(event)) {
184 return false;
185 }
186 int index = event.findPointerIndex(mActivePointerId);
Lucas Dupinabb18f42019-05-20 19:10:22 -0700187 float touchSlop = mViewConfiguration.getScaledTouchSlop() * SLOP_SCALE;
Lucas Dupinc26ab7512019-05-13 13:49:17 -0700188 if (mCurrentSecurityView != null && index != -1
Lucas Dupin7156bc72019-05-03 19:37:39 -0700189 && mStartTouchY - event.getY(index) > touchSlop) {
190 mIsDragging = true;
191 return true;
192 }
193 break;
194 case MotionEvent.ACTION_CANCEL:
195 case MotionEvent.ACTION_UP:
196 mIsDragging = false;
197 break;
198 }
199 return false;
200 }
201
202 @Override
203 public boolean onTouchEvent(MotionEvent event) {
204 final int action = event.getActionMasked();
205 switch (action) {
206 case MotionEvent.ACTION_MOVE:
207 mVelocityTracker.addMovement(event);
208 int pointerIndex = event.findPointerIndex(mActivePointerId);
209 float y = event.getY(pointerIndex);
210 if (mLastTouchY != -1) {
211 float dy = y - mLastTouchY;
212 setTranslationY(getTranslationY() + dy * TOUCH_Y_MULTIPLIER);
213 }
214 mLastTouchY = y;
215 break;
216 case MotionEvent.ACTION_UP:
217 case MotionEvent.ACTION_CANCEL:
218 mActivePointerId = -1;
219 mLastTouchY = -1;
220 mIsDragging = false;
221 startSpringAnimation(mVelocityTracker.getYVelocity());
222 break;
223 case MotionEvent.ACTION_POINTER_UP:
224 int index = event.getActionIndex();
225 int pointerId = event.getPointerId(index);
226 if (pointerId == mActivePointerId) {
227 // This was our active pointer going up. Choose a new
228 // active pointer and adjust accordingly.
229 final int newPointerIndex = index == 0 ? 1 : 0;
230 mLastTouchY = event.getY(newPointerIndex);
231 mActivePointerId = event.getPointerId(newPointerIndex);
232 }
233 break;
234 }
235 if (action == MotionEvent.ACTION_UP) {
236 if (-getTranslationY() > TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
Lucas Dupin51996bb2019-05-16 17:56:43 -0700237 MIN_DRAG_SIZE, getResources().getDisplayMetrics())
238 && !mUpdateMonitor.isFaceDetectionRunning()) {
Lucas Dupin7156bc72019-05-03 19:37:39 -0700239 mUpdateMonitor.requestFaceAuth();
Lucas Dupin51996bb2019-05-16 17:56:43 -0700240 showMessage(null, null);
Lucas Dupin7156bc72019-05-03 19:37:39 -0700241 }
242 }
243 return true;
244 }
245
246 private void startSpringAnimation(float startVelocity) {
247 mSpringAnimation
248 .setStartVelocity(startVelocity)
249 .animateToFinalPosition(0);
250 }
251
Jorim Jaggic14f8292014-05-27 02:25:45 +0200252 public void startAppearAnimation() {
Jorim Jaggif4797922014-08-04 22:49:41 +0200253 if (mCurrentSecuritySelection != SecurityMode.None) {
254 getSecurityView(mCurrentSecuritySelection).startAppearAnimation();
255 }
Jorim Jaggic14f8292014-05-27 02:25:45 +0200256 }
257
Jorim Jaggi76a16232014-08-08 17:00:47 +0200258 public boolean startDisappearAnimation(Runnable onFinishRunnable) {
259 if (mCurrentSecuritySelection != SecurityMode.None) {
260 return getSecurityView(mCurrentSecuritySelection).startDisappearAnimation(
261 onFinishRunnable);
262 }
263 return false;
264 }
265
Lucas Dupin7156bc72019-05-03 19:37:39 -0700266 /**
267 * Enables/disables swipe up to retry on the bouncer.
268 */
269 private void updateBiometricRetry() {
270 SecurityMode securityMode = getSecurityMode();
Lucas Dupin206fe562019-05-31 14:36:42 -0700271 mSwipeUpToRetry = mUnlockMethodCache.isUnlockingWithFacePossible()
Lucas Dupin7156bc72019-05-03 19:37:39 -0700272 && securityMode != SecurityMode.SimPin
273 && securityMode != SecurityMode.SimPuk
Lucas Dupinfc8de932019-06-13 14:20:52 -0700274 && securityMode != SecurityMode.None;
Lucas Dupin7156bc72019-05-03 19:37:39 -0700275 }
276
Phil Weaver7d847b02018-02-13 16:02:35 -0800277 public CharSequence getTitle() {
278 return mSecurityViewFlipper.getTitle();
Jorim Jaggic1dff8c2015-02-02 14:45:39 +0100279 }
280
Jim Miller7751ff62014-01-14 18:57:03 -0800281 private KeyguardSecurityView getSecurityView(SecurityMode securityMode) {
282 final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
283 KeyguardSecurityView view = null;
284 final int children = mSecurityViewFlipper.getChildCount();
285 for (int child = 0; child < children; child++) {
286 if (mSecurityViewFlipper.getChildAt(child).getId() == securityViewIdForMode) {
287 view = ((KeyguardSecurityView)mSecurityViewFlipper.getChildAt(child));
288 break;
289 }
290 }
291 int layoutId = getLayoutIdFor(securityMode);
292 if (view == null && layoutId != 0) {
293 final LayoutInflater inflater = LayoutInflater.from(mContext);
294 if (DEBUG) Log.v(TAG, "inflating id = " + layoutId);
Priyank Singh051353e2019-04-25 10:14:57 -0700295 View v = mInjectionInflationController.injectable(inflater)
296 .inflate(layoutId, mSecurityViewFlipper, false);
Jim Miller7751ff62014-01-14 18:57:03 -0800297 mSecurityViewFlipper.addView(v);
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100298 updateSecurityView(v);
Jim Miller7751ff62014-01-14 18:57:03 -0800299 view = (KeyguardSecurityView)v;
Lucas Dupinf7fd03d2018-06-26 15:58:51 -0700300 view.reset();
Jim Miller7751ff62014-01-14 18:57:03 -0800301 }
302
Jim Miller7751ff62014-01-14 18:57:03 -0800303 return view;
304 }
305
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100306 private void updateSecurityView(View view) {
Jim Miller7751ff62014-01-14 18:57:03 -0800307 if (view instanceof KeyguardSecurityView) {
308 KeyguardSecurityView ksv = (KeyguardSecurityView) view;
309 ksv.setKeyguardCallback(mCallback);
310 ksv.setLockPatternUtils(mLockPatternUtils);
Jim Miller7751ff62014-01-14 18:57:03 -0800311 } else {
312 Log.w(TAG, "View " + view + " is not a KeyguardSecurityView");
313 }
314 }
315
316 protected void onFinishInflate() {
Alan Viverette51efddb2017-04-05 10:00:01 -0400317 mSecurityViewFlipper = findViewById(R.id.view_flipper);
Jim Miller7751ff62014-01-14 18:57:03 -0800318 mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils);
319 }
320
321 public void setLockPatternUtils(LockPatternUtils utils) {
322 mLockPatternUtils = utils;
323 mSecurityModel.setLockPatternUtils(utils);
324 mSecurityViewFlipper.setLockPatternUtils(mLockPatternUtils);
325 }
326
Lucas Dupin7156bc72019-05-03 19:37:39 -0700327 @Override
Lucas Dupin3ac28ac2019-05-13 16:33:32 -0700328 protected boolean fitSystemWindows(Rect insets) {
329 // Consume bottom insets because we're setting the padding locally (for IME and navbar.)
330 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), insets.bottom);
331 insets.bottom = 0;
332 return false;
Lucas Dupin7156bc72019-05-03 19:37:39 -0700333 }
334
Jim Miller7751ff62014-01-14 18:57:03 -0800335 private void showDialog(String title, String message) {
Lucas Dupin7f729822017-12-13 15:27:10 -0800336 if (mAlertDialog != null) {
337 mAlertDialog.dismiss();
338 }
339
340 mAlertDialog = new AlertDialog.Builder(mContext)
Jim Miller7751ff62014-01-14 18:57:03 -0800341 .setTitle(title)
342 .setMessage(message)
Edward Savage-Jonesb08a1462016-10-05 10:29:02 +0200343 .setCancelable(false)
Jim Miller7751ff62014-01-14 18:57:03 -0800344 .setNeutralButton(R.string.ok, null)
345 .create();
346 if (!(mContext instanceof Activity)) {
Lucas Dupin7f729822017-12-13 15:27:10 -0800347 mAlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
Jim Miller7751ff62014-01-14 18:57:03 -0800348 }
Lucas Dupin7f729822017-12-13 15:27:10 -0800349 mAlertDialog.show();
Jim Miller7751ff62014-01-14 18:57:03 -0800350 }
351
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700352 private void showTimeoutDialog(int userId, int timeoutMs) {
Andres Morales23974272015-05-14 22:42:26 -0700353 int timeoutInSeconds = (int) timeoutMs / 1000;
Jim Miller7751ff62014-01-14 18:57:03 -0800354 int messageId = 0;
355
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700356 switch (mSecurityModel.getSecurityMode(userId)) {
Jim Miller7751ff62014-01-14 18:57:03 -0800357 case Pattern:
358 messageId = R.string.kg_too_many_failed_pattern_attempts_dialog_message;
359 break;
360 case PIN:
361 messageId = R.string.kg_too_many_failed_pin_attempts_dialog_message;
362 break;
363 case Password:
364 messageId = R.string.kg_too_many_failed_password_attempts_dialog_message;
365 break;
366 // These don't have timeout dialogs.
Jim Miller7751ff62014-01-14 18:57:03 -0800367 case Invalid:
368 case None:
369 case SimPin:
370 case SimPuk:
371 break;
372 }
373
374 if (messageId != 0) {
375 final String message = mContext.getString(messageId,
Pavel Grafovb90dc432018-08-15 20:02:58 +0100376 mLockPatternUtils.getCurrentFailedPasswordAttempts(userId),
Jim Miller7751ff62014-01-14 18:57:03 -0800377 timeoutInSeconds);
378 showDialog(null, message);
379 }
380 }
381
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700382 private void showAlmostAtWipeDialog(int attempts, int remaining, int userType) {
383 String message = null;
384 switch (userType) {
385 case USER_TYPE_PRIMARY:
386 message = mContext.getString(R.string.kg_failed_attempts_almost_at_wipe,
387 attempts, remaining);
388 break;
389 case USER_TYPE_SECONDARY_USER:
390 message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_user,
391 attempts, remaining);
392 break;
393 case USER_TYPE_WORK_PROFILE:
394 message = mContext.getString(R.string.kg_failed_attempts_almost_at_erase_profile,
395 attempts, remaining);
396 break;
397 }
Jim Miller7751ff62014-01-14 18:57:03 -0800398 showDialog(null, message);
399 }
400
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700401 private void showWipeDialog(int attempts, int userType) {
402 String message = null;
403 switch (userType) {
404 case USER_TYPE_PRIMARY:
405 message = mContext.getString(R.string.kg_failed_attempts_now_wiping,
406 attempts);
407 break;
408 case USER_TYPE_SECONDARY_USER:
409 message = mContext.getString(R.string.kg_failed_attempts_now_erasing_user,
410 attempts);
411 break;
412 case USER_TYPE_WORK_PROFILE:
413 message = mContext.getString(R.string.kg_failed_attempts_now_erasing_profile,
414 attempts);
415 break;
416 }
Jim Miller7751ff62014-01-14 18:57:03 -0800417 showDialog(null, message);
418 }
419
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800420 private void reportFailedUnlockAttempt(int userId, int timeoutMs) {
Pavel Grafovb90dc432018-08-15 20:02:58 +0100421 // +1 for this time
422 final int failedAttempts = mLockPatternUtils.getCurrentFailedPasswordAttempts(userId) + 1;
Jim Miller7751ff62014-01-14 18:57:03 -0800423
424 if (DEBUG) Log.d(TAG, "reportFailedPatternAttempt: #" + failedAttempts);
425
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700426 final DevicePolicyManager dpm = mLockPatternUtils.getDevicePolicyManager();
427 final int failedAttemptsBeforeWipe =
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800428 dpm.getMaximumFailedPasswordsForWipe(null, userId);
Jim Miller7751ff62014-01-14 18:57:03 -0800429
Jim Miller7751ff62014-01-14 18:57:03 -0800430 final int remainingBeforeWipe = failedAttemptsBeforeWipe > 0 ?
431 (failedAttemptsBeforeWipe - failedAttempts)
432 : Integer.MAX_VALUE; // because DPM returns 0 if no restriction
Jim Miller7751ff62014-01-14 18:57:03 -0800433 if (remainingBeforeWipe < LockPatternUtils.FAILED_ATTEMPTS_BEFORE_WIPE_GRACE) {
Esteban Talaverafe0f24c2014-08-06 16:20:56 +0100434 // The user has installed a DevicePolicyManager that requests a user/profile to be wiped
435 // N attempts. Once we get below the grace period, we post this dialog every time as a
436 // clear warning until the deletion fires.
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700437 // Check which profile has the strictest policy for failed password attempts
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800438 final int expiringUser = dpm.getProfileWithMinimumFailedPasswordsForWipe(userId);
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700439 int userType = USER_TYPE_PRIMARY;
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800440 if (expiringUser == userId) {
Xiaohui Chencc791bc2015-08-26 14:54:34 -0700441 // TODO: http://b/23522538
442 if (expiringUser != UserHandle.USER_SYSTEM) {
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700443 userType = USER_TYPE_SECONDARY_USER;
444 }
445 } else if (expiringUser != UserHandle.USER_NULL) {
446 userType = USER_TYPE_WORK_PROFILE;
447 } // If USER_NULL, which shouldn't happen, leave it as USER_TYPE_PRIMARY
Jim Miller7751ff62014-01-14 18:57:03 -0800448 if (remainingBeforeWipe > 0) {
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700449 showAlmostAtWipeDialog(failedAttempts, remainingBeforeWipe, userType);
Jim Miller7751ff62014-01-14 18:57:03 -0800450 } else {
451 // Too many attempts. The device will be wiped shortly.
Amith Yamasani3a3d2122014-10-29 11:41:31 -0700452 Slog.i(TAG, "Too many unlock attempts; user " + expiringUser + " will be wiped!");
453 showWipeDialog(failedAttempts, userType);
Jim Miller7751ff62014-01-14 18:57:03 -0800454 }
Jim Miller7751ff62014-01-14 18:57:03 -0800455 }
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800456 mLockPatternUtils.reportFailedPasswordAttempt(userId);
Andres Morales23974272015-05-14 22:42:26 -0700457 if (timeoutMs > 0) {
Zachary Iqbal327323d2017-01-12 14:41:13 -0800458 mLockPatternUtils.reportPasswordLockout(timeoutMs, userId);
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700459 showTimeoutDialog(userId, timeoutMs);
Jim Miller7751ff62014-01-14 18:57:03 -0800460 }
461 }
462
463 /**
464 * Shows the primary security screen for the user. This will be either the multi-selector
465 * or the user's security method.
466 * @param turningOff true if the device is being turned off
467 */
468 void showPrimarySecurityScreen(boolean turningOff) {
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700469 SecurityMode securityMode = mSecurityModel.getSecurityMode(
470 KeyguardUpdateMonitor.getCurrentUser());
Jim Miller7751ff62014-01-14 18:57:03 -0800471 if (DEBUG) Log.v(TAG, "showPrimarySecurityScreen(turningOff=" + turningOff + ")");
Jim Miller7751ff62014-01-14 18:57:03 -0800472 showSecurityScreen(securityMode);
473 }
474
475 /**
Jim Millerba7d94b2014-02-05 17:30:50 -0800476 * Shows the next security screen if there is one.
477 * @param authenticated true if the user entered the correct authentication
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700478 * @param targetUserId a user that needs to be the foreground user at the finish (if called)
479 * completion.
Jim Millerba7d94b2014-02-05 17:30:50 -0800480 * @return true if keyguard is done
481 */
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700482 boolean showNextSecurityScreenOrFinish(boolean authenticated, int targetUserId) {
Jim Miller7751ff62014-01-14 18:57:03 -0800483 if (DEBUG) Log.d(TAG, "showNextSecurityScreenOrFinish(" + authenticated + ")");
484 boolean finish = false;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700485 boolean strongAuth = false;
Steven Wucfe398d2019-03-21 11:32:15 -0400486 int eventSubtype = -1;
487 if (mUpdateMonitor.getUserHasTrust(targetUserId)) {
Adrian Roos336be7f2014-05-19 17:11:18 +0200488 finish = true;
Steven Wucfe398d2019-03-21 11:32:15 -0400489 eventSubtype = BOUNCER_DISMISS_EXTENDED_ACCESS;
490 } else if (mUpdateMonitor.getUserUnlockedWithBiometric(targetUserId)) {
491 finish = true;
492 eventSubtype = BOUNCER_DISMISS_BIOMETRIC;
Adrian Roos336be7f2014-05-19 17:11:18 +0200493 } else if (SecurityMode.None == mCurrentSecuritySelection) {
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700494 SecurityMode securityMode = mSecurityModel.getSecurityMode(targetUserId);
Jim Miller7751ff62014-01-14 18:57:03 -0800495 if (SecurityMode.None == securityMode) {
496 finish = true; // no security required
Steven Wucfe398d2019-03-21 11:32:15 -0400497 eventSubtype = BOUNCER_DISMISS_NONE_SECURITY;
Jim Miller7751ff62014-01-14 18:57:03 -0800498 } else {
499 showSecurityScreen(securityMode); // switch to the alternate security view
500 }
501 } else if (authenticated) {
502 switch (mCurrentSecuritySelection) {
503 case Pattern:
504 case Password:
505 case PIN:
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700506 strongAuth = true;
Jim Miller7751ff62014-01-14 18:57:03 -0800507 finish = true;
Steven Wucfe398d2019-03-21 11:32:15 -0400508 eventSubtype = BOUNCER_DISMISS_PASSWORD;
Jim Miller7751ff62014-01-14 18:57:03 -0800509 break;
510
511 case SimPin:
512 case SimPuk:
513 // Shortcut for SIM PIN/PUK to go to directly to user's security screen or home
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700514 SecurityMode securityMode = mSecurityModel.getSecurityMode(targetUserId);
Jordan Liub4299d92018-09-26 12:49:11 -0700515 if (securityMode == SecurityMode.None || mLockPatternUtils.isLockScreenDisabled(
Selim Cinek245273e2015-06-24 13:17:56 -0400516 KeyguardUpdateMonitor.getCurrentUser())) {
Jim Miller7751ff62014-01-14 18:57:03 -0800517 finish = true;
Steven Wucfe398d2019-03-21 11:32:15 -0400518 eventSubtype = BOUNCER_DISMISS_SIM;
Jordan Liub4299d92018-09-26 12:49:11 -0700519 } else {
520 showSecurityScreen(securityMode);
Jim Miller7751ff62014-01-14 18:57:03 -0800521 }
522 break;
523
524 default:
525 Log.v(TAG, "Bad security screen " + mCurrentSecuritySelection + ", fail safe");
526 showPrimarySecurityScreen(false);
527 break;
528 }
Jim Miller7751ff62014-01-14 18:57:03 -0800529 }
Steven Wucfe398d2019-03-21 11:32:15 -0400530 if (eventSubtype != -1) {
531 mMetricsLogger.write(new LogMaker(MetricsEvent.BOUNCER)
532 .setType(MetricsEvent.TYPE_DISMISS).setSubtype(eventSubtype));
533 }
Jim Miller7751ff62014-01-14 18:57:03 -0800534 if (finish) {
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700535 mSecurityCallback.finish(strongAuth, targetUserId);
Jim Miller7751ff62014-01-14 18:57:03 -0800536 }
537 return finish;
538 }
539
540 /**
541 * Switches to the given security view unless it's already being shown, in which case
542 * this is a no-op.
543 *
544 * @param securityMode
545 */
546 private void showSecurityScreen(SecurityMode securityMode) {
547 if (DEBUG) Log.d(TAG, "showSecurityScreen(" + securityMode + ")");
548
549 if (securityMode == mCurrentSecuritySelection) return;
550
551 KeyguardSecurityView oldView = getSecurityView(mCurrentSecuritySelection);
552 KeyguardSecurityView newView = getSecurityView(securityMode);
553
554 // Emulate Activity life cycle
555 if (oldView != null) {
556 oldView.onPause();
557 oldView.setKeyguardCallback(mNullCallback); // ignore requests from old view
558 }
Jorim Jaggif4797922014-08-04 22:49:41 +0200559 if (securityMode != SecurityMode.None) {
560 newView.onResume(KeyguardSecurityView.VIEW_REVEALED);
561 newView.setKeyguardCallback(mCallback);
562 }
Jim Miller7751ff62014-01-14 18:57:03 -0800563
564 // Find and show this child.
565 final int childCount = mSecurityViewFlipper.getChildCount();
566
567 final int securityViewIdForMode = getSecurityViewIdForMode(securityMode);
568 for (int i = 0; i < childCount; i++) {
569 if (mSecurityViewFlipper.getChildAt(i).getId() == securityViewIdForMode) {
570 mSecurityViewFlipper.setDisplayedChild(i);
571 break;
572 }
573 }
574
575 mCurrentSecuritySelection = securityMode;
Lucas Dupin27321c42019-03-20 16:22:24 -0700576 mCurrentSecurityView = newView;
Jorim Jaggif4797922014-08-04 22:49:41 +0200577 mSecurityCallback.onSecurityModeChanged(securityMode,
578 securityMode != SecurityMode.None && newView.needsInput());
Jim Miller7751ff62014-01-14 18:57:03 -0800579 }
580
581 private KeyguardSecurityViewFlipper getFlipper() {
Chris Wren052999f2012-11-02 14:36:56 -0400582 for (int i = 0; i < getChildCount(); i++) {
583 View child = getChildAt(i);
584 if (child instanceof KeyguardSecurityViewFlipper) {
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500585 return (KeyguardSecurityViewFlipper) child;
Chris Wren052999f2012-11-02 14:36:56 -0400586 }
587 }
588 return null;
589 }
590
Jim Miller7751ff62014-01-14 18:57:03 -0800591 private KeyguardSecurityCallback mCallback = new KeyguardSecurityCallback() {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200592 public void userActivity() {
Jim Miller7751ff62014-01-14 18:57:03 -0800593 if (mSecurityCallback != null) {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200594 mSecurityCallback.userActivity();
Jim Miller7751ff62014-01-14 18:57:03 -0800595 }
596 }
597
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700598 public void dismiss(boolean authenticated, int targetId) {
599 mSecurityCallback.dismiss(authenticated, targetId);
Jim Miller7751ff62014-01-14 18:57:03 -0800600 }
601
602 public boolean isVerifyUnlockOnly() {
603 return mIsVerifyUnlockOnly;
604 }
605
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800606 public void reportUnlockAttempt(int userId, boolean success, int timeoutMs) {
Jim Miller7751ff62014-01-14 18:57:03 -0800607 if (success) {
Tej Singhdd7bd352018-02-09 19:33:15 -0800608 StatsLog.write(StatsLog.KEYGUARD_BOUNCER_PASSWORD_ENTERED,
609 StatsLog.KEYGUARD_BOUNCER_PASSWORD_ENTERED__RESULT__SUCCESS);
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800610 mLockPatternUtils.reportSuccessfulPasswordAttempt(userId);
Jim Miller7751ff62014-01-14 18:57:03 -0800611 } else {
Tej Singhdd7bd352018-02-09 19:33:15 -0800612 StatsLog.write(StatsLog.KEYGUARD_BOUNCER_PASSWORD_ENTERED,
613 StatsLog.KEYGUARD_BOUNCER_PASSWORD_ENTERED__RESULT__FAILURE);
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800614 KeyguardSecurityContainer.this.reportFailedUnlockAttempt(userId, timeoutMs);
Jim Miller7751ff62014-01-14 18:57:03 -0800615 }
Steven Wu6ef24b22019-03-11 17:14:11 -0400616 mMetricsLogger.write(new LogMaker(MetricsEvent.BOUNCER)
617 .setType(success ? MetricsEvent.TYPE_SUCCESS : MetricsEvent.TYPE_FAILURE));
Jim Miller7751ff62014-01-14 18:57:03 -0800618 }
619
Andrew Lee72b46d42015-01-30 13:23:21 -0800620 public void reset() {
621 mSecurityCallback.reset();
622 }
Aarthi Balachander0a427ef2018-07-13 15:00:58 -0700623
624 public void onCancelClicked() {
625 mSecurityCallback.onCancelClicked();
626 }
Jim Miller7751ff62014-01-14 18:57:03 -0800627 };
628
629 // The following is used to ignore callbacks from SecurityViews that are no longer current
630 // (e.g. face unlock). This avoids unwanted asynchronous events from messing with the
631 // state for the current security method.
632 private KeyguardSecurityCallback mNullCallback = new KeyguardSecurityCallback() {
633 @Override
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200634 public void userActivity() { }
Jim Miller7751ff62014-01-14 18:57:03 -0800635 @Override
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800636 public void reportUnlockAttempt(int userId, boolean success, int timeoutMs) { }
Jim Miller7751ff62014-01-14 18:57:03 -0800637 @Override
638 public boolean isVerifyUnlockOnly() { return false; }
639 @Override
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700640 public void dismiss(boolean securityVerified, int targetUserId) { }
Andrew Lee72b46d42015-01-30 13:23:21 -0800641 @Override
642 public void reset() {}
Jim Miller7751ff62014-01-14 18:57:03 -0800643 };
644
645 private int getSecurityViewIdForMode(SecurityMode securityMode) {
646 switch (securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800647 case Pattern: return R.id.keyguard_pattern_view;
648 case PIN: return R.id.keyguard_pin_view;
649 case Password: return R.id.keyguard_password_view;
Jim Miller7751ff62014-01-14 18:57:03 -0800650 case SimPin: return R.id.keyguard_sim_pin_view;
651 case SimPuk: return R.id.keyguard_sim_puk_view;
652 }
653 return 0;
654 }
655
Lucas Dupine17ce522017-07-17 15:45:06 -0700656 @VisibleForTesting
657 public int getLayoutIdFor(SecurityMode securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800658 switch (securityMode) {
Jim Miller7751ff62014-01-14 18:57:03 -0800659 case Pattern: return R.layout.keyguard_pattern_view;
660 case PIN: return R.layout.keyguard_pin_view;
661 case Password: return R.layout.keyguard_password_view;
Jim Miller7751ff62014-01-14 18:57:03 -0800662 case SimPin: return R.layout.keyguard_sim_pin_view;
663 case SimPuk: return R.layout.keyguard_sim_puk_view;
664 default:
665 return 0;
666 }
667 }
668
669 public SecurityMode getSecurityMode() {
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700670 return mSecurityModel.getSecurityMode(KeyguardUpdateMonitor.getCurrentUser());
Jim Miller7751ff62014-01-14 18:57:03 -0800671 }
672
Jorim Jaggi95e89ca2014-11-24 20:12:50 +0100673 public SecurityMode getCurrentSecurityMode() {
674 return mCurrentSecuritySelection;
675 }
676
Lucas Dupin27321c42019-03-20 16:22:24 -0700677 public KeyguardSecurityView getCurrentSecurityView() {
678 return mCurrentSecurityView;
679 }
680
Jim Miller7751ff62014-01-14 18:57:03 -0800681 public void verifyUnlock() {
682 mIsVerifyUnlockOnly = true;
683 showSecurityScreen(getSecurityMode());
684 }
685
686 public SecurityMode getCurrentSecuritySelection() {
687 return mCurrentSecuritySelection;
688 }
689
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700690 public void dismiss(boolean authenticated, int targetUserId) {
691 mCallback.dismiss(authenticated, targetUserId);
Jim Miller7751ff62014-01-14 18:57:03 -0800692 }
693
694 public boolean needsInput() {
695 return mSecurityViewFlipper.needsInput();
696 }
697
698 @Override
699 public void setKeyguardCallback(KeyguardSecurityCallback callback) {
700 mSecurityViewFlipper.setKeyguardCallback(callback);
701 }
702
703 @Override
704 public void reset() {
705 mSecurityViewFlipper.reset();
706 }
707
708 @Override
709 public KeyguardSecurityCallback getCallback() {
710 return mSecurityViewFlipper.getCallback();
711 }
712
713 @Override
Selim Cinek3122fa82015-06-18 01:38:59 -0700714 public void showPromptReason(int reason) {
715 if (mCurrentSecuritySelection != SecurityMode.None) {
Adrian Roos569edb82016-03-03 15:39:03 -0800716 if (reason != PROMPT_REASON_NONE) {
717 Log.i(TAG, "Strong auth required, reason: " + reason);
718 }
Selim Cinek3122fa82015-06-18 01:38:59 -0700719 getSecurityView(mCurrentSecuritySelection).showPromptReason(reason);
720 }
721 }
722
Jason Chang1e4a4bd2018-05-22 17:30:19 +0800723 public void showMessage(CharSequence message, ColorStateList colorState) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700724 if (mCurrentSecuritySelection != SecurityMode.None) {
Jason Chang1e4a4bd2018-05-22 17:30:19 +0800725 getSecurityView(mCurrentSecuritySelection).showMessage(message, colorState);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700726 }
727 }
728
Selim Cinek3122fa82015-06-18 01:38:59 -0700729 @Override
Jim Miller7751ff62014-01-14 18:57:03 -0800730 public void showUsabilityHint() {
731 mSecurityViewFlipper.showUsabilityHint();
732 }
733
Jim Miller838906b2012-10-19 18:41:25 -0700734}
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500735