-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: option to require biometric authentication
- Loading branch information
Showing
8 changed files
with
138 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/bnyro/contacts/util/BiometricAuthUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.bnyro.contacts.util | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.biometric.BiometricManager | ||
import androidx.biometric.BiometricPrompt | ||
import androidx.core.content.ContextCompat | ||
import com.bnyro.contacts.R | ||
import com.bnyro.contacts.ui.activities.BaseActivity | ||
|
||
object BiometricAuthUtil { | ||
private const val ALLOWED_AUTHENTICATORS = | ||
BiometricManager.Authenticators.BIOMETRIC_STRONG or | ||
BiometricManager.Authenticators.BIOMETRIC_WEAK or | ||
BiometricManager.Authenticators.DEVICE_CREDENTIAL | ||
|
||
@RequiresApi(Build.VERSION_CODES.P) | ||
fun requestAuth(context: Context, onResult: (Boolean) -> Unit) { | ||
val executor = ContextCompat.getMainExecutor(context) | ||
|
||
val biometricPrompt = BiometricPrompt(context as BaseActivity, executor, | ||
object : BiometricPrompt.AuthenticationCallback() { | ||
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { | ||
super.onAuthenticationError(errorCode, errString) | ||
onResult(false) | ||
} | ||
|
||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | ||
super.onAuthenticationSucceeded(result) | ||
onResult(true) | ||
} | ||
|
||
override fun onAuthenticationFailed() { | ||
super.onAuthenticationFailed() | ||
onResult(false) | ||
} | ||
} | ||
) | ||
|
||
val promptInfo = BiometricPrompt.PromptInfo.Builder() | ||
.setTitle(context.getString(R.string.biometric_authentication)) | ||
.setAllowedAuthenticators(ALLOWED_AUTHENTICATORS) | ||
.build() | ||
|
||
biometricPrompt.authenticate(promptInfo) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters