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

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

Small safety checks in WallpaperCropper

If WallpaperCropper couldn't generate the crop, always remove any crop
guidelines. Also, always check in getCrop that all suggested crops are
valid. Also, avoid a potential infinite recursion in getAdjustedCrop.

Flag: ACONFIG com.android.window.flags.multi_crop DEVELOPMENT
Bug: NA, let's not spam bugs
Test: manual debug with logs
Change-Id: I3684d489f3dd7d5656db559b6e5cee5537e3bc5a
parent b6e4f098
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -131,20 +131,24 @@ public class WallpaperCropper {
                    (bitmapSize.y - crop.height()) / 2);
            return crop;
        }

        // If any suggested crop is invalid, fallback to case 1
        for (int i = 0; i < suggestedCrops.size(); i++) {
            Rect testCrop = suggestedCrops.valueAt(i);
            if (testCrop == null || testCrop.left < 0 || testCrop.top < 0
                    || testCrop.right > bitmapSize.x || testCrop.bottom > bitmapSize.y) {
                Slog.w(TAG, "invalid crop: " + testCrop + " for bitmap size: " + bitmapSize);
                return getCrop(displaySize, bitmapSize, new SparseArray<>(), rtl);
            }
        }

        int orientation = getOrientation(displaySize);

        // Case 2: if the orientation exists in the suggested crops, adjust the suggested crop
        Rect suggestedCrop = suggestedCrops.get(orientation);
        if (suggestedCrop != null) {
            if (suggestedCrop.left < 0 || suggestedCrop.top < 0
                    || suggestedCrop.right > bitmapSize.x || suggestedCrop.bottom > bitmapSize.y) {
                Slog.w(TAG, "invalid suggested crop: " + suggestedCrop);
                Rect fullImage = new Rect(0, 0, bitmapSize.x, bitmapSize.y);
                return getAdjustedCrop(fullImage, bitmapSize, displaySize, true, rtl, ADD);
            } else {
                return getAdjustedCrop(suggestedCrop, bitmapSize, displaySize, true, rtl, ADD);
        }
        }

        // Case 3: if we have the 90° rotated orientation in the suggested crops, reuse it and
        // trying to preserve the zoom level and the center of the image
@@ -247,6 +251,7 @@ public class WallpaperCropper {
        Rect adjustedCrop = new Rect(crop);
        float cropRatio = ((float) crop.width()) / crop.height();
        float screenRatio = ((float) screenSize.x) / screenSize.y;
        if (cropRatio == screenRatio) return crop;
        if (cropRatio > screenRatio) {
            if (!parallax) {
                // rotate everything 90 degrees clockwise, compute the result, and rotate back
@@ -276,6 +281,7 @@ public class WallpaperCropper {
                }
            }
        } else {
            // TODO (b/281648899) the third case is not always correct, fix that.
            int widthToAdd = mode == REMOVE ? 0
                    : mode == ADD ? (int) (0.5 + crop.height() * screenRatio - crop.width())
                    : (int) (0.5 + crop.height() - crop.width());
@@ -646,6 +652,9 @@ public class WallpaperCropper {
        if (!success) {
            Slog.e(TAG, "Unable to apply new wallpaper");
            wallpaper.getCropFile().delete();
            wallpaper.mCropHints.clear();
            wallpaper.cropHint.set(0, 0, 0, 0);
            wallpaper.mSampleSize = 1f;
        }

        if (wallpaper.getCropFile().exists()) {