From c969cfa0f7b2834ba2b4a90cedff9444c7e01b4d Mon Sep 17 00:00:00 2001 From: Dave Mankoff Date: Thu, 16 Mar 2023 22:02:56 +0000 Subject: Wait to restart Doze until after DreamMonitor starts. Also, don't set the stored value to doze to false when we are starting doze. Just leave it as true. It will get set to false if and when the phone leaves the doze state. Otherwise, if sysui dies in the moment between storing `false` and actually starting doze, it can get left in a bad state. Test: manually kill sysui a lot Fixes: 268060399 Change-Id: I5cc60b30fbfc3c86da2f36b0f2789bb0c434339b --- .../com/android/systemui/dreams/DreamMonitor.java | 10 +++++++-- .../systemui/flags/FeatureFlagsDebugStartable.kt | 6 ------ .../systemui/flags/FeatureFlagsReleaseStartable.kt | 12 +---------- .../android/systemui/flags/RestartDozeListener.kt | 25 ++++++++++++++++------ .../systemui/flags/RestartDozeListenerTest.kt | 10 ++++++++- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/dreams/DreamMonitor.java b/packages/SystemUI/src/com/android/systemui/dreams/DreamMonitor.java index 055cd52b23d6..7f567aa334a6 100644 --- a/packages/SystemUI/src/com/android/systemui/dreams/DreamMonitor.java +++ b/packages/SystemUI/src/com/android/systemui/dreams/DreamMonitor.java @@ -23,6 +23,7 @@ import android.util.Log; import com.android.systemui.CoreStartable; import com.android.systemui.dreams.callbacks.DreamStatusBarStateCallback; import com.android.systemui.dreams.conditions.DreamCondition; +import com.android.systemui.flags.RestartDozeListener; import com.android.systemui.shared.condition.Monitor; import com.android.systemui.util.condition.ConditionalCoreStartable; @@ -39,17 +40,19 @@ public class DreamMonitor extends ConditionalCoreStartable { private final Monitor mConditionMonitor; private final DreamCondition mDreamCondition; private final DreamStatusBarStateCallback mCallback; + private RestartDozeListener mRestartDozeListener; @Inject public DreamMonitor(Monitor monitor, DreamCondition dreamCondition, @Named(DREAM_PRETEXT_MONITOR) Monitor pretextMonitor, - DreamStatusBarStateCallback callback) { + DreamStatusBarStateCallback callback, + RestartDozeListener restartDozeListener) { super(pretextMonitor); mConditionMonitor = monitor; mDreamCondition = dreamCondition; mCallback = callback; - + mRestartDozeListener = restartDozeListener; } @Override @@ -61,5 +64,8 @@ public class DreamMonitor extends ConditionalCoreStartable { mConditionMonitor.addSubscription(new Monitor.Subscription.Builder(mCallback) .addCondition(mDreamCondition) .build()); + + mRestartDozeListener.init(); + mRestartDozeListener.maybeRestartSleep(); } } diff --git a/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsDebugStartable.kt b/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsDebugStartable.kt index 06ca0adfa928..28c45b874b24 100644 --- a/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsDebugStartable.kt +++ b/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsDebugStartable.kt @@ -22,7 +22,6 @@ import com.android.systemui.broadcast.BroadcastSender import com.android.systemui.dump.DumpManager import com.android.systemui.statusbar.commandline.CommandRegistry import com.android.systemui.util.InitializationChecker -import com.android.systemui.util.concurrency.DelayableExecutor import dagger.Binds import dagger.Module import dagger.multibindings.ClassKey @@ -38,8 +37,6 @@ constructor( private val featureFlags: FeatureFlagsDebug, private val broadcastSender: BroadcastSender, private val initializationChecker: InitializationChecker, - private val restartDozeListener: RestartDozeListener, - private val delayableExecutor: DelayableExecutor ) : CoreStartable { init { @@ -55,9 +52,6 @@ constructor( // protected broadcast should only be sent for the main process val intent = Intent(FlagManager.ACTION_SYSUI_STARTED) broadcastSender.sendBroadcast(intent) - - restartDozeListener.init() - delayableExecutor.executeDelayed({ restartDozeListener.maybeRestartSleep() }, 1000) } } } diff --git a/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsReleaseStartable.kt b/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsReleaseStartable.kt index 133e67f2822b..f97112d384be 100644 --- a/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsReleaseStartable.kt +++ b/packages/SystemUI/src/com/android/systemui/flags/FeatureFlagsReleaseStartable.kt @@ -18,8 +18,6 @@ package com.android.systemui.flags import com.android.systemui.CoreStartable import com.android.systemui.dump.DumpManager -import com.android.systemui.util.InitializationChecker -import com.android.systemui.util.concurrency.DelayableExecutor import dagger.Binds import dagger.Module import dagger.multibindings.ClassKey @@ -31,9 +29,6 @@ class FeatureFlagsReleaseStartable constructor( dumpManager: DumpManager, featureFlags: FeatureFlags, - private val initializationChecker: InitializationChecker, - private val restartDozeListener: RestartDozeListener, - private val delayableExecutor: DelayableExecutor ) : CoreStartable { init { @@ -42,12 +37,7 @@ constructor( } } - override fun start() { - if (initializationChecker.initializeComponents()) { - restartDozeListener.init() - delayableExecutor.executeDelayed({ restartDozeListener.maybeRestartSleep() }, 1000) - } - } + override fun start() {} } @Module diff --git a/packages/SystemUI/src/com/android/systemui/flags/RestartDozeListener.kt b/packages/SystemUI/src/com/android/systemui/flags/RestartDozeListener.kt index bd74f4e5daab..b49d60dbdde1 100644 --- a/packages/SystemUI/src/com/android/systemui/flags/RestartDozeListener.kt +++ b/packages/SystemUI/src/com/android/systemui/flags/RestartDozeListener.kt @@ -20,7 +20,9 @@ import android.os.PowerManager import android.util.Log import com.android.internal.annotations.VisibleForTesting import com.android.systemui.dagger.SysUISingleton +import com.android.systemui.dagger.qualifiers.Background import com.android.systemui.plugins.statusbar.StatusBarStateController +import com.android.systemui.util.concurrency.DelayableExecutor import com.android.systemui.util.settings.SecureSettings import com.android.systemui.util.time.SystemClock import javax.inject.Inject @@ -33,6 +35,7 @@ constructor( private val statusBarStateController: StatusBarStateController, private val powerManager: PowerManager, private val systemClock: SystemClock, + @Background val bgExecutor: DelayableExecutor, ) { companion object { @@ -44,7 +47,7 @@ constructor( val listener = object : StatusBarStateController.StateListener { override fun onDreamingChanged(isDreaming: Boolean) { - settings.putBool(RESTART_NAP_KEY, isDreaming) + storeSleepState(isDreaming) } } @@ -62,11 +65,19 @@ constructor( } fun maybeRestartSleep() { - if (settings.getBool(RESTART_NAP_KEY, false)) { - Log.d("RestartDozeListener", "Restarting sleep state") - powerManager.wakeUp(systemClock.uptimeMillis()) - powerManager.goToSleep(systemClock.uptimeMillis()) - settings.putBool(RESTART_NAP_KEY, false) - } + bgExecutor.executeDelayed( + { + if (settings.getBool(RESTART_NAP_KEY, false)) { + Log.d("RestartDozeListener", "Restarting sleep state") + powerManager.wakeUp(systemClock.uptimeMillis()) + powerManager.goToSleep(systemClock.uptimeMillis()) + } + }, + 1000 + ) + } + + private fun storeSleepState(sleeping: Boolean) { + bgExecutor.execute { settings.putBool(RESTART_NAP_KEY, sleeping) } } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/flags/RestartDozeListenerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/flags/RestartDozeListenerTest.kt index de0e5113571f..2db4596c80a7 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/flags/RestartDozeListenerTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/flags/RestartDozeListenerTest.kt @@ -20,6 +20,7 @@ import android.os.PowerManager 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.settings.FakeSettings import com.android.systemui.util.time.FakeSystemClock import com.google.common.truth.Truth.assertThat @@ -41,13 +42,14 @@ class RestartDozeListenerTest : SysuiTestCase() { @Mock lateinit var statusBarStateController: StatusBarStateController @Mock lateinit var powerManager: PowerManager val clock = FakeSystemClock() + val executor = FakeExecutor(clock) lateinit var listener: StatusBarStateController.StateListener @Before fun setup() { MockitoAnnotations.initMocks(this) restartDozeListener = - RestartDozeListener(settings, statusBarStateController, powerManager, clock) + RestartDozeListener(settings, statusBarStateController, powerManager, clock, executor) val captor = ArgumentCaptor.forClass(StatusBarStateController.StateListener::class.java) restartDozeListener.init() @@ -58,12 +60,14 @@ class RestartDozeListenerTest : SysuiTestCase() { @Test fun testStoreDreamState_onDreamingStarted() { listener.onDreamingChanged(true) + executor.runAllReady() assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isTrue() } @Test fun testStoreDreamState_onDreamingStopped() { listener.onDreamingChanged(false) + executor.runAllReady() assertThat(settings.getBool(RestartDozeListener.RESTART_NAP_KEY)).isFalse() } @@ -71,6 +75,8 @@ class RestartDozeListenerTest : SysuiTestCase() { fun testRestoreDreamState_dreamingShouldStart() { settings.putBool(RestartDozeListener.RESTART_NAP_KEY, true) restartDozeListener.maybeRestartSleep() + executor.advanceClockToLast() + executor.runAllReady() verify(powerManager).wakeUp(clock.uptimeMillis()) verify(powerManager).goToSleep(clock.uptimeMillis()) } @@ -79,6 +85,8 @@ class RestartDozeListenerTest : SysuiTestCase() { fun testRestoreDreamState_dreamingShouldNot() { settings.putBool(RestartDozeListener.RESTART_NAP_KEY, false) restartDozeListener.maybeRestartSleep() + executor.advanceClockToLast() + executor.runAllReady() verify(powerManager, never()).wakeUp(anyLong()) verify(powerManager, never()).goToSleep(anyLong()) } -- cgit v1.2.3-59-g8ed1b