From 84335770f0d03feab5d88ebfdf03dc8b2d3ee35c Mon Sep 17 00:00:00 2001 From: 0140454 Date: Sat, 9 Jun 2018 00:13:40 +0800 Subject: [PATCH 1/2] Use FingerprintManager instead of FingerprintManagerCompat --- .../xmrwallet/util/FingerprintHelper.java | 29 ++++++--- .../com/m2049r/xmrwallet/util/Helper.java | 65 ++++++++++--------- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java b/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java index 33dcdc5b08..7c7cc1b60c 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java @@ -2,18 +2,21 @@ import android.app.KeyguardManager; import android.content.Context; -import android.support.v4.hardware.fingerprint.FingerprintManagerCompat; -import android.support.v4.os.CancellationSignal; - -import timber.log.Timber; +import android.hardware.fingerprint.FingerprintManager; +import android.os.Build; +import android.os.CancellationSignal; public class FingerprintHelper { public static boolean isDeviceSupported(Context context) { - FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.from(context); - KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return false; + } + + FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class); + KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class); - return keyguardManager != null && + return keyguardManager != null && fingerprintManager != null && keyguardManager.isKeyguardSecure() && fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints(); @@ -29,8 +32,14 @@ public static boolean isFingerPassValid(Context context, String wallet) { } public static void authenticate(Context context, CancellationSignal cancelSignal, - FingerprintManagerCompat.AuthenticationCallback callback) { - FingerprintManagerCompat manager = FingerprintManagerCompat.from(context); - manager.authenticate(null, 0, cancelSignal, callback, null); + FingerprintManager.AuthenticationCallback callback) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return; + } + + FingerprintManager manager = context.getSystemService(FingerprintManager.class); + if (manager != null) { + manager.authenticate(null, cancelSignal, 0, callback, null); + } } } diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java b/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java index fed1bb805a..512231d177 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/Helper.java @@ -31,11 +31,12 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.VectorDrawable; +import android.hardware.fingerprint.FingerprintManager; +import android.os.Build; +import android.os.CancellationSignal; import android.os.Environment; import android.support.design.widget.TextInputLayout; import android.support.v4.content.ContextCompat; -import android.support.v4.hardware.fingerprint.FingerprintManagerCompat; -import android.support.v4.os.CancellationSignal; import android.system.ErrnoException; import android.system.Os; import android.text.Editable; @@ -394,10 +395,6 @@ static public void promptPassword(final Context context, final String wallet, bo final boolean fingerprintAuthAllowed = !fingerprintDisabled && fingerprintAuthCheck; final CancellationSignal cancelSignal = new CancellationSignal(); - if (fingerprintAuthAllowed) { - promptsView.findViewById(R.id.txtFingerprintAuth).setVisibility(View.VISIBLE); - } - etPassword.getEditText().addTextChangedListener(new TextWatcher() { @Override @@ -433,40 +430,46 @@ public void onClick(DialogInterface dialog, int id) { }); openDialog = alertDialogBuilder.create(); - final FingerprintManagerCompat.AuthenticationCallback fingerprintAuthCallback = new FingerprintManagerCompat.AuthenticationCallback() { - @Override - public void onAuthenticationError(int errMsgId, CharSequence errString) { - ((TextView) promptsView.findViewById(R.id.txtFingerprintAuth)).setText(errString); - } + final FingerprintManager.AuthenticationCallback fingerprintAuthCallback; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + fingerprintAuthCallback = null; + } else { + fingerprintAuthCallback = new FingerprintManager.AuthenticationCallback() { + @Override + public void onAuthenticationError(int errMsgId, CharSequence errString) { + ((TextView) promptsView.findViewById(R.id.txtFingerprintAuth)).setText(errString); + } - @Override - public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { - try { - String userPass = KeyStoreHelper.loadWalletUserPass(context, wallet); - if (Helper.processPasswordEntry(context, wallet, userPass, true, action)) { - Helper.hideKeyboardAlways((Activity) context); - openDialog.dismiss(); - openDialog = null; - } else { + @Override + public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { + try { + String userPass = KeyStoreHelper.loadWalletUserPass(context, wallet); + if (Helper.processPasswordEntry(context, wallet, userPass, true, action)) { + Helper.hideKeyboardAlways((Activity) context); + openDialog.dismiss(); + openDialog = null; + } else { + etPassword.setError(context.getString(R.string.bad_password)); + } + } catch (KeyStoreHelper.BrokenPasswordStoreException ex) { etPassword.setError(context.getString(R.string.bad_password)); + // TODO: better errror message here - what would it be? } - } catch (KeyStoreHelper.BrokenPasswordStoreException ex) { - etPassword.setError(context.getString(R.string.bad_password)); - // TODO: better errror message here - what would it be? } - } - @Override - public void onAuthenticationFailed() { - ((TextView) promptsView.findViewById(R.id.txtFingerprintAuth)) - .setText(context.getString(R.string.bad_fingerprint)); - } - }; + @Override + public void onAuthenticationFailed() { + ((TextView) promptsView.findViewById(R.id.txtFingerprintAuth)) + .setText(context.getString(R.string.bad_fingerprint)); + } + }; + } openDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { - if (fingerprintAuthAllowed) { + if (fingerprintAuthAllowed && fingerprintAuthCallback != null) { + promptsView.findViewById(R.id.txtFingerprintAuth).setVisibility(View.VISIBLE); FingerprintHelper.authenticate(context, cancelSignal, fingerprintAuthCallback); } Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE); From 9b9ecde79869a8d302571d1eb60970cab1865320 Mon Sep 17 00:00:00 2001 From: 0140454 Date: Sun, 10 Jun 2018 02:14:09 +0800 Subject: [PATCH 2/2] Improve legibility --- .../main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java b/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java index 7c7cc1b60c..960c484642 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java @@ -16,7 +16,7 @@ public static boolean isDeviceSupported(Context context) { FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class); KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class); - return keyguardManager != null && fingerprintManager != null && + return (keyguardManager != null) && (fingerprintManager != null) && keyguardManager.isKeyguardSecure() && fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints();