Fix synchronization in camera test.
diff --git a/tests/tests/hardware/src/android/hardware/cts/CameraTest.java b/tests/tests/hardware/src/android/hardware/cts/CameraTest.java
index 593ffdb..9aef52c 100644
--- a/tests/tests/hardware/src/android/hardware/cts/CameraTest.java
+++ b/tests/tests/hardware/src/android/hardware/cts/CameraTest.java
@@ -31,6 +31,7 @@
 import android.hardware.Camera.PreviewCallback;
 import android.hardware.Camera.Size;
 import android.hardware.Camera.ShutterCallback;
+import android.os.ConditionVariable;
 import android.os.Looper;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
@@ -69,8 +70,7 @@
     private TestAutoFocusCallback mAutoFocusCallback = new TestAutoFocusCallback();
 
     private Looper mLooper = null;
-    private final Object mLock = new Object();
-    private final Object mPreviewDone = new Object();
+    private final ConditionVariable mPreviewDone = new ConditionVariable();
 
     Camera mCamera;
 
@@ -92,6 +92,7 @@
      */
     private void initializeMessageLooper() {
         if (LOGV) Log.v(TAG, "start looper");
+        final ConditionVariable startDone = new ConditionVariable();
         new Thread() {
             @Override
             public void run() {
@@ -102,13 +103,15 @@
                 // after we are done with it.
                 mLooper = Looper.myLooper();
                 mCamera = Camera.open();
-                synchronized (mLock) {
-                    mLock.notify();
-                }
+                startDone.open();
                 Looper.loop(); // Blocks forever until Looper.quit() is called.
                 if (LOGV) Log.v(TAG, "initializeMessageLooper: quit.");
             }
         }.start();
+
+        if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
+            fail("initializeMessageLooper: start timeout");
+        }
     }
 
     /*
@@ -133,11 +136,8 @@
                 mRawPreviewCallbackResult = false;
             }
             mCamera.stopPreview();
-            synchronized (mPreviewDone) {
-                if (LOGV) Log.v(TAG, "notify the preview callback");
-                mPreviewDone.notify();
-            }
-
+            if (LOGV) Log.v(TAG, "notify the preview callback");
+            mPreviewDone.open();
             if (LOGV) Log.v(TAG, "Preview callback stop");
         }
     }
@@ -222,16 +222,12 @@
     }
 
     private void waitForPreviewDone() {
-        synchronized (mPreviewDone) {
-            try {
-                mPreviewDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
-                if (LOGV)
-                    Log.v(TAG, "Wait for preview callback");
-            } catch (Exception e) {
-                if (LOGV)
-                    Log.v(TAG, "wait was interrupted.");
-            }
+        if (LOGV) Log.v(TAG, "Wait for preview callback");
+        if (!mPreviewDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
+            // timeout could be expected or unexpected. The caller will decide.
+            if (LOGV) Log.v(TAG, "waitForPreviewDone: timeout");
         }
+        mPreviewDone.close();
     }
 
     private void checkPreviewCallback() throws Exception {
@@ -291,7 +287,6 @@
     // There is some problems in testing autoFocus, setErrorCallback
     public void testTakePicture() throws Exception {
         initializeMessageLooper();
-        syncLock();
         checkTakePicture();
         terminateMessageLooper();
         assertTrue(mShutterCallbackResult);
@@ -346,7 +341,6 @@
     @BrokenTest("Flaky test. Occasionally fails without a stack trace.")
     public void testCheckPreview() throws Exception {
         initializeMessageLooper();
-        syncLock();
         mCamera.setPreviewCallback(mRawPreviewCallback);
         mCamera.setErrorCallback(mErrorCallback);
         checkPreviewCallback();
@@ -361,7 +355,6 @@
     )
     public void testSetOneShotPreviewCallback() throws Exception {
         initializeMessageLooper();
-        syncLock();
         mCamera.setOneShotPreviewCallback(mRawPreviewCallback);
         checkPreviewCallback();
         terminateMessageLooper();
@@ -369,7 +362,6 @@
 
         mRawPreviewCallbackResult = false;
         initializeMessageLooper();
-        syncLock();
         checkPreviewCallback();
         terminateMessageLooper();
         assertFalse(mRawPreviewCallbackResult);
@@ -384,7 +376,6 @@
         SurfaceHolder mSurfaceHolder;
         mSurfaceHolder = CameraStubActivity.mSurfaceView.getHolder();
         initializeMessageLooper();
-        syncLock();
 
         // Check the order: startPreview->setPreviewDisplay.
         mCamera.setOneShotPreviewCallback(mRawPreviewCallback);
@@ -396,7 +387,6 @@
 
         // Check the order: setPreviewDisplay->startPreview.
         initializeMessageLooper();
-        syncLock();
         mRawPreviewCallbackResult = false;
         mCamera.setOneShotPreviewCallback(mRawPreviewCallback);
         mCamera.setPreviewDisplay(mSurfaceHolder);
@@ -431,16 +421,10 @@
     })
     public void testAccessParameters() throws Exception {
         initializeMessageLooper();
-        syncLock();
         // we can get parameters just by getxxx method due to the private constructor
         Parameters pSet = mCamera.getParameters();
         assertParameters(pSet);
-    }
-
-    private void syncLock() throws Exception {
-        synchronized (mLock) {
-            mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
-        }
+        terminateMessageLooper();
     }
 
     // Also test Camera.Parameters