Android fingerprint authentication library
FingerLock is a library designed to make fingerprint authentication an easy task for Android developers.
Note: this library is powered by material-dialogs, depending on this library will automatically depend on Material Dialogs.
Check the related post on Medium.
repositories {
maven { url "https://jitpack.io" }
}The core module contains the core class FingerLock to perform full fingerprint authentication.
dependencies {
// ... other dependencies here
compile 'com.github.aitorvs.fingerlock:core:1.0.0'
}The dialog extension module is powered by material-dialogs library and provides an out-of-the-box-ready material authentication dialog based on the design guidelines on fingerprint authentication.
dependencies {
// ... other dependencies here
compile 'com.github.aitorvs.fingerlock:dialog:1.0.0'
}The library requires the USE_FINGERPRINT permission. Just add the following to your AndroidManifest.xml file
The core module requires Android M and above and also a device supporting fingerprint sensor. If you target devices before M or not having fingerprint sensors, use the dialog extension.
FingerLockResultCallback inside your Activity/Fragmentpublic class MainActivity extends AppCompatActivity
implements FingerLockResultCallback {
//...
}onCreate() method) @Override
protected void onCreate(Bundle savedInstanceState) {
private FingerLockManager mFingerLockManager;
// ... Some code here
// returns a manager object to manage the fingerprint authentication
mFingerLockManager = FingerLock.initialize(this, KEY_NAME);
}It is as simple as calling the start() method.
@Override
protected void onCreate(Bundle savedInstanceState) {
// ... Some code here
mButton = (Button) findViewById(R.id.button_authenticate);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFingerLockManager.start();
}
});
// ... Some more code here
} @Override
public void onFingerLockReady() {
// Called right after registration if the device supports fingerprint authentication.
// This is normally a good place to call mFingerLockManager.start()
}This method is called when the library finishes the registration process successfully.
This is normally a good place to call start() so start the fingerprint(s) scanning process.
@Override
public void onFingerLockScanning(boolean invalidKey) {
// Called to notify the fingerprint scanning is started successfully as a result of mFingerLockManager.start()
// The 'invalidKey' parameter will be true when the key is no longer valid. This happens when
// the user disables the lock screen, resets or adds a new fingerprint after the key was created (i.e. mFingerLockManager.register())
}The callback onFingerLockScanning(boolean) is called when the library has started
scanning for fingerprint(s) to authenticate the user. The input parameter invalidKey flags when the key provided
during registration is no longer valid. Either because the user disabled the lock screen, device reset or
a new fingerprint was added.
For security purposes it is recommended to stop scanning fingerpring(s) calling stop() and
fallback to any other type of authentication (i.e. password) that authenticates the user and let
them use fingerprint the next time.
@Override
public void onFingerLockAuthenticationSucceeded() {
// Called when the user fingerprint has been correctly authenticated
}This method is called upon successful fingerprint authentication.
The callback provides error events that may happen throughout the fingerprint authentication process.
…The dialog extension module provides an out-of-the-box-ready material design dialog implementation that follows the design rules for fingerprint authentication and handles the core library module for you. The dialog extension automatically falls back to password authentication when the device does not support fingerprint authentication.
…public class MainActivity extends AppCompatActivity
implements FingerprintDialog.Callback {
//...Looooots of code here
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create and show the fingerprint dialog using the Builder
new FingerprintDialog.Builder()
.with(MainActivity.this) // context, must call
.setKeyName(KEY_NAME) // String key name, must call
.setRequestCode(69) // request code identifier, must call
.show(); // show the dialog
}Additionally, one could also call setCancelable(Boolean) in the builder
to set the dialog as cancelable or not. This call is optional and default
value is set to true.
@Override
public void onFingerprintDialogAuthenticated() {
// Authentication is successful
} @Override
public void onFingerprintDialogVerifyPassword(final FingerprintDialog dialog, final String password) {
// Password verification has been requested. Use this method to verify the `password` passed
// as parameter against your backend
// Simulate exchange with backend
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dialog.notifyPasswordValidation(password.equals("aitorvs"));
}
}, 1500);
}Called when password verification is required. Either because triggered automatically when the provided key is no longer valid or because the user required so.
Once the password is verified, notify the dialog calling dialog.notifyPasswordValidation(boolean).
@Override
public void onFingerprintDialogStageUpdated(FingerprintDialog dialog, FingerprintDialog.Stage stage) {
Log.d(TAG, "Dialog stage: " + stage.name());
}Method called at every internal stage change. Possible states are:
public enum Stage {
FINGERPRINT, // fingerprint authentication allowed
KEY_INVALIDATED, // key invalidated, password to be required
PASSWORD // password authentication selected by the user
}It is normally not necessary to act on any of the stages.
Called when the user cancels the authentication dialog.
@Override
public void onFingerprintDialogCancelled() {
Toast.makeText(this, R.string.dialog_cancelled, Toast.LENGTH_SHORT).show();
}…No open issues yet, or sync has not completed.