Skip to content

Instantly share code, notes, and snippets.

@faridfor
Created January 7, 2020 11:49
Show Gist options
  • Save faridfor/afb41424ef36a7197488efed9d2c07af to your computer and use it in GitHub Desktop.
Save faridfor/afb41424ef36a7197488efed9d2c07af to your computer and use it in GitHub Desktop.
package com.test.biometrictest
import android.util.Log
import androidx.biometric.BiometricPrompt
class MyCallBack(private val myData: MyData) :
BiometricPrompt.AuthenticationCallback() {
init {
Log.e("MyCallback", "onConstruct data: $myData")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Log.e("MyCallback", "onSuccess data: $myData")
}
}
@faridfor
Copy link
Author

faridfor commented Jan 7, 2020

output :
E/MyCallback: onConstruct data: MyData(id=1, text=first)
E/MyCallback: onSuccess data: MyData(id=1, text=first)
E/MyCallback: onConstruct data: MyData(id=2, text=second)
E/MyCallback: onSuccess data: MyData(id=1, text=first)

@faridfor
Copy link
Author

faridfor commented Jan 7, 2020

package com.test.biometrictest

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<View>(R.id.first).setOnClickListener {
            authenticate(MyData(1, "first"))
        }

        findViewById<View>(R.id.second).setOnClickListener {
            authenticate(MyData(2, "second"))
        }
    }

    private fun authenticate(data: MyData) {
        Log.e(TAG, "starting auth with $data")
        val biometricPrompt = BiometricPrompt(
            this,
            ContextCompat.getMainExecutor(this),
            MyCallBack(data)
            /*object : BiometricPrompt.AuthenticationCallback() {
                override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                    Log.e(TAG, "auth done : $data")
                }
            }*/)

        val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setDeviceCredentialAllowed(true)
            .setTitle("title")
            .build()
        biometricPrompt.authenticate(promptInfo)
    }

    companion object {
        private val TAG = MainActivity::class.java.name

    }
}

data class MyData(
    var id: Int,
    val text: String
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment