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

Commit 60fa7d9a authored by Andreas Miko's avatar Andreas Miko
Browse files

Consolidate finishedKeyguardStep and make it a StateFlow

Reroute the usages of finishedKeyguardStep to the flexiglass-aware
variants like isFinishedIn. This makes sure that future devs don't
missuse this API.

There is one unintended change in this refactor, where I have noticed
that for all isFinished... methods we explicitly call
distinctUntilChanged() while finishedKeyguardStep is not that. We should
be consistent here instead across the API. In this case we also want to
move to StateFlows which are always distinctUntilChanged which means
that I have now made it consistent in being distinct. If there is
actually any feature that relies on that we would need to keep it a
sharedFlow and remove the distinct call on all methods instead.

Bug: b/330311871
Flag: EXEMPT refactor
Test: REFACTOR_ONLY
Change-Id: I07be68ab6b0ce07c82c006e69ea0ddbcd076fb07
parent ec2b7356
Loading
Loading
Loading
Loading
+24 −7
Original line number Diff line number Diff line
@@ -39,12 +39,15 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.keyguard.data.repository.FakeKeyguardTransitionRepository
import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
import com.android.systemui.keyguard.data.repository.fakeKeyguardTransitionRepository
import com.android.systemui.keyguard.data.repository.keyguardOcclusionRepository
import com.android.systemui.keyguard.shared.model.BiometricUnlockMode
import com.android.systemui.keyguard.shared.model.KeyguardState
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.TransitionState
import com.android.systemui.keyguard.shared.model.TransitionStep
import com.android.systemui.keyguard.util.KeyguardTransitionRepositorySpySubject.Companion.assertThat
@@ -53,6 +56,7 @@ import com.android.systemui.power.domain.interactor.PowerInteractor
import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAsleepForTest
import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAwakeForTest
import com.android.systemui.power.domain.interactor.powerInteractor
import com.android.systemui.scene.shared.model.Scenes
import com.android.systemui.statusbar.domain.interactor.keyguardOcclusionInteractor
import com.android.systemui.testKosmos
import junit.framework.Assert.assertEquals
@@ -166,6 +170,10 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
    @EnableFlags(Flags.FLAG_KEYGUARD_WM_STATE_REFACTOR)
    fun testTransitionToGone_onWakeUp_ifPowerButtonGestureDetected_fromGone() =
        testScope.runTest {
            val isGone by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Gone, GONE)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
@@ -175,7 +183,7 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
            runCurrent()

            // Make sure we're GONE.
            assertEquals(KeyguardState.GONE, kosmos.keyguardTransitionInteractor.getFinishedState())
            assertEquals(true, isGone)

            // Get part way to AOD.
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
@@ -204,6 +212,10 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
    @EnableFlags(Flags.FLAG_KEYGUARD_WM_STATE_REFACTOR)
    fun testTransitionToOccluded_onWakeUp_ifPowerButtonGestureDetectedAfterFinishedInAod_fromGone() =
        testScope.runTest {
            val isGone by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Gone, GONE)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
@@ -213,7 +225,7 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
            runCurrent()

            // Make sure we're GONE.
            assertEquals(KeyguardState.GONE, kosmos.keyguardTransitionInteractor.getFinishedState())
            assertEquals(true, isGone)

            // Get all the way to AOD
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
@@ -239,6 +251,10 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
    @EnableFlags(Flags.FLAG_KEYGUARD_WM_STATE_REFACTOR)
    fun testTransitionToOccluded_onWakeUp_ifPowerButtonGestureDetected_fromLockscreen() =
        testScope.runTest {
            val isLockscreen by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Lockscreen, LOCKSCREEN)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
@@ -248,10 +264,7 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
            runCurrent()

            // Make sure we're in LOCKSCREEN.
            assertEquals(
                KeyguardState.LOCKSCREEN,
                kosmos.keyguardTransitionInteractor.getFinishedState()
            )
            assertEquals(true, isLockscreen)

            // Get part way to AOD.
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
@@ -327,6 +340,10 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
    @DisableFlags(Flags.FLAG_KEYGUARD_WM_STATE_REFACTOR)
    fun testTransitionToGone_onWakeUp_ifPowerButtonGestureDetectedInAod_fromGone() =
        testScope.runTest {
            val isGone by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Gone, GONE)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
@@ -336,7 +353,7 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
            runCurrent()

            // Make sure we're GONE.
            assertEquals(KeyguardState.GONE, kosmos.keyguardTransitionInteractor.getFinishedState())
            assertEquals(true, isGone)

            // Start going to AOD on first button push
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
+19 −6
Original line number Diff line number Diff line
@@ -48,12 +48,15 @@ import com.android.systemui.communal.data.repository.fakeCommunalSceneRepository
import com.android.systemui.communal.domain.interactor.setCommunalAvailable
import com.android.systemui.communal.shared.model.CommunalScenes
import com.android.systemui.communal.shared.model.CommunalTransitionKeys
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.keyguard.data.repository.FakeKeyguardTransitionRepository
import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
import com.android.systemui.keyguard.data.repository.fakeKeyguardTransitionRepository
import com.android.systemui.keyguard.data.repository.keyguardOcclusionRepository
import com.android.systemui.keyguard.shared.model.BiometricUnlockMode
import com.android.systemui.keyguard.shared.model.KeyguardState
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.TransitionState
import com.android.systemui.keyguard.util.KeyguardTransitionRepositorySpySubject.Companion.assertThat
import com.android.systemui.kosmos.applicationCoroutineScope
@@ -62,6 +65,7 @@ import com.android.systemui.power.domain.interactor.PowerInteractor
import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAsleepForTest
import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAwakeForTest
import com.android.systemui.power.domain.interactor.powerInteractor
import com.android.systemui.scene.shared.model.Scenes
import com.android.systemui.testKosmos
import junit.framework.Assert.assertEquals
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -316,6 +320,10 @@ class FromDozingTransitionInteractorTest(flags: FlagsParameterization?) : SysuiT
    @EnableFlags(FLAG_KEYGUARD_WM_STATE_REFACTOR)
    fun testTransitionToGone_onWakeUp_ifPowerButtonGestureDetected_fromGone() =
        testScope.runTest {
            val isGone by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Gone, GONE)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.DOZING,
@@ -325,7 +333,7 @@ class FromDozingTransitionInteractorTest(flags: FlagsParameterization?) : SysuiT
            runCurrent()

            // Make sure we're GONE.
            assertEquals(KeyguardState.GONE, kosmos.keyguardTransitionInteractor.getFinishedState())
            assertEquals(true, isGone)

            // Get part way to AOD.
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
@@ -355,6 +363,10 @@ class FromDozingTransitionInteractorTest(flags: FlagsParameterization?) : SysuiT
    @Suppress("ktlint:standard:max-line-length")
    fun testTransitionToOccluded_onWakeUp_ifPowerButtonGestureDetectedAfterFinishedInAod_fromGone() =
        testScope.runTest {
            val isGone by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Gone, GONE)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.DOZING,
@@ -364,7 +376,7 @@ class FromDozingTransitionInteractorTest(flags: FlagsParameterization?) : SysuiT
            runCurrent()

            // Make sure we're GONE.
            assertEquals(KeyguardState.GONE, kosmos.keyguardTransitionInteractor.getFinishedState())
            assertEquals(true, isGone)

            // Get all the way to AOD
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
@@ -390,6 +402,10 @@ class FromDozingTransitionInteractorTest(flags: FlagsParameterization?) : SysuiT
    @EnableFlags(FLAG_KEYGUARD_WM_STATE_REFACTOR)
    fun testTransitionToOccluded_onWakeUp_ifPowerButtonGestureDetected_fromLockscreen() =
        testScope.runTest {
            val isLockscreen by
                collectLastValue(
                    kosmos.keyguardTransitionInteractor.isFinishedIn(Scenes.Lockscreen, LOCKSCREEN)
                )
            powerInteractor.setAwakeForTest()
            transitionRepository.sendTransitionSteps(
                from = KeyguardState.DOZING,
@@ -399,10 +415,7 @@ class FromDozingTransitionInteractorTest(flags: FlagsParameterization?) : SysuiT
            runCurrent()

            // Make sure we're in LOCKSCREEN.
            assertEquals(
                KeyguardState.LOCKSCREEN,
                kosmos.keyguardTransitionInteractor.getFinishedState()
            )
            assertEquals(true, isLockscreen)

            // Get part way to AOD.
            powerInteractor.onStartedGoingToSleep(PowerManager.GO_TO_SLEEP_REASON_MIN)
+0 −112
Original line number Diff line number Diff line
@@ -89,29 +89,6 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
            assertThat(lockscreenToAodSteps).isEqualTo(steps.subList(5, 8))
        }

    @Test
    fun finishedKeyguardStateTests() =
        testScope.runTest {
            val finishedSteps by collectValues(underTest.finishedKeyguardState)
            runCurrent()
            val steps = mutableListOf<TransitionStep>()

            steps.add(TransitionStep(AOD, PRIMARY_BOUNCER, 0f, STARTED))
            steps.add(TransitionStep(AOD, PRIMARY_BOUNCER, 0.5f, RUNNING))
            steps.add(TransitionStep(AOD, PRIMARY_BOUNCER, 1f, FINISHED))
            steps.add(TransitionStep(PRIMARY_BOUNCER, AOD, 0f, STARTED))
            steps.add(TransitionStep(PRIMARY_BOUNCER, AOD, 0.9f, RUNNING))
            steps.add(TransitionStep(PRIMARY_BOUNCER, AOD, 1f, FINISHED))
            steps.add(TransitionStep(AOD, GONE, 1f, STARTED))

            steps.forEach {
                repository.sendTransitionStep(it)
                runCurrent()
            }

            assertThat(finishedSteps).isEqualTo(listOf(LOCKSCREEN, PRIMARY_BOUNCER, AOD))
        }

    @Test
    fun startedKeyguardTransitionStepTests() =
        testScope.runTest {
@@ -1182,95 +1159,6 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
                )
        }

    @Test
    fun finishedKeyguardState_emitsAgainIfCancelledAndReversed() =
        testScope.runTest {
            val finishedStates by collectValues(underTest.finishedKeyguardState)

            // We default FINISHED in LOCKSCREEN.
            assertEquals(listOf(LOCKSCREEN), finishedStates)

            sendSteps(
                TransitionStep(LOCKSCREEN, AOD, 0f, STARTED),
                TransitionStep(LOCKSCREEN, AOD, 0.5f, RUNNING),
                TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED),
            )

            // We're FINISHED in AOD.
            assertEquals(
                listOf(
                    LOCKSCREEN,
                    AOD,
                ),
                finishedStates
            )

            // Transition back to LOCKSCREEN.
            sendSteps(
                TransitionStep(AOD, LOCKSCREEN, 0f, STARTED),
                TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING),
                TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED),
            )

            // We're FINISHED in LOCKSCREEN.
            assertEquals(
                listOf(
                    LOCKSCREEN,
                    AOD,
                    LOCKSCREEN,
                ),
                finishedStates
            )

            sendSteps(
                TransitionStep(LOCKSCREEN, GONE, 0f, STARTED),
                TransitionStep(LOCKSCREEN, GONE, 0.5f, RUNNING),
            )

            // We've STARTED a transition to GONE but not yet finished it so we're still FINISHED in
            // LOCKSCREEN.
            assertEquals(
                listOf(
                    LOCKSCREEN,
                    AOD,
                    LOCKSCREEN,
                ),
                finishedStates
            )

            sendSteps(
                TransitionStep(LOCKSCREEN, GONE, 0.6f, CANCELED),
            )

            // We've CANCELED a transition to GONE, we're still FINISHED in LOCKSCREEN.
            assertEquals(
                listOf(
                    LOCKSCREEN,
                    AOD,
                    LOCKSCREEN,
                ),
                finishedStates
            )

            sendSteps(
                TransitionStep(GONE, LOCKSCREEN, 0.6f, STARTED),
                TransitionStep(GONE, LOCKSCREEN, 0.9f, RUNNING),
                TransitionStep(GONE, LOCKSCREEN, 1f, FINISHED),
            )

            // Expect another emission of LOCKSCREEN, as we have FINISHED a second transition to
            // LOCKSCREEN after the cancellation.
            assertEquals(
                listOf(
                    LOCKSCREEN,
                    AOD,
                    LOCKSCREEN,
                    LOCKSCREEN,
                ),
                finishedStates
            )
        }

    @Test
    fun testCurrentState() =
        testScope.runTest {
+3 −7
Original line number Diff line number Diff line
@@ -134,16 +134,12 @@ constructor(
                .filterRelevantKeyguardState()
                .sampleCombine(
                    internalTransitionInteractor.currentTransitionInfoInternal,
                    transitionInteractor.finishedKeyguardState,
                    transitionInteractor.isFinishedIn(KeyguardState.LOCKSCREEN),
                    keyguardInteractor.isActiveDreamLockscreenHosted,
                )
                .collect {
                    (
                        isAbleToDream,
                        transitionInfo,
                        finishedKeyguardState,
                        isActiveDreamLockscreenHosted) ->
                    val isOnLockscreen = finishedKeyguardState == KeyguardState.LOCKSCREEN
                    (isAbleToDream, transitionInfo, isOnLockscreen, isActiveDreamLockscreenHosted)
                    ->
                    val isTransitionInterruptible =
                        transitionInfo.to == KeyguardState.LOCKSCREEN &&
                            !invalidFromStates.contains(transitionInfo.from)
+1 −4
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
@@ -74,9 +73,7 @@ constructor(
    val isLongPressHandlingEnabled: StateFlow<Boolean> =
        if (isFeatureEnabled()) {
                combine(
                    transitionInteractor.finishedKeyguardState.map {
                        it == KeyguardState.LOCKSCREEN
                    },
                    transitionInteractor.isFinishedIn(KeyguardState.LOCKSCREEN),
                    repository.isQuickSettingsVisible,
                ) { isFullyTransitionedToLockScreen, isQuickSettingsVisible ->
                    isFullyTransitionedToLockScreen && !isQuickSettingsVisible
Loading