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 | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 24 | import com.android.systemui.plugins.statusbar.StatusBarStateController |
Selim Cinek | a6a3ebc | 2019-06-19 18:33:17 -0700 | [diff] [blame] | 25 | import com.android.systemui.statusbar.NotificationLockscreenUserManager |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 26 | import com.android.systemui.statusbar.StatusBarState |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 27 | import com.android.systemui.tuner.TunerService |
Lucas Dupin | 5a5e0bd | 2019-07-11 14:19:11 +0900 | [diff] [blame] | 28 | import java.io.PrintWriter |
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 |
Lucas Dupin | 4c50704 | 2019-07-22 16:47:34 -0700 | [diff] [blame^] | 50 | get() = field && unlockMethodCache.isFaceAuthEnabled |
Selim Cinek | f89a5dc | 2019-06-18 15:10:25 -0700 | [diff] [blame] | 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 |
Lucas Dupin | 5a5e0bd | 2019-07-11 14:19:11 +0900 | [diff] [blame] | 65 | constructor( |
| 66 | context: Context, |
| 67 | tunerService: TunerService, |
| 68 | statusBarStateController: StatusBarStateController, |
| 69 | lockscreenUserManager: NotificationLockscreenUserManager |
| 70 | ) { |
Lucas Dupin | 206fe56 | 2019-05-31 14:36:42 -0700 | [diff] [blame] | 71 | unlockMethodCache = UnlockMethodCache.getInstance(context) |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 72 | this.statusBarStateController = statusBarStateController |
Lucas Dupin | 5a5e0bd | 2019-07-11 14:19:11 +0900 | [diff] [blame] | 73 | |
Mady Mellor | dce1bf3 | 2019-06-25 14:31:19 -0700 | [diff] [blame] | 74 | if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) { |
| 75 | return |
| 76 | } |
Lucas Dupin | 1b883b4 | 2019-05-28 17:43:59 -0700 | [diff] [blame] | 77 | val faceManager = context.getSystemService(FaceManager::class.java) |
| 78 | if (faceManager?.isHardwareDetected != true) { |
| 79 | return |
| 80 | } |
| 81 | |
Lucas Dupin | 5a5e0bd | 2019-07-11 14:19:11 +0900 | [diff] [blame] | 82 | statusBarStateController.addCallback(object : StatusBarStateController.StateListener { |
| 83 | override fun onStateChanged(newState: Int) { |
| 84 | if (newState != StatusBarState.KEYGUARD) { |
| 85 | pendingUnlockType = null |
| 86 | } |
| 87 | } |
| 88 | }) |
| 89 | |
Lucas Dupin | 1b883b4 | 2019-05-28 17:43:59 -0700 | [diff] [blame] | 90 | val dismissByDefault = if (context.resources.getBoolean( |
Lucas Dupin | 5a5e0bd | 2019-07-11 14:19:11 +0900 | [diff] [blame] | 91 | com.android.internal.R.bool.config_faceAuthDismissesKeyguard)) 1 else 0 |
| 92 | tunerService.addTunable(object : TunerService.Tunable { |
| 93 | override fun onTuningChanged(key: String?, newValue: String?) { |
| 94 | bypassEnabled = tunerService.getValue(key, dismissByDefault) != 0 |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 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) { |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 107 | val can = canBypass() |
| 108 | if (!can && (isPulseExpanding || qSExpanded)) { |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 109 | pendingUnlockType = biometricSourceType |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 110 | } |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 111 | return can |
Selim Cinek | bd84c16 | 2019-06-11 16:23:23 -0700 | [diff] [blame] | 112 | } |
| 113 | return true |
| 114 | } |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 115 | |
| 116 | fun maybePerformPendingUnlock() { |
| 117 | if (pendingUnlockType != null) { |
| 118 | if (onBiometricAuthenticated(pendingUnlockType!!)) { |
| 119 | unlockController.startWakeAndUnlock(pendingUnlockType) |
| 120 | pendingUnlockType = null |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Lucas Dupin | 4befb74 | 2019-07-01 11:31:31 -0700 | [diff] [blame] | 125 | /** |
| 126 | * If keyguard can be dismissed because of bypass. |
| 127 | */ |
| 128 | fun canBypass(): Boolean { |
| 129 | if (bypassEnabled) { |
| 130 | return when { |
| 131 | bouncerShowing -> true |
| 132 | statusBarStateController.state != StatusBarState.KEYGUARD -> false |
| 133 | launchingAffordance -> false |
| 134 | isPulseExpanding || qSExpanded -> false |
| 135 | else -> true |
| 136 | } |
| 137 | } |
| 138 | return false |
| 139 | } |
| 140 | |
Lucas Dupin | fa817a0 | 2019-07-02 15:22:54 -0700 | [diff] [blame] | 141 | /** |
| 142 | * If shorter animations should be played when unlocking. |
| 143 | */ |
| 144 | fun canPlaySubtleWindowAnimations(): Boolean { |
| 145 | if (bypassEnabled) { |
| 146 | return when { |
| 147 | statusBarStateController.state != StatusBarState.KEYGUARD -> false |
| 148 | qSExpanded -> false |
| 149 | else -> true |
| 150 | } |
| 151 | } |
| 152 | return false |
| 153 | } |
| 154 | |
Selim Cinek | b8cc6ef | 2019-06-14 16:37:53 -0700 | [diff] [blame] | 155 | fun onStartedGoingToSleep() { |
| 156 | pendingUnlockType = null |
| 157 | } |
Lucas Dupin | 5a5e0bd | 2019-07-11 14:19:11 +0900 | [diff] [blame] | 158 | |
| 159 | fun dump(pw: PrintWriter) { |
| 160 | pw.println("KeyguardBypassController:") |
| 161 | pw.print(" pendingUnlockType: "); pw.println(pendingUnlockType) |
| 162 | pw.print(" bypassEnabled: "); pw.println(bypassEnabled) |
| 163 | pw.print(" canBypass: "); pw.println(canBypass()) |
| 164 | pw.print(" bouncerShowing: "); pw.println(bouncerShowing) |
| 165 | pw.print(" isPulseExpanding: "); pw.println(isPulseExpanding) |
| 166 | pw.print(" launchingAffordance: "); pw.println(launchingAffordance) |
| 167 | pw.print(" qSExpanded: "); pw.println(qSExpanded) |
| 168 | pw.print(" bouncerShowing: "); pw.println(bouncerShowing) |
| 169 | } |
Selim Cinek | 84b2acc | 2019-07-07 00:40:38 -0700 | [diff] [blame] | 170 | |
| 171 | companion object { |
| 172 | const val BYPASS_PANEL_FADE_DURATION = 67 |
| 173 | } |
Selim Cinek | 2c890ee | 2019-05-20 19:16:43 -0700 | [diff] [blame] | 174 | } |