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

Commit 39ee5d8f authored by Behnam Heydarshahi's avatar Behnam Heydarshahi
Browse files

Add optional res to Icon.Loaded

In the case of modes tile this removes redundant state updates.

Flag: com.android.systemui.qs_new_tiles
Bug: 383761789
Test: atest ModesTileTest ModesTileDataInteractorTest
Change-Id: I0609ee5823b0720e8368c305451ed48b34495ff3
parent 7177560e
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import com.android.internal.R
import com.android.settingslib.notification.modes.TestModeBuilder
import com.android.systemui.SysuiTestCase
import com.android.systemui.SysuiTestableContext
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.common.shared.model.asIcon
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
@@ -63,7 +64,7 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
    fun setUp() {
        context.orCreateTestableResources.apply {
            addOverride(MODES_DRAWABLE_ID, MODES_DRAWABLE)
            addOverride(R.drawable.ic_zen_mode_type_bedtime, BEDTIME_DRAWABLE)
            addOverride(BEDTIME_DRAWABLE_ID, BEDTIME_DRAWABLE)
        }

        val customPackageContext = SysuiTestableContext(context)
@@ -158,7 +159,7 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
            zenModeRepository.addMode(
                id = "Bedtime with default icon",
                type = AutomaticZenRule.TYPE_BEDTIME,
                active = true
                active = true,
            )
            runCurrent()
            assertThat(tileData?.icon).isEqualTo(BEDTIME_ICON)
@@ -259,12 +260,14 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
        val MODES_DRAWABLE_ID = R.drawable.ic_zen_priority_modes
        const val CUSTOM_DRAWABLE_ID = 12345

        val BEDTIME_DRAWABLE_ID = R.drawable.ic_zen_mode_type_bedtime

        val MODES_DRAWABLE = TestStubDrawable("modes_icon")
        val BEDTIME_DRAWABLE = TestStubDrawable("bedtime")
        val CUSTOM_DRAWABLE = TestStubDrawable("custom")

        val MODES_ICON = MODES_DRAWABLE.asIcon()
        val BEDTIME_ICON = BEDTIME_DRAWABLE.asIcon()
        val MODES_ICON = Icon.Loaded(MODES_DRAWABLE, null, MODES_DRAWABLE_ID)
        val BEDTIME_ICON = Icon.Loaded(BEDTIME_DRAWABLE, null, BEDTIME_DRAWABLE_ID)
        val CUSTOM_ICON = CUSTOM_DRAWABLE.asIcon()
    }
}
+13 −5
Original line number Diff line number Diff line
@@ -21,14 +21,17 @@ import android.graphics.drawable.Drawable

/**
 * Models an icon, that can either be already [loaded][Icon.Loaded] or be a [reference]
 * [Icon.Resource] to a resource.
 * [Icon.Resource] to a resource. In case of [Loaded], the resource ID [res] is optional.
 */
sealed class Icon {
    abstract val contentDescription: ContentDescription?

    data class Loaded(
    data class Loaded
    @JvmOverloads
    constructor(
        val drawable: Drawable,
        override val contentDescription: ContentDescription?,
        @DrawableRes val res: Int? = null,
    ) : Icon()

    data class Resource(
@@ -37,6 +40,11 @@ sealed class Icon {
    ) : Icon()
}

/** Creates [Icon.Loaded] for a given drawable with an optional [contentDescription]. */
fun Drawable.asIcon(contentDescription: ContentDescription? = null): Icon.Loaded =
    Icon.Loaded(this, contentDescription)
/**
 * Creates [Icon.Loaded] for a given drawable with an optional [contentDescription] and an optional
 * [res].
 */
fun Drawable.asIcon(
    contentDescription: ContentDescription? = null,
    @DrawableRes res: Int? = null,
): Icon.Loaded = Icon.Loaded(this, contentDescription, res)
+8 −3
Original line number Diff line number Diff line
@@ -22,16 +22,21 @@ import com.android.systemui.qs.tileimpl.QSTileImpl

/**
 * Creates a [QSTile.Icon] from an [Icon].
 * * [Icon.Loaded] -> [QSTileImpl.DrawableIcon]
 * * [Icon.Loaded] with null [res] -> [QSTileImpl.DrawableIcon]
 * * [Icon.Loaded] & with non null [res] -> [QSTileImpl.DrawableIconWithRes]
 * * [Icon.Resource] -> [QSTileImpl.ResourceIcon]
 */
fun Icon.asQSTileIcon(): QSTile.Icon {
    return when (this) {
        is Icon.Loaded -> {
            QSTileImpl.DrawableIcon(this.drawable)
            if (res == null) {
                QSTileImpl.DrawableIcon(drawable)
            } else {
                QSTileImpl.DrawableIconWithRes(drawable, res)
            }
        }
        is Icon.Resource -> {
            QSTileImpl.ResourceIcon.get(this.res)
            QSTileImpl.ResourceIcon.get(res)
        }
    }
}
+12 −3
Original line number Diff line number Diff line
@@ -79,7 +79,10 @@ constructor(
        } else {
            return ModesTileModel(
                isActivated = activeModes.isAnyActive(),
                icon = context.getDrawable(ModesTile.ICON_RES_ID)!!.asIcon(),
                icon =
                    context
                        .getDrawable(ModesTile.ICON_RES_ID)!!
                        .asIcon(res = ModesTile.ICON_RES_ID),
                iconResId = ModesTile.ICON_RES_ID,
                activeModes = activeModes.modeNames,
            )
@@ -92,12 +95,18 @@ constructor(
        return if (activeMode != null) {
            // ZenIconKey.resPackage is null if its resId is a system icon.
            if (activeMode.icon.key.resPackage == null) {
                TileIcon(activeMode.icon.drawable.asIcon(), activeMode.icon.key.resId)
                TileIcon(
                    activeMode.icon.drawable.asIcon(res = activeMode.icon.key.resId),
                    activeMode.icon.key.resId,
                )
            } else {
                TileIcon(activeMode.icon.drawable.asIcon(), null)
            }
        } else {
            TileIcon(context.getDrawable(ModesTile.ICON_RES_ID)!!.asIcon(), ModesTile.ICON_RES_ID)
            TileIcon(
                context.getDrawable(ModesTile.ICON_RES_ID)!!.asIcon(res = ModesTile.ICON_RES_ID),
                ModesTile.ICON_RES_ID,
            )
        }
    }