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

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

Replacing all int lookup flags and boolean overrides with an object to make it...

Replacing all int lookup flags and boolean overrides with an object to make it easier to extend options

Bug: 381897614
Bug: 366237794
Flag: EXEMPT refactor
Test: atest CacheLookupFlagTest
Change-Id: Ia20cede9d57bcfa80b3beb5f455244c6fa59a3de
parent 31161792
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.launcher3.icons;

import static com.android.launcher3.icons.cache.CacheLookupFlag.DEFAULT_LOOKUP_FLAG;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
@@ -25,6 +27,7 @@ import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.launcher3.icons.cache.CacheLookupFlag;
import com.android.launcher3.util.FlagOp;

public class BitmapInfo {
@@ -120,6 +123,13 @@ public class BitmapInfo {
        return LOW_RES_ICON == icon;
    }

    /**
     * Returns the lookup flag to match this current state of this info
     */
    public CacheLookupFlag getMatchingLookupFlag() {
        return DEFAULT_LOOKUP_FLAG.withUseLowRes(isLowRes());
    }

    /**
     * BitmapInfo can be stored on disk or other persistent storage
     */
+5 −40
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
@@ -65,8 +64,6 @@ import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.FlagOp;
import com.android.launcher3.util.SQLiteCacheHelper;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collections;
@@ -88,37 +85,6 @@ public abstract class BaseIconCache {
    // Empty class name is used for storing package default entry.
    public static final String EMPTY_CLASS_NAME = ".";

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(value = {
            LookupFlag.DEFAULT,
            LookupFlag.USE_LOW_RES,
            LookupFlag.USE_PACKAGE_ICON,
            LookupFlag.SKIP_ADD_TO_MEM_CACHE
    }, flag = true)
    /** Various options to control cache lookup */
    public @interface LookupFlag {
        /**
         * Default behavior of cache lookup is to load high-res icon with no fallback
         */
        int DEFAULT = 0;

        /**
         * When specified, the cache tries to load the low res version of the entry unless a
         * high-res is already in memory
         */
        int USE_LOW_RES = 1 << 0;
        /**
         * When specified, the cache tries to lookup the package entry for the item, if the object
         * entry fails
         */
        int USE_PACKAGE_ICON = 1 << 1;
        /**
         * When specified, the entry will not be added to the memory cache if it was not already
         * added by a previous lookup
         */
        int SKIP_ADD_TO_MEM_CACHE = 1 << 2;
    }

    public static class CacheEntry {

        @NonNull
@@ -404,7 +370,7 @@ public abstract class BaseIconCache {
    protected <T> CacheEntry cacheLocked(
            @NonNull final ComponentName componentName, @NonNull final UserHandle user,
            @NonNull final Supplier<T> infoProvider, @NonNull final CachingLogic<T> cachingLogic,
            @LookupFlag int lookupFlags) {
            @NonNull CacheLookupFlag lookupFlags) {
        return cacheLocked(
                componentName,
                user,
@@ -418,14 +384,13 @@ public abstract class BaseIconCache {
    protected <T> CacheEntry cacheLocked(
            @NonNull final ComponentName componentName, @NonNull final UserHandle user,
            @NonNull final Supplier<T> infoProvider, @NonNull final CachingLogic<T> cachingLogic,
            @LookupFlag int lookupFlags, @Nullable final Cursor cursor) {
            @NonNull CacheLookupFlag lookupFlags, @Nullable final Cursor cursor) {
        assertWorkerThread();
        ComponentKey cacheKey = new ComponentKey(componentName, user);
        CacheEntry entry = mCache.get(cacheKey);
        final boolean useLowResIcon = (lookupFlags & LookupFlag.USE_LOW_RES) != 0;
        final boolean useLowResIcon = lookupFlags.useLowRes();
        if (entry == null || (entry.bitmap.isLowRes() && !useLowResIcon)) {
            boolean addToMemCache = entry != null
                    || (lookupFlags & LookupFlag.SKIP_ADD_TO_MEM_CACHE) == 0;
            boolean addToMemCache = entry != null || !lookupFlags.skipAddToMemCache();
            entry = new CacheEntry();
            if (addToMemCache) {
                mCache.put(cacheKey, entry);
@@ -445,7 +410,7 @@ public abstract class BaseIconCache {
                        object,
                        entry,
                        cachingLogic,
                        (lookupFlags & LookupFlag.USE_PACKAGE_ICON) != 0,
                        lookupFlags.usePackageIcon(),
                        /* usePackageTitle= */ true,
                        componentName,
                        user);
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.icons.cache

import androidx.annotation.IntDef
import kotlin.annotation.AnnotationRetention.SOURCE

/** Flags to control cache lookup behavior */
data class CacheLookupFlag private constructor(@LookupFlag private val flag: Int) {

    /**
     * Cache will try to load the low res version of the entry unless a high-res is already in
     * memory
     */
    fun useLowRes() = hasFlag(USE_LOW_RES)

    @JvmOverloads fun withUseLowRes(useLowRes: Boolean = true) = updateMask(USE_LOW_RES, useLowRes)

    /** Cache will try to lookup the package entry for the item, if the object entry fails */
    fun usePackageIcon() = hasFlag(USE_PACKAGE_ICON)

    @JvmOverloads
    fun withUsePackageIcon(usePackageIcon: Boolean = true) =
        updateMask(USE_PACKAGE_ICON, usePackageIcon)

    /**
     * Entry will not be added to the memory cache if it was not already added by a previous lookup
     */
    fun skipAddToMemCache() = hasFlag(SKIP_ADD_TO_MEM_CACHE)

    @JvmOverloads
    fun withSkipAddToMemCache(skipAddToMemCache: Boolean = true) =
        updateMask(SKIP_ADD_TO_MEM_CACHE, skipAddToMemCache)

    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())]

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

    companion object {
        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 val flagCache = Array(8) { CacheLookupFlag(it) }

        @JvmField val DEFAULT_LOOKUP_FLAG = CacheLookupFlag(0)
    }
}