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

Commit 87d01e72 authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Exposing some utility methods in icon lib" into main

parents ef8a1965 115a6dec
Loading
Loading
Loading
Loading
+0 −22
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2017 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.
-->
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@color/legacy_icon_background"/>
    <foreground>
        <com.android.launcher3.icons.FixedScaleDrawable />
    </foreground>
</adaptive-icon>
+0 −1
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@
    <color name="themed_icon_background_color">#D3E3FD</color>
    <color name="themed_badge_icon_color">#0842A0</color>
    <color name="themed_badge_icon_background_color">#D3E3FD</color>
    <color name="legacy_icon_background">#FFFFFF</color>

    <!-- Yellow 600, used for highlighting "important" conversations in settings & notifications -->
    <color name="important_conversation">#f9ab00</color>
+60 −10
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ 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;
@@ -52,6 +53,7 @@ import java.util.Objects;
public class BaseIconFactory implements AutoCloseable {

    private static final int DEFAULT_WRAPPER_BACKGROUND = Color.WHITE;
    private static final float LEGACY_ICON_SCALE = .7f * (1f / (1 + 2 * getExtraInsetFraction()));

    public static final int MODE_DEFAULT = 0;
    public static final int MODE_ALPHA = 1;
@@ -309,24 +311,19 @@ public class BaseIconFactory implements AutoCloseable {
        if (icon == null) {
            return null;
        }
        float scale = 1f;

        float scale;
        if (shrinkNonAdaptiveIcons && !(icon instanceof AdaptiveIconDrawable)) {
            if (mWrapperIcon == null) {
                mWrapperIcon = mContext.getDrawable(R.drawable.adaptive_icon_drawable_wrapper)
                        .mutate();
            }
            AdaptiveIconDrawable dr = (AdaptiveIconDrawable) mWrapperIcon;
            EmptyWrapper foreground = new EmptyWrapper();
            AdaptiveIconDrawable dr = new AdaptiveIconDrawable(
                    new ColorDrawable(mWrapperBackgroundColor), foreground);
            dr.setBounds(0, 0, 1, 1);
            boolean[] outShape = new boolean[1];
            scale = getNormalizer().getScale(icon, outIconBounds, dr.getIconMask(), outShape);
            if (!outShape[0]) {
                FixedScaleDrawable fsd = ((FixedScaleDrawable) dr.getForeground());
                fsd.setDrawable(icon);
                fsd.setScale(scale);
                foreground.setDrawable(createScaledDrawable(icon, scale * LEGACY_ICON_SCALE));
                icon = dr;
                scale = getNormalizer().getScale(icon, outIconBounds, null, null);
                ((ColorDrawable) dr.getBackground()).setColor(mWrapperBackgroundColor);
            }
        } else {
            scale = getNormalizer().getScale(icon, outIconBounds, null, null);
@@ -336,6 +333,46 @@ public class BaseIconFactory implements AutoCloseable {
        return icon;
    }

    /**
     * Returns a drawable which draws the original drawable at a fixed scale
     */
    private Drawable createScaledDrawable(@NonNull Drawable main, float scale) {
        float h = main.getIntrinsicHeight();
        float w = main.getIntrinsicWidth();
        float scaleX = scale;
        float scaleY = scale;
        if (h > w && w > 0) {
            scaleX *= w / h;
        } else if (w > h && h > 0) {
            scaleY *= h / w;
        }
        scaleX = (1 - scaleX) / 2;
        scaleY = (1 - scaleY) / 2;
        return new InsetDrawable(main, scaleX, scaleY, scaleX, scaleY);
    }

    /**
     * Wraps the provided icon in an adaptive icon drawable
     */
    public AdaptiveIconDrawable wrapToAdaptiveIcon(@NonNull Drawable icon) {
        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);
            boolean[] outShape = new boolean[1];
            float scale = getNormalizer().getScale(icon, null, dr.getIconMask(), outShape);
            if (!outShape[0]) {
                foreground.setDrawable(createScaledDrawable(icon, scale * LEGACY_ICON_SCALE));
            } else {
                foreground.setDrawable(createScaledDrawable(icon, 1 - getExtraInsetFraction()));
            }
            return dr;
        }
    }

    @NonNull
    protected Bitmap createIconBitmap(@Nullable final Drawable icon, final float scale) {
        return createIconBitmap(icon, scale, MODE_DEFAULT);
@@ -615,4 +652,17 @@ public class BaseIconFactory implements AutoCloseable {
                    mTextPaint);
        }
    }

    private static class EmptyWrapper extends DrawableWrapper {

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

        @Override
        public ConstantState getConstantState() {
            Drawable d = getDrawable();
            return d == null ? null : d.getConstantState();
        }
    }
}
+28 −15
Original line number Diff line number Diff line
@@ -16,10 +16,10 @@
package com.android.launcher3.icons;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
@@ -151,23 +151,36 @@ public class BitmapInfo {
    protected void applyFlags(Context context, FastBitmapDrawable drawable,
            @DrawableCreationFlags int creationFlags) {
        drawable.mDisabledAlpha = GraphicsUtils.getFloat(context, R.attr.disabledIconAlpha, 1f);
        drawable.mCreationFlags = creationFlags;
        if ((creationFlags & FLAG_NO_BADGE) == 0) {
            Drawable badge = getBadgeDrawable(context, (creationFlags & FLAG_THEMED) != 0);
            if (badge != null) {
                drawable.setBadge(badge);
            }
        }
    }

    /**
     * Returns a drawable representing the badge for this info
     */
    @Nullable
    public Drawable getBadgeDrawable(Context context, boolean isThemed) {
        if (badgeInfo != null) {
                drawable.setBadge(badgeInfo.newIcon(context, creationFlags));
            return badgeInfo.newIcon(context, isThemed ? FLAG_THEMED : 0);
        } else if ((flags & FLAG_INSTANT) != 0) {
                drawable.setBadge(context.getDrawable(drawable.isThemed()
            return context.getDrawable(isThemed
                    ? R.drawable.ic_instant_app_badge_themed
                        : R.drawable.ic_instant_app_badge));
                    : R.drawable.ic_instant_app_badge);
        } else if ((flags & FLAG_WORK) != 0) {
                drawable.setBadge(context.getDrawable(drawable.isThemed()
            return context.getDrawable(isThemed
                    ? R.drawable.ic_work_app_badge_themed
                        : R.drawable.ic_work_app_badge));
                    : R.drawable.ic_work_app_badge);
        } else if ((flags & FLAG_CLONE) != 0) {
                drawable.setBadge(context.getDrawable(drawable.isThemed()
            return context.getDrawable(isThemed
                    ? R.drawable.ic_clone_app_badge_themed
                        : R.drawable.ic_clone_app_badge));
            }
                    : R.drawable.ic_clone_app_badge);
        }
        return null;
    }

    public static BitmapInfo fromBitmap(@NonNull Bitmap bitmap) {
+17 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.launcher3.icons;

import static com.android.launcher3.icons.BaseIconFactory.getBadgeSizeForIconSize;
import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED;
import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;

import android.animation.ObjectAnimator;
@@ -40,6 +41,8 @@ import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.ColorUtils;

import com.android.launcher3.icons.BitmapInfo.DrawableCreationFlags;

public class FastBitmapDrawable extends Drawable implements Drawable.Callback {

    private static final Interpolator ACCEL = new AccelerateInterpolator();
@@ -71,6 +74,8 @@ public class FastBitmapDrawable extends Drawable implements Drawable.Callback {
    protected boolean mIsDisabled;
    float mDisabledAlpha = 1f;

    @DrawableCreationFlags int mCreationFlags = 0;

    // Animator and properties for the fast bitmap drawable's scale
    @VisibleForTesting protected static final FloatProperty<FastBitmapDrawable> SCALE
            = new FloatProperty<FastBitmapDrawable>("scale") {
@@ -155,6 +160,14 @@ public class FastBitmapDrawable extends Drawable implements Drawable.Callback {
        return false;
    }

    /**
     * Returns true if the drawable was created with theme, even if it doesn't
     * support theming itself.
     */
    public boolean isCreatedForTheme() {
        return isThemed() || (mCreationFlags & FLAG_THEMED) != 0;
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mColorFilter = cf;
@@ -317,6 +330,7 @@ public class FastBitmapDrawable extends Drawable implements Drawable.Callback {
        if (mBadge != null) {
            cs.mBadgeConstantState = mBadge.getConstantState();
        }
        cs.mCreationFlags = mCreationFlags;
        return cs;
    }

@@ -395,6 +409,8 @@ public class FastBitmapDrawable extends Drawable implements Drawable.Callback {
        protected boolean mIsDisabled;
        private ConstantState mBadgeConstantState;

        @DrawableCreationFlags int mCreationFlags = 0;

        public FastBitmapConstantState(Bitmap bitmap, int color) {
            mBitmap = bitmap;
            mIconColor = color;
@@ -411,6 +427,7 @@ public class FastBitmapDrawable extends Drawable implements Drawable.Callback {
            if (mBadgeConstantState != null) {
                drawable.setBadge(mBadgeConstantState.newDrawable());
            }
            drawable.mCreationFlags = mCreationFlags;
            return drawable;
        }

Loading