Stop and restart the preview in surfaceChanged.

If surface is changed, applications should call setPreviewDisplay
again.
Change-Id: I91072e6daba841db486b54240bb85c455588aaee
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java
index cb76226..4008f82 100644
--- a/src/com/android/camera/Camera.java
+++ b/src/com/android/camera/Camera.java
@@ -1283,12 +1283,7 @@
         // Start the preview if it is not started.
         if (!mPreviewing && !mStartPreviewFail) {
             resetExposureCompensation();
-            try {
-                startPreview();
-            } catch (CameraHardwareException e) {
-                showCameraErrorAndFinish();
-                return;
-            }
+            if (!restartPreview()) return;
         }
 
         if (mSurfaceHolder != null) {
@@ -1615,13 +1610,14 @@
                 ress.getString(R.string.cannot_connect_camera));
     }
 
-    private void restartPreview() {
+    private boolean restartPreview() {
         try {
             startPreview();
         } catch (CameraHardwareException e) {
             showCameraErrorAndFinish();
-            return;
+            return false;
         }
+        return true;
     }
 
     private void setPreviewDisplay(SurfaceHolder holder) {
@@ -2110,12 +2106,7 @@
 
         // Restart the preview.
         resetExposureCompensation();
-        try {
-            startPreview();
-        } catch (CameraHardwareException e) {
-            showCameraErrorAndFinish();
-            return;
-        }
+        if (!restartPreview()) return;
 
         initializeZoom();
 
diff --git a/src/com/android/camera/VideoCamera.java b/src/com/android/camera/VideoCamera.java
index 8916b1b..59f6ab9 100644
--- a/src/com/android/camera/VideoCamera.java
+++ b/src/com/android/camera/VideoCamera.java
@@ -246,13 +246,23 @@
         return dateFormat.format(date);
     }
 
-    private void showCameraBusyAndFinish() {
+    private void showCameraErrorAndFinish() {
         Resources ress = getResources();
         Util.showFatalErrorAndFinish(VideoCamera.this,
                 ress.getString(R.string.camera_error_title),
                 ress.getString(R.string.cannot_connect_camera));
     }
 
+    private boolean restartPreview() {
+        try {
+            startPreview();
+        } catch (CameraHardwareException e) {
+            showCameraErrorAndFinish();
+            return false;
+        }
+        return true;
+    }
+
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -360,7 +370,7 @@
         try {
             startPreviewThread.join();
             if (mStartPreviewFail) {
-                showCameraBusyAndFinish();
+                showCameraErrorAndFinish();
                 return;
             }
         } catch (InterruptedException ex) {
@@ -582,12 +592,7 @@
         readVideoPreferences();
         resizeForPreviewAspectRatio();
         if (!mPreviewing && !mStartPreviewFail) {
-            try {
-                startPreview();
-            } catch (CameraHardwareException e) {
-                showCameraBusyAndFinish();
-                return;
-            }
+            if (!restartPreview()) return;
         }
         keepScreenOnAwhile();
 
@@ -627,11 +632,6 @@
 
     private void startPreview() throws CameraHardwareException {
         Log.v(TAG, "startPreview");
-        if (mPreviewing) {
-            // After recording a video, preview is not stopped. So just return.
-            return;
-        }
-
         if (mCameraDevice == null) {
             // If the activity is paused and resumed, camera device has been
             // released and we need to open the camera.
@@ -780,6 +780,8 @@
             return;
         }
 
+        mSurfaceHolder = holder;
+
         if (mPausing) {
             // We're pausing, the screen is off and we already stopped
             // video recording. We don't want to start the camera again
@@ -795,21 +797,20 @@
         // finish the activity, so it's OK to ignore it.
         if (mCameraDevice == null) return;
 
-        if (mMediaRecorderRecording) {
-            stopVideoRecording();
-        }
-
         // Set preview display if the surface is being created. Preview was
         // already started.
         if (holder.isCreating()) {
             setPreviewDisplay(holder);
             mCameraDevice.unlock();
             mHandler.sendEmptyMessage(INIT_RECORDER);
+        } else {
+            stopVideoRecording();
+            restartPreview();
+            initializeRecorder();
         }
     }
 
     public void surfaceCreated(SurfaceHolder holder) {
-        mSurfaceHolder = holder;
     }
 
     public void surfaceDestroyed(SurfaceHolder holder) {
@@ -1108,13 +1109,7 @@
         mPreferences.setLocalId(this, mCameraId);
         CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
 
-        // Restart preview
-        try {
-            startPreview();
-        } catch (CameraHardwareException e) {
-            showCameraBusyAndFinish();
-            return;
-        }
+        restartPreview();
 
         // Reload the UI.
         initializeHeadUpDisplay();
@@ -1525,12 +1520,8 @@
             // It is assumed media recorder is released before
             // onSharedPreferenceChanged, so we can close the camera here.
             closeCamera();
-            try {
-                resizeForPreviewAspectRatio();
-                startPreview(); // Parameters will be set in startPreview().
-            } catch (CameraHardwareException e) {
-                showCameraBusyAndFinish();
-            }
+            resizeForPreviewAspectRatio();
+            restartPreview(); // Parameters will be set in startPreview().
         } else {
             try {
                 // We need to lock the camera before writing parameters.