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

Commit 5bac3741 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Fix icon color animator

The animation for the icon color change in QS assumed that the colors
were grayscale (leftover from the old tiles). Since the colors are not
necessarily grayscale, animate using an ArgbEvaluator.

Fixes: 225043591
Test: manual
Change-Id: I85a05f31704651fbf953efa44bde71f741574916
parent edbe5bf3
Loading
Loading
Loading
Loading
+39 −25
Original line number Original line Diff line number Diff line
@@ -16,13 +16,14 @@ package com.android.systemui.qs.tileimpl;


import android.animation.Animator;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorListenerAdapter;
import android.animation.ArgbEvaluator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.Context;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.Animatable2.AnimationCallback;
import android.graphics.drawable.Animatable2.AnimationCallback;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable;
@@ -53,6 +54,8 @@ public class QSIconViewImpl extends QSIconView {
    @Nullable
    @Nullable
    private QSTile.Icon mLastIcon;
    private QSTile.Icon mLastIcon;


    private ValueAnimator mColorAnimator = new ValueAnimator();

    public QSIconViewImpl(Context context) {
    public QSIconViewImpl(Context context) {
        super(context);
        super(context);


@@ -61,6 +64,7 @@ public class QSIconViewImpl extends QSIconView {


        mIcon = createIcon();
        mIcon = createIcon();
        addView(mIcon);
        addView(mIcon);
        mColorAnimator.setDuration(QS_ANIM_LENGTH);
    }
    }


    @Override
    @Override
@@ -165,7 +169,6 @@ public class QSIconViewImpl extends QSIconView {
            mState = state.state;
            mState = state.state;
            if (mTint != 0 && allowAnimations && shouldAnimate(iv)) {
            if (mTint != 0 && allowAnimations && shouldAnimate(iv)) {
                animateGrayScale(mTint, color, iv, () -> updateIcon(iv, state, allowAnimations));
                animateGrayScale(mTint, color, iv, () -> updateIcon(iv, state, allowAnimations));
                mTint = color;
            } else {
            } else {
                if (iv instanceof AlphaControlledSlashImageView) {
                if (iv instanceof AlphaControlledSlashImageView) {
                    ((AlphaControlledSlashImageView)iv)
                    ((AlphaControlledSlashImageView)iv)
@@ -173,7 +176,6 @@ public class QSIconViewImpl extends QSIconView {
                } else {
                } else {
                    setTint(iv, color);
                    setTint(iv, color);
                }
                }
                mTint = color;
                updateIcon(iv, state, allowAnimations);
                updateIcon(iv, state, allowAnimations);
            }
            }
        } else {
        } else {
@@ -191,39 +193,30 @@ public class QSIconViewImpl extends QSIconView {
            ((AlphaControlledSlashImageView)iv)
            ((AlphaControlledSlashImageView)iv)
                    .setFinalImageTintList(ColorStateList.valueOf(toColor));
                    .setFinalImageTintList(ColorStateList.valueOf(toColor));
        }
        }
        mColorAnimator.cancel();
        if (mAnimationEnabled && ValueAnimator.areAnimatorsEnabled()) {
        if (mAnimationEnabled && ValueAnimator.areAnimatorsEnabled()) {
            final float fromAlpha = Color.alpha(fromColor);
            PropertyValuesHolder values = PropertyValuesHolder.ofInt("color", fromColor, toColor);
            final float toAlpha = Color.alpha(toColor);
            values.setEvaluator(ArgbEvaluator.getInstance());
            final float fromChannel = Color.red(fromColor);
            mColorAnimator.setValues(values);
            final float toChannel = Color.red(toColor);
            mColorAnimator.removeAllListeners();

            mColorAnimator.addUpdateListener(animation -> {
            ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
                setTint(iv, (int) animation.getAnimatedValue());
            anim.setDuration(QS_ANIM_LENGTH);
            anim.addUpdateListener(animation -> {
                float fraction = animation.getAnimatedFraction();
                int alpha = (int) (fromAlpha + (toAlpha - fromAlpha) * fraction);
                int channel = (int) (fromChannel + (toChannel - fromChannel) * fraction);

                setTint(iv, Color.argb(alpha, channel, channel, channel));
            });
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    endRunnable.run();
                }
            });
            });
            anim.start();
            mColorAnimator.addListener(new EndRunnableAnimatorListener(endRunnable));

            mColorAnimator.start();
        } else {
        } else {

            setTint(iv, toColor);
            setTint(iv, toColor);
            endRunnable.run();
            endRunnable.run();
        }
        }
    }
    }


    public static void setTint(ImageView iv, int color) {
    public void setTint(ImageView iv, int color) {
        iv.setImageTintList(ColorStateList.valueOf(color));
        iv.setImageTintList(ColorStateList.valueOf(color));
        mTint = color;
    }
    }



    protected int getIconMeasureMode() {
    protected int getIconMeasureMode() {
        return MeasureSpec.EXACTLY;
        return MeasureSpec.EXACTLY;
    }
    }
@@ -261,4 +254,25 @@ public class QSIconViewImpl extends QSIconView {
                return 0;
                return 0;
        }
        }
    }
    }

    private static class EndRunnableAnimatorListener extends AnimatorListenerAdapter {
        private Runnable mRunnable;

        EndRunnableAnimatorListener(Runnable endRunnable) {
            super();
            mRunnable = endRunnable;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            super.onAnimationCancel(animation);
            mRunnable.run();
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            mRunnable.run();
        }
    }
}
}