Skip to content

Commit 03e8958

Browse files
author
Hao Dong
committed
Add a new BiometricConstants.
Move constants in a new file to simplify keeping AndroidX in sync with framework values. Test: ./gradlew biometric:biometric:assemble Change-Id: Id02b3672a0b5c19bcfab107541345ae762534b07
1 parent 7d7fe37 commit 03e8958

5 files changed

Lines changed: 286 additions & 233 deletions

File tree

‎biometric/biometric/src/main/java/androidx/biometric/AuthenticationRequest.kt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public abstract class AuthenticationRequest internal constructor() {
113113
/**
114114
* Builder used to create an instance of [Biometric].
115115
*
116+
* **Note for Kotlin users:** Prefer using the [AuthenticationRequest.biometricRequest]
117+
* function to construct [Biometric] instances.
118+
*
116119
* @param title The title of the prompt.
117120
* @param authFallback The [Fallback] for the biometric authentication.
118121
*/
@@ -278,6 +281,9 @@ public abstract class AuthenticationRequest internal constructor() {
278281
/**
279282
* Builder used to create an instance of [Credential].
280283
*
284+
* **Note for Kotlin users:** Prefer using the [AuthenticationRequest.credentialRequest]
285+
* function to construct [Credential] instances.
286+
*
281287
* @param title The title of the prompt.
282288
*/
283289
public class Builder(private val title: String) {
@@ -321,6 +327,10 @@ public abstract class AuthenticationRequest internal constructor() {
321327
/**
322328
* A vertical list as body content.
323329
*
330+
* Compatibility Note: This content type is only supported starting from
331+
* [Build.VERSION_CODES.VANILLA_ICE_CREAM] (API 35). On devices running earlier versions of
332+
* Android, this content will be ignored and will not be displayed.
333+
*
324334
* @property description The description of this list.
325335
* @property items The [PromptContentItem] to display on the list.
326336
*/
@@ -344,6 +354,10 @@ public abstract class AuthenticationRequest internal constructor() {
344354
* finally allow the app to decide how to proceed once selected. This requires
345355
* [SET_BIOMETRIC_DIALOG_ADVANCED] permission.
346356
*
357+
* Compatibility Note: This content type is only supported starting from
358+
* [Build.VERSION_CODES.VANILLA_ICE_CREAM] (API 35). On devices running earlier versions of
359+
* Android, this content will be ignored and will not be displayed.
360+
*
347361
* @property description The description of this view.
348362
*/
349363
public class ContentViewWithMoreOptionsButton

‎biometric/biometric/src/main/java/androidx/biometric/AuthenticationResult.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed interface AuthenticationResult {
2828
*/
2929
public class Success(
3030
public val crypto: BiometricPrompt.CryptoObject?,
31-
@BiometricPrompt.AuthenticationResultType public val authType: Int,
31+
@param:BiometricPrompt.AuthenticationResultType public val authType: Int,
3232
) : AuthenticationResult {
3333
override fun success(): Success {
3434
return this
@@ -43,7 +43,7 @@ public sealed interface AuthenticationResult {
4343
* @see [BiometricPrompt.AuthenticationError]
4444
*/
4545
public class Error(
46-
@BiometricPrompt.AuthenticationError public val errorCode: Int,
46+
@param:BiometricPrompt.AuthenticationError public val errorCode: Int,
4747
public val errString: CharSequence,
4848
) : AuthenticationResult {
4949
override fun error(): Error {
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* Copyright 2026 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 androidx.biometric;
18+
19+
20+
import androidx.annotation.RestrictTo;
21+
22+
/**
23+
* Interface containing all of the biometric modality agnostic constants.
24+
*
25+
* NOTE: The error messages must be consistent with
26+
* {@link android.hardware.biometrics.BiometricConstants}
27+
*/
28+
interface BiometricConstants {
29+
30+
/**
31+
* There is no error, and the user can successfully authenticate.
32+
*/
33+
@RestrictTo(RestrictTo.Scope.LIBRARY)
34+
int BIOMETRIC_SUCCESS = 0;
35+
36+
/**
37+
* The hardware is unavailable. Try again later.
38+
*/
39+
int ERROR_HW_UNAVAILABLE = 1;
40+
41+
/**
42+
* The sensor was unable to process the current image.
43+
*/
44+
int ERROR_UNABLE_TO_PROCESS = 2;
45+
46+
/**
47+
* The current operation has been running too long and has timed out.
48+
*
49+
* <p>This is intended to prevent programs from waiting for the biometric sensor indefinitely.
50+
* The timeout is platform and sensor-specific, but is generally on the order of ~30 seconds.
51+
*/
52+
int ERROR_TIMEOUT = 3;
53+
54+
/**
55+
* The operation can't be completed because there is not enough device storage remaining.
56+
*/
57+
int ERROR_NO_SPACE = 4;
58+
59+
/**
60+
* The operation was canceled because the biometric sensor is unavailable. This may happen when
61+
* the user is switched, the device is locked, or another pending operation prevents it.
62+
*/
63+
int ERROR_CANCELED = 5;
64+
65+
/**
66+
* The operation was canceled because the API is locked out due to too many attempts. This
67+
* occurs after 5 failed attempts, and lasts for 30 seconds.
68+
*/
69+
int ERROR_LOCKOUT = 7;
70+
71+
/**
72+
* The operation failed due to a vendor-specific error.
73+
*
74+
* <p>This error code may be used by hardware vendors to extend this list to cover errors that
75+
* don't fall under one of the other predefined categories. Vendors are responsible for
76+
* providing the strings for these errors.
77+
*
78+
* <p>These messages are typically reserved for internal operations such as enrollment but may
79+
* be used to express any error that is not otherwise covered. In this case, applications are
80+
* expected to show the error message, but they are advised not to rely on the message ID, since
81+
* this may vary by vendor and device.
82+
*/
83+
int ERROR_VENDOR = 8;
84+
85+
/**
86+
* The operation was canceled because {@link #ERROR_LOCKOUT} occurred too many times. Biometric
87+
* authentication is disabled until the user unlocks with their device credential (i.e. PIN,
88+
* pattern, or password).
89+
*/
90+
int ERROR_LOCKOUT_PERMANENT = 9;
91+
92+
/**
93+
* The user canceled the operation.
94+
*
95+
* <p>Upon receiving this, applications should use alternate authentication, such as a password.
96+
* The application should also provide the user a way of returning to biometric authentication,
97+
* such as a button.
98+
*/
99+
int ERROR_USER_CANCELED = 10;
100+
101+
/**
102+
* The user does not have any biometrics enrolled.
103+
*/
104+
int ERROR_NO_BIOMETRICS = 11;
105+
106+
/**
107+
* The device does not have the required authentication hardware.
108+
*/
109+
int ERROR_HW_NOT_PRESENT = 12;
110+
111+
/**
112+
* The user pressed the negative button.
113+
*/
114+
int ERROR_NEGATIVE_BUTTON = 13;
115+
116+
/**
117+
* The device does not have pin, pattern, or password set up.
118+
*/
119+
int ERROR_NO_DEVICE_CREDENTIAL = 14;
120+
121+
/**
122+
* A security vulnerability has been discovered with one or more hardware sensors. The
123+
* affected sensor(s) are unavailable until a security update has addressed the issue.
124+
*/
125+
int ERROR_SECURITY_UPDATE_REQUIRED = 15;
126+
127+
/**
128+
* The privacy setting has been enabled and will block use of the sensor.
129+
*/
130+
@RestrictTo(RestrictTo.Scope.LIBRARY)
131+
int ERROR_SENSOR_PRIVACY_ENABLED = 18;
132+
133+
/**
134+
* Identity Check is currently not active.
135+
*
136+
* This device either doesn't have this feature enabled, or it's not considered in a
137+
* high-risk environment that requires extra security measures for accessing sensitive data.
138+
*/
139+
int ERROR_IDENTITY_CHECK_NOT_ACTIVE = 20;
140+
141+
/**
142+
* Biometrics is not allowed to verify the user in apps. It's for internal use only. This
143+
* error code, introduced in API 35, was previously covered by ERROR_HW_UNAVAILABLE and
144+
* doesn't need to be public. Therefore, for backward compatibility, this error will be
145+
* converted to ERROR_HW_UNAVAILABLE.
146+
*/
147+
@RestrictTo(RestrictTo.Scope.LIBRARY)
148+
int ERROR_NOT_ENABLED_FOR_APPS = 21;
149+
150+
/**
151+
* The user pressed the more options button on {@link PromptContentViewWithMoreOptionsButton}
152+
* set by {@link BiometricPrompt.PromptInfo.Builder#setContentView}
153+
*/
154+
int ERROR_CONTENT_VIEW_MORE_OPTIONS_BUTTON = 22;
155+
156+
157+
/**
158+
* Authentication type reported by {@link BiometricPrompt.AuthenticationResult} when the user
159+
* authenticated via an unknown method.
160+
*
161+
* <p>This value may be returned on older Android versions due to partial incompatibility
162+
* with a newer API. It does NOT necessarily imply that the user authenticated with a method
163+
* other than those represented by {@link #AUTHENTICATION_RESULT_TYPE_DEVICE_CREDENTIAL} and
164+
* {@link #AUTHENTICATION_RESULT_TYPE_BIOMETRIC}.
165+
*/
166+
int AUTHENTICATION_RESULT_TYPE_UNKNOWN = -1;
167+
168+
/**
169+
* Authentication type reported by {@link BiometricPrompt.AuthenticationResult} when the user
170+
* authenticated by entering their device PIN, pattern, or password.
171+
*/
172+
int AUTHENTICATION_RESULT_TYPE_DEVICE_CREDENTIAL = 1;
173+
174+
/**
175+
* Authentication type reported by {@link BiometricPrompt.AuthenticationResult} when the user
176+
* authenticated by presenting some form of biometric (e.g. fingerprint or face).
177+
*/
178+
int AUTHENTICATION_RESULT_TYPE_BIOMETRIC = 2;
179+
180+
}

‎biometric/biometric/src/main/java/androidx/biometric/BiometricManager.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class BiometricManager {
6363
/**
6464
* The user can successfully authenticate.
6565
*/
66-
public static final int BIOMETRIC_SUCCESS = 0;
66+
public static final int BIOMETRIC_SUCCESS = BiometricConstants.BIOMETRIC_SUCCESS;
6767

6868
/**
6969
* Unable to determine whether the user can authenticate.
@@ -99,33 +99,35 @@ public class BiometricManager {
9999
* rather than by calling {@link BiometricManager#canAuthenticate(int)}.
100100
*/
101101
@RestrictTo(RestrictTo.Scope.LIBRARY)
102-
public static final int BIOMETRIC_ERROR_LOCKOUT = 7;
102+
public static final int BIOMETRIC_ERROR_LOCKOUT = BiometricConstants.ERROR_LOCKOUT;
103103

104104
/**
105105
* The user can't authenticate because no biometric or device credential is enrolled.
106106
*/
107-
public static final int BIOMETRIC_ERROR_NONE_ENROLLED = 11;
107+
public static final int BIOMETRIC_ERROR_NONE_ENROLLED = BiometricConstants.ERROR_NO_BIOMETRICS;
108108

109109
/**
110110
* The user can't authenticate because there is no suitable hardware (e.g. no biometric sensor
111111
* or no keyguard).
112112
*/
113-
public static final int BIOMETRIC_ERROR_NO_HARDWARE = 12;
113+
public static final int BIOMETRIC_ERROR_NO_HARDWARE = BiometricConstants.ERROR_HW_NOT_PRESENT;
114114

115115
/**
116116
* The user can't authenticate because a security vulnerability has been discovered with one or
117117
* more hardware sensors. The affected sensor(s) are unavailable until a security update has
118118
* addressed the issue.
119119
*/
120-
public static final int BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED = 15;
120+
public static final int BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED =
121+
BiometricConstants.ERROR_SECURITY_UPDATE_REQUIRED;
121122

122123
/**
123124
* Identity Check is currently not active.
124125
*
125126
* This device either doesn't have this feature enabled, or it's not considered in a
126127
* high-risk environment that requires extra security measures for accessing sensitive data.
127128
*/
128-
public static final int BIOMETRIC_ERROR_IDENTITY_CHECK_NOT_ACTIVE = 20;
129+
public static final int BIOMETRIC_ERROR_IDENTITY_CHECK_NOT_ACTIVE =
130+
BiometricConstants.ERROR_IDENTITY_CHECK_NOT_ACTIVE;
129131

130132
/**
131133
* Biometrics is not allowed to verify the user in apps. It's for internal use only. This
@@ -134,7 +136,8 @@ public class BiometricManager {
134136
* converted to BIOMETRIC_ERROR_HW_UNAVAILABLE.
135137
*/
136138
@RestrictTo(RestrictTo.Scope.LIBRARY)
137-
public static final int BIOMETRIC_ERROR_NOT_ENABLED_FOR_APPS = 21;
139+
public static final int BIOMETRIC_ERROR_NOT_ENABLED_FOR_APPS =
140+
BiometricConstants.ERROR_NOT_ENABLED_FOR_APPS;
138141

139142
/**
140143
* A status code that may be returned when checking for biometric authentication.

0 commit comments

Comments
 (0)