Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8f73b9bd authored by Jesse Dai's avatar Jesse Dai
Browse files

Convert KeyguardTouchHandlingViewModel to Activatable

Following go/sysui-arch:summer-24 to convert the view model into
an Activatable so the lifecycle of coroutines can be automatically
handled in views or composables.

Bug: 406586544
Test: atest SystemUiRoboTests
Test: manual - verify tap to wake with AOD on/off
Test: manual - verify long press launches default settings menu and
touching outside areas will dismiss the settings menu
Test: manual - verify double tap in empty space goes to sleep
Flag: EXEMPT refactor

Change-Id: Ic3314a1e4818379a3f7e306b49855afb35837423
parent 838013a2
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -36,25 +36,25 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.systemui.communal.ui.compose.extensions.detectLongPressGesture
import com.android.systemui.keyguard.ui.viewmodel.KeyguardTouchHandlingViewModel
import com.android.systemui.lifecycle.rememberViewModel

/** Container for lockscreen content that handles long-press to bring up the settings menu. */
@Composable
// TODO(b/344879669): now that it's more generic than long-press, rename it.
fun LockscreenLongPress(
    viewModel: KeyguardTouchHandlingViewModel,
    viewModelFactory: KeyguardTouchHandlingViewModel.Factory,
    modifier: Modifier = Modifier,
    content: @Composable BoxScope.(onSettingsMenuPlaces: (coordinates: Rect?) -> Unit) -> Unit,
) {
    val isEnabled: Boolean by
        viewModel.isLongPressHandlingEnabled.collectAsStateWithLifecycle(initialValue = false)
    val viewModel = rememberViewModel("LockscreenLongPress") { viewModelFactory.create() }
    val (settingsMenuBounds, setSettingsMenuBounds) = remember { mutableStateOf<Rect?>(null) }
    val interactionSource = remember { MutableInteractionSource() }

    Box(
        modifier =
            modifier
                .pointerInput(isEnabled) {
                    if (isEnabled) {
                .pointerInput(viewModel.isLongPressHandlingEnabled) {
                    if (viewModel.isLongPressHandlingEnabled) {
                        detectLongPressGesture { viewModel.onLongPress(isA11yAction = false) }
                    }
                }
+4 −1
Original line number Diff line number Diff line
@@ -38,7 +38,10 @@ class CommunalBlueprint @Inject constructor() : ComposableLockscreenSceneBluepri

    @Composable
    override fun ContentScope.Content(viewModel: LockscreenContentViewModel, modifier: Modifier) {
        LockscreenLongPress(viewModel = viewModel.touchHandling, modifier = modifier) { _ ->
        LockscreenLongPress(
            viewModelFactory = viewModel.touchHandlingFactory,
            modifier = modifier,
        ) { _ ->
            Box(modifier.background(Color.Black)) {
                Text(
                    text = "TODO(b/316211368): communal blueprint",
+4 −2
Original line number Diff line number Diff line
@@ -78,8 +78,10 @@ constructor(
            with(notificationSection) { HeadsUpNotifications() }
        }

        LockscreenLongPress(viewModel = viewModel.touchHandling, modifier = modifier) {
            onSettingsMenuPlaced ->
        LockscreenLongPress(
            viewModelFactory = viewModel.touchHandlingFactory,
            modifier = modifier,
        ) { onSettingsMenuPlaced ->
            Layout(
                content = {
                    // Constrained to above the lock icon.
+6 −12
Original line number Diff line number Diff line
@@ -42,25 +42,19 @@ class SettingsMenuSection
@Inject
constructor(
    private val viewModel: KeyguardSettingsMenuViewModel,
    private val touchHandlingViewModel: KeyguardTouchHandlingViewModel,
    private val touchHandlingViewModelFactory: KeyguardTouchHandlingViewModel.Factory,
    private val vibratorHelper: VibratorHelper,
    private val activityStarter: ActivityStarter,
) {
    @Composable
    @SuppressWarnings("InflateParams") // null is passed into the inflate call, on purpose.
    fun SettingsMenu(
        onPlaced: (Rect?) -> Unit,
        modifier: Modifier = Modifier,
    ) {
    fun SettingsMenu(onPlaced: (Rect?) -> Unit, modifier: Modifier = Modifier) {
        val (disposableHandle, setDisposableHandle) =
            remember { mutableStateOf<DisposableHandle?>(null) }
        AndroidView(
            factory = { context ->
                LayoutInflater.from(context)
                    .inflate(
                        R.layout.keyguard_settings_popup_menu,
                        null,
                    )
                    .inflate(R.layout.keyguard_settings_popup_menu, null)
                    .apply {
                        isVisible = false
                        alpha = 0f
@@ -69,7 +63,7 @@ constructor(
                            KeyguardSettingsViewBinder.bind(
                                view = this,
                                viewModel = viewModel,
                                touchHandlingViewModel = touchHandlingViewModel,
                                touchHandlingViewModelFactory = touchHandlingViewModelFactory,
                                rootViewModel = null,
                                vibratorHelper = vibratorHelper,
                                activityStarter = activityStarter,
@@ -81,11 +75,11 @@ constructor(
            modifier =
                modifier
                    .padding(
                        bottom = dimensionResource(R.dimen.keyguard_affordance_vertical_offset),
                        bottom = dimensionResource(R.dimen.keyguard_affordance_vertical_offset)
                    )
                    .padding(
                        horizontal =
                            dimensionResource(R.dimen.keyguard_affordance_horizontal_offset),
                            dimensionResource(R.dimen.keyguard_affordance_horizontal_offset)
                    )
                    .onPlaced { coordinates ->
                        onPlaced(
+2 −2
Original line number Diff line number Diff line
@@ -275,7 +275,7 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
    @Mock protected DreamingToLockscreenTransitionViewModel
            mDreamingToLockscreenTransitionViewModel;
    @Mock protected KeyguardTransitionInteractor mKeyguardTransitionInteractor;
    @Mock protected KeyguardTouchHandlingViewModel mKeyuardTouchHandlingViewModel;
    @Mock protected KeyguardTouchHandlingViewModel.Factory mKeyguardTouchHandlingViewModelFactory;
    @Mock protected WallpaperFocalAreaViewModel mWallpaperFocalAreaViewModel;
    @Mock protected AlternateBouncerInteractor mAlternateBouncerInteractor;
    @Mock protected MotionEvent mDownMotionEvent;
@@ -585,7 +585,7 @@ public class NotificationPanelViewControllerBaseTest extends SysuiTestCase {
                mMainDispatcher,
                mKeyguardTransitionInteractor,
                mDumpManager,
                mKeyuardTouchHandlingViewModel,
                mKeyguardTouchHandlingViewModelFactory,
                mWallpaperFocalAreaViewModel,
                mKeyguardInteractor,
                mActivityStarter,
Loading