@@ -10,44 +10,95 @@ import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG
10
10
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
11
11
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
12
12
import androidx.biometric.BiometricPrompt
13
+ import androidx.compose.material.icons.Icons
14
+ import androidx.compose.material.icons.filled.Info
13
15
import androidx.compose.material3.CircularProgressIndicator
14
16
import androidx.compose.runtime.Composable
15
- import androidx.compose.runtime.LaunchedEffect
17
+ import androidx.compose.runtime.DisposableEffect
18
+ import androidx.compose.runtime.mutableStateOf
19
+ import androidx.compose.runtime.remember
16
20
import androidx.compose.ui.Modifier
17
21
import androidx.compose.ui.platform.LocalContext
22
+ import androidx.compose.ui.platform.LocalLifecycleOwner
18
23
import androidx.core.content.ContextCompat
19
24
import androidx.fragment.app.FragmentActivity
25
+ import androidx.lifecycle.Lifecycle
26
+ import androidx.lifecycle.LifecycleEventObserver
20
27
import androidx.navigation.NavController
21
28
29
+ @Composable
30
+ fun LifecycleResumeEffect (onResume : () -> Unit ) {
31
+ // 現在の画面のLifecycleOwnerを取得
32
+ val lifecycleOwner = LocalLifecycleOwner .current
33
+ DisposableEffect (lifecycleOwner) {
34
+ // LifecycleEventObserverでLifecycleのイベントを監視する
35
+ val observer = LifecycleEventObserver { _, event ->
36
+ if (event == Lifecycle .Event .ON_RESUME ) {
37
+ // onResumeになったときに呼び出される
38
+ onResume()
39
+ }
40
+ }
41
+ // 監視を開始
42
+ lifecycleOwner.lifecycle.addObserver(observer)
43
+
44
+ // Composableが破棄されるときに呼び出される
45
+ onDispose {
46
+ lifecycleOwner.lifecycle.removeObserver(observer)
47
+ }
48
+ }
49
+ }
50
+
51
+
22
52
@Composable
23
53
fun AuthenticationScreen (
24
54
navController : NavController ,
25
55
modifier : Modifier = Modifier ,
26
56
) {
27
57
val context = LocalContext .current
28
- LaunchedEffect (key1 = Unit ) {
58
+ val openAlertDialog = remember { mutableStateOf(false ) }
59
+ when {
60
+ openAlertDialog.value -> {
61
+ AuthenticationErrorDialog (
62
+ onDismissRequest = { openAlertDialog.value = false },
63
+ onConfirmation = {
64
+ openAlertDialog.value = false
65
+ println (" Confirmation registered" ) // Add logic here to handle confirmation.
66
+ },
67
+ dialogTitle = " 端末のパスワードを設定してください" ,
68
+ dialogText = " 本サービスは、端末のパスワードを設定していただくと利用可能になります。" ,
69
+ icon = Icons .Default .Info
70
+ )
71
+ }
72
+ }
73
+ LifecycleResumeEffect {
29
74
when (BiometricManager .from(context).canAuthenticate(BIOMETRIC_STRONG or BIOMETRIC_WEAK or DEVICE_CREDENTIAL )) {
30
75
BiometricManager .BIOMETRIC_SUCCESS -> { // 生体認証が利用可能
31
76
showAuthenticationDialog(context, navController)
32
77
}
33
78
79
+
34
80
BiometricManager .BIOMETRIC_ERROR_NONE_ENROLLED -> { // 生体情報が端末に登録されていない
35
81
val enrollIntent =
36
82
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
37
- Intent (Settings .ACTION_BIOMETRIC_ENROLL ).apply {
83
+ val enrollIntent = Intent (Settings .ACTION_BIOMETRIC_ENROLL ).apply {
38
84
putExtra(
39
85
Settings .EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED ,
40
- BIOMETRIC_STRONG or DEVICE_CREDENTIAL ,
86
+ BIOMETRIC_STRONG or DEVICE_CREDENTIAL
41
87
)
42
88
}
89
+ context.startActivity(enrollIntent)
43
90
} else {
44
- TODO (" VERSION.SDK_INT < R" )
91
+ // Android 10 以下
92
+ showToast(context, " 端末のパスワードを設定してください" )
93
+ val securityIntent = Intent (Settings .ACTION_SECURITY_SETTINGS ).apply {
94
+ addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
95
+ }
96
+ context.startActivity(securityIntent)
45
97
}
46
- context.startActivity(enrollIntent)
47
98
}
48
99
49
100
BiometricManager .BIOMETRIC_ERROR_HW_UNAVAILABLE , BiometricManager .BIOMETRIC_ERROR_NO_HARDWARE -> { // 生体認証ハードウェアが利用不可
50
- TODO ( " ダイアログを表示 " )
101
+ showToast(context, " お使いの端末は対応本サービスを利用できません。 " )
51
102
}
52
103
53
104
else -> throw IllegalStateException (" ここには入らないはず。" )
@@ -56,6 +107,10 @@ fun AuthenticationScreen(
56
107
CircularProgressIndicator ()
57
108
}
58
109
110
+ fun showToast (context : android.content.Context , message : String ) {
111
+ Toast .makeText(context, message, Toast .LENGTH_SHORT ).show()
112
+ }
113
+
59
114
fun showAuthenticationDialog (
60
115
context : Context ,
61
116
navController : NavController ,
0 commit comments