diff options
| author | Jelle Fresen <jellefresen@google.com> | 2018-11-30 10:23:25 +0000 |
|---|---|---|
| committer | Jelle Fresen <jellefresen@google.com> | 2018-12-21 15:18:09 +0000 |
| commit | 65d2171d0090ff78af3ebbd55bf19d5a55f2708b (patch) | |
| tree | f61417349e75f3798d4b279fb2eabe92fbc38872 /core/java | |
| parent | d5ad1252a3de026f9f57031cce62b9c39f7b3bb5 (diff) | |
Add add/remove for AnimationListener's
FragmentManagerImpl in AndroidX currently uses reflection to read
mListener, so it can wrap it with another listener. Adding add/remove
methods for AnimationListener's next to setAnimationListener removes the
need for AndroidX to touch mListener, which is private API.
Bug: 117519981
Test: atest AnimationTest
Change-Id: I69cb19d61078215ca6697b3d41f4c536decc2e6e
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/view/animation/Animation.java | 100 | ||||
| -rw-r--r-- | core/java/android/view/animation/AnimationSet.java | 8 |
2 files changed, 84 insertions, 24 deletions
diff --git a/core/java/android/view/animation/Animation.java b/core/java/android/view/animation/Animation.java index 0e1f7675c8c3..95a346f34d24 100644 --- a/core/java/android/view/animation/Animation.java +++ b/core/java/android/view/animation/Animation.java @@ -30,6 +30,9 @@ import android.util.TypedValue; import dalvik.system.CloseGuard; +import java.util.ArrayList; +import java.util.List; + /** * Abstraction for an Animation that can be applied to Views, Surfaces, or * other objects. See the {@link android.view.animation animation package @@ -182,10 +185,14 @@ public abstract class Animation implements Cloneable { Interpolator mInterpolator; /** - * The animation listener to be notified when the animation starts, ends or repeats. + * An animation listener to be notified when the animation starts, ends or repeats. */ - @UnsupportedAppUsage - AnimationListener mListener; + private AnimationListener mListener; + + /** + * A list of animation listeners to be notified when the animation starts, ends or repeats. + */ + private List<AnimationListener> mListeners; /** * Desired Z order mode during animation. @@ -371,23 +378,17 @@ public abstract class Animation implements Cloneable { if (mListenerHandler == null) { mOnStart = new Runnable() { public void run() { - if (mListener != null) { - mListener.onAnimationStart(Animation.this); - } + dispatchAnimationStart(); } }; mOnRepeat = new Runnable() { public void run() { - if (mListener != null) { - mListener.onAnimationRepeat(Animation.this); - } + dispatchAnimationRepeat(); } }; mOnEnd = new Runnable() { public void run() { - if (mListener != null) { - mListener.onAnimationEnd(Animation.this); - } + dispatchAnimationEnd(); } }; } @@ -830,6 +831,10 @@ public abstract class Animation implements Cloneable { return true; } + private boolean hasAnimationListener() { + return mListener != null || (mListeners != null && !mListeners.isEmpty()); + } + /** * <p>Binds an animation listener to this animation. The animation listener * is notified of animation events such as the end of the animation or the @@ -842,6 +847,32 @@ public abstract class Animation implements Cloneable { } /** + * <p>Adds an animation listener to this animation. The animation listener + * is notified of animation events such as the end of the animation or the + * repetition of the animation.</p> + * + * @param listener the animation listener to be notified + */ + public void addAnimationListener(AnimationListener listener) { + if (mListeners == null) { + mListeners = new ArrayList<>(1); + } + mListeners.add(listener); + } + + /** + * <p>Removes an animation listener that has been added with + * {@link #addAnimationListener(AnimationListener)}.</p> + * + * @param listener the animation listener to be removed + */ + public void removeAnimationListener(AnimationListener listener) { + if (mListeners != null) { + mListeners.remove(listener); + } + } + + /** * Gurantees that this animation has an interpolator. Will use * a AccelerateDecelerateInterpolator is nothing else was specified. */ @@ -947,26 +978,59 @@ public abstract class Animation implements Cloneable { } private void fireAnimationStart() { - if (mListener != null) { - if (mListenerHandler == null) mListener.onAnimationStart(this); + if (hasAnimationListener()) { + if (mListenerHandler == null) dispatchAnimationStart(); else mListenerHandler.postAtFrontOfQueue(mOnStart); } } private void fireAnimationRepeat() { - if (mListener != null) { - if (mListenerHandler == null) mListener.onAnimationRepeat(this); + if (hasAnimationListener()) { + if (mListenerHandler == null) dispatchAnimationRepeat(); else mListenerHandler.postAtFrontOfQueue(mOnRepeat); } } private void fireAnimationEnd() { - if (mListener != null) { - if (mListenerHandler == null) mListener.onAnimationEnd(this); + if (hasAnimationListener()) { + if (mListenerHandler == null) dispatchAnimationEnd(); else mListenerHandler.postAtFrontOfQueue(mOnEnd); } } + void dispatchAnimationStart() { + if (mListener != null) { + mListener.onAnimationStart(this); + } + if (mListeners != null && !mListeners.isEmpty()) { + for (AnimationListener listener : mListeners) { + listener.onAnimationStart(this); + } + } + } + + void dispatchAnimationRepeat() { + if (mListener != null) { + mListener.onAnimationRepeat(this); + } + if (mListeners != null && !mListeners.isEmpty()) { + for (AnimationListener listener : mListeners) { + listener.onAnimationRepeat(this); + } + } + } + + void dispatchAnimationEnd() { + if (mListener != null) { + mListener.onAnimationEnd(this); + } + if (mListeners != null && !mListeners.isEmpty()) { + for (AnimationListener listener : mListeners) { + listener.onAnimationEnd(this); + } + } + } + /** * Gets the transformation to apply at a specified point in time. Implementations of this * method should always replace the specified Transformation or document they are doing diff --git a/core/java/android/view/animation/AnimationSet.java b/core/java/android/view/animation/AnimationSet.java index 767024ecfecf..03c6ca69a592 100644 --- a/core/java/android/view/animation/AnimationSet.java +++ b/core/java/android/view/animation/AnimationSet.java @@ -389,16 +389,12 @@ public class AnimationSet extends Animation { } if (started && !mStarted) { - if (mListener != null) { - mListener.onAnimationStart(this); - } + dispatchAnimationStart(); mStarted = true; } if (ended != mEnded) { - if (mListener != null) { - mListener.onAnimationEnd(this); - } + dispatchAnimationEnd(); mEnded = ended; } |
