IMS: Correctly set preview surface dimensions

If preview is to be displayed in portrait window,
set preview buffer dimensions (i.e. negotiated width/height
received with PARAM_READY_EVT) to landscape and vice-versa

CRs-Fixed: 1105902
Change-Id: Ic6fa8355d74a5a297a04cbe4b870c8605d23f48e
diff --git a/InCallUI/src/com/android/incallui/VideoCallFragment.java b/InCallUI/src/com/android/incallui/VideoCallFragment.java
index 4e55fd7..9292110 100644
--- a/InCallUI/src/com/android/incallui/VideoCallFragment.java
+++ b/InCallUI/src/com/android/incallui/VideoCallFragment.java
@@ -398,7 +398,11 @@
                     + " mSurfaceId =" + mSurfaceId + " mWidth " + width + " mHeight=" + height);
             if (width != DIMENSIONS_NOT_SET && height != DIMENSIONS_NOT_SET
                     && mSavedSurfaceTexture != null) {
-                mSavedSurfaceTexture.setDefaultBufferSize(width, height);
+                if (mSurfaceId == SURFACE_PREVIEW) {
+                    mPresenter.setPreviewSurfaceSize(width, height);
+                } else {
+                    mSavedSurfaceTexture.setDefaultBufferSize(width, height);
+                }
                 mSavedSurface = new Surface(mSavedSurfaceTexture);
                 return true;
             }
diff --git a/InCallUI/src/com/android/incallui/VideoCallPresenter.java b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
index c1e4f2e..324c786 100644
--- a/InCallUI/src/com/android/incallui/VideoCallPresenter.java
+++ b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
@@ -1098,7 +1098,9 @@
             }
 
             if (showOutgoingVideo) {
-                setPreviewSize(mDeviceOrientation, mPreviewAspectRatio);
+                if ((SystemProperties.getInt(PROP_DISABLE_VIDEOCALL_PIP_MODE, 0) == 1)) {
+                    setPreviewSize(mDeviceOrientation, mPreviewAspectRatio);
+                }
                 if (QtiCallUtils.shallShowStaticImageUi(mContext)) {
                     maybeLoadPreConfiguredImageAsync();
                 }
@@ -1300,6 +1302,37 @@
         }
     }
 
+    private boolean isLandscapeOrientation(final int orientation) {
+        return (orientation == InCallOrientationEventListener.SCREEN_ORIENTATION_90 ||
+                orientation == InCallOrientationEventListener.SCREEN_ORIENTATION_270);
+    }
+
+    public void setPreviewSurfaceSize(int width, int height) {
+        VideoCallUi ui = getUi();
+        if (ui == null) {
+            return;
+        }
+
+        final int orientation = mDeviceOrientation;
+        Log.d(this, "setPreviewSurfaceSize orientation: " + orientation +
+                " width = " + width + " height = " + height);
+
+        final boolean isPortrait = width < height;
+        int w = width;
+        int h = height;
+
+        if ((isLandscapeOrientation(orientation) && !isPortrait) ||
+                !isLandscapeOrientation(orientation) && isPortrait) {
+            /* preview video needs to be displayed in landscape window so set
+               portrait preview surface size and vice-versa */
+            width = h;
+            height = w;
+        }
+
+        Log.d(this, "setPreviewSurfaceSize final width: " + width + " final height: " + height);
+        ui.setPreviewSurfaceSize(width, height);
+    }
+
     /**
      * Changes the dimensions of the preview surface.
      *
@@ -1312,9 +1345,6 @@
             return;
         }
 
-        // Resize the surface used to display the preview video
-        ui.setPreviewSurfaceSize(width, height);
-
         // Configure the preview surface to the correct aspect ratio.
         float aspectRatio = 1.0f;
         if (width > 0 && height > 0) {
@@ -1326,6 +1356,9 @@
         // Resize the textureview housing the preview video and rotate it appropriately based on
         // the device orientation
         setPreviewSize(mDeviceOrientation, mPreviewAspectRatio);
+
+        // Resize the surface used to display the preview video
+        setPreviewSurfaceSize(width, height);
     }
 
     /**