blob: d8086da277f24204e2ff5682f92fb40697567570 [file] [log] [blame]
Jim Millerdcb3d842012-08-23 19:18:12 -07001/*
2 * Copyright (C) 2012 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 Millerdcb3d842012-08-23 19:18:12 -070017
Jason Monkea03be12017-12-04 11:08:41 -050018import static com.android.internal.util.LatencyTracker.ACTION_CHECK_CREDENTIAL;
19import static com.android.internal.util.LatencyTracker.ACTION_CHECK_CREDENTIAL_UNLOCKED;
Jorim Jaggied3032b2016-10-07 16:49:34 +020020
Jim Millerdcb3d842012-08-23 19:18:12 -070021import android.content.Context;
Jason Chang1e4a4bd2018-05-22 17:30:19 +080022import android.content.res.ColorStateList;
Jim Miller57375342012-09-09 15:20:31 -070023import android.graphics.Rect;
Xiyuan Xia6e380582015-05-05 15:16:08 -070024import android.os.AsyncTask;
Jim Millerdcb3d842012-08-23 19:18:12 -070025import android.os.CountDownTimer;
26import android.os.SystemClock;
Selim Cinek30181972014-05-30 03:33:06 +020027import android.text.TextUtils;
Jim Millerdcb3d842012-08-23 19:18:12 -070028import android.util.AttributeSet;
29import android.util.Log;
30import android.view.MotionEvent;
31import android.view.View;
Selim Cinek30181972014-05-30 03:33:06 +020032import android.view.ViewGroup;
Jorim Jaggi98f85302014-08-07 17:45:04 +020033import android.view.animation.AnimationUtils;
Selim Cinek30181972014-05-30 03:33:06 +020034import android.view.animation.Interpolator;
Jim Miller3af630c2012-09-26 14:29:18 -070035import android.widget.LinearLayout;
Jim Millerdcb3d842012-08-23 19:18:12 -070036
Lucas Dupin2e838ac2019-04-17 16:50:58 -070037import com.android.internal.annotations.VisibleForTesting;
Jason Monkea03be12017-12-04 11:08:41 -050038import com.android.internal.util.LatencyTracker;
Xiyuan Xia6e380582015-05-05 15:16:08 -070039import com.android.internal.widget.LockPatternChecker;
Jim Millerdcb3d842012-08-23 19:18:12 -070040import com.android.internal.widget.LockPatternUtils;
41import com.android.internal.widget.LockPatternView;
Jorim Jaggi56733532015-06-09 15:25:40 -070042import com.android.settingslib.animation.AppearAnimationCreator;
43import com.android.settingslib.animation.AppearAnimationUtils;
44import com.android.settingslib.animation.DisappearAnimationUtils;
Jim Millerdcb3d842012-08-23 19:18:12 -070045
Jim Millerdcb3d842012-08-23 19:18:12 -070046import java.util.List;
47
Selim Cinek30181972014-05-30 03:33:06 +020048public class KeyguardPatternView extends LinearLayout implements KeyguardSecurityView,
Andrew Lee72b46d42015-01-30 13:23:21 -080049 AppearAnimationCreator<LockPatternView.CellState>,
50 EmergencyButton.EmergencyButtonCallback {
Jim Millerdcb3d842012-08-23 19:18:12 -070051
52 private static final String TAG = "SecurityPatternView";
Jorim Jaggi5cf17872014-03-26 18:31:48 +010053 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Jim Millerdcb3d842012-08-23 19:18:12 -070054
55 // how long before we clear the wrong pattern
56 private static final int PATTERN_CLEAR_TIMEOUT_MS = 2000;
57
58 // how long we stay awake after each key beyond MIN_PATTERN_BEFORE_POKE_WAKELOCK
59 private static final int UNLOCK_PATTERN_WAKE_INTERVAL_MS = 7000;
60
Jim Millerdcb3d842012-08-23 19:18:12 -070061 // how many cells the user has to cross before we poke the wakelock
62 private static final int MIN_PATTERN_BEFORE_POKE_WAKELOCK = 2;
63
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070064 // How much we scale up the duration of the disappear animation when the current user is locked
65 public static final float DISAPPEAR_MULTIPLIER_LOCKED = 1.5f;
66
Lucas Dupinabb18f42019-05-20 19:10:22 -070067 // Extra padding, in pixels, that should eat touch events.
68 private static final int PATTERNS_TOUCH_AREA_EXTENSION = 40;
69
Adrian Roos7c11f8b2014-03-10 17:00:11 +010070 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Selim Cinek30181972014-05-30 03:33:06 +020071 private final AppearAnimationUtils mAppearAnimationUtils;
Selim Cinekf9c0e8f2014-11-11 13:41:02 +010072 private final DisappearAnimationUtils mDisappearAnimationUtils;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -070073 private final DisappearAnimationUtils mDisappearAnimationUtilsLocked;
Lucas Dupin727890f2019-05-15 14:32:50 -070074 private final int[] mTmpPosition = new int[2];
75 private final Rect mTempRect = new Rect();
76 private final Rect mLockPatternScreenBounds = new Rect();
Adrian Roos7c11f8b2014-03-10 17:00:11 +010077
Jim Millerdcb3d842012-08-23 19:18:12 -070078 private CountDownTimer mCountdownTimer = null;
79 private LockPatternUtils mLockPatternUtils;
Xiyuan Xia6e380582015-05-05 15:16:08 -070080 private AsyncTask<?, ?, ?> mPendingLockCheck;
Jim Millerdcb3d842012-08-23 19:18:12 -070081 private LockPatternView mLockPatternView;
Jim Millerdcb3d842012-08-23 19:18:12 -070082 private KeyguardSecurityCallback mCallback;
Jim Millerdcb3d842012-08-23 19:18:12 -070083
84 /**
85 * Keeps track of the last time we poked the wake lock during dispatching of the touch event.
86 * Initialized to something guaranteed to make us poke the wakelock when the user starts
87 * drawing the pattern.
88 * @see #dispatchTouchEvent(android.view.MotionEvent)
89 */
90 private long mLastPokeTime = -UNLOCK_PATTERN_WAKE_INTERVAL_MS;
91
92 /**
93 * Useful for clearing out the wrong pattern after a delay
94 */
95 private Runnable mCancelPatternRunnable = new Runnable() {
Jim Miller51e3a672015-07-31 18:42:53 -070096 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -070097 public void run() {
98 mLockPatternView.clearPattern();
99 }
100 };
Lucas Dupin2e838ac2019-04-17 16:50:58 -0700101 @VisibleForTesting
102 KeyguardMessageArea mSecurityMessageDisplay;
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500103 private View mEcaView;
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100104 private ViewGroup mContainer;
Jorim Jaggi76a16232014-08-08 17:00:47 +0200105 private int mDisappearYTranslation;
Jim Millerdcb3d842012-08-23 19:18:12 -0700106
107 enum FooterMode {
108 Normal,
109 ForgotLockPattern,
110 VerifyUnlocked
111 }
112
113 public KeyguardPatternView(Context context) {
114 this(context, null);
115 }
116
117 public KeyguardPatternView(Context context, AttributeSet attrs) {
118 super(context, attrs);
Adrian Roos7c11f8b2014-03-10 17:00:11 +0100119 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
Jorim Jaggi98f85302014-08-07 17:45:04 +0200120 mAppearAnimationUtils = new AppearAnimationUtils(context,
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100121 AppearAnimationUtils.DEFAULT_APPEAR_DURATION, 1.5f /* translationScale */,
122 2.0f /* delayScale */, AnimationUtils.loadInterpolator(
Jorim Jaggi98f85302014-08-07 17:45:04 +0200123 mContext, android.R.interpolator.linear_out_slow_in));
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100124 mDisappearAnimationUtils = new DisappearAnimationUtils(context,
125 125, 1.2f /* translationScale */,
Jorim Jaggi94f6f0612015-06-25 14:47:24 -0700126 0.6f /* delayScale */, AnimationUtils.loadInterpolator(
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100127 mContext, android.R.interpolator.fast_out_linear_in));
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700128 mDisappearAnimationUtilsLocked = new DisappearAnimationUtils(context,
129 (long) (125 * DISAPPEAR_MULTIPLIER_LOCKED), 1.2f /* translationScale */,
130 0.6f /* delayScale */, AnimationUtils.loadInterpolator(
131 mContext, android.R.interpolator.fast_out_linear_in));
Jorim Jaggi76a16232014-08-08 17:00:47 +0200132 mDisappearYTranslation = getResources().getDimensionPixelSize(
133 R.dimen.disappear_y_translation);
Jim Millerdcb3d842012-08-23 19:18:12 -0700134 }
135
Jim Miller51e3a672015-07-31 18:42:53 -0700136 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700137 public void setKeyguardCallback(KeyguardSecurityCallback callback) {
138 mCallback = callback;
139 }
140
Jim Miller51e3a672015-07-31 18:42:53 -0700141 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700142 public void setLockPatternUtils(LockPatternUtils utils) {
143 mLockPatternUtils = utils;
144 }
145
146 @Override
147 protected void onFinishInflate() {
148 super.onFinishInflate();
Jim Millerdcb3d842012-08-23 19:18:12 -0700149 mLockPatternUtils = mLockPatternUtils == null
150 ? new LockPatternUtils(mContext) : mLockPatternUtils;
151
Alan Viverette51efddb2017-04-05 10:00:01 -0400152 mLockPatternView = findViewById(R.id.lockPatternView);
Jim Millerdcb3d842012-08-23 19:18:12 -0700153 mLockPatternView.setSaveEnabled(false);
Jim Millerdcb3d842012-08-23 19:18:12 -0700154 mLockPatternView.setOnPatternListener(new UnlockPatternListener());
Lucas Dupine1732742018-08-23 18:26:49 -0700155 mLockPatternView.setInStealthMode(!mLockPatternUtils.isVisiblePatternEnabled(
156 KeyguardUpdateMonitor.getCurrentUser()));
Jim Millerdcb3d842012-08-23 19:18:12 -0700157
Jim Millerdcb3d842012-08-23 19:18:12 -0700158 // vibrate mode will be the same for the life of this screen
159 mLockPatternView.setTactileFeedbackEnabled(mLockPatternUtils.isTactileFeedbackEnabled());
160
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500161 mEcaView = findViewById(R.id.keyguard_selector_fade_container);
Alan Viverette51efddb2017-04-05 10:00:01 -0400162 mContainer = findViewById(R.id.container);
Andrew Lee72b46d42015-01-30 13:23:21 -0800163
Alan Viverette51efddb2017-04-05 10:00:01 -0400164 EmergencyButton button = findViewById(R.id.emergency_call_button);
Andrew Leef98f7b92015-02-02 15:50:29 -0800165 if (button != null) {
166 button.setCallback(this);
167 }
Jian Jin44e4d822018-04-06 12:40:50 -0700168
169 View cancelBtn = findViewById(R.id.cancel_button);
170 if (cancelBtn != null) {
171 cancelBtn.setOnClickListener(view -> {
172 mCallback.reset();
Aarthi Balachander0a427ef2018-07-13 15:00:58 -0700173 mCallback.onCancelClicked();
Jian Jin44e4d822018-04-06 12:40:50 -0700174 });
175 }
Andrew Lee72b46d42015-01-30 13:23:21 -0800176 }
177
Jim Miller51e3a672015-07-31 18:42:53 -0700178 @Override
Lucas Dupin2e838ac2019-04-17 16:50:58 -0700179 protected void onAttachedToWindow() {
180 super.onAttachedToWindow();
181 mSecurityMessageDisplay = KeyguardMessageArea.findSecurityMessageDisplay(this);
182 }
183
184 @Override
Andrew Lee72b46d42015-01-30 13:23:21 -0800185 public void onEmergencyButtonClickedWhenInCall() {
186 mCallback.reset();
Jim Millerdcb3d842012-08-23 19:18:12 -0700187 }
188
Jim Millerdcb3d842012-08-23 19:18:12 -0700189 @Override
Jim Millerd2b82f72012-09-18 20:52:55 -0700190 public boolean onTouchEvent(MotionEvent ev) {
191 boolean result = super.onTouchEvent(ev);
Jim Millerdcb3d842012-08-23 19:18:12 -0700192 // as long as the user is entering a pattern (i.e sending a touch event that was handled
193 // by this screen), keep poking the wake lock so that the screen will stay on.
194 final long elapsed = SystemClock.elapsedRealtime() - mLastPokeTime;
195 if (result && (elapsed > (UNLOCK_PATTERN_WAKE_INTERVAL_MS - 100))) {
196 mLastPokeTime = SystemClock.elapsedRealtime();
197 }
Jim Miller57375342012-09-09 15:20:31 -0700198 mTempRect.set(0, 0, 0, 0);
199 offsetRectIntoDescendantCoords(mLockPatternView, mTempRect);
200 ev.offsetLocation(mTempRect.left, mTempRect.top);
201 result = mLockPatternView.dispatchTouchEvent(ev) || result;
202 ev.offsetLocation(-mTempRect.left, -mTempRect.top);
Jim Millerdcb3d842012-08-23 19:18:12 -0700203 return result;
204 }
205
Jim Miller51e3a672015-07-31 18:42:53 -0700206 @Override
Lucas Dupin727890f2019-05-15 14:32:50 -0700207 protected void onLayout(boolean changed, int l, int t, int r, int b) {
208 super.onLayout(changed, l, t, r, b);
209 mLockPatternView.getLocationOnScreen(mTmpPosition);
Lucas Dupinabb18f42019-05-20 19:10:22 -0700210 mLockPatternScreenBounds.set(mTmpPosition[0] - PATTERNS_TOUCH_AREA_EXTENSION,
211 mTmpPosition[1] - PATTERNS_TOUCH_AREA_EXTENSION,
212 mTmpPosition[0] + mLockPatternView.getWidth() + PATTERNS_TOUCH_AREA_EXTENSION,
213 mTmpPosition[1] + mLockPatternView.getHeight() + PATTERNS_TOUCH_AREA_EXTENSION);
Lucas Dupin727890f2019-05-15 14:32:50 -0700214 }
215
216 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700217 public void reset() {
218 // reset lock pattern
Toni Barzic17fdb132015-10-16 13:38:11 -0700219 mLockPatternView.setInStealthMode(!mLockPatternUtils.isVisiblePatternEnabled(
220 KeyguardUpdateMonitor.getCurrentUser()));
Jim Millerdcb3d842012-08-23 19:18:12 -0700221 mLockPatternView.enableInput();
222 mLockPatternView.setEnabled(true);
223 mLockPatternView.clearPattern();
224
Lucas Dupin2e838ac2019-04-17 16:50:58 -0700225 if (mSecurityMessageDisplay == null) {
226 return;
227 }
228
Jim Millerdcb3d842012-08-23 19:18:12 -0700229 // if the user is currently locked out, enforce it.
Adrian Roos8150d2a2015-04-16 17:11:20 -0700230 long deadline = mLockPatternUtils.getLockoutAttemptDeadline(
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700231 KeyguardUpdateMonitor.getCurrentUser());
Jim Millerdcb3d842012-08-23 19:18:12 -0700232 if (deadline != 0) {
233 handleAttemptLockout(deadline);
234 } else {
Danielle Millett1625e872012-11-01 15:00:12 -0400235 displayDefaultSecurityMessage();
Jim Millerdcb3d842012-08-23 19:18:12 -0700236 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700237 }
238
Danielle Millett1625e872012-11-01 15:00:12 -0400239 private void displayDefaultSecurityMessage() {
Lucas Dupin2e838ac2019-04-17 16:50:58 -0700240 if (mSecurityMessageDisplay != null) {
241 mSecurityMessageDisplay.setMessage("");
242 }
Danielle Millett1625e872012-11-01 15:00:12 -0400243 }
244
Adam Cohen6fb841f2012-10-24 13:15:38 -0700245 @Override
246 public void showUsabilityHint() {
247 }
248
Lucas Dupin7156bc72019-05-03 19:37:39 -0700249 @Override
250 public boolean disallowInterceptTouch(MotionEvent event) {
Lucas Dupin727890f2019-05-15 14:32:50 -0700251 return mLockPatternScreenBounds.contains((int) event.getRawX(), (int) event.getRawY());
Lucas Dupin7156bc72019-05-03 19:37:39 -0700252 }
253
Jim Millerdcb3d842012-08-23 19:18:12 -0700254 /** TODO: hook this up */
255 public void cleanUp() {
256 if (DEBUG) Log.v(TAG, "Cleanup() called on " + this);
257 mLockPatternUtils = null;
258 mLockPatternView.setOnPatternListener(null);
259 }
260
Jim Millerdcb3d842012-08-23 19:18:12 -0700261 private class UnlockPatternListener implements LockPatternView.OnPatternListener {
262
Jim Miller51e3a672015-07-31 18:42:53 -0700263 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700264 public void onPatternStart() {
265 mLockPatternView.removeCallbacks(mCancelPatternRunnable);
Adrian Roosdb327e92016-10-12 16:41:28 -0700266 mSecurityMessageDisplay.setMessage("");
Jim Millerdcb3d842012-08-23 19:18:12 -0700267 }
268
Jim Miller51e3a672015-07-31 18:42:53 -0700269 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700270 public void onPatternCleared() {
271 }
272
Jim Miller51e3a672015-07-31 18:42:53 -0700273 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700274 public void onPatternCellAdded(List<LockPatternView.Cell> pattern) {
Jorim Jaggib690f0d2014-07-03 23:25:44 +0200275 mCallback.userActivity();
Jim Millerdcb3d842012-08-23 19:18:12 -0700276 }
277
Jim Miller51e3a672015-07-31 18:42:53 -0700278 @Override
Xiyuan Xia6e380582015-05-05 15:16:08 -0700279 public void onPatternDetected(final List<LockPatternView.Cell> pattern) {
280 mLockPatternView.disableInput();
281 if (mPendingLockCheck != null) {
282 mPendingLockCheck.cancel(false);
283 }
284
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800285 final int userId = KeyguardUpdateMonitor.getCurrentUser();
Andres Morales23974272015-05-14 22:42:26 -0700286 if (pattern.size() < LockPatternUtils.MIN_PATTERN_REGISTER_FAIL) {
287 mLockPatternView.enableInput();
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800288 onPatternChecked(userId, false, 0, false /* not valid - too short */);
Andres Morales23974272015-05-14 22:42:26 -0700289 return;
290 }
291
Jorim Jaggied3032b2016-10-07 16:49:34 +0200292 if (LatencyTracker.isEnabled(mContext)) {
293 LatencyTracker.getInstance(mContext).onActionStart(ACTION_CHECK_CREDENTIAL);
294 LatencyTracker.getInstance(mContext).onActionStart(ACTION_CHECK_CREDENTIAL_UNLOCKED);
295 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700296 mPendingLockCheck = LockPatternChecker.checkPattern(
297 mLockPatternUtils,
298 pattern,
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800299 userId,
Xiyuan Xia6e380582015-05-05 15:16:08 -0700300 new LockPatternChecker.OnCheckCallback() {
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700301
302 @Override
303 public void onEarlyMatched() {
Jorim Jaggied3032b2016-10-07 16:49:34 +0200304 if (LatencyTracker.isEnabled(mContext)) {
305 LatencyTracker.getInstance(mContext).onActionEnd(
306 ACTION_CHECK_CREDENTIAL);
307 }
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700308 onPatternChecked(userId, true /* matched */, 0 /* timeoutMs */,
309 true /* isValidPattern */);
310 }
311
Xiyuan Xia6e380582015-05-05 15:16:08 -0700312 @Override
Andres Morales23974272015-05-14 22:42:26 -0700313 public void onChecked(boolean matched, int timeoutMs) {
Jorim Jaggied3032b2016-10-07 16:49:34 +0200314 if (LatencyTracker.isEnabled(mContext)) {
315 LatencyTracker.getInstance(mContext).onActionEnd(
316 ACTION_CHECK_CREDENTIAL_UNLOCKED);
317 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700318 mLockPatternView.enableInput();
319 mPendingLockCheck = null;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700320 if (!matched) {
321 onPatternChecked(userId, false /* matched */, timeoutMs,
322 true /* isValidPattern */);
323 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700324 }
Jorim Jaggied3032b2016-10-07 16:49:34 +0200325
326 @Override
327 public void onCancelled() {
328 // We already got dismissed with the early matched callback, so we
329 // cancelled the check. However, we still need to note down the latency.
330 if (LatencyTracker.isEnabled(mContext)) {
331 LatencyTracker.getInstance(mContext).onActionEnd(
332 ACTION_CHECK_CREDENTIAL_UNLOCKED);
333 }
334 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700335 });
Jim Miller51e3a672015-07-31 18:42:53 -0700336 if (pattern.size() > MIN_PATTERN_BEFORE_POKE_WAKELOCK) {
337 mCallback.userActivity();
338 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700339 }
340
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800341 private void onPatternChecked(int userId, boolean matched, int timeoutMs,
342 boolean isValidPattern) {
343 boolean dismissKeyguard = KeyguardUpdateMonitor.getCurrentUser() == userId;
Xiyuan Xia6e380582015-05-05 15:16:08 -0700344 if (matched) {
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800345 mCallback.reportUnlockAttempt(userId, true, 0);
346 if (dismissKeyguard) {
347 mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Correct);
Vadim Tryshev8702ca72016-04-22 08:14:12 -0700348 mCallback.dismiss(true, userId);
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800349 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700350 } else {
Jim Millerdcb3d842012-08-23 19:18:12 -0700351 mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
Jim Miller51e3a672015-07-31 18:42:53 -0700352 if (isValidPattern) {
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800353 mCallback.reportUnlockAttempt(userId, false, timeoutMs);
Jim Miller51e3a672015-07-31 18:42:53 -0700354 if (timeoutMs > 0) {
355 long deadline = mLockPatternUtils.setLockoutAttemptDeadline(
Xiyuan Xiace64cea2016-01-06 08:51:16 -0800356 userId, timeoutMs);
Jim Miller51e3a672015-07-31 18:42:53 -0700357 handleAttemptLockout(deadline);
358 }
359 }
360 if (timeoutMs == 0) {
Adrian Roosdb327e92016-10-12 16:41:28 -0700361 mSecurityMessageDisplay.setMessage(R.string.kg_wrong_pattern);
Jim Millerdcb3d842012-08-23 19:18:12 -0700362 mLockPatternView.postDelayed(mCancelPatternRunnable, PATTERN_CLEAR_TIMEOUT_MS);
363 }
364 }
365 }
366 }
367
Jim Millerdcb3d842012-08-23 19:18:12 -0700368 private void handleAttemptLockout(long elapsedRealtimeDeadline) {
369 mLockPatternView.clearPattern();
370 mLockPatternView.setEnabled(false);
371 final long elapsedRealtime = SystemClock.elapsedRealtime();
Hidenari Koshimae5be868c2014-08-21 11:27:33 +0200372 final long secondsInFuture = (long) Math.ceil(
373 (elapsedRealtimeDeadline - elapsedRealtime) / 1000.0);
374 mCountdownTimer = new CountDownTimer(secondsInFuture * 1000, 1000) {
Jim Millerdcb3d842012-08-23 19:18:12 -0700375
376 @Override
377 public void onTick(long millisUntilFinished) {
Hidenari Koshimae5be868c2014-08-21 11:27:33 +0200378 final int secondsRemaining = (int) Math.round(millisUntilFinished / 1000.0);
Kevin Chyne01c5e12017-08-29 19:04:25 -0700379 mSecurityMessageDisplay.setMessage(mContext.getResources().getQuantityString(
380 R.plurals.kg_too_many_failed_attempts_countdown,
381 secondsRemaining, secondsRemaining));
Jim Millerdcb3d842012-08-23 19:18:12 -0700382 }
383
384 @Override
385 public void onFinish() {
386 mLockPatternView.setEnabled(true);
Danielle Millett1625e872012-11-01 15:00:12 -0400387 displayDefaultSecurityMessage();
Jim Millerdcb3d842012-08-23 19:18:12 -0700388 }
389
390 }.start();
391 }
392
393 @Override
394 public boolean needsInput() {
395 return false;
396 }
397
398 @Override
399 public void onPause() {
400 if (mCountdownTimer != null) {
401 mCountdownTimer.cancel();
402 mCountdownTimer = null;
403 }
Xiyuan Xia6e380582015-05-05 15:16:08 -0700404 if (mPendingLockCheck != null) {
405 mPendingLockCheck.cancel(false);
406 mPendingLockCheck = null;
407 }
Lucas Dupinc6b50c62018-10-09 20:00:35 -0700408 displayDefaultSecurityMessage();
Jim Millerdcb3d842012-08-23 19:18:12 -0700409 }
410
411 @Override
Chris Wrena042ac92012-11-07 11:37:06 -0500412 public void onResume(int reason) {
Jim Millerdcb3d842012-08-23 19:18:12 -0700413 }
414
415 @Override
416 public KeyguardSecurityCallback getCallback() {
417 return mCallback;
418 }
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500419
420 @Override
Selim Cinek3122fa82015-06-18 01:38:59 -0700421 public void showPromptReason(int reason) {
422 switch (reason) {
423 case PROMPT_REASON_RESTART:
Adrian Roosdb327e92016-10-12 16:41:28 -0700424 mSecurityMessageDisplay.setMessage(R.string.kg_prompt_reason_restart_pattern);
Selim Cinek3122fa82015-06-18 01:38:59 -0700425 break;
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700426 case PROMPT_REASON_TIMEOUT:
Adrian Roosdb327e92016-10-12 16:41:28 -0700427 mSecurityMessageDisplay.setMessage(R.string.kg_prompt_reason_timeout_pattern);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700428 break;
Adrian Roosd6d253a2016-04-13 13:06:21 -0700429 case PROMPT_REASON_DEVICE_ADMIN:
Adrian Roosdb327e92016-10-12 16:41:28 -0700430 mSecurityMessageDisplay.setMessage(R.string.kg_prompt_reason_device_admin);
Adrian Roosd6d253a2016-04-13 13:06:21 -0700431 break;
432 case PROMPT_REASON_USER_REQUEST:
Adrian Roosdb327e92016-10-12 16:41:28 -0700433 mSecurityMessageDisplay.setMessage(R.string.kg_prompt_reason_user_request);
Adrian Roosd6d253a2016-04-13 13:06:21 -0700434 break;
Adrian Roosc13723f2016-01-12 20:29:03 +0100435 case PROMPT_REASON_NONE:
436 break;
Selim Cinek3122fa82015-06-18 01:38:59 -0700437 default:
Adrian Roosdb327e92016-10-12 16:41:28 -0700438 mSecurityMessageDisplay.setMessage(R.string.kg_prompt_reason_timeout_pattern);
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700439 break;
Selim Cinek3122fa82015-06-18 01:38:59 -0700440 }
441 }
442
443 @Override
Jason Chang1e4a4bd2018-05-22 17:30:19 +0800444 public void showMessage(CharSequence message, ColorStateList colorState) {
445 mSecurityMessageDisplay.setNextMessageColor(colorState);
Adrian Roosdb327e92016-10-12 16:41:28 -0700446 mSecurityMessageDisplay.setMessage(message);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700447 }
448
449 @Override
Jorim Jaggic14f8292014-05-27 02:25:45 +0200450 public void startAppearAnimation() {
Selim Cinek30181972014-05-30 03:33:06 +0200451 enableClipping(false);
Jorim Jaggifb28c0e2014-08-22 16:28:42 +0200452 setAlpha(1f);
Jorim Jaggi76a16232014-08-08 17:00:47 +0200453 setTranslationY(mAppearAnimationUtils.getStartTranslation());
Jorim Jaggi613f55f2015-07-16 15:30:10 -0700454 AppearAnimationUtils.startTranslationYAnimation(this, 0 /* delay */, 500 /* duration */,
455 0, mAppearAnimationUtils.getInterpolator());
Jorim Jaggi56733532015-06-09 15:25:40 -0700456 mAppearAnimationUtils.startAnimation2d(
Selim Cinek30181972014-05-30 03:33:06 +0200457 mLockPatternView.getCellStates(),
458 new Runnable() {
459 @Override
460 public void run() {
461 enableClipping(true);
462 }
463 },
464 this);
Selim Cinek3122fa82015-06-18 01:38:59 -0700465 if (!TextUtils.isEmpty(mSecurityMessageDisplay.getText())) {
466 mAppearAnimationUtils.createAnimation(mSecurityMessageDisplay, 0,
Jorim Jaggi98f85302014-08-07 17:45:04 +0200467 AppearAnimationUtils.DEFAULT_APPEAR_DURATION,
Selim Cinek30181972014-05-30 03:33:06 +0200468 mAppearAnimationUtils.getStartTranslation(),
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100469 true /* appearing */,
Selim Cinek30181972014-05-30 03:33:06 +0200470 mAppearAnimationUtils.getInterpolator(),
471 null /* finishRunnable */);
472 }
473 }
474
Jorim Jaggi76a16232014-08-08 17:00:47 +0200475 @Override
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100476 public boolean startDisappearAnimation(final Runnable finishRunnable) {
Jorim Jaggi031f7952016-09-01 16:39:26 -0700477 float durationMultiplier = mKeyguardUpdateMonitor.needsSlowUnlockTransition()
478 ? DISAPPEAR_MULTIPLIER_LOCKED
479 : 1f;
Jorim Jaggi76a16232014-08-08 17:00:47 +0200480 mLockPatternView.clearPattern();
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100481 enableClipping(false);
482 setTranslationY(0);
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700483 AppearAnimationUtils.startTranslationYAnimation(this, 0 /* delay */,
484 (long) (300 * durationMultiplier),
Jorim Jaggi613f55f2015-07-16 15:30:10 -0700485 -mDisappearAnimationUtils.getStartTranslation(),
486 mDisappearAnimationUtils.getInterpolator());
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700487
Jorim Jaggi031f7952016-09-01 16:39:26 -0700488 DisappearAnimationUtils disappearAnimationUtils = mKeyguardUpdateMonitor
489 .needsSlowUnlockTransition()
490 ? mDisappearAnimationUtilsLocked
491 : mDisappearAnimationUtils;
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700492 disappearAnimationUtils.startAnimation2d(mLockPatternView.getCellStates(),
493 () -> {
494 enableClipping(true);
495 if (finishRunnable != null) {
496 finishRunnable.run();
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100497 }
498 }, KeyguardPatternView.this);
Selim Cinek3122fa82015-06-18 01:38:59 -0700499 if (!TextUtils.isEmpty(mSecurityMessageDisplay.getText())) {
500 mDisappearAnimationUtils.createAnimation(mSecurityMessageDisplay, 0,
Jorim Jaggie8fde5d2016-06-30 23:41:37 -0700501 (long) (200 * durationMultiplier),
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100502 - mDisappearAnimationUtils.getStartTranslation() * 3,
503 false /* appearing */,
504 mDisappearAnimationUtils.getInterpolator(),
505 null /* finishRunnable */);
506 }
Jorim Jaggi76a16232014-08-08 17:00:47 +0200507 return true;
508 }
509
Selim Cinek30181972014-05-30 03:33:06 +0200510 private void enableClipping(boolean enable) {
511 setClipChildren(enable);
Jorim Jaggi6b88cdf2014-12-22 20:56:50 +0100512 mContainer.setClipToPadding(enable);
513 mContainer.setClipChildren(enable);
Selim Cinek30181972014-05-30 03:33:06 +0200514 }
515
516 @Override
517 public void createAnimation(final LockPatternView.CellState animatedCell, long delay,
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100518 long duration, float translationY, final boolean appearing,
519 Interpolator interpolator,
Selim Cinek30181972014-05-30 03:33:06 +0200520 final Runnable finishListener) {
Jorim Jaggi613f55f2015-07-16 15:30:10 -0700521 mLockPatternView.startCellStateAnimation(animatedCell,
522 1f, appearing ? 1f : 0f, /* alpha */
523 appearing ? translationY : 0f, appearing ? 0f : translationY, /* translation */
524 appearing ? 0f : 1f, 1f /* scale */,
525 delay, duration, interpolator, finishListener);
Selim Cinek30181972014-05-30 03:33:06 +0200526 if (finishListener != null) {
Selim Cinek30181972014-05-30 03:33:06 +0200527 // Also animate the Emergency call
Selim Cinekf9c0e8f2014-11-11 13:41:02 +0100528 mAppearAnimationUtils.createAnimation(mEcaView, delay, duration, translationY,
529 appearing, interpolator, null);
Selim Cinek30181972014-05-30 03:33:06 +0200530 }
Jorim Jaggic14f8292014-05-27 02:25:45 +0200531 }
Jorim Jaggi76a16232014-08-08 17:00:47 +0200532
533 @Override
534 public boolean hasOverlappingRendering() {
535 return false;
536 }
Phil Weaver7d847b02018-02-13 16:02:35 -0800537
538 @Override
539 public CharSequence getTitle() {
540 return getContext().getString(
541 com.android.internal.R.string.keyguard_accessibility_pattern_unlock);
542 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700543}