1 /*
2  * Copyright (C) 2023 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.keyguard.shared.model
18 
19 import android.hardware.biometrics.BiometricFingerprintConstants
20 import android.hardware.biometrics.BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_GOOD
21 import android.hardware.biometrics.BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_START
22 import android.hardware.biometrics.BiometricFingerprintConstants.FINGERPRINT_ACQUIRED_UNKNOWN
23 import android.hardware.fingerprint.FingerprintManager
24 import android.os.SystemClock.elapsedRealtime
25 import com.android.systemui.biometrics.shared.model.AuthenticationReason
26 
27 /**
28  * Fingerprint authentication status provided by
29  * [com.android.systemui.keyguard.data.repository.DeviceEntryFingerprintAuthRepository]
30  *
31  * @isEngaged whether fingerprint is actively engaged by the user. This is distinct from fingerprint
32  * running on the device. Can be null if the status does not have an associated isEngaged state.
33  */
34 sealed class FingerprintAuthenticationStatus(val isEngaged: Boolean?)
35 
36 /** Fingerprint authentication success status. */
37 data class SuccessFingerprintAuthenticationStatus(
38     val userId: Int,
39     val isStrongBiometric: Boolean,
40 ) : FingerprintAuthenticationStatus(isEngaged = false)
41 
42 /** Fingerprint authentication help message. */
43 data class HelpFingerprintAuthenticationStatus(
44     val msgId: Int,
45     val msg: String?,
46 ) : FingerprintAuthenticationStatus(isEngaged = null)
47 
48 /** Fingerprint acquired message. */
49 data class AcquiredFingerprintAuthenticationStatus(
50     val authenticationReason: AuthenticationReason,
51     val acquiredInfo: Int
52 ) :
53     FingerprintAuthenticationStatus(
54         isEngaged =
55             if (acquiredInfo == FINGERPRINT_ACQUIRED_START) {
56                 true
57             } else if (
58                 acquiredInfo == FINGERPRINT_ACQUIRED_UNKNOWN ||
59                     acquiredInfo == FINGERPRINT_ACQUIRED_GOOD
60             ) {
61                 null
62             } else {
63                 // soft errors that indicate fingerprint activity ended
64                 false
65             }
66     ) {
67 
68     val fingerprintCaptureStarted: Boolean = acquiredInfo == FINGERPRINT_ACQUIRED_START
69 
70     val fingerprintCaptureCompleted: Boolean = acquiredInfo == FINGERPRINT_ACQUIRED_GOOD
71 }
72 
73 /** Fingerprint authentication failed message. */
74 data object FailFingerprintAuthenticationStatus :
75     FingerprintAuthenticationStatus(isEngaged = false)
76 
77 /** Fingerprint authentication error message */
78 data class ErrorFingerprintAuthenticationStatus(
79     val msgId: Int,
80     val msg: String? = null,
81     // present to break equality check if the same error occurs repeatedly.
82     val createdAt: Long = elapsedRealtime(),
83 ) : FingerprintAuthenticationStatus(isEngaged = false) {
isCancellationErrornull84     fun isCancellationError(): Boolean =
85         msgId == BiometricFingerprintConstants.FINGERPRINT_ERROR_CANCELED ||
86             msgId == BiometricFingerprintConstants.FINGERPRINT_ERROR_USER_CANCELED
87 
88     fun isPowerPressedError(): Boolean =
89         msgId == BiometricFingerprintConstants.BIOMETRIC_ERROR_POWER_PRESSED
90 
91     fun isLockoutError(): Boolean =
92         msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT ||
93             msgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT_PERMANENT
94 }
95