Keep camera controls on the same physical side

Change-Id: I09c50650c77a89fadfeb376564ef43e750994f8a
diff --git a/src/com/android/camera/CameraActivity.java b/src/com/android/camera/CameraActivity.java
index eed3470..b1f0847 100644
--- a/src/com/android/camera/CameraActivity.java
+++ b/src/com/android/camera/CameraActivity.java
@@ -61,7 +61,6 @@
     private View mCameraControls;
     private View mControlsBackground;
     private View mPieMenuButton;
-    private View mSwitcherControl;
     private Drawable[] mDrawables;
     private int mCurrentModuleIndex;
     private MotionEvent mDown;
@@ -122,8 +121,7 @@
         mCameraControls = findViewById(R.id.camera_controls);
         mShutter = (ShutterButton) findViewById(R.id.shutter_button);
         mSwitcher = (CameraSwitcher) findViewById(R.id.camera_switcher);
-        mPieMenuButton = findViewById(R.id.menu_button);
-        mSwitcherControl = findViewById(R.id.switcher_control);
+        mPieMenuButton = findViewById(R.id.menu);
         int totaldrawid = (LightCycleHelper.hasLightCycleCapture(this)
                                 ? DRAW_IDS.length : DRAW_IDS.length - 1);
         if (!ApiHelper.HAS_OLD_PANORAMA) totaldrawid--;
@@ -222,11 +220,8 @@
                 mCurrentModule = LightCycleHelper.createPanoramaModule();
                 break;
         }
-        if (mCurrentModule.needsPieMenu()) {
-            mPieMenuButton.setVisibility(View.VISIBLE);
-        } else {
-            mPieMenuButton.setVisibility(View.INVISIBLE);
-        }
+        showPieMenuButton(mCurrentModule.needsPieMenu());
+
         openModule(mCurrentModule, canReuse);
         mCurrentModule.onOrientationChanged(mLastRawOrientation);
         if (mMediaSaveService != null) {
@@ -236,6 +231,18 @@
         getCameraScreenNail().setOnFrameDrawnOneShot(mOnFrameDrawn);
     }
 
+    public void showPieMenuButton(boolean show) {
+        if (show) {
+            findViewById(R.id.blocker).setVisibility(View.VISIBLE);
+            findViewById(R.id.menu).setVisibility(View.VISIBLE);
+            findViewById(R.id.on_screen_indicators).setVisibility(View.VISIBLE);
+        } else {
+            findViewById(R.id.blocker).setVisibility(View.INVISIBLE);
+            findViewById(R.id.menu).setVisibility(View.INVISIBLE);
+            findViewById(R.id.on_screen_indicators).setVisibility(View.INVISIBLE);
+        }
+    }
+
     private Runnable mOnFrameDrawn = new Runnable() {
 
         @Override
@@ -313,17 +320,23 @@
         ViewGroup appRoot = (ViewGroup) findViewById(R.id.content);
         boolean landscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE);
         FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) appRoot.getLayoutParams();
+        int offset = getResources().getDimensionPixelSize(R.dimen.margin_systemui_offset);
+        int navBarHeight = getResources().getDimensionPixelSize(R.dimen.navigation_bar_height);
         if (landscape) {
-            lp.rightMargin = getResources().getDimensionPixelSize(R.dimen.margin_systemui_offset);
+            lp.rightMargin = offset;
         } else {
             lp.rightMargin = 0;
         }
         appRoot.setLayoutParams(lp);
 
-        // Reset the background after rotation
-        mControlsBackground.setBackgroundResource(0);  // remove the current background
-        mControlsBackground.setBackgroundResource(R.drawable.switcher_bg);
-
+        // Set padding to move camera controls away from the edge of the screen
+        // so that they are in the same place as if there was a navigation bar between
+        // the screen edge and the controls
+        if (landscape) {
+            mCameraControls.setPadding(navBarHeight, 0, 0, 0);
+        } else {
+            mCameraControls.setPadding(0, navBarHeight, 0, 0);
+        }
         mCurrentModule.onConfigurationChanged(config);
     }
 
@@ -466,7 +479,7 @@
         if ((mSwitcher != null) && mSwitcher.showsPopup() && !mSwitcher.isInsidePopup(m)) {
             return mSwitcher.onTouch(null, m);
         } else {
-            return mSwitcherControl.dispatchTouchEvent(m)
+            return mCameraControls.dispatchTouchEvent(m)
                     || mCurrentModule.dispatchTouchEvent(m);
         }
     }
diff --git a/src/com/android/camera/PhotoModule.java b/src/com/android/camera/PhotoModule.java
index 664246a..9a45461 100644
--- a/src/com/android/camera/PhotoModule.java
+++ b/src/com/android/camera/PhotoModule.java
@@ -54,6 +54,7 @@
 import android.view.View;
 import android.view.ViewStub;
 import android.view.View.OnClickListener;
+import android.view.View.OnLayoutChangeListener;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import android.widget.FrameLayout;
@@ -206,6 +207,14 @@
         }
     };
 
+    private final View.OnLayoutChangeListener mLayoutChangeListener =
+            new View.OnLayoutChangeListener() {
+        @Override
+        public void onLayoutChange(View v, int left, int top, int right,
+                int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
+            onScreenSizeChanged(right - left, bottom - top);
+        }
+    };
     private final StringBuilder mBuilder = new StringBuilder();
     private final Formatter mFormatter = new Formatter(mBuilder);
     private final Object[] mFormatterArgs = new Object[1];
@@ -461,6 +470,7 @@
 
         mActivity.getLayoutInflater().inflate(R.layout.photo_module,
                 (ViewGroup) mRootView, true);
+        mRootView.addOnLayoutChangeListener(mLayoutChangeListener);
         if (ApiHelper.HAS_FACE_DETECTION) {
             ViewStub faceViewStub = (ViewStub) mRootView
                     .findViewById(R.id.face_view_stub);
@@ -586,8 +596,8 @@
         initializePhotoControl();
 
         // These depend on camera parameters.
-        int width = mActivity.getWindowManager().getDefaultDisplay().getWidth();
-        int height = mActivity.getWindowManager().getDefaultDisplay().getHeight();
+        int width = mRootView.getWidth();
+        int height = mRootView.getHeight();
         mFocusManager.setPreviewSize(width, height);
         // Full-screen screennail
         if (Util.getDisplayRotation(mActivity) % 180 == 0) {
@@ -604,6 +614,16 @@
         onFullScreenChanged(mActivity.isInCameraApp());
     }
 
+    public void onScreenSizeChanged(int width, int height) {
+        if (mFocusManager != null) mFocusManager.setPreviewSize(width, height);
+        // Full-screen screennail
+        if (Util.getDisplayRotation(mActivity) % 180 == 0) {
+            ((CameraScreenNail) mActivity.mCameraScreenNail).setPreviewFrameLayoutSize(width, height);
+        } else {
+            ((CameraScreenNail) mActivity.mCameraScreenNail).setPreviewFrameLayoutSize(height, width);
+        }
+    }
+
     private void initializePhotoControl() {
         loadCameraPreferences();
         if (mPhotoControl != null) {
@@ -1618,6 +1638,7 @@
         mHandler.removeMessages(OPEN_CAMERA_FAIL);
         mHandler.removeMessages(CAMERA_DISABLED);
 
+        mRootView.removeOnLayoutChangeListener(mLayoutChangeListener);
         mPendingSwitchCameraId = -1;
         if (mFocusManager != null) mFocusManager.removeMessages();
         MediaSaveService s = mActivity.getMediaSaveService();
diff --git a/src/com/android/camera/ShutterButton.java b/src/com/android/camera/ShutterButton.java
index 41775a1..228fc51 100755
--- a/src/com/android/camera/ShutterButton.java
+++ b/src/com/android/camera/ShutterButton.java
@@ -130,10 +130,4 @@
         }
         return result;
     }
-
-    @Override
-    public void onConfigurationChanged(Configuration config) {
-        super.onConfigurationChanged(config);
-        RotatableLayout.rotate(this, config.orientation == Configuration.ORIENTATION_PORTRAIT);
-    }
 }
diff --git a/src/com/android/camera/ui/CameraSwitcher.java b/src/com/android/camera/ui/CameraSwitcher.java
index 3fa4a01..897729b 100644
--- a/src/com/android/camera/ui/CameraSwitcher.java
+++ b/src/com/android/camera/ui/CameraSwitcher.java
@@ -19,11 +19,13 @@
 import android.animation.Animator;
 import android.animation.Animator.AnimatorListener;
 import android.animation.AnimatorListenerAdapter;
+import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.graphics.Canvas;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
+import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
@@ -33,6 +35,7 @@
 import android.widget.FrameLayout.LayoutParams;
 import android.widget.LinearLayout;
 
+import com.android.camera.Util;
 import com.android.gallery3d.R;
 import com.android.gallery3d.common.ApiHelper;
 
@@ -120,6 +123,13 @@
                 (ViewGroup) getParent());
         LinearLayout content = (LinearLayout) mParent.findViewById(R.id.content);
         mPopup = content;
+        // Set the gravity of the popup, so that it shows up at the right position
+        // on screen
+        LayoutParams lp = ((LayoutParams) mPopup.getLayoutParams());
+        lp.gravity = ((LayoutParams) mParent.findViewById(R.id.camera_switcher)
+                .getLayoutParams()).gravity;
+        mPopup.setLayoutParams(lp);
+
         mPopup.setVisibility(View.INVISIBLE);
         mNeedsAnimationSetup = true;
         for (int i = mDrawIds.length - 1; i >= 0; i--) {
@@ -224,15 +234,22 @@
     }
 
     private void updateInitialTranslations() {
-        if (getResources().getConfiguration().orientation
-                == Configuration.ORIENTATION_PORTRAIT) {
+        int orientation = Util.getDisplayRotation((Activity) getContext());
+        if (orientation == 0) {
             mTranslationX = -getWidth() / 2;
             mTranslationY = getHeight();
-        } else {
+        } else if (orientation == 90) {
             mTranslationX = getWidth();
             mTranslationY = getHeight() / 2;
+        } else if (orientation == 180) {
+            mTranslationX = getWidth();
+            mTranslationY = -getHeight() / 2;
+        } else {
+            mTranslationX = -getWidth();
+            mTranslationY = -getHeight() / 2;
         }
     }
+
     private void popupAnimationSetup() {
         if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
             return;
diff --git a/src/com/android/camera/ui/RotatableLayout.java b/src/com/android/camera/ui/RotatableLayout.java
index 9c5ebd3..4edec5d 100644
--- a/src/com/android/camera/ui/RotatableLayout.java
+++ b/src/com/android/camera/ui/RotatableLayout.java
@@ -16,13 +16,17 @@
 
 package com.android.camera.ui;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.util.AttributeSet;
 import android.view.Gravity;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.FrameLayout;
 
+import com.android.camera.Util;
+
 /* RotatableLayout rotates itself as well as all its children when orientation
  * changes. Specifically, when going from portrait to landscape, camera
  * controls move from the bottom of the screen to right side of the screen
@@ -33,6 +37,8 @@
 
 public class RotatableLayout extends FrameLayout {
 
+    private static final String TAG = "RotatableLayout";
+    private int mPrevRotation;
     public RotatableLayout(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
     }
@@ -46,11 +52,24 @@
     }
 
     @Override
+    public void onFinishInflate() { // get initial orientation
+        mPrevRotation = Util.getDisplayRotation((Activity) getContext());
+    }
+
+    @Override
     public void onConfigurationChanged(Configuration config) {
         super.onConfigurationChanged(config);
-        // rotate the layout itself and all its children
-        boolean clockwise = (config.orientation == Configuration.ORIENTATION_PORTRAIT);
-        rotate(this, clockwise);
+        // Change the size of the layout
+        ViewGroup.LayoutParams lp = getLayoutParams();
+        int width = lp.width;
+        int height = lp.height;
+        lp.height = width;
+        lp.width = height;
+        setLayoutParams(lp);
+        // rotate all the children
+        int rotation = Util.getDisplayRotation((Activity) getContext());
+        boolean clockwise = isClockWiseRotation(mPrevRotation, rotation);
+        mPrevRotation = rotation;
         int childCount = getChildCount();
         for (int i = 0; i < childCount; i++) {
             View child = getChildAt(i);
@@ -58,6 +77,13 @@
         }
     }
 
+    public static boolean isClockWiseRotation(int prevRotation, int currentRotation) {
+        if (prevRotation == (currentRotation + 90) % 360) {
+            return true;
+        }
+        return false;
+    }
+
     public static void rotate(View view, boolean isClockwise) {
         if (isClockwise) {
             rotateClockwise(view);