<lambda>null1 package com.android.systemui.bouncer.ui.binder
2 
3 import android.view.ViewGroup
4 import androidx.activity.OnBackPressedDispatcher
5 import androidx.activity.OnBackPressedDispatcherOwner
6 import androidx.activity.setViewTreeOnBackPressedDispatcherOwner
7 import androidx.compose.ui.platform.ComposeView
8 import androidx.core.view.isVisible
9 import androidx.lifecycle.Lifecycle
10 import androidx.lifecycle.repeatOnLifecycle
11 import com.android.compose.theme.PlatformTheme
12 import com.android.keyguard.ViewMediatorCallback
13 import com.android.systemui.authentication.domain.interactor.AuthenticationInteractor
14 import com.android.systemui.bouncer.domain.interactor.PrimaryBouncerInteractor
15 import com.android.systemui.bouncer.ui.BouncerDialogFactory
16 import com.android.systemui.bouncer.ui.composable.BouncerContent
17 import com.android.systemui.bouncer.ui.viewmodel.BouncerViewModel
18 import com.android.systemui.lifecycle.repeatWhenAttached
19 import com.android.systemui.user.domain.interactor.SelectedUserInteractor
20 import kotlinx.coroutines.flow.collectLatest
21 import kotlinx.coroutines.launch
22 
23 /** View binder responsible for binding the compose version of the bouncer. */
24 object ComposeBouncerViewBinder {
25     fun bind(
26         view: ViewGroup,
27         legacyInteractor: PrimaryBouncerInteractor,
28         viewModel: BouncerViewModel,
29         dialogFactory: BouncerDialogFactory,
30         authenticationInteractor: AuthenticationInteractor,
31         selectedUserInteractor: SelectedUserInteractor,
32         viewMediatorCallback: ViewMediatorCallback?,
33     ) {
34         view.addView(
35             ComposeView(view.context).apply {
36                 repeatWhenAttached {
37                     repeatOnLifecycle(Lifecycle.State.CREATED) {
38                         setViewTreeOnBackPressedDispatcherOwner(
39                             object : OnBackPressedDispatcherOwner {
40                                 override val onBackPressedDispatcher =
41                                     OnBackPressedDispatcher().apply {
42                                         setOnBackInvokedDispatcher(
43                                             view.viewRootImpl.onBackInvokedDispatcher
44                                         )
45                                     }
46 
47                                 override val lifecycle: Lifecycle =
48                                     this@repeatWhenAttached.lifecycle
49                             }
50                         )
51                         setContent { PlatformTheme { BouncerContent(viewModel, dialogFactory) } }
52                     }
53                 }
54             }
55         )
56 
57         view.repeatWhenAttached {
58             repeatOnLifecycle(Lifecycle.State.CREATED) {
59                 launch {
60                     legacyInteractor.isShowing.collectLatest { bouncerShowing ->
61                         view.isVisible = bouncerShowing
62                     }
63                 }
64 
65                 launch {
66                     authenticationInteractor.onAuthenticationResult.collectLatest {
67                         authenticationSucceeded ->
68                         if (authenticationSucceeded) {
69                             // Some dismiss actions require that keyguard be dismissed right away or
70                             // deferred until something else later on dismisses keyguard (eg. end of
71                             // a hide animation).
72                             val deferKeyguardDone =
73                                 legacyInteractor.bouncerDismissAction?.onDismissAction?.onDismiss()
74                             legacyInteractor.setDismissAction(null, null)
75 
76                             viewMediatorCallback?.let {
77                                 val selectedUserId = selectedUserInteractor.getSelectedUserId()
78                                 if (deferKeyguardDone == true) {
79                                     it.keyguardDonePending(selectedUserId)
80                                 } else {
81                                     it.keyguardDone(selectedUserId)
82                                 }
83                             }
84                         }
85                     }
86                 }
87                 launch {
88                     legacyInteractor.startingDisappearAnimation.collectLatest {
89                         it.run()
90                         legacyInteractor.hide()
91                     }
92                 }
93             }
94         }
95     }
96 }
97