Add recent changes to new photo module

Change-Id: Icd29a41e1564094e4feafd6c4d4c809f6253753d
diff --git a/src/com/android/camera/NewCameraModule.java b/src/com/android/camera/NewCameraModule.java
index 061cc6c..35452bb 100644
--- a/src/com/android/camera/NewCameraModule.java
+++ b/src/com/android/camera/NewCameraModule.java
@@ -52,8 +52,6 @@
 
     public void onSingleTapUp(View view, int x, int y);
 
-    public boolean dispatchTouchEvent(MotionEvent m);
-
     public void onPreviewTextureCopied();
 
     public void onCaptureTextureCopied();
diff --git a/src/com/android/camera/NewPhotoMenu.java b/src/com/android/camera/NewPhotoMenu.java
index f324033..c63aff4 100644
--- a/src/com/android/camera/NewPhotoMenu.java
+++ b/src/com/android/camera/NewPhotoMenu.java
@@ -16,8 +16,6 @@
 
 package com.android.camera;
 
-import java.util.Locale;
-
 import android.content.res.Resources;
 import android.hardware.Camera.Parameters;
 
@@ -29,19 +27,13 @@
 import com.android.camera.ui.PieRenderer;
 import com.android.gallery3d.R;
 
+import java.util.Locale;
+
 public class NewPhotoMenu extends PieController
         implements CountdownTimerPopup.Listener,
         ListPrefSettingPopup.Listener {
     private static String TAG = "CAM_photomenu";
 
-    private static final int POS_HDR = 0;
-    private static final int POS_EXP = 1;
-    private static final int POS_MORE = 2;
-    private static final int POS_FLASH = 3;
-    private static final int POS_SWITCH = 4;
-    private static final int POS_WB = 1;
-    private static final int POS_SET = 2;
-
     private final String mSettingOff;
 
     private NewPhotoUI mUI;
diff --git a/src/com/android/camera/NewPhotoModule.java b/src/com/android/camera/NewPhotoModule.java
index e5d922a..0121fb5 100644
--- a/src/com/android/camera/NewPhotoModule.java
+++ b/src/com/android/camera/NewPhotoModule.java
@@ -105,6 +105,7 @@
     private static final int START_PREVIEW_DONE = 10;
     private static final int OPEN_CAMERA_FAIL = 11;
     private static final int CAMERA_DISABLED = 12;
+    private static final int CAPTURE_ANIMATION_DONE = 13;
 
     // The subset of parameters we need to update in setCameraParameters().
     private static final int UPDATE_PARAM_INITIALIZE = 1;
@@ -125,7 +126,9 @@
 
     private NewPhotoUI mUI;
 
-    // -1 means camera is not switching.
+    // The activity is going to switch to the specified camera id. This is
+    // needed because texture copy is done in GL thread. -1 means camera is not
+    // switching.
     protected int mPendingSwitchCameraId = -1;
     private boolean mOpenCameraFail;
     private boolean mCameraDisabled;
@@ -169,6 +172,13 @@
         }
     };
 
+    private Runnable mFlashRunnable = new Runnable() {
+        @Override
+        public void run() {
+            animateFlash();
+        }
+    };
+
     private final StringBuilder mBuilder = new StringBuilder();
     private final Formatter mFormatter = new Formatter(mBuilder);
     private final Object[] mFormatterArgs = new Object[1];
@@ -201,7 +211,6 @@
 
     private LocationManager mLocationManager;
 
-    private final ShutterCallback mShutterCallback = new ShutterCallback();
     private final PostViewPictureCallback mPostViewPictureCallback =
             new PostViewPictureCallback();
     private final RawPictureCallback mRawPictureCallback =
@@ -389,6 +398,10 @@
                             R.string.camera_disabled);
                     break;
                 }
+                case CAPTURE_ANIMATION_DONE: {
+                    mUI.enablePreviewThumb(false);
+                    break;
+                }
             }
         }
     }
@@ -687,19 +700,23 @@
         }
     }
 
-    @Override
-    public boolean dispatchTouchEvent(MotionEvent m) {
-        if (mCameraState == SWITCHING_CAMERA) return true;
-        return mUI.dispatchTouchEvent(m);
-    }
-
     private final class ShutterCallback
             implements android.hardware.Camera.ShutterCallback {
+
+        private boolean mAnimateFlash;
+
+        public ShutterCallback(boolean animateFlash) {
+            mAnimateFlash = animateFlash;
+        }
+
         @Override
         public void onShutter() {
             mShutterCallbackTime = System.currentTimeMillis();
             mShutterLag = mShutterCallbackTime - mCaptureStartTime;
             Log.v(TAG, "mShutterLag = " + mShutterLag + "ms");
+            if (mAnimateFlash) {
+                mActivity.runOnUiThread(mFlashRunnable);
+            }
         }
     }
 
@@ -767,7 +784,10 @@
             if (ApiHelper.HAS_SURFACE_TEXTURE && !mIsImageCaptureIntent
                     && mActivity.mShowCameraAppView) {
                 // Finish capture animation
+                mHandler.removeMessages(CAPTURE_ANIMATION_DONE);
                 ((CameraScreenNail) mActivity.mCameraScreenNail).animateSlide();
+                mHandler.sendEmptyMessageDelayed(CAPTURE_ANIMATION_DONE,
+                        CaptureAnimManager.getAnimationDuration());
             } */
             mFocusManager.updateFocusUI(); // Ensure focus indicator is hidden.
             if (!mIsImageCaptureIntent) {
@@ -924,6 +944,9 @@
                 && mActivity.mShowCameraAppView) {
             // Start capture animation.
             ((CameraScreenNail) mActivity.mCameraScreenNail).animateFlash(mDisplayRotation);
+            mUI.enablePreviewThumb(true);
+            mHandler.sendEmptyMessageDelayed(CAPTURE_ANIMATION_DONE,
+                    CaptureAnimManager.getAnimationDuration());
         } */
     }
 
@@ -947,7 +970,7 @@
         }
 
         // Set rotation and gps data.
-        int orientation = (360 - mDisplayRotation) % 360;
+        int orientation;
         // We need to be consistent with the framework orientation (i.e. the
         // orientation of the UI.) when the auto-rotate screen setting is on.
         if (mActivity.isAutoRotateScreen()) {
@@ -961,18 +984,17 @@
         Util.setGpsParameters(mParameters, loc);
         mCameraDevice.setParameters(mParameters);
 
-        mCameraDevice.takePicture2(mShutterCallback, mRawPictureCallback,
-                mPostViewPictureCallback, new JpegPictureCallback(loc),
-                mCameraState, mFocusManager.getFocusState());
-
-        if (!animateBefore) {
-            animateFlash();
-        }
+        mCameraDevice.takePicture2(new ShutterCallback(!animateBefore),
+                mRawPictureCallback, mPostViewPictureCallback,
+                new JpegPictureCallback(loc), mCameraState,
+                mFocusManager.getFocusState());
 
         mNamedImages.nameNewImage(mContentResolver, mCaptureStartTime);
 
         mFaceDetectionStarted = false;
         setCameraState(SNAPSHOT_IN_PROGRESS);
+        UsageStatistics.onEvent(UsageStatistics.COMPONENT_CAMERA,
+                UsageStatistics.ACTION_CAPTURE_DONE, "Photo");
         return true;
     }
 
@@ -1145,10 +1167,6 @@
         if (pressed && !canTakePicture()) return;
 
         if (pressed) {
-            if (mSceneMode == Util.SCENE_MODE_HDR) {
-                mUI.hideSwitcher();
-                //TODO: mActivity.setSwipingEnabled(false);
-            }
             mFocusManager.onShutterDown();
         } else {
             // for countdown mode, we need to postpone the shutter release
@@ -1173,6 +1191,10 @@
         }
         Log.v(TAG, "onShutterButtonClick: mCameraState=" + mCameraState);
 
+        if (mSceneMode == Util.SCENE_MODE_HDR) {
+            mUI.hideSwitcher();
+            //TODO: mActivity.setSwipingEnabled(false);
+        }
         // If the user wants to do a snapshot while the previous one is still
         // in progress, remember the fact and do it after we finish the previous
         // one and re-start the preview. Snapshot in progress also includes the
@@ -1656,7 +1678,12 @@
 
             // Zoom related settings will be changed for different preview
             // sizes, so set and read the parameters to get latest values
-            mCameraDevice.setParameters(mParameters);
+            if (mHandler.getLooper() == Looper.myLooper()) {
+                // On UI thread only, not when camera starts up
+                setupPreview();
+            } else {
+                mCameraDevice.setParameters(mParameters);
+            }
             mParameters = mCameraDevice.getParameters();
         }
         Log.v(TAG, "Preview size is " + optimalSize.width + "x" + optimalSize.height);
diff --git a/src/com/android/camera/NewPhotoUI.java b/src/com/android/camera/NewPhotoUI.java
index caf3d5a..d6e5057 100644
--- a/src/com/android/camera/NewPhotoUI.java
+++ b/src/com/android/camera/NewPhotoUI.java
@@ -105,6 +105,8 @@
     private float mSurfaceTextureUncroppedWidth;
     private float mSurfaceTextureUncroppedHeight;
 
+    private View mPreviewThumb;
+
     private SurfaceTextureSizeChangedListener mSurfaceTextureSizeListener;
     private TextureView mTextureView;
     private Matrix mMatrix = null;
@@ -311,6 +313,14 @@
 
     public void initializeControlByIntent() {
         mBlocker = mRootView.findViewById(R.id.blocker);
+        mPreviewThumb = mActivity.findViewById(R.id.preview_thumb);
+        mPreviewThumb.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                // TODO: go to filmstrip
+                // mActivity.gotoGallery();
+            }
+        });
         mMenuButton = mRootView.findViewById(R.id.menu);
         mMenuButton.setOnClickListener(new OnClickListener() {
             @Override
@@ -434,25 +444,6 @@
     public void setCameraState(int state) {
     }
 
-    // Gestures and touch events
-
-    public boolean dispatchTouchEvent(MotionEvent m) {
-        if (mPopup != null || mSwitcher.showsPopup()) {
-            boolean handled = mRootView.dispatchTouchEvent(m);
-            if (!handled && mPopup != null) {
-                dismissPopup();
-            }
-            return handled;
-        } else if (mGestures != null && mRenderOverlay != null) {
-            if (mGestures.dispatchTouch(m)) {
-                return true;
-            } else {
-                return mRootView.dispatchTouchEvent(m);
-            }
-        }
-        return true;
-    }
-
     public void enableGestures(boolean enable) {
         if (mGestures != null) {
             mGestures.setEnabled(enable);
@@ -511,6 +502,14 @@
         if (!full && mCountDownView != null) mCountDownView.cancelCountDown();
     }
 
+    public void enablePreviewThumb(boolean enabled) {
+        if (enabled) {
+            mPreviewThumb.setVisibility(View.VISIBLE);
+        } else {
+            mPreviewThumb.setVisibility(View.GONE);
+        }
+    }
+
     public boolean removeTopLevelPopup() {
         // Remove the top level popup or dialog box and return true if there's any
         if (mPopup != null) {
@@ -571,6 +570,7 @@
             dismissPopup();
             ret = true;
         }
+        onShowSwitcherPopup();
         return ret;
     }
 
@@ -580,6 +580,7 @@
         Util.fadeIn(mReviewDoneButton);
         mShutterButton.setVisibility(View.INVISIBLE);
         Util.fadeIn(mReviewRetakeButton);
+        pauseFaceDetection();
     }
 
     protected void hidePostCaptureAlert() {
@@ -588,6 +589,7 @@
         Util.fadeOut(mReviewDoneButton);
         mShutterButton.setVisibility(View.VISIBLE);
         Util.fadeOut(mReviewRetakeButton);
+        resumeFaceDetection();
     }
 
     public void setDisplayOrientation(int orientation) {
@@ -645,6 +647,7 @@
     public void onPieOpened(int centerX, int centerY) {
       //TODO:   mActivity.cancelActivityTouchHandling();
       //TODO:    mActivity.setSwipingEnabled(false);
+        dismissPopup(false);
         if (mFaceView != null) {
             mFaceView.setBlockDraw(true);
         }
diff --git a/src/com/android/camera/NewVideoModule.java b/src/com/android/camera/NewVideoModule.java
index 54e3373..87c343b 100644
--- a/src/com/android/camera/NewVideoModule.java
+++ b/src/com/android/camera/NewVideoModule.java
@@ -2099,12 +2099,6 @@
         startPreview();
     }
 
-    @Override
-    public boolean dispatchTouchEvent(MotionEvent m) {
-        if (mSwitchingCamera) return true;
-        return mUI.dispatchTouchEvent(m);
-    }
-
     private void initializeVideoSnapshot() {
         if (mParameters == null) return;
         if (Util.isVideoSnapshotSupported(mParameters) && !mIsVideoCaptureIntent) {
diff --git a/src/com/android/camera/NewVideoUI.java b/src/com/android/camera/NewVideoUI.java
index 71ab669..ffc1c35 100644
--- a/src/com/android/camera/NewVideoUI.java
+++ b/src/com/android/camera/NewVideoUI.java
@@ -475,6 +475,7 @@
     public void onPieOpened(int centerX, int centerY) {
         // TODO: mActivity.cancelActivityTouchHandling();
         // mActivity.setSwipingEnabled(false);
+        dismissPopup(false, true);
     }
 
     @Override
@@ -598,24 +599,6 @@
         return mShutterButton;
     }
 
-    // Gestures and touch events
-
-    public boolean dispatchTouchEvent(MotionEvent m) {
-        if (mPopup != null || mSwitcher.showsPopup()) {
-            boolean handled = mRootView.dispatchTouchEvent(m);
-            if (!handled && mPopup != null) {
-                dismissPopup(false);
-            }
-            return handled;
-        } else if (mGestures != null && mRenderOverlay != null) {
-            if (mGestures.dispatchTouch(m)) {
-                return true;
-            } else {
-                return mRootView.dispatchTouchEvent(m);
-            }
-        }
-        return true;
-    }
     public void setRecordingTime(String text) {
         mRecordingTimeView.setText(text);
     }
diff --git a/src/com/android/camera/ui/NewCameraRootView.java b/src/com/android/camera/ui/NewCameraRootView.java
index abb77b8..bf81925 100644
--- a/src/com/android/camera/ui/NewCameraRootView.java
+++ b/src/com/android/camera/ui/NewCameraRootView.java
@@ -20,7 +20,6 @@
 import android.content.Context;
 import android.content.res.Configuration;
 import android.graphics.Rect;
-import android.os.Debug;
 import android.util.AttributeSet;
 import android.view.Gravity;
 import android.view.MotionEvent;