Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FingerprintManager instead of FingerprintManagerCompat (Fix #300) #302

Merged
merged 2 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down Expand Up @@ -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);
Expand Down