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

Commit 9893fd97 authored by Chet Haase's avatar Chet Haase Committed by Android (Google) Code Review
Browse files

Merge "Fix Animator cancel() behavior"

parents 9702b5e0 7dfacdb1
Loading
Loading
Loading
Loading
+84 −64
Original line number Original line Diff line number Diff line
@@ -87,11 +87,13 @@ public final class AnimatorSet extends Animator {
    private AnimatorSetListener mSetListener = null;
    private AnimatorSetListener mSetListener = null;


    /**
    /**
     * Flag indicating that the AnimatorSet has been canceled (by calling cancel() or end()).
     * Flag indicating that the AnimatorSet has been manually
     * terminated (by calling cancel() or end()).
     * This flag is used to avoid starting other animations when currently-playing
     * This flag is used to avoid starting other animations when currently-playing
     * child animations of this AnimatorSet end.
     * child animations of this AnimatorSet end. It also determines whether cancel/end
     * notifications are sent out via the normal AnimatorSetListener mechanism.
     */
     */
    boolean mCanceled = false;
    boolean mTerminated = false;


    // The amount of time in ms to delay starting the animation after start() is called
    // The amount of time in ms to delay starting the animation after start() is called
    private long mStartDelay = 0;
    private long mStartDelay = 0;
@@ -271,10 +273,11 @@ public final class AnimatorSet extends Animator {
    @SuppressWarnings("unchecked")
    @SuppressWarnings("unchecked")
    @Override
    @Override
    public void cancel() {
    public void cancel() {
        mCanceled = true;
        mTerminated = true;
        if (isRunning()) {
            ArrayList<AnimatorListener> tmpListeners = null;
            if (mListeners != null) {
            if (mListeners != null) {
            ArrayList<AnimatorListener> tmpListeners =
                tmpListeners = (ArrayList<AnimatorListener>) mListeners.clone();
                    (ArrayList<AnimatorListener>) mListeners.clone();
                for (AnimatorListener listener : tmpListeners) {
                for (AnimatorListener listener : tmpListeners) {
                    listener.onAnimationCancel(this);
                    listener.onAnimationCancel(this);
                }
                }
@@ -283,19 +286,16 @@ public final class AnimatorSet extends Animator {
                // If we're currently in the startDelay period, just cancel that animator and
                // If we're currently in the startDelay period, just cancel that animator and
                // send out the end event to all listeners
                // send out the end event to all listeners
                mDelayAnim.cancel();
                mDelayAnim.cancel();
            if (mListeners != null) {
            } else  if (mSortedNodes.size() > 0) {
                ArrayList<AnimatorListener> tmpListeners =
                for (Node node : mSortedNodes) {
                        (ArrayList<AnimatorListener>) mListeners.clone();
                    node.animation.cancel();
                }
            }
            if (tmpListeners != null) {
                for (AnimatorListener listener : tmpListeners) {
                for (AnimatorListener listener : tmpListeners) {
                    listener.onAnimationEnd(this);
                    listener.onAnimationEnd(this);
                }
                }
            }
            }
            return;
        }
        if (mSortedNodes.size() > 0) {
            for (Node node : mSortedNodes) {
                node.animation.cancel();
            }
        }
        }
    }
    }


@@ -307,7 +307,8 @@ public final class AnimatorSet extends Animator {
     */
     */
    @Override
    @Override
    public void end() {
    public void end() {
        mCanceled = true;
        mTerminated = true;
        if (isRunning()) {
            if (mSortedNodes.size() != mNodes.size()) {
            if (mSortedNodes.size() != mNodes.size()) {
                // hasn't been started yet - sort the nodes now, then end them
                // hasn't been started yet - sort the nodes now, then end them
                sortNodes();
                sortNodes();
@@ -326,6 +327,14 @@ public final class AnimatorSet extends Animator {
                    node.animation.end();
                    node.animation.end();
                }
                }
            }
            }
            if (mListeners != null) {
                ArrayList<AnimatorListener> tmpListeners =
                        (ArrayList<AnimatorListener>) mListeners.clone();
                for (AnimatorListener listener : tmpListeners) {
                    listener.onAnimationEnd(this);
                }
            }
        }
    }
    }


    /**
    /**
@@ -424,7 +433,7 @@ public final class AnimatorSet extends Animator {
    @SuppressWarnings("unchecked")
    @SuppressWarnings("unchecked")
    @Override
    @Override
    public void start() {
    public void start() {
        mCanceled = false;
        mTerminated = false;


        // First, sort the nodes (if necessary). This will ensure that sortedNodes
        // First, sort the nodes (if necessary). This will ensure that sortedNodes
        // contains the animation nodes in the correct order.
        // contains the animation nodes in the correct order.
@@ -437,7 +446,8 @@ public final class AnimatorSet extends Animator {
            ArrayList<AnimatorListener> oldListeners = node.animation.getListeners();
            ArrayList<AnimatorListener> oldListeners = node.animation.getListeners();
            if (oldListeners != null && oldListeners.size() > 0) {
            if (oldListeners != null && oldListeners.size() > 0) {
                for (AnimatorListener listener : oldListeners) {
                for (AnimatorListener listener : oldListeners) {
                    if (listener instanceof DependencyListener) {
                    if (listener instanceof DependencyListener ||
                            listener instanceof AnimatorSetListener) {
                        node.animation.removeListener(listener);
                        node.animation.removeListener(listener);
                    }
                    }
                }
                }
@@ -522,7 +532,7 @@ public final class AnimatorSet extends Animator {
         * and will populate any appropriate lists, when it is started.
         * and will populate any appropriate lists, when it is started.
         */
         */
        anim.mNeedsSort = true;
        anim.mNeedsSort = true;
        anim.mCanceled = false;
        anim.mTerminated = false;
        anim.mPlayingSet = new ArrayList<Animator>();
        anim.mPlayingSet = new ArrayList<Animator>();
        anim.mNodeMap = new HashMap<Animator, Node>();
        anim.mNodeMap = new HashMap<Animator, Node>();
        anim.mNodes = new ArrayList<Node>();
        anim.mNodes = new ArrayList<Node>();
@@ -640,7 +650,7 @@ public final class AnimatorSet extends Animator {
         * @param dependencyAnimation the animation that sent the event.
         * @param dependencyAnimation the animation that sent the event.
         */
         */
        private void startIfReady(Animator dependencyAnimation) {
        private void startIfReady(Animator dependencyAnimation) {
            if (mAnimatorSet.mCanceled) {
            if (mAnimatorSet.mTerminated) {
                // if the parent AnimatorSet was canceled, then don't start any dependent anims
                // if the parent AnimatorSet was canceled, then don't start any dependent anims
                return;
                return;
            }
            }
@@ -676,6 +686,9 @@ public final class AnimatorSet extends Animator {
        }
        }


        public void onAnimationCancel(Animator animation) {
        public void onAnimationCancel(Animator animation) {
            if (!mTerminated) {
                // Listeners are already notified of the AnimatorSet canceling in cancel().
                // The logic below only kicks in when animations end normally
                if (mPlayingSet.size() == 0) {
                if (mPlayingSet.size() == 0) {
                    if (mListeners != null) {
                    if (mListeners != null) {
                        int numListeners = mListeners.size();
                        int numListeners = mListeners.size();
@@ -685,6 +698,7 @@ public final class AnimatorSet extends Animator {
                    }
                    }
                }
                }
            }
            }
        }


        @SuppressWarnings("unchecked")
        @SuppressWarnings("unchecked")
        public void onAnimationEnd(Animator animation) {
        public void onAnimationEnd(Animator animation) {
@@ -692,6 +706,9 @@ public final class AnimatorSet extends Animator {
            mPlayingSet.remove(animation);
            mPlayingSet.remove(animation);
            Node animNode = mAnimatorSet.mNodeMap.get(animation);
            Node animNode = mAnimatorSet.mNodeMap.get(animation);
            animNode.done = true;
            animNode.done = true;
            if (!mTerminated) {
                // Listeners are already notified of the AnimatorSet ending in cancel() or
                // end(); the logic below only kicks in when animations end normally
                ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes;
                ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes;
                boolean allDone = true;
                boolean allDone = true;
                int numSortedNodes = sortedNodes.size();
                int numSortedNodes = sortedNodes.size();
@@ -714,6 +731,7 @@ public final class AnimatorSet extends Animator {
                    }
                    }
                }
                }
            }
            }
        }


        // Nothing to do
        // Nothing to do
        public void onAnimationRepeat(Animator animation) {
        public void onAnimationRepeat(Animator animation) {
@@ -791,6 +809,8 @@ public final class AnimatorSet extends Animator {
                        }
                        }
                    }
                    }
                }
                }
                // nodes are 'done' by default; they become un-done when started, and done
                // again when ended
                node.done = false;
                node.done = false;
            }
            }
        }
        }
+7 −7
Original line number Original line Diff line number Diff line
@@ -933,6 +933,10 @@ public class ValueAnimator extends Animator {


    @Override
    @Override
    public void cancel() {
    public void cancel() {
        // Only cancel if the animation is actually running or has been started and is about
        // to run
        if (mPlayingState != STOPPED || sPendingAnimations.get().contains(this) ||
                sDelayedAnims.get().contains(this)) {
            if (mListeners != null) {
            if (mListeners != null) {
                ArrayList<AnimatorListener> tmpListeners =
                ArrayList<AnimatorListener> tmpListeners =
                        (ArrayList<AnimatorListener>) mListeners.clone();
                        (ArrayList<AnimatorListener>) mListeners.clone();
@@ -940,10 +944,6 @@ public class ValueAnimator extends Animator {
                    listener.onAnimationCancel(this);
                    listener.onAnimationCancel(this);
                }
                }
            }
            }
        // Only cancel if the animation is actually running or has been started and is about
        // to run
        if (mPlayingState != STOPPED || sPendingAnimations.get().contains(this) ||
                sDelayedAnims.get().contains(this)) {
            endAnimation();
            endAnimation();
        }
        }
    }
    }
+1 −1
Original line number Original line Diff line number Diff line
@@ -12,7 +12,7 @@ LOCAL_SRC_FILES := \
	$(call all-java-files-under, EnabledTestApp/src)
	$(call all-java-files-under, EnabledTestApp/src)


LOCAL_DX_FLAGS := --core-library
LOCAL_DX_FLAGS := --core-library
LOCAL_STATIC_JAVA_LIBRARIES := core-tests android-common frameworks-core-util-lib mockwebserver
LOCAL_STATIC_JAVA_LIBRARIES := core-tests android-common frameworks-core-util-lib mockwebserver guava
LOCAL_JAVA_LIBRARIES := android.test.runner
LOCAL_JAVA_LIBRARIES := android.test.runner
LOCAL_PACKAGE_NAME := FrameworksCoreTests
LOCAL_PACKAGE_NAME := FrameworksCoreTests


+7 −0
Original line number Original line Diff line number Diff line
@@ -1242,6 +1242,13 @@
            </intent-filter>
            </intent-filter>
        </activity>
        </activity>


        <activity android:name="android.animation.BasicAnimatorActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
            </intent-filter>
        </activity>

    </application>
    </application>


    <instrumentation android:name="android.test.InstrumentationTestRunner"
    <instrumentation android:name="android.test.InstrumentationTestRunner"
+11 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/animatingButton"/>
</LinearLayout>
 No newline at end of file
Loading