androidx.biometric


Interfaces

AuthenticationResult

Types of the terminal result of the authentication.

AuthenticationResultCallback

A callback to be called when an AuthenticationResult is available.

AuthenticationResultLauncher

A launcher for a previously-prepared call to start the process of executing an authentication.

BiometricManager.Authenticators

Types of authenticators, defined at a level of granularity supported by BiometricManager and BiometricPrompt.

PromptContentItem

An item shown on PromptContentView.

PromptContentView

Contains the information of the template of content view for Biometric Prompt.

Classes

AuthenticationRequest

Types for configuring authentication prompt with options that are commonly used together.

AuthenticationRequest.Biometric

A set of configurable options for how the BiometricPrompt should appear and behave with biometric authentication with fallbacks.

AuthenticationRequest.Biometric.Builder

Builder used to create an instance of Biometric.

AuthenticationRequest.Biometric.Fallback

Fallback options for the biometric authentication.

AuthenticationRequest.Biometric.Fallback.NegativeButton

A customized negative button as the fallback.

AuthenticationRequest.Biometric.Strength

Types of biometric strength for the prompt.

AuthenticationRequest.Biometric.Strength.Class3

Class 3 (formerly Strong).

AuthenticationRequest.BodyContent

Types of the body content to be displayed on the prompt.

AuthenticationRequest.BodyContent.ContentViewWithMoreOptionsButton

A view with "more options" button.

AuthenticationRequest.BodyContent.PlainText

Plain text description as body content.

AuthenticationRequest.BodyContent.VerticalList

A vertical list as body content.

AuthenticationRequest.Credential

A set of configurable options for how the BiometricPrompt should appear and behave with device credential only.

AuthenticationRequest.Credential.Builder

Builder used to create an instance of Credential.

AuthenticationResult.Error

A result when an error has been encountered and authentication has stopped.

AuthenticationResult.Success

A result when the user has successfully authenticated.

BiometricManager

A class that provides system information related to biometrics (e.g. fingerprint, face, etc.).

BiometricManager.Strings

Provides localized strings for an application that uses BiometricPrompt to authenticate the user.

BiometricPrompt

A class that manages a system-provided biometric prompt.

BiometricPrompt.AuthenticationCallback

A collection of methods that may be invoked by BiometricPrompt during authentication.

BiometricPrompt.AuthenticationResult

A container for data passed to onAuthenticationSucceeded when the user has successfully authenticated.

BiometricPrompt.CryptoObject

A wrapper class for the crypto objects supported by BiometricPrompt.

BiometricPrompt.PromptInfo

A set of configurable options for how the BiometricPrompt should appear and behave.

BiometricPrompt.PromptInfo.Builder

A builder used to set individual options for the PromptInfo class.

PromptContentItemBulletedText

A list item with bulleted text shown on PromptVerticalListContentView.

PromptContentItemPlainText

A list item with plain text shown on PromptVerticalListContentView.

PromptContentViewWithMoreOptionsButton

Contains the information of the template of content view with a more options button for Biometric Prompt.

PromptContentViewWithMoreOptionsButton.Builder

A builder used to set individual options for the PromptContentViewWithMoreOptionsButton class.

PromptVerticalListContentView

Contains the information of the template of vertical list content view for Biometric Prompt.

PromptVerticalListContentView.Builder

A builder used to set individual options for the PromptVerticalListContentView class.

Objects

Extension functions summary

AuthenticationResultLauncher

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

AuthenticationResultLauncher

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

AuthenticationResultLauncher
FragmentActivity.registerForAuthenticationResult(
    callbackExecutor: Executor,
    resultCallback: AuthenticationResultCallback
)

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

AuthenticationResultLauncher
Fragment.registerForAuthenticationResult(
    callbackExecutor: Executor,
    resultCallback: AuthenticationResultCallback
)

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

Extension functions

registerForAuthenticationResult

fun FragmentActivity.registerForAuthenticationResult(
    resultCallback: AuthenticationResultCallback
): AuthenticationResultLauncher

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

A success or error result will be delivered to AuthenticationResultCallback.onAuthResult and (one or more) failures will be delivered to AuthenticationResultCallback.onAuthAttemptFailed, which is set by resultCallback. The callback will be executed on the main thread.

This must be called unconditionally, as part of initialization path, typically as a field initializer of an Activity.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.AuthenticationResultCallback
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

class MyActivityForBiometricAuth : FragmentActivity() {
    val requestAuthentication =
        registerForAuthenticationResult(
            object : AuthenticationResultCallback {
                override fun onAuthResult(result: AuthenticationResult) {
                    when (result) {
                        // Handle successful authentication
                        is AuthenticationResult.Success -> {
                            Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                        }
                        // Handle authentication error, e.g. negative button click, user
                        // cancellation, etc
                        is AuthenticationResult.Error -> {
                            Log.i(
                                TAG,
                                "onAuthenticationError " +
                                    "with error code: ${result.errorCode} " +
                                    "and error string: ${result.errString}",
                            )
                        }
                    }
                }

                // Handle intermediate authentication failure, this is optional and
                // not needed in most cases
                override fun onAuthAttemptFailed() {
                    Log.i(TAG, "onAuthenticationFailed, try again")
                }
            }
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                authFallback = Biometric.Fallback.DeviceCredential,
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class3(/*optional: cryptoObject*/ ))
                setIsConfirmationRequired(true)
            }

        Button(this).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}
See also
registerForAuthenticationResult

(Executor, AuthenticationResultCallback)

registerForAuthenticationResult

fun Fragment.registerForAuthenticationResult(
    resultCallback: AuthenticationResultCallback
): AuthenticationResultLauncher

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

A success or error result will be delivered to AuthenticationResultCallback.onAuthResult and (one or more) failures will be delivered to AuthenticationResultCallback.onAuthAttemptFailed, which is set by resultCallback. The callback will be executed on the main thread.

This must be called unconditionally, as part of initialization path, typically as a field initializer of an Fragment.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult
import androidx.fragment.app.Fragment

class MyFragmentForCredentialOnlyAuth : Fragment() {
    val requestAuthentication =
        registerForAuthenticationResult { result: AuthenticationResult ->
            when (result) {
                // Handle successful authentication
                is AuthenticationResult.Success -> {
                    Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                }
                // Handle authentication error, e.g. negative button click, user
                // cancellation, etc
                is AuthenticationResult.Error -> {
                    Log.i(
                        TAG,
                        "onAuthenticationError " +
                            "with error code: ${result.errorCode} " +
                            "and error string: ${result.errString}",
                    )
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                authFallback = Biometric.Fallback.DeviceCredential,
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class3(/*optional: cryptoObject*/ ))
                setIsConfirmationRequired(true)
            }

        Button(context).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}
See also
registerForAuthenticationResult

(Executor, AuthenticationResultCallback)

registerForAuthenticationResult

fun FragmentActivity.registerForAuthenticationResult(
    callbackExecutor: Executor,
    resultCallback: AuthenticationResultCallback
): AuthenticationResultLauncher

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

A success or error result will be delivered to AuthenticationResultCallback.onAuthResult and (one or more) failures will be delivered to AuthenticationResultCallback.onAuthAttemptFailed, which is set by resultCallback. The callback will be executed on the thread provided by the callbackExecutor.

This must be called unconditionally, as part of initialization path, typically as a field initializer of an Activity.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.AuthenticationResultCallback
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

class MyActivityForBiometricAuth : FragmentActivity() {
    val requestAuthentication =
        registerForAuthenticationResult(
            object : AuthenticationResultCallback {
                override fun onAuthResult(result: AuthenticationResult) {
                    when (result) {
                        // Handle successful authentication
                        is AuthenticationResult.Success -> {
                            Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                        }
                        // Handle authentication error, e.g. negative button click, user
                        // cancellation, etc
                        is AuthenticationResult.Error -> {
                            Log.i(
                                TAG,
                                "onAuthenticationError " +
                                    "with error code: ${result.errorCode} " +
                                    "and error string: ${result.errString}",
                            )
                        }
                    }
                }

                // Handle intermediate authentication failure, this is optional and
                // not needed in most cases
                override fun onAuthAttemptFailed() {
                    Log.i(TAG, "onAuthenticationFailed, try again")
                }
            }
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                authFallback = Biometric.Fallback.DeviceCredential,
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class3(/*optional: cryptoObject*/ ))
                setIsConfirmationRequired(true)
            }

        Button(this).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}

registerForAuthenticationResult

fun Fragment.registerForAuthenticationResult(
    callbackExecutor: Executor,
    resultCallback: AuthenticationResultCallback
): AuthenticationResultLauncher

Returns an AuthenticationResultLauncher that can be used to initiate authentication.

A success or error result will be delivered to AuthenticationResultCallback.onAuthResult and (one or more) failures will be delivered to AuthenticationResultCallback.onAuthAttemptFailed, which is set by resultCallback. The callback will be executed on the thread provided by the callbackExecutor.

This must be called unconditionally, as part of initialization path, typically as a field initializer of an Fragment.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult
import androidx.fragment.app.Fragment

class MyFragmentForCredentialOnlyAuth : Fragment() {
    val requestAuthentication =
        registerForAuthenticationResult { result: AuthenticationResult ->
            when (result) {
                // Handle successful authentication
                is AuthenticationResult.Success -> {
                    Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                }
                // Handle authentication error, e.g. negative button click, user
                // cancellation, etc
                is AuthenticationResult.Error -> {
                    Log.i(
                        TAG,
                        "onAuthenticationError " +
                            "with error code: ${result.errorCode} " +
                            "and error string: ${result.errString}",
                    )
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                authFallback = Biometric.Fallback.DeviceCredential,
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class3(/*optional: cryptoObject*/ ))
                setIsConfirmationRequired(true)
            }

        Button(context).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}