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

Commit cf0999cb authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Lazily loading theme icons" into main

parents 6c6ddf37 b6882a00
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -126,14 +126,16 @@ public class BitmapInfo {
    }

    public final boolean isLowRes() {
        return LOW_RES_ICON == icon;
        return getMatchingLookupFlag().useLowRes();
    }

    /**
     * Returns the lookup flag to match this current state of this info
     */
    public CacheLookupFlag getMatchingLookupFlag() {
        return DEFAULT_LOOKUP_FLAG.withUseLowRes(isLowRes());
        return DEFAULT_LOOKUP_FLAG
                .withUseLowRes(LOW_RES_ICON == icon)
                .withThemeIcon(mThemedBitmap != null);
    }

    /**
@@ -170,7 +172,8 @@ public class BitmapInfo {
        FastBitmapDrawable drawable;
        if (isLowRes()) {
            drawable = new PlaceHolderIconDrawable(this, context);
        } else  if ((creationFlags & FLAG_THEMED) != 0 && mThemedBitmap != null) {
        } else  if ((creationFlags & FLAG_THEMED) != 0 && mThemedBitmap != null
                && mThemedBitmap != ThemedBitmap.NOT_SUPPORTED) {
            drawable = mThemedBitmap.newDrawable(this, context);
        } else {
            drawable = new FastBitmapDrawable(this);
+13 −1
Original line number Diff line number Diff line
@@ -28,6 +28,18 @@ interface ThemedBitmap {
    fun newDrawable(info: BitmapInfo, context: Context): FastBitmapDrawable

    fun serialize(): ByteArray

    companion object {

        @JvmField
        /** 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) = info.newIcon(context)

                override fun serialize() = ByteArray(0)
            }
    }
}

interface IconThemeController {
@@ -46,7 +58,7 @@ interface IconThemeController {
        info: BitmapInfo,
        factory: BaseIconFactory,
        sourceHint: SourceHint,
    ): ThemedBitmap?
    ): ThemedBitmap

    fun createThemedAdaptiveIcon(
        context: Context,
+55 −26
Original line number Diff line number Diff line
@@ -45,9 +45,11 @@ import com.android.launcher3.Flags
import com.android.launcher3.icons.BaseIconFactory
import com.android.launcher3.icons.BaseIconFactory.IconOptions
import com.android.launcher3.icons.BitmapInfo
import com.android.launcher3.icons.BitmapInfo.LOW_RES_ICON
import com.android.launcher3.icons.GraphicsUtils
import com.android.launcher3.icons.IconProvider
import com.android.launcher3.icons.SourceHint
import com.android.launcher3.icons.ThemedBitmap
import com.android.launcher3.icons.cache.CacheLookupFlag.Companion.DEFAULT_LOOKUP_FLAG
import com.android.launcher3.util.ComponentKey
import com.android.launcher3.util.FlagOp
@@ -221,9 +223,11 @@ constructor(
            }

        // Only add an entry in memory, if there was already something previously
        if (cache[key] != null) {
        val existingEntry = cache[key]
        if (existingEntry != null) {
            val entry = CacheEntry()
            entry.bitmap = bitmapInfo
            entry.bitmap =
                bitmapInfo.downSampleToLookupFlag(existingEntry.bitmap.matchingLookupFlag)
            entry.title = entryTitle
            entry.contentDescription = getUserBadgedLabel(entryTitle, user)
            cache[key] = entry
@@ -290,7 +294,7 @@ constructor(
                    obj,
                    entry,
                    cachingLogic,
                    lookupFlags.usePackageIcon(),
                    lookupFlags,
                    /* usePackageTitle= */ true,
                    componentName,
                    user,
@@ -309,7 +313,7 @@ constructor(
        obj: T?,
        entry: CacheEntry,
        cachingLogic: CachingLogic<T>,
        usePackageIcon: Boolean,
        lookupFlag: CacheLookupFlag,
        usePackageTitle: Boolean,
        componentName: ComponentName,
        user: UserHandle,
@@ -317,8 +321,9 @@ constructor(
        if (obj != null) {
            entry.bitmap = cachingLogic.loadIcon(context, this, obj)
        } else {
            if (usePackageIcon) {
                val packageEntry = getEntryForPackageLocked(componentName.packageName, user)
            if (lookupFlag.usePackageIcon()) {
                val packageEntry =
                    getEntryForPackageLocked(componentName.packageName, user, lookupFlag)
                if (DEBUG) {
                    Log.d(TAG, "using package default icon for " + componentName.toShortString())
                }
@@ -329,6 +334,7 @@ constructor(
                    entry.title = packageEntry.title
                }
            }
            entry.bitmap = entry.bitmap.downSampleToLookupFlag(lookupFlag)
        }
    }

@@ -440,8 +446,7 @@ constructor(
                    // only keep the low resolution icon instead of the larger full-sized icon
                    val iconInfo = appInfoCachingLogic.loadIcon(context, this, appInfo)
                    entry.bitmap =
                        if (lookupFlags.useLowRes())
                            BitmapInfo.of(BitmapInfo.LOW_RES_ICON, iconInfo.color)
                        if (lookupFlags.useLowRes()) BitmapInfo.of(LOW_RES_ICON, iconInfo.color)
                        else iconInfo

                    loadFallbackTitle(appInfo, entry, appInfoCachingLogic, user)
@@ -514,7 +519,7 @@ constructor(
        // Set the alpha to be 255, so that we never have a wrong color
        entry.bitmap =
            BitmapInfo.of(
                BitmapInfo.LOW_RES_ICON,
                LOW_RES_ICON,
                GraphicsUtils.setColorAlphaBound(c.getInt(INDEX_COLOR), 255),
            )
        c.getString(INDEX_TITLE).let {
@@ -544,6 +549,10 @@ constructor(
                return false
            }

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

                iconFactory.use { factory ->
                    val themeController = factory.themeController
                    val monoIconData = c.getBlob(INDEX_MONO_ICON)
@@ -559,6 +568,7 @@ constructor(
                    }
                }
            }
        }
        entry.bitmap.flags = c.getInt(INDEX_FLAGS)
        entry.bitmap = entry.bitmap.withFlags(getUserFlagOpLocked(cacheKey.user))
        return true
@@ -660,12 +670,17 @@ constructor(
        val COLUMNS_LOW_RES =
            arrayOf(COLUMN_COMPONENT, COLUMN_LABEL, COLUMN_ICON_COLOR, COLUMN_FLAGS)

        @JvmField
        val COLUMNS_HIGH_RES_NO_THEME =
            COLUMNS_LOW_RES.copyOf(COLUMNS_LOW_RES.size + 2).apply {
                this[size - 1] = COLUMN_ICON
                this[size - 2] = COLUMN_FRESHNESS_ID
            }

        @JvmField
        val COLUMNS_HIGH_RES =
            COLUMNS_LOW_RES.copyOf(COLUMNS_LOW_RES.size + 3).apply {
                this[size - 3] = COLUMN_ICON
                this[size - 2] = COLUMN_MONO_ICON
                this[size - 1] = COLUMN_FRESHNESS_ID
            COLUMNS_HIGH_RES_NO_THEME.copyOf(COLUMNS_HIGH_RES_NO_THEME.size + 1).apply {
                this[size - 1] = COLUMN_MONO_ICON
            }

        @JvmField val INDEX_TITLE = COLUMNS_HIGH_RES.indexOf(COLUMN_LABEL)
@@ -677,6 +692,20 @@ constructor(

        @JvmStatic
        fun CacheLookupFlag.toLookupColumns() =
            if (useLowRes()) COLUMNS_LOW_RES else COLUMNS_HIGH_RES
            when {
                useLowRes() -> COLUMNS_LOW_RES
                Flags.extendibleThemeManager() && !hasThemeIcon() -> COLUMNS_HIGH_RES_NO_THEME
                else -> COLUMNS_HIGH_RES
            }

        @JvmStatic
        protected fun BitmapInfo.downSampleToLookupFlag(flag: CacheLookupFlag) =
            when {
                !Flags.extendibleThemeManager() -> this
                flag.useLowRes() -> BitmapInfo.of(LOW_RES_ICON, color)
                !flag.hasThemeIcon() && themedBitmap != null ->
                    clone().apply { themedBitmap = null }
                else -> this
            }
    }
}
+19 −5
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.launcher3.icons.cache

import androidx.annotation.IntDef
import com.android.launcher3.Flags
import kotlin.annotation.AnnotationRetention.SOURCE

/** Flags to control cache lookup behavior */
@@ -45,18 +46,30 @@ data class CacheLookupFlag private constructor(@LookupFlag private val flag: Int
    fun withSkipAddToMemCache(skipAddToMemCache: Boolean = true) =
        updateMask(SKIP_ADD_TO_MEM_CACHE, skipAddToMemCache)

    /** Entry will include theme icon. Note that theme icon is only loaded for high-res icons */
    fun hasThemeIcon() = hasFlag(LOAD_THEME_ICON)

    @JvmOverloads
    fun withThemeIcon(addThemeIcon: Boolean = true) = updateMask(LOAD_THEME_ICON, addThemeIcon)

    private fun hasFlag(@LookupFlag mask: Int) = flag.and(mask) != 0

    private fun updateMask(@LookupFlag mask: Int, addMask: Boolean) =
        if (addMask) flagCache[flag.or(mask)] else flagCache[flag.and(mask.inv())]

    /** Returns `true` if this flag has less UI information then [other] */
    fun isVisuallyLessThan(other: CacheLookupFlag): Boolean {
        return useLowRes() && !other.useLowRes()
    fun isVisuallyLessThan(other: CacheLookupFlag) =
        when {
            useLowRes() && !other.useLowRes() -> true
            Flags.extendibleThemeManager() && !hasThemeIcon() && other.hasThemeIcon() -> true
            else -> false
        }

    @Retention(SOURCE)
    @IntDef(value = [USE_LOW_RES, USE_PACKAGE_ICON, SKIP_ADD_TO_MEM_CACHE], flag = true)
    @IntDef(
        value = [USE_LOW_RES, USE_PACKAGE_ICON, SKIP_ADD_TO_MEM_CACHE, LOAD_THEME_ICON],
        flag = true,
    )
    /** Various options to control cache lookup */
    private annotation class LookupFlag

@@ -64,8 +77,9 @@ data class CacheLookupFlag private constructor(@LookupFlag private val flag: Int
        private const val USE_LOW_RES: Int = 1 shl 0
        private const val USE_PACKAGE_ICON: Int = 1 shl 1
        private const val SKIP_ADD_TO_MEM_CACHE: Int = 1 shl 2
        private const val LOAD_THEME_ICON: Int = 1 shl 3

        private val flagCache = Array(8) { CacheLookupFlag(it) }
        private val flagCache = Array(1 shl 4) { CacheLookupFlag(it) }

        @JvmField val DEFAULT_LOOKUP_FLAG = CacheLookupFlag(0)
    }
+2 −2
Original line number Diff line number Diff line
@@ -102,9 +102,9 @@ class MonoIconThemeController(
        info: BitmapInfo,
        factory: BaseIconFactory,
        sourceHint: SourceHint,
    ): ThemedBitmap? {
    ): ThemedBitmap {
        val icon = info.icon
        if (data.size != icon.height * icon.width) return null
        if (data.size != icon.height * icon.width) return ThemedBitmap.NOT_SUPPORTED

        var monoBitmap = Bitmap.createBitmap(icon.width, icon.height, ALPHA_8)
        monoBitmap.copyPixelsFromBuffer(ByteBuffer.wrap(data))