|
| 1 | +package com.example.runningavater.authentication |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.os.Build |
| 6 | +import android.provider.Settings |
| 7 | +import android.widget.Toast |
| 8 | +import androidx.biometric.BiometricManager |
| 9 | +import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG |
| 10 | +import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK |
| 11 | +import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL |
| 12 | +import androidx.biometric.BiometricPrompt |
| 13 | +import androidx.compose.material3.CircularProgressIndicator |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.LaunchedEffect |
| 16 | +import androidx.compose.ui.Modifier |
| 17 | +import androidx.compose.ui.platform.LocalContext |
| 18 | +import androidx.core.content.ContextCompat |
| 19 | +import androidx.fragment.app.FragmentActivity |
| 20 | +import androidx.navigation.NavController |
| 21 | + |
| 22 | +@Composable |
| 23 | +fun AuthenticationScreen( |
| 24 | + navController: NavController, |
| 25 | + modifier: Modifier = Modifier, |
| 26 | +) { |
| 27 | + val context = LocalContext.current |
| 28 | + LaunchedEffect(key1 = Unit) { |
| 29 | + when (BiometricManager.from(context).canAuthenticate(BIOMETRIC_STRONG or BIOMETRIC_WEAK or DEVICE_CREDENTIAL)) { |
| 30 | + BiometricManager.BIOMETRIC_SUCCESS -> { // 生体認証が利用可能 |
| 31 | + showAuthenticationDialog(context, navController) |
| 32 | + } |
| 33 | + |
| 34 | + BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> { // 生体情報が端末に登録されていない |
| 35 | + val enrollIntent = |
| 36 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 37 | + Intent(Settings.ACTION_BIOMETRIC_ENROLL).apply { |
| 38 | + putExtra( |
| 39 | + Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED, |
| 40 | + BIOMETRIC_STRONG or DEVICE_CREDENTIAL, |
| 41 | + ) |
| 42 | + } |
| 43 | + } else { |
| 44 | + TODO("VERSION.SDK_INT < R") |
| 45 | + } |
| 46 | + context.startActivity(enrollIntent) |
| 47 | + } |
| 48 | + |
| 49 | + BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE, BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> { // 生体認証ハードウェアが利用不可 |
| 50 | + TODO("ダイアログを表示") |
| 51 | + } |
| 52 | + |
| 53 | + else -> throw IllegalStateException("ここには入らないはず。") |
| 54 | + } |
| 55 | + } |
| 56 | + CircularProgressIndicator() |
| 57 | +} |
| 58 | + |
| 59 | +fun showAuthenticationDialog( |
| 60 | + context: Context, |
| 61 | + navController: NavController, |
| 62 | +) { |
| 63 | + val biometricPrompt = |
| 64 | + BiometricPrompt( |
| 65 | + context as FragmentActivity, |
| 66 | + ContextCompat.getMainExecutor(context), |
| 67 | + object : BiometricPrompt.AuthenticationCallback() { |
| 68 | + override fun onAuthenticationError( |
| 69 | + errorCode: Int, |
| 70 | + errString: CharSequence, |
| 71 | + ) { |
| 72 | + super.onAuthenticationError(errorCode, errString) |
| 73 | + Toast |
| 74 | + .makeText( |
| 75 | + context, |
| 76 | + "Authentication error: $errString", |
| 77 | + Toast.LENGTH_SHORT, |
| 78 | + ).show() |
| 79 | + } |
| 80 | + |
| 81 | + override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { |
| 82 | + super.onAuthenticationSucceeded(result) |
| 83 | + navController.navigate("home") { |
| 84 | + // これにより、「ホーム」に戻るときにバックスタックがクリアされる |
| 85 | + popUpTo(navController.graph.startDestinationId) { |
| 86 | + saveState = true |
| 87 | + } |
| 88 | + launchSingleTop = true // 同じ画面を複数スタックしない |
| 89 | + restoreState = true // 前の状態を復元 |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + override fun onAuthenticationFailed() { |
| 94 | + super.onAuthenticationFailed() |
| 95 | + Toast |
| 96 | + .makeText( |
| 97 | + context, |
| 98 | + "Authentication failed", |
| 99 | + Toast.LENGTH_SHORT, |
| 100 | + ).show() |
| 101 | + } |
| 102 | + }, |
| 103 | + ) |
| 104 | + |
| 105 | + val promptInfo = |
| 106 | + BiometricPrompt.PromptInfo |
| 107 | + .Builder() |
| 108 | + .setTitle("Biometric login for my app") |
| 109 | + .setSubtitle("Log in using your biometric credential") |
| 110 | + .setAllowedAuthenticators(BIOMETRIC_STRONG or BIOMETRIC_WEAK or DEVICE_CREDENTIAL) |
| 111 | + .build() |
| 112 | + biometricPrompt.authenticate(promptInfo) |
| 113 | +} |
0 commit comments