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

Commit 43a1f4b4 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Updating AdaptiveIconDrawable to support themed icons

Bug: 201718966
Test: CTS updated
Change-Id: Ibb7e106ae11babe533d0787c6245557ebcc02d32
parent 67639c67
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -16510,11 +16510,13 @@ package android.graphics.drawable {
  public class AdaptiveIconDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Drawable.Callback {
  public class AdaptiveIconDrawable extends android.graphics.drawable.Drawable implements android.graphics.drawable.Drawable.Callback {
    ctor public AdaptiveIconDrawable(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable);
    ctor public AdaptiveIconDrawable(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable);
    ctor public AdaptiveIconDrawable(@Nullable android.graphics.drawable.Drawable, @Nullable android.graphics.drawable.Drawable, @Nullable android.graphics.drawable.Drawable);
    method public void draw(android.graphics.Canvas);
    method public void draw(android.graphics.Canvas);
    method public android.graphics.drawable.Drawable getBackground();
    method public android.graphics.drawable.Drawable getBackground();
    method public static float getExtraInsetFraction();
    method public static float getExtraInsetFraction();
    method public android.graphics.drawable.Drawable getForeground();
    method public android.graphics.drawable.Drawable getForeground();
    method public android.graphics.Path getIconMask();
    method public android.graphics.Path getIconMask();
    method @Nullable public android.graphics.drawable.Drawable getMonochrome();
    method public int getOpacity();
    method public int getOpacity();
    method public void invalidateDrawable(@NonNull android.graphics.drawable.Drawable);
    method public void invalidateDrawable(@NonNull android.graphics.drawable.Drawable);
    method public void scheduleDrawable(@NonNull android.graphics.drawable.Drawable, @NonNull Runnable, long);
    method public void scheduleDrawable(@NonNull android.graphics.drawable.Drawable, @NonNull Runnable, long);
+52 −21
Original line number Original line Diff line number Diff line
@@ -74,6 +74,10 @@ import java.io.IOException;
 *      getBounds().right + getBounds().getWidth() * #getExtraInsetFraction(),
 *      getBounds().right + getBounds().getWidth() * #getExtraInsetFraction(),
 *      getBounds().bottom + getBounds().getHeight() * #getExtraInsetFraction())
 *      getBounds().bottom + getBounds().getHeight() * #getExtraInsetFraction())
 * </pre>
 * </pre>
 *
 * <p>An alternate drawable can be specified using <code>&lt;monochrome></code> tag which can be
 * drawn in place of the two (background and foreground) layers. This drawable is tinted
 * according to the device or surface theme.
 */
 */
public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback {
public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback {


@@ -120,6 +124,7 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
     */
     */
    private static final int BACKGROUND_ID = 0;
    private static final int BACKGROUND_ID = 0;
    private static final int FOREGROUND_ID = 1;
    private static final int FOREGROUND_ID = 1;
    private static final int MONOCHROME_ID = 2;


    /**
    /**
     * State variable that maintains the {@link ChildDrawable} array.
     * State variable that maintains the {@link ChildDrawable} array.
@@ -188,6 +193,18 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
     */
     */
    public AdaptiveIconDrawable(Drawable backgroundDrawable,
    public AdaptiveIconDrawable(Drawable backgroundDrawable,
            Drawable foregroundDrawable) {
            Drawable foregroundDrawable) {
        this(backgroundDrawable, foregroundDrawable, null);
    }

    /**
     * Constructor used to dynamically create this drawable.
     *
     * @param backgroundDrawable drawable that should be rendered in the background
     * @param foregroundDrawable drawable that should be rendered in the foreground
     * @param monochromeDrawable an alternate drawable which can be tinted per system theme color
     */
    public AdaptiveIconDrawable(@Nullable Drawable backgroundDrawable,
            @Nullable Drawable foregroundDrawable, @Nullable Drawable monochromeDrawable) {
        this((LayerState)null, null);
        this((LayerState)null, null);
        if (backgroundDrawable != null) {
        if (backgroundDrawable != null) {
            addLayer(BACKGROUND_ID, createChildDrawable(backgroundDrawable));
            addLayer(BACKGROUND_ID, createChildDrawable(backgroundDrawable));
@@ -195,6 +212,9 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
        if (foregroundDrawable != null) {
        if (foregroundDrawable != null) {
            addLayer(FOREGROUND_ID, createChildDrawable(foregroundDrawable));
            addLayer(FOREGROUND_ID, createChildDrawable(foregroundDrawable));
        }
        }
        if (monochromeDrawable != null) {
            addLayer(MONOCHROME_ID, createChildDrawable(monochromeDrawable));
        }
    }
    }


    /**
    /**
@@ -227,9 +247,8 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
        state.mSourceDrawableId = Resources.getAttributeSetSourceResId(attrs);
        state.mSourceDrawableId = Resources.getAttributeSetSourceResId(attrs);


        final ChildDrawable[] array = state.mChildren;
        final ChildDrawable[] array = state.mChildren;
        for (int i = 0; i < state.mChildren.length; i++) {
        for (int i = 0; i < array.length; i++) {
            final ChildDrawable layer = array[i];
            array[i].setDensity(deviceDensity);
            layer.setDensity(deviceDensity);
        }
        }


        inflateLayers(r, parser, attrs, theme);
        inflateLayers(r, parser, attrs, theme);
@@ -286,6 +305,18 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
        return mLayerState.mChildren[BACKGROUND_ID].mDrawable;
        return mLayerState.mChildren[BACKGROUND_ID].mDrawable;
    }
    }



    /**
     * Returns the monochrome version of this drawable. Callers can use a tinted version of
     * this drawable instead of the original drawable on surfaces stressing user theming.
     *
     *  @return the monochrome drawable
     */
    @Nullable
    public Drawable getMonochrome() {
        return mLayerState.mChildren[MONOCHROME_ID].mDrawable;
    }

    @Override
    @Override
    protected void onBoundsChange(Rect bounds) {
    protected void onBoundsChange(Rect bounds) {
        if (bounds.isEmpty()) {
        if (bounds.isEmpty()) {
@@ -316,9 +347,6 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback


        for (int i = 0, count = mLayerState.N_CHILDREN; i < count; i++) {
        for (int i = 0, count = mLayerState.N_CHILDREN; i < count; i++) {
            final ChildDrawable r = mLayerState.mChildren[i];
            final ChildDrawable r = mLayerState.mChildren[i];
            if (r == null) {
                continue;
            }
            final Drawable d = r.mDrawable;
            final Drawable d = r.mDrawable;
            if (d == null) {
            if (d == null) {
                continue;
                continue;
@@ -359,14 +387,11 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
        if (mLayersShader == null) {
        if (mLayersShader == null) {
            mCanvas.setBitmap(mLayersBitmap);
            mCanvas.setBitmap(mLayersBitmap);
            mCanvas.drawColor(Color.BLACK);
            mCanvas.drawColor(Color.BLACK);
            for (int i = 0; i < mLayerState.N_CHILDREN; i++) {
            if (mLayerState.mChildren[BACKGROUND_ID].mDrawable != null) {
                if (mLayerState.mChildren[i] == null) {
                mLayerState.mChildren[BACKGROUND_ID].mDrawable.draw(mCanvas);
                    continue;
                }
                final Drawable dr = mLayerState.mChildren[i].mDrawable;
                if (dr != null) {
                    dr.draw(mCanvas);
            }
            }
            if (mLayerState.mChildren[FOREGROUND_ID].mDrawable != null) {
                mLayerState.mChildren[FOREGROUND_ID].mDrawable.draw(mCanvas);
            }
            }
            mLayersShader = new BitmapShader(mLayersBitmap, TileMode.CLAMP, TileMode.CLAMP);
            mLayersShader = new BitmapShader(mLayersBitmap, TileMode.CLAMP, TileMode.CLAMP);
            mPaint.setShader(mLayersShader);
            mPaint.setShader(mLayersShader);
@@ -480,11 +505,17 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
                continue;
                continue;
            }
            }
            String tagName = parser.getName();
            String tagName = parser.getName();
            if (tagName.equals("background")) {
            switch (tagName) {
                case "background":
                    childIndex = BACKGROUND_ID;
                    childIndex = BACKGROUND_ID;
            } else if (tagName.equals("foreground")) {
                    break;
                case "foreground":
                    childIndex = FOREGROUND_ID;
                    childIndex = FOREGROUND_ID;
            } else {
                    break;
                case "monochrome":
                    childIndex = MONOCHROME_ID;
                    break;
                default:
                    continue;
                    continue;
            }
            }


@@ -941,7 +972,7 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
    static class LayerState extends ConstantState {
    static class LayerState extends ConstantState {
        private int[] mThemeAttrs;
        private int[] mThemeAttrs;


        final static int N_CHILDREN = 2;
        static final int N_CHILDREN = 3;
        ChildDrawable[] mChildren;
        ChildDrawable[] mChildren;


        // The density at which to render the drawable and its children.
        // The density at which to render the drawable and its children.