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

Commit 451f13e4 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding support for loading system themed icons

Bug: 183641907
Test: Manual
Change-Id: Ic921f46659545316b5a1ead0b3ec5bb758f3fc37
parent cf43b612
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ package {

android_library {
    name: "iconloader_base",
    sdk_version: "28",
    sdk_version: "current",
    min_sdk_version: "21",
    static_libs: [
        "androidx.core_core",
+19 −1
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ public class BaseIconFactory implements AutoCloseable {

    private Drawable mWrapperIcon;
    private int mWrapperBackgroundColor = DEFAULT_WRAPPER_BACKGROUND;
    private Bitmap mUserBadgeBitmap;

    private final Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    private static final float PLACEHOLDER_TEXT_SIZE = 20f;
@@ -256,10 +257,27 @@ public class BaseIconFactory implements AutoCloseable {
        }
        int color = extractColor(bitmap);
        return icon instanceof BitmapInfo.Extender
                ? ((BitmapInfo.Extender) icon).getExtendedInfo(bitmap, color, this)
                ? ((BitmapInfo.Extender) icon).getExtendedInfo(bitmap, color, this, scale[0], user)
                : BitmapInfo.of(bitmap, color);
    }

    public Bitmap getUserBadgeBitmap(UserHandle user) {
        if (mUserBadgeBitmap == null) {
            Bitmap bitmap = Bitmap.createBitmap(
                    mIconBitmapSize, mIconBitmapSize, Bitmap.Config.ARGB_8888);
            Drawable badgedDrawable = mPm.getUserBadgedIcon(
                    new FixedSizeBitmapDrawable(bitmap), user);
            if (badgedDrawable instanceof BitmapDrawable) {
                mUserBadgeBitmap = ((BitmapDrawable) badgedDrawable).getBitmap();
            } else {
                badgedDrawable.setBounds(0, 0, mIconBitmapSize, mIconBitmapSize);
                mUserBadgeBitmap = BitmapRenderer.createSoftwareBitmap(
                        mIconBitmapSize, mIconBitmapSize, badgedDrawable::draw);
            }
        }
        return mUserBadgeBitmap;
    }

    public Bitmap createScaledBitmapWithoutShadow(Drawable icon, boolean shrinkNonAdaptiveIcons) {
        RectF iconBounds = new RectF();
        float[] scale = new float[1];
+52 −6
Original line number Diff line number Diff line
@@ -15,21 +15,36 @@
 */
package com.android.launcher3.icons;

import static com.android.launcher3.icons.GraphicsUtils.getExpectedBitmapSize;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Build;
import android.os.UserHandle;
import android.util.Log;

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

import com.android.launcher3.icons.ThemedIconDrawable.ThemedBitmapInfo;
import com.android.launcher3.icons.cache.BaseIconCache;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class BitmapInfo {

    public static final Bitmap LOW_RES_ICON = Bitmap.createBitmap(1, 1, Config.ALPHA_8);
    public static final BitmapInfo LOW_RES_INFO = fromBitmap(LOW_RES_ICON);

    public static final String TAG = "BitmapInfo";

    protected static final byte TYPE_DEFAULT = 1;
    protected static final byte TYPE_THEMED = 2;

    public final Bitmap icon;
    public final int color;

@@ -54,7 +69,27 @@ public class BitmapInfo {
     */
    @Nullable
    public byte[] toByteArray() {
        return isNullOrLowRes() ? null : GraphicsUtils.flattenBitmap(icon);
        if (isNullOrLowRes()) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream(getExpectedBitmapSize(icon) + 1);
        try {
            out.write(TYPE_DEFAULT);
            icon.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
            Log.w(TAG, "Could not write bitmap");
            return null;
        }
    }

    /**
     * Returns a new icon based on the theme of the context
     */
    public FastBitmapDrawable newThemedIcon(Context context) {
        return newIcon(context);
    }

    /**
@@ -72,7 +107,11 @@ public class BitmapInfo {
     * Returns a BitmapInfo previously serialized using {@link #toByteArray()};
     */
    @NonNull
    public static BitmapInfo fromByteArray(byte[] data, int color) {
    public static BitmapInfo fromByteArray(byte[] data, int color, UserHandle user,
            BaseIconCache iconCache, Context context) {
        if (data == null) {
            return null;
        }
        BitmapFactory.Options decodeOptions;
        if (BitmapRenderer.USE_HARDWARE_BITMAP && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            decodeOptions = new BitmapFactory.Options();
@@ -80,9 +119,15 @@ public class BitmapInfo {
        } else {
            decodeOptions = null;
        }
        if (data[0] == TYPE_DEFAULT) {
            return BitmapInfo.of(
                BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions),
                    BitmapFactory.decodeByteArray(data, 1, data.length - 1, decodeOptions),
                    color);
        } else if (data[0] == TYPE_THEMED) {
            return ThemedBitmapInfo.decode(data, color, decodeOptions, user, iconCache, context);
        } else {
            return null;
        }
    }

    public static BitmapInfo fromBitmap(@NonNull Bitmap bitmap) {
@@ -101,7 +146,8 @@ public class BitmapInfo {
        /**
         * Called for creating a custom BitmapInfo
         */
        default BitmapInfo getExtendedInfo(Bitmap bitmap, int color, BaseIconFactory iconFactory) {
        default BitmapInfo getExtendedInfo(Bitmap bitmap, int color,
                BaseIconFactory iconFactory, float normalizationScale, UserHandle user) {
            return BitmapInfo.of(bitmap, color);
        }

+14 −4
Original line number Diff line number Diff line
@@ -29,8 +29,11 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

@@ -138,15 +141,16 @@ public class ClockDrawableWrapper extends AdaptiveIconDrawable implements Bitmap
    }

    @Override
    public BitmapInfo getExtendedInfo(Bitmap bitmap, int color, BaseIconFactory iconFactory) {
    public BitmapInfo getExtendedInfo(Bitmap bitmap, int color,
            BaseIconFactory iconFactory, float normalizationScale, UserHandle user) {
        iconFactory.disableColorExtraction();
        float [] scale = new float[1];
        AdaptiveIconDrawable background = new AdaptiveIconDrawable(
                getBackground().getConstantState().newDrawable(), null);
        BitmapInfo bitmapInfo = iconFactory.createBadgedIconBitmap(background,
                Process.myUserHandle(), mTargetSdkVersion, false, scale);
                Process.myUserHandle(), mTargetSdkVersion, false);

        return new ClockBitmapInfo(bitmap, color, scale[0], mAnimationInfo, bitmapInfo.icon);
        return new ClockBitmapInfo(bitmap, color, normalizationScale,
                mAnimationInfo, bitmapInfo.icon);
    }

    @Override
@@ -232,6 +236,12 @@ public class ClockDrawableWrapper extends AdaptiveIconDrawable implements Bitmap
            d.mDisabledAlpha = GraphicsUtils.getFloat(context, R.attr.disabledIconAlpha, 1f);
            return d;
        }

        @Nullable
        @Override
        public byte[] toByteArray() {
            return null;
        }
    }

    private static class ClockIconDrawable extends FastBitmapDrawable implements Runnable {
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ public class FastBitmapDrawable extends Drawable {
    @Nullable private ColorFilter mColorFilter;

    private boolean mIsPressed;
    private boolean mIsDisabled;
    protected boolean mIsDisabled;
    float mDisabledAlpha = 1f;

    // Animator and properties for the fast bitmap drawable's scale
Loading