Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -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.statusbar.phone |
| 18 | |
| 19 | import android.content.Context |
Mady Mellor | dce1bf3 | 2019-06-25 14:31:19 -0700 | [diff] [blame] | 20 | import android.content.pm.PackageManager |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 21 | import android.hardware.biometrics.BiometricSourceType |
Lucas Dupin | 1b883b4 | 2019-05-28 17:43:59 -0700 | [diff] [blame] | 22 | import android.hardware.face.FaceManager |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 23 | import android.provider.Settings |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 24 | import com.android.keyguard.KeyguardUpdateMonitor |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 25 | import com.android.systemui.plugins.statusbar.StatusBarStateController |
Selim Cinek | a6a3ebc | 2019-06-19 18:33:17 -0700 | [diff] [blame] | 26 | import com.android.systemui.statusbar.NotificationLockscreenUserManager |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 27 | import com.android.systemui.statusbar.StatusBarState |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 28 | import com.android.systemui.tuner.TunerService |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 29 | import javax.inject.Inject |
| 30 | import javax.inject.Singleton |
| 31 | |
| 32 | @Singleton |
| 33 | class KeyguardBypassController { |
| 34 | |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 35 | private val unlockMethodCache: UnlockMethodCache |
| 36 | private val statusBarStateController: StatusBarStateController |
| 37 | |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 38 | /** |
| 39 | * The pending unlock type which is set if the bypass was blocked when it happened. |
| 40 | */ |
| 41 | private var pendingUnlockType: BiometricSourceType? = null |
Lucas Dupin | 206fe56 | 2019-05-31 14:36:42 -0700 | [diff] [blame] | 42 | |
Selim Cinek | f89a5dc | 2019-06-18 15:10:25 -0700 | [diff] [blame] | 43 | lateinit var unlockController: BiometricUnlockController |
| 44 | var isPulseExpanding = false |
| 45 | |
| 46 | /** |
| 47 | * If face unlock dismisses the lock screen or keeps user on keyguard for the current user. |
| 48 | */ |
| 49 | var bypassEnabled: Boolean = false |
| 50 | get() = field && unlockMethodCache.isUnlockingWithFacePossible |
| 51 | private set |
| 52 | |
| 53 | var bouncerShowing: Boolean = false |
Selim Cinek | ec17775 | 2019-06-19 18:13:51 -0700 | [diff] [blame] | 54 | var launchingAffordance: Boolean = false |
Selim Cinek | ae2702d | 2019-06-19 18:04:05 -0700 | [diff] [blame] | 55 | var qSExpanded = false |
| 56 | set(value) { |
| 57 | val changed = field != value |
| 58 | field = value |
| 59 | if (changed && !value) { |
| 60 | maybePerformPendingUnlock() |
| 61 | } |
| 62 | } |
Selim Cinek | f89a5dc | 2019-06-18 15:10:25 -0700 | [diff] [blame] | 63 | |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 64 | @Inject |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 65 | constructor(context: Context, tunerService: TunerService, |
Selim Cinek | a6a3ebc | 2019-06-19 18:33:17 -0700 | [diff] [blame] | 66 | statusBarStateController: StatusBarStateController, |
| 67 | lockscreenUserManager: NotificationLockscreenUserManager) { |
Lucas Dupin | 206fe56 | 2019-05-31 14:36:42 -0700 | [diff] [blame] | 68 | unlockMethodCache = UnlockMethodCache.getInstance(context) |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 69 | this.statusBarStateController = statusBarStateController |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 70 | statusBarStateController.addCallback(object : StatusBarStateController.StateListener { |
| 71 | override fun onStateChanged(newState: Int) { |
| 72 | if (newState != StatusBarState.KEYGUARD) { |
| 73 | pendingUnlockType = null; |
| 74 | } |
| 75 | } |
| 76 | }) |
Mady Mellor | dce1bf3 | 2019-06-25 14:31:19 -0700 | [diff] [blame] | 77 | if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) { |
| 78 | return |
| 79 | } |
Lucas Dupin | 1b883b4 | 2019-05-28 17:43:59 -0700 | [diff] [blame] | 80 | val faceManager = context.getSystemService(FaceManager::class.java) |
| 81 | if (faceManager?.isHardwareDetected != true) { |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | val dismissByDefault = if (context.resources.getBoolean( |
| 86 | com.android.internal.R.bool.config_faceAuthDismissesKeyguard)) 1 else 0 |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 87 | tunerService.addTunable( |
| 88 | object : TunerService.Tunable { |
| 89 | override fun onTuningChanged(key: String?, newValue: String?) { |
| 90 | bypassEnabled = Settings.Secure.getIntForUser( |
| 91 | context.contentResolver, |
| 92 | Settings.Secure.FACE_UNLOCK_DISMISSES_KEYGUARD, |
| 93 | dismissByDefault, |
| 94 | KeyguardUpdateMonitor.getCurrentUser()) != 0 |
| 95 | } |
| 96 | }, Settings.Secure.FACE_UNLOCK_DISMISSES_KEYGUARD) |
Selim Cinek | a6a3ebc | 2019-06-19 18:33:17 -0700 | [diff] [blame] | 97 | lockscreenUserManager.addUserChangedListener { pendingUnlockType = null } |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 98 | } |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Notify that the biometric unlock has happened. |
| 102 | * |
| 103 | * @return false if we can not wake and unlock right now |
| 104 | */ |
| 105 | fun onBiometricAuthenticated(biometricSourceType: BiometricSourceType): Boolean { |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 106 | if (bypassEnabled) { |
Selim Cinek | f89a5dc | 2019-06-18 15:10:25 -0700 | [diff] [blame] | 107 | if (bouncerShowing) { |
| 108 | // Whenever the bouncer is showing, we want to unlock. Otherwise we can get stuck |
| 109 | // in the shade locked where the bouncer wouldn't unlock |
| 110 | return true |
| 111 | } |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 112 | if (statusBarStateController.state != StatusBarState.KEYGUARD) { |
| 113 | // We're bypassing but not actually on the lockscreen, the user should decide when |
| 114 | // to unlock |
| 115 | return false |
| 116 | } |
Selim Cinek | ec17775 | 2019-06-19 18:13:51 -0700 | [diff] [blame] | 117 | if (launchingAffordance) { |
| 118 | return false |
| 119 | } |
Selim Cinek | ae2702d | 2019-06-19 18:04:05 -0700 | [diff] [blame] | 120 | if (isPulseExpanding || qSExpanded) { |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 121 | pendingUnlockType = biometricSourceType |
| 122 | return false |
| 123 | } |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 124 | } |
| 125 | return true |
| 126 | } |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 127 | |
| 128 | fun maybePerformPendingUnlock() { |
| 129 | if (pendingUnlockType != null) { |
| 130 | if (onBiometricAuthenticated(pendingUnlockType!!)) { |
| 131 | unlockController.startWakeAndUnlock(pendingUnlockType) |
| 132 | pendingUnlockType = null |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fun onStartedGoingToSleep() { |
| 138 | pendingUnlockType = null |
| 139 | } |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 140 | } |