Revert parallel opening camera in photo mode.

Reverts:
* ag/386409
* ag/386206
* ag/385573

Bug: 11598948
Bug: 11625767
Bug: 11627454
Bug: 11255097
Change-Id: Iad28e32dbcbbce42d9ee261c961cdd5a7712ede5

Conflicts:
	src/com/android/camera/PhotoModule.java
diff --git a/src/com/android/camera/PhotoModule.java b/src/com/android/camera/PhotoModule.java
index 593fb01..ca0a3eb 100644
--- a/src/com/android/camera/PhotoModule.java
+++ b/src/com/android/camera/PhotoModule.java
@@ -40,7 +40,6 @@
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
-import android.os.HandlerThread;
 import android.os.Looper;
 import android.os.Message;
 import android.os.MessageQueue;
@@ -104,10 +103,6 @@
     private static final int OPEN_CAMERA_FAIL = 9;
     private static final int CAMERA_DISABLED = 10;
     private static final int SWITCH_TO_GCAM_MODULE = 11;
-    private static final int CAMERA_PREVIEW_DONE = 12;
-
-    private static final int OPEN_CAMERA_ASYNC = 1;
-    private static final int START_PREVIEW_ASYNC = 2;
 
     // The subset of parameters we need to update in setCameraParameters().
     private static final int UPDATE_PARAM_INITIALIZE = 1;
@@ -244,13 +239,6 @@
 
     private final Handler mHandler = new MainHandler();
 
-    /** A thread separate from the UI thread for camera startup. */
-    private volatile HandlerThread mOpenCameraThread;
-    /** A handler to run on the camera startup thread. */
-    private volatile Handler mOpenCameraHandler;
-    /** This lock should always protect openCamera and closeCamera. */
-    private final Object mCameraOpenLock = new Object();
-
     private PreferenceGroup mPreferenceGroup;
 
     private boolean mQuickCapture;
@@ -293,74 +281,6 @@
     }
 
     /**
-     * This Handler is used to open the camera.
-     */
-    private class OpenCameraHandler extends Handler {
-        public OpenCameraHandler(Looper looper) {
-            super(looper);
-        }
-
-        @Override
-        public void handleMessage(Message msg) {
-            switch (msg.what) {
-                case OPEN_CAMERA_ASYNC: {
-                    // Prevent closeCamera from thinking the camera
-                    // is already closed when it is still opening.
-                    //
-                    // This happens during the lockscreen sequence:
-                    // onResume -> onPause -> onResume.
-                    synchronized (mCameraOpenLock) {
-                        Log.v(TAG, "openCamera");
-                        if (mCameraDevice != null) {
-                            throw new IllegalArgumentException("Camera already open.");
-                        }
-
-                        mCameraDevice = CameraUtil.openCamera(
-                            mActivity, mCameraId, mHandler,
-                            mActivity.getCameraOpenErrorCallback());
-
-                        if (mCameraDevice == null) {
-                            Log.e(TAG, "Failed to open camera:" + mCameraId);
-                            break;
-                        }
-                        mParameters = mCameraDevice.getParameters();
-
-                        initializeCapabilities();
-                        if (mFocusManager == null) {
-                            initializeFocusManager();
-                        }
-
-                        // The views can't be updated from a non UI thread.
-                        mHandler.sendEmptyMessage(CAMERA_OPEN_DONE);
-
-                        setCameraParameters(UPDATE_PARAM_ALL);
-                        mCameraPreviewParamsReady = true;
-
-                        // This will exit early if the surface texture
-                        // isn't ready. We also need to protect the surface
-                        // texture from concurrent updates/checks.
-                        startPreview();
-                    }
-                    break;
-                }
-
-                case START_PREVIEW_ASYNC: {
-                    if (mCameraDevice == null) {
-                        throw new IllegalStateException("Camera not yet opened.");
-                    }
-
-                    startPreview();
-                    break;
-                }
-
-                default: {
-                    throw new UnsupportedOperationException("Unknown message " + msg.what);
-                }
-            }
-        };
-    }
-
-    /**
      * This Handler is used to post message back onto the main thread of the
      * application
      */
@@ -429,13 +349,7 @@
                 }
 
                 case SWITCH_TO_GCAM_MODULE: {
-                    mActivity.onModeSelected(ModeListView.MODE_GCAM);
-                    break;
-                }
-
-                case CAMERA_PREVIEW_DONE: {
-                    // Modifies views, so must be executed on the UI thread.
-                    onPreviewStarted();
+                    mActivity.onModuleSelected(ModuleSwitcher.GCAM_MODULE_INDEX);
                 }
             }
         }
@@ -504,11 +418,7 @@
 
     @Override
     public void onPreviewUIReady() {
-        // Requires that OPEN_CAMERA_ASYNC has been already sent.
-        Handler openCameraHandler = mOpenCameraHandler;
-        if (openCameraHandler != null) {
-            openCameraHandler.sendEmptyMessage(START_PREVIEW_ASYNC);
-        }
+        startPreview();
     }
 
     @Override
@@ -548,11 +458,7 @@
         setCameraId(mCameraId);
 
         // from onPause
-        mOpenCameraHandler.removeMessages(OPEN_CAMERA_ASYNC);
-        mOpenCameraHandler.removeMessages(START_PREVIEW_ASYNC);
-        synchronized (mCameraOpenLock) {
-            closeCamera();
-        }
+        closeCamera();
         mUI.collapseCameraControls();
         mUI.clearFaces();
         if (mFocusManager != null) mFocusManager.removeMessages();
@@ -560,12 +466,10 @@
         // Restart the camera and initialize the UI. From onCreate.
         mPreferences.setLocalId(mActivity, mCameraId);
         CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
-        synchronized (mCameraOpenLock) {
-            Log.v(TAG, "openCamera");
-            mCameraDevice = CameraUtil.openCamera(
+        mCameraDevice = CameraUtil.openCamera(
                 mActivity, mCameraId, mHandler,
                 mActivity.getCameraOpenErrorCallback());
-        }
+
         if (mCameraDevice == null) {
             Log.e(TAG, "Failed to open camera:" + mCameraId + ", aborting.");
             return;
@@ -1262,25 +1166,41 @@
         mPaused = false;
     }
 
+    private boolean prepareCamera() {
+        // We need to check whether the activity is paused before long
+        // operations to ensure that onPause() can be done ASAP.
+        mCameraDevice = CameraUtil.openCamera(
+                mActivity, mCameraId, mHandler,
+                mActivity.getCameraOpenErrorCallback());
+        if (mCameraDevice == null) {
+            Log.e(TAG, "Failed to open camera:" + mCameraId);
+            return false;
+        }
+        mParameters = mCameraDevice.getParameters();
+
+        initializeCapabilities();
+        if (mFocusManager == null) initializeFocusManager();
+        setCameraParameters(UPDATE_PARAM_ALL);
+        mHandler.sendEmptyMessage(CAMERA_OPEN_DONE);
+        mCameraPreviewParamsReady = true;
+        startPreview();
+        mOnResumeTime = SystemClock.uptimeMillis();
+        checkDisplayRotation();
+        return true;
+    }
+
     @Override
     public void onResumeAfterSuper() {
         Log.v(TAG, "On resume.");
         if (mOpenCameraFail || mCameraDisabled) return;
 
-        if (mOpenCameraThread == null) {
-            Log.e("DEBUG", "new OpenCameraThread");
-            mOpenCameraThread = new HandlerThread("OpenCameraThread");
-            mOpenCameraThread.start();
-            mOpenCameraHandler = new OpenCameraHandler(mOpenCameraThread.getLooper());
-        }
-
         mJpegPictureCallbackTime = 0;
         mZoomValue = 0;
         resetExposureCompensation();
-
-        mOpenCameraHandler.sendEmptyMessage(OPEN_CAMERA_ASYNC);
-        mOnResumeTime = SystemClock.uptimeMillis();
-        checkDisplayRotation();
+        if (!prepareCamera()) {
+            // Camera failure.
+            return;
+        }
 
         // If first time initialization is not finished, put it in the
         // message queue.
@@ -1357,20 +1277,7 @@
         // Postpones actually releasing for KEEP_CAMERA_TIMEOUT,
         // so if onResume is directly called after this, the camera
         // simply needs to reconnect (takes about 2-5ms).
-        if (mOpenCameraHandler != null) {
-            mOpenCameraHandler.removeMessages(OPEN_CAMERA_ASYNC);
-            mOpenCameraHandler.removeMessages(START_PREVIEW_ASYNC);
-            mOpenCameraHandler = null;
-        }
-        synchronized (mCameraOpenLock) {
-            closeCamera();
-        }
-        // Stop the long running open camera thread.
-        if (mOpenCameraThread != null) {
-            mOpenCameraThread.quitSafely();
-            mOpenCameraThread = null;
-        }
-        Log.e(TAG, "Done quiting safely.");
+        closeCamera();
 
         resetScreenOn();
         mUI.onPause();
@@ -1532,7 +1439,6 @@
     }
 
     private void closeCamera() {
-        Log.v(TAG, "closeCamera");
         if (mCameraDevice != null) {
             mCameraDevice.setZoomChangeListener(null);
             mCameraDevice.setFaceDetectionCallback(null, null);
@@ -1578,55 +1484,47 @@
             return;
         }
 
-        Object textureLock = mUI.getSurfaceTextureLock();
-
          // Any decisions we make based on the surface texture state
          // need to be protected.
-        synchronized (textureLock) {
-            SurfaceTexture st = mUI.getSurfaceTexture();
-            if (st == null) {
-                Log.w(TAG, "startPreview: surfaceTexture is not ready.");
-                return;
+        SurfaceTexture st = mUI.getSurfaceTexture();
+        if (st == null) {
+            Log.w(TAG, "startPreview: surfaceTexture is not ready.");
+            return;
+        }
+
+        if (!mCameraPreviewParamsReady) {
+            Log.w(TAG, "startPreview: parameters for preview is not ready.");
+            return;
+        }
+        mCameraDevice.setErrorCallback(mErrorCallback);
+        // ICS camera frameworks has a bug. Face detection state is not cleared 1589
+        // after taking a picture. Stop the preview to work around it. The bug
+        // was fixed in JB.
+        if (mCameraState != PREVIEW_STOPPED) {
+            stopPreview();
+        }
+
+        setDisplayOrientation();
+
+        if (!mSnapshotOnIdle) {
+            // If the focus mode is continuous autofocus, call cancelAutoFocus to
+            // resume it because it may have been paused by autoFocus call.
+            if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode())) {
+                mCameraDevice.cancelAutoFocus();
             }
+            mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
+        }
+        setCameraParameters(UPDATE_PARAM_ALL);
+        // Let UI set its expected aspect ratio
+        mCameraDevice.setPreviewTexture(st);
 
-            if (!mCameraPreviewParamsReady) {
-                Log.w(TAG, "startPreview: parameters for preview is not ready.");
-                return;
-            }
-            mCameraDevice.setErrorCallback(mErrorCallback);
-            // ICS camera frameworks has a bug. Face detection state is not cleared 1589
-            // after taking a picture. Stop the preview to work around it. The bug
-            // was fixed in JB.
-            if (mCameraState != PREVIEW_STOPPED) {
-                stopPreview();
-            }
+        Log.v(TAG, "startPreview");
+        mCameraDevice.startPreview();
+        mFocusManager.onPreviewStarted();
+        onPreviewStarted();
 
-            setDisplayOrientation();
-
-            if (!mSnapshotOnIdle) {
-                // If the focus mode is continuous autofocus, call cancelAutoFocus to
-                // resume it because it may have been paused by autoFocus call.
-                if (CameraUtil.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mFocusManager.getFocusMode())) {
-                    mCameraDevice.cancelAutoFocus();
-                }
-                mFocusManager.setAeAwbLock(false); // Unlock AE and AWB.
-            }
-            setCameraParameters(UPDATE_PARAM_ALL);
-            // Let UI set its expected aspect ratio
-            mCameraDevice.setPreviewTexture(st);
-
-            Log.v(TAG, "startPreview");
-            mCameraDevice.startPreview();
-
-            // Since the preview actually started, remove any messages to
-            // start it again.
-            mOpenCameraHandler.removeMessages(START_PREVIEW_ASYNC);
-            mFocusManager.onPreviewStarted();
-
-            if (mSnapshotOnIdle) {
-                mHandler.post(mDoSnapRunnable);
-            }
-            mHandler.sendEmptyMessage(CAMERA_PREVIEW_DONE);
+        if (mSnapshotOnIdle) {
+            mHandler.post(mDoSnapRunnable);
         }
     }