|
4 | 4 |
|
5 | 5 | #### FingerprintDialog from Android 28 (P) back ported to Android 23 (M).
|
6 | 6 |
|
| 7 | +## Why do we need this library⁉️ |
| 8 | +- Fingerprint is currently the most secure and most user friendly way of authenticating the user in Android. |
| 9 | +- Android provides different APIs to deal with the fingerprint authentication. These APIs changed overtime. |
| 10 | + - Android versions below API 23 did not support fingerprint hardware at all. |
| 11 | + - Android versions between API 23 and API 27, uses [`FingerprintManager`](https://developer.android.com/reference/android/hardware/fingerprint/FingerprintManager.html) for fingerprint authentication. Developer has to manually authenticate by implementing bunch of APIs. |
| 12 | + - Android version above API 28, uses [`FingerprintDialog`](https://developer.android.com/reference/android/hardware/fingerprint/FingerprintDialog.html) for fingerprint authentication. |
| 13 | +- Due to the Android fragmentation developer has to implement all those APIs based on the android version.😔 It makes managing fingerprint authentication very hard. |
| 14 | +- This library aims to simplify fingerprint authentication by providing backport of the `FingerprintDialog` from Android P and provides single public APIs for the authentication all the way to API 14. So as a developer you don't have to deal with the complexity of dealing with different APIs.✌️ |
| 15 | + |
| 16 | +## Features: |
| 17 | +- Extremely lightweight 🏋. |
| 18 | +- Backport of [`FingerprintDialog`](https://developer.android.com/reference/android/hardware/fingerprint/FingerprintDialog.html) from Android P, provides same fingerprint authentication on all Android versions. |
| 19 | +- Easy to integrate. All you have to do is implement the builder and you are done. The library will take care of authentication based on the device's android version. |
| 20 | +- Classifies different error codes provided by the Android framework and wraps them into different error callbacks for easy error handling. |
| 21 | + |
| 22 | +## How to use this library? |
| 23 | +- ### Gradle dependency: |
| 24 | + - Add below dependency into your build.gradle file. |
| 25 | + ```groovy |
| 26 | + implementation 'com.kevalpatel2106:fingerprint-dialog-compat:1.0' |
| 27 | + ``` |
| 28 | + - For other build systems see [Import.md](/.github/IMPORT.md). |
| 29 | + |
| 30 | +- ### Prepare the builder. |
| 31 | + - Create the `FingerprintDialogBuilder` and provide the title, subtitle and the description. Application should explain why they need to access user's fingerprint. |
| 32 | + -  |
| 33 | + |
| 34 | + #### Java: |
| 35 | + ```java |
| 36 | + final FingerprintDialogBuilder dialogBuilder = new FingerprintDialogBuilder(Activity.this) |
| 37 | + .setTitle(/* Title of the fingerprint dialog */) |
| 38 | + .setSubtitle(/* Subtitle of the fingerprint dialog */) |
| 39 | + .setDescription(/* Description of the fingerprint dialog */) |
| 40 | + .setNegativeButton(/* Negative button of the fingerprint dialog */); |
| 41 | + ``` |
| 42 | + |
| 43 | + #### Kotlin: |
| 44 | + ```kotlin |
| 45 | + val dialogBuilder = FingerprintDialogBuilder(this) |
| 46 | + .set Title(/* Title of the fingerprint dialog */) |
| 47 | + .setSubtitle(/* Subtitle of the fingerprint dialog */) |
| 48 | + .setDescription(/* Description of the fingerprint dialog */) |
| 49 | + .setNegativeButton(/* Negative button of the fingerprint dialog */) |
| 50 | + ``` |
| 51 | + |
| 52 | +- ### Implement callbacks and show the dialog. |
| 53 | + - Implement the `AuthenticationCallback` to get callbacks for error or success from the fingerprint authentication dialog. |
| 54 | + |
| 55 | + #### Java: |
| 56 | + ```java |
| 57 | + final AuthenticationCallback callback = new AuthenticationCallback() { |
| 58 | + @Override |
| 59 | + public void fingerprintAuthenticationNotSupported() { |
| 60 | + // Device doesn't support fingerprint authentication. May be device doesn't have fingerprint hardware or device is running on Android below Marshmallow. |
| 61 | + // Switch to alternate authentication method. |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void hasNoFingerprintEnrolled() { |
| 66 | + // User has no fingerprint enrolled. |
| 67 | + // Application should redirect the user to the lock screen settings. |
| 68 | + // FingerprintUtils.openSecuritySettings(this) |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onAuthenticationError(final int errorCode, @Nullable final CharSequence errString) { |
| 73 | + // Unrecoverable error. Cannot use fingerprint scanner. Library will stop scanning for the fingerprint after this callback. |
| 74 | + // Switch to alternate authentication method. |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onAuthenticationHelp(final int helpCode, @Nullable final CharSequence helpString) { |
| 79 | + // Authentication process has some warning. such as "Sensor dirty, please clean it." |
| 80 | + // Handle it if you want. Library will continue scanning for the fingerprint after this callback. |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void authenticationCanceledByUser() { |
| 85 | + // User canceled the authentication by tapping on the cancel button (which is at the bottom of the dialog). |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onAuthenticationSucceeded() { |
| 90 | + // Authentication success |
| 91 | + // Your user is now authenticated. |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onAuthenticationFailed() { |
| 96 | + // Authentication failed. |
| 97 | + // Library will continue scanning the fingerprint after this callback. |
| 98 | + } |
| 99 | + }; |
| 100 | + ``` |
| 101 | + |
| 102 | + #### Kotlin: |
| 103 | + ```kotlin |
| 104 | + val callback = object : AuthenticationCallback { |
| 105 | + |
| 106 | + override fun fingerprintAuthenticationNotSupported() { |
| 107 | + // Device doesn't support fingerprint authentication. May be device doesn't have fingerprint hardware or device is running on Android below Marshmallow. |
| 108 | + // Switch to alternate authentication method. |
| 109 | + } |
| 110 | + |
| 111 | + override fun hasNoFingerprintEnrolled() { |
| 112 | + // User has no fingerprint enrolled. |
| 113 | + // Application should redirect the user to the lock screen settings. |
| 114 | + // FingerprintUtils.openSecuritySettings(this@SecureActivity) |
| 115 | + } |
| 116 | + |
| 117 | + override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) { |
| 118 | + // Unrecoverable error. Cannot use fingerprint scanner. Library will stop scanning for the fingerprint after this callback. |
| 119 | + // Switch to alternate authentication method. |
| 120 | + } |
| 121 | + |
| 122 | + override fun onAuthenticationHelp(helpCode: Int, helpString: CharSequence?) { |
| 123 | + // Authentication process has some warning. such as "Sensor dirty, please clean it." |
| 124 | + // Handle it if you want. Library will continue scanning for the fingerprint after this callback. |
| 125 | + } |
| 126 | + |
| 127 | + override fun authenticationCanceledByUser() { |
| 128 | + // User canceled the authentication by tapping on the cancel button (which is at the bottom of the dialog). |
| 129 | + } |
| 130 | + |
| 131 | + override fun onAuthenticationSucceeded() { |
| 132 | + // Authentication success |
| 133 | + // Your user is now authenticated. |
| 134 | + } |
| 135 | + |
| 136 | + override fun onAuthenticationFailed() { |
| 137 | + // Authentication failed. |
| 138 | + // Library will continue scanning the fingerprint after this callback. |
| 139 | + } |
| 140 | + } |
| 141 | + ``` |
| 142 | + |
| 143 | + - Show the dialog and start fingerprint authentication. |
| 144 | + |
| 145 | + #### Java: |
| 146 | + ```java |
| 147 | + dialogBuilder.show(getSupportFragmentanager(), callback); |
| 148 | + ``` |
| 149 | + |
| 150 | + #### Kotlin: |
| 151 | + ```kotlin |
| 152 | + dialogBuilder.show(supportFragmentManager, callback) |
| 153 | + ``` |
| 154 | + |
| 155 | +## Screenshots: |
| 156 | + |
| 157 | +|Authentication success|Authentication fail| |
| 158 | +|:---:|:---:| |
| 159 | +||| |
| 160 | + |
| 161 | + |
| 162 | +## What to try this out? |
| 163 | +- You can download the sample apk from [here](https://github.com/kevalpatel2106/FingerprintDialogCompat/releases) and play with it. |
| 164 | + |
| 165 | + |
7 | 166 | ## Want to contribute?
|
8 | 167 | Every small or large contribution to this project is appreciated. Make sure you read the [contribution guide](/.github/CONTRIBUTING.md) before generating the pull request.
|
9 | 168 |
|
|
0 commit comments