IMS-VT: Fix size and mirror image issue for preview video.

- Remove the wrong mirror image flip transformation.
- Use proper transform matrix to rotate the preview video
  so that preview image maintains original aspect ratio.
- Initialize mDeviceOrientation from the UI display
  rotation, so that preview image for calls originated
  directly in landscape mode are displayed correctly.

Change-Id: I3d23c698649f8df6e425b7b92ef50d49100134c4
CRs-Fixed: 1050214
diff --git a/InCallUI/src/com/android/incallui/InCallOrientationEventListener.java b/InCallUI/src/com/android/incallui/InCallOrientationEventListener.java
index 3cab6dc..ed10b73 100644
--- a/InCallUI/src/com/android/incallui/InCallOrientationEventListener.java
+++ b/InCallUI/src/com/android/incallui/InCallOrientationEventListener.java
@@ -20,7 +20,9 @@
 import android.content.res.Configuration;
 import android.view.OrientationEventListener;
 import android.hardware.SensorManager;
+import android.view.Display;
 import android.view.Surface;
+import android.view.WindowManager;
 import android.content.pm.ActivityInfo;
 
 /**
@@ -51,7 +53,7 @@
      * within x degrees right or left of the screen orientation angles. If it's not within those
      * ranges, we return SCREEN_ORIENTATION_UNKNOWN and ignore it.
      */
-    private static int SCREEN_ORIENTATION_UNKNOWN = -1;
+    public static int SCREEN_ORIENTATION_UNKNOWN = -1;
 
     // Rotation threshold is 10 degrees. So if the rotation angle is within 10 degrees of any of
     // the above angles, we will notify orientation changed.
@@ -64,8 +66,11 @@
     private static int sCurrentOrientation = SCREEN_ORIENTATION_0;
     private boolean mEnabled = false;
 
+    private static WindowManager sWindowManager = null;
+
     public InCallOrientationEventListener(Context context) {
         super(context);
+        sWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
     }
 
     /**
@@ -175,4 +180,32 @@
     private static boolean isInRightRange(int value, int center, int threshold) {
         return isWithinRange(value, center, center + threshold);
     }
+
+    /**
+     * Returns the current display orientation.
+     * return values 0, 90, 180 and 270
+     * -1 if unknown
+     */
+    public static int getCurrentUiOrientation() {
+        if (sWindowManager == null) return SCREEN_ORIENTATION_UNKNOWN;
+        Display display = sWindowManager.getDefaultDisplay();
+        if (display == null) return SCREEN_ORIENTATION_UNKNOWN;
+        int rotation = display.getRotation();
+        int orientation = SCREEN_ORIENTATION_UNKNOWN;
+        switch (rotation) {
+            case Surface.ROTATION_0:
+                orientation = SCREEN_ORIENTATION_0;
+                break;
+            case Surface.ROTATION_90:
+                orientation = SCREEN_ORIENTATION_90;
+                break;
+            case Surface.ROTATION_180:
+                orientation = SCREEN_ORIENTATION_180;
+                break;
+            case Surface.ROTATION_270:
+                orientation = SCREEN_ORIENTATION_270;
+                break;
+        }
+        return orientation;
+    }
 }
diff --git a/InCallUI/src/com/android/incallui/VideoCallFragment.java b/InCallUI/src/com/android/incallui/VideoCallFragment.java
index a210e6e..fe1422f 100644
--- a/InCallUI/src/com/android/incallui/VideoCallFragment.java
+++ b/InCallUI/src/com/android/incallui/VideoCallFragment.java
@@ -780,13 +780,6 @@
                 }
                 mPreviewVideoContainer.setLayoutParams(containerParams);
             }
-
-            // The width and height are interchanged outside of this method based on the current
-            // orientation, so we can transform using "width", which will be either the width or
-            // the height.
-            Matrix transform = new Matrix();
-            transform.setScale(-1, 1, width/2, 0);
-            preview.setTransform(transform);
         }
     }
 
@@ -813,7 +806,10 @@
                 return;
             }
 
-            preview.setRotation(orientation);
+            // Set transform matrix based on orientation
+            ViewGroup.LayoutParams params = preview.getLayoutParams();
+            setTransformMatrixForRotation(preview, orientation,
+                    params.width, params.height);
         }
     }
 
@@ -969,6 +965,56 @@
     }
 
     /**
+     * Sets the transform matrix to textureview based on orientation so that image
+     * is always up right.
+     */
+    private void setTransformMatrixForRotation(TextureView textureView, int rotation,
+            int width, int height) {
+
+        Matrix matrix = new Matrix();
+
+        if (rotation != InCallOrientationEventListener.SCREEN_ORIENTATION_0) {
+
+            float[] srcArray =  new float[] {
+                    0.f, 0.f, // top left
+                    width, 0.f, // top right
+                    0.f, height, // bottom left
+                    width, height, // bottom right
+            };
+
+            float[] destArray = srcArray;
+
+            // Rotate the image for landscape device orientations
+            if (rotation == InCallOrientationEventListener.SCREEN_ORIENTATION_90) {
+                destArray = new float[] {
+                        0.f, height, // top left
+                        0.f, 0.f, // top right
+                        width, height, // bottom left
+                        width, 0.f, // bottom right
+                };
+            } else if (rotation == InCallOrientationEventListener.SCREEN_ORIENTATION_270) {
+                destArray = new float[] {
+                        width, 0.f, // top left
+                        width, height, // top right
+                        0.f, 0.f, // bottom left
+                        0.f, height, // bottom right
+                };
+            } else if (rotation == InCallOrientationEventListener.SCREEN_ORIENTATION_180) {
+                // Flip the image vertically and horizontally for reverse portrait
+                destArray = new float[] {
+                        width, height, // top left
+                        0.f, height, // top right
+                        width, 0.f, // bottom left
+                        0.f, 0.f, // bottom right
+                };
+            }
+
+            matrix.setPolyToPoly(srcArray, 0, destArray, 0, 4);
+        }
+        textureView.setTransform(matrix);
+    }
+
+    /**
      * Resizes a surface so that it has the same size as the full screen and so that it is
      * centered vertically below the call card.
      *
diff --git a/InCallUI/src/com/android/incallui/VideoCallPresenter.java b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
index 6a04de2..92bd5e5 100644
--- a/InCallUI/src/com/android/incallui/VideoCallPresenter.java
+++ b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
@@ -284,6 +284,13 @@
             return;
         }
 
+        // Initialize current ui rotation
+        final int uiOrientation = InCallOrientationEventListener.getCurrentUiOrientation();
+        Log.d(this, "onUiReady: uiOrientation = " + uiOrientation);
+        if (uiOrientation != InCallOrientationEventListener.SCREEN_ORIENTATION_UNKNOWN) {
+            mDeviceOrientation = uiOrientation;
+        }
+
         // Register for call state changes last
         InCallPresenter.getInstance().addListener(this);
         InCallPresenter.getInstance().addDetailsListener(this);
@@ -1241,6 +1248,7 @@
 
         mPreviewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
         changePreviewDimensions(width, height);
+        ui.setPreviewRotation(mDeviceOrientation);
 
         if (shallTransmitStaticImage()) {
             setPauseImage();