Skip to content

Commit

Permalink
Use FingerprintManager instead of FingerprintManagerCompat (Fix m2049…
Browse files Browse the repository at this point in the history
  • Loading branch information
0140454 authored and crtlib committed Jun 28, 2018
1 parent b5d73b2 commit 7e21e6b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
29 changes: 19 additions & 10 deletions app/src/main/java/com/m2049r/xmrwallet/util/FingerprintHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}
}
65 changes: 34 additions & 31 deletions app/src/main/java/com/m2049r/xmrwallet/util/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -387,10 +388,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
Expand Down Expand Up @@ -421,38 +418,44 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
});
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(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);
Expand Down

0 comments on commit 7e21e6b

Please sign in to comment.