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

Commit de42137e authored by Tony Huang's avatar Tony Huang
Browse files

Prevent return giant size thumbnail

Use Math.max rather than min to avoid return giant size thumbnail.

Fix: 143267761
Test: atest FrameworksCoreTests:android.content.ContentResolverTest
Change-Id: Ib7ddc6f4daa364a6402f72852e0b7995a3c9ec0c
parent f6be0e7f
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
@@ -3647,7 +3647,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
@@ -85,19 +85,19 @@ public class ContentResolverTest {
        when(mProvider.openTypedAssetFile(any(), any(), any(), any(), any())).thenReturn(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
@@ -112,7 +112,7 @@ public class ContentResolverTest {
        assertEquals(1280, res.getWidth());
        assertEquals(960, res.getHeight());

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

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

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

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

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

    @Test
@@ -157,7 +157,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