Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9e0d3a38 authored by Tyler Gunn's avatar Tyler Gunn
Browse files

Fix orientation of preview surface when device rotated.

- Cleaned up some unused methods in InCallActivity.
- In onDeviceOrientationChanged, handle orientatoin change by resizing
surface.
- Fixed setPreviewSize which was not rotating the surface the appropriate
way (ensured it was setting a transform on the surface, and also made sure
that the correct dimensions were being passed in).

Bug: 19850018
Change-Id: Ia9324ce6bad5a797839e69ba20244f2bcc44acb5
parent b41550c2
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -448,16 +448,11 @@ public class InCallActivity extends Activity {
        return false;
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        InCallPresenter.getInstance().getProximitySensor().onConfigurationChanged(config);
        Log.d(this, "onConfigurationChanged "+config.orientation);

        doOrientationChanged(config.orientation);
        super.onConfigurationChanged(config);
    }


    /**
     * Handles changes in device orientation.
     *
     * @param orientation The new device orientation.
     */
    private void doOrientationChanged(int orientation) {
        Log.d(this, "doOrientationChanged prevOrientation=" + sCurrentOrientation +
                " newOrientation=" + orientation);
+29 −18
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.incallui;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
@@ -87,11 +88,6 @@ public class VideoCallFragment extends BaseFragment<VideoCallPresenter,
     */
    private View mVideoViews;

    /**
     * {@code True} when the entering the activity again after a restart due to orientation change.
     */
    private boolean mIsActivityRestart;

    /**
     * {@code True} when the layout of the activity has been completed.
     */
@@ -385,12 +381,20 @@ public class VideoCallFragment extends BaseFragment<VideoCallPresenter,
                Log.e(this, "onClick: Presenter is null.");
            }
        }

        /**
         * Returns the dimensions of the surface.
         *
         * @return The dimensions of the surface.
         */
        public Point getSurfaceDimensions() {
            return new Point(mWidth, mHeight);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIsActivityRestart = sVideoSurfacesInUse;
    }

    /**
@@ -675,12 +679,6 @@ public class VideoCallFragment extends BaseFragment<VideoCallPresenter,
        }
    }

    @Override
    public boolean isActivityRestart() {
        Log.d(this, "isActivityRestart " + mIsActivityRestart);
        return mIsActivityRestart;
    }

    /**
     * @return {@code True} if the display video surface has been created.
     */
@@ -741,12 +739,12 @@ public class VideoCallFragment extends BaseFragment<VideoCallPresenter,
            params.height = height;
            preview.setLayoutParams(params);

            int rotation = InCallPresenter.toRotationAngle(getCurrentRotation());
            int rotationAngle = 360 - rotation;
            preview.setRotation(rotationAngle);
            Log.d(this, "setPreviewSize: rotation=" + rotation +
                    " rotationAngle=" + rotationAngle);

            // 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);
        }
    }

@@ -852,6 +850,19 @@ public class VideoCallFragment extends BaseFragment<VideoCallPresenter,
        return size;
    }

    /**
     * Determines the size of the preview surface.
     *
     * @return {@link Point} specifying the width and height of the preview surface.
     */
    @Override
    public Point getPreviewSize() {
        if (sPreviewSurface == null) {
            return null;
        }
        return sPreviewSurface.getSurfaceDimensions();
    }

    /**
     * Inflates the {@link ViewStub} containing the incoming and outgoing surfaces, if necessary,
     * and creates {@link VideoCallSurface} instances to track the surfaces.
+32 −11
Original line number Diff line number Diff line
@@ -165,7 +165,6 @@ public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi
     */
    private int mCurrentCallSubstate;


    /** Handler which resets request state to NO_REQUEST after an interval. */
    private Handler mSessionModificationResetHandler;
    private static final long SESSION_MODIFICATION_RESET_DELAY_MS = 3000;
@@ -833,6 +832,29 @@ public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi
        }

        mPreviewSurfaceState = PreviewSurfaceState.CAPABILITIES_RECEIVED;
        changePreviewDimensions(width, height);

        // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
        // If it not yet ready, it will be set when when creation completes.
        if (ui.isPreviewVideoSurfaceCreated()) {
            mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
            mVideoCall.setPreviewSurface(ui.getPreviewVideoSurface());
        }
    }

    /**
     * Changes the dimensions of the preview surface.
     *
     * @param width The new width.
     * @param height The new height.
     */
    private void changePreviewDimensions(int width, int height) {
        VideoCallUi ui = getUi();
        if (ui == null) {
            return;
        }

        // Resize the surface used to display the preview video
        ui.setPreviewSurfaceSize(width, height);

        // Configure the preview surface to the correct aspect ratio.
@@ -840,14 +862,10 @@ public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi
        if (width > 0 && height > 0) {
            aspectRatio = (float) width / (float) height;
        }
        setPreviewSize(mDeviceOrientation, aspectRatio);

        // Check if the preview surface is ready yet; if it is, set it on the {@code VideoCall}.
        // If it not yet ready, it will be set when when creation completes.
        if (ui.isPreviewVideoSurfaceCreated()) {
            mPreviewSurfaceState = PreviewSurfaceState.SURFACE_SET;
            mVideoCall.setPreviewSurface(ui.getPreviewVideoSurface());
        }
        // Resize the textureview housing the preview video and rotate it appropriately based on
        // the device orientation
        setPreviewSize(mDeviceOrientation, aspectRatio);
    }

    /**
@@ -889,8 +907,11 @@ public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi
     */
    @Override
    public void onDeviceOrientationChanged(int orientation) {
        Log.d(this, "onDeviceOrientationChanged: orientation=" + orientation);
        mDeviceOrientation = orientation;
        Point previewDimensions = getUi().getPreviewSize();
        Log.d(this, "onDeviceOrientationChanged: orientation=" + orientation + " size: "
                + previewDimensions);
        changePreviewDimensions(previewDimensions.x, previewDimensions.y);
    }

    @Override
@@ -1118,8 +1139,8 @@ public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi
        void setCallDataUsage(Context context, long dataUsage);
        void displayCallSessionEvent(int event);
        Point getScreenSize();
        Point getPreviewSize();
        void cleanupSurfaces();
        boolean isActivityRestart();
        void showCallSubstateChanged(int callSubstate);
    }
}