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

Commit 0c4729a1 authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Revert "Postpone AnimatedVectorDrawable animator inflation until applyTheme()"" into mnc-dev

parents f8575f66 344ad712
Loading
Loading
Loading
Loading
+13 −106
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.Animator.AnimatorListener;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
@@ -128,19 +127,13 @@ import java.util.List;
 * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_animation
 */
public class AnimatedVectorDrawable extends Drawable implements Animatable {
    private static final String LOGTAG = "AnimatedVectorDrawable";
    private static final String LOGTAG = AnimatedVectorDrawable.class.getSimpleName();

    private static final String ANIMATED_VECTOR = "animated-vector";
    private static final String TARGET = "target";

    private static final boolean DBG_ANIMATION_VECTOR_DRAWABLE = false;

    /**
     * The resources against which this drawable was created. Used to attempt
     * to inflate animators if applyTheme() doesn't get called.
     */
    private Resources mRes;

    private AnimatedVectorDrawableState mAnimatedVectorState;

    private boolean mMutated;
@@ -151,7 +144,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {

    private AnimatedVectorDrawable(AnimatedVectorDrawableState state, Resources res) {
        mAnimatedVectorState = new AnimatedVectorDrawableState(state, mCallback, res);
        mRes = res;
    }

    @Override
@@ -281,7 +273,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
    @Override
    public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme)
            throws XmlPullParserException, IOException {
        final AnimatedVectorDrawableState state = mAnimatedVectorState;

        int eventType = parser.getEventType();
        float pathErrorScale = 1;
@@ -299,10 +290,10 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
                        vectorDrawable.setAllowCaching(false);
                        vectorDrawable.setCallback(mCallback);
                        pathErrorScale = vectorDrawable.getPixelSize();
                        if (state.mVectorDrawable != null) {
                            state.mVectorDrawable.setCallback(null);
                        if (mAnimatedVectorState.mVectorDrawable != null) {
                            mAnimatedVectorState.mVectorDrawable.setCallback(null);
                        }
                        state.mVectorDrawable = vectorDrawable;
                        mAnimatedVectorState.mVectorDrawable = vectorDrawable;
                    }
                    a.recycle();
                } else if (TARGET.equals(tagName)) {
@@ -311,25 +302,12 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
                    final String target = a.getString(
                            R.styleable.AnimatedVectorDrawableTarget_name);

                    final int animResId = a.getResourceId(
                    int id = a.getResourceId(
                            R.styleable.AnimatedVectorDrawableTarget_animation, 0);
                    if (animResId != 0) {
                        if (theme != null) {
                            final Animator objectAnimator = AnimatorInflater.loadAnimator(
                                    res, theme, animResId, pathErrorScale);
                    if (id != 0) {
                        Animator objectAnimator = AnimatorInflater.loadAnimator(res, theme, id,
                                pathErrorScale);
                        setupAnimatorsForTarget(target, objectAnimator);
                        } else {
                            // The animation may be theme-dependent. As a
                            // workaround until Animator has full support for
                            // applyTheme(), postpone loading the animator
                            // until we have a theme in applyTheme().
                            if (state.mPendingAnims == null) {
                                state.mPendingAnims = new ArrayList<>(1);
                            }
                            state.mPendingAnims.add(
                                    new PendingAnimator(animResId, pathErrorScale, target));

                        }
                    }
                    a.recycle();
                }
@@ -337,13 +315,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {

            eventType = parser.next();
        }

        // If we don't have any pending animations, we don't need to hold a
        // reference to the resources.
        if (state.mPendingAnims == null) {
            mRes = null;
        }

        setupAnimatorSet();
    }

@@ -369,29 +340,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
        if (vectorDrawable != null && vectorDrawable.canApplyTheme()) {
            vectorDrawable.applyTheme(t);
        }

        if (t != null) {
            inflatePendingAnimators(t.getResources(), t);
        }
    }

    /**
     * Inflates pending animators, if any, against a theme. Clears the list of
     * pending animators.
     *
     * @param t the theme against which to inflate the animators
     */
    private void inflatePendingAnimators(@NonNull Resources res, @Nullable Theme t) {
        final ArrayList<PendingAnimator> pendingAnims = mAnimatedVectorState.mPendingAnims;
        if (pendingAnims != null) {
            mAnimatedVectorState.mPendingAnims = null;

            for (int i = 0, count = pendingAnims.size(); i < count; i++) {
                final PendingAnimator pendingAnimator = pendingAnims.get(i);
                final Animator objectAnimator = pendingAnimator.newInstance(res, t);
                setupAnimatorsForTarget(pendingAnimator.target, objectAnimator);
            }
        }
    }

    /**
@@ -433,7 +381,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
        // we add this array into the mAnimatorSet.
        private ArrayList<Animator> mTempAnimators;
        ArrayMap<Animator, String> mTargetNameMap;
        ArrayList<PendingAnimator> mPendingAnims;

        public AnimatedVectorDrawableState(AnimatedVectorDrawableState copy,
                Callback owner, Resources res) {
@@ -456,7 +403,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
                    final int numAnimators = copy.mTargetNameMap.size();
                    // Deep copy a animator set, and then setup the target map again.
                    mAnimatorSet = copy.mAnimatorSet.clone();
                    mTargetNameMap = new ArrayMap<>(numAnimators);
                    mTargetNameMap = new ArrayMap<Animator, String>(numAnimators);
                    // Since the new AnimatorSet is cloned from the old one, the order must be the
                    // same inside the array.
                    ArrayList<Animator> oldAnim = copy.mAnimatorSet.getChildAnimations();
@@ -471,9 +418,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
                        mTargetNameMap.put(newAnim.get(i), targetName);
                    }
                }

                // Shallow copy since the array is immutable after inflate().
                mPendingAnims = copy.mPendingAnims;
            } else {
                mVectorDrawable = new VectorDrawable();
            }
@@ -482,7 +426,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
        @Override
        public boolean canApplyTheme() {
            return (mVectorDrawable != null && mVectorDrawable.canApplyTheme())
                    || mPendingAnims != null || super.canApplyTheme();
                    || super.canApplyTheme();
        }

        @Override
@@ -501,32 +445,12 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
        }
    }

    /**
     * Basically a constant state for Animators until we actually implement
     * constant states for Animators.
     */
    private static class PendingAnimator {
        public final int animResId;
        public final float pathErrorScale;
        public final String target;

        public PendingAnimator(int animResId, float pathErrorScale, String target) {
            this.animResId = animResId;
            this.pathErrorScale = pathErrorScale;
            this.target = target;
        }

        public Animator newInstance(Resources res, Theme theme) {
            return AnimatorInflater.loadAnimator(res, theme, animResId, pathErrorScale);
        }
    }

    private void setupAnimatorsForTarget(String name, Animator animator) {
        Object target = mAnimatedVectorState.mVectorDrawable.getTargetByName(name);
        animator.setTarget(target);
        if (mAnimatedVectorState.mTempAnimators == null) {
            mAnimatedVectorState.mTempAnimators = new ArrayList<>();
            mAnimatedVectorState.mTargetNameMap = new ArrayMap<>();
            mAnimatedVectorState.mTempAnimators = new ArrayList<Animator>();
            mAnimatedVectorState.mTargetNameMap = new ArrayMap<Animator, String>();
        }
        mAnimatedVectorState.mTempAnimators.add(animator);
        mAnimatedVectorState.mTargetNameMap.put(animator, name);
@@ -550,23 +474,6 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
        if (isStarted()) {
            return;
        }

        // Check for uninflated animators. We can remove this after we add
        // support for Animator.applyTheme(). See comments in inflate().
        if (mAnimatedVectorState.mPendingAnims != null) {
            // Attempt to load animators without applying a theme.
            if (mRes != null) {
                inflatePendingAnimators(mRes, null);
                mRes = null;
            } else {
                Log.e(LOGTAG, "Failed to load animators. Either the AnimatedVectorDrawable must be"
                        + " created using a Resources object or applyTheme() must be called with"
                        + " a non-null Theme object.");
            }

            mAnimatedVectorState.mPendingAnims = null;
        }

        mAnimatedVectorState.mAnimatorSet.start();
        invalidateSelf();
    }