117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/*
217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Copyright (C) 2010 The Android Open Source Project
317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * you may not use this file except in compliance with the License.
617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * You may obtain a copy of the License at
717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
1017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Unless required by applicable law or agreed to in writing, software
1117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
1217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * See the License for the specific language governing permissions and
1417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * limitations under the License.
1517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
1617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepackage android.animation;
1817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.ArrayList;
2037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haaseimport java.util.Collection;
2117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.HashMap;
2237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haaseimport java.util.List;
2317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
2417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/**
25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class plays a set of {@link Animator} objects in the specified order. Animations
2617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * can be set up to play together, in sequence, or after a specified delay.
2717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There are two different approaches to adding animations to a <code>AnimatorSet</code>:
29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * either the {@link AnimatorSet#playTogether(Animator[]) playTogether()} or
30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorSet#playSequentially(Animator[]) playSequentially()} methods can be called to add
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * a set of animations all at once, or the {@link AnimatorSet#play(Animator)} can be
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * used in conjunction with methods in the {@link AnimatorSet.Builder Builder}
3317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * class to add animations
3417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * one by one.</p>
3517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>It is possible to set up a <code>AnimatorSet</code> with circular dependencies between
3717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * its animations. For example, an animation a1 could be set up to start before animation a2, a2
3817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * before a3, and a3 before a1. The results of this configuration are undefined, but will typically
3917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * result in none of the affected animations being played. Because of this (and because
4017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * circular dependencies do not make logical sense anyway), circular dependencies
4117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be avoided, and the dependency flow of animations should only be in one direction.
423aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
433aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
443aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
453aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code AnimatorSet}, read the
463aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#choreography">Property
473aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p>
483aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
4917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
50a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepublic final class AnimatorSet extends Animator {
5117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
5217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
5349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * Internal variables
5449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
5549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
5649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * to clone() to make deep copies of them.
5749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     */
5849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
5949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    /**
603b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase     * Tracks animations currently being played, so that we know what to
61a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * cancel or end when cancel() or end() is called on this AnimatorSet
6217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
63a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private ArrayList<Animator> mPlayingSet = new ArrayList<Animator>();
6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
66a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Contains all nodes, mapped to their respective Animators. When new
67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * dependency information is added for an Animator, we want to add it
68a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a single node representing that Animator, not create a new Node
6917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * if one already exists.
7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private HashMap<Animator, Node> mNodeMap = new HashMap<Animator, Node>();
7217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
7317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set of all nodes created for this AnimatorSet. This list is used upon
75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * starting the set, and the nodes are placed in sorted order into the
7617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sortedNodes collection.
7717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
7849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private ArrayList<Node> mNodes = new ArrayList<Node>();
7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The sorted list of nodes. This is the order in which the animations will
8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * be played. The details about when exactly they will be played depend
8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * on the dependency relationships of the nodes.
8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
8549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private ArrayList<Node> mSortedNodes = new ArrayList<Node>();
8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Flag indicating whether the nodes should be sorted prior to playing. This
8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * flag allows us to cache the previous sorted nodes so that if the sequence
9017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * is replayed with no changes, it does not have to re-sort the nodes again.
9117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
9217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean mNeedsSort = true;
9317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
94a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private AnimatorSetListener mSetListener = null;
9517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
977dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase     * Flag indicating that the AnimatorSet has been manually
987dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase     * terminated (by calling cancel() or end()).
99010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase     * This flag is used to avoid starting other animations when currently-playing
1007dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase     * child animations of this AnimatorSet end. It also determines whether cancel/end
1017dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase     * notifications are sent out via the normal AnimatorSetListener mechanism.
102010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase     */
1037dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase    boolean mTerminated = false;
104010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
1058b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    /**
1068b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Indicates whether an AnimatorSet has been start()'d, whether or
1078b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not there is a nonzero startDelay.
1088b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     */
1098b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    private boolean mStarted = false;
1108b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
11121cd1389d2ef218b20994b617c57af120841a57fChet Haase    // The amount of time in ms to delay starting the animation after start() is called
11221cd1389d2ef218b20994b617c57af120841a57fChet Haase    private long mStartDelay = 0;
11321cd1389d2ef218b20994b617c57af120841a57fChet Haase
114e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase    // Animator used for a nonzero startDelay
115e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase    private ValueAnimator mDelayAnim = null;
116e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase
11721cd1389d2ef218b20994b617c57af120841a57fChet Haase
11821cd1389d2ef218b20994b617c57af120841a57fChet Haase    // How long the child animations should last in ms. The default value is negative, which
119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // simply means that there is no duration set on the AnimatorSet. When a real duration is
12021cd1389d2ef218b20994b617c57af120841a57fChet Haase    // set, it is passed along to the child animations.
12121cd1389d2ef218b20994b617c57af120841a57fChet Haase    private long mDuration = -1;
12221cd1389d2ef218b20994b617c57af120841a57fChet Haase
123430742f09063574271e6c4091de13b9b9e762514Chet Haase    // Records the interpolator for the set. Null value indicates that no interpolator
124430742f09063574271e6c4091de13b9b9e762514Chet Haase    // was set on this AnimatorSet, so it should not be passed down to the children.
125430742f09063574271e6c4091de13b9b9e762514Chet Haase    private TimeInterpolator mInterpolator = null;
126430742f09063574271e6c4091de13b9b9e762514Chet Haase
12721cd1389d2ef218b20994b617c57af120841a57fChet Haase
128010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase    /**
129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets up this AnimatorSet to play all of the supplied animations at the same time.
130430742f09063574271e6c4091de13b9b9e762514Chet Haase     * This is equivalent to calling {@link #play(Animator)} with the first animator in the
131430742f09063574271e6c4091de13b9b9e762514Chet Haase     * set and then {@link Builder#with(Animator)} with each of the other animators. Note that
132430742f09063574271e6c4091de13b9b9e762514Chet Haase     * an Animator with a {@link Animator#setStartDelay(long) startDelay} will not actually
133430742f09063574271e6c4091de13b9b9e762514Chet Haase     * start until that delay elapses, which means that if the first animator in the list
134430742f09063574271e6c4091de13b9b9e762514Chet Haase     * supplied to this constructor has a startDelay, none of the other animators will start
135430742f09063574271e6c4091de13b9b9e762514Chet Haase     * until that first animator's startDelay has elapsed.
13617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
137a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param items The animations that will be started simultaneously.
13817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void playTogether(Animator... items) {
140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (items != null) {
14117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Builder builder = play(items[0]);
143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 1; i < items.length; ++i) {
144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                builder.with(items[i]);
14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
14617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
14717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
14817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
14917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
15037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * Sets up this AnimatorSet to play all of the supplied animations at the same time.
15137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     *
15237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started simultaneously.
15337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     */
15437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    public void playTogether(Collection<Animator> items) {
15537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        if (items != null && items.size() > 0) {
15637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            mNeedsSort = true;
15737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            Builder builder = null;
15837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            for (Animator anim : items) {
15937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                if (builder == null) {
16037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    builder = play(anim);
16137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                } else {
16237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    builder.with(anim);
16337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                }
16437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            }
16537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        }
16637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    }
16737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase
16837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    /**
169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets up this AnimatorSet to play each of the supplied animations when the
17017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * previous animation ends.
17117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
17237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started one after another.
17317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void playSequentially(Animator... items) {
175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (items != null) {
17617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (items.length == 1) {
178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                play(items[0]);
17917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                for (int i = 0; i < items.length - 1; ++i) {
181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    play(items[i]).before(items[i+1]);
18217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
18317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
18417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
18517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
18617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
18717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
18837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * Sets up this AnimatorSet to play each of the supplied animations when the
18937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * previous animation ends.
19037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     *
19137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started one after another.
19237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     */
19337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    public void playSequentially(List<Animator> items) {
19437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        if (items != null && items.size() > 0) {
19537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            mNeedsSort = true;
19637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            if (items.size() == 1) {
19737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                play(items.get(0));
19837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            } else {
19937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                for (int i = 0; i < items.size() - 1; ++i) {
20037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    play(items.get(i)).before(items.get(i+1));
20137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                }
20237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            }
20337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        }
20437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    }
20537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase
20637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    /**
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the current list of child Animator objects controlled by this
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet. This is a copy of the internal list; modifications to the returned list
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will not affect the AnimatorSet, although changes to the underlying Animator objects
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will affect those objects being managed by the AnimatorSet.
211f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     *
212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return ArrayList<Animator> The list of child animations of this AnimatorSet.
213f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ArrayList<Animator> getChildAnimations() {
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        ArrayList<Animator> childList = new ArrayList<Animator>();
216f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        for (Node node : mNodes) {
217f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            childList.add(node.animation);
218f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        }
219f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        return childList;
220f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    }
221f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
222f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
223811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     * Sets the target object for all current {@link #getChildAnimations() child animations}
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of this AnimatorSet that take targets ({@link ObjectAnimator} and
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet).
226811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     *
227811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     * @param target The object being animated
228811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     */
22921cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
230811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    public void setTarget(Object target) {
231811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase        for (Node node : mNodes) {
232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Animator animation = node.animation;
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (animation instanceof AnimatorSet) {
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ((AnimatorSet)animation).setTarget(target);
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            } else if (animation instanceof ObjectAnimator) {
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ((ObjectAnimator)animation).setTarget(target);
237811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            }
238811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase        }
239811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    }
240811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase
241811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    /**
242e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase     * Sets the TimeInterpolator for all current {@link #getChildAnimations() child animations}
243430742f09063574271e6c4091de13b9b9e762514Chet Haase     * of this AnimatorSet. The default value is null, which means that no interpolator
244430742f09063574271e6c4091de13b9b9e762514Chet Haase     * is set on this AnimatorSet. Setting the interpolator to any non-null value
245430742f09063574271e6c4091de13b9b9e762514Chet Haase     * will cause that interpolator to be set on the child animations
246430742f09063574271e6c4091de13b9b9e762514Chet Haase     * when the set is started.
24721cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param interpolator the interpolator to be used by each child animation of this AnimatorSet
24921cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
25021cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
251e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator interpolator) {
252430742f09063574271e6c4091de13b9b9e762514Chet Haase        mInterpolator = interpolator;
253430742f09063574271e6c4091de13b9b9e762514Chet Haase    }
254430742f09063574271e6c4091de13b9b9e762514Chet Haase
255430742f09063574271e6c4091de13b9b9e762514Chet Haase    @Override
256430742f09063574271e6c4091de13b9b9e762514Chet Haase    public TimeInterpolator getInterpolator() {
257430742f09063574271e6c4091de13b9b9e762514Chet Haase        return mInterpolator;
25821cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
25921cd1389d2ef218b20994b617c57af120841a57fChet Haase
26021cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
26117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method creates a <code>Builder</code> object, which is used to
26217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * set up playing constraints. This initial <code>play()</code> method
26317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * tells the <code>Builder</code> the animation that is the dependency for
26417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the succeeding commands to the <code>Builder</code>. For example,
265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calling <code>play(a1).with(a2)</code> sets up the AnimatorSet to play
26617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a1</code> and <code>a2</code> at the same time,
267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>play(a1).before(a2)</code> sets up the AnimatorSet to play
26817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a1</code> first, followed by <code>a2</code>, and
269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>play(a1).after(a2)</code> sets up the AnimatorSet to play
27017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a2</code> first, followed by <code>a1</code>.
27117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
27217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that <code>play()</code> is the only way to tell the
27317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Builder</code> the animation upon which the dependency is created,
27417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * so successive calls to the various functions in <code>Builder</code>
27517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * will all refer to the initial parameter supplied in <code>play()</code>
27617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * as the dependency of the other animations. For example, calling
27717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code>
27817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and <code>a3</code> when a1 ends; it does not set up a dependency between
27917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a2</code> and <code>a3</code>.</p>
28017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
28117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param anim The animation that is the dependency used in later calls to the
28217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * methods in the returned <code>Builder</code> object. A null parameter will result
28317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * in a null <code>Builder</code> return value.
284a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return Builder The object that constructs the AnimatorSet based on the dependencies
28517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * outlined in the calls to <code>play</code> and the other methods in the
28617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Builder</code object.
28717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
288a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Builder play(Animator anim) {
28917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (anim != null) {
29017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
29117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            return new Builder(anim);
29217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
29317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return null;
29417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
29517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
29617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
29717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
29817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
2998b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * <p>Note that canceling a <code>AnimatorSet</code> also cancels all of the animations that it
3008b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * is responsible for.</p>
30117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
30217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @SuppressWarnings("unchecked")
30317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
30417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void cancel() {
3057dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase        mTerminated = true;
3068b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        if (isStarted()) {
3077dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            ArrayList<AnimatorListener> tmpListeners = null;
308e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            if (mListeners != null) {
3097dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                tmpListeners = (ArrayList<AnimatorListener>) mListeners.clone();
310e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                for (AnimatorListener listener : tmpListeners) {
3117dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    listener.onAnimationCancel(this);
312e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                }
313e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            }
3147dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (mDelayAnim != null && mDelayAnim.isRunning()) {
3157dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // If we're currently in the startDelay period, just cancel that animator and
3167dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // send out the end event to all listeners
3177dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                mDelayAnim.cancel();
3187dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            } else  if (mSortedNodes.size() > 0) {
3197dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (Node node : mSortedNodes) {
3207dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    node.animation.cancel();
3217dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
3227dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
3237dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (tmpListeners != null) {
3247dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (AnimatorListener listener : tmpListeners) {
3257dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    listener.onAnimationEnd(this);
3267dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
32717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
3288b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            mStarted = false;
32917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
33017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
33117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
33217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
33317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
33417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
335a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note that ending a <code>AnimatorSet</code> also ends all of the animations that it is
33617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * responsible for.</p>
33717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
33817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
33917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void end() {
3407dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase        mTerminated = true;
3418b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        if (isStarted()) {
3427dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (mSortedNodes.size() != mNodes.size()) {
3437dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // hasn't been started yet - sort the nodes now, then end them
3447dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                sortNodes();
3457dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (Node node : mSortedNodes) {
3467dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    if (mSetListener == null) {
3477dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        mSetListener = new AnimatorSetListener(this);
3487dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    }
3497dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    node.animation.addListener(mSetListener);
3501e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase                }
3511e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            }
3527dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (mDelayAnim != null) {
3537dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                mDelayAnim.cancel();
3547dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
3557dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (mSortedNodes.size() > 0) {
3567dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (Node node : mSortedNodes) {
3577dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    node.animation.end();
3587dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
3597dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
3607dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (mListeners != null) {
3617dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                ArrayList<AnimatorListener> tmpListeners =
3627dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
3637dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (AnimatorListener listener : tmpListeners) {
3647dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    listener.onAnimationEnd(this);
3657dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
36617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
3678b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            mStarted = false;
36817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
36917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
37017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
37117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
3728b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Returns true if any of the child animations of this AnimatorSet have been started and have
3738b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not yet ended.
374a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return Whether this AnimatorSet has been started and has not yet ended.
375673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase     */
376673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    @Override
377673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    public boolean isRunning() {
378673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        for (Node node : mNodes) {
379673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase            if (node.animation.isRunning()) {
380673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase                return true;
381673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase            }
382673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        }
3838b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return false;
3848b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    }
3858b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
3868b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    @Override
3878b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    public boolean isStarted() {
3888b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return mStarted;
389673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    }
390673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase
391673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    /**
39221cd1389d2ef218b20994b617c57af120841a57fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
39321cd1389d2ef218b20994b617c57af120841a57fChet Haase     * {@link #start()} is called.
39421cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
39521cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @return the number of milliseconds to delay running the animation
39621cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
39721cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
39821cd1389d2ef218b20994b617c57af120841a57fChet Haase    public long getStartDelay() {
39921cd1389d2ef218b20994b617c57af120841a57fChet Haase        return mStartDelay;
40021cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
40121cd1389d2ef218b20994b617c57af120841a57fChet Haase
40221cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
40321cd1389d2ef218b20994b617c57af120841a57fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
40421cd1389d2ef218b20994b617c57af120841a57fChet Haase     * {@link #start()} is called.
40521cd1389d2ef218b20994b617c57af120841a57fChet Haase
40621cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param startDelay The amount of the delay, in milliseconds
40721cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
40821cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
40921cd1389d2ef218b20994b617c57af120841a57fChet Haase    public void setStartDelay(long startDelay) {
41021cd1389d2ef218b20994b617c57af120841a57fChet Haase        mStartDelay = startDelay;
41121cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
41221cd1389d2ef218b20994b617c57af120841a57fChet Haase
41321cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
414a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the length of each of the child animations of this AnimatorSet. This value may
415a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be less than 0, which indicates that no duration has been set on this AnimatorSet
41621cd1389d2ef218b20994b617c57af120841a57fChet Haase     * and each of the child animations will use their own duration.
41721cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
41821cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @return The length of the animation, in milliseconds, of each of the child
419a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations of this AnimatorSet.
42021cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
42121cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
42221cd1389d2ef218b20994b617c57af120841a57fChet Haase    public long getDuration() {
42321cd1389d2ef218b20994b617c57af120841a57fChet Haase        return mDuration;
42421cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
42521cd1389d2ef218b20994b617c57af120841a57fChet Haase
42621cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
427a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the length of each of the current child animations of this AnimatorSet. By default,
428a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * each child animation will use its own duration. If the duration is set on the AnimatorSet,
42921cd1389d2ef218b20994b617c57af120841a57fChet Haase     * then each child animation inherits this duration.
43021cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
43121cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param duration The length of the animation, in milliseconds, of each of the child
432a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations of this AnimatorSet.
43321cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
43421cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
4352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public AnimatorSet setDuration(long duration) {
43621cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (duration < 0) {
43721cd1389d2ef218b20994b617c57af120841a57fChet Haase            throw new IllegalArgumentException("duration must be a value of zero or greater");
43821cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
439c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase        // Just record the value for now - it will be used later when the AnimatorSet starts
44021cd1389d2ef218b20994b617c57af120841a57fChet Haase        mDuration = duration;
4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
44221cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
44321cd1389d2ef218b20994b617c57af120841a57fChet Haase
4442970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    @Override
4452970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    public void setupStartValues() {
4462970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        for (Node node : mNodes) {
4472970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            node.animation.setupStartValues();
4482970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
4492970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    }
4502970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase
4512970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    @Override
4522970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    public void setupEndValues() {
4532970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        for (Node node : mNodes) {
4542970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            node.animation.setupEndValues();
4552970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
4562970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    }
4572970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase
45821cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
46017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
461a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Starting this <code>AnimatorSet</code> will, in turn, start the animations for which
46217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * it is responsible. The details of when exactly those animations are started depends on
46317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the dependency relationships that have been set up between the animations.
46417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
46517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @SuppressWarnings("unchecked")
46617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
46717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void start() {
4687dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase        mTerminated = false;
4698b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mStarted = true;
470010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
471c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase        if (mDuration >= 0) {
472c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase            // If the duration was set on this AnimatorSet, pass it along to all child animations
473c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase            for (Node node : mNodes) {
474c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase                // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to
475c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase                // insert "play-after" delays
476c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase                node.animation.setDuration(mDuration);
477c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase            }
478c299a3384171e36fc9ab6d1011d8a589a7f344d1Chet Haase        }
479430742f09063574271e6c4091de13b9b9e762514Chet Haase        if (mInterpolator != null) {
480430742f09063574271e6c4091de13b9b9e762514Chet Haase            for (Node node : mNodes) {
481430742f09063574271e6c4091de13b9b9e762514Chet Haase                node.animation.setInterpolator(mInterpolator);
482430742f09063574271e6c4091de13b9b9e762514Chet Haase            }
483430742f09063574271e6c4091de13b9b9e762514Chet Haase        }
484430742f09063574271e6c4091de13b9b9e762514Chet Haase            // First, sort the nodes (if necessary). This will ensure that sortedNodes
48517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // contains the animation nodes in the correct order.
48617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sortNodes();
48717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
488e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        int numSortedNodes = mSortedNodes.size();
489e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        for (int i = 0; i < numSortedNodes; ++i) {
490e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            Node node = mSortedNodes.get(i);
491e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            // First, clear out the old listeners
492e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            ArrayList<AnimatorListener> oldListeners = node.animation.getListeners();
493e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            if (oldListeners != null && oldListeners.size() > 0) {
494d45204ba092312630a0e516ea7c247e594ce893bMartijn Coenen                final ArrayList<AnimatorListener> clonedListeners = new
495d45204ba092312630a0e516ea7c247e594ce893bMartijn Coenen                        ArrayList<AnimatorListener>(oldListeners);
496d45204ba092312630a0e516ea7c247e594ce893bMartijn Coenen
497d45204ba092312630a0e516ea7c247e594ce893bMartijn Coenen                for (AnimatorListener listener : clonedListeners) {
4987dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    if (listener instanceof DependencyListener ||
4997dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                            listener instanceof AnimatorSetListener) {
500e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        node.animation.removeListener(listener);
501e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    }
502e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                }
503e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            }
504e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
505e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase
50617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // nodesToStart holds the list of nodes to be started immediately. We don't want to
50717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // start the animations in the loop directly because we first need to set up
50817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // dependencies on all of the nodes. For example, we don't want to start an animation
50917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // when some other animation also wants to start when the first animation begins.
51021cd1389d2ef218b20994b617c57af120841a57fChet Haase        final ArrayList<Node> nodesToStart = new ArrayList<Node>();
5117c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        for (int i = 0; i < numSortedNodes; ++i) {
5127c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            Node node = mSortedNodes.get(i);
513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mSetListener == null) {
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mSetListener = new AnimatorSetListener(this);
51517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
51617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node.dependencies == null || node.dependencies.size() == 0) {
51717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodesToStart.add(node);
51817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
5197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                int numDependencies = node.dependencies.size();
5207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                for (int j = 0; j < numDependencies; ++j) {
5217c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    Dependency dependency = node.dependencies.get(j);
52217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependency.node.animation.addListener(
523010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                            new DependencyListener(this, node, dependency.rule));
52417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
52517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node.tmpDependencies = (ArrayList<Dependency>) node.dependencies.clone();
52617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
527a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            node.animation.addListener(mSetListener);
52817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
52917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Now that all dependencies are set up, start the animations that should be started.
53021cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (mStartDelay <= 0) {
53121cd1389d2ef218b20994b617c57af120841a57fChet Haase            for (Node node : nodesToStart) {
53221cd1389d2ef218b20994b617c57af120841a57fChet Haase                node.animation.start();
53321cd1389d2ef218b20994b617c57af120841a57fChet Haase                mPlayingSet.add(node.animation);
53421cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
53521cd1389d2ef218b20994b617c57af120841a57fChet Haase        } else {
536e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim = ValueAnimator.ofFloat(0f, 1f);
537e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.setDuration(mStartDelay);
538e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.addListener(new AnimatorListenerAdapter() {
539e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                boolean canceled = false;
540e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                public void onAnimationCancel(Animator anim) {
541e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    canceled = true;
542e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                }
543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                public void onAnimationEnd(Animator anim) {
544e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    if (!canceled) {
545e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        int numNodes = nodesToStart.size();
546e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        for (int i = 0; i < numNodes; ++i) {
547e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                            Node node = nodesToStart.get(i);
548e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                            node.animation.start();
549e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                            mPlayingSet.add(node.animation);
550e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        }
55121cd1389d2ef218b20994b617c57af120841a57fChet Haase                    }
55221cd1389d2ef218b20994b617c57af120841a57fChet Haase                }
55321cd1389d2ef218b20994b617c57af120841a57fChet Haase            });
554e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.start();
55517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
55617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
557a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
558a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
5597c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
5607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
5617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationStart(this);
5628b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            }
5638b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        }
5648b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        if (mNodes.size() == 0 && mStartDelay == 0) {
5658b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            // Handle unusual case where empty AnimatorSet is started - should send out
5668b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            // end event immediately since the event will not be sent out at all otherwise
5678b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            mStarted = false;
5688b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            if (mListeners != null) {
5698b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase                ArrayList<AnimatorListener> tmpListeners =
5708b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
5718b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase                int numListeners = tmpListeners.size();
5728b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase                for (int i = 0; i < numListeners; ++i) {
5732970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    tmpListeners.get(i).onAnimationEnd(this);
5742970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                }
57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
57617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
57717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
57817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
57949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    @Override
580a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public AnimatorSet clone() {
581a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final AnimatorSet anim = (AnimatorSet) super.clone();
58249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        /*
58349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * The basic clone() operation copies all items. This doesn't work very well for
584a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * AnimatorSet, because it will copy references that need to be recreated and state
58549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * that may not apply. What we need to do now is put the clone in an uninitialized
58649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * state, with fresh, empty data structures. Then we will build up the nodes list
58749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * manually, as we clone each Node (and its animation). The clone will then be sorted,
58849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * and will populate any appropriate lists, when it is started.
58949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         */
59049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mNeedsSort = true;
5917dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase        anim.mTerminated = false;
5928b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        anim.mStarted = false;
593a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingSet = new ArrayList<Animator>();
594a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mNodeMap = new HashMap<Animator, Node>();
59549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mNodes = new ArrayList<Node>();
59649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mSortedNodes = new ArrayList<Node>();
59749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
59849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // Walk through the old nodes list, cloning each node and adding it to the new nodemap.
599a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // One problem is that the old node dependencies point to nodes in the old AnimatorSet.
60049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // We need to track the old/new nodes in order to reconstruct the dependencies in the clone.
60149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        HashMap<Node, Node> nodeCloneMap = new HashMap<Node, Node>(); // <old, new>
60249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        for (Node node : mNodes) {
60349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            Node nodeClone = node.clone();
60449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeCloneMap.put(node, nodeClone);
60549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mNodes.add(nodeClone);
60649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mNodeMap.put(nodeClone.animation, nodeClone);
60749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            // Clear out the dependencies in the clone; we'll set these up manually later
60849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.dependencies = null;
60949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.tmpDependencies = null;
61049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.nodeDependents = null;
61149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.nodeDependencies = null;
612a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // clear out any listeners that were set up by the AnimatorSet; these will
61349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            // be set up when the clone's nodes are sorted
614a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> cloneListeners = nodeClone.animation.getListeners();
61549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            if (cloneListeners != null) {
616a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ArrayList<AnimatorListener> listenersToRemove = null;
617a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                for (AnimatorListener listener : cloneListeners) {
618a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (listener instanceof AnimatorSetListener) {
61949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        if (listenersToRemove == null) {
620a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            listenersToRemove = new ArrayList<AnimatorListener>();
62149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        }
62249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        listenersToRemove.add(listener);
62349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    }
62449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
62549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                if (listenersToRemove != null) {
626a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    for (AnimatorListener listener : listenersToRemove) {
62749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        cloneListeners.remove(listener);
62849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    }
62949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
63049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
63149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
63249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // Now that we've cloned all of the nodes, we're ready to walk through their
63349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // dependencies, mapping the old dependencies to the new nodes
63449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        for (Node node : mNodes) {
63549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            Node nodeClone = nodeCloneMap.get(node);
63649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            if (node.dependencies != null) {
63749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                for (Dependency dependency : node.dependencies) {
63849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    Node clonedDependencyNode = nodeCloneMap.get(dependency.node);
63949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    Dependency cloneDependency = new Dependency(clonedDependencyNode,
64049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                            dependency.rule);
64149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    nodeClone.addDependency(cloneDependency);
64249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
64349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
64449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
64549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
64649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        return anim;
64749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    }
64849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
64917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This class is the mechanism by which animations are started based on events in other
65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations. If an animation has multiple dependencies on other animations, then
65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * all dependencies must be satisfied before the animation is started.
65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
654a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static class DependencyListener implements AnimatorListener {
65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
656a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private AnimatorSet mAnimatorSet;
657010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
65817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The node upon which the dependency is based.
65917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private Node mNode;
66017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
66117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The Dependency rule (WITH or AFTER) that the listener should wait for on
66217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // the node
66317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private int mRule;
66417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
665a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public DependencyListener(AnimatorSet animatorSet, Node node, int rule) {
666a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            this.mAnimatorSet = animatorSet;
66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.mNode = node;
66817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.mRule = rule;
66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
67017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
67117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
672010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * Ignore cancel events for now. We may want to handle this eventually,
673010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * to prevent follow-on animations from running when some dependency
674010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * animation is canceled.
67517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
676a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationCancel(Animator animation) {
67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
67817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * An end event is received - see if this is an event we are listening for
68117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
682a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationEnd(Animator animation) {
68317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRule == Dependency.AFTER) {
68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                startIfReady(animation);
68517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Ignore repeat events for now
69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
691a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationRepeat(Animator animation) {
69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
69517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * A start event is received - see if this is an event we are listening for
69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
697a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationStart(Animator animation) {
69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRule == Dependency.WITH) {
69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                startIfReady(animation);
70017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
70117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
70217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
70317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
70417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Check whether the event received is one that the node was waiting for.
70517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * If so, mark it as complete and see whether it's time to start
70617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the animation.
70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param dependencyAnimation the animation that sent the event.
70817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
709a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private void startIfReady(Animator dependencyAnimation) {
7107dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (mAnimatorSet.mTerminated) {
711a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // if the parent AnimatorSet was canceled, then don't start any dependent anims
712010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                return;
713010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            }
71417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependencyToRemove = null;
7157c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numDependencies = mNode.tmpDependencies.size();
7167c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numDependencies; ++i) {
7177c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Dependency dependency = mNode.tmpDependencies.get(i);
71817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (dependency.rule == mRule &&
71917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        dependency.node.animation == dependencyAnimation) {
72017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // rule fired - remove the dependency and listener and check to
72117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // see whether it's time to start the animation
72217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependencyToRemove = dependency;
72317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependencyAnimation.removeListener(this);
72417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    break;
72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNode.tmpDependencies.remove(dependencyToRemove);
72817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mNode.tmpDependencies.size() == 0) {
72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // all dependencies satisfied: start the animation
73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNode.animation.start();
731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mAnimatorSet.mPlayingSet.add(mNode.animation);
73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
73417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
73517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private class AnimatorSetListener implements AnimatorListener {
73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private AnimatorSet mAnimatorSet;
74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
741a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        AnimatorSetListener(AnimatorSet animatorSet) {
742a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mAnimatorSet = animatorSet;
74317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationCancel(Animator animation) {
7467dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (!mTerminated) {
7477dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // Listeners are already notified of the AnimatorSet canceling in cancel().
7487dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // The logic below only kicks in when animations end normally
7497dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                if (mPlayingSet.size() == 0) {
7507dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    if (mListeners != null) {
7517dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        int numListeners = mListeners.size();
7527dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        for (int i = 0; i < numListeners; ++i) {
7537dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                            mListeners.get(i).onAnimationCancel(mAnimatorSet);
7547dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        }
75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
75617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        @SuppressWarnings("unchecked")
761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationEnd(Animator animation) {
76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            animation.removeListener(this);
76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mPlayingSet.remove(animation);
764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Node animNode = mAnimatorSet.mNodeMap.get(animation);
7651e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            animNode.done = true;
7667dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            if (!mTerminated) {
7677dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // Listeners are already notified of the AnimatorSet ending in cancel() or
7687dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // end(); the logic below only kicks in when animations end normally
7697dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes;
7707dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                boolean allDone = true;
7717dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                int numSortedNodes = sortedNodes.size();
7727dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (int i = 0; i < numSortedNodes; ++i) {
7737dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    if (!sortedNodes.get(i).done) {
7747dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        allDone = false;
7757dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        break;
7767dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    }
777a6e4a1757728efc91627ece602b0899d75303659Chet Haase                }
7787dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                if (allDone) {
7797dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    // If this was the last child animation to end, then notify listeners that this
7807dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    // AnimatorSet has ended
7817dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    if (mListeners != null) {
7827dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        ArrayList<AnimatorListener> tmpListeners =
7837dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                                (ArrayList<AnimatorListener>) mListeners.clone();
7847dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        int numListeners = tmpListeners.size();
7857dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        for (int i = 0; i < numListeners; ++i) {
7867dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                            tmpListeners.get(i).onAnimationEnd(mAnimatorSet);
7877dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        }
78817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
7898b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase                    mAnimatorSet.mStarted = false;
79017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
79417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Nothing to do
795a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationRepeat(Animator animation) {
79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
79717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Nothing to do
799a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationStart(Animator animation) {
80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
80417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
80517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method sorts the current set of nodes, if needed. The sort is a simple
80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * DependencyGraph sort, which goes like this:
80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * - All nodes without dependencies become 'roots'
80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * - while roots list is not null
80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -   for each root r
81017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -     add r to sorted list
81117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -     remove r as a dependency from any other node
81217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -   any nodes with no dependencies are added to the roots list
81317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
81417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void sortNodes() {
81517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mNeedsSort) {
81617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mSortedNodes.clear();
81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<Node> roots = new ArrayList<Node>();
8187c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numNodes = mNodes.size();
8197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numNodes; ++i) {
8207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Node node = mNodes.get(i);
82117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (node.dependencies == null || node.dependencies.size() == 0) {
82217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    roots.add(node);
82317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
82417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
82517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<Node> tmpRoots = new ArrayList<Node>();
82617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            while (roots.size() > 0) {
8277c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                int numRoots = roots.size();
8287c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                for (int i = 0; i < numRoots; ++i) {
8297c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    Node root = roots.get(i);
83017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    mSortedNodes.add(root);
83117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (root.nodeDependents != null) {
8327c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numDependents = root.nodeDependents.size();
8337c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int j = 0; j < numDependents; ++j) {
8347c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            Node node = root.nodeDependents.get(j);
83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies.remove(root);
83617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            if (node.nodeDependencies.size() == 0) {
83717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                tmpRoots.add(node);
83817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            }
83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
84017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
84117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
842010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                roots.clear();
84317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                roots.addAll(tmpRoots);
84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                tmpRoots.clear();
84517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = false;
84717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mSortedNodes.size() != mNodes.size()) {
84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                throw new IllegalStateException("Circular dependencies cannot exist"
849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        + " in AnimatorSet");
85017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        } else {
85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // Doesn't need sorting, but still need to add in the nodeDependencies list
85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // because these get removed as the event listeners fire and the dependencies
85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // are satisfied
8557c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numNodes = mNodes.size();
8567c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numNodes; ++i) {
8577c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Node node = mNodes.get(i);
85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (node.dependencies != null && node.dependencies.size() > 0) {
8597c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numDependencies = node.dependencies.size();
8607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int j = 0; j < numDependencies; ++j) {
8617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        Dependency dependency = node.dependencies.get(j);
86217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (node.nodeDependencies == null) {
86317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies = new ArrayList<Node>();
86417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
86517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (!node.nodeDependencies.contains(dependency.node)) {
86617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies.add(dependency.node);
86717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
86817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
86917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
8707dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // nodes are 'done' by default; they become un-done when started, and done
8717dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                // again when ended
8721e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase                node.done = false;
87317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
87417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
87517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
87617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
87817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Dependency holds information about the node that some other node is
87917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * dependent upon and the nature of that dependency.
88017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
88117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
88217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static class Dependency {
88317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        static final int WITH = 0; // dependent node must start with this dependency node
88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        static final int AFTER = 1; // dependent node must start when this dependency node finishes
88517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The node that the other node with this Dependency is dependent upon
88717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public Node node;
88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The nature of the dependency (WITH or AFTER)
89017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public int rule;
89117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
89217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public Dependency(Node node, int rule) {
89317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.node = node;
89417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.rule = rule;
89517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
89617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
89717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
89817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A Node is an embodiment of both the Animator that it wraps as well as
90017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * any dependencies that are associated with that Animation. This includes
90117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * both dependencies upon other nodes (in the dependencies list) as
90217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * well as dependencies of other nodes upon this (in the nodeDependents list).
90317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
90449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private static class Node implements Cloneable {
905a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public Animator animation;
90617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
90717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
90817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  These are the dependencies that this node's animation has on other
90917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  nodes. For example, if this node's animation should begin with some
91017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  other animation ends, then there will be an item in this node's
91117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  dependencies list for that other animation's node.
91217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
91317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Dependency> dependencies = null;
91417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
91517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
91617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * tmpDependencies is a runtime detail. We use the dependencies list for sorting.
91717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * But we also use the list to keep track of when multiple dependencies are satisfied,
91817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * but removing each dependency as it is satisfied. We do not want to remove
91917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the dependency itself from the list, because we need to retain that information
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * if the AnimatorSet is launched in the future. So we create a copy of the dependency
921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * list when the AnimatorSet starts and use this tmpDependencies list to track the
92217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * list of satisfied dependencies.
92317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Dependency> tmpDependencies = null;
92517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
92617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * nodeDependencies is just a list of the nodes that this Node is dependent upon.
92817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * This information is used in sortNodes(), to determine when a node is a root.
92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
93017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Node> nodeDependencies = null;
93117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
93217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
93317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * nodeDepdendents is the list of nodes that have this node as a dependency. This
93417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * is a utility field used in sortNodes to facilitate removing this node as a
93517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * dependency when it is a root node.
93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
93717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Node> nodeDependents = null;
93817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
93917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
9401e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase         * Flag indicating whether the animation in this node is finished. This flag
941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * is used by AnimatorSet to check, as each animation ends, whether all child animations
942a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * are done and it's time to send out an end event for the entire AnimatorSet.
9431e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase         */
9441e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase        public boolean done = false;
9451e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase
9461e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase        /**
94717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Constructs the Node with the animation that it encapsulates. A Node has no
94817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * dependencies by default; dependencies are added via the addDependency()
94917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * method.
95017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
95117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param animation The animation that the Node encapsulates.
95217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public Node(Animator animation) {
95417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.animation = animation;
95517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
95617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
95717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
95817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Add a dependency to this Node. The dependency includes information about the
95917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * node that this node is dependency upon and the nature of the dependency.
96017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param dependency
96117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
96217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public void addDependency(Dependency dependency) {
96317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (dependencies == null) {
96417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                dependencies = new ArrayList<Dependency>();
96517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodeDependencies = new ArrayList<Node>();
96617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
96717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            dependencies.add(dependency);
96817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (!nodeDependencies.contains(dependency.node)) {
96917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodeDependencies.add(dependency.node);
97017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
97117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node dependencyNode = dependency.node;
97217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (dependencyNode.nodeDependents == null) {
97317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                dependencyNode.nodeDependents = new ArrayList<Node>();
97417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
97517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            dependencyNode.nodeDependents.add(this);
97617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
97749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
97849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        @Override
97921cd1389d2ef218b20994b617c57af120841a57fChet Haase        public Node clone() {
98021cd1389d2ef218b20994b617c57af120841a57fChet Haase            try {
98121cd1389d2ef218b20994b617c57af120841a57fChet Haase                Node node = (Node) super.clone();
982a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                node.animation = (Animator) animation.clone();
98321cd1389d2ef218b20994b617c57af120841a57fChet Haase                return node;
98421cd1389d2ef218b20994b617c57af120841a57fChet Haase            } catch (CloneNotSupportedException e) {
98521cd1389d2ef218b20994b617c57af120841a57fChet Haase               throw new AssertionError();
98621cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
98749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
98817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
98917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
99017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
99117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The <code>Builder</code> object is a utility class to facilitate adding animations to a
992a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>AnimatorSet</code> along with the relationships between the various animations. The
99317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * intention of the <code>Builder</code> methods, along with the {@link
9948b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * AnimatorSet#play(Animator) play()} method of <code>AnimatorSet</code> is to make it possible
9958b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * to express the dependency relationships of animations in a natural way. Developers can also
9968b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * use the {@link AnimatorSet#playTogether(Animator[]) playTogether()} and {@link
997a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet#playSequentially(Animator[]) playSequentially()} methods if these suit the need,
998a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * but it might be easier in some situations to express the AnimatorSet of animations in pairs.
99917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
100017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>The <code>Builder</code> object cannot be constructed directly, but is rather constructed
1001a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * internally via a call to {@link AnimatorSet#play(Animator)}.</p>
100217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
1003a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to
100417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * play when anim2 finishes, and anim4 to play when anim3 finishes:</p>
100517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
1006a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *     AnimatorSet s = new AnimatorSet();
100717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim1).with(anim2);
100817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim2).before(anim3);
100917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim4).after(anim3);
101017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
101117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
1012a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note in the example that both {@link Builder#before(Animator)} and {@link
1013a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Builder#after(Animator)} are used. These are just different ways of expressing the same
101417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * relationship and are provided to make it easier to say things in a way that is more natural,
101517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * depending on the situation.</p>
101617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
101717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>It is possible to make several calls into the same <code>Builder</code> object to express
101817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * multiple relationships. However, note that it is only the animation passed into the initial
1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorSet#play(Animator)} method that is the dependency in any of the successive
102017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * calls to the <code>Builder</code> object. For example, the following code starts both anim2
102117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and anim3 when anim1 ends; there is no direct dependency relationship between anim2 and
102217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * anim3:
102317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
1024a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *   AnimatorSet s = new AnimatorSet();
102517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim1).before(anim2).before(anim3);
102617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
102717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * If the desired result is to play anim1 then anim2 then anim3, this code expresses the
102817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * relationship correctly:</p>
102917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
1030a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *   AnimatorSet s = new AnimatorSet();
103117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim1).before(anim2);
103217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim2).before(anim3);
103317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
103417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
103517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that it is possible to express relationships that cannot be resolved and will not
103617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * result in sensible results. For example, <code>play(anim1).after(anim1)</code> makes no
103717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sense. In general, circular dependencies like this one (or more indirect ones where a depends
1038a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * on b, which depends on c, which depends on a) should be avoided. Only create AnimatorSets
1039a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * that can boil down to a simple, one-way relationship of animations starting with, before, and
104017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * after other, different, animations.</p>
104117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
104217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public class Builder {
104317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
104417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
104517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * This tracks the current node being processed. It is supplied to the play() method
1046a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * of AnimatorSet and passed into the constructor of Builder.
104717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
104817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private Node mCurrentNode;
104917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
105017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
1051a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * package-private constructor. Builders are only constructed by AnimatorSet, when the
105217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * play() method is called.
105317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
105417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that is the dependency for the other animations passed into
105517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the other methods of this Builder object.
105617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
1057a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        Builder(Animator anim) {
105817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mCurrentNode = mNodeMap.get(anim);
105917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mCurrentNode == null) {
106017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mCurrentNode = new Node(anim);
106117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, mCurrentNode);
106217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(mCurrentNode);
106317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
106417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
106517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
106617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
106717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play at the same time as the animation supplied in the
1068a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object.
106917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
107017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that will play when the animation supplied to the
1071a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method starts.
107217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
10732970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder with(Animator anim) {
107417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
107517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
107617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
107717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
107817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
107917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
108017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(mCurrentNode, Dependency.WITH);
108117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            node.addDependency(dependency);
10822970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
108317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
108417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
108517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
108617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play when the animation supplied in the
1087a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
108817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * ends.
108917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
109017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that will play when the animation supplied to the
1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method ends.
109217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
10932970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder before(Animator anim) {
109417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
109517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
109617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
109717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
109817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
109917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
110017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(mCurrentNode, Dependency.AFTER);
110117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            node.addDependency(dependency);
11022970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
110317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
110417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
110517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
110617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play when the animation supplied in the
1107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
110817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * to start when the animation supplied in this method call ends.
110917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
111017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation whose end will cause the animation supplied to the
1111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method to play.
111217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
11132970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder after(Animator anim) {
111417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
111517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
111617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
111717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
111817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
111917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
112017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(node, Dependency.AFTER);
112117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mCurrentNode.addDependency(dependency);
11222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
112317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
112417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
112517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
112617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the animation supplied in the
1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
112817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * to play when the given amount of time elapses.
112917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
113017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param delay The number of milliseconds that should elapse before the
113117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * animation starts.
113217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
11332970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder after(long delay) {
1134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // setup dummy ValueAnimator just to run the clock
11352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
11362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            anim.setDuration(delay);
11372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            after(anim);
11382970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
113917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
114017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
114117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
114217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
114317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}
1144