Camera2: On stopping preview, wait until captures are completed.

If preview is stopped while a still capture or a video snapshot is
underway, wait until the capture completes before stopping preview.

Also use the same behavior for disconnect - do not shut down the
camera device until captures are complete.

This works around HAL implementations which report to be done
(get_in_progress_count returns 0) even when a capture is still
outstanding.

Bug: 7276954

Change-Id: I66290acf1740cc330eadecbcded0c59fa9c5d2fd
diff --git a/services/camera/libcameraservice/camera2/CaptureSequencer.h b/services/camera/libcameraservice/camera2/CaptureSequencer.h
index 07e4c01..8df6d95 100644
--- a/services/camera/libcameraservice/camera2/CaptureSequencer.h
+++ b/services/camera/libcameraservice/camera2/CaptureSequencer.h
@@ -53,6 +53,11 @@
     // Begin still image capture
     status_t startCapture();
 
+    // Wait until current image capture completes; returns immediately if no
+    // capture is active. Returns TIMED_OUT if capture does not complete during
+    // the specified duration.
+    status_t waitUntilIdle(nsecs_t timeout);
+
     // Notifications about AE state changes
     void notifyAutoExposure(uint8_t newState, int triggerId);
 
@@ -118,6 +123,8 @@
         NUM_CAPTURE_STATES
     } mCaptureState;
     static const char* kStateNames[];
+    Mutex mStateMutex; // Guards mCaptureState
+    Condition mStateChanged;
 
     typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
     static const StateManager kStateManagers[];