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

Commit a5ed8c10 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

[flexiglass] Skip lockscreen when devices wakes.

...if the auth method is None.

Fix: 292502433
Test: manually verified that, with an auth method of None, turning on
the device skips the lockscreen (but the lockscreen _is_ shown when the
device display is asleep - to show AOD).
Test: manually verified that, with a secure auth method (tried pattern),
turning on the device does NOT skip the lockscreen.
Test: added unit tests cases to cover new functionality.

Change-Id: I5d80ff6dc52b4f0dc0e7adf16159df32858f8815
parent 1cf0f476
Loading
Loading
Loading
Loading
+20 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.systemui.scene.domain.startable

import com.android.systemui.CoreStartable
import com.android.systemui.authentication.domain.interactor.AuthenticationInteractor
import com.android.systemui.authentication.domain.model.AuthenticationMethodModel
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.DisplayId
@@ -135,24 +136,29 @@ constructor(

        applicationScope.launch {
            keyguardInteractor.wakefulnessModel
                .map { it.state == WakefulnessState.ASLEEP }
                .map { wakefulnessModel -> wakefulnessModel.state }
                .distinctUntilChanged()
                .collect { isAsleep ->
                    if (isAsleep) {
                        // When the device goes to sleep, reset the current scene.
                        val isUnlocked = authenticationInteractor.isUnlocked.value
                        val (targetSceneKey, loggingReason) =
                            if (isUnlocked) {
                                SceneKey.Gone to "device is asleep while unlocked"
                            } else {
                                SceneKey.Lockscreen to "device is asleep while locked"
                .collect { wakefulnessState ->
                    when (wakefulnessState) {
                        WakefulnessState.STARTING_TO_SLEEP -> {
                            switchToScene(
                                targetSceneKey = SceneKey.Lockscreen,
                                loggingReason = "device is asleep",
                            )
                        }
                        WakefulnessState.STARTING_TO_WAKE -> {
                            val authMethod = authenticationInteractor.getAuthenticationMethod()
                            if (authMethod == AuthenticationMethodModel.None) {
                                switchToScene(
                            targetSceneKey = targetSceneKey,
                            loggingReason = loggingReason,
                                    targetSceneKey = SceneKey.Gone,
                                    loggingReason =
                                        "device is starting to wake up while auth method is None",
                                )
                            }
                        }
                        else -> Unit
                    }
                }
        }
    }

+68 −37
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ package com.android.systemui.scene.domain.startable
import android.view.Display
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.data.model.AuthenticationMethodModel
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.flags.Flags
import com.android.systemui.keyguard.shared.model.WakeSleepReason
@@ -245,93 +246,110 @@ class SceneContainerStartableTest : SysuiTestCase() {
        }

    @Test
    fun switchToGoneWhenDeviceSleepsUnlocked_featureEnabled() =
    fun switchToLockscreenWhenDeviceSleepsLocked_featureEnabled() =
        testScope.runTest {
            val currentSceneKey by collectLastValue(sceneInteractor.currentScene.map { it.key })
            prepareState(
                isFeatureEnabled = true,
                isDeviceUnlocked = true,
                isDeviceUnlocked = false,
                initialSceneKey = SceneKey.Shade,
            )
            assertThat(currentSceneKey).isEqualTo(SceneKey.Shade)
            underTest.start()

            keyguardRepository.setWakefulnessModel(ASLEEP)
            keyguardRepository.setWakefulnessModel(STARTING_TO_SLEEP)

            assertThat(currentSceneKey).isEqualTo(SceneKey.Gone)
            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
        }

    @Test
    fun switchToGoneWhenDeviceSleepsUnlocked_featureDisabled() =
    fun switchToLockscreenWhenDeviceSleepsLocked_featureDisabled() =
        testScope.runTest {
            val currentSceneKey by collectLastValue(sceneInteractor.currentScene.map { it.key })
            prepareState(
                isFeatureEnabled = false,
                isDeviceUnlocked = true,
                isDeviceUnlocked = false,
                initialSceneKey = SceneKey.Shade,
            )
            assertThat(currentSceneKey).isEqualTo(SceneKey.Shade)
            underTest.start()

            keyguardRepository.setWakefulnessModel(ASLEEP)
            keyguardRepository.setWakefulnessModel(STARTING_TO_SLEEP)

            assertThat(currentSceneKey).isEqualTo(SceneKey.Shade)
        }

    @Test
    fun switchToLockscreenWhenDeviceSleepsLocked_featureEnabled() =
    fun hydrateSystemUiState() =
        testScope.runTest {
            underTest.start()
            runCurrent()
            clearInvocations(sysUiState)

            listOf(
                    SceneKey.Gone,
                    SceneKey.Lockscreen,
                    SceneKey.Bouncer,
                    SceneKey.Shade,
                    SceneKey.QuickSettings,
                )
                .forEachIndexed { index, sceneKey ->
                    sceneInteractor.setCurrentScene(SceneModel(sceneKey), "reason")
                    runCurrent()

                    verify(sysUiState, times(index + 1)).commitUpdate(Display.DEFAULT_DISPLAY)
                }
        }

    @Test
    fun switchToGoneWhenDeviceStartsToWakeUp_authMethodNone_featureEnabled() =
        testScope.runTest {
            val currentSceneKey by collectLastValue(sceneInteractor.currentScene.map { it.key })
            prepareState(
                isFeatureEnabled = true,
                isDeviceUnlocked = false,
                initialSceneKey = SceneKey.Shade,
                initialSceneKey = SceneKey.Lockscreen,
                authenticationMethod = AuthenticationMethodModel.None,
            )
            assertThat(currentSceneKey).isEqualTo(SceneKey.Shade)
            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
            underTest.start()

            keyguardRepository.setWakefulnessModel(ASLEEP)
            keyguardRepository.setWakefulnessModel(STARTING_TO_WAKE)

            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
            assertThat(currentSceneKey).isEqualTo(SceneKey.Gone)
        }

    @Test
    fun switchToLockscreenWhenDeviceSleepsLocked_featureDisabled() =
    fun switchToGoneWhenDeviceStartsToWakeUp_authMethodNotNone_featureEnabled() =
        testScope.runTest {
            val currentSceneKey by collectLastValue(sceneInteractor.currentScene.map { it.key })
            prepareState(
                isFeatureEnabled = false,
                isDeviceUnlocked = false,
                initialSceneKey = SceneKey.Shade,
                isFeatureEnabled = true,
                initialSceneKey = SceneKey.Lockscreen,
                authenticationMethod = AuthenticationMethodModel.Pin,
            )
            assertThat(currentSceneKey).isEqualTo(SceneKey.Shade)
            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
            underTest.start()

            keyguardRepository.setWakefulnessModel(ASLEEP)
            keyguardRepository.setWakefulnessModel(STARTING_TO_WAKE)

            assertThat(currentSceneKey).isEqualTo(SceneKey.Shade)
            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
        }

    @Test
    fun hydrateSystemUiState() =
    fun switchToGoneWhenDeviceStartsToWakeUp_authMethodNone_featureDisabled() =
        testScope.runTest {
            val currentSceneKey by collectLastValue(sceneInteractor.currentScene.map { it.key })
            prepareState(
                isFeatureEnabled = false,
                initialSceneKey = SceneKey.Lockscreen,
                authenticationMethod = AuthenticationMethodModel.None,
            )
            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
            underTest.start()
            runCurrent()
            clearInvocations(sysUiState)

            listOf(
                    SceneKey.Gone,
                    SceneKey.Lockscreen,
                    SceneKey.Bouncer,
                    SceneKey.Shade,
                    SceneKey.QuickSettings,
                )
                .forEachIndexed { index, sceneKey ->
                    sceneInteractor.setCurrentScene(SceneModel(sceneKey), "reason")
                    runCurrent()
            keyguardRepository.setWakefulnessModel(STARTING_TO_WAKE)

                    verify(sysUiState, times(index + 1)).commitUpdate(Display.DEFAULT_DISPLAY)
                }
            assertThat(currentSceneKey).isEqualTo(SceneKey.Lockscreen)
        }

    private fun prepareState(
@@ -339,17 +357,30 @@ class SceneContainerStartableTest : SysuiTestCase() {
        isDeviceUnlocked: Boolean = false,
        isBypassEnabled: Boolean = false,
        initialSceneKey: SceneKey? = null,
        authenticationMethod: AuthenticationMethodModel? = null,
    ) {
        featureFlags.set(Flags.SCENE_CONTAINER, isFeatureEnabled)
        authenticationRepository.setUnlocked(isDeviceUnlocked)
        keyguardRepository.setBypassEnabled(isBypassEnabled)
        initialSceneKey?.let { sceneInteractor.setCurrentScene(SceneModel(it), "reason") }
        authenticationMethod?.let {
            authenticationRepository.setAuthenticationMethod(authenticationMethod)
            authenticationRepository.setLockscreenEnabled(
                authenticationMethod != AuthenticationMethodModel.None
            )
        }
    }

    companion object {
        private val ASLEEP =
        private val STARTING_TO_SLEEP =
            WakefulnessModel(
                state = WakefulnessState.STARTING_TO_SLEEP,
                lastWakeReason = WakeSleepReason.POWER_BUTTON,
                lastSleepReason = WakeSleepReason.POWER_BUTTON
            )
        private val STARTING_TO_WAKE =
            WakefulnessModel(
                state = WakefulnessState.ASLEEP,
                state = WakefulnessState.STARTING_TO_WAKE,
                lastWakeReason = WakeSleepReason.POWER_BUTTON,
                lastSleepReason = WakeSleepReason.POWER_BUTTON
            )