Class2BiometricAuthPrompt


class Class2BiometricAuthPrompt


An authentication prompt that requires the user to present a Class 2 biometric (e.g. fingerprint, face, or iris).

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 Class2BiometricAuthPrompt with configurable options.

Public functions

CharSequence?

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

CharSequence

Gets the label text for the negative button on the prompt.

CharSequence?

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

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.

AuthPrompt

Shows an authentication prompt to the user.

AuthPrompt
startAuthentication(
    host: AuthPromptHost,
    executor: Executor,
    callback: AuthPromptCallback
)

Shows an authentication prompt to the user.

Extension functions

suspend BiometricPrompt.AuthenticationResult

Shows an authentication prompt to the user.

Public functions

getDescription

Added in 1.4.0-alpha02
fun getDescription(): CharSequence?

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

Returns
CharSequence?

The description for the prompt.

See also
setDescription

getNegativeButtonText

Added in 1.4.0-alpha02
fun getNegativeButtonText(): CharSequence

Gets the label text for the negative button on the prompt.

Returns
CharSequence

The negative button text for the prompt.

getSubtitle

Added in 1.4.0-alpha02
fun getSubtitle(): CharSequence?

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

Returns
CharSequence?

The subtitle for the prompt.

See also
setSubtitle

getTitle

Added in 1.4.0-alpha02
fun getTitle(): CharSequence

Gets the title to be displayed on the prompt.

Returns
CharSequence

The title for the prompt.

isConfirmationRequired

Added in 1.4.0-alpha02
fun isConfirmationRequired(): 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.

Returns
Boolean

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

startAuthentication

Added in 1.4.0-alpha02
fun startAuthentication(host: AuthPromptHost, callback: AuthPromptCallback): AuthPrompt

Shows an authentication prompt to the user.

Parameters
host: AuthPromptHost

A wrapper for the component that will host the prompt.

callback: AuthPromptCallback

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

Returns
AuthPrompt

A handle to the shown prompt.

startAuthentication

Added in 1.4.0-alpha02
fun startAuthentication(
    host: AuthPromptHost,
    executor: Executor,
    callback: AuthPromptCallback
): AuthPrompt

Shows an authentication prompt to the user.

Parameters
host: AuthPromptHost

A wrapper for the component that will host the prompt.

executor: Executor

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

callback: AuthPromptCallback

The callback object that will receive and process authentication events.

Returns
AuthPrompt

A handle to the shown prompt.

Extension functions

suspend fun Class2BiometricAuthPrompt.authenticate(host: AuthPromptHost): BiometricPrompt.AuthenticationResult

Shows an authentication prompt to the user.

import androidx.biometric.BiometricPrompt
import androidx.biometric.auth.AuthPromptErrorException
import androidx.biometric.auth.AuthPromptFailureException
import androidx.biometric.auth.AuthPromptHost
import androidx.biometric.auth.Class2BiometricAuthPrompt
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 =
    Class2BiometricAuthPrompt.Builder(title, negativeButtonText)
        .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
host: AuthPromptHost

A wrapper for the component that will host the prompt.

Returns
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)