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

Commit 40d8da39 authored by Tony Huang's avatar Tony Huang Committed by Android (Google) Code Review
Browse files

Merge "Prevent return giant size thumbnail"

parents 4eaacb25 de42137e
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
@@ -3658,7 +3658,7 @@ public abstract class ContentResolver implements ContentInterface {
                // returned something giant, so defensively scale down as needed.
                final int widthSample = info.getSize().getWidth() / size.getWidth();
                final int heightSample = info.getSize().getHeight() / size.getHeight();
            final int sample = Math.min(widthSample, heightSample);
                final int sample = Math.max(widthSample, heightSample);
                if (sample > 1) {
                    decoder.setTargetSampleSize(sample);
                }
+26 −10
Original line number Diff line number Diff line
@@ -86,19 +86,19 @@ public class ContentResolverTest {
                afd);
    }

    private static void assertImageAspectAndContents(Bitmap bitmap) {
    private static void assertImageAspectAndContents(int width, int height, Bitmap bitmap) {
        // And correct aspect ratio
        final int before = (100 * 1280) / 960;
        final int before = (100 * width) / height;
        final int after = (100 * bitmap.getWidth()) / bitmap.getHeight();
        assertEquals(before, after);

        // And scaled correctly
        final int halfX = bitmap.getWidth() / 2;
        final int halfY = bitmap.getHeight() / 2;
        assertEquals(Color.BLUE, bitmap.getPixel(halfX - 10, halfY - 10));
        assertEquals(Color.RED, bitmap.getPixel(halfX + 10, halfY - 10));
        assertEquals(Color.RED, bitmap.getPixel(halfX - 10, halfY + 10));
        assertEquals(Color.RED, bitmap.getPixel(halfX + 10, halfY + 10));
        assertEquals(Color.BLUE, bitmap.getPixel(halfX - 5, halfY - 5));
        assertEquals(Color.RED, bitmap.getPixel(halfX + 5, halfY - 5));
        assertEquals(Color.RED, bitmap.getPixel(halfX - 5, halfY + 5));
        assertEquals(Color.RED, bitmap.getPixel(halfX + 5, halfY + 5));
    }

    @Test
@@ -113,7 +113,7 @@ public class ContentResolverTest {
        assertEquals(1280, res.getWidth());
        assertEquals(960, res.getHeight());

        assertImageAspectAndContents(res);
        assertImageAspectAndContents(1280, 960, res);
    }

    @Test
@@ -128,7 +128,7 @@ public class ContentResolverTest {
        assertTrue(res.getWidth() <= 640);
        assertTrue(res.getHeight() <= 480);

        assertImageAspectAndContents(res);
        assertImageAspectAndContents(1280, 960, res);
    }

    @Test
@@ -143,7 +143,7 @@ public class ContentResolverTest {
        assertTrue(res.getWidth() <= 640);
        assertTrue(res.getHeight() <= 480);

        assertImageAspectAndContents(res);
        assertImageAspectAndContents(1280, 960, res);
    }

    @Test
@@ -158,7 +158,23 @@ public class ContentResolverTest {
        assertEquals(32, res.getWidth());
        assertEquals(24, res.getHeight());

        assertImageAspectAndContents(res);
        assertImageAspectAndContents(32, 24, res);
    }

    @Test
    public void testLoadThumbnail_Large() throws Exception {
        // Test very large and extreme ratio image
        initImage(1080, 30000);

        Bitmap res = ContentResolver.loadThumbnail(mClient,
                Uri.parse("content://com.example/"), new Size(1080, 540), null,
                ImageDecoder.ALLOCATOR_SOFTWARE);

        // Size should be much smaller
        assertTrue(res.getWidth() <= 2160);
        assertTrue(res.getHeight() <= 1080);

        assertImageAspectAndContents(1080, 30000, res);
    }

    @Test