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

Commit 7128a0a9 authored by d34d's avatar d34d Committed by Clark Scheff
Browse files

Themes: Pass resource IDs instead of bitmaps [1/2]

Rather than passing around bitmaps in the ComposedIconInfo we
can pass the resource IDs associated with each of the components
and retrieve those drawables when composing the icon.

Change-Id: If855e60e505571f25316e6728e423fbee62c32d3
parent 7bb802a4
Loading
Loading
Loading
Loading
+10 −13
Original line number Diff line number Diff line
@@ -15,15 +15,13 @@
 */
package android.app;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Parcel;
import android.os.Parcelable;

/** @hide */
public class ComposedIconInfo implements Parcelable {
    public BitmapDrawable iconUpon, iconMask;
    public BitmapDrawable[] iconBacks;
    public int iconUpon, iconMask;
    public int[] iconBacks;
    public float iconScale;
    public int iconDensity;
    public int iconSize;
@@ -33,19 +31,18 @@ public class ComposedIconInfo implements Parcelable {
    }

    private ComposedIconInfo(Parcel source) {
        ClassLoader bmpClassLoader = Bitmap.class.getClassLoader();
        iconScale = source.readFloat();
        iconDensity = source.readInt();
        iconSize = source.readInt();
        int backCount = source.readInt();
        if (backCount > 0) {
            iconBacks = new BitmapDrawable[backCount];
            iconBacks = new int[backCount];
            for (int i = 0; i < backCount; i++) {
                iconBacks[i] = new BitmapDrawable((Bitmap) source.readParcelable(bmpClassLoader));
                iconBacks[i] = source.readInt();
            }
        }
        iconMask = new BitmapDrawable((Bitmap) source.readParcelable(bmpClassLoader));
        iconUpon = new BitmapDrawable((Bitmap) source.readParcelable(bmpClassLoader));
        iconMask = source.readInt();
        iconUpon = source.readInt();
    }

    @Override
@@ -60,12 +57,12 @@ public class ComposedIconInfo implements Parcelable {
        dest.writeInt(iconSize);
        dest.writeInt(iconBacks != null ? iconBacks.length : 0);
        if (iconBacks != null) {
            for (BitmapDrawable d : iconBacks) {
                dest.writeParcelable(d != null ? d.getBitmap() : null, flags);
            for (int resId : iconBacks) {
                dest.writeInt(resId);
            }
        }
        dest.writeParcelable(iconMask != null ? iconMask.getBitmap() : null, flags);
        dest.writeParcelable(iconUpon != null ? iconUpon.getBitmap() : null, flags);
        dest.writeInt(iconMask);
        dest.writeInt(iconUpon);
    }

    public static final Creator<ComposedIconInfo> CREATOR
+36 −32
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ public class IconPackHelper {
            mLoadedIconPackResource = null;
            mLoadedIconPackName = null;
            mComposedIconInfo.iconBacks = null;
            mComposedIconInfo.iconMask = mComposedIconInfo.iconUpon = null;
            mComposedIconInfo.iconMask = mComposedIconInfo.iconUpon = 0;
            mComposedIconInfo.iconScale = 0;
        } else {
            mIconBackCount = 0;
@@ -209,15 +209,15 @@ public class IconPackHelper {
    }

    private void loadComposedIconComponents() {
        mComposedIconInfo.iconMask = (BitmapDrawable) getDrawableForName(ICON_MASK_COMPONENT);
        mComposedIconInfo.iconUpon = (BitmapDrawable) getDrawableForName(ICON_UPON_COMPONENT);
        mComposedIconInfo.iconMask = getResourceIdForName(ICON_MASK_COMPONENT);
        mComposedIconInfo.iconUpon = getResourceIdForName(ICON_UPON_COMPONENT);

        // Take care of loading iconback which can have multiple images
        if (mIconBackCount > 0) {
            mComposedIconInfo.iconBacks = new BitmapDrawable[mIconBackCount];
            mComposedIconInfo.iconBacks = new int[mIconBackCount];
            for (int i = 0; i < mIconBackCount; i++) {
                mComposedIconInfo.iconBacks[i] =
                        (BitmapDrawable) getDrawableForName(
                        getResourceIdForName(
                                new ComponentName(String.format(ICON_BACK_FORMAT, i), ""));
            }
        }
@@ -235,17 +235,12 @@ public class IconPackHelper {
        }
    }

    private Drawable getDrawableForName(ComponentName component) {
        if (isIconPackLoaded()) {
    private int getResourceIdForName(ComponentName component) {
        String item = mIconPackResourceMap.get(component);
        if (!TextUtils.isEmpty(item)) {
                int id = getResourceIdForDrawable(item);
                if (id != 0) {
                    return mLoadedIconPackResource.getDrawable(id);
            return getResourceIdForDrawable(item);
        }
            }
        }
        return null;
        return 0;
    }

    public static Resources createIconResource(Context context, String packageName) throws NameNotFoundException {
@@ -432,7 +427,7 @@ public class IconPackHelper {
        public static Drawable getComposedIconDrawable(Drawable icon, Resources res,
                                                       ComposedIconInfo iconInfo) {
            if (iconInfo == null) return icon;
            Drawable back = null;
            int back = 0;
            if (iconInfo.iconBacks != null && iconInfo.iconBacks.length > 0) {
                back = iconInfo.iconBacks[sRandom.nextInt(iconInfo.iconBacks.length)];
            }
@@ -455,7 +450,7 @@ public class IconPackHelper {
            if (!(new File(outValue.string.toString()).exists())) {
                // compose the icon and cache it
                final ComposedIconInfo iconInfo = res.getComposedIconInfo();
                Drawable back = null;
                int back = 0;
                if (iconInfo.iconBacks != null && iconInfo.iconBacks.length > 0) {
                    back = iconInfo.iconBacks[(outValue.string.hashCode() & 0x7fffffff)
                            % iconInfo.iconBacks.length];
@@ -470,8 +465,8 @@ public class IconPackHelper {
            }
        }

        private static Bitmap createIconBitmap(Drawable icon, Resources res, Drawable iconBack,
                                               Drawable iconMask, Drawable iconUpon, float scale,
        private static Bitmap createIconBitmap(Drawable icon, Resources res, int iconBack,
                                               int iconMask, int iconUpon, float scale,
                                               int iconSize) {
            if (iconSize <= 0) return null;

@@ -528,23 +523,32 @@ public class IconPackHelper {
            canvas.restore();

            // Mask off the original if iconMask is not null
            if (iconMask != null) {
                iconMask.setBounds(icon.getBounds());
                ((BitmapDrawable) iconMask).getPaint().setXfermode(
            if (iconMask != 0) {
                Drawable mask = res.getDrawable(iconMask);
                if (mask != null) {
                    mask.setBounds(icon.getBounds());
                    ((BitmapDrawable) mask).getPaint().setXfermode(
                            new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
                iconMask.draw(canvas);
                    mask.draw(canvas);
                }
            }
            // Draw the iconBacks if not null and then the original (scaled and masked) icon on top
            if (iconBack != null) {
                iconBack.setBounds(icon.getBounds());
                ((BitmapDrawable) iconBack).getPaint().setXfermode(
            if (iconBack != 0) {
                Drawable back = res.getDrawable(iconBack);
                if (back != null) {
                    back.setBounds(icon.getBounds());
                    ((BitmapDrawable) back).getPaint().setXfermode(
                            new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
                iconBack.draw(canvas);
                    back.draw(canvas);
                }
            }
            // Finally draw the foreground if one was supplied
            if (iconUpon != null) {
                iconUpon.setBounds(icon.getBounds());
                iconUpon.draw(canvas);
            if (iconUpon != 0) {
                Drawable upon = res.getDrawable(iconUpon);
                if (upon != null) {
                    upon.setBounds(icon.getBounds());
                    upon.draw(canvas);
                }
            }
            icon.setBounds(oldBounds);
            bitmap.setDensity(canvas.getDensity());