-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented biometric authentication
* Upstream sdk version is needed to compile the imported libraries Signed-off-by: aditya-gupta99 <[email protected]>
- Loading branch information
1 parent
c8c8cd0
commit cc9fb98
Showing
5 changed files
with
134 additions
and
1 deletion.
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
104 changes: 104 additions & 0 deletions
104
mifosng-android/src/main/java/com/mifos/mifosxdroid/passcode/BiometricAuthentication.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,104 @@ | ||
package com.mifos.mifosxdroid.passcode | ||
|
||
import android.content.Intent | ||
import androidx.biometric.BiometricManager | ||
import android.os.Build | ||
import android.provider.Settings | ||
import android.provider.Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.biometric.BiometricPrompt | ||
import androidx.core.content.ContextCompat | ||
import androidx.fragment.app.FragmentActivity | ||
import com.mifos.mifosxdroid.HomeActivity | ||
import com.mifos.mifosxdroid.R | ||
|
||
open class BiometricAuthentication( | ||
val context: FragmentActivity, | ||
) { | ||
private val executor = ContextCompat.getMainExecutor(context) | ||
private val callback = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
object : BiometricPrompt.AuthenticationCallback() { | ||
override fun onAuthenticationFailed() { | ||
super.onAuthenticationFailed() | ||
} | ||
|
||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | ||
super.onAuthenticationSucceeded(result) | ||
val intent= Intent(context, HomeActivity::class.java) | ||
context.startActivity(intent) | ||
} | ||
|
||
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { | ||
super.onAuthenticationError(errorCode, errString) | ||
} | ||
} | ||
} else { | ||
TODO("VERSION.SDK_INT < P") | ||
} | ||
|
||
private val biometricPrompt = BiometricPrompt(context, executor, callback) | ||
|
||
|
||
|
||
fun launchBiometricEnrollment(resultLauncher: ActivityResultLauncher<Intent>) { | ||
val intent: Intent = when { | ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> { | ||
Intent(Settings.ACTION_BIOMETRIC_ENROLL).putExtra( | ||
EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED, | ||
BiometricManager.Authenticators.BIOMETRIC_STRONG or BiometricManager.Authenticators.BIOMETRIC_WEAK | ||
) | ||
} | ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> { | ||
Intent(Settings.ACTION_FINGERPRINT_ENROLL) | ||
} | ||
else -> { | ||
Intent(Settings.ACTION_SECURITY_SETTINGS) | ||
} | ||
} | ||
resultLauncher.launch(intent) | ||
} | ||
|
||
fun getBiometricCapabilities(): BiometricCapability { | ||
val biometricManager = BiometricManager.from(context) | ||
return when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)) { | ||
BiometricManager.BIOMETRIC_SUCCESS -> { | ||
BiometricCapability.HAS_BIOMETRIC_AUTH | ||
} | ||
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> { | ||
BiometricCapability.NOT_SUPPORTED | ||
} | ||
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> { | ||
BiometricCapability.NOT_SUPPORTED | ||
} | ||
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> { | ||
BiometricCapability.NOT_ENROLLED | ||
} | ||
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> { | ||
BiometricCapability.SECURITY_UPDATE_REQUIRED | ||
} | ||
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> { | ||
BiometricCapability.NOT_SUPPORTED | ||
} | ||
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> { | ||
BiometricCapability.NOT_SUPPORTED | ||
} | ||
else -> { | ||
BiometricCapability.NOT_SUPPORTED | ||
} | ||
} | ||
} | ||
|
||
fun authenticateWithBiometrics() { | ||
val promptInfo = BiometricPrompt.PromptInfo.Builder().apply { | ||
setTitle(context.getString(R.string.unlocked_mifos)) | ||
setDescription(context.getString(R.string.text_description_biometrics_dialog)) | ||
setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL) | ||
}.build() | ||
|
||
biometricPrompt.authenticate(promptInfo) | ||
} | ||
} | ||
|
||
enum class BiometricCapability { | ||
HAS_BIOMETRIC_AUTH, NOT_ENROLLED, SECURITY_UPDATE_REQUIRED, NOT_SUPPORTED | ||
} |
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