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

Commit 96103bef authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Converting various cache lookup option booleans to flags, so that it can

easily be extended

Bug: 366237794
Test: Presubmit
Flag: EXEMPT refactor, method signature change
Change-Id: I98296642232dfa6f20f876d67b24ccf808cad870
parent 8dd21fd8
Loading
Loading
Loading
Loading
+34 −7
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ 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;
@@ -66,6 +67,8 @@ 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.nio.ByteBuffer;
import java.util.AbstractMap;
import java.util.Arrays;
@@ -88,6 +91,31 @@ 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,
    }, 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;
    }

    public static class CacheEntry {

        @NonNull
@@ -441,26 +469,25 @@ 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,
            final boolean usePackageIcon, final boolean useLowResIcon) {
            @LookupFlag int lookupFlags) {
        return cacheLocked(
                componentName,
                user,
                infoProvider,
                cachingLogic,
                null,
                usePackageIcon,
                useLowResIcon);
                lookupFlags,
                null);
    }

    @NonNull
    protected <T> CacheEntry cacheLocked(
            @NonNull final ComponentName componentName, @NonNull final UserHandle user,
            @NonNull final Supplier<T> infoProvider, @NonNull final CachingLogic<T> cachingLogic,
            @Nullable final Cursor cursor, final boolean usePackageIcon,
            final boolean useLowResIcon) {
            @LookupFlag int 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;
        if (entry == null || (entry.bitmap.isLowRes() && !useLowResIcon)) {
            entry = new CacheEntry();
            if (cachingLogic.addToMemCache()) {
@@ -481,7 +508,7 @@ public abstract class BaseIconCache {
                        object,
                        entry,
                        cachingLogic,
                        usePackageIcon,
                        (lookupFlags & LookupFlag.USE_PACKAGE_ICON) != 0,
                        /* usePackageTitle= */ true,
                        componentName,
                        user);