Class2BiometricOrCredentialAuthPrompt

public class Class2BiometricOrCredentialAuthPrompt


An authentication prompt that requires the user to present a Class 2 biometric (e.g. fingerprint, face, or iris) or the screen lock credential (i.e. PIN, pattern, or password) for the device.

Note that Class 3 biometrics are guaranteed to meet the requirements for Class 2 and thus will also be accepted.

Summary

Nested types

Builder for a Class2BiometricOrCredentialAuthPrompt with configurable options.

Public methods

@Nullable CharSequence

Gets the description to be displayed on the prompt, if set.

@Nullable CharSequence

Gets the subtitle to be displayed on the prompt, if set.

@NonNull CharSequence

Gets the title to be displayed on the prompt.

boolean

Checks if the prompt should require explicit user confirmation after a passive biometric (e.g. iris or face) has been recognized but before onAuthenticationSucceeded is called.

@NonNull AuthPrompt

Shows an authentication prompt to the user.

@NonNull AuthPrompt
startAuthentication(
    @NonNull AuthPromptHost host,
    @NonNull Executor executor,
    @NonNull AuthPromptCallback callback
)

Shows an authentication prompt to the user.

Extension functions

final @NonNull BiometricPrompt.AuthenticationResult

Shows an authentication prompt to the user.

Public methods

getDescription

Added in 1.2.0-alpha06
public @Nullable CharSequence getDescription()

Gets the description to be displayed on the prompt, if set.

Returns
@Nullable CharSequence

The description for the prompt.

See also
setDescription

getSubtitle

Added in 1.2.0-alpha06
public @Nullable CharSequence getSubtitle()

Gets the subtitle to be displayed on the prompt, if set.

Returns
@Nullable CharSequence

The subtitle for the prompt.

getTitle

Added in 1.2.0-alpha06
public @NonNull CharSequence getTitle()

Gets the title to be displayed on the prompt.

Returns
@NonNull CharSequence

The title for the prompt.

isConfirmationRequired

Added in 1.2.0-alpha06
public boolean isConfirmationRequired()

Checks if the prompt should require explicit user confirmation after a passive biometric (e.g. iris or face) has been recognized but before onAuthenticationSucceeded is called.

Returns
boolean

Whether the prompt should require explicit user confirmation for passive biometrics.

startAuthentication

Added in 1.2.0-alpha06
public @NonNull AuthPrompt startAuthentication(
    @NonNull AuthPromptHost host,
    @NonNull AuthPromptCallback callback
)

Shows an authentication prompt to the user.

Parameters
@NonNull AuthPromptHost host

A wrapper for the component that will host the prompt.

@NonNull AuthPromptCallback callback

The callback object that will receive and process authentication events. Each callback method will be run on the main thread.

Returns
@NonNull AuthPrompt

A handle to the shown prompt.

startAuthentication

Added in 1.2.0-alpha06
public @NonNull AuthPrompt startAuthentication(
    @NonNull AuthPromptHost host,
    @NonNull Executor executor,
    @NonNull AuthPromptCallback callback
)

Shows an authentication prompt to the user.

Parameters
@NonNull AuthPromptHost host

A wrapper for the component that will host the prompt.

@NonNull Executor executor

The executor that will be used to run authentication callback methods.

@NonNull AuthPromptCallback callback

The callback object that will receive and process authentication events.

Returns
@NonNull AuthPrompt

A handle to the shown prompt.

Extension functions

Class2BiometricOrCredentialAuthExtensionsKt.authenticate

public final @NonNull BiometricPrompt.AuthenticationResult Class2BiometricOrCredentialAuthExtensionsKt.authenticate(
    @NonNull Class2BiometricOrCredentialAuthPrompt receiver,
    @NonNull AuthPromptHost host
)

Shows an authentication prompt to the user.

import androidx.biometric.auth.AuthPromptHost
import androidx.biometric.auth.authenticate

val payload = "A message to encrypt".toByteArray(Charset.defaultCharset())

// Construct AuthPrompt with localized Strings to be displayed to UI.
val authPrompt = Class2BiometricOrCredentialAuthPrompt.Builder(title).apply {
    setSubtitle(subtitle)
    setDescription(description)
    setConfirmationRequired(true)
}.build()

try {
    val authResult = authPrompt.authenticate(AuthPromptHost(this))

    // Encrypt a payload using the result of crypto-based auth.
    val encryptedPayload = authResult.cryptoObject?.cipher?.doFinal(payload)

    // Use the encrypted payload somewhere interesting.
    sendEncryptedPayload(encryptedPayload)
} catch (e: AuthPromptErrorException) {
    // Handle irrecoverable error during authentication.
    // Possible values for AuthPromptErrorException.errorCode are listed in the @IntDef,
    // androidx.biometric.BiometricPrompt.AuthenticationError.
} catch (e: AuthPromptFailureException) {
    // Handle auth failure due biometric credentials being rejected.
}
Parameters
@NonNull AuthPromptHost host

A wrapper for the component that will host the prompt.

Returns
@NonNull BiometricPrompt.AuthenticationResult

AuthenticationResult for a successful authentication.

Throws
androidx.biometric.auth.AuthPromptErrorException

when an unrecoverable error has been encountered and authentication has stopped.

androidx.biometric.auth.AuthPromptFailureException

when an authentication attempt by the user has been rejected.

See also
authenticate

( AuthPromptHost, AuthPromptCallback )