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

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

Moving some utility class to IconCache to make it easier to use outside Launcher

Bug: 366237794
Flag: EXEMPT, code move
Test: Presubmit
Change-Id: I499d51350191731a11ac63ce869bd9b9e95bbff5
parent 566c1c62
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ public class BaseIconFactory implements AutoCloseable {
    @NonNull
    private final ColorExtractor mColorExtractor;

    protected final int mFillResIconDpi;
    protected final int mFullResIconDpi;
    protected final int mIconBitmapSize;

    protected boolean mMonoIconEnabled;
@@ -106,11 +106,11 @@ public class BaseIconFactory implements AutoCloseable {

    private static int PLACEHOLDER_BACKGROUND_COLOR = Color.rgb(245, 245, 245);

    protected BaseIconFactory(Context context, int fillResIconDpi, int iconBitmapSize,
    protected BaseIconFactory(Context context, int fullResIconDpi, int iconBitmapSize,
            boolean shapeDetection) {
        mContext = context.getApplicationContext();
        mShapeDetection = shapeDetection;
        mFillResIconDpi = fillResIconDpi;
        mFullResIconDpi = fullResIconDpi;
        mIconBitmapSize = iconBitmapSize;

        mPm = mContext.getPackageManager();
@@ -121,8 +121,8 @@ public class BaseIconFactory implements AutoCloseable {
        clear();
    }

    public BaseIconFactory(Context context, int fillResIconDpi, int iconBitmapSize) {
        this(context, fillResIconDpi, iconBitmapSize, false);
    public BaseIconFactory(Context context, int fullResIconDpi, int iconBitmapSize) {
        this(context, fullResIconDpi, iconBitmapSize, false);
    }

    protected void clear() {
@@ -145,6 +145,10 @@ public class BaseIconFactory implements AutoCloseable {
        return mNormalizer;
    }

    public int getFullResIconDpi() {
        return mFullResIconDpi;
    }

    @SuppressWarnings("deprecation")
    public BitmapInfo createIconBitmap(Intent.ShortcutIconResource iconRes) {
        try {
@@ -152,7 +156,7 @@ public class BaseIconFactory implements AutoCloseable {
            if (resources != null) {
                final int id = resources.getIdentifier(iconRes.resourceName, null, null);
                // do not stamp old legacy shortcuts as the app may have already forgotten about it
                return createBadgedIconBitmap(resources.getDrawableForDensity(id, mFillResIconDpi));
                return createBadgedIconBitmap(resources.getDrawableForDensity(id, mFullResIconDpi));
            }
        } catch (Exception e) {
            // Icon not found.
@@ -485,7 +489,7 @@ public class BaseIconFactory implements AutoCloseable {

    @NonNull
    public BitmapInfo makeDefaultIcon() {
        return createBadgedIconBitmap(getFullResDefaultActivityIcon(mFillResIconDpi));
        return createBadgedIconBitmap(getFullResDefaultActivityIcon(mFullResIconDpi));
    }

    @NonNull
@@ -547,6 +551,14 @@ public class BaseIconFactory implements AutoCloseable {
            return this;
        }

        /**
         * If the icon represents an archived app
         */
        public IconOptions setIsArchived(boolean isArchived) {
            mIsArchived = isArchived;
            return this;
        }

        /**
         * Disables auto color extraction and overrides the color to the provided value
         */
+19 −3
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ import androidx.annotation.WorkerThread;
import com.android.launcher3.icons.BaseIconFactory;
import com.android.launcher3.icons.BaseIconFactory.IconOptions;
import com.android.launcher3.icons.BitmapInfo;
import com.android.launcher3.icons.IconProvider;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.FlagOp;
import com.android.launcher3.util.SQLiteCacheHelper;
@@ -102,6 +103,9 @@ public abstract class BaseIconCache {
    @NonNull
    protected final PackageManager mPackageManager;

    @NonNull
    protected final IconProvider mIconProvider;

    @NonNull
    private final Map<ComponentKey, CacheEntry> mCache;

@@ -138,8 +142,16 @@ public abstract class BaseIconCache {
    public BaseIconCache(@NonNull final Context context, @Nullable final String dbFileName,
            @NonNull final Looper bgLooper, final int iconDpi, final int iconPixelSize,
            final boolean inMemoryCache) {
        this(context, dbFileName, bgLooper, iconDpi, iconPixelSize, inMemoryCache,
                new IconProvider(context));
    }

    public BaseIconCache(@NonNull final Context context, @Nullable final String dbFileName,
            @NonNull final Looper bgLooper, final int iconDpi, final int iconPixelSize,
            final boolean inMemoryCache, @NonNull IconProvider iconProvider) {
        mContext = context;
        mDbFileName = dbFileName;
        mIconProvider = iconProvider;
        mPackageManager = context.getPackageManager();
        mBgLooper = bgLooper;
        mWorkerHandler = new Handler(mBgLooper);
@@ -302,7 +314,11 @@ public abstract class BaseIconCache {

    @NonNull
    protected String getIconSystemState(@Nullable final String packageName) {
        return mSystemState;
        return mIconProvider.getSystemStateForPackage(mSystemState, packageName);
    }

    public IconProvider getIconProvider() {
        return mIconProvider;
    }

    public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
@@ -346,7 +362,7 @@ public abstract class BaseIconCache {
        }
        if (entry == null) {
            entry = new CacheEntry();
            entry.bitmap = cachingLogic.loadIcon(mContext, object);
            entry.bitmap = cachingLogic.loadIcon(mContext, this, object);
        }
        // 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
@@ -489,7 +505,7 @@ public abstract class BaseIconCache {
            final boolean usePackageTitle, @NonNull final ComponentName componentName,
            @NonNull final UserHandle user) {
        if (object != null) {
            entry.bitmap = cachingLogic.loadIcon(mContext, object);
            entry.bitmap = cachingLogic.loadIcon(mContext, this, object);
        } else {
            if (usePackageIcon) {
                CacheEntry packageEntry = getEntryForPackageLocked(
+56 −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 android.content.ComponentName;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * A simple interface to represent an object which can be added to icon cache
 *
 * @param <T> Any subclass of the icon cache with which this object is associated
 */
public interface CachedObject<T extends BaseIconCache> {

    /**
     * Returns the component name for the underlying object
     */
    @NonNull ComponentName getComponent();

    /**
     * Returns the user for the underlying object
     */
    @NonNull UserHandle getUser();

    /**
     * Loads the user visible label for the provided object
     */
    @Nullable CharSequence getLabel(PackageManager pm);

    /**
     * Loads the user visible icon for the provided object
     */
    @Nullable
    default Drawable getFullResIcon(@NonNull T cache) {
        return null;
    }
}
+55 −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 android.content.ComponentName
import android.content.Context
import android.os.UserHandle
import com.android.launcher3.icons.BaseIconFactory.IconOptions
import com.android.launcher3.icons.BitmapInfo

/** Caching logic for ComponentWithLabelAndIcon */
class CachedObjectCachingLogic<T : BaseIconCache>
@JvmOverloads
constructor(
    context: Context,
    private val loadIcons: Boolean = true,
    private val addToMemCache: Boolean = true,
) : CachingLogic<CachedObject<T>> {

    private val pm = context.packageManager

    override fun getComponent(info: CachedObject<T>): ComponentName = info.component

    override fun getUser(info: CachedObject<T>): UserHandle = info.user

    override fun getLabel(info: CachedObject<T>): CharSequence? = info.getLabel(pm)

    override fun loadIcon(
        context: Context,
        cache: BaseIconCache,
        info: CachedObject<T>,
    ): BitmapInfo {
        if (!loadIcons) return BitmapInfo.LOW_RES_INFO
        val d = info.getFullResIcon(cache as T) ?: return BitmapInfo.LOW_RES_INFO
        cache.iconFactory.use { li ->
            return li.createBadgedIconBitmap(d, IconOptions().setUser(info.user))
        }
    }

    override fun addToMemCache() = addToMemCache
}
+5 −2
Original line number Diff line number Diff line
@@ -34,7 +34,10 @@ public interface CachingLogic<T> {
    @NonNull
    UserHandle getUser(@NonNull final T object);

    @NonNull
    /**
     * Loads the user visible label for the object
     */
    @Nullable
    CharSequence getLabel(@NonNull final T object);

    @NonNull
@@ -44,7 +47,7 @@ public interface CachingLogic<T> {
    }

    @NonNull
    BitmapInfo loadIcon(@NonNull final Context context, @NonNull final T object);
    BitmapInfo loadIcon(@NonNull Context context, @NonNull BaseIconCache cache, @NonNull T object);

    /**
     * Provides a option list of keywords to associate with this object
Loading