Loading packages/SystemUI/src/com/android/systemui/flags/RestartDozeListener.kt +10 −6 Original line number Original line Diff line number Diff line Loading @@ -39,15 +39,15 @@ constructor( ) { ) { companion object { companion object { @VisibleForTesting val RESTART_NAP_KEY = "restart_nap_after_start" @VisibleForTesting val RESTART_SLEEP_KEY = "restart_nap_after_start" } } private var inited = false private var inited = false val listener = val listener = object : StatusBarStateController.StateListener { object : StatusBarStateController.StateListener { override fun onDreamingChanged(isDreaming: Boolean) { override fun onDozingChanged(isDozing: Boolean) { storeSleepState(isDreaming) storeSleepState(isDozing) } } } } Loading @@ -67,9 +67,13 @@ constructor( fun maybeRestartSleep() { fun maybeRestartSleep() { bgExecutor.executeDelayed( bgExecutor.executeDelayed( { { if (settings.getBool(RESTART_NAP_KEY, false)) { if (settings.getBool(RESTART_SLEEP_KEY, false)) { Log.d("RestartDozeListener", "Restarting sleep state") Log.d("RestartDozeListener", "Restarting sleep state") powerManager.wakeUp(systemClock.uptimeMillis()) powerManager.wakeUp( systemClock.uptimeMillis(), PowerManager.WAKE_REASON_APPLICATION, "RestartDozeListener" ) powerManager.goToSleep(systemClock.uptimeMillis()) powerManager.goToSleep(systemClock.uptimeMillis()) } } }, }, Loading @@ -78,6 +82,6 @@ constructor( } } private fun storeSleepState(sleeping: Boolean) { private fun storeSleepState(sleeping: Boolean) { bgExecutor.execute { settings.putBool(RESTART_NAP_KEY, sleeping) } bgExecutor.execute { settings.putBool(RESTART_SLEEP_KEY, sleeping) } } } } } packages/SystemUI/tests/src/com/android/systemui/flags/RestartDozeListenerTest.kt +16 −12 Original line number Original line Diff line number Diff line Loading @@ -21,12 +21,15 @@ import android.test.suitebuilder.annotation.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.SysuiTestCase import com.android.systemui.plugins.statusbar.StatusBarStateController import com.android.systemui.plugins.statusbar.StatusBarStateController import com.android.systemui.util.concurrency.FakeExecutor 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.settings.FakeSettings import com.android.systemui.util.time.FakeSystemClock import com.android.systemui.util.time.FakeSystemClock import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat import org.junit.Before import org.junit.Before import org.junit.Test import org.junit.Test import org.mockito.ArgumentCaptor import org.mockito.ArgumentCaptor import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.Mock import org.mockito.Mockito.anyLong import org.mockito.Mockito.anyLong import org.mockito.Mockito.never import org.mockito.Mockito.never Loading Loading @@ -58,36 +61,37 @@ class RestartDozeListenerTest : SysuiTestCase() { } } @Test @Test fun testStoreDreamState_onDreamingStarted() { fun testStoreDreamState_onDozingStarted() { listener.onDreamingChanged(true) listener.onDozingChanged(true) executor.runAllReady() executor.runAllReady() assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isTrue() assertThat(settings.getBool(RestartDozeListener.RESTART_SLEEP_KEY)).isTrue() } } @Test @Test fun testStoreDreamState_onDreamingStopped() { fun testStoreDozeState_onDozingStopped() { listener.onDreamingChanged(false) listener.onDozingChanged(false) executor.runAllReady() executor.runAllReady() assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isFalse() assertThat(settings.getBool(RestartDozeListener.RESTART_SLEEP_KEY)).isFalse() } } @Test @Test fun testRestoreDreamState_dreamingShouldStart() { fun testRestoreDozeState_dozingShouldStart() { settings.putBool(RestartDozeListener.RESTART_NAP_KEY, true) settings.putBool(RestartDozeListener.RESTART_SLEEP_KEY, true) restartDozeListener.maybeRestartSleep() restartDozeListener.maybeRestartSleep() executor.advanceClockToLast() executor.advanceClockToLast() executor.runAllReady() executor.runAllReady() verify(powerManager).wakeUp(clock.uptimeMillis()) verify(powerManager) .wakeUp(eq(clock.uptimeMillis()), eq(PowerManager.WAKE_REASON_APPLICATION), anyString()) verify(powerManager).goToSleep(clock.uptimeMillis()) verify(powerManager).goToSleep(clock.uptimeMillis()) } } @Test @Test fun testRestoreDreamState_dreamingShouldNot() { fun testRestoreDozeState_dozingShouldNotStart() { settings.putBool(RestartDozeListener.RESTART_NAP_KEY, false) settings.putBool(RestartDozeListener.RESTART_SLEEP_KEY, false) restartDozeListener.maybeRestartSleep() restartDozeListener.maybeRestartSleep() executor.advanceClockToLast() executor.advanceClockToLast() executor.runAllReady() executor.runAllReady() verify(powerManager, never()).wakeUp(anyLong()) verify(powerManager, never()).wakeUp(anyLong(), anyInt(), anyString()) verify(powerManager, never()).goToSleep(anyLong()) verify(powerManager, never()).goToSleep(anyLong()) } } } } Loading
packages/SystemUI/src/com/android/systemui/flags/RestartDozeListener.kt +10 −6 Original line number Original line Diff line number Diff line Loading @@ -39,15 +39,15 @@ constructor( ) { ) { companion object { companion object { @VisibleForTesting val RESTART_NAP_KEY = "restart_nap_after_start" @VisibleForTesting val RESTART_SLEEP_KEY = "restart_nap_after_start" } } private var inited = false private var inited = false val listener = val listener = object : StatusBarStateController.StateListener { object : StatusBarStateController.StateListener { override fun onDreamingChanged(isDreaming: Boolean) { override fun onDozingChanged(isDozing: Boolean) { storeSleepState(isDreaming) storeSleepState(isDozing) } } } } Loading @@ -67,9 +67,13 @@ constructor( fun maybeRestartSleep() { fun maybeRestartSleep() { bgExecutor.executeDelayed( bgExecutor.executeDelayed( { { if (settings.getBool(RESTART_NAP_KEY, false)) { if (settings.getBool(RESTART_SLEEP_KEY, false)) { Log.d("RestartDozeListener", "Restarting sleep state") Log.d("RestartDozeListener", "Restarting sleep state") powerManager.wakeUp(systemClock.uptimeMillis()) powerManager.wakeUp( systemClock.uptimeMillis(), PowerManager.WAKE_REASON_APPLICATION, "RestartDozeListener" ) powerManager.goToSleep(systemClock.uptimeMillis()) powerManager.goToSleep(systemClock.uptimeMillis()) } } }, }, Loading @@ -78,6 +82,6 @@ constructor( } } private fun storeSleepState(sleeping: Boolean) { private fun storeSleepState(sleeping: Boolean) { bgExecutor.execute { settings.putBool(RESTART_NAP_KEY, sleeping) } bgExecutor.execute { settings.putBool(RESTART_SLEEP_KEY, sleeping) } } } } }
packages/SystemUI/tests/src/com/android/systemui/flags/RestartDozeListenerTest.kt +16 −12 Original line number Original line Diff line number Diff line Loading @@ -21,12 +21,15 @@ import android.test.suitebuilder.annotation.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.SysuiTestCase import com.android.systemui.plugins.statusbar.StatusBarStateController import com.android.systemui.plugins.statusbar.StatusBarStateController import com.android.systemui.util.concurrency.FakeExecutor 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.settings.FakeSettings import com.android.systemui.util.time.FakeSystemClock import com.android.systemui.util.time.FakeSystemClock import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat import org.junit.Before import org.junit.Before import org.junit.Test import org.junit.Test import org.mockito.ArgumentCaptor import org.mockito.ArgumentCaptor import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.Mock import org.mockito.Mockito.anyLong import org.mockito.Mockito.anyLong import org.mockito.Mockito.never import org.mockito.Mockito.never Loading Loading @@ -58,36 +61,37 @@ class RestartDozeListenerTest : SysuiTestCase() { } } @Test @Test fun testStoreDreamState_onDreamingStarted() { fun testStoreDreamState_onDozingStarted() { listener.onDreamingChanged(true) listener.onDozingChanged(true) executor.runAllReady() executor.runAllReady() assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isTrue() assertThat(settings.getBool(RestartDozeListener.RESTART_SLEEP_KEY)).isTrue() } } @Test @Test fun testStoreDreamState_onDreamingStopped() { fun testStoreDozeState_onDozingStopped() { listener.onDreamingChanged(false) listener.onDozingChanged(false) executor.runAllReady() executor.runAllReady() assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isFalse() assertThat(settings.getBool(RestartDozeListener.RESTART_SLEEP_KEY)).isFalse() } } @Test @Test fun testRestoreDreamState_dreamingShouldStart() { fun testRestoreDozeState_dozingShouldStart() { settings.putBool(RestartDozeListener.RESTART_NAP_KEY, true) settings.putBool(RestartDozeListener.RESTART_SLEEP_KEY, true) restartDozeListener.maybeRestartSleep() restartDozeListener.maybeRestartSleep() executor.advanceClockToLast() executor.advanceClockToLast() executor.runAllReady() executor.runAllReady() verify(powerManager).wakeUp(clock.uptimeMillis()) verify(powerManager) .wakeUp(eq(clock.uptimeMillis()), eq(PowerManager.WAKE_REASON_APPLICATION), anyString()) verify(powerManager).goToSleep(clock.uptimeMillis()) verify(powerManager).goToSleep(clock.uptimeMillis()) } } @Test @Test fun testRestoreDreamState_dreamingShouldNot() { fun testRestoreDozeState_dozingShouldNotStart() { settings.putBool(RestartDozeListener.RESTART_NAP_KEY, false) settings.putBool(RestartDozeListener.RESTART_SLEEP_KEY, false) restartDozeListener.maybeRestartSleep() restartDozeListener.maybeRestartSleep() executor.advanceClockToLast() executor.advanceClockToLast() executor.runAllReady() executor.runAllReady() verify(powerManager, never()).wakeUp(anyLong()) verify(powerManager, never()).wakeUp(anyLong(), anyInt(), anyString()) verify(powerManager, never()).goToSleep(anyLong()) verify(powerManager, never()).goToSleep(anyLong()) } } } }