AuthenticationUtils
public final class AuthenticationUtils
Summary
Public methods |
|
|---|---|
static final @NonNull AuthenticationResultLauncher |
registerForAuthenticationResult(Register a request to start an authentication for result. |
static final @NonNull AuthenticationResultLauncher |
registerForAuthenticationResult(Register a request to start an authentication for result. |
Public methods
registerForAuthenticationResult
public static final @NonNull AuthenticationResultLauncher registerForAuthenticationResult(
@NonNull Fragment receiver,
@NonNull Function0<Unit> onAuthFailedCallback,
@NonNull AuthenticationResultCallback resultCallback
)
Register a request to start an authentication for result.
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( // Handle intermediate authentication failure, this is optional. onAuthFailedCallback = { Log.i(TAG, "onAuthenticationFailed, try again") } ) { 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 " + result.errorCode + " " + 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) } } }
| Parameters | |
|---|---|
@NonNull Function0<Unit> onAuthFailedCallback |
the optional callback to be called on the main thread when authentication fails intermediately. This is not a terminal auth result, and could happen multiple times. |
@NonNull AuthenticationResultCallback resultCallback |
the callback to be called on the main thread when authentication result is available |
| Returns | |
|---|---|
@NonNull AuthenticationResultLauncher |
the launcher that can be used to start the authentication. |
registerForAuthenticationResult
public static final @NonNull AuthenticationResultLauncher registerForAuthenticationResult(
@NonNull FragmentActivity receiver,
@NonNull Function0<Unit> onAuthFailedCallback,
@NonNull AuthenticationResultCallback resultCallback
)
Register a request to start an authentication for result.
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.PromptContentItemBulletedText import androidx.biometric.registerForAuthenticationResult import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity class MyActivityForBiometricAuth : FragmentActivity() { val requestAuthentication = registerForAuthenticationResult( // Handle intermediate authentication failure, this is optional. onAuthFailedCallback = { Log.i(TAG, "onAuthenticationFailed, try again") } ) { 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 " + result.errorCode + " " + 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(this).setOnClickListener { requestAuthentication.launch(authRequest) } } }
| Parameters | |
|---|---|
@NonNull Function0<Unit> onAuthFailedCallback |
the optional callback to be called on the main thread when authentication fails intermediately. This is not a terminal auth result, so it could happen multiple times. |
@NonNull AuthenticationResultCallback resultCallback |
the callback to be called on the main thread when authentication result is available |
| Returns | |
|---|---|
@NonNull AuthenticationResultLauncher |
the launcher that can be used to start the authentication. |