From cf3da8a95bd631433c44f303561322b4e408e53f Mon Sep 17 00:00:00 2001 From: Chandru S Date: Tue, 14 Nov 2023 20:17:12 +0530 Subject: Update isAuthenticated before authentication event is emitted Convert the flow to a state flow as we need that for the child CL. Emitting the authentication status first results in the collectors running before `isAuthenticated` flow is updated. Bug: NA Test: unable to replicate the same coroutine behavior in a unit test. Flag: LEGACY FACE_AUTH_REFACTOR ENABLED Change-Id: Ie258205863ccaafe3215309fd7b40691b2263f60 --- .../data/repository/DeviceEntryFaceAuthRepository.kt | 18 +++++++++++++----- .../repository/NoopDeviceEntryFaceAuthRepository.kt | 3 +-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/DeviceEntryFaceAuthRepository.kt b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/DeviceEntryFaceAuthRepository.kt index c4dfe9afeb2a..cca922098da4 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/DeviceEntryFaceAuthRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/DeviceEntryFaceAuthRepository.kt @@ -89,7 +89,7 @@ import kotlinx.coroutines.withContext */ interface DeviceEntryFaceAuthRepository { /** Provide the current face authentication state for device entry. */ - val isAuthenticated: Flow + val isAuthenticated: StateFlow /** Whether face auth can run at this point. */ val canRunFaceAuth: StateFlow @@ -199,8 +199,7 @@ constructor( private val canRunDetection: StateFlow private val _isAuthenticated = MutableStateFlow(false) - override val isAuthenticated: Flow - get() = _isAuthenticated + override val isAuthenticated: StateFlow = _isAuthenticated private var cancellationInProgress = MutableStateFlow(false) @@ -454,8 +453,8 @@ constructor( if (errorStatus.isLockoutError()) { _isLockedOut.value = true } - _authenticationStatus.value = errorStatus _isAuthenticated.value = false + _authenticationStatus.value = errorStatus if (errorStatus.isHardwareError()) { faceAuthLogger.hardwareError(errorStatus) handleFaceHardwareError() @@ -477,8 +476,17 @@ constructor( } override fun onAuthenticationSucceeded(result: FaceManager.AuthenticationResult) { - _authenticationStatus.value = SuccessFaceAuthenticationStatus(result) + // Update _isAuthenticated before _authenticationStatus is updated. There are + // consumers that receive the face authentication updates through a long chain of + // callbacks + // _authenticationStatus -> KeyguardUpdateMonitor -> KeyguardStateController -> + // onUnlockChanged + // These consumers then query the isAuthenticated boolean. This makes sure that the + // boolean is updated to new value before the event is propagated. + // TODO (b/310592822): once all consumers can use the new system directly, we don't + // have to worry about this ordering. _isAuthenticated.value = true + _authenticationStatus.value = SuccessFaceAuthenticationStatus(result) faceAuthLogger.faceAuthSuccess(result) onFaceAuthRequestCompleted() } diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/NoopDeviceEntryFaceAuthRepository.kt b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/NoopDeviceEntryFaceAuthRepository.kt index c8cb9e6aa0dd..9f7aeb9dd0f4 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/NoopDeviceEntryFaceAuthRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/data/repository/NoopDeviceEntryFaceAuthRepository.kt @@ -34,8 +34,7 @@ import kotlinx.coroutines.flow.emptyFlow */ @SysUISingleton class NoopDeviceEntryFaceAuthRepository @Inject constructor() : DeviceEntryFaceAuthRepository { - override val isAuthenticated: Flow - get() = emptyFlow() + override val isAuthenticated: StateFlow = MutableStateFlow(false) private val _canRunFaceAuth = MutableStateFlow(false) override val canRunFaceAuth: StateFlow = _canRunFaceAuth -- cgit v1.2.3-59-g8ed1b