diff options
4 files changed, 48 insertions, 42 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt index 742e53515e82..81f62b687e6c 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/KeyguardRepository.kt @@ -25,6 +25,7 @@ import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLoggin import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow import com.android.systemui.common.shared.model.Position import com.android.systemui.dagger.SysUISingleton +import com.android.systemui.dagger.qualifiers.Application import com.android.systemui.dagger.qualifiers.Main import com.android.systemui.doze.DozeMachine import com.android.systemui.doze.DozeTransitionCallback @@ -44,13 +45,16 @@ import com.android.systemui.statusbar.phone.DozeParameters import com.android.systemui.statusbar.policy.KeyguardStateController import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.stateIn /** Defines interface for classes that encapsulate application state for the keyguard. */ interface KeyguardRepository { @@ -138,7 +142,7 @@ interface KeyguardRepository { val statusBarState: Flow<StatusBarState> /** Observable for device wake/sleep state */ - val wakefulness: Flow<WakefulnessModel> + val wakefulness: StateFlow<WakefulnessModel> /** Observable for biometric unlock modes */ val biometricUnlockState: Flow<BiometricUnlockModel> @@ -202,7 +206,8 @@ constructor( private val dozeParameters: DozeParameters, private val authController: AuthController, private val dreamOverlayCallbackController: DreamOverlayCallbackController, - @Main private val mainDispatcher: CoroutineDispatcher + @Main private val mainDispatcher: CoroutineDispatcher, + @Application private val scope: CoroutineScope, ) : KeyguardRepository { private val _animateBottomAreaDozingTransitions = MutableStateFlow(false) override val animateBottomAreaDozingTransitions = @@ -486,47 +491,48 @@ constructor( awaitClose { biometricUnlockController.removeListener(callback) } } - override val wakefulness: Flow<WakefulnessModel> = conflatedCallbackFlow { - val observer = - object : WakefulnessLifecycle.Observer { - override fun onStartedWakingUp() { - dispatchNewState() - } - - override fun onFinishedWakingUp() { - dispatchNewState() - } + override val wakefulness: StateFlow<WakefulnessModel> = + conflatedCallbackFlow { + val observer = + object : WakefulnessLifecycle.Observer { + override fun onStartedWakingUp() { + dispatchNewState() + } - override fun onPostFinishedWakingUp() { - dispatchNewState() - } + override fun onFinishedWakingUp() { + dispatchNewState() + } - override fun onStartedGoingToSleep() { - dispatchNewState() - } + override fun onPostFinishedWakingUp() { + dispatchNewState() + } - override fun onFinishedGoingToSleep() { - dispatchNewState() - } + override fun onStartedGoingToSleep() { + dispatchNewState() + } - private fun dispatchNewState() { - trySendWithFailureLogging( - WakefulnessModel.fromWakefulnessLifecycle(wakefulnessLifecycle), - TAG, - "updated wakefulness state" - ) - } - } + override fun onFinishedGoingToSleep() { + dispatchNewState() + } - wakefulnessLifecycle.addObserver(observer) - trySendWithFailureLogging( - WakefulnessModel.fromWakefulnessLifecycle(wakefulnessLifecycle), - TAG, - "initial wakefulness state" - ) + private fun dispatchNewState() { + trySendWithFailureLogging( + WakefulnessModel.fromWakefulnessLifecycle(wakefulnessLifecycle), + TAG, + "updated wakefulness state", + ) + } + } - awaitClose { wakefulnessLifecycle.removeObserver(observer) } - } + wakefulnessLifecycle.addObserver(observer) + awaitClose { wakefulnessLifecycle.removeObserver(observer) } + } + .stateIn( + scope, + // Use Eagerly so that we're always listening and never miss an event. + SharingStarted.Eagerly, + initialValue = WakefulnessModel.fromWakefulnessLifecycle(wakefulnessLifecycle), + ) override val fingerprintSensorLocation: Flow<Point?> = conflatedCallbackFlow { fun sendFpLocation() { diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt index f99b8a2458f2..d13d5ad67cae 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardInteractor.kt @@ -40,6 +40,7 @@ import javax.inject.Inject import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filter @@ -99,7 +100,7 @@ constructor( } /** The device wake/sleep state */ - val wakefulnessModel: Flow<WakefulnessModel> = repository.wakefulness + val wakefulnessModel: StateFlow<WakefulnessModel> = repository.wakefulness /** * Dozing and dreaming have overlapping events. If the doze state remains in FINISH, it means diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardRepositoryImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardRepositoryImplTest.kt index 4b797cb1ba46..953d61844596 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardRepositoryImplTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/data/repository/KeyguardRepositoryImplTest.kt @@ -97,7 +97,8 @@ class KeyguardRepositoryImplTest : SysuiTestCase() { dozeParameters, authController, dreamOverlayCallbackController, - mainDispatcher + mainDispatcher, + testScope.backgroundScope, ) } @@ -343,8 +344,6 @@ class KeyguardRepositoryImplTest : SysuiTestCase() { ) job.cancel() - runCurrent() - verify(wakefulnessLifecycle).removeObserver(captor.value) } @Test diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt index b52a76839a99..f6cbb072495f 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/data/repository/FakeKeyguardRepository.kt @@ -81,7 +81,7 @@ class FakeKeyguardRepository : KeyguardRepository { MutableStateFlow( WakefulnessModel(WakefulnessState.ASLEEP, WakeSleepReason.OTHER, WakeSleepReason.OTHER) ) - override val wakefulness: Flow<WakefulnessModel> = _wakefulnessModel + override val wakefulness = _wakefulnessModel private val _isUdfpsSupported = MutableStateFlow(false) |