|
| 1 | +package com.example.identity.credentialmanager |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import android.util.Log |
| 6 | +import androidx.compose.runtime.Composable |
| 7 | +import androidx.compose.runtime.LaunchedEffect |
| 8 | +import androidx.compose.runtime.rememberCoroutineScope |
| 9 | +import androidx.credentials.CredentialManager |
| 10 | +import androidx.credentials.GetCredentialRequest |
| 11 | +import androidx.credentials.GetCredentialResponse |
| 12 | +import androidx.credentials.GetPasswordOption |
| 13 | +import androidx.credentials.PasswordCredential |
| 14 | +import androidx.credentials.exceptions.GetCredentialCancellationException |
| 15 | +import androidx.credentials.exceptions.GetCredentialCustomException |
| 16 | +import androidx.credentials.exceptions.GetCredentialException |
| 17 | +import androidx.credentials.exceptions.GetCredentialInterruptedException |
| 18 | +import androidx.credentials.exceptions.GetCredentialProviderConfigurationException |
| 19 | +import androidx.credentials.exceptions.GetCredentialUnknownException |
| 20 | +import androidx.credentials.exceptions.GetCredentialUnsupportedException |
| 21 | +import androidx.credentials.exceptions.NoCredentialException |
| 22 | +import kotlinx.coroutines.CoroutineScope |
| 23 | +import kotlinx.coroutines.launch |
| 24 | + |
| 25 | +class SmartLockToCredMan( |
| 26 | + private val credentialManager: CredentialManager, |
| 27 | + private val activityContext: Context, |
| 28 | + private val coroutineScope: CoroutineScope, |
| 29 | +) { |
| 30 | + // [START android_identity_init_password_option] |
| 31 | + // Retrieves the user's saved password for your app from their |
| 32 | + // password provider. |
| 33 | + val getPasswordOption = GetPasswordOption() |
| 34 | + // [END android_identity_init_password_option] |
| 35 | + |
| 36 | + // [START android_identity_get_cred_request] |
| 37 | + val getCredRequest = GetCredentialRequest( |
| 38 | + listOf(getPasswordOption) |
| 39 | + ) |
| 40 | + // [END android_identity_get_cred_request] |
| 41 | + |
| 42 | + val TAG: String = "tag" |
| 43 | + |
| 44 | + // [START android_identity_launch_sign_in_flow] |
| 45 | + fun launchSignInFlow() { |
| 46 | + coroutineScope.launch { |
| 47 | + try { |
| 48 | + // Attempt to retrieve the credential from the Credential Manager. |
| 49 | + val result = credentialManager.getCredential( |
| 50 | + // Use an activity-based context to avoid undefined system UI |
| 51 | + // launching behavior. |
| 52 | + context = activityContext, |
| 53 | + request = getCredRequest |
| 54 | + ) |
| 55 | + |
| 56 | + // Process the successfully retrieved credential. |
| 57 | + handleSignIn(result) |
| 58 | + } catch (e: GetCredentialException) { |
| 59 | + // Handle any errors that occur during the credential retrieval |
| 60 | + // process. |
| 61 | + handleFailure(e) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private fun handleSignIn(result: GetCredentialResponse) { |
| 67 | + // Extract the credential from the response. |
| 68 | + val credential = result.credential |
| 69 | + |
| 70 | + // Determine the type of credential and handle it accordingly. |
| 71 | + when (credential) { |
| 72 | + is PasswordCredential -> { |
| 73 | + val username = credential.id |
| 74 | + val password = credential.password |
| 75 | + |
| 76 | + // Use the extracted username and password to perform |
| 77 | + // authentication. |
| 78 | + } |
| 79 | + |
| 80 | + else -> { |
| 81 | + // Handle unrecognized credential types. |
| 82 | + Log.e(TAG, "Unexpected type of credential") |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private fun handleFailure(e: GetCredentialException) { |
| 88 | + // Handle specific credential retrieval errors. |
| 89 | + when (e) { |
| 90 | + is GetCredentialCancellationException -> { |
| 91 | + /* This exception is thrown when the user intentionally cancels |
| 92 | + the credential retrieval operation. Update the application's state |
| 93 | + accordingly. */ |
| 94 | + } |
| 95 | + |
| 96 | + is GetCredentialCustomException -> { |
| 97 | + /* This exception is thrown when a custom error occurs during the |
| 98 | + credential retrieval flow. Refer to the documentation of the |
| 99 | + third-party SDK used to create the GetCredentialRequest for |
| 100 | + handling this exception. */ |
| 101 | + } |
| 102 | + |
| 103 | + is GetCredentialInterruptedException -> { |
| 104 | + /* This exception is thrown when an interruption occurs during the |
| 105 | + credential retrieval flow. Determine whether to retry the |
| 106 | + operation or proceed with an alternative authentication method. */ |
| 107 | + } |
| 108 | + |
| 109 | + is GetCredentialProviderConfigurationException -> { |
| 110 | + /* This exception is thrown when there is a mismatch in |
| 111 | + configurations for the credential provider. Verify that the |
| 112 | + provider dependency is included in the manifest and that the |
| 113 | + required system services are enabled. */ |
| 114 | + } |
| 115 | + |
| 116 | + is GetCredentialUnknownException -> { |
| 117 | + /* This exception is thrown when the credential retrieval |
| 118 | + operation fails without providing any additional details. Handle |
| 119 | + the error appropriately based on the application's context. */ |
| 120 | + } |
| 121 | + |
| 122 | + is GetCredentialUnsupportedException -> { |
| 123 | + /* This exception is thrown when the device does not support the |
| 124 | + Credential Manager feature. Inform the user that credential-based |
| 125 | + authentication is unavailable and guide them to an alternative |
| 126 | + authentication method. */ |
| 127 | + } |
| 128 | + |
| 129 | + is NoCredentialException -> { |
| 130 | + /* This exception is thrown when there are no viable credentials |
| 131 | + available for the user. Prompt the user to sign up for an account |
| 132 | + or provide an alternative authentication method. Upon successful |
| 133 | + authentication, store the login information using |
| 134 | + androidx.credentials.CredentialManager.createCredential to |
| 135 | + facilitate easier sign-in the next time. */ |
| 136 | + } |
| 137 | + |
| 138 | + else -> { |
| 139 | + // Handle unexpected exceptions. |
| 140 | + Log.w(TAG, "Unexpected exception type: ${e::class.java.name}") |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + // [END android_identity_launch_sign_in_flow] |
| 145 | +} |
0 commit comments