Skip to content

Commit

Permalink
Fingeprint authentication added wrt issue 1208
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 committed Feb 2, 2023
1 parent de91381 commit e98209d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package org.mifos.mobile.ui.activities

import android.Manifest
import android.annotation.SuppressLint
import android.app.KeyguardManager
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.hardware.biometrics.BiometricPrompt
import android.os.Build
import android.os.Bundle
import android.os.CancellationSignal
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatButton
import androidx.core.app.ActivityCompat

import com.mifos.mobile.passcode.MifosPassCodeActivity
import com.mifos.mobile.passcode.utils.EncryptionUtil
Expand All @@ -15,15 +30,42 @@ import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.MaterialDialog
import org.mifos.mobile.utils.Toaster

@RequiresApi(Build.VERSION_CODES.M)
class PassCodeActivity : MifosPassCodeActivity() {

private var cancellationSignal: CancellationSignal? = null

private val authenticationCallback: BiometricPrompt.AuthenticationCallback
get() = @RequiresApi(Build.VERSION_CODES.P)
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
super.onAuthenticationError(errorCode, errString)
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult?) {
super.onAuthenticationSucceeded(result)
startNextActivity()
}
}

@SuppressLint("ResourceType")
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!CheckSelfPermissionAndRequest.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)) {
if (!CheckSelfPermissionAndRequest.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE ))
{
requestPermission()
}
if(findViewById<AppCompatButton>(R.id.btn_save).visibility==View.GONE){
val btn=findViewById<AppCompatButton>(R.id.btn_save)
btn.visibility=View.VISIBLE
btn.text="Use Touch Id"
btn.setOnClickListener { openBiometric() }
}
checkBiometricSupport() // checks if the device has finger print enabled
if(findViewById<AppCompatButton>(R.id.btn_save).text.equals("Use Touch Id")){
openBiometric()
}
}

/**
* Uses [CheckSelfPermissionAndRequest] to check for runtime permissions
*/
Expand Down Expand Up @@ -63,6 +105,42 @@ class PassCodeActivity : MifosPassCodeActivity() {
.createMaterialDialog()
.show()
}
private fun getCancellationSignal(): CancellationSignal {
cancellationSignal = CancellationSignal()
cancellationSignal?.setOnCancelListener {

}
return cancellationSignal as CancellationSignal
}
@RequiresApi(Build.VERSION_CODES.M)
private fun checkBiometricSupport(): Boolean {
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (!keyguardManager.isDeviceSecure) {
notifyUser("Fingerprint authentication has not been enabled in settings")
return false
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.USE_BIOMETRIC) != PackageManager.PERMISSION_GRANTED) {
notifyUser("Fingerprint Authentication Permission is not enabled")
return false
}
return if (packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
true
} else true
}
private fun openBiometric(){
val biometricPrompt = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
BiometricPrompt.Builder(this)
.setTitle("Sign in Using fingerprint")
.setNegativeButton(
"Enter Pin",
this.mainExecutor,
DialogInterface.OnClickListener { dialog, which ->
}).build()
} else {
TODO("VERSION.SDK_INT < P")
}
biometricPrompt.authenticate(getCancellationSignal(), mainExecutor, authenticationCallback)
}

override fun showToaster(view: View, msg: Int) {
Toaster.show(view, msg)
Expand All @@ -71,4 +149,14 @@ class PassCodeActivity : MifosPassCodeActivity() {
override fun getEncryptionType(): Int {
return EncryptionUtil.MOBILE_BANKING
}
private fun notifyUser(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

override fun onResume() {
if(findViewById<AppCompatButton>(R.id.btn_save).text.equals("Use Touch Id")){
openBiometric()
}
super.onResume()
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ext {
retrofitVersion = '2.2.0'
okHttp3Version = '3.6.0'
butterKnifeVersion = '8.0.1'
dbflowVersion = '4.1.2'
dbflowVersion = '4.2.4'
playServicesVersion = '17.0.0'
firebaseMessagingVersion = '21.0.1'
oss_licenses = '17.0.0'
Expand Down

0 comments on commit e98209d

Please sign in to comment.