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

Commit fd0a2b52 authored by Aurélien Pomini's avatar Aurélien Pomini
Browse files

Fix getAdjustedCrop computation

See b/340422332#comment22. This also removes the no-longer-used rtl
argument from the method.

Flag: NONE, trivial bug fix
Bug: 340422332
Test: atest WallpaperCropperTest
Change-Id: I7f429082a5403008cd3ccb307c38a9b0c4063a52
parent e7961f12
Loading
Loading
Loading
Loading
+13 −15
Original line number Original line Diff line number Diff line
@@ -150,7 +150,7 @@ public class WallpaperCropper {
                Rect landscapeCrop = getCrop(rotatedDisplaySize, bitmapSize, suggestedCrops, rtl);
                Rect landscapeCrop = getCrop(rotatedDisplaySize, bitmapSize, suggestedCrops, rtl);
                landscapeCrop = noParallax(landscapeCrop, rotatedDisplaySize, bitmapSize, rtl);
                landscapeCrop = noParallax(landscapeCrop, rotatedDisplaySize, bitmapSize, rtl);
                // compute the crop on portrait at the center of the landscape crop
                // compute the crop on portrait at the center of the landscape crop
                crop = getAdjustedCrop(landscapeCrop, bitmapSize, displaySize, false, rtl, ADD);
                crop = getAdjustedCrop(landscapeCrop, bitmapSize, displaySize, false, ADD);


                // add some parallax (until the border of the landscape crop without parallax)
                // add some parallax (until the border of the landscape crop without parallax)
                if (rtl) {
                if (rtl) {
@@ -160,7 +160,7 @@ public class WallpaperCropper {
                }
                }
            }
            }


            return getAdjustedCrop(crop, bitmapSize, displaySize, true, rtl, ADD);
            return getAdjustedCrop(crop, bitmapSize, displaySize, true, ADD);
        }
        }


        // If any suggested crop is invalid, fallback to case 1
        // If any suggested crop is invalid, fallback to case 1
@@ -176,7 +176,7 @@ public class WallpaperCropper {
        // Case 2: if the orientation exists in the suggested crops, adjust the suggested crop
        // Case 2: if the orientation exists in the suggested crops, adjust the suggested crop
        Rect suggestedCrop = suggestedCrops.get(orientation);
        Rect suggestedCrop = suggestedCrops.get(orientation);
        if (suggestedCrop != null) {
        if (suggestedCrop != null) {
                return getAdjustedCrop(suggestedCrop, bitmapSize, displaySize, true, rtl, ADD);
            return getAdjustedCrop(suggestedCrop, bitmapSize, displaySize, true, ADD);
        }
        }


        // Case 3: if we have the 90° rotated orientation in the suggested crops, reuse it and
        // Case 3: if we have the 90° rotated orientation in the suggested crops, reuse it and
@@ -188,7 +188,7 @@ public class WallpaperCropper {
        if (suggestedCrop != null) {
        if (suggestedCrop != null) {
            // only keep the visible part (without parallax)
            // only keep the visible part (without parallax)
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            return getAdjustedCrop(adjustedCrop, bitmapSize, displaySize, false, rtl, BALANCE);
            return getAdjustedCrop(adjustedCrop, bitmapSize, displaySize, false, BALANCE);
        }
        }


        // Case 4: if the device is a foldable, if we're looking for a folded orientation and have
        // Case 4: if the device is a foldable, if we're looking for a folded orientation and have
@@ -200,13 +200,13 @@ public class WallpaperCropper {
            // compute the visible part (without parallax) of the unfolded screen
            // compute the visible part (without parallax) of the unfolded screen
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            // compute the folded crop, at the center of the crop of the unfolded screen
            // compute the folded crop, at the center of the crop of the unfolded screen
            Rect res = getAdjustedCrop(adjustedCrop, bitmapSize, displaySize, false, rtl, REMOVE);
            Rect res = getAdjustedCrop(adjustedCrop, bitmapSize, displaySize, false, REMOVE);
            // if we removed some width, add it back to add a parallax effect
            // if we removed some width, add it back to add a parallax effect
            if (res.width() < adjustedCrop.width()) {
            if (res.width() < adjustedCrop.width()) {
                if (rtl) res.left = Math.min(res.left, adjustedCrop.left);
                if (rtl) res.left = Math.min(res.left, adjustedCrop.left);
                else res.right = Math.max(res.right, adjustedCrop.right);
                else res.right = Math.max(res.right, adjustedCrop.right);
                // use getAdjustedCrop(parallax=true) to make sure we don't exceed MAX_PARALLAX
                // use getAdjustedCrop(parallax=true) to make sure we don't exceed MAX_PARALLAX
                res = getAdjustedCrop(res, bitmapSize, displaySize, true, rtl, ADD);
                res = getAdjustedCrop(res, bitmapSize, displaySize, true, ADD);
            }
            }
            return res;
            return res;
        }
        }
@@ -220,7 +220,7 @@ public class WallpaperCropper {
        if (suggestedCrop != null) {
        if (suggestedCrop != null) {
            // only keep the visible part (without parallax)
            // only keep the visible part (without parallax)
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            return getAdjustedCrop(adjustedCrop, bitmapSize, displaySize, false, rtl, ADD);
            return getAdjustedCrop(adjustedCrop, bitmapSize, displaySize, false, ADD);
        }
        }


        // Case 6: for a foldable device, try to combine case 3 + case 4 or 5:
        // Case 6: for a foldable device, try to combine case 3 + case 4 or 5:
@@ -255,7 +255,7 @@ public class WallpaperCropper {
    @VisibleForTesting
    @VisibleForTesting
    static Rect noParallax(Rect crop, Point displaySize, Point bitmapSize, boolean rtl) {
    static Rect noParallax(Rect crop, Point displaySize, Point bitmapSize, boolean rtl) {
        if (displaySize == null) return crop;
        if (displaySize == null) return crop;
        Rect adjustedCrop = getAdjustedCrop(crop, bitmapSize, displaySize, true, rtl, ADD);
        Rect adjustedCrop = getAdjustedCrop(crop, bitmapSize, displaySize, true, ADD);
        // only keep the visible part (without parallax)
        // only keep the visible part (without parallax)
        float suggestedDisplayRatio = 1f * displaySize.x / displaySize.y;
        float suggestedDisplayRatio = 1f * displaySize.x / displaySize.y;
        int widthToRemove = (int) (adjustedCrop.width()
        int widthToRemove = (int) (adjustedCrop.width()
@@ -272,7 +272,7 @@ public class WallpaperCropper {
     * Adjust a given crop:
     * Adjust a given crop:
     * <ul>
     * <ul>
     *     <li>If parallax = true, make sure we have a parallax of at most {@link #MAX_PARALLAX},
     *     <li>If parallax = true, make sure we have a parallax of at most {@link #MAX_PARALLAX},
     *     by removing content from the right (or left if RTL layout) if necessary.
     *     by removing content from both sides if necessary.
     *     <li>If parallax = false, make sure we do not have additional width for parallax. If we
     *     <li>If parallax = false, make sure we do not have additional width for parallax. If we
     *     have additional width for parallax, remove half of the additional width on both sides.
     *     have additional width for parallax, remove half of the additional width on both sides.
     *     <li>Make sure the crop fills the screen, i.e. that the width/height ratio of the crop
     *     <li>Make sure the crop fills the screen, i.e. that the width/height ratio of the crop
@@ -282,7 +282,7 @@ public class WallpaperCropper {
     */
     */
    @VisibleForTesting
    @VisibleForTesting
    static Rect getAdjustedCrop(Rect crop, Point bitmapSize, Point screenSize,
    static Rect getAdjustedCrop(Rect crop, Point bitmapSize, Point screenSize,
            boolean parallax, boolean rtl, int mode) {
            boolean parallax, int mode) {
        Rect adjustedCrop = new Rect(crop);
        Rect adjustedCrop = new Rect(crop);
        float cropRatio = ((float) crop.width()) / crop.height();
        float cropRatio = ((float) crop.width()) / crop.height();
        float screenRatio = ((float) screenSize.x) / screenSize.y;
        float screenRatio = ((float) screenSize.x) / screenSize.y;
@@ -297,8 +297,7 @@ public class WallpaperCropper {
                Rect rotatedCrop = new Rect(newLeft, newTop, newRight, newBottom);
                Rect rotatedCrop = new Rect(newLeft, newTop, newRight, newBottom);
                Point rotatedBitmap = new Point(bitmapSize.y, bitmapSize.x);
                Point rotatedBitmap = new Point(bitmapSize.y, bitmapSize.x);
                Point rotatedScreen = new Point(screenSize.y, screenSize.x);
                Point rotatedScreen = new Point(screenSize.y, screenSize.x);
                Rect rect = getAdjustedCrop(rotatedCrop, rotatedBitmap, rotatedScreen, false, rtl,
                Rect rect = getAdjustedCrop(rotatedCrop, rotatedBitmap, rotatedScreen, false, mode);
                        mode);
                int resultLeft = rect.top;
                int resultLeft = rect.top;
                int resultRight = resultLeft + rect.height();
                int resultRight = resultLeft + rect.height();
                int resultTop = rotatedBitmap.x - rect.right;
                int resultTop = rotatedBitmap.x - rect.right;
@@ -318,9 +317,8 @@ public class WallpaperCropper {
            // total surface of W * H. In other words it is the width to add to get the desired
            // total surface of W * H. In other words it is the width to add to get the desired
            // aspect ratio R, while preserving the total number of pixels W * H.
            // aspect ratio R, while preserving the total number of pixels W * H.
            int widthToAdd = mode == REMOVE ? 0
            int widthToAdd = mode == REMOVE ? 0
                    : mode == ADD ? (int) (0.5 + crop.height() * screenRatio - crop.width())
                    : mode == ADD ? (int) (crop.height() * screenRatio - crop.width())
                    : (int) (0.5 - crop.width()
                    : (int) (-crop.width() + Math.sqrt(crop.width() * crop.height() * screenRatio));
                            + Math.sqrt(crop.width() * crop.height() * screenRatio));
            int availableWidth = bitmapSize.x - crop.width();
            int availableWidth = bitmapSize.x - crop.width();
            if (availableWidth >= widthToAdd) {
            if (availableWidth >= widthToAdd) {
                int widthToAddLeft = widthToAdd / 2;
                int widthToAddLeft = widthToAdd / 2;
+18 −28
Original line number Original line Diff line number Diff line
@@ -210,17 +210,15 @@ public class WallpaperCropperTest {
                    new Rect(0, 0, bitmapSize.x, bitmapSize.y),
                    new Rect(0, 0, bitmapSize.x, bitmapSize.y),
                    new Rect(100, 200, bitmapSize.x - 100, bitmapSize.y))) {
                    new Rect(100, 200, bitmapSize.x - 100, bitmapSize.y))) {
                for (int mode: ALL_MODES) {
                for (int mode: ALL_MODES) {
                    for (boolean rtl: List.of(true, false)) {
                    for (boolean parallax: List.of(true, false)) {
                    for (boolean parallax: List.of(true, false)) {
                        assertThat(WallpaperCropper.getAdjustedCrop(
                        assertThat(WallpaperCropper.getAdjustedCrop(
                                    crop, bitmapSize, displaySize, parallax, rtl, mode))
                                crop, bitmapSize, displaySize, parallax, mode))
                                .isEqualTo(crop);
                                .isEqualTo(crop);
                    }
                    }
                }
                }
            }
            }
        }
        }
    }
    }
    }


    /**
    /**
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with parallax = true,
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with parallax = true,
@@ -235,13 +233,11 @@ public class WallpaperCropperTest {
        int expectedWidth = (int) (displaySize.x * (1 + WallpaperCropper.MAX_PARALLAX));
        int expectedWidth = (int) (displaySize.x * (1 + WallpaperCropper.MAX_PARALLAX));
        Point expectedCropSize = new Point(expectedWidth, 1000);
        Point expectedCropSize = new Point(expectedWidth, 1000);
        for (int mode: ALL_MODES) {
        for (int mode: ALL_MODES) {
            for (boolean rtl: List.of(false, true)) {
            assertThat(WallpaperCropper.getAdjustedCrop(
            assertThat(WallpaperCropper.getAdjustedCrop(
                        crop, bitmapSize, displaySize, true, rtl, mode))
                    crop, bitmapSize, displaySize, true, mode))
                    .isEqualTo(centerOf(crop, expectedCropSize));
                    .isEqualTo(centerOf(crop, expectedCropSize));
        }
        }
    }
    }
    }


    /**
    /**
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with parallax = true,
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with parallax = true,
@@ -258,14 +254,12 @@ public class WallpaperCropperTest {
            Point bitmapSize = new Point(acceptableWidth, 1000);
            Point bitmapSize = new Point(acceptableWidth, 1000);
            Rect crop = new Rect(0, 0, bitmapSize.x, bitmapSize.y);
            Rect crop = new Rect(0, 0, bitmapSize.x, bitmapSize.y);
            for (int mode : ALL_MODES) {
            for (int mode : ALL_MODES) {
                for (boolean rtl : List.of(false, true)) {
                assertThat(WallpaperCropper.getAdjustedCrop(
                assertThat(WallpaperCropper.getAdjustedCrop(
                            crop, bitmapSize, displaySize, true, rtl, mode))
                        crop, bitmapSize, displaySize, true, mode))
                        .isEqualTo(crop);
                        .isEqualTo(crop);
            }
            }
        }
        }
    }
    }
    }


    /**
    /**
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with
@@ -292,13 +286,11 @@ public class WallpaperCropperTest {
        for (int i = 0; i < crops.size(); i++) {
        for (int i = 0; i < crops.size(); i++) {
            Rect crop = crops.get(i);
            Rect crop = crops.get(i);
            Rect expectedCrop = expectedAdjustedCrops.get(i);
            Rect expectedCrop = expectedAdjustedCrops.get(i);
            for (boolean rtl: List.of(false, true)) {
            assertThat(WallpaperCropper.getAdjustedCrop(
            assertThat(WallpaperCropper.getAdjustedCrop(
                        crop, bitmapSize, displaySize, false, rtl, WallpaperCropper.ADD))
                    crop, bitmapSize, displaySize, false, WallpaperCropper.ADD))
                    .isEqualTo(expectedCrop);
                    .isEqualTo(expectedCrop);
        }
        }
    }
    }
    }


    /**
    /**
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with
@@ -317,13 +309,11 @@ public class WallpaperCropperTest {
        Point expectedCropSize = new Point(1000, 1000);
        Point expectedCropSize = new Point(1000, 1000);


        for (Rect crop: crops) {
        for (Rect crop: crops) {
            for (boolean rtl : List.of(false, true)) {
            assertThat(WallpaperCropper.getAdjustedCrop(
            assertThat(WallpaperCropper.getAdjustedCrop(
                        crop, bitmapSize, displaySize, false, rtl, WallpaperCropper.REMOVE))
                    crop, bitmapSize, displaySize, false, WallpaperCropper.REMOVE))
                    .isEqualTo(centerOf(crop, expectedCropSize));
                    .isEqualTo(centerOf(crop, expectedCropSize));
        }
        }
    }
    }
    }


    /**
    /**
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with
     * Test that {@link WallpaperCropper#getAdjustedCrop}, when called with
@@ -348,14 +338,14 @@ public class WallpaperCropperTest {
            Rect crop = crops.get(i);
            Rect crop = crops.get(i);
            Rect expected = expectedAdjustedCrops.get(i);
            Rect expected = expectedAdjustedCrops.get(i);
            assertThat(WallpaperCropper.getAdjustedCrop(
            assertThat(WallpaperCropper.getAdjustedCrop(
                    crop, bitmapSize, displaySize, false, false, WallpaperCropper.BALANCE))
                    crop, bitmapSize, displaySize, false, WallpaperCropper.BALANCE))
                    .isEqualTo(expected);
                    .isEqualTo(expected);


            Rect transposedCrop = new Rect(crop.top, crop.left, crop.bottom, crop.right);
            Rect transposedCrop = new Rect(crop.top, crop.left, crop.bottom, crop.right);
            Rect expectedTransposed = new Rect(
            Rect expectedTransposed = new Rect(
                    expected.top, expected.left, expected.bottom, expected.right);
                    expected.top, expected.left, expected.bottom, expected.right);
            assertThat(WallpaperCropper.getAdjustedCrop(transposedCrop, bitmapSize,
            assertThat(WallpaperCropper.getAdjustedCrop(transposedCrop, bitmapSize,
                    transposedDisplaySize, false, false, WallpaperCropper.BALANCE))
                    transposedDisplaySize, false, WallpaperCropper.BALANCE))
                    .isEqualTo(expectedTransposed);
                    .isEqualTo(expectedTransposed);
        }
        }
    }
    }