|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.example.android.authentication.myvault |
| 17 | + |
| 18 | +import android.annotation.SuppressLint |
| 19 | +import android.content.Context |
| 20 | +import androidx.credentials.provider.BiometricPromptResult |
| 21 | + |
| 22 | +/** |
| 23 | + * Utility class for handling biometric authentication errors. |
| 24 | + * |
| 25 | + * <p>This class provides a collection of static utility methods for managing |
| 26 | + * and processing errors that may occur during biometric authentication flows. |
| 27 | + * It encapsulates the logic for extracting error information from |
| 28 | + * {@link BiometricPromptResult} objects and constructing user-friendly error |
| 29 | + * messages. |
| 30 | + * |
| 31 | + * <p>The primary function of this class is to centralize the error-handling |
| 32 | + * logic related to biometric authentication, promoting code reuse and |
| 33 | + * maintainability. |
| 34 | + */ |
| 35 | +object BiometricErrorUtils { |
| 36 | + |
| 37 | + /** |
| 38 | + * Checks if there was an error during the biometric authentication flow and returns an error message if so. |
| 39 | + * |
| 40 | + * <p>This method determines whether the biometric authentication flow resulted in |
| 41 | + * an error. It checks if the {@link BiometricPromptResult} is null or if the |
| 42 | + * authentication was successful. If neither of these conditions is met, it |
| 43 | + * extracts the error code and message from the {@link BiometricPromptResult}, |
| 44 | + * constructs an error message, and returns it. |
| 45 | + * |
| 46 | + * <p>The error message is built using the following format: |
| 47 | + * "Biometric Error Code: [errorCode] [errorMessage] Other providers may be available." |
| 48 | + * |
| 49 | + * @param context The context used to retrieve string resources. |
| 50 | + * @param biometricPromptResult The result of the biometric authentication prompt. |
| 51 | + * @return An error message if there was an error during the biometric flow, or an empty string otherwise. |
| 52 | + */ |
| 53 | + @SuppressLint("StringFormatMatches") |
| 54 | + fun getBiometricErrorMessage( |
| 55 | + context: Context, |
| 56 | + biometricPromptResult: BiometricPromptResult?, |
| 57 | + ): String { |
| 58 | + // If the biometricPromptResult is null, there was no error. |
| 59 | + if (biometricPromptResult == null) return context.getString(R.string.empty) |
| 60 | + |
| 61 | + // If the biometricPromptResult indicates success, there was no error. |
| 62 | + if (biometricPromptResult.isSuccessful) return context.getString(R.string.empty) |
| 63 | + |
| 64 | + // Initialize default values for the error code and message. |
| 65 | + var biometricAuthErrorCode = -1 |
| 66 | + var biometricAuthErrorMsg = context.getString(R.string.unknown_failure) |
| 67 | + |
| 68 | + // Check if there is an authentication error in the biometricPromptResult. |
| 69 | + if (biometricPromptResult.authenticationError != null) { |
| 70 | + // Extract the error code and message from the authentication error. |
| 71 | + biometricAuthErrorCode = biometricPromptResult.authenticationError!!.errorCode |
| 72 | + biometricAuthErrorMsg = biometricPromptResult.authenticationError!!.errorMsg.toString() |
| 73 | + } |
| 74 | + |
| 75 | + // Build the error message to be sent to the client. |
| 76 | + val errorMessage = buildString { |
| 77 | + append( |
| 78 | + context.getString( |
| 79 | + R.string.biometric_error_code_with_message, |
| 80 | + biometricAuthErrorCode, |
| 81 | + ), |
| 82 | + ) |
| 83 | + append(biometricAuthErrorMsg) |
| 84 | + append(context.getString(R.string.other_providers_error_message)) |
| 85 | + } |
| 86 | + |
| 87 | + // Indicate that there was an error during the biometric flow. |
| 88 | + return errorMessage |
| 89 | + } |
| 90 | +} |
0 commit comments