blob: fdfd8e88b44655e9234521f6cba107decfd71840 [file] [log] [blame]
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.phone;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.PowerManager;
22import android.os.SystemClock;
Nick Desaulniers1d396752016-07-25 15:05:33 -070023import android.os.Trace;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070024import android.util.Log;
25
26import com.android.keyguard.KeyguardConstants;
27import com.android.keyguard.KeyguardUpdateMonitor;
28import com.android.keyguard.KeyguardUpdateMonitorCallback;
Jorim Jaggid0565172016-09-15 16:31:14 -070029import com.android.keyguard.LatencyTracker;
Jason Monk421a9412017-02-06 09:15:21 -080030import com.android.systemui.Dependency;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070031import com.android.systemui.keyguard.KeyguardViewMediator;
Adrian Roos7a5e4c92017-07-31 16:40:19 +020032import com.android.systemui.keyguard.ScreenLifecycle;
Adrian Roos58ba6852017-07-18 17:10:29 +020033import com.android.systemui.keyguard.WakefulnessLifecycle;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070034
35/**
36 * Controller which coordinates all the fingerprint unlocking actions with the UI.
37 */
38public class FingerprintUnlockController extends KeyguardUpdateMonitorCallback {
39
40 private static final String TAG = "FingerprintController";
41 private static final boolean DEBUG_FP_WAKELOCK = KeyguardConstants.DEBUG_FP_WAKELOCK;
42 private static final long FINGERPRINT_WAKELOCK_TIMEOUT_MS = 15 * 1000;
43 private static final String FINGERPRINT_WAKE_LOCK_NAME = "wake-and-unlock wakelock";
44
45 /**
46 * Mode in which we don't need to wake up the device when we get a fingerprint.
47 */
48 public static final int MODE_NONE = 0;
49
50 /**
51 * Mode in which we wake up the device, and directly dismiss Keyguard. Active when we acquire
52 * a fingerprint while the screen is off and the device was sleeping.
53 */
54 public static final int MODE_WAKE_AND_UNLOCK = 1;
55
56 /**
57 * Mode in which we wake the device up, and fade out the Keyguard contents because they were
58 * already visible while pulsing in doze mode.
59 */
60 public static final int MODE_WAKE_AND_UNLOCK_PULSING = 2;
61
62 /**
63 * Mode in which we wake up the device, but play the normal dismiss animation. Active when we
64 * acquire a fingerprint pulsing in doze mode.
65 */
66 public static final int MODE_SHOW_BOUNCER = 3;
67
68 /**
69 * Mode in which we only wake up the device, and keyguard was not showing when we acquired a
70 * fingerprint.
71 * */
72 public static final int MODE_ONLY_WAKE = 4;
73
74 /**
75 * Mode in which fingerprint unlocks the device.
76 */
77 public static final int MODE_UNLOCK = 5;
78
79 /**
80 * Mode in which fingerprint brings up the bouncer because fingerprint unlocking is currently
81 * not allowed.
82 */
83 public static final int MODE_DISMISS_BOUNCER = 6;
84
85 /**
86 * How much faster we collapse the lockscreen when authenticating with fingerprint.
87 */
Selim Cinekdf5501b2017-04-14 20:01:27 -070088 private static final float FINGERPRINT_COLLAPSE_SPEEDUP_FACTOR = 1.1f;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -070089
90 private PowerManager mPowerManager;
91 private Handler mHandler = new Handler();
92 private PowerManager.WakeLock mWakeLock;
93 private KeyguardUpdateMonitor mUpdateMonitor;
94 private int mMode;
95 private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
96 private StatusBarWindowManager mStatusBarWindowManager;
97 private DozeScrimController mDozeScrimController;
98 private KeyguardViewMediator mKeyguardViewMediator;
99 private ScrimController mScrimController;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500100 private StatusBar mStatusBar;
Jorim Jaggi8adb30c2016-09-13 15:02:22 -0700101 private final UnlockMethodCache mUnlockMethodCache;
102 private final Context mContext;
Jorim Jaggi95e40382015-09-16 15:53:42 -0700103 private int mPendingAuthenticatedUserId = -1;
Adrian Roos58ba6852017-07-18 17:10:29 +0200104 private boolean mPendingShowBouncer;
Adrian Roos7a5e4c92017-07-31 16:40:19 +0200105 private boolean mHasScreenTurnedOnSinceAuthenticating;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700106
107 public FingerprintUnlockController(Context context,
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700108 DozeScrimController dozeScrimController,
109 KeyguardViewMediator keyguardViewMediator,
110 ScrimController scrimController,
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500111 StatusBar statusBar,
Jorim Jaggi8adb30c2016-09-13 15:02:22 -0700112 UnlockMethodCache unlockMethodCache) {
113 mContext = context;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700114 mPowerManager = context.getSystemService(PowerManager.class);
115 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
116 mUpdateMonitor.registerCallback(this);
Adrian Roos58ba6852017-07-18 17:10:29 +0200117 Dependency.get(WakefulnessLifecycle.class).addObserver(mWakefulnessObserver);
Adrian Roos7a5e4c92017-07-31 16:40:19 +0200118 Dependency.get(ScreenLifecycle.class).addObserver(mScreenObserver);
Jason Monk421a9412017-02-06 09:15:21 -0800119 mStatusBarWindowManager = Dependency.get(StatusBarWindowManager.class);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700120 mDozeScrimController = dozeScrimController;
121 mKeyguardViewMediator = keyguardViewMediator;
122 mScrimController = scrimController;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500123 mStatusBar = statusBar;
Jorim Jaggi8adb30c2016-09-13 15:02:22 -0700124 mUnlockMethodCache = unlockMethodCache;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700125 }
126
127 public void setStatusBarKeyguardViewManager(
128 StatusBarKeyguardViewManager statusBarKeyguardViewManager) {
129 mStatusBarKeyguardViewManager = statusBarKeyguardViewManager;
130 }
131
132 private final Runnable mReleaseFingerprintWakeLockRunnable = new Runnable() {
133 @Override
134 public void run() {
135 if (DEBUG_FP_WAKELOCK) {
136 Log.i(TAG, "fp wakelock: TIMEOUT!!");
137 }
138 releaseFingerprintWakeLock();
139 }
140 };
141
142 private void releaseFingerprintWakeLock() {
143 if (mWakeLock != null) {
144 mHandler.removeCallbacks(mReleaseFingerprintWakeLockRunnable);
145 if (DEBUG_FP_WAKELOCK) {
146 Log.i(TAG, "releasing fp wakelock");
147 }
148 mWakeLock.release();
149 mWakeLock = null;
150 }
151 }
152
153 @Override
154 public void onFingerprintAcquired() {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700155 Trace.beginSection("FingerprintUnlockController#onFingerprintAcquired");
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700156 releaseFingerprintWakeLock();
157 if (!mUpdateMonitor.isDeviceInteractive()) {
Jorim Jaggi8adb30c2016-09-13 15:02:22 -0700158 if (LatencyTracker.isEnabled(mContext)) {
159 LatencyTracker.getInstance(mContext).onActionStart(
160 LatencyTracker.ACTION_FINGERPRINT_WAKE_AND_UNLOCK);
161 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700162 mWakeLock = mPowerManager.newWakeLock(
163 PowerManager.PARTIAL_WAKE_LOCK, FINGERPRINT_WAKE_LOCK_NAME);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700164 Trace.beginSection("acquiring wake-and-unlock");
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700165 mWakeLock.acquire();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700166 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700167 if (DEBUG_FP_WAKELOCK) {
168 Log.i(TAG, "fingerprint acquired, grabbing fp wakelock");
169 }
170 mHandler.postDelayed(mReleaseFingerprintWakeLockRunnable,
171 FINGERPRINT_WAKELOCK_TIMEOUT_MS);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700172 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700173 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700174 }
175
Adrian Roos3e23eb52017-07-07 15:58:57 +0200176 private boolean pulsingOrAod() {
177 boolean pulsing = mDozeScrimController.isPulsing();
178 boolean dozingWithScreenOn = mStatusBar.isDozing() && !mStatusBar.isScreenFullyOff();
179 return pulsing || dozingWithScreenOn;
180 }
181
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700182 @Override
183 public void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700184 Trace.beginSection("FingerprintUnlockController#onFingerprintAuthenticated");
Jorim Jaggi95e40382015-09-16 15:53:42 -0700185 if (mUpdateMonitor.isGoingToSleep()) {
186 mPendingAuthenticatedUserId = userId;
Nick Desaulniers1d396752016-07-25 15:05:33 -0700187 Trace.endSection();
Jorim Jaggi95e40382015-09-16 15:53:42 -0700188 return;
189 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700190 boolean wasDeviceInteractive = mUpdateMonitor.isDeviceInteractive();
191 mMode = calculateMode();
Adrian Roos7a5e4c92017-07-31 16:40:19 +0200192 mHasScreenTurnedOnSinceAuthenticating = false;
Adrian Roosb880afe72017-07-25 17:02:46 +0200193 if (mMode == MODE_WAKE_AND_UNLOCK_PULSING && pulsingOrAod()) {
194 // If we are waking the device up while we are pulsing the clock and the
195 // notifications would light up first, creating an unpleasant animation.
196 // Defer changing the screen brightness by forcing doze brightness on our window
197 // until the clock and the notifications are faded out.
198 mStatusBarWindowManager.setForceDozeBrightness(true);
199 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700200 if (!wasDeviceInteractive) {
201 if (DEBUG_FP_WAKELOCK) {
202 Log.i(TAG, "fp wakelock: Authenticated, waking up...");
203 }
Adam Lesinski5bcb8bb2015-11-03 16:31:25 -0800204 mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.policy:FINGERPRINT");
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700205 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700206 Trace.beginSection("release wake-and-unlock");
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700207 releaseFingerprintWakeLock();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700208 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700209 switch (mMode) {
210 case MODE_DISMISS_BOUNCER:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700211 Trace.beginSection("MODE_DISMISS");
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700212 mStatusBarKeyguardViewManager.notifyKeyguardAuthenticated(
213 false /* strongAuth */);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700214 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700215 break;
216 case MODE_UNLOCK:
217 case MODE_SHOW_BOUNCER:
Nick Desaulniers1d396752016-07-25 15:05:33 -0700218 Trace.beginSection("MODE_UNLOCK or MODE_SHOW_BOUNCER");
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700219 if (!wasDeviceInteractive) {
220 mStatusBarKeyguardViewManager.notifyDeviceWakeUpRequested();
Adrian Roos58ba6852017-07-18 17:10:29 +0200221 mPendingShowBouncer = true;
222 } else {
223 showBouncer();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700224 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700225 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700226 break;
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700227 case MODE_WAKE_AND_UNLOCK_PULSING:
Jorim Jaggid94d3a22015-08-21 16:52:55 -0700228 case MODE_WAKE_AND_UNLOCK:
Adrian Roos88e61aa2017-05-23 16:16:50 -0700229 if (mMode == MODE_WAKE_AND_UNLOCK_PULSING) {
230 Trace.beginSection("MODE_WAKE_AND_UNLOCK_PULSING");
231 mStatusBar.updateMediaMetaData(false /* metaDataChanged */,
232 true /* allowEnterAnimation */);
233 } else {
234 Trace.beginSection("MODE_WAKE_AND_UNLOCK");
Adrian Roos7a5e4c92017-07-31 16:40:19 +0200235
Adrian Roos88e61aa2017-05-23 16:16:50 -0700236 mDozeScrimController.abortDoze();
237 }
Jorim Jaggid94d3a22015-08-21 16:52:55 -0700238 mStatusBarWindowManager.setStatusBarFocusable(false);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700239 mKeyguardViewMediator.onWakeAndUnlocking();
240 mScrimController.setWakeAndUnlocking();
Adrian Roos88e61aa2017-05-23 16:16:50 -0700241 mDozeScrimController.setWakeAndUnlocking();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500242 if (mStatusBar.getNavigationBarView() != null) {
243 mStatusBar.getNavigationBarView().setWakeAndUnlocking(true);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700244 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700245 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700246 break;
247 case MODE_ONLY_WAKE:
248 case MODE_NONE:
249 break;
250 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500251 mStatusBar.notifyFpAuthModeChanged();
Nick Desaulniers1d396752016-07-25 15:05:33 -0700252 Trace.endSection();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700253 }
254
Adrian Roos58ba6852017-07-18 17:10:29 +0200255 private void showBouncer() {
256 mStatusBarKeyguardViewManager.animateCollapsePanels(
257 FINGERPRINT_COLLAPSE_SPEEDUP_FACTOR);
258 mPendingShowBouncer = false;
259 }
260
Jorim Jaggi95e40382015-09-16 15:53:42 -0700261 @Override
262 public void onStartedGoingToSleep(int why) {
263 mPendingAuthenticatedUserId = -1;
264 }
265
266 @Override
267 public void onFinishedGoingToSleep(int why) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700268 Trace.beginSection("FingerprintUnlockController#onFinishedGoingToSleep");
Jorim Jaggi95e40382015-09-16 15:53:42 -0700269 if (mPendingAuthenticatedUserId != -1) {
270
271 // Post this to make sure it's executed after the device is fully locked.
272 mHandler.post(new Runnable() {
273 @Override
274 public void run() {
275 onFingerprintAuthenticated(mPendingAuthenticatedUserId);
276 }
277 });
278 }
279 mPendingAuthenticatedUserId = -1;
Nick Desaulniers1d396752016-07-25 15:05:33 -0700280 Trace.endSection();
Jorim Jaggi95e40382015-09-16 15:53:42 -0700281 }
282
Adrian Roos710a0b12017-07-07 19:02:34 +0200283 public boolean hasPendingAuthentication() {
284 return mPendingAuthenticatedUserId != -1
285 && mUpdateMonitor.isUnlockingWithFingerprintAllowed()
286 && mPendingAuthenticatedUserId == KeyguardUpdateMonitor.getCurrentUser();
287 }
288
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700289 public int getMode() {
290 return mMode;
291 }
292
293 private int calculateMode() {
294 boolean unlockingAllowed = mUpdateMonitor.isUnlockingWithFingerprintAllowed();
Adrian Roos02626662017-07-06 18:22:17 +0200295
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700296 if (!mUpdateMonitor.isDeviceInteractive()) {
297 if (!mStatusBarKeyguardViewManager.isShowing()) {
298 return MODE_ONLY_WAKE;
Adrian Roose4cb6c8a2017-07-19 18:08:07 +0200299 } else if (mDozeScrimController.isPulsing() && unlockingAllowed) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700300 return MODE_WAKE_AND_UNLOCK_PULSING;
Jorim Jaggi8adb30c2016-09-13 15:02:22 -0700301 } else if (unlockingAllowed || !mUnlockMethodCache.isMethodSecure()) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700302 return MODE_WAKE_AND_UNLOCK;
303 } else {
304 return MODE_SHOW_BOUNCER;
305 }
306 }
307 if (mStatusBarKeyguardViewManager.isShowing()) {
308 if (mStatusBarKeyguardViewManager.isBouncerShowing() && unlockingAllowed) {
309 return MODE_DISMISS_BOUNCER;
310 } else if (unlockingAllowed) {
311 return MODE_UNLOCK;
Jorim Jaggibf04cf52015-09-02 11:53:02 -0700312 } else if (!mStatusBarKeyguardViewManager.isBouncerShowing()) {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700313 return MODE_SHOW_BOUNCER;
314 }
315 }
316 return MODE_NONE;
317 }
318
319 @Override
320 public void onFingerprintAuthFailed() {
321 cleanup();
322 }
323
324 @Override
325 public void onFingerprintError(int msgId, String errString) {
326 cleanup();
327 }
328
329 private void cleanup() {
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700330 releaseFingerprintWakeLock();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700331 }
332
333 public void startKeyguardFadingAway() {
334
335 // Disable brightness override when the ambient contents are fully invisible.
336 mHandler.postDelayed(new Runnable() {
337 @Override
338 public void run() {
339 mStatusBarWindowManager.setForceDozeBrightness(false);
340 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500341 }, StatusBar.FADE_KEYGUARD_DURATION_PULSING);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700342 }
343
344 public void finishKeyguardFadingAway() {
345 mMode = MODE_NONE;
Adrian Roos88e61aa2017-05-23 16:16:50 -0700346 mStatusBarWindowManager.setForceDozeBrightness(false);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500347 if (mStatusBar.getNavigationBarView() != null) {
348 mStatusBar.getNavigationBarView().setWakeAndUnlocking(false);
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700349 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500350 mStatusBar.notifyFpAuthModeChanged();
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700351 }
Adrian Roos58ba6852017-07-18 17:10:29 +0200352
353 private final WakefulnessLifecycle.Observer mWakefulnessObserver =
354 new WakefulnessLifecycle.Observer() {
355 @Override
356 public void onFinishedWakingUp() {
357 if (mPendingShowBouncer) {
358 FingerprintUnlockController.this.showBouncer();
359 }
360 }
361 };
Adrian Roos7a5e4c92017-07-31 16:40:19 +0200362
363 private final ScreenLifecycle.Observer mScreenObserver =
364 new ScreenLifecycle.Observer() {
365 @Override
366 public void onScreenTurnedOn() {
367 mHasScreenTurnedOnSinceAuthenticating = true;
368 }
369 };
370
371 public boolean hasScreenTurnedOnSinceAuthenticating() {
372 return mHasScreenTurnedOnSinceAuthenticating;
373 }
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700374}