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

Commit a9babccb authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Converting BitmapInfo into data class

Bug: 427523903
Flag: EXEMPT refactor
Test: Presubmit
Change-Id: I984888cd10f13828b3752bc2e6fc0add43d0bc82
parent 3de17107
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -245,7 +245,7 @@ public class BaseIconFactory implements AutoCloseable {
        }

        if (IconProvider.ATLEAST_T && mThemeController != null && adaptiveIcon != null) {
            info.setThemedBitmap(
            info = info.withThemedBitmap(
                    mThemeController.createThemedBitmap(
                        adaptiveIcon,
                        info,
+25 −41
Original line number Diff line number Diff line
@@ -27,13 +27,25 @@ import com.android.launcher3.icons.PlaceHolderDrawableDelegate.PlaceHolderDelega
import com.android.launcher3.icons.cache.CacheLookupFlag
import com.android.launcher3.util.FlagOp

class BitmapInfo(
/**
 * Data class that holds all the information needed to create an icon drawable.
 *
 * @property icon the bitmap of the icon.
 * @property color the color of the icon.
 * @property flags extra source information associated with this icon
 * @property defaultIconShape the fallback shape when no shape is provided during icon creation
 * @property themedBitmap theming information if the icon is created using [FLAG_THEMED]
 * @property delegateFactory factory used for icon creation
 * @property badgeInfo optional badge drawn on the icon
 */
data class BitmapInfo(
    @JvmField val icon: Bitmap,
    @JvmField val color: Int,
    @JvmField val defaultIconShape: IconShape = IconShape.EMPTY,
    @BitmapInfoFlags @JvmField var flags: Int = 0,
    var themedBitmap: ThemedBitmap? = null,
    var delegateFactory: DelegateFactory = SimpleDelegateFactory,
    @BitmapInfoFlags val flags: Int = 0,
    val defaultIconShape: IconShape = IconShape.EMPTY,
    val themedBitmap: ThemedBitmap? = null,
    val badgeInfo: BitmapInfo? = null,
    val delegateFactory: DelegateFactory = SimpleDelegateFactory,
) {
    @IntDef(
        flag = true,
@@ -52,37 +64,14 @@ class BitmapInfo(
    @IntDef(flag = true, value = [FLAG_THEMED, FLAG_NO_BADGE, FLAG_SKIP_USER_BADGE])
    annotation class DrawableCreationFlags

    // b/377618519: These are saved to debug why work badges sometimes don't show up on work apps
    @DrawableCreationFlags @JvmField var creationFlags: Int = 0

    private var badgeInfo: BitmapInfo? = null

    fun withBadgeInfo(badgeInfo: BitmapInfo?) = clone().also { it.badgeInfo = badgeInfo }
    fun withBadgeInfo(badgeInfo: BitmapInfo?) = copy(badgeInfo = badgeInfo)

    /** Returns a bitmapInfo with the flagOP applied */
    fun withFlags(op: FlagOp): BitmapInfo {
        if (op === FlagOp.NO_OP) {
            return this
        }
        return clone().also { it.flags = op.apply(it.flags) }
    }

    @Override
    fun clone(): BitmapInfo {
        return copyInternalsTo(BitmapInfo(icon, color, defaultIconShape))
    }

    private fun copyInternalsTo(target: BitmapInfo): BitmapInfo {
        target.themedBitmap = themedBitmap
        target.flags = flags
        target.badgeInfo = badgeInfo
        target.delegateFactory = delegateFactory
        return target
    }
    fun withFlags(op: FlagOp): BitmapInfo =
        if (op === FlagOp.NO_OP) this else copy(flags = op.apply(this.flags))

    // TODO: rename or remove because icon can no longer be null?
    val isNullOrLowRes: Boolean
        get() = icon == LOW_RES_ICON
    /** Helper class to allow copy from java code */
    fun withThemedBitmap(themedBitmap: ThemedBitmap?) = copy(themedBitmap = themedBitmap)

    val isLowRes: Boolean
        get() = matchingLookupFlag.useLowRes()
@@ -95,7 +84,7 @@ class BitmapInfo(

    /** BitmapInfo can be stored on disk or other persistent storage */
    fun canPersist(): Boolean {
        return !isNullOrLowRes && delegateFactory == SimpleDelegateFactory
        return !isLowRes && delegateFactory == SimpleDelegateFactory
    }

    /**
@@ -124,7 +113,7 @@ class BitmapInfo(
                    themedBitmap != null &&
                    themedBitmap !== ThemedBitmap.NOT_SUPPORTED
            ) {
                themedBitmap!!.newDrawable(this, context, iconShape ?: defaultIconShape)
                themedBitmap.newDrawable(this, context, iconShape ?: defaultIconShape)
            } else {
                FastBitmapDrawable(this, iconShape ?: defaultIconShape, delegateFactory)
            }
@@ -137,7 +126,6 @@ class BitmapInfo(
        drawable: FastBitmapDrawable,
        @DrawableCreationFlags creationFlags: Int,
    ) {
        this.creationFlags = creationFlags
        drawable.disabledAlpha = GraphicsUtils.getFloat(context, R.attr.disabledIconAlpha, 1f)
        drawable.creationFlags = creationFlags
        if ((creationFlags and FLAG_NO_BADGE) == 0) {
@@ -158,8 +146,6 @@ class BitmapInfo(
     *
     * @param context Context
     * @param isThemed If Drawable is themed.
     * @param badgeShape Optional Path to mask badges to a shape. Should be 100x100.
     * @return Drawable for the badge.
     */
    fun getBadgeDrawable(context: Context, isThemed: Boolean): Drawable? {
        return getBadgeDrawable(context, isThemed, false)
@@ -171,8 +157,6 @@ class BitmapInfo(
     * @param context Context
     * @param isThemed If the drawable is themed.
     * @param skipUserBadge If should skip User Profile badging.
     * @param badgeShape Optional Path to mask badge Drawable to a shape. Should be 100x100.
     * @return Drawable for an icon Badge.
     */
    private fun getBadgeDrawable(
        context: Context,
@@ -184,7 +168,7 @@ class BitmapInfo(
            if (skipUserBadge) {
                creationFlag = creationFlag or FLAG_SKIP_USER_BADGE
            }
            return badgeInfo!!.newIcon(context, creationFlag, null)
            return badgeInfo.newIcon(context, creationFlag, null)
        }
        if (skipUserBadge) {
            return null
+7 −7
Original line number Diff line number Diff line
@@ -62,13 +62,13 @@ private constructor(base: AdaptiveIconDrawable, private val animationInfo: Clock
                it.drawColor(Color.BLACK)
                drawable.background?.draw(it)
            }
        val result = info.clone()
        result.delegateFactory =
        return info.copy(
            delegateFactory =
                animationInfo.copy(
                    themeFgColor = NO_COLOR,
                    shaderProvider = { BitmapShader(flattenBG, CLAMP, CLAMP) },
                )
        return result
        )
    }

    override fun drawForPersistence() {
+21 −13
Original line number Diff line number Diff line
@@ -213,7 +213,7 @@ constructor(
        // Icon can't be loaded from cachingLogic, which implies alternative icon was loaded
        // (e.g. fallback icon, default icon). So we drop here since there's no point in caching
        // an empty entry.
        if (bitmapInfo.isNullOrLowRes || isDefaultIcon(bitmapInfo, user)) {
        if (bitmapInfo.isLowRes || isDefaultIcon(bitmapInfo, user)) {
            return
        }
        val entryTitle =
@@ -545,26 +545,35 @@ constructor(

            if (!extendibleThemeManager() || lookupFlags.hasThemeIcon()) {
                // Always set a non-null theme bitmap if theming was requested
                entry.bitmap.themedBitmap = ThemedBitmap.NOT_SUPPORTED
                entry.bitmap = entry.bitmap.copy(themedBitmap = ThemedBitmap.NOT_SUPPORTED)

                iconFactory.use { factory ->
                    val themeController = factory.themeController
                    val monoIconData = c.getBlob(INDEX_MONO_ICON)
                    if (themeController != null && monoIconData != null) {
                        entry.bitmap.themedBitmap =
                        entry.bitmap =
                            entry.bitmap.copy(
                                themedBitmap =
                                    themeController.decode(
                                        bytes = monoIconData,
                                        info = entry.bitmap,
                                        factory = factory,
                                        sourceHint =
                                    SourceHint(cacheKey, logic, c.getString(INDEX_FRESHNESS_ID)),
                                            SourceHint(
                                                cacheKey,
                                                logic,
                                                c.getString(INDEX_FRESHNESS_ID),
                                            ),
                                    )
                            )
                    }
                }
            }
        }
        entry.bitmap.flags = c.getInt(INDEX_FLAGS)
        entry.bitmap = entry.bitmap.withFlags(getUserFlagOpLocked(cacheKey.user))
        entry.bitmap =
            entry.bitmap.copy(
                flags = getUserFlagOpLocked(cacheKey.user).apply(c.getInt(INDEX_FLAGS))
            )
        iconProvider.notifyIconLoaded(entry.bitmap, cacheKey, logic)
        return true
    }
@@ -693,8 +702,7 @@ constructor(
            when {
                !extendibleThemeManager() -> this
                flag.useLowRes() -> BitmapInfo.of(LOW_RES_ICON, color)
                !flag.hasThemeIcon() && themedBitmap != null ->
                    clone().apply { themedBitmap = null }
                !flag.hasThemeIcon() && themedBitmap != null -> copy(themedBitmap = null)
                else -> this
            }
    }