1 /*
2  * Copyright (C) 2024 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.biometrics.shared.model
18 
19 import android.hardware.biometrics.BiometricFaceConstants
20 import android.hardware.biometrics.BiometricFingerprintConstants
21 import android.hardware.biometrics.BiometricRequestConstants
22 import android.hardware.biometrics.BiometricSourceType
23 
24 /**
25  * Describes the current state of biometric authentication, including whether authentication is
26  * started, stopped, or acquired and relevant parameters, and the [AuthenticationReason] for
27  * authentication.
28  */
29 sealed interface AuthenticationState {
30     /** Indicates [BiometricSourceType] of authentication state update, null in idle auth state. */
31     val biometricSourceType: BiometricSourceType?
32 
33     /**
34      * Indicates [AuthenticationReason] from [BiometricRequestConstants.RequestReason] for
35      * requesting auth
36      */
37     val requestReason: AuthenticationReason
38 
39     /**
40      * AuthenticationState when a biometric has been acquired.
41      *
42      * @param biometricSourceType indicates [BiometricSourceType] of acquired authentication
43      * @param requestReason reason from [BiometricRequestConstants.RequestReason] for authentication
44      * @param acquiredInfo [BiometricFaceConstants.FaceAcquired] or
45      *   [BiometricFingerprintConstants.FingerprintAcquired] int corresponding to a known acquired
46      *   message.
47      */
48     data class Acquired(
49         override val biometricSourceType: BiometricSourceType,
50         override val requestReason: AuthenticationReason,
51         val acquiredInfo: Int
52     ) : AuthenticationState
53 
54     /**
55      * AuthenticationState when an unrecoverable error is encountered during authentication.
56      *
57      * @param biometricSourceType identifies [BiometricSourceType] for auth error
58      * @param errString authentication error string shown on the UI
59      * @param errCode [BiometricFaceConstants.FaceError] or
60      *   [BiometricFingerprintConstants.FingerprintError] int identifying the error message for an
61      *   authentication error
62      * @param requestReason reason from [BiometricRequestConstants.RequestReason] for authentication
63      */
64     data class Error(
65         override val biometricSourceType: BiometricSourceType,
66         val errString: String?,
67         val errCode: Int,
68         override val requestReason: AuthenticationReason,
69     ) : AuthenticationState
70 
71     /**
72      * AuthenticationState when a biometric couldn't be authenticated.
73      *
74      * @param biometricSourceType identifies [BiometricSourceType] for failed auth
75      * @param requestReason reason from [BiometricRequestConstants.RequestReason] for authentication
76      * @param userId The user id for the requested authentication
77      */
78     data class Failed(
79         override val biometricSourceType: BiometricSourceType,
80         override val requestReason: AuthenticationReason,
81         val userId: Int
82     ) : AuthenticationState
83 
84     /**
85      * AuthenticationState when a recoverable error is encountered during authentication.
86      *
87      * @param biometricSourceType identifies [BiometricSourceType] for failed auth
88      * @param helpString helpString guidance help string shown on the UI
89      * @param helpCode An integer identifying the help message
90      * @param requestReason reason from [BiometricRequestConstants.RequestReason] for authentication
91      */
92     data class Help(
93         override val biometricSourceType: BiometricSourceType,
94         val helpString: String?,
95         val helpCode: Int,
96         override val requestReason: AuthenticationReason,
97     ) : AuthenticationState
98 
99     /**
100      * Authentication state when no auth is running
101      *
102      * @param biometricSourceType null
103      * @param requestReason [AuthenticationReason.NotRunning]
104      */
105     data class Idle(
106         override val biometricSourceType: BiometricSourceType? = null,
107         override val requestReason: AuthenticationReason
108     ) : AuthenticationState
109 
110     /**
111      * AuthenticationState when auth is started
112      *
113      * @param biometricSourceType identifies [BiometricSourceType] for auth
114      * @param requestReason reason from [BiometricRequestConstants.RequestReason] for authentication
115      */
116     data class Started(
117         override val biometricSourceType: BiometricSourceType,
118         override val requestReason: AuthenticationReason
119     ) : AuthenticationState
120 
121     /**
122      * Authentication state when auth is stopped
123      *
124      * @param biometricSourceType identifies [BiometricSourceType] for auth stopped
125      * @param requestReason [AuthenticationReason.NotRunning]
126      */
127     data class Stopped(
128         override val biometricSourceType: BiometricSourceType,
129         override val requestReason: AuthenticationReason
130     ) : AuthenticationState
131 
132     /**
133      * AuthenticationState when a biometric is successfully authenticated.
134      *
135      * @param biometricSourceType identifies [BiometricSourceType] of successful auth
136      * @param isStrongBiometric indicates whether auth was from strong biometric
137      * @param requestReason reason from [BiometricRequestConstants.RequestReason] for authentication
138      * @param userId The user id for the requested authentication
139      */
140     data class Succeeded(
141         override val biometricSourceType: BiometricSourceType,
142         val isStrongBiometric: Boolean,
143         override val requestReason: AuthenticationReason,
144         val userId: Int
145     ) : AuthenticationState
146 }
147