android6-5 Exercise 2: Biometric gate ================================== import androidx.biometric.BiometricManager import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL import androidx.biometric.BiometricPrompt import androidx.core.content.ContextCompat import androidx.fragment.app.FragmentActivity // Docs show the View-style API: an executor, a callback, a PromptInfo. There is no Compose wrapper // in the sources checked, so a small function bridges it. MainActivity must be a FragmentActivity // (ComponentActivity alone is not enough for BiometricPrompt: general knowledge, check). fun showBiometricPrompt(activity: FragmentActivity, onSuccess: () -> Unit, onFail: () -> Unit) { val authenticators = BIOMETRIC_STRONG or DEVICE_CREDENTIAL val canAuthenticate = BiometricManager.from(activity).canAuthenticate(authenticators) if (canAuthenticate != BiometricManager.BIOMETRIC_SUCCESS) { onFail(); return } val prompt = BiometricPrompt( activity, ContextCompat.getMainExecutor(activity), object : BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) = onSuccess() override fun onAuthenticationError(errorCode: Int, errString: CharSequence) = onFail() } ) val info = BiometricPrompt.PromptInfo.Builder() .setTitle("Unlock your notes") .setSubtitle("Use your fingerprint, face or screen lock") .setAllowedAuthenticators(authenticators) // as in the docs' example .build() prompt.authenticate(info) // no CryptoObject: this is a simple "who is holding the phone" gate } Test: emulator Settings > Security > fingerprint (enrol one, general knowledge; menu names vary) and an emulator with no screen lock/biometrics. canAuthenticate returns BIOMETRIC_SUCCESS when something usable is enrolled; otherwise a different code (docs list them): report the exact values you see. With nothing enrolled the app should say so and not show the private notes (or offer the screen-lock route). Make sure MainActivity extends FragmentActivity (AppCompatActivity does) if the prompt will not build. Code unrun.