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

Commit 691ad7ea authored by Sherry Zhou's avatar Sherry Zhou Committed by Android (Google) Code Review
Browse files

Merge "Fix Magic Portrait is off center" into main

parents 0e529667 54712785
Loading
Loading
Loading
Loading
+120 −30
Original line number Original line Diff line number Diff line
@@ -23,8 +23,10 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.flags.DisableSceneContainer
import com.android.systemui.keyguard.data.repository.fakeKeyguardTransitionRepository
import com.android.systemui.keyguard.data.repository.fakeKeyguardTransitionRepository
import com.android.systemui.keyguard.domain.interactor.keyguardTransitionInteractor
import com.android.systemui.keyguard.domain.interactor.keyguardTransitionInteractor
import com.android.systemui.keyguard.shared.model.KeyguardState.GONE
import com.android.systemui.keyguard.shared.model.KeyguardState.LOCKSCREEN
import com.android.systemui.keyguard.shared.model.KeyguardState.LOCKSCREEN
import com.android.systemui.keyguard.shared.model.TransitionState
import com.android.systemui.keyguard.shared.model.TransitionState
import com.android.systemui.keyguard.shared.model.TransitionStep
import com.android.systemui.keyguard.shared.model.TransitionStep
@@ -76,7 +78,7 @@ class WallpaperFocalAreaViewModelTest : SysuiTestCase() {
                        )
                        )
                )
                )
            )
            )
            .thenReturn(2f)
            .thenReturn(1f)
        kosmos.wallpaperFocalAreaInteractor =
        kosmos.wallpaperFocalAreaInteractor =
            WallpaperFocalAreaInteractor(
            WallpaperFocalAreaInteractor(
                context = kosmos.mockedContext,
                context = kosmos.mockedContext,
@@ -91,58 +93,146 @@ class WallpaperFocalAreaViewModelTest : SysuiTestCase() {
    }
    }


    @Test
    @Test
    @DisableSceneContainer
    fun focalAreaBoundsSent_whenFinishTransitioningToLockscreen() =
    fun focalAreaBoundsSent_whenFinishTransitioningToLockscreen() =
        testScope.runTest {
        testScope.runTest {
            overrideMockedResources(
                mockedResources,
                OverrideResources(
                    screenWidth = 1600,
                    screenHeight = 2000,
                    centerAlignFocalArea = false,
                ),
            )
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)

            kosmos.wallpaperFocalAreaRepository.setHasFocalArea(true)
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(
                listOf(TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN)),
                    TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN),
                testScope,
                    TransitionStep(transitionState = TransitionState.FINISHED, to = LOCKSCREEN),
            )
                ),
            setTestFocalAreaBounds()
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.FINISHED, to = LOCKSCREEN)),
                testScope,
                testScope,
            )
            )
            assertThat(bounds).isNotNull()
        }


    @Test
    @DisableSceneContainer
    fun wallpaperHasFocalArea_shouldSendBounds() =
        testScope.runTest {
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)
            kosmos.wallpaperFocalAreaRepository.setHasFocalArea(true)
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN)),
                testScope,
            )
            setTestFocalAreaBounds()
            setTestFocalAreaBounds()
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.FINISHED, to = LOCKSCREEN)),
                testScope,
            )


            assertThat(bounds).isEqualTo(RectF(400F, 510F, 1200F, 700F))
            assertThat(bounds).isNotNull()
        }
        }


    @Test
    @Test
    fun focalAreaBoundsNotSent_whenNotFinishTransitioningToLockscreen() =
    @DisableSceneContainer
    fun wallpaperDoesNotHaveFocalArea_shouldNotSendBounds() =
        testScope.runTest {
        testScope.runTest {
            overrideMockedResources(
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)
                mockedResources,
            kosmos.wallpaperFocalAreaRepository.setHasFocalArea(false)
                OverrideResources(
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                    screenWidth = 1600,
                listOf(TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN)),
                    screenHeight = 2000,
                testScope,
                    centerAlignFocalArea = false,
                ),
            )
            )
            kosmos.wallpaperFocalAreaRepository.setWallpaperFocalAreaBounds(
                DEFAULT_FOCAL_AREA_BOUNDS
            )
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.FINISHED, to = LOCKSCREEN)),
                testScope,
            )

            assertThat(bounds).isNull()
        }

    @Test
    @DisableSceneContainer
    fun boundsChangeWhenGoingFromLockscreenToGone_shouldNotSendBounds() =
        testScope.runTest {
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)
            kosmos.wallpaperFocalAreaRepository.setHasFocalArea(true)
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN)),
                testScope,
            )

            setTestFocalAreaBounds()
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.FINISHED, to = LOCKSCREEN)),
                testScope,
            )
            assertThat(bounds?.top).isEqualTo(20F)
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(
                    TransitionStep(
                        transitionState = TransitionState.STARTED,
                        from = LOCKSCREEN,
                        to = GONE,
                    )
                ),
                testScope,
            )
            setTestFocalAreaBounds(
                activeNotifs = 3,
                shortcutAbsoluteTop = 400F,
                notificationDefaultTop = 20F,
                notificationStackAbsoluteBottom = 200F,
            )
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(
                    TransitionStep(
                        transitionState = TransitionState.FINISHED,
                        from = LOCKSCREEN,
                        to = GONE,
                    )
                ),
                testScope,
            )
            assertThat(bounds?.top).isEqualTo(20F)
        }


    @Test
    @DisableSceneContainer
    fun boundsChangeOnLockscreen_shouldSendBounds() =
        testScope.runTest {
            val bounds by collectLastValue(underTest.wallpaperFocalAreaBounds)
            kosmos.wallpaperFocalAreaRepository.setHasFocalArea(true)
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN)),
                listOf(TransitionStep(transitionState = TransitionState.STARTED, to = LOCKSCREEN)),
                testScope,
                testScope,
            )
            )
            setTestFocalAreaBounds()
            setTestFocalAreaBounds()


            assertThat(bounds).isEqualTo(null)
            kosmos.fakeKeyguardTransitionRepository.sendTransitionSteps(
                listOf(TransitionStep(transitionState = TransitionState.FINISHED, to = LOCKSCREEN)),
                testScope,
            )

            assertThat(bounds).isNotNull()
        }

    private fun setTestFocalAreaBounds(
        shadeLayoutWide: Boolean = false,
        activeNotifs: Int = 0,
        shortcutAbsoluteTop: Float = 400F,
        notificationDefaultTop: Float = 20F,
        notificationStackAbsoluteBottom: Float = 20F,
    ) {
        kosmos.shadeRepository.setShadeLayoutWide(shadeLayoutWide)
        kosmos.activeNotificationListRepository.setActiveNotifs(activeNotifs)
        kosmos.wallpaperFocalAreaRepository.setShortcutAbsoluteTop(shortcutAbsoluteTop)
        kosmos.wallpaperFocalAreaRepository.setNotificationDefaultTop(notificationDefaultTop)
        kosmos.wallpaperFocalAreaRepository.setNotificationStackAbsoluteBottom(
            notificationStackAbsoluteBottom
        )
    }
    }


    private fun setTestFocalAreaBounds() {
    companion object {
        kosmos.shadeRepository.setShadeLayoutWide(false)
        val DEFAULT_FOCAL_AREA_BOUNDS = RectF(0f, 400f, 1000F, 1900F)
        kosmos.activeNotificationListRepository.setActiveNotifs(0)
        kosmos.wallpaperFocalAreaRepository.setShortcutAbsoluteTop(400F)
        kosmos.wallpaperFocalAreaRepository.setNotificationDefaultTop(20F)
        kosmos.wallpaperFocalAreaRepository.setNotificationStackAbsoluteBottom(20F)
    }
    }
}
}
+6 −13
Original line number Original line Diff line number Diff line
@@ -362,20 +362,7 @@ object KeyguardRootViewBinder {
                            }
                            }
                        }
                        }
                    }
                    }
                }
            }

        burnInParams.update { current ->
            current.copy(
                translationX = { childViews[burnInLayerId]?.translationX },
                translationY = { childViews[burnInLayerId]?.translationY },
            )
        }


        disposables +=
            view.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    if (wallpaperFocalAreaViewModel.hasFocalArea.value) {
                    launch {
                    launch {
                        wallpaperFocalAreaViewModel.wallpaperFocalAreaBounds.collect {
                        wallpaperFocalAreaViewModel.wallpaperFocalAreaBounds.collect {
                            wallpaperFocalAreaViewModel.setFocalAreaBounds(it)
                            wallpaperFocalAreaViewModel.setFocalAreaBounds(it)
@@ -383,6 +370,12 @@ object KeyguardRootViewBinder {
                    }
                    }
                }
                }
            }
            }

        burnInParams.update { current ->
            current.copy(
                translationX = { childViews[burnInLayerId]?.translationX },
                translationY = { childViews[burnInLayerId]?.translationY },
            )
        }
        }


        disposables +=
        disposables +=
+7 −0
Original line number Original line Diff line number Diff line
@@ -180,6 +180,13 @@ constructor(
            .map {
            .map {
                val focalAreaTarget = context.resources.getString(SysUIR.string.focal_area_target)
                val focalAreaTarget = context.resources.getString(SysUIR.string.focal_area_target)
                val shouldSendNotificationLayout = it?.component?.className == focalAreaTarget
                val shouldSendNotificationLayout = it?.component?.className == focalAreaTarget
                if (DEBUG) {
                    Log.d(
                        TAG,
                        "shouldSendNotificationLayout:$shouldSendNotificationLayout " +
                            "wallpaperInfo:${it?.component?.className}",
                    )
                }
                shouldSendNotificationLayout
                shouldSendNotificationLayout
            }
            }
            .stateIn(
            .stateIn(
+33 −25
Original line number Original line Diff line number Diff line
@@ -24,10 +24,11 @@ import com.android.systemui.keyguard.shared.model.TransitionState
import com.android.systemui.scene.shared.model.Scenes
import com.android.systemui.scene.shared.model.Scenes
import com.android.systemui.wallpapers.domain.interactor.WallpaperFocalAreaInteractor
import com.android.systemui.wallpapers.domain.interactor.WallpaperFocalAreaInteractor
import javax.inject.Inject
import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map


class WallpaperFocalAreaViewModel
class WallpaperFocalAreaViewModel
@Inject
@Inject
@@ -37,32 +38,39 @@ constructor(
) {
) {
    val hasFocalArea = wallpaperFocalAreaInteractor.hasFocalArea
    val hasFocalArea = wallpaperFocalAreaInteractor.hasFocalArea


    @OptIn(ExperimentalCoroutinesApi::class)
    val wallpaperFocalAreaBounds =
    val wallpaperFocalAreaBounds =
        hasFocalArea.flatMapLatest { hasFocalArea ->
            if (hasFocalArea) {
                combine(
                combine(
                wallpaperFocalAreaInteractor.wallpaperFocalAreaBounds,
                        keyguardTransitionInteractor.startedKeyguardTransitionStep,
                        keyguardTransitionInteractor.startedKeyguardTransitionStep,
                // Emit transition state when FINISHED instead of STARTED to avoid race with
                        // Emit bounds when finishing transition to LOCKSCREEN to avoid race
                // wakingup command, causing layout change command not be received.
                        // condition with COMMAND_WAKING_UP
                        keyguardTransitionInteractor
                        keyguardTransitionInteractor
                            .transition(
                            .transition(
                                edge = Edge.create(to = Scenes.Lockscreen),
                                edge = Edge.create(to = Scenes.Lockscreen),
                        edgeWithoutSceneContainer = Edge.create(to = KeyguardState.LOCKSCREEN),
                                edgeWithoutSceneContainer =
                                    Edge.create(to = KeyguardState.LOCKSCREEN),
                            )
                            )
                            .filter { it.transitionState == TransitionState.FINISHED },
                            .filter { it.transitionState == TransitionState.FINISHED },
                ::Triple,
                        ::Pair,
                    )
                    )
            .map { (bounds, startedStep, _) ->
                    .flatMapLatest { (startedStep, _) ->
                // Avoid sending wrong bounds when transitioning from LOCKSCREEN to GONE
                        // Subscribe to bounds within the period of transitioning to the lockscreen,
                        // prior to any transitions away.
                        if (
                        if (
                            startedStep.to == KeyguardState.LOCKSCREEN &&
                            startedStep.to == KeyguardState.LOCKSCREEN &&
                                startedStep.from != KeyguardState.LOCKSCREEN
                                startedStep.from != KeyguardState.LOCKSCREEN
                        ) {
                        ) {
                    bounds
                            wallpaperFocalAreaInteractor.wallpaperFocalAreaBounds
                        } else {
                        } else {
                    null
                            emptyFlow()
                        }
                    }
            } else {
                emptyFlow()
            }
            }
        }
        }
            .filterNotNull()


    fun setFocalAreaBounds(bounds: RectF) {
    fun setFocalAreaBounds(bounds: RectF) {
        wallpaperFocalAreaInteractor.setFocalAreaBounds(bounds)
        wallpaperFocalAreaInteractor.setFocalAreaBounds(bounds)
+4 −0
Original line number Original line Diff line number Diff line
@@ -62,4 +62,8 @@ class FakeWallpaperFocalAreaRepository : WallpaperFocalAreaRepository {
    override fun setTapPosition(tapPosition: PointF) {
    override fun setTapPosition(tapPosition: PointF) {
        _wallpaperFocalAreaTapPosition.value = tapPosition
        _wallpaperFocalAreaTapPosition.value = tapPosition
    }
    }

    fun setHasFocalArea(hasFocalArea: Boolean) {
        _hasFocalArea.value = hasFocalArea
    }
}
}