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

Commit b3341550 authored by Aurélien Pomini's avatar Aurélien Pomini Committed by Android (Google) Code Review
Browse files

Merge "Fix getAdjustedCrop computation" into main

parents 0b3bad0a fd0a2b52
Loading
Loading
Loading
Loading
+13 −15
Original line number Diff line number Diff line
@@ -150,7 +150,7 @@ public class WallpaperCropper {
                Rect landscapeCrop = getCrop(rotatedDisplaySize, bitmapSize, suggestedCrops, rtl);
                landscapeCrop = noParallax(landscapeCrop, rotatedDisplaySize, bitmapSize, rtl);
                // 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)
                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
@@ -176,7 +176,7 @@ public class WallpaperCropper {
        // Case 2: if the orientation exists in the suggested crops, adjust the suggested crop
        Rect suggestedCrop = suggestedCrops.get(orientation);
        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
@@ -188,7 +188,7 @@ public class WallpaperCropper {
        if (suggestedCrop != null) {
            // only keep the visible part (without parallax)
            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
@@ -200,13 +200,13 @@ public class WallpaperCropper {
            // compute the visible part (without parallax) of the unfolded screen
            Rect adjustedCrop = noParallax(suggestedCrop, suggestedDisplaySize, bitmapSize, rtl);
            // 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 (res.width() < adjustedCrop.width()) {
                if (rtl) res.left = Math.min(res.left, adjustedCrop.left);
                else res.right = Math.max(res.right, adjustedCrop.right);
                // 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;
        }
@@ -220,7 +220,7 @@ public class WallpaperCropper {
        if (suggestedCrop != null) {
            // only keep the visible part (without parallax)
            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:
@@ -255,7 +255,7 @@ public class WallpaperCropper {
    @VisibleForTesting
    static Rect noParallax(Rect crop, Point displaySize, Point bitmapSize, boolean rtl) {
        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)
        float suggestedDisplayRatio = 1f * displaySize.x / displaySize.y;
        int widthToRemove = (int) (adjustedCrop.width()
@@ -272,7 +272,7 @@ public class WallpaperCropper {
     * Adjust a given crop:
     * <ul>
     *     <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
     *     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
@@ -282,7 +282,7 @@ public class WallpaperCropper {
     */
    @VisibleForTesting
    static Rect getAdjustedCrop(Rect crop, Point bitmapSize, Point screenSize,
            boolean parallax, boolean rtl, int mode) {
            boolean parallax, int mode) {
        Rect adjustedCrop = new Rect(crop);
        float cropRatio = ((float) crop.width()) / crop.height();
        float screenRatio = ((float) screenSize.x) / screenSize.y;
@@ -297,8 +297,7 @@ public class WallpaperCropper {
                Rect rotatedCrop = new Rect(newLeft, newTop, newRight, newBottom);
                Point rotatedBitmap = new Point(bitmapSize.y, bitmapSize.x);
                Point rotatedScreen = new Point(screenSize.y, screenSize.x);
                Rect rect = getAdjustedCrop(rotatedCrop, rotatedBitmap, rotatedScreen, false, rtl,
                        mode);
                Rect rect = getAdjustedCrop(rotatedCrop, rotatedBitmap, rotatedScreen, false, mode);
                int resultLeft = rect.top;
                int resultRight = resultLeft + rect.height();
                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
            // aspect ratio R, while preserving the total number of pixels W * H.
            int widthToAdd = mode == REMOVE ? 0
                    : mode == ADD ? (int) (0.5 + crop.height() * screenRatio - crop.width())
                    : (int) (0.5 - crop.width()
                            + Math.sqrt(crop.width() * crop.height() * screenRatio));
                    : mode == ADD ? (int) (crop.height() * screenRatio - crop.width())
                    : (int) (-crop.width() + Math.sqrt(crop.width() * crop.height() * screenRatio));
            int availableWidth = bitmapSize.x - crop.width();
            if (availableWidth >= widthToAdd) {
                int widthToAddLeft = widthToAdd / 2;
+18 −28
Original line number Diff line number Diff line
@@ -210,17 +210,15 @@ public class WallpaperCropperTest {
                    new Rect(0, 0, bitmapSize.x, bitmapSize.y),
                    new Rect(100, 200, bitmapSize.x - 100, bitmapSize.y))) {
                for (int mode: ALL_MODES) {
                    for (boolean rtl: List.of(true, false)) {
                    for (boolean parallax: List.of(true, false)) {
                        assertThat(WallpaperCropper.getAdjustedCrop(
                                    crop, bitmapSize, displaySize, parallax, rtl, mode))
                                crop, bitmapSize, displaySize, parallax, mode))
                                .isEqualTo(crop);
                    }
                }
            }
        }
    }
    }

    /**
     * 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));
        Point expectedCropSize = new Point(expectedWidth, 1000);
        for (int mode: ALL_MODES) {
            for (boolean rtl: List.of(false, true)) {
            assertThat(WallpaperCropper.getAdjustedCrop(
                        crop, bitmapSize, displaySize, true, rtl, mode))
                    crop, bitmapSize, displaySize, true, mode))
                    .isEqualTo(centerOf(crop, expectedCropSize));
        }
    }
    }

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

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

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

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

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

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