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

Commit 906de616 authored by Josh Tsuji's avatar Josh Tsuji Committed by Android (Google) Code Review
Browse files

Merge "Listen to wakefulness instead of DozeMachine for AOD -> isAwake(*)." into main

parents ab57e5b3 860922f3
Loading
Loading
Loading
Loading
+65 −73
Original line number Diff line number Diff line
@@ -24,13 +24,10 @@ import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.keyguard.KeyguardWmStateRefactor
import com.android.systemui.keyguard.data.repository.KeyguardTransitionRepository
import com.android.systemui.keyguard.shared.model.BiometricUnlockModel.Companion.isWakeAndUnlock
import com.android.systemui.keyguard.shared.model.DozeStateModel
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.keyguard.shared.model.TransitionModeOnCanceled
import com.android.systemui.keyguard.shared.model.TransitionStep
import com.android.systemui.power.domain.interactor.PowerInteractor
import com.android.systemui.util.kotlin.Utils.Companion.sample
import java.util.UUID
import javax.inject.Inject
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.CoroutineDispatcher
@@ -72,76 +69,70 @@ constructor(
     * Listen for the signal that we're waking up and figure what state we need to transition to.
     */
    private fun listenForAodToAwake() {
        val transitionToLockscreen: suspend (TransitionStep) -> UUID? =
            { startedStep: TransitionStep ->
                val modeOnCanceled =
                    if (startedStep.from == KeyguardState.LOCKSCREEN) {
                        TransitionModeOnCanceled.REVERSE
                    } else if (startedStep.from == KeyguardState.GONE) {
                        TransitionModeOnCanceled.RESET
                    } else {
                        TransitionModeOnCanceled.LAST_VALUE
                    }
                startTransitionTo(
                    toState = KeyguardState.LOCKSCREEN,
                    modeOnCanceled = modeOnCanceled,
                )
            }

        if (KeyguardWmStateRefactor.isEnabled) {
            // The refactor uses PowerInteractor's wakefulness, which is the earliest wake signal
            // available. We have all of the information we need at this time to make a decision
            // about where to transition.
        // Use PowerInteractor's wakefulness, which is the earliest wake signal available. We
        // have all of the information we need at this time to make a decision about where to
        // transition.
        scope.launch {
            powerInteractor.detailedWakefulness
                    // React only to wake events.
                    .filterRelevantKeyguardStateAnd { it.isAwake() }
                .filterRelevantKeyguardStateAnd { wakefulness -> wakefulness.isAwake() }
                .sample(
                    startedKeyguardTransitionStep,
                    keyguardInteractor.biometricUnlockState,
                    keyguardInteractor.primaryBouncerShowing,
                    )
                    // Make sure we've at least STARTED a transition to AOD.
                    .collect { (_, startedStep, biometricUnlockState, primaryBouncerShowing) ->
                        // Check with the superclass to see if an occlusion transition is needed.
                        // Also, don't react to wake and unlock events, as we'll be receiving a call
                        // to #dismissAod() shortly when the authentication completes.
                        if (
                            !maybeStartTransitionToOccludedOrInsecureCamera() &&
                                !isWakeAndUnlock(biometricUnlockState) &&
                                !primaryBouncerShowing
                        ) {
                            transitionToLockscreen(startedStep)
                        }
                    }
            }
        } else {
            scope.launch {
                keyguardInteractor
                    .dozeTransitionTo(DozeStateModel.FINISH)
                    .filterRelevantKeyguardState()
                    .sample(
                    keyguardInteractor.isKeyguardShowing,
                        startedKeyguardTransitionStep,
                    keyguardInteractor.isKeyguardOccluded,
                        keyguardInteractor.biometricUnlockState,
                        keyguardInteractor.primaryBouncerShowing,
                    keyguardInteractor.isKeyguardDismissible,
                )
                .collect {
                    (
                        _,
                            isKeyguardShowing,
                            lastStartedStep,
                            occluded,
                        startedStep,
                        biometricUnlockState,
                            primaryBouncerShowing) ->
                        if (
                            !occluded &&
                        primaryBouncerShowing,
                        _,
                        isKeyguardOccludedLegacy,
                        _) ->
                    if (!maybeHandleInsecurePowerGesture()) {
                        val shouldTransitionToLockscreen =
                            if (KeyguardWmStateRefactor.isEnabled) {
                                // Check with the superclass to see if an occlusion transition is
                                // needed. Also, don't react to wake and unlock events, as we'll be
                                // receiving a call to #dismissAod() shortly when the authentication
                                // completes.
                                !maybeStartTransitionToOccludedOrInsecureCamera() &&
                                    !isWakeAndUnlock(biometricUnlockState) &&
                                isKeyguardShowing &&
                                    !primaryBouncerShowing
                        ) {
                            transitionToLockscreen(lastStartedStep)
                            } else {
                                !isKeyguardOccludedLegacy &&
                                    !isWakeAndUnlock(biometricUnlockState) &&
                                    !primaryBouncerShowing
                            }

                        // With the refactor enabled, maybeStartTransitionToOccludedOrInsecureCamera
                        // handles transitioning to OCCLUDED.
                        val shouldTransitionToOccluded =
                            !KeyguardWmStateRefactor.isEnabled && isKeyguardOccludedLegacy

                        if (shouldTransitionToLockscreen) {
                            val modeOnCanceled =
                                if (startedStep.from == KeyguardState.LOCKSCREEN) {
                                    TransitionModeOnCanceled.REVERSE
                                } else if (startedStep.from == KeyguardState.GONE) {
                                    TransitionModeOnCanceled.RESET
                                } else {
                                    TransitionModeOnCanceled.LAST_VALUE
                                }

                            startTransitionTo(
                                toState = KeyguardState.LOCKSCREEN,
                                modeOnCanceled = modeOnCanceled,
                                ownerReason = "listen for aod to awake"
                            )
                        } else if (shouldTransitionToOccluded) {
                            startTransitionTo(
                                toState = KeyguardState.OCCLUDED,
                                ownerReason = "waking up and isOccluded=true",
                            )
                        }
                    }
                }
@@ -165,7 +156,8 @@ constructor(
                .collect {
                    startTransitionTo(
                        toState = KeyguardState.OCCLUDED,
                        modeOnCanceled = TransitionModeOnCanceled.RESET
                        modeOnCanceled = TransitionModeOnCanceled.RESET,
                        ownerReason = "isOccluded = true",
                    )
                }
        }
+45 −18
Original line number Diff line number Diff line
@@ -120,25 +120,15 @@ sealed class TransitionInteractor(
     * Returns true if a transition was started, false otherwise.
     */
    suspend fun maybeStartTransitionToOccludedOrInsecureCamera(): Boolean {
        // The refactor is required for the occlusion interactor to work.
        KeyguardWmStateRefactor.isUnexpectedlyInLegacyMode()

        // Check if we should start a transition from the power gesture.
        if (keyguardOcclusionInteractor.shouldTransitionFromPowerButtonGesture()) {
            if (transitionInteractor.getCurrentState() == KeyguardState.GONE) {
                // If the current state is GONE when the launch gesture is triggered, it means we
                // were in transition from GONE -> DOZING/AOD due to the first power button tap. The
                // second tap indicates that the user's intent was actually to launch the unlocked
                // (insecure) camera, so we should transition back to GONE.
                startTransitionTo(
                    KeyguardState.GONE,
                    ownerReason = "Power button gesture while GONE"
                )
            } else if (keyguardOcclusionInteractor.occludingActivityWillDismissKeyguard.value) {
                // The double tap gesture occurred while not GONE (AOD/LOCKSCREEN/etc.), but the
                // keyguard is dismissable. The activity launch will dismiss the keyguard, so we
                // should transition to GONE.
                startTransitionTo(
                    KeyguardState.GONE,
                    ownerReason = "Power button gesture on dismissable keyguard"
                )
            } else {
            // See if we handled the insecure power gesture. If not, then we'll be launching the
            // secure camera. Once KeyguardWmStateRefactor is fully enabled, we can clean up this
            // code path by pulling maybeHandleInsecurePowerGesture() into this conditional.
            if (!maybeHandleInsecurePowerGesture()) {
                // Otherwise, the double tap gesture occurred while not GONE and not dismissable,
                // which means we will launch the secure camera, which OCCLUDES the keyguard.
                startTransitionTo(
@@ -164,6 +154,43 @@ sealed class TransitionInteractor(
        }
    }

    /**
     * Transition to [KeyguardState.GONE] for the insecure power button launch gesture, if the
     * conditions to do so are met.
     *
     * Called from [FromAodTransitionInteractor] if [KeyguardWmStateRefactor] is not enabled, or
     * [maybeStartTransitionToOccludedOrInsecureCamera] if it's enabled.
     */
    @Deprecated("Will be merged into maybeStartTransitionToOccludedOrInsecureCamera")
    suspend fun maybeHandleInsecurePowerGesture(): Boolean {
        if (keyguardOcclusionInteractor.shouldTransitionFromPowerButtonGesture()) {
            if (transitionInteractor.getCurrentState() == KeyguardState.GONE) {
                // If the current state is GONE when the launch gesture is triggered, it means we
                // were in transition from GONE -> DOZING/AOD due to the first power button tap. The
                // second tap indicates that the user's intent was actually to launch the unlocked
                // (insecure) camera, so we should transition back to GONE.
                startTransitionTo(
                    KeyguardState.GONE,
                    ownerReason = "Power button gesture while GONE"
                )

                return true
            } else if (keyguardOcclusionInteractor.occludingActivityWillDismissKeyguard.value) {
                // The double tap gesture occurred while not GONE (AOD/LOCKSCREEN/etc.), but the
                // keyguard is dismissable. The activity launch will dismiss the keyguard, so we
                // should transition to GONE.
                startTransitionTo(
                    KeyguardState.GONE,
                    ownerReason = "Power button gesture on dismissable keyguard"
                )

                return true
            }
        }

        return false
    }

    /**
     * Transition to the appropriate state when the device goes to sleep while in [from].
     *
+12 −0
Original line number Diff line number Diff line
@@ -288,4 +288,16 @@ class FromAodTransitionInteractorTest : SysuiTestCase() {
            assertThat(transitionRepository)
                .startedTransition(from = KeyguardState.AOD, to = KeyguardState.GONE)
        }

    @Test
    fun testTransitionToOccluded_onWake() =
        testScope.runTest {
            kosmos.fakeKeyguardRepository.setKeyguardOccluded(true)
            powerInteractor.setAwakeForTest()
            runCurrent()

            // Waking up from AOD while occluded should transition to OCCLUDED.
            assertThat(transitionRepository)
                .startedTransition(from = KeyguardState.AOD, to = KeyguardState.OCCLUDED)
        }
}