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

Commit 5cd87140 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Making some properties in FastBitmapDrawable final

Bug: 427523903
Flag: EXEMPT refactor
Test: Updated tests
Change-Id: Ic8a01d46a5cfecfddbf81a96834db88b1a0ce018
parent 9aec2cd7
Loading
Loading
Loading
Loading
+30 −44
Original line number Diff line number Diff line
@@ -90,46 +90,30 @@ data class BitmapInfo(
        context: Context,
        @DrawableCreationFlags creationFlags: Int = 0,
        iconShape: IconShape? = null,
    ): FastBitmapDrawable {
        val drawable: FastBitmapDrawable =
            if (isLowRes) {
    ) =
        FastBitmapDrawable(
                    this,
                    iconShape ?: defaultIconShape,
                    PlaceHolderDelegateFactory(context),
                )
            } else if (
                (creationFlags and FLAG_THEMED) != 0 &&
            info = this,
            iconShape = iconShape ?: defaultIconShape,
            delegateFactory =
                when {
                    isLowRes -> PlaceHolderDelegateFactory(context)
                    creationFlags.hasMask(FLAG_THEMED) &&
                        themedBitmap != null &&
                    themedBitmap !== ThemedBitmap.NOT_SUPPORTED
            ) {
                themedBitmap.newDrawable(this, context, iconShape ?: defaultIconShape)
            } else {
                FastBitmapDrawable(this, iconShape ?: defaultIconShape, delegateFactory)
            }
        applyFlags(context, drawable, creationFlags)
        return drawable
    }

    private fun applyFlags(
        context: Context,
        drawable: FastBitmapDrawable,
        @DrawableCreationFlags creationFlags: Int,
    ) {
        drawable.disabledAlpha = GraphicsUtils.getFloat(context, R.attr.disabledIconAlpha, 1f)
        drawable.creationFlags = creationFlags
        if ((creationFlags and FLAG_NO_BADGE) == 0) {
            val badge =
                        themedBitmap !== ThemedBitmap.NOT_SUPPORTED ->
                        themedBitmap.newDelegateFactory(this, context)
                    else -> delegateFactory
                },
            disabledAlpha = GraphicsUtils.getFloat(context, R.attr.disabledIconAlpha, 1f),
            creationFlags = creationFlags,
            badge =
                if (!creationFlags.hasMask(FLAG_NO_BADGE)) {
                    getBadgeDrawable(
                        context,
                    (creationFlags and FLAG_THEMED) != 0,
                    (creationFlags and FLAG_SKIP_USER_BADGE) != 0,
                        creationFlags.hasMask(FLAG_THEMED),
                        creationFlags.hasMask(FLAG_SKIP_USER_BADGE),
                    )
                } else null,
        )
            if (badge != null) {
                drawable.badge = badge
            }
        }
    }

    /**
     * Gets Badge drawable based on current flags
@@ -173,13 +157,13 @@ data class BitmapInfo(
    /** Returns information about the badge to apply based on current flags. */
    fun getBadgeDrawableInfo(): BadgeDrawableInfo? {
        return when {
            (flags and FLAG_INSTANT) != 0 ->
            flags.hasMask(FLAG_INSTANT) ->
                BadgeDrawableInfo(R.drawable.ic_instant_app_badge, R.color.badge_tint_instant)
            (flags and FLAG_WORK) != 0 ->
            flags.hasMask(FLAG_WORK) ->
                BadgeDrawableInfo(R.drawable.ic_work_app_badge, R.color.badge_tint_work)
            (flags and FLAG_CLONE) != 0 ->
            flags.hasMask(FLAG_CLONE) ->
                BadgeDrawableInfo(R.drawable.ic_clone_app_badge, R.color.badge_tint_clone)
            (flags and FLAG_PRIVATE) != 0 ->
            flags.hasMask(FLAG_PRIVATE) ->
                BadgeDrawableInfo(
                    R.drawable.ic_private_profile_app_badge,
                    R.color.badge_tint_private,
@@ -240,5 +224,7 @@ data class BitmapInfo(
        fun of(bitmap: Bitmap, color: Int, defaultShape: IconShape = IconShape.EMPTY): BitmapInfo {
            return BitmapInfo(icon = bitmap, color = color, defaultIconShape = defaultShape)
        }

        private inline fun Int.hasMask(mask: Int) = (this and mask) != 0
    }
}
+19 −37
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ import android.R
import android.animation.ObjectAnimator
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
@@ -40,7 +39,6 @@ import com.android.launcher3.icons.BitmapInfo.Companion.LOW_RES_INFO
import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags
import com.android.launcher3.icons.FastBitmapDrawableDelegate.DelegateFactory
import com.android.launcher3.icons.FastBitmapDrawableDelegate.SimpleDelegateFactory
import kotlin.math.min

class FastBitmapDrawable
@JvmOverloads
@@ -48,6 +46,9 @@ constructor(
    info: BitmapInfo?,
    private val iconShape: IconShape = IconShape.EMPTY,
    private val delegateFactory: DelegateFactory = SimpleDelegateFactory,
    @JvmField @DrawableCreationFlags val creationFlags: Int = 0,
    private val disabledAlpha: Float = 1f,
    val badge: Drawable? = null,
) : Drawable(), Callback {

    @JvmOverloads constructor(b: Bitmap, iconColor: Int = 0) : this(BitmapInfo.of(b, iconColor))
@@ -63,8 +64,6 @@ constructor(
    @JvmField @VisibleForTesting var isPressed: Boolean = false
    @JvmField @VisibleForTesting var isHovered: Boolean = false

    @JvmField var disabledAlpha: Float = 1f

    var isDisabled: Boolean = false
        set(value) {
            if (field != value) {
@@ -74,7 +73,6 @@ constructor(
            }
        }

    @JvmField @DrawableCreationFlags var creationFlags: Int = 0
    @JvmField @VisibleForTesting var scaleAnimation: ObjectAnimator? = null
    var hoverScaleEnabledForDisplay = true

@@ -84,18 +82,7 @@ constructor(
    private var paintFilter: ColorFilter? = null

    init {
        isFilterBitmap = true
    }

    var badge: Drawable? = null
        set(value) {
            field?.callback = null
            field = value
            field?.let {
                it.callback = this
                it.setBadgeBounds(bounds)
            }
            updateFilter()
        badge?.callback = this
    }

    /** Returns true if the drawable points to the same bitmap icon object */
@@ -255,6 +242,7 @@ constructor(
            badge?.constantState,
            iconShape,
            creationFlags,
            disabledAlpha,
            delegateFactory,
            level,
        )
@@ -284,19 +272,23 @@ constructor(
        val badgeConstantState: ConstantState?,
        val iconShape: IconShape,
        val creationFlags: Int,
        val disabledAlpha: Float,
        val delegateFactory: DelegateFactory,
        val level: Int,
    ) : ConstantState() {

        override fun newDrawable(): FastBitmapDrawable {
            val drawable = FastBitmapDrawable(bitmapInfo, iconShape, delegateFactory)
            drawable.isDisabled = isDisabled
            if (badgeConstantState != null) {
                drawable.badge = badgeConstantState.newDrawable()
            }
            drawable.creationFlags = creationFlags
            drawable.level = level
            return drawable
        override fun newDrawable() =
            FastBitmapDrawable(
                    info = bitmapInfo,
                    iconShape = iconShape,
                    delegateFactory = delegateFactory,
                    creationFlags = creationFlags,
                    badge = badgeConstantState?.newDrawable(),
                    disabledAlpha = disabledAlpha,
                )
                .apply {
                    isDisabled = this@FastBitmapConstantState.isDisabled
                    level = this@FastBitmapConstantState.level
                }

        override fun getChangingConfigurations(): Int = 0
@@ -315,7 +307,6 @@ constructor(

        private const val DISABLED_DESATURATION = 1f
        private const val DISABLED_BRIGHTNESS = 0.5f
        const val FULLY_OPAQUE: Int = 255

        const val CLICK_FEEDBACK_DURATION: Int = 200
        const val HOVER_FEEDBACK_DURATION: Int = 300
@@ -356,15 +347,6 @@ constructor(
            return ColorMatrixColorFilter(tempFilterMatrix)
        }

        @JvmStatic
        fun getDisabledColor(color: Int): Int {
            val avgComponent = (Color.red(color) + Color.green(color) + Color.blue(color)) / 3
            val scale = 1 - DISABLED_BRIGHTNESS
            val brightnessI = (255 * DISABLED_BRIGHTNESS).toInt()
            val component = min(Math.round(scale * avgComponent + brightnessI), FULLY_OPAQUE)
            return Color.rgb(component, component, component)
        }

        /** Sets the bounds for the badge drawable based on the main icon bounds */
        @JvmStatic
        fun Drawable.setBadgeBounds(iconBounds: Rect) {
+5 −4
Original line number Diff line number Diff line
@@ -18,14 +18,15 @@ package com.android.launcher3.icons

import android.content.Context
import android.graphics.drawable.AdaptiveIconDrawable
import com.android.launcher3.icons.FastBitmapDrawableDelegate.DelegateFactory
import com.android.launcher3.icons.cache.CachingLogic
import com.android.launcher3.util.ComponentKey

/** Represents a themed version of a BitmapInfo */
interface ThemedBitmap {

    /** Creates a new Drawable */
    fun newDrawable(info: BitmapInfo, context: Context, shape: IconShape): FastBitmapDrawable
    /** Creates a new [DelegateFactory] based on the [context] */
    fun newDelegateFactory(info: BitmapInfo, context: Context): DelegateFactory

    fun serialize(): ByteArray

@@ -35,8 +36,8 @@ interface ThemedBitmap {
        /** ThemedBitmap to be used when theming is not supported for a particular bitmap */
        val NOT_SUPPORTED =
            object : ThemedBitmap {
                override fun newDrawable(info: BitmapInfo, context: Context, shape: IconShape) =
                    info.newIcon(context = context, iconShape = shape)
                override fun newDelegateFactory(info: BitmapInfo, context: Context) =
                    info.delegateFactory

                override fun serialize() = ByteArray(0)
            }
+7 −22
Original line number Diff line number Diff line
@@ -24,8 +24,7 @@ import android.util.Log
import androidx.annotation.VisibleForTesting
import com.android.launcher3.icons.BitmapInfo
import com.android.launcher3.icons.ClockDrawableWrapper.ClockAnimationInfo
import com.android.launcher3.icons.FastBitmapDrawable
import com.android.launcher3.icons.IconShape
import com.android.launcher3.icons.FastBitmapDrawableDelegate.DelegateFactory
import com.android.launcher3.icons.LuminanceComputer
import com.android.launcher3.icons.ThemedBitmap
import java.nio.ByteBuffer
@@ -36,14 +35,8 @@ class MonoThemedBitmap(
    @get:VisibleForTesting val luminanceDelta: Double? = null,
) : ThemedBitmap {

    override fun newDrawable(
        info: BitmapInfo,
        context: Context,
        shape: IconShape,
    ): FastBitmapDrawable {
        val colors = getUpdatedColors(context)
        return FastBitmapDrawable(info, shape, ThemedIconInfo(mono, colors[0], colors[1]))
    }
    override fun newDelegateFactory(info: BitmapInfo, context: Context): DelegateFactory =
        getUpdatedColors(context).let { ThemedIconInfo(mono, it[0], it[1]) }

    override fun serialize(): ByteArray {
        val expectedSize = mono.width * mono.height
@@ -72,19 +65,11 @@ class ClockThemedBitmap(
    private val colorProvider: (Context) -> IntArray = ThemedIconDelegate.Companion::getColors,
) : ThemedBitmap {

    override fun newDrawable(
        info: BitmapInfo,
        context: Context,
        shape: IconShape,
    ): FastBitmapDrawable {
        val colors = colorProvider(context)
        return FastBitmapDrawable(
            info,
            shape,
    override fun newDelegateFactory(info: BitmapInfo, context: Context): DelegateFactory =
        colorProvider(context).let { colors ->
            animInfo.copy(
                themeFgColor = colors[1],
                shader = LinearGradient(0f, 0f, 1f, 1f, colors[0], colors[0], CLAMP),
            ),
            )
        }