Skip detector enablement if isUserInteracting is true

Fixes: 311266890
Test: Manually set the flow to always return true and prompted
Flag: N/A
Change-Id: Ia96544625d206d149a6fab5d144e93d6ee0ca4af
diff --git a/packages/SystemUI/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetector.kt b/packages/SystemUI/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetector.kt
index 8d1d905..b343add 100644
--- a/packages/SystemUI/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetector.kt
+++ b/packages/SystemUI/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetector.kt
@@ -37,17 +37,23 @@
 
     @MainThread
     fun enable(onShadeInteraction: Runnable) {
-        if (shadeExpansionCollectorJob == null) {
-            shadeExpansionCollectorJob =
-                scope.launch {
-                    // wait for it to emit true once
-                    shadeInteractorLazy.get().isUserInteracting.first { it }
-                    onShadeInteraction.run()
-                }
-            shadeExpansionCollectorJob?.invokeOnCompletion { shadeExpansionCollectorJob = null }
-        } else {
+        if (shadeExpansionCollectorJob != null) {
             Log.e(TAG, "Already enabled")
+            return
         }
+        if (shadeInteractorLazy.get().isUserInteracting.value) {
+            Log.e(TAG, "isUserInteracting already true, skipping enable")
+            return
+        }
+        shadeExpansionCollectorJob =
+            scope.launch {
+                Log.i(TAG, "Enable detector")
+                // wait for it to emit true once
+                shadeInteractorLazy.get().isUserInteracting.first { it }
+                Log.i(TAG, "Detector detected shade interaction")
+                onShadeInteraction.run()
+            }
+        shadeExpansionCollectorJob?.invokeOnCompletion { shadeExpansionCollectorJob = null }
     }
 
     @MainThread
diff --git a/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractor.kt b/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractor.kt
index 6a9757f..31a4de4 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractor.kt
@@ -39,7 +39,7 @@
      * input (i.e. dragging a pointer). This will be true even if the user's input gesture had ended
      * but a transition they initiated is still animating.
      */
-    val isUserInteracting: Flow<Boolean>
+    val isUserInteracting: StateFlow<Boolean>
 
     /** Are touches allowed on the notification panel? */
     val isShadeTouchable: Flow<Boolean>
diff --git a/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorEmptyImpl.kt b/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorEmptyImpl.kt
index d41c5a6..6defbcf 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorEmptyImpl.kt
+++ b/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorEmptyImpl.kt
@@ -39,7 +39,7 @@
     override val isAnyExpanded: StateFlow<Boolean> = inactiveFlowBoolean
     override val isUserInteractingWithShade: Flow<Boolean> = inactiveFlowBoolean
     override val isUserInteractingWithQs: Flow<Boolean> = inactiveFlowBoolean
-    override val isUserInteracting: Flow<Boolean> = inactiveFlowBoolean
+    override val isUserInteracting: StateFlow<Boolean> = inactiveFlowBoolean
     override val isShadeTouchable: Flow<Boolean> = inactiveFlowBoolean
     override val isExpandToQsEnabled: Flow<Boolean> = inactiveFlowBoolean
 }
diff --git a/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorImpl.kt b/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorImpl.kt
index 68600e9..7a340d2 100644
--- a/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorImpl.kt
+++ b/packages/SystemUI/src/com/android/systemui/shade/domain/interactor/ShadeInteractorImpl.kt
@@ -65,9 +65,10 @@
     override val isShadeFullyExpanded: Flow<Boolean> =
         baseShadeInteractor.shadeExpansion.map { it >= 1f }.distinctUntilChanged()
 
-    override val isUserInteracting: Flow<Boolean> =
+    override val isUserInteracting: StateFlow<Boolean> =
         combine(isUserInteractingWithShade, isUserInteractingWithQs) { shade, qs -> shade || qs }
             .distinctUntilChanged()
+            .stateIn(scope, SharingStarted.Eagerly, false)
 
     override val isShadeTouchable: Flow<Boolean> =
         combine(
diff --git a/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetectorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetectorTest.kt
index 993dbac..54d6b53 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetectorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/biometrics/AuthDialogPanelInteractionDetectorTest.kt
@@ -95,6 +95,18 @@
         }
 
     @Test
+    fun enableDetector_isUserInteractingTrue_shouldNotPostRunnable() =
+        testComponent.runTest {
+            // GIVEN isInteracting starts true
+            shadeRepository.setLegacyShadeTracking(true)
+            runCurrent()
+            detector.enable(action)
+
+            // THEN action was not run
+            verifyZeroInteractions(action)
+        }
+
+    @Test
     fun enableDetector_shadeExpandImmediate_shouldNotPostRunnable() =
         testComponent.runTest {
             // GIVEN shade is closed and detector is enabled