-
Notifications
You must be signed in to change notification settings - Fork 599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passcode activity enhanced with biometric authentication #1905
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package com.mifos.mifosxdroid.passcode; | |
import android.content.Intent | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.appcompat.widget.AppCompatButton | ||
import androidx.core.widget.NestedScrollView | ||
import butterknife.BindView | ||
import butterknife.ButterKnife | ||
|
@@ -34,6 +35,20 @@ class PassCodeActivity : MifosPassCodeActivity() { | |
currPassCode = it.getStringExtra(Constants.CURR_PASSWORD) | ||
isToUpdatePassCode = it.getBooleanExtra(Constants.IS_TO_UPDATE_PASS_CODE, false) | ||
} | ||
if(findViewById<AppCompatButton>(R.id.btn_save).text.equals("Login with biometric")){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Aditya-gupta99 , |
||
BiometricAuthentication(this).authenticateWithBiometrics() | ||
} | ||
if(BiometricAuthentication(this).getBiometricCapabilities()==BiometricCapability.HAS_BIOMETRIC_AUTH){ | ||
val btn=findViewById<AppCompatButton>(R.id.btn_save) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. butterknife |
||
if (btn.visibility==View.GONE){ | ||
btn.visibility=View.VISIBLE | ||
btn.text="Login with biometric" | ||
btn.setOnClickListener { | ||
BiometricAuthentication(this).authenticateWithBiometrics() | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
override fun showToaster(view: View?, msg: Int) { | ||
|
@@ -66,4 +81,11 @@ class PassCodeActivity : MifosPassCodeActivity() { | |
} | ||
finish() | ||
} | ||
|
||
override fun onResume() { | ||
if(findViewById<AppCompatButton>(R.id.btn_save).text.equals("Login with biometric")){ | ||
BiometricAuthentication(this).authenticateWithBiometrics() | ||
} | ||
super.onResume() | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aditya-gupta99
what is expected here?