Skip to content
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

feat : fingerprint authentication added in the passcode activity #2034

Merged
merged 2 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ dependencies {
//TableView
implementation "com.evrencoskun.library:tableview:$rootProject.tableViewVersion"

//Biometric Authentication
implementation "androidx.biometric:biometric:$rootProject.biometric"

// Unit tests dependencies
testImplementation "junit:junit:$rootProject.jUnitVersion"
testImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.mifos.mobile.ui.activities

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 org.mifos.mobile.R
import org.mifos.mobile.ui.enums.BiometricCapability

open class BiometricAuthentication(
val context: FragmentActivity,
) {
private val executor = ContextCompat.getMainExecutor(context)
private val callback = 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)
}
}

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_NONE_ENROLLED -> {
BiometricCapability.NOT_ENROLLED
}
else -> {
BiometricCapability.NOT_SUPPORTED
}
}
}

fun authenticateWithBiometrics() {
val promptInfo = BiometricPrompt.PromptInfo.Builder().apply {
setTitle(context.getString(R.string.sign_in_fingerprint))
setDescription(context.getString(R.string.scan_your_fingerprint))
setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)
}.build()

biometricPrompt.authenticate(promptInfo)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.appcompat.widget.AppCompatButton
import butterknife.BindView
import butterknife.ButterKnife

import com.mifos.mobile.passcode.MifosPassCodeActivity
import com.mifos.mobile.passcode.utils.EncryptionUtil

import org.mifos.mobile.R
import org.mifos.mobile.ui.enums.BiometricCapability
import org.mifos.mobile.utils.CheckSelfPermissionAndRequest
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.MaterialDialog
Expand All @@ -18,30 +23,60 @@ import org.mifos.mobile.utils.Toaster
class PassCodeActivity : MifosPassCodeActivity() {
private var currPassCode: String? = null
private var isToUpdatePassCode: Boolean = false

@JvmField
@BindView(R.id.btn_save)
var btnSave: AppCompatButton? = null

private var biometricAuthentication: BiometricAuthentication? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!CheckSelfPermissionAndRequest.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)) {
ButterKnife.bind(this)
biometricAuthentication = BiometricAuthentication(this)

if (!CheckSelfPermissionAndRequest.checkSelfPermission(
this,
Manifest.permission.READ_PHONE_STATE
)
) {
requestPermission()
}
if (btnSave?.text?.equals(getString(R.string.use_touch_id)) == true) {
biometricAuthentication?.authenticateWithBiometrics()
}
if (biometricAuthentication?.getBiometricCapabilities() == BiometricCapability.HAS_BIOMETRIC_AUTH) {
if (btnSave?.visibility == View.GONE) {
btnSave?.visibility = View.VISIBLE
btnSave?.text = getString(R.string.use_touch_id)
btnSave?.setOnClickListener {
biometricAuthentication?.authenticateWithBiometrics()
}
}
} else if (biometricAuthentication?.getBiometricCapabilities() == BiometricCapability.NOT_SUPPORTED) {
startActivity(Intent(Settings.ACTION_SECURITY_SETTINGS))
}

intent?.let {
currPassCode = it.getStringExtra(Constants.CURR_PASSWORD)
isToUpdatePassCode = it.getBooleanExtra(Constants.IS_TO_UPDATE_PASS_CODE, false)
}
}

/**
* Uses [CheckSelfPermissionAndRequest] to check for runtime permissions
* Uses [CheckSelfPermissionAndRequest] to check fomifosr runtime permissions
*/
private fun requestPermission() {
CheckSelfPermissionAndRequest.requestPermission(
this,
Manifest.permission.READ_PHONE_STATE,
Constants.PERMISSIONS_REQUEST_READ_PHONE_STATE,
resources.getString(
R.string.dialog_message_phone_state_permission_denied_prompt),
resources.getString(R.string.dialog_message_phone_state_permission_never_ask_again),
Constants.PERMISSIONS_READ_PHONE_STATE_STATUS)
this,
Manifest.permission.READ_PHONE_STATE,
Constants.PERMISSIONS_REQUEST_READ_PHONE_STATE,
resources.getString(
R.string.dialog_message_phone_state_permission_denied_prompt
),
resources.getString(R.string.dialog_message_phone_state_permission_never_ask_again),
Constants.PERMISSIONS_READ_PHONE_STATE_STATUS
)
}

override fun getLogo(): Int {
Expand All @@ -54,20 +89,22 @@ class PassCodeActivity : MifosPassCodeActivity() {

override fun startLoginActivity() {
MaterialDialog.Builder().init(this@PassCodeActivity)
.setCancelable(false)
.setMessage(R.string.login_using_password_confirmation)
.setPositiveButton(getString(R.string.logout),
DialogInterface.OnClickListener { _, _ ->
val i = Intent(this@PassCodeActivity,
LoginActivity::class.java)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)
finish()
})
.setNegativeButton(getString(R.string.cancel),
DialogInterface.OnClickListener { dialog, _ -> dialog.dismiss() })
.createMaterialDialog()
.show()
.setCancelable(false)
.setMessage(R.string.login_using_password_confirmation)
.setPositiveButton(getString(R.string.logout),
DialogInterface.OnClickListener { _, _ ->
val i = Intent(
this@PassCodeActivity,
LoginActivity::class.java
)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)
finish()
})
.setNegativeButton(getString(R.string.cancel),
DialogInterface.OnClickListener { dialog, _ -> dialog.dismiss() })
.createMaterialDialog()
.show()
}

override fun showToaster(view: View, msg: Int) {
Expand All @@ -77,4 +114,11 @@ class PassCodeActivity : MifosPassCodeActivity() {
override fun getEncryptionType(): Int {
return EncryptionUtil.MOBILE_BANKING
}

override fun onResume() {
if (btnSave?.text?.equals(getString(R.string.use_touch_id)) == true) {
biometricAuthentication?.authenticateWithBiometrics()
}
super.onResume()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mifos.mobile.ui.enums

enum class BiometricCapability {
HAS_BIOMETRIC_AUTH, NOT_ENROLLED, NOT_SUPPORTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class HomeFragment : BaseFragment(), HomeView, OnRefreshListener {
inflater.inflate(R.menu.menu_main, menu)
val menuItem = menu.findItem(R.id.menu_notifications)
val count = menuItem.actionView
tvNotificationCount = count.findViewById(R.id.tv_notification_indicator)
count.setOnClickListener {
tvNotificationCount = count?.findViewById(R.id.tv_notification_indicator)
count?.setOnClickListener {
(activity as BaseActivity?)?.replaceFragment(NotificationFragment.newInstance(),
true, R.id.container)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ class HomeOldFragment : BaseFragment(), HomeOldView, OnRefreshListener {
inflater.inflate(R.menu.menu_main, menu)
val menuItem = menu.findItem(R.id.menu_notifications)
val count = menuItem.actionView
tvNotificationCount = count.findViewById(R.id.tv_notification_indicator)
tvNotificationCount = count?.findViewById(R.id.tv_notification_indicator)
presenter?.unreadNotificationsCount
count.setOnClickListener { startActivity(Intent(context, NotificationActivity::class.java)) }
count?.setOnClickListener { startActivity(Intent(context, NotificationActivity::class.java)) }
super.onCreateOptionsMenu(menu, inflater)
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<string name="verification_mode">Verification Mode</string>
<string name="blank">&#160;</string>
<string name="import_qr">Import QR</string>
<string name="use_touch_id">Use Touch Id</string>

<string name="view_guarantor">View Guarantor</string>
<string name="add_guarantor">Add Guarantor</string>
Expand Down Expand Up @@ -490,6 +491,8 @@
<string name="no_saving_account">Saving Accounts</string>
<string name="no_loan_account">Loan Accounts</string>
<string name="no_sharing_account">Sharing Accounts</string>
<string name="sign_in_fingerprint">Sign in using fingerprint</string>
<string name="scan_your_fingerprint">Scan your fingerprint</string>


<string-array name="faq_qs">
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ task clean(type: Delete) {
ext {
// Sdk and tools
minSdkVersion = 22
targetSdkVersion = 31
compileSdkVersion = 31
targetSdkVersion = 33
compileSdkVersion = 33

// App dependencies
countryCodePicker = '2.5.1'
Expand All @@ -66,6 +66,7 @@ ext {
oss_licenses = '17.0.0'
kotlinVersion = '1.6.10'
tableViewVersion = '0.8.9.4'
biometric = '1.1.0'

jUnitVersion = '4.13.2'
mockitoVersion = '3.6.28'
Expand Down