Merge "Send an message to decouple the gcam module change." into gb-ub-photos-carlsbad
diff --git a/res/mipmap-hdpi/ic_launcher_camera.png b/res/mipmap-hdpi/ic_launcher_camera.png
index 8781ace..c1ebe77 100644
--- a/res/mipmap-hdpi/ic_launcher_camera.png
+++ b/res/mipmap-hdpi/ic_launcher_camera.png
Binary files differ
diff --git a/res/mipmap-mdpi/ic_launcher_camera.png b/res/mipmap-mdpi/ic_launcher_camera.png
index 963728d..20f1499 100644
--- a/res/mipmap-mdpi/ic_launcher_camera.png
+++ b/res/mipmap-mdpi/ic_launcher_camera.png
Binary files differ
diff --git a/res/mipmap-xhdpi/ic_launcher_camera.png b/res/mipmap-xhdpi/ic_launcher_camera.png
index 01f550d..972ee40 100644
--- a/res/mipmap-xhdpi/ic_launcher_camera.png
+++ b/res/mipmap-xhdpi/ic_launcher_camera.png
Binary files differ
diff --git a/res/mipmap-xxhdpi/ic_launcher_camera.png b/res/mipmap-xxhdpi/ic_launcher_camera.png
index 4cc2e72..4cc58ee 100644
--- a/res/mipmap-xxhdpi/ic_launcher_camera.png
+++ b/res/mipmap-xxhdpi/ic_launcher_camera.png
Binary files differ
diff --git a/res/mipmap-xxxhdpi/ic_launcher_camera.png b/res/mipmap-xxxhdpi/ic_launcher_camera.png
index 25d4c47..00de613 100644
--- a/res/mipmap-xxxhdpi/ic_launcher_camera.png
+++ b/res/mipmap-xxxhdpi/ic_launcher_camera.png
Binary files differ
diff --git a/src/com/android/camera/PhotoUI.java b/src/com/android/camera/PhotoUI.java
index aa86853..b966b7b 100644
--- a/src/com/android/camera/PhotoUI.java
+++ b/src/com/android/camera/PhotoUI.java
@@ -767,8 +767,6 @@
             mLocationDialog.dismiss();
         }
         mLocationDialog = null;
-        mPreviewWidth = 0;
-        mPreviewHeight = 0;
     }
 
     public void initDisplayChangeListener() {
@@ -845,6 +843,7 @@
 
     @Override
     public void onDisplayChanged() {
+        Log.d(TAG, "Device flip detected.");
         mCameraControls.checkLayoutFlip();
         mController.updateCameraOrientation();
     }
diff --git a/src/com/android/camera/ui/RotatableLayout.java b/src/com/android/camera/ui/RotatableLayout.java
index 04c3633..6867e6a 100644
--- a/src/com/android/camera/ui/RotatableLayout.java
+++ b/src/com/android/camera/ui/RotatableLayout.java
@@ -19,11 +19,8 @@
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
-import android.graphics.Point;
 import android.util.AttributeSet;
-import android.view.Display;
 import android.view.Gravity;
-import android.view.Surface;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.FrameLayout;
@@ -73,14 +70,16 @@
         // we need to rotate the view if necessary. After that, onConfigurationChanged
         // call will track all the subsequent device rotation.
         if (mPrevRotation == UNKOWN_ORIENTATION) {
-            calculateDefaultOrientation();
+            mIsDefaultToPortrait = CameraUtil.isDefaultToPortrait((Activity) getContext());
             if (mIsDefaultToPortrait) {
                 // Natural orientation for tablet is landscape
                 mPrevRotation =  mInitialOrientation == Configuration.ORIENTATION_PORTRAIT ?
                         0 : 90;
             } else {
+                // When tablet orientation is 0 or 270 (i.e. getUnifiedOrientation
+                // = 0 or 90), we load the layout resource without any rotation.
                 mPrevRotation =  mInitialOrientation == Configuration.ORIENTATION_LANDSCAPE ?
-                        0 : 90;
+                        0 : 270;
             }
 
             // check if there is any rotation before the view is attached to window
@@ -88,22 +87,6 @@
         }
     }
 
-    private void calculateDefaultOrientation() {
-        Display currentDisplay = getDisplay();
-        Point displaySize = new Point();
-        currentDisplay.getSize(displaySize);
-        int orientation = currentDisplay.getRotation();
-        int naturalWidth, naturalHeight;
-        if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180) {
-            naturalWidth = displaySize.x;
-            naturalHeight = displaySize.y;
-        } else {
-            naturalWidth = displaySize.y;
-            naturalHeight = displaySize.x;
-        }
-        mIsDefaultToPortrait = naturalWidth < naturalHeight;
-    }
-
     private void rotateIfNeeded() {
         if (mPrevRotation == UNKOWN_ORIENTATION) {
             return;
diff --git a/src/com/android/camera/util/CameraUtil.java b/src/com/android/camera/util/CameraUtil.java
index 458ae04..eff0c40 100644
--- a/src/com/android/camera/util/CameraUtil.java
+++ b/src/com/android/camera/util/CameraUtil.java
@@ -43,6 +43,7 @@
 import android.util.DisplayMetrics;
 import android.util.Log;
 import android.util.TypedValue;
+import android.view.Display;
 import android.view.OrientationEventListener;
 import android.view.Surface;
 import android.view.View;
@@ -411,6 +412,28 @@
         return 0;
     }
 
+    /**
+     * Calculate the default orientation of the device based on the width and
+     * height of the display when rotation = 0 (i.e. natural width and height)
+     * @param activity the activity context
+     * @return whether the default orientation of the device is portrait
+     */
+    public static boolean isDefaultToPortrait(Activity activity) {
+        Display currentDisplay = activity.getWindowManager().getDefaultDisplay();
+        Point displaySize = new Point();
+        currentDisplay.getSize(displaySize);
+        int orientation = currentDisplay.getRotation();
+        int naturalWidth, naturalHeight;
+        if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180) {
+            naturalWidth = displaySize.x;
+            naturalHeight = displaySize.y;
+        } else {
+            naturalWidth = displaySize.y;
+            naturalHeight = displaySize.x;
+        }
+        return naturalWidth < naturalHeight;
+    }
+
     public static int getDisplayOrientation(int degrees, int cameraId) {
         // See android.hardware.Camera.setDisplayOrientation for
         // documentation.