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

Commit 1129df1b authored by Beverly's avatar Beverly
Browse files

Only restart the sleep state if SysUI was dozing

Screensaver (other non-sysi dreams) are handled by other apps, so
if SysUI crashes then SysUI isn't responsible for starting back up those
dreams. SysUI is only responsible for restarting doze/AOD.

Test: atest RestartDozeListenerTest
Test: manually crash systemui (adb shell am crash com.android.systemui)
from: awake, doze, aod (if possible), and screensaver and observe
that after a few seconds from the restart, the device is in the same
awake/doze/aod/screensaver state
Fixes: 273963292

Change-Id: I7f19831ea04d97fea3e64d43dbefcd517ba3d2a5
parent c82fa1ff
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -39,15 +39,15 @@ constructor(
) {

    companion object {
        @VisibleForTesting val RESTART_NAP_KEY = "restart_nap_after_start"
        @VisibleForTesting val RESTART_SLEEP_KEY = "restart_nap_after_start"
    }

    private var inited = false

    val listener =
        object : StatusBarStateController.StateListener {
            override fun onDreamingChanged(isDreaming: Boolean) {
                storeSleepState(isDreaming)
            override fun onDozingChanged(isDozing: Boolean) {
                storeSleepState(isDozing)
            }
        }

@@ -67,9 +67,13 @@ constructor(
    fun maybeRestartSleep() {
        bgExecutor.executeDelayed(
            {
                if (settings.getBool(RESTART_NAP_KEY, false)) {
                if (settings.getBool(RESTART_SLEEP_KEY, false)) {
                    Log.d("RestartDozeListener", "Restarting sleep state")
                    powerManager.wakeUp(systemClock.uptimeMillis())
                    powerManager.wakeUp(
                        systemClock.uptimeMillis(),
                        PowerManager.WAKE_REASON_APPLICATION,
                        "RestartDozeListener"
                    )
                    powerManager.goToSleep(systemClock.uptimeMillis())
                }
            },
@@ -78,6 +82,6 @@ constructor(
    }

    private fun storeSleepState(sleeping: Boolean) {
        bgExecutor.execute { settings.putBool(RESTART_NAP_KEY, sleeping) }
        bgExecutor.execute { settings.putBool(RESTART_SLEEP_KEY, sleeping) }
    }
}
+16 −12
Original line number Diff line number Diff line
@@ -21,12 +21,15 @@ import android.test.suitebuilder.annotation.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.settings.FakeSettings
import com.android.systemui.util.time.FakeSystemClock
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito.anyLong
import org.mockito.Mockito.never
@@ -58,36 +61,37 @@ class RestartDozeListenerTest : SysuiTestCase() {
    }

    @Test
    fun testStoreDreamState_onDreamingStarted() {
        listener.onDreamingChanged(true)
    fun testStoreDreamState_onDozingStarted() {
        listener.onDozingChanged(true)
        executor.runAllReady()
        assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isTrue()
        assertThat(settings.getBool(RestartDozeListener.RESTART_SLEEP_KEY)).isTrue()
    }

    @Test
    fun testStoreDreamState_onDreamingStopped() {
        listener.onDreamingChanged(false)
    fun testStoreDozeState_onDozingStopped() {
        listener.onDozingChanged(false)
        executor.runAllReady()
        assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isFalse()
        assertThat(settings.getBool(RestartDozeListener.RESTART_SLEEP_KEY)).isFalse()
    }

    @Test
    fun testRestoreDreamState_dreamingShouldStart() {
        settings.putBool(RestartDozeListener.RESTART_NAP_KEY, true)
    fun testRestoreDozeState_dozingShouldStart() {
        settings.putBool(RestartDozeListener.RESTART_SLEEP_KEY, true)
        restartDozeListener.maybeRestartSleep()
        executor.advanceClockToLast()
        executor.runAllReady()
        verify(powerManager).wakeUp(clock.uptimeMillis())
        verify(powerManager)
            .wakeUp(eq(clock.uptimeMillis()), eq(PowerManager.WAKE_REASON_APPLICATION), anyString())
        verify(powerManager).goToSleep(clock.uptimeMillis())
    }

    @Test
    fun testRestoreDreamState_dreamingShouldNot() {
        settings.putBool(RestartDozeListener.RESTART_NAP_KEY, false)
    fun testRestoreDozeState_dozingShouldNotStart() {
        settings.putBool(RestartDozeListener.RESTART_SLEEP_KEY, false)
        restartDozeListener.maybeRestartSleep()
        executor.advanceClockToLast()
        executor.runAllReady()
        verify(powerManager, never()).wakeUp(anyLong())
        verify(powerManager, never()).wakeUp(anyLong(), anyInt(), anyString())
        verify(powerManager, never()).goToSleep(anyLong())
    }
}