Merge "Don't access mParameters dirty bit in other thread" into gb-ub-photos-bryce
diff --git a/src/com/android/camera/CameraActivity.java b/src/com/android/camera/CameraActivity.java
index 3813cce..1215cec 100644
--- a/src/com/android/camera/CameraActivity.java
+++ b/src/com/android/camera/CameraActivity.java
@@ -118,7 +118,6 @@
 
     public void init() {
         boolean landscape = Util.getDisplayRotation(this) % 180 == 90;
-        setMargins(landscape);
         mControlsBackground = findViewById(R.id.blocker);
         mCameraControls = findViewById(R.id.camera_controls);
         mShutter = (ShutterButton) findViewById(R.id.shutter_button);
@@ -318,24 +317,9 @@
     @Override
     public void onConfigurationChanged(Configuration config) {
         super.onConfigurationChanged(config);
-        boolean landscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE);
-        setMargins(landscape);
         mCurrentModule.onConfigurationChanged(config);
     }
 
-    private void setMargins(boolean landscape) {
-        ViewGroup appRoot = (ViewGroup) findViewById(R.id.content);
-        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) appRoot.getLayoutParams();
-        int navBarWidth = getResources().getDimensionPixelSize(R.dimen.navigation_bar_width);
-        int navBarHeight = getResources().getDimensionPixelSize(R.dimen.navigation_bar_height);
-        if (landscape) {
-            lp.setMargins(navBarHeight, 0, navBarHeight - navBarWidth, 0);
-        } else {
-            lp.setMargins(0, navBarHeight, 0, 0);
-        }
-        appRoot.setLayoutParams(lp);
-    }
-
     @Override
     public void onPause() {
         mPaused = true;
diff --git a/src/com/android/camera/PhotoUI.java b/src/com/android/camera/PhotoUI.java
index 1af870a..29ebd6a 100644
--- a/src/com/android/camera/PhotoUI.java
+++ b/src/com/android/camera/PhotoUI.java
@@ -115,6 +115,8 @@
                 h = width;
             }
             if (mPreviewWidth != w || mPreviewHeight != h) {
+                mPreviewWidth = w;
+                mPreviewHeight = h;
                 mController.onScreenSizeChanged(width, height, w, h);
             }
         }
diff --git a/src/com/android/camera/ui/CameraControls.java b/src/com/android/camera/ui/CameraControls.java
new file mode 100644
index 0000000..a7d1f21
--- /dev/null
+++ b/src/com/android/camera/ui/CameraControls.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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.widget.FrameLayout.LayoutParams;
+
+import com.android.camera.Util;
+import com.android.gallery3d.R;
+
+public class CameraControls extends RotatableLayout
+{
+    private View mBackgroundView;
+    public CameraControls(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public CameraControls(Context context) {
+        super(context);
+    }
+
+    @Override
+    public void onConfigurationChanged(Configuration config) {
+        super.onConfigurationChanged(config);
+        adjustBackground();
+    }
+
+    @Override
+    public void onFinishInflate() {
+        super.onFinishInflate();
+        mBackgroundView = findViewById(R.id.blocker);
+    }
+
+    // In reverse landscape and reverse portrait, camera controls will be laid out
+    // on the wrong side of the screen. We need to make adjustment to move the controls
+    // to the USB side
+    public void adjustControlsToRightPosition() {
+        Configuration config = getResources().getConfiguration();
+        int orientation = Util.getDisplayRotation((Activity) getContext());
+        if (orientation == 270 && config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
+            flipChildren();
+        }
+        if (orientation == 180 && config.orientation == Configuration.ORIENTATION_PORTRAIT) {
+            flipChildren();
+        }
+        adjustBackground();
+    }
+
+    private void adjustBackground() {
+        // remove current drawable and reset rotation
+        mBackgroundView.setBackgroundDrawable(null);
+        mBackgroundView.setRotationX(0);
+        mBackgroundView.setRotationY(0);
+        // if the switcher background is top aligned we need to flip the background
+        // drawable vertically; if left aligned, flip horizontally
+        int gravity = ((LayoutParams) mBackgroundView.getLayoutParams()).gravity;
+        if ((gravity & Gravity.TOP) == Gravity.TOP) {
+            mBackgroundView.setRotationX(180);
+        } else if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
+            mBackgroundView.setRotationY(180);
+        }
+        mBackgroundView.setBackgroundResource(R.drawable.switcher_bg);
+    }
+}
diff --git a/src/com/android/camera/ui/CameraRootView.java b/src/com/android/camera/ui/CameraRootView.java
new file mode 100644
index 0000000..cce6495
--- /dev/null
+++ b/src/com/android/camera/ui/CameraRootView.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.camera.ui;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.graphics.Rect;
+import android.util.AttributeSet;
+import android.view.Gravity;
+import android.view.View;
+import android.widget.FrameLayout;
+import android.widget.RelativeLayout;
+
+import com.android.camera.Util;
+import com.android.gallery3d.R;
+
+public class CameraRootView extends RelativeLayout
+    implements RotatableLayout.RotationListener {
+
+    private int mOffset = 0;
+    public CameraRootView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        // Layout the window as if we did not need navigation bar
+        setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
+                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
+    }
+
+    @Override
+    protected boolean fitSystemWindows(Rect insets) {
+        super.fitSystemWindows(insets);
+        // insets include status bar, navigation bar, etc
+        // In this case, we are only concerned with the size of nav bar
+        if (mOffset > 0) return true;
+        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
+        if (insets.bottom > 0) {
+            mOffset = insets.bottom;
+        } else if (insets.right > 0) {
+            mOffset = insets.right;
+        }
+        Configuration config = getResources().getConfiguration();
+        if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
+            lp.setMargins(0, 0, 0, mOffset);
+        } else if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
+            lp.setMargins(0, 0, mOffset, 0);
+        }
+        CameraControls controls = (CameraControls) findViewById(R.id.camera_controls);
+        if (controls != null) {
+            controls.setRotationListener(this);
+            controls.adjustControlsToRightPosition();
+        }
+        return true;
+    }
+
+    @Override
+    public void onRotation(int rotation) {
+        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
+        int b = lp.bottomMargin;
+        int t = lp.topMargin;
+        int l = lp.leftMargin;
+        int r = lp.rightMargin;
+        rotation = (rotation + 360) % 360;
+        if (rotation == 90) {
+            lp.setMargins(b, l, t, r);
+        } else if (rotation == 270) {
+            lp.setMargins(t, r, b, l);
+        } else if (rotation == 180) {
+            lp.setMargins(r, b, l, t);
+        }
+    }
+}
diff --git a/src/com/android/camera/ui/RotatableLayout.java b/src/com/android/camera/ui/RotatableLayout.java
index 4edec5d..6836b39 100644
--- a/src/com/android/camera/ui/RotatableLayout.java
+++ b/src/com/android/camera/ui/RotatableLayout.java
@@ -39,6 +39,10 @@
 
     private static final String TAG = "RotatableLayout";
     private int mPrevRotation;
+    private RotationListener mListener = null;
+    public interface RotationListener {
+        public void onRotation(int rotation);
+    }
     public RotatableLayout(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
     }
@@ -53,12 +57,15 @@
 
     @Override
     public void onFinishInflate() { // get initial orientation
+        super.onFinishInflate();
         mPrevRotation = Util.getDisplayRotation((Activity) getContext());
     }
 
     @Override
     public void onConfigurationChanged(Configuration config) {
         super.onConfigurationChanged(config);
+        int rotation = Util.getDisplayRotation((Activity) getContext());
+        boolean clockwise = isClockWiseRotation(mPrevRotation, rotation);
         // Change the size of the layout
         ViewGroup.LayoutParams lp = getLayoutParams();
         int width = lp.width;
@@ -66,15 +73,33 @@
         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;
+        rotateChildren(clockwise);
+    }
+
+    protected void rotateChildren(boolean clockwise) {
         int childCount = getChildCount();
         for (int i = 0; i < childCount; i++) {
             View child = getChildAt(i);
             rotate(child, clockwise);
         }
+        if (mListener != null) mListener.onRotation(clockwise ? 90 : 270);
+    }
+
+    protected void flipChildren() {
+        mPrevRotation = Util.getDisplayRotation((Activity) getContext());
+        int childCount = getChildCount();
+        for (int i = 0; i < childCount; i++) {
+            View child = getChildAt(i);
+            flip(child);
+        }
+        if (mListener != null) mListener.onRotation(180);
+    }
+
+    public void setRotationListener(RotationListener listener) {
+        mListener = listener;
     }
 
     public static boolean isClockWiseRotation(int prevRotation, int currentRotation) {
@@ -181,4 +206,10 @@
         lp.height = width;
         view.setLayoutParams(lp);
     }
+
+    // Rotate a given view 180 degrees
+    public static void flip(View view) {
+        rotateClockwise(view);
+        rotateClockwise(view);
+    }
 }
\ No newline at end of file
diff --git a/src/com/android/camera/ui/SwitcherBackgroundView.java b/src/com/android/camera/ui/SwitcherBackgroundView.java
deleted file mode 100644
index 710412c..0000000
--- a/src/com/android/camera/ui/SwitcherBackgroundView.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-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.widget.FrameLayout.LayoutParams;
-
-import com.android.camera.Util;
-import com.android.gallery3d.R;
-
-/*
- * This is a simple view that has a gradient background. The background
- * needs to rotate when orientation changes, so that the side of the drawable
- * that is dark is always aligned to the side of the screen, and the side that is
- * closer to the center of the screen is transparent.
- * */
-public class SwitcherBackgroundView extends View
-{
-    public SwitcherBackgroundView(Context context, AttributeSet attrs) {
-        super(context, attrs);
-        setBackgroundResource(R.drawable.switcher_bg);
-    }
-
-    public SwitcherBackgroundView(Context context) {
-        super(context);
-        setBackgroundResource(R.drawable.switcher_bg);
-    }
-    @Override
-    public void onConfigurationChanged(Configuration config) {
-        super.onConfigurationChanged(config);
-        // remove current drawable and reset rotation
-        setBackgroundDrawable(null);
-        setRotationX(0);
-        setRotationY(0);
-        // if the switcher background is top aligned we need to flip the background
-        // drawable vertically; if left aligned, flip horizontally
-        int gravity = ((LayoutParams) getLayoutParams()).gravity;
-        if ((gravity & Gravity.TOP) == Gravity.TOP) {
-            setRotationX(180);
-        } else if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
-            setRotationY(180);
-        }
-        setBackgroundResource(R.drawable.switcher_bg);
-    }
-}
diff --git a/src/com/android/gallery3d/filtershow/imageshow/ImageDraw.java b/src/com/android/gallery3d/filtershow/imageshow/ImageDraw.java
index 6c3417a..8fcc028 100644
--- a/src/com/android/gallery3d/filtershow/imageshow/ImageDraw.java
+++ b/src/com/android/gallery3d/filtershow/imageshow/ImageDraw.java
@@ -83,8 +83,8 @@
     float[] mTmpPoint = new float[2]; // so we do not malloc
     @Override
     public boolean onTouchEvent(MotionEvent event) {
-        boolean ret = super.onTouchEvent(event);
         if (event.getPointerCount() > 1) {
+            boolean ret = super.onTouchEvent(event);
             if (mFRep.getCurrentDrawing() != null) {
                 mFRep.clearCurrentSection();
                 mEditorDraw.commitLocalRepresentation();
@@ -93,7 +93,7 @@
         }
         if (event.getAction() != MotionEvent.ACTION_DOWN) {
             if (mFRep.getCurrentDrawing() == null) {
-                return ret;
+                return super.onTouchEvent(event);
             }
         }