From bb7226538128ef936a920ee1cb2aeac7fe07e2fb Mon Sep 17 00:00:00 2001 From: Raul Candelario Date: Thu, 7 Dec 2023 01:57:02 -0600 Subject: [PATCH] Fix #9 --- android/build.gradle | 18 ++++++------ .../java/ee/forgr/biometric/AuthActivity.java | 28 ++++++++----------- .../ee/forgr/biometric/NativeBiometric.java | 9 ++++-- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 2fd196c..4dbfbf4 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,15 +2,16 @@ ext { junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5' androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1' + androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1' } buildscript { repositories { google() - jcenter() + mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:8.0.0' + classpath 'com.android.tools.build:gradle:8.0.2' } } @@ -39,20 +40,19 @@ android { repositories { google() - jcenter() mavenCentral() } dependencies { - implementation 'androidx.biometric:biometric:1.0.1' + implementation 'androidx.biometric:biometric:1.1.0' implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':capacitor-android') - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'com.google.android.material:material:1.1.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation 'androidx.navigation:navigation-fragment:2.0.0' - implementation 'androidx.navigation:navigation-ui:2.0.0' + implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" + implementation 'com.google.android.material:material:1.9.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.navigation:navigation-fragment:2.5.3' + implementation 'androidx.navigation:navigation-ui:2.5.3' testImplementation "junit:junit:$junitVersion" androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" diff --git a/android/src/main/java/ee/forgr/biometric/AuthActivity.java b/android/src/main/java/ee/forgr/biometric/AuthActivity.java index cca883f..9a556f5 100644 --- a/android/src/main/java/ee/forgr/biometric/AuthActivity.java +++ b/android/src/main/java/ee/forgr/biometric/AuthActivity.java @@ -4,15 +4,9 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; -import android.util.Log; -import android.view.View; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; -import androidx.biometric.BiometricConstants; import androidx.biometric.BiometricPrompt; -import com.google.android.material.floatingactionbutton.FloatingActionButton; -import com.google.android.material.snackbar.Snackbar; import ee.forgr.biometric.capacitornativebiometric.R; import java.util.concurrent.Executor; @@ -136,29 +130,29 @@ void finishActivity(String result, Integer errorCode, String errorDetails) { * Convert Auth Error Codes to plugin expected Biometric Auth Errors (in README.md) * This way both iOS and Android return the same error codes for the same authentication failure reasons. * !!IMPORTANT!!: Whenever this is modified, check if similar function in iOS Plugin.swift needs to be modified as well - * @see https://developer.android.com/reference/androidx/biometric/BiometricPrompt#constants + * @see ... * @return BiometricAuthError */ public static int convertToPluginErrorCode(int errorCode) { switch (errorCode) { - case BiometricConstants.ERROR_HW_UNAVAILABLE: - case BiometricConstants.ERROR_HW_NOT_PRESENT: + case BiometricPrompt.ERROR_HW_UNAVAILABLE: + case BiometricPrompt.ERROR_HW_NOT_PRESENT: return 1; - case BiometricConstants.ERROR_LOCKOUT_PERMANENT: + case BiometricPrompt.ERROR_LOCKOUT_PERMANENT: return 2; - case BiometricConstants.ERROR_NO_BIOMETRICS: + case BiometricPrompt.ERROR_NO_BIOMETRICS: return 3; - case BiometricConstants.ERROR_LOCKOUT: + case BiometricPrompt.ERROR_LOCKOUT: return 4; // Authentication Failure (10) Handled by `onAuthenticationFailed`. // App Cancel (11), Invalid Context (12), and Not Interactive (13) are not valid error codes for Android. - case BiometricConstants.ERROR_NO_DEVICE_CREDENTIAL: + case BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL: return 14; - case BiometricConstants.ERROR_TIMEOUT: - case BiometricConstants.ERROR_CANCELED: + case BiometricPrompt.ERROR_TIMEOUT: + case BiometricPrompt.ERROR_CANCELED: return 15; - case BiometricConstants.ERROR_USER_CANCELED: - case BiometricConstants.ERROR_NEGATIVE_BUTTON: + case BiometricPrompt.ERROR_USER_CANCELED: + case BiometricPrompt.ERROR_NEGATIVE_BUTTON: return 16; default: return 0; diff --git a/android/src/main/java/ee/forgr/biometric/NativeBiometric.java b/android/src/main/java/ee/forgr/biometric/NativeBiometric.java index 358e990..381541c 100644 --- a/android/src/main/java/ee/forgr/biometric/NativeBiometric.java +++ b/android/src/main/java/ee/forgr/biometric/NativeBiometric.java @@ -14,7 +14,6 @@ import android.security.keystore.StrongBoxUnavailableException; import android.util.Base64; import androidx.activity.result.ActivityResult; -import androidx.biometric.BiometricConstants; import androidx.biometric.BiometricManager; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; @@ -124,7 +123,7 @@ public void isAvailable(PluginCall call) { // @see https://developer.android.com/reference/androidx/biometric/BiometricManager#canAuthenticate(int) boolean fallbackAvailable = useFallback && this.deviceHasCredentials(); if (useFallback && !fallbackAvailable) { - canAuthenticateResult = BiometricConstants.ERROR_NO_DEVICE_CREDENTIAL; + canAuthenticateResult = BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE; } boolean isAvailable = @@ -496,6 +495,10 @@ private boolean deviceHasCredentials() { KeyguardManager keyguardManager = (KeyguardManager) getActivity() .getSystemService(Context.KEYGUARD_SERVICE); // Can only use fallback if the device has a pin/pattern/password lockscreen. - return keyguardManager.isDeviceSecure(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + return keyguardManager.isDeviceSecure(); + } else { + return keyguardManager.isKeyguardSecure(); + } } }