Phase 2: biometric app lock
Browse files
app/src/main/java/io/github/abrarul/modularai/security/Lock.kt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package io.github.abrarul.modularai.security
|
| 2 |
+
|
| 3 |
+
import android.content.Context
|
| 4 |
+
import androidx.biometric.BiometricManager
|
| 5 |
+
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
|
| 6 |
+
import androidx.biometric.BiometricPrompt
|
| 7 |
+
import androidx.core.content.ContextCompat
|
| 8 |
+
import androidx.fragment.app.FragmentActivity
|
| 9 |
+
|
| 10 |
+
object Lock {
|
| 11 |
+
fun available(ctx: Context): Boolean =
|
| 12 |
+
BiometricManager.from(ctx).canAuthenticate(BIOMETRIC_WEAK) == BiometricManager.BIOMETRIC_SUCCESS
|
| 13 |
+
|
| 14 |
+
/** Calls [onOk] after a successful biometric check. Stays locked on cancel or failure. */
|
| 15 |
+
fun prompt(a: FragmentActivity, onOk: () -> Unit) {
|
| 16 |
+
if (!available(a)) return // caller only enables the lock when biometrics exist
|
| 17 |
+
val cb = object : BiometricPrompt.AuthenticationCallback() {
|
| 18 |
+
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) = onOk()
|
| 19 |
+
}
|
| 20 |
+
val info = BiometricPrompt.PromptInfo.Builder()
|
| 21 |
+
.setTitle("Unlock ModularAI")
|
| 22 |
+
.setNegativeButtonText("Cancel")
|
| 23 |
+
.setAllowedAuthenticators(BIOMETRIC_WEAK)
|
| 24 |
+
.build()
|
| 25 |
+
BiometricPrompt(a, ContextCompat.getMainExecutor(a), cb).authenticate(info)
|
| 26 |
+
}
|
| 27 |
+
}
|