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

Commit 89dc00a3 authored by Mady Mellor's avatar Mady Mellor
Browse files

PIP should only move along long edges when on phone

Phone portrait: restrict movement to left / right edges
Phone landscape: restrict movement to top / bottom edges

Tablet: allow PIP on all edges regardless of orientation

Test: manual - have PIP try to move along top / bottom edge
in phone portrait, and left / right edge in phone landscape
Fixes: 35358644
Change-Id: Ie50ae220782936942d1bcd8d9251d7275832a47f
parent 9c2886ba
Loading
Loading
Loading
Loading
+28 −12
Original line number Diff line number Diff line
@@ -36,15 +36,18 @@ import java.util.ArrayList;
 */
public class PipSnapAlgorithm {

    // The below SNAP_MODE_* constants correspond to the config resource value
    // config_pictureInPictureSnapMode and should not be changed independently.
    // Allows snapping to the four corners
    private static final int SNAP_MODE_CORNERS_ONLY = 0;
    // Allows snapping to the four corners and the mid-points on the long edge in each orientation
    private static final int SNAP_MODE_CORNERS_AND_SIDES = 1;
    // Allows snapping to anywhere along the edge of the screen
    private static final int SNAP_MODE_EDGE = 2;
    // Allows snapping to four corners on a fling towards a corner or slow move near a corner
    // snaps anywhere along the edge of screen otherwise
    private static final int SNAP_MODE_CORNERS_AND_EDGES = 3;
    // Allows snapping anywhere along the edge of the screen and magnets towards corners
    private static final int SNAP_MODE_EDGE_MAGNET_CORNERS = 3;
    // Allows snapping on the long edge in each orientation and magnets towards corners
    private static final int SNAP_MODE_LONG_EDGE_MAGNET_CORNERS = 4;

    // The friction multiplier to control how slippery the PIP is when flung
    private static final float SCROLL_FRICTION_MULTIPLIER = 8f;
@@ -55,7 +58,7 @@ public class PipSnapAlgorithm {
    private final Context mContext;

    private final ArrayList<Integer> mSnapGravities = new ArrayList<>();
    private final int mDefaultSnapMode = SNAP_MODE_CORNERS_AND_EDGES;
    private final int mDefaultSnapMode = SNAP_MODE_EDGE_MAGNET_CORNERS;
    private int mSnapMode = mDefaultSnapMode;

    private final float mDefaultSizePercent;
@@ -85,7 +88,9 @@ public class PipSnapAlgorithm {
     * Updates the snap algorithm when the configuration changes.
     */
    public void onConfigurationChanged() {
        mOrientation = mContext.getResources().getConfiguration().orientation;
        Resources res = mContext.getResources();
        mOrientation = res.getConfiguration().orientation;
        mSnapMode = res.getInteger(com.android.internal.R.integer.config_pictureInPictureSnapMode);
        calculateSnapTargets();
    }

@@ -127,7 +132,8 @@ public class PipSnapAlgorithm {
                movementBounds.right + stackBounds.width(),
                movementBounds.bottom + stackBounds.height());
        final Rect newBounds = new Rect(stackBounds);
        if (mSnapMode == SNAP_MODE_CORNERS_AND_EDGES) {
        if (mSnapMode == SNAP_MODE_LONG_EDGE_MAGNET_CORNERS
                || mSnapMode == SNAP_MODE_EDGE_MAGNET_CORNERS) {
            final Rect tmpBounds = new Rect();
            final Point[] snapTargets = new Point[mSnapGravities.size()];
            for (int i = 0; i < mSnapGravities.size(); i++) {
@@ -137,11 +143,11 @@ public class PipSnapAlgorithm {
            }
            Point snapTarget = findClosestPoint(stackBounds.left, stackBounds.top, snapTargets);
            float distance = distanceToPoint(snapTarget, stackBounds.left, stackBounds.top);
            final float thresh = stackBounds.width() * CORNER_MAGNET_THRESHOLD;
            final float thresh = Math.max(stackBounds.width(), stackBounds.height())
                    * CORNER_MAGNET_THRESHOLD;
            if (distance < thresh) {
                newBounds.offsetTo(snapTarget.x, snapTarget.y);
            } else {
                // Otherwise we snap to the edge
                snapRectToClosestEdge(stackBounds, movementBounds, newBounds);
            }
        } else if (mSnapMode == SNAP_MODE_EDGE) {
@@ -324,11 +330,20 @@ public class PipSnapAlgorithm {
        final int fromTop = Math.abs(stackBounds.top - movementBounds.top);
        final int fromRight = Math.abs(movementBounds.right - stackBounds.left);
        final int fromBottom = Math.abs(movementBounds.bottom - stackBounds.top);
        if (fromLeft <= fromTop && fromLeft <= fromRight && fromLeft <= fromBottom) {
        int shortest;
        if (mSnapMode == SNAP_MODE_LONG_EDGE_MAGNET_CORNERS) {
            // Only check longest edges
            shortest = (mOrientation == Configuration.ORIENTATION_LANDSCAPE)
                    ? Math.min(fromTop, fromBottom)
                    : Math.min(fromLeft, fromRight);
        } else {
            shortest = Math.min(Math.min(fromLeft, fromRight), Math.min(fromTop, fromBottom));
        }
        if (shortest == fromLeft) {
            boundsOut.offsetTo(movementBounds.left, boundedTop);
        } else if (fromTop <= fromLeft && fromTop <= fromRight && fromTop <= fromBottom) {
        } else if (shortest == fromTop) {
            boundsOut.offsetTo(boundedLeft, movementBounds.top);
        } else if (fromRight < fromLeft && fromRight < fromTop && fromRight < fromBottom) {
        } else if (shortest == fromRight) {
            boundsOut.offsetTo(movementBounds.right, boundedTop);
        } else {
            boundsOut.offsetTo(boundedLeft, movementBounds.bottom);
@@ -358,7 +373,8 @@ public class PipSnapAlgorithm {
                }
                // Fall through
            case SNAP_MODE_CORNERS_ONLY:
            case SNAP_MODE_CORNERS_AND_EDGES:
            case SNAP_MODE_EDGE_MAGNET_CORNERS:
            case SNAP_MODE_LONG_EDGE_MAGNET_CORNERS:
                mSnapGravities.add(Gravity.TOP | Gravity.LEFT);
                mSnapGravities.add(Gravity.TOP | Gravity.RIGHT);
                mSnapGravities.add(Gravity.BOTTOM | Gravity.LEFT);
+11 −1
Original line number Diff line number Diff line
@@ -41,5 +41,15 @@
    <dimen name="config_minScalingSpan">32mm</dimen>

    <integer name="config_dockedStackDividerSnapMode">1</integer>

    <!-- The snap mode to use for picture-in-picture. These values correspond to constants defined
         in PipSnapAlgorithm and should not be changed independently.
             0 - Snap to the four corners
             1 - Snap to the four corners and the mid-points on the long edge in each orientation
             2 - Snap anywhere along the edge of the screen
             3 - Snap anywhere along the edge of the screen and magnet to corners
             4 - Snap to the long edges in each orientation and magnet to corners
    -->
    <integer name="config_pictureInPictureSnapMode">3</integer>
</resources>
+10 −0
Original line number Diff line number Diff line
@@ -2569,6 +2569,16 @@
         ratio larger than this is considered to wide and short to be usable. -->
    <item name="config_pictureInPictureMaxAspectRatio" format="float" type="dimen">2.35</item>

    <!-- The snap mode to use for picture-in-picture. These values correspond to constants defined
         in PipSnapAlgorithm and should not be changed independently.
             0 - Snap to the four corners
             1 - Snap to the four corners and the mid-points on the long edge in each orientation
             2 - Snap anywhere along the edge of the screen
             3 - Snap anywhere along the edge of the screen and magnet to corners
             4 - Snap to the long edges in each orientation and magnet to corners
    -->
    <integer name="config_pictureInPictureSnapMode">4</integer>

    <!-- Controls the snap mode for the docked stack divider
             0 - 3 snap targets: left/top has 16:9 ratio, 1:1, and right/bottom has 16:9 ratio
             1 - 3 snap targets: fixed ratio, 1:1, (1 - fixed ratio)
+1 −0
Original line number Diff line number Diff line
@@ -1556,6 +1556,7 @@
  <java-symbol type="dimen" name="docked_stack_minimize_thickness" />
  <java-symbol type="dimen" name="pip_minimized_visible_size" />
  <java-symbol type="integer" name="config_dockedStackDividerSnapMode" />
  <java-symbol type="integer" name="config_pictureInPictureSnapMode" />
  <java-symbol type="fraction" name="docked_stack_divider_fixed_ratio" />
  <java-symbol type="fraction" name="thumbnail_fullscreen_scale" />
  <java-symbol type="integer" name="thumbnail_width_tv" />