|
| 1 | +package ml.dev.kotlin.openotp.util |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import androidx.biometric.BiometricManager |
| 6 | +import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK |
| 7 | +import androidx.biometric.BiometricPrompt |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.compose.runtime.LaunchedEffect |
| 10 | +import androidx.compose.ui.platform.LocalContext |
| 11 | +import androidx.compose.ui.platform.LocalLifecycleOwner |
| 12 | +import androidx.core.content.ContextCompat |
| 13 | +import androidx.fragment.app.Fragment |
| 14 | +import androidx.fragment.app.FragmentActivity |
| 15 | +import androidx.fragment.app.FragmentManager |
| 16 | +import androidx.lifecycle.Lifecycle |
| 17 | +import androidx.lifecycle.LifecycleObserver |
| 18 | +import androidx.lifecycle.LifecycleOwner |
| 19 | +import androidx.lifecycle.OnLifecycleEvent |
| 20 | +import java.util.concurrent.Executor |
| 21 | +import kotlin.coroutines.suspendCoroutine |
| 22 | + |
| 23 | +actual class BiometryAuthenticator( |
| 24 | + private val applicationContext: Context, |
| 25 | +) { |
| 26 | + private var fragmentManager: FragmentManager? = null |
| 27 | + |
| 28 | + fun bind(lifecycle: Lifecycle, fragmentManager: FragmentManager) { |
| 29 | + this.fragmentManager = fragmentManager |
| 30 | + |
| 31 | + val observer = object : LifecycleObserver { |
| 32 | + |
| 33 | + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) |
| 34 | + fun onDestroyed(source: LifecycleOwner) { |
| 35 | + this@BiometryAuthenticator.fragmentManager = null |
| 36 | + source.lifecycle.removeObserver(this) |
| 37 | + } |
| 38 | + } |
| 39 | + lifecycle.addObserver(observer) |
| 40 | + } |
| 41 | + |
| 42 | + actual suspend fun checkBiometryAuthentication( |
| 43 | + requestTitle: String, |
| 44 | + requestReason: String, |
| 45 | + ): Boolean { |
| 46 | + val resolverFragment: ResolverFragment = getResolverFragment() |
| 47 | + |
| 48 | + return suspendCoroutine { continuation -> |
| 49 | + var resumed = false |
| 50 | + resolverFragment.showBiometricPrompt( |
| 51 | + requestTitle = requestTitle, |
| 52 | + requestReason = requestReason, |
| 53 | + ) { |
| 54 | + if (!resumed) { |
| 55 | + continuation.resumeWith(it) |
| 56 | + resumed = true |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + actual fun isBiometricAvailable(): Boolean { |
| 63 | + val manager: BiometricManager = BiometricManager.from(applicationContext) |
| 64 | + return manager.canAuthenticate(BIOMETRIC_WEAK) == BiometricManager.BIOMETRIC_SUCCESS |
| 65 | + } |
| 66 | + |
| 67 | + private fun getResolverFragment(): ResolverFragment { |
| 68 | + val fragmentManager = fragmentManager |
| 69 | + ?: error("can't check biometry without active window") |
| 70 | + |
| 71 | + val currentFragment = fragmentManager |
| 72 | + .findFragmentByTag(BIOMETRY_RESOLVER_FRAGMENT_TAG) |
| 73 | + |
| 74 | + return if (currentFragment != null) { |
| 75 | + currentFragment as ResolverFragment |
| 76 | + } else { |
| 77 | + ResolverFragment().apply { |
| 78 | + fragmentManager |
| 79 | + .beginTransaction() |
| 80 | + .add(this, BIOMETRY_RESOLVER_FRAGMENT_TAG) |
| 81 | + .commitNow() |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + class ResolverFragment : Fragment() { |
| 87 | + private lateinit var executor: Executor |
| 88 | + private lateinit var biometricPrompt: BiometricPrompt |
| 89 | + private lateinit var promptInfo: BiometricPrompt.PromptInfo |
| 90 | + |
| 91 | + init { |
| 92 | + retainInstance = true |
| 93 | + } |
| 94 | + |
| 95 | + fun showBiometricPrompt( |
| 96 | + requestTitle: String, |
| 97 | + requestReason: String, |
| 98 | + callback: (Result<Boolean>) -> Unit, |
| 99 | + ) { |
| 100 | + val context = requireContext() |
| 101 | + |
| 102 | + executor = ContextCompat.getMainExecutor(context) |
| 103 | + |
| 104 | + biometricPrompt = BiometricPrompt(this, executor, |
| 105 | + object : BiometricPrompt.AuthenticationCallback() { |
| 106 | + @SuppressLint("RestrictedApi") |
| 107 | + override fun onAuthenticationError( |
| 108 | + errorCode: Int, |
| 109 | + errString: CharSequence, |
| 110 | + ) { |
| 111 | + super.onAuthenticationError(errorCode, errString) |
| 112 | + if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON || |
| 113 | + errorCode == BiometricPrompt.ERROR_USER_CANCELED |
| 114 | + ) { |
| 115 | + callback.invoke(Result.success(false)) |
| 116 | + } else { |
| 117 | + callback.invoke(Result.failure(Exception(errString.toString()))) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + override fun onAuthenticationSucceeded( |
| 122 | + result: BiometricPrompt.AuthenticationResult, |
| 123 | + ) { |
| 124 | + super.onAuthenticationSucceeded(result) |
| 125 | + callback.invoke(Result.success(true)) |
| 126 | + } |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + promptInfo = BiometricPrompt.PromptInfo.Builder() |
| 131 | + .setTitle(requestTitle) |
| 132 | + .setSubtitle(requestReason) |
| 133 | + .setDeviceCredentialAllowed(true) |
| 134 | + .build() |
| 135 | + |
| 136 | + biometricPrompt.authenticate(promptInfo) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + companion object { |
| 141 | + private const val BIOMETRY_RESOLVER_FRAGMENT_TAG = "BiometryControllerResolver" |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +@Composable |
| 146 | +actual fun BindBiometryAuthenticatorEffect(biometryAuthenticator: BiometryAuthenticator) { |
| 147 | + val lifecycleOwner = LocalLifecycleOwner.current |
| 148 | + val context = LocalContext.current |
| 149 | + |
| 150 | + LaunchedEffect(biometryAuthenticator, lifecycleOwner, context) { |
| 151 | + val fragmentManager = (context as FragmentActivity).supportFragmentManager |
| 152 | + biometryAuthenticator.bind(lifecycleOwner.lifecycle, fragmentManager) |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments