import
android.app.KeyguardManager;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.pm.PackageManager;
import
android.hardware.biometrics.BiometricPrompt;
import
android.os.Build;
import
android.os.Bundle;
import
android.os.CancellationSignal;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
import
androidx.annotation.Nullable;
import
androidx.annotation.RequiresApi;
import
androidx.appcompat.app.AppCompatActivity;
import
androidx.core.app.ActivityCompat;
public
class
MainActivity
extends
AppCompatActivity {
private
CancellationSignal cancellationSignal =
null
;
private
BiometricPrompt.AuthenticationCallback authenticationCallback;
@RequiresApi
(api = Build.VERSION_CODES.P)
@Override
protected
void
onCreate(
@Nullable
Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
authenticationCallback =
new
BiometricPrompt.AuthenticationCallback() {
@Override
public
void
onAuthenticationError(
int
errorCode, CharSequence errString)
{
super
.onAuthenticationError(errorCode, errString);
notifyUser(
"Authentication Error : "
+ errString);
}
@Override
public
void
onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result)
{
super
.onAuthenticationSucceeded(result);
notifyUser(
"Authentication Succeeded"
);
}
};
checkBiometricSupport();
(Button) findViewById(R.id.start_authentication).setOnClickListener(
new
View.OnClickListener() {
@RequiresApi
(api = Build.VERSION_CODES.P)
@Override
public
void
onClick(View view)
{
BiometricPrompt biometricPrompt =
new
BiometricPrompt
.Builder(getApplicationContext())
.setTitle(
"Title of Prompt"
)
.setSubtitle(
"Subtitle"
)
.setDescription(
"Uses FP"
)
.setNegativeButton(
"Cancel"
, getMainExecutor(),
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialogInterface,
int
i)
{
notifyUser(
"Authentication Cancelled"
);
}
}).build();
biometricPrompt.authenticate(
getCancellationSignal(),
getMainExecutor(),
authenticationCallback);
}
});
}
private
CancellationSignal getCancellationSignal()
{
cancellationSignal =
new
CancellationSignal();
cancellationSignal.setOnCancelListener(
new
CancellationSignal.OnCancelListener() {
@Override
public
void
onCancel()
{
notifyUser(
"Authentication was Cancelled by the user"
);
}
});
return
cancellationSignal;
}
@RequiresApi
(Build.VERSION_CODES.M)
private
Boolean checkBiometricSupport()
{
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
if
(!keyguardManager.isDeviceSecure()) {
notifyUser(
"Fingerprint authentication has not been enabled in settings"
);
return
false
;
}
if
(ActivityCompat.checkSelfPermission(
this
, android.Manifest.permission.USE_BIOMETRIC)!= PackageManager.PERMISSION_GRANTED) {
notifyUser(
"Fingerprint Authentication Permission is not enabled"
);
return
false
;
}
if
(getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
return
true
;
}
else
return
true
;
}
private
void
notifyUser(String message)
{
Toast.makeText(
this
, message, Toast.LENGTH_SHORT).show();
}
}