Await when abortCapturesOnStop

When a CaptureSessionState is disconnected and
CameraGraph.Flags.abortCapturesOnStop is true, we'll invoke
abortCaptures on the current session, which also triggers a flush on the
camera device inside the camera framework. Since the `disconnect` call
is done in `async`, there's a possibility the `disconnect` call ends up
aborting captures while a new capture session is configuring, causing
unwanted behaviors.

Await when abortCapturesOnStop to prevent the aforementioned scenario.

Bug: 365171667
Test: CameraPipe unit test.
Change-Id: I846a1e6c08cb9699ad3dd5712b6e47d42c41944b
diff --git a/camera/camera-camera2-pipe/src/main/java/androidx/camera/camera2/pipe/compat/Camera2CameraController.kt b/camera/camera-camera2-pipe/src/main/java/androidx/camera/camera2/pipe/compat/Camera2CameraController.kt
index 0c41ed2..23fbcca 100644
--- a/camera/camera-camera2-pipe/src/main/java/androidx/camera/camera2/pipe/compat/Camera2CameraController.kt
+++ b/camera/camera-camera2-pipe/src/main/java/androidx/camera/camera2/pipe/compat/Camera2CameraController.kt
@@ -303,19 +303,22 @@
                 session?.disconnect()
                 camera?.disconnect()
             }
-        if (graphConfig.flags.closeCaptureSessionOnDisconnect) {
+        if (
+            graphConfig.flags.abortCapturesOnStop ||
+                graphConfig.flags.closeCaptureSessionOnDisconnect
+        ) {
             // It seems that on certain devices, CameraCaptureSession.close() can block for an
             // extended period of time [1]. Wrap the await call with a timeout to prevent us from
             // getting blocked for too long.
             //
             // [1] b/307594946 - [ANR] at Camera2CameraController.disconnectSessionAndCamera
-            runBlockingWithTimeout(threads.backgroundDispatcher, CLOSE_CAPTURE_SESSION_TIMEOUT_MS) {
+            runBlockingWithTimeout(threads.backgroundDispatcher, DISCONNECT_TIMEOUT_MS) {
                 deferred.await()
             }
         }
     }
 
     companion object {
-        private const val CLOSE_CAPTURE_SESSION_TIMEOUT_MS = 2_000L // 2s
+        private const val DISCONNECT_TIMEOUT_MS = 2_000L // 2s
     }
 }