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

Commit 1ef586cb authored by András Kurucz's avatar András Kurucz
Browse files

Disable bigpicture lazy loading for unsupported icons

Don't set placeholders for bitmap or data based icons inside
BigPictureStyle Notifications, because it doesn't save on memory.

Bug: 283082473
Test: atest BigPictureIconManager
Change-Id: I547668b5b35cf1f9932eee77d941b69d9e023e38
parent 03021851
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ constructor(
            this.lastLoadingJob?.cancel()
            this.lastLoadingJob =
                when {
                    skipLazyLoading(state.icon) -> null
                    state is Empty && shown -> state.icon?.let(::startLoadingJob)
                    state is PlaceHolder && shown -> startLoadingJob(state.icon)
                    state is FullImage && !shown ->
@@ -144,7 +145,7 @@ constructor(
    private fun loadImageOrPlaceHolderSync(icon: Icon?): Drawable? {
        icon ?: return null

        if (viewShown) {
        if (viewShown || skipLazyLoading(icon)) {
            return loadImageSync(icon)
        }

@@ -228,6 +229,19 @@ constructor(
            }
        )

    /**
     * We don't support lazy-loading or set placeholders for bitmap and data based icons, because
     * they gonna stay in memory anyways.
     */
    private fun skipLazyLoading(icon: Icon?): Boolean =
        when (icon?.type) {
            Icon.TYPE_BITMAP,
            Icon.TYPE_ADAPTIVE_BITMAP,
            Icon.TYPE_DATA,
            null -> true
            else -> false
        }

    private fun log(msg: String) {
        if (DEBUG) {
            Log.d(TAG, "$msg state=${getDebugString()}")
+45 −8
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ class BigPictureIconManagerTest : SysuiTestCase() {
    }
    private val unsupportedIcon by lazy {
        Icon.createWithBitmap(
            BitmapFactory.decodeResource(context.resources, R.drawable.dessert_zombiegingerbread)
            BitmapFactory.decodeResource(context.resources, R.drawable.dessert_donutburger)
        )
    }
    private val invalidIcon by lazy { Icon.createWithContentUri(Uri.parse("this.is/broken")) }
@@ -100,7 +100,7 @@ class BigPictureIconManagerTest : SysuiTestCase() {
        }

    @Test
    fun onIconUpdated_notSupportedType_fullImageLoaded() =
    fun onIconUpdated_unsupportedType_fullImageLoaded() =
        testScope.runTest {
            // WHEN update with an unsupported icon
            iconManager.updateIcon(mockConsumer, unsupportedIcon).run()
@@ -152,6 +152,24 @@ class BigPictureIconManagerTest : SysuiTestCase() {
            assertSize(drawableCaptor.value)
        }

    @Test
    fun onIconUpdated_iconAlreadySetForUnsupportedIcon_loadsNewIcon() =
        testScope.runTest {
            // GIVEN an unsupported icon is set
            iconManager.updateIcon(mockConsumer, unsupportedIcon).run()
            // AND the view is shown
            iconManager.onViewShown(true)
            reset(mockConsumer)

            // WHEN a new icon is set
            iconManager.updateIcon(mockConsumer, supportedIcon).run()

            // THEN consumer is updated with the new image
            verify(mockConsumer).setImageDrawable(drawableCaptor.capture())
            assertIsFullImage(drawableCaptor.value)
            assertSize(drawableCaptor.value)
        }

        @Test
    fun onIconUpdated_supportedTypeButTooWide_resizedPlaceholderLoaded() =
        testScope.runTest {
@@ -249,12 +267,10 @@ class BigPictureIconManagerTest : SysuiTestCase() {
            verifyZeroInteractions(mockConsumer)
        }

    // nice to have tests

    @Test
    fun onViewShown_fullImageLoaded_nothingHappens() =
    fun onViewShown_unsupportedIconLoaded_nothingHappens() =
        testScope.runTest {
            // GIVEN full image is showing
            // GIVEN full image is showing for an unsupported icon
            iconManager.updateIcon(mockConsumer, unsupportedIcon).run()
            reset(mockConsumer)

@@ -266,11 +282,31 @@ class BigPictureIconManagerTest : SysuiTestCase() {
            verifyZeroInteractions(mockConsumer)
        }

    @Test
    fun onViewHidden_unsupportedIconLoadedAndViewIsShown_nothingHappens() =
        testScope.runTest {
            // GIVEN full image is showing for an unsupported icon
            iconManager.updateIcon(mockConsumer, unsupportedIcon).run()
            // AND the view is shown
            iconManager.onViewShown(true)
            runCurrent()
            reset(mockConsumer)

            // WHEN the view goes off the screen
            iconManager.onViewShown(false)
            // AND we wait a bit
            advanceTimeBy(FREE_IMAGE_DELAY_MS)
            runCurrent()

            // THEN nothing happens
            verifyZeroInteractions(mockConsumer)
        }

    @Test
    fun onViewHidden_placeholderShowing_nothingHappens() =
        testScope.runTest {
            // GIVEN placeholder image is showing
            iconManager.updateIcon(mockConsumer, unsupportedIcon).run()
            iconManager.updateIcon(mockConsumer, supportedIcon).run()
            reset(mockConsumer)

            // WHEN the view is hidden
@@ -296,6 +332,7 @@ class BigPictureIconManagerTest : SysuiTestCase() {
            iconManager.onViewShown(true)
            runCurrent()

            // THEN nothing happens
            verifyZeroInteractions(mockConsumer)
        }

@@ -303,7 +340,7 @@ class BigPictureIconManagerTest : SysuiTestCase() {
    fun onViewHidden_alreadyHidden_nothingHappens() =
        testScope.runTest {
            // GIVEN placeholder image is showing and the view is hidden
            iconManager.updateIcon(mockConsumer, unsupportedIcon).run()
            iconManager.updateIcon(mockConsumer, supportedIcon).run()
            iconManager.onViewShown(false)
            advanceTimeBy(FREE_IMAGE_DELAY_MS)
            runCurrent()