The context is needed to access the check for availability
public class Auth0Fingerprint implements FingerprintManager.AuthenticationCallback {
private final FingerprintManagerCompat manager;
public Auth0Fingerprint (Context context){
//Use the support version as it handles the missing APIs case
manager = new FingerprintManagerCompat(context);
}
//See below for the other methods
}
public static boolean checkPermission(Context context) {
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT);
return permissionCheck == PackageManager.PERMISSION_GRANTED;
}
public static void requestPermission(Activity activity, int requestCode) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.USE_FINGERPRINT)) {
//Show explanation and retry.
//AlertDialog ...
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.USE_FINGERPRINT}, requestCode);
}
}
public boolean isAvailable() {
if (!manager.isHardwareDetected()){
throw new IllegalStateException("Fingerprint scanner not available.");
}
if (!manager.hasEnrolledFingerprints()){
throw new IllegalStateException("No fingerprints enrolled.");
}
return manager.isHardwareDetected() && manager.hasEnrolledFingerprints();
}
public boolean authenticate() {
return manager.authenticate(createCryptoObject(), new CancellationSignal(), this, null);
}
The general methods are ok (for handling permission, availability etc) but I propose something like this for authentication: