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

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

Adding support for IconProvider to intercept icon loading

Bug: 366237794
Flag: EXEMPT refactor
Test: Manual
Change-Id: I14abcadbd9a97a297e703da67255d9c9843514ca
parent 2a810456
Loading
Loading
Loading
Loading
+12 −16
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableWrapper;
import android.graphics.drawable.InsetDrawable;
import android.os.Build;
import android.os.UserHandle;
@@ -242,7 +241,11 @@ public class BaseIconFactory implements AutoCloseable {
                    )
            );
        }
        info = info.withFlags(getBitmapFlagOp(options));
        FlagOp flagOp = getBitmapFlagOp(options);
        if (adaptiveIcon instanceof WrappedAdaptiveIcon) {
            flagOp = flagOp.addFlag(BitmapInfo.FLAG_WRAPPED_NON_ADAPTIVE);
        }
        info = info.withFlags(flagOp);
        return info;
    }

@@ -355,12 +358,11 @@ public class BaseIconFactory implements AutoCloseable {
        if (icon instanceof AdaptiveIconDrawable aid) {
            return aid;
        } else {
            EmptyWrapper foreground = new EmptyWrapper();
            AdaptiveIconDrawable dr = new AdaptiveIconDrawable(
                    new ColorDrawable(mWrapperBackgroundColor), foreground);
            dr.setBounds(0, 0, 1, 1);
            float scale = new IconNormalizer(mIconBitmapSize).getScale(icon);
            foreground.setDrawable(createScaledDrawable(icon, scale * LEGACY_ICON_SCALE));
            AdaptiveIconDrawable dr = new WrappedAdaptiveIcon(
                    new ColorDrawable(mWrapperBackgroundColor),
                    createScaledDrawable(icon, scale * LEGACY_ICON_SCALE));
            dr.setBounds(0, 0, 1, 1);
            return dr;
        }
    }
@@ -659,16 +661,10 @@ public class BaseIconFactory implements AutoCloseable {
        }
    }

    private static class EmptyWrapper extends DrawableWrapper {
    private static class WrappedAdaptiveIcon extends AdaptiveIconDrawable {

        EmptyWrapper() {
            super(new ColorDrawable());
        }

        @Override
        public ConstantState getConstantState() {
            Drawable d = getDrawable();
            return d == null ? null : d.getConstantState();
        WrappedAdaptiveIcon(Drawable backgroundDrawable, Drawable foregroundDrawable) {
            super(backgroundDrawable, foregroundDrawable);
        }
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -37,11 +37,13 @@ public class BitmapInfo {
    public static final int FLAG_INSTANT = 1 << 1;
    public static final int FLAG_CLONE = 1 << 2;
    public static final int FLAG_PRIVATE = 1 << 3;
    public static final int FLAG_WRAPPED_NON_ADAPTIVE = 1 << 4;
    @IntDef(flag = true, value = {
            FLAG_WORK,
            FLAG_INSTANT,
            FLAG_CLONE,
            FLAG_PRIVATE
            FLAG_PRIVATE,
            FLAG_WRAPPED_NON_ADAPTIVE
    })
    @interface BitmapInfoFlags {}

+30 −19
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.os.BuildCompat;

import com.android.launcher3.icons.cache.CachingLogic;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.SafeCloseable;

import java.util.Calendar;
@@ -149,7 +151,7 @@ public class IconProvider {
            icon = ClockDrawableWrapper.forPackage(mContext, mClock.getPackageName(), iconDpi);
        }
        if (icon == null) {
            icon = loadPackageIcon(info, appInfo, iconDpi);
            icon = loadPackageIconWithFallback(info, appInfo, iconDpi);
            if (ATLEAST_T && icon instanceof AdaptiveIconDrawable && td != null) {
                AdaptiveIconDrawable aid = (AdaptiveIconDrawable) icon;
                if  (aid.getMonochrome() == null) {
@@ -165,36 +167,39 @@ public class IconProvider {
        return null;
    }

    private Drawable loadPackageIcon(PackageItemInfo info, ApplicationInfo appInfo, int density) {
    private Drawable loadPackageIconWithFallback(
            PackageItemInfo info, ApplicationInfo appInfo, int density) {
        Drawable icon = null;
        if (BuildCompat.isAtLeastV() && info.isArchived) {
            // Icons for archived apps com from system service, let the default impl handle that
            icon = info.loadIcon(mContext.getPackageManager());
        }
        if (icon == null && density != 0 && (info.icon != 0 || appInfo.icon != 0)) {
            icon = loadPackageIcon(info, appInfo, density);
        }
        return icon != null ? icon : getFullResDefaultActivityIcon(density);
    }

    @Nullable
    protected Drawable loadPackageIcon(
            @NonNull PackageItemInfo info, @NonNull ApplicationInfo appInfo, int density) {
        try {
            final Resources resources = mContext.getPackageManager()
                    .getResourcesForApplication(appInfo);
            // Try to load the package item icon first
            if (info != appInfo && info.icon != 0) {
                try {
                        icon = resources.getDrawableForDensity(info.icon, density);
                    Drawable icon = resources.getDrawableForDensity(info.icon, density);
                    if (icon != null) return icon;
                } catch (Resources.NotFoundException exc) { }
            }
                if (icon == null && appInfo.icon != 0) {
            if (appInfo.icon != 0) {
                // Load the fallback app icon
                    icon = loadAppInfoIcon(appInfo, resources, density);
                }
            } catch (NameNotFoundException | Resources.NotFoundException exc) { }
        }
        return icon != null ? icon : getFullResDefaultActivityIcon(density);
    }

    @Nullable
    protected Drawable loadAppInfoIcon(ApplicationInfo info, Resources resources, int density) {
                try {
            return resources.getDrawableForDensity(info.icon, density);
                    return resources.getDrawableForDensity(appInfo.icon, density);
                } catch (Resources.NotFoundException exc) { }
            }
        } catch (NameNotFoundException | Resources.NotFoundException exc) { }
        return null;
    }

@@ -298,6 +303,12 @@ public class IconProvider {
        return new IconChangeReceiver(listener, handler);
    }

    /**
     * Notifies the provider when an icon is loaded from cache
     */
    public void notifyIconLoaded(
            @NonNull BitmapInfo icon, @NonNull ComponentKey key, @NonNull CachingLogic<?> logic) { }

    public static class ThemeData {

        final Resources mResources;
+1 −0
Original line number Diff line number Diff line
@@ -571,6 +571,7 @@ constructor(
        }
        entry.bitmap.flags = c.getInt(INDEX_FLAGS)
        entry.bitmap = entry.bitmap.withFlags(getUserFlagOpLocked(cacheKey.user))
        iconProvider.notifyIconLoaded(entry.bitmap, cacheKey, logic)
        return true
    }