AnimatorSet.java revision e2ab7ccd385cdb6517955c719e1d2b49771bedb6
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.
4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
43a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepublic final class AnimatorSet extends Animator {
4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
4649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * Internal variables
4749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
4849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
4949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * to clone() to make deep copies of them.
5049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     */
5149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
5249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    /**
533b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase     * Tracks animations currently being played, so that we know what to
54a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * cancel or end when cancel() or end() is called on this AnimatorSet
5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
56a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private ArrayList<Animator> mPlayingSet = new ArrayList<Animator>();
5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
59a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Contains all nodes, mapped to their respective Animators. When new
60a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * dependency information is added for an Animator, we want to add it
61a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a single node representing that Animator, not create a new Node
6217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * if one already exists.
6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
64a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private HashMap<Animator, Node> mNodeMap = new HashMap<Animator, Node>();
6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set of all nodes created for this AnimatorSet. This list is used upon
68a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * starting the set, and the nodes are placed in sorted order into the
6917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sortedNodes collection.
7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
7149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private ArrayList<Node> mNodes = new ArrayList<Node>();
7217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
7317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
7417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The sorted list of nodes. This is the order in which the animations will
7517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * be played. The details about when exactly they will be played depend
7617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * on the dependency relationships of the nodes.
7717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
7849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private ArrayList<Node> mSortedNodes = new ArrayList<Node>();
7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Flag indicating whether the nodes should be sorted prior to playing. This
8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * flag allows us to cache the previous sorted nodes so that if the sequence
8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * is replayed with no changes, it does not have to re-sort the nodes again.
8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
8517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean mNeedsSort = true;
8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
87a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private AnimatorSetListener mSetListener = null;
8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
90a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag indicating that the AnimatorSet has been canceled (by calling cancel() or end()).
91010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase     * This flag is used to avoid starting other animations when currently-playing
92a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * child animations of this AnimatorSet end.
93010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase     */
94010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase    boolean mCanceled = false;
95010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
9621cd1389d2ef218b20994b617c57af120841a57fChet Haase    // The amount of time in ms to delay starting the animation after start() is called
9721cd1389d2ef218b20994b617c57af120841a57fChet Haase    private long mStartDelay = 0;
9821cd1389d2ef218b20994b617c57af120841a57fChet Haase
99e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase    // Animator used for a nonzero startDelay
100e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase    private ValueAnimator mDelayAnim = null;
101e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase
10221cd1389d2ef218b20994b617c57af120841a57fChet Haase
10321cd1389d2ef218b20994b617c57af120841a57fChet Haase    // How long the child animations should last in ms. The default value is negative, which
104a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // simply means that there is no duration set on the AnimatorSet. When a real duration is
10521cd1389d2ef218b20994b617c57af120841a57fChet Haase    // set, it is passed along to the child animations.
10621cd1389d2ef218b20994b617c57af120841a57fChet Haase    private long mDuration = -1;
10721cd1389d2ef218b20994b617c57af120841a57fChet Haase
10821cd1389d2ef218b20994b617c57af120841a57fChet Haase
109010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase    /**
110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets up this AnimatorSet to play all of the supplied animations at the same time.
11117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param items The animations that will be started simultaneously.
11317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void playTogether(Animator... items) {
115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (items != null) {
11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Builder builder = play(items[0]);
118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 1; i < items.length; ++i) {
119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                builder.with(items[i]);
12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
12317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
12417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
12537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * Sets up this AnimatorSet to play all of the supplied animations at the same time.
12637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     *
12737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started simultaneously.
12837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     */
12937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    public void playTogether(Collection<Animator> items) {
13037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        if (items != null && items.size() > 0) {
13137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            mNeedsSort = true;
13237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            Builder builder = null;
13337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            for (Animator anim : items) {
13437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                if (builder == null) {
13537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    builder = play(anim);
13637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                } else {
13737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    builder.with(anim);
13837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                }
13937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            }
14037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        }
14137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    }
14237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase
14337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    /**
144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets up this AnimatorSet to play each of the supplied animations when the
14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * previous animation ends.
14617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
14737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started one after another.
14817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void playSequentially(Animator... items) {
150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (items != null) {
15117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (items.length == 1) {
153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                play(items[0]);
15417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                for (int i = 0; i < items.length - 1; ++i) {
156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    play(items[i]).before(items[i+1]);
15717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
15817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
15917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
16017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
16117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
16337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * Sets up this AnimatorSet to play each of the supplied animations when the
16437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * previous animation ends.
16537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     *
16637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started one after another.
16737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     */
16837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    public void playSequentially(List<Animator> items) {
16937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        if (items != null && items.size() > 0) {
17037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            mNeedsSort = true;
17137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            if (items.size() == 1) {
17237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                play(items.get(0));
17337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            } else {
17437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                for (int i = 0; i < items.size() - 1; ++i) {
17537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    play(items.get(i)).before(items.get(i+1));
17637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                }
17737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            }
17837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        }
17937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    }
18037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase
18137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    /**
182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the current list of child Animator objects controlled by this
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet. This is a copy of the internal list; modifications to the returned list
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will not affect the AnimatorSet, although changes to the underlying Animator objects
185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will affect those objects being managed by the AnimatorSet.
186f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     *
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return ArrayList<Animator> The list of child animations of this AnimatorSet.
188f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ArrayList<Animator> getChildAnimations() {
190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        ArrayList<Animator> childList = new ArrayList<Animator>();
191f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        for (Node node : mNodes) {
192f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            childList.add(node.animation);
193f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        }
194f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        return childList;
195f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    }
196f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
197f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
198811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     * Sets the target object for all current {@link #getChildAnimations() child animations}
199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of this AnimatorSet that take targets ({@link ObjectAnimator} and
200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet).
201811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     *
202811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     * @param target The object being animated
203811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     */
20421cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
205811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    public void setTarget(Object target) {
206811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase        for (Node node : mNodes) {
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Animator animation = node.animation;
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (animation instanceof AnimatorSet) {
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ((AnimatorSet)animation).setTarget(target);
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            } else if (animation instanceof ObjectAnimator) {
211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ((ObjectAnimator)animation).setTarget(target);
212811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            }
213811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase        }
214811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    }
215811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase
216811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    /**
217e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase     * Sets the TimeInterpolator for all current {@link #getChildAnimations() child animations}
218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of this AnimatorSet.
21921cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param interpolator the interpolator to be used by each child animation of this AnimatorSet
22121cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
22221cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
223e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator interpolator) {
22421cd1389d2ef218b20994b617c57af120841a57fChet Haase        for (Node node : mNodes) {
22521cd1389d2ef218b20994b617c57af120841a57fChet Haase            node.animation.setInterpolator(interpolator);
22621cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
22721cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
22821cd1389d2ef218b20994b617c57af120841a57fChet Haase
22921cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
23017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method creates a <code>Builder</code> object, which is used to
23117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * set up playing constraints. This initial <code>play()</code> method
23217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * tells the <code>Builder</code> the animation that is the dependency for
23317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the succeeding commands to the <code>Builder</code>. For example,
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calling <code>play(a1).with(a2)</code> sets up the AnimatorSet to play
23517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a1</code> and <code>a2</code> at the same time,
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>play(a1).before(a2)</code> sets up the AnimatorSet to play
23717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a1</code> first, followed by <code>a2</code>, and
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>play(a1).after(a2)</code> sets up the AnimatorSet to play
23917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a2</code> first, followed by <code>a1</code>.
24017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
24117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that <code>play()</code> is the only way to tell the
24217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Builder</code> the animation upon which the dependency is created,
24317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * so successive calls to the various functions in <code>Builder</code>
24417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * will all refer to the initial parameter supplied in <code>play()</code>
24517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * as the dependency of the other animations. For example, calling
24617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code>
24717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and <code>a3</code> when a1 ends; it does not set up a dependency between
24817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a2</code> and <code>a3</code>.</p>
24917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
25017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param anim The animation that is the dependency used in later calls to the
25117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * methods in the returned <code>Builder</code> object. A null parameter will result
25217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * in a null <code>Builder</code> return value.
253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return Builder The object that constructs the AnimatorSet based on the dependencies
25417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * outlined in the calls to <code>play</code> and the other methods in the
25517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Builder</code object.
25617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Builder play(Animator anim) {
25817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (anim != null) {
25917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
26017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            return new Builder(anim);
26117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
26217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return null;
26317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
26417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
26517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
26617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
26717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note that canceling a <code>AnimatorSet</code> also cancels all of the animations that it is
26917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * responsible for.</p>
27017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
27117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @SuppressWarnings("unchecked")
27217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
27317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void cancel() {
274010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        mCanceled = true;
27517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
276a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
277a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
278a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (AnimatorListener listener : tmpListeners) {
27917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationCancel(this);
28017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
28117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
282e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        if (mDelayAnim != null && mDelayAnim.isRunning()) {
283e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            // If we're currently in the startDelay period, just cancel that animator and
284e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            // send out the end event to all listeners
285e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.cancel();
286e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            if (mListeners != null) {
287e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                ArrayList<AnimatorListener> tmpListeners =
288e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
289e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                for (AnimatorListener listener : tmpListeners) {
290e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    listener.onAnimationEnd(this);
291e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                }
292e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            }
293e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            return;
294e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
295010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        if (mSortedNodes.size() > 0) {
296010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            for (Node node : mSortedNodes) {
297010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                node.animation.cancel();
29817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
29917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
30017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
30117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
30217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
30317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
30417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note that ending a <code>AnimatorSet</code> also ends all of the animations that it is
30617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * responsible for.</p>
30717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
30817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
30917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void end() {
310010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        mCanceled = true;
311f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase        if (mSortedNodes.size() != mNodes.size()) {
312f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase            // hasn't been started yet - sort the nodes now, then end them
313f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase            sortNodes();
3141e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            for (Node node : mSortedNodes) {
315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                if (mSetListener == null) {
316a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mSetListener = new AnimatorSetListener(this);
3171e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase                }
318a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                node.animation.addListener(mSetListener);
3191e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            }
320f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase        }
321e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        if (mDelayAnim != null) {
322e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.cancel();
323e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
324010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        if (mSortedNodes.size() > 0) {
325010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            for (Node node : mSortedNodes) {
326010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                node.animation.end();
32717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
32817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
32917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
33017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
33117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
332a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns true if any of the child animations of this AnimatorSet have been started and have not
333673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase     * yet ended.
334a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return Whether this AnimatorSet has been started and has not yet ended.
335673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase     */
336673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    @Override
337673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    public boolean isRunning() {
338673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        for (Node node : mNodes) {
339673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase            if (node.animation.isRunning()) {
340673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase                return true;
341673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase            }
342673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        }
343673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        return false;
344673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    }
345673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase
346673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    /**
34721cd1389d2ef218b20994b617c57af120841a57fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
34821cd1389d2ef218b20994b617c57af120841a57fChet Haase     * {@link #start()} is called.
34921cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
35021cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @return the number of milliseconds to delay running the animation
35121cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
35221cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
35321cd1389d2ef218b20994b617c57af120841a57fChet Haase    public long getStartDelay() {
35421cd1389d2ef218b20994b617c57af120841a57fChet Haase        return mStartDelay;
35521cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
35621cd1389d2ef218b20994b617c57af120841a57fChet Haase
35721cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
35821cd1389d2ef218b20994b617c57af120841a57fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
35921cd1389d2ef218b20994b617c57af120841a57fChet Haase     * {@link #start()} is called.
36021cd1389d2ef218b20994b617c57af120841a57fChet Haase
36121cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param startDelay The amount of the delay, in milliseconds
36221cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
36321cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
36421cd1389d2ef218b20994b617c57af120841a57fChet Haase    public void setStartDelay(long startDelay) {
36521cd1389d2ef218b20994b617c57af120841a57fChet Haase        mStartDelay = startDelay;
36621cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
36721cd1389d2ef218b20994b617c57af120841a57fChet Haase
36821cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
369a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the length of each of the child animations of this AnimatorSet. This value may
370a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be less than 0, which indicates that no duration has been set on this AnimatorSet
37121cd1389d2ef218b20994b617c57af120841a57fChet Haase     * and each of the child animations will use their own duration.
37221cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
37321cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @return The length of the animation, in milliseconds, of each of the child
374a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations of this AnimatorSet.
37521cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
37621cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
37721cd1389d2ef218b20994b617c57af120841a57fChet Haase    public long getDuration() {
37821cd1389d2ef218b20994b617c57af120841a57fChet Haase        return mDuration;
37921cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
38021cd1389d2ef218b20994b617c57af120841a57fChet Haase
38121cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
382a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the length of each of the current child animations of this AnimatorSet. By default,
383a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * each child animation will use its own duration. If the duration is set on the AnimatorSet,
38421cd1389d2ef218b20994b617c57af120841a57fChet Haase     * then each child animation inherits this duration.
38521cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
38621cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param duration The length of the animation, in milliseconds, of each of the child
387a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations of this AnimatorSet.
38821cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
38921cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public AnimatorSet setDuration(long duration) {
39121cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (duration < 0) {
39221cd1389d2ef218b20994b617c57af120841a57fChet Haase            throw new IllegalArgumentException("duration must be a value of zero or greater");
39321cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
39421cd1389d2ef218b20994b617c57af120841a57fChet Haase        for (Node node : mNodes) {
395a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to
39621cd1389d2ef218b20994b617c57af120841a57fChet Haase            // insert "play-after" delays
39721cd1389d2ef218b20994b617c57af120841a57fChet Haase            node.animation.setDuration(duration);
39821cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
39921cd1389d2ef218b20994b617c57af120841a57fChet Haase        mDuration = duration;
4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
40121cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
40221cd1389d2ef218b20994b617c57af120841a57fChet Haase
4032970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    @Override
4042970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    public void setupStartValues() {
4052970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        for (Node node : mNodes) {
4062970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            node.animation.setupStartValues();
4072970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
4082970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    }
4092970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase
4102970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    @Override
4112970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    public void setupEndValues() {
4122970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        for (Node node : mNodes) {
4132970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            node.animation.setupEndValues();
4142970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
4152970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    }
4162970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase
41721cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
41817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
41917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
420a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Starting this <code>AnimatorSet</code> will, in turn, start the animations for which
42117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * it is responsible. The details of when exactly those animations are started depends on
42217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the dependency relationships that have been set up between the animations.
42317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
42417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @SuppressWarnings("unchecked")
42517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
42617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void start() {
427010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        mCanceled = false;
428010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
42917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // First, sort the nodes (if necessary). This will ensure that sortedNodes
43017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // contains the animation nodes in the correct order.
43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sortNodes();
43217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
433e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        int numSortedNodes = mSortedNodes.size();
434e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        for (int i = 0; i < numSortedNodes; ++i) {
435e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            Node node = mSortedNodes.get(i);
436e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            // First, clear out the old listeners
437e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            ArrayList<AnimatorListener> oldListeners = node.animation.getListeners();
438e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            if (oldListeners != null && oldListeners.size() > 0) {
439e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                for (AnimatorListener listener : oldListeners) {
440e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    if (listener instanceof DependencyListener) {
441e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        node.animation.removeListener(listener);
442e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    }
443e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                }
444e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            }
445e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
446e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase
44717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // nodesToStart holds the list of nodes to be started immediately. We don't want to
44817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // start the animations in the loop directly because we first need to set up
44917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // dependencies on all of the nodes. For example, we don't want to start an animation
45017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // when some other animation also wants to start when the first animation begins.
45121cd1389d2ef218b20994b617c57af120841a57fChet Haase        final ArrayList<Node> nodesToStart = new ArrayList<Node>();
4527c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        for (int i = 0; i < numSortedNodes; ++i) {
4537c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            Node node = mSortedNodes.get(i);
454a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mSetListener == null) {
455a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mSetListener = new AnimatorSetListener(this);
45617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
45717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node.dependencies == null || node.dependencies.size() == 0) {
45817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodesToStart.add(node);
45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
4607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                int numDependencies = node.dependencies.size();
4617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                for (int j = 0; j < numDependencies; ++j) {
4627c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    Dependency dependency = node.dependencies.get(j);
46317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependency.node.animation.addListener(
464010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                            new DependencyListener(this, node, dependency.rule));
46517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
46617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node.tmpDependencies = (ArrayList<Dependency>) node.dependencies.clone();
46717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            node.animation.addListener(mSetListener);
46917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
47017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Now that all dependencies are set up, start the animations that should be started.
47121cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (mStartDelay <= 0) {
47221cd1389d2ef218b20994b617c57af120841a57fChet Haase            for (Node node : nodesToStart) {
47321cd1389d2ef218b20994b617c57af120841a57fChet Haase                node.animation.start();
47421cd1389d2ef218b20994b617c57af120841a57fChet Haase                mPlayingSet.add(node.animation);
47521cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
47621cd1389d2ef218b20994b617c57af120841a57fChet Haase        } else {
47721cd1389d2ef218b20994b617c57af120841a57fChet Haase            // TODO: Need to cancel out of the delay appropriately
478e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim = ValueAnimator.ofFloat(0f, 1f);
479e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.setDuration(mStartDelay);
480e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.addListener(new AnimatorListenerAdapter() {
481e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                boolean canceled = false;
482e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                public void onAnimationCancel(Animator anim) {
483e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    canceled = true;
484e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                }
485a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                public void onAnimationEnd(Animator anim) {
486e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                    if (!canceled) {
487e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        int numNodes = nodesToStart.size();
488e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        for (int i = 0; i < numNodes; ++i) {
489e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                            Node node = nodesToStart.get(i);
490e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                            node.animation.start();
491e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                            mPlayingSet.add(node.animation);
492e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase                        }
49321cd1389d2ef218b20994b617c57af120841a57fChet Haase                    }
49421cd1389d2ef218b20994b617c57af120841a57fChet Haase                }
49521cd1389d2ef218b20994b617c57af120841a57fChet Haase            });
496e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase            mDelayAnim.start();
49717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
49817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
5017c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
5027c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
5037c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationStart(this);
5042970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                if (mNodes.size() == 0) {
5052970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    // Handle unusual case where empty AnimatorSet is started - should send out
5062970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    // end event immediately since the event will not be sent out at all otherwise
5072970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    tmpListeners.get(i).onAnimationEnd(this);
5082970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                }
50917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
51017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
51117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
51217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
51349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    @Override
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public AnimatorSet clone() {
515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final AnimatorSet anim = (AnimatorSet) super.clone();
51649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        /*
51749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * The basic clone() operation copies all items. This doesn't work very well for
518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * AnimatorSet, because it will copy references that need to be recreated and state
51949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * that may not apply. What we need to do now is put the clone in an uninitialized
52049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * state, with fresh, empty data structures. Then we will build up the nodes list
52149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * manually, as we clone each Node (and its animation). The clone will then be sorted,
52249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * and will populate any appropriate lists, when it is started.
52349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         */
52449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mNeedsSort = true;
52549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mCanceled = false;
526a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingSet = new ArrayList<Animator>();
527a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mNodeMap = new HashMap<Animator, Node>();
52849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mNodes = new ArrayList<Node>();
52949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mSortedNodes = new ArrayList<Node>();
53049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
53149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // Walk through the old nodes list, cloning each node and adding it to the new nodemap.
532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // One problem is that the old node dependencies point to nodes in the old AnimatorSet.
53349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // We need to track the old/new nodes in order to reconstruct the dependencies in the clone.
53449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        HashMap<Node, Node> nodeCloneMap = new HashMap<Node, Node>(); // <old, new>
53549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        for (Node node : mNodes) {
53649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            Node nodeClone = node.clone();
53749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeCloneMap.put(node, nodeClone);
53849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mNodes.add(nodeClone);
53949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mNodeMap.put(nodeClone.animation, nodeClone);
54049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            // Clear out the dependencies in the clone; we'll set these up manually later
54149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.dependencies = null;
54249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.tmpDependencies = null;
54349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.nodeDependents = null;
54449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.nodeDependencies = null;
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // clear out any listeners that were set up by the AnimatorSet; these will
54649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            // be set up when the clone's nodes are sorted
547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> cloneListeners = nodeClone.animation.getListeners();
54849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            if (cloneListeners != null) {
549a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ArrayList<AnimatorListener> listenersToRemove = null;
550a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                for (AnimatorListener listener : cloneListeners) {
551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (listener instanceof AnimatorSetListener) {
55249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        if (listenersToRemove == null) {
553a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            listenersToRemove = new ArrayList<AnimatorListener>();
55449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        }
55549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        listenersToRemove.add(listener);
55649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    }
55749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
55849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                if (listenersToRemove != null) {
559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    for (AnimatorListener listener : listenersToRemove) {
56049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        cloneListeners.remove(listener);
56149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    }
56249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
56349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
56449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
56549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // Now that we've cloned all of the nodes, we're ready to walk through their
56649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // dependencies, mapping the old dependencies to the new nodes
56749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        for (Node node : mNodes) {
56849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            Node nodeClone = nodeCloneMap.get(node);
56949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            if (node.dependencies != null) {
57049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                for (Dependency dependency : node.dependencies) {
57149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    Node clonedDependencyNode = nodeCloneMap.get(dependency.node);
57249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    Dependency cloneDependency = new Dependency(clonedDependencyNode,
57349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                            dependency.rule);
57449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    nodeClone.addDependency(cloneDependency);
57549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
57649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
57749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
57849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
57949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        return anim;
58049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    }
58149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This class is the mechanism by which animations are started based on events in other
58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations. If an animation has multiple dependencies on other animations, then
58517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * all dependencies must be satisfied before the animation is started.
58617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
587a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static class DependencyListener implements AnimatorListener {
58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
589a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private AnimatorSet mAnimatorSet;
590010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
59117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The node upon which the dependency is based.
59217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private Node mNode;
59317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
59417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The Dependency rule (WITH or AFTER) that the listener should wait for on
59517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // the node
59617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private int mRule;
59717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
598a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public DependencyListener(AnimatorSet animatorSet, Node node, int rule) {
599a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            this.mAnimatorSet = animatorSet;
60017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.mNode = node;
60117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.mRule = rule;
60217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
60317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
60417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
605010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * Ignore cancel events for now. We may want to handle this eventually,
606010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * to prevent follow-on animations from running when some dependency
607010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * animation is canceled.
60817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
609a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationCancel(Animator animation) {
61017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
61117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
61217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * An end event is received - see if this is an event we are listening for
61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
615a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationEnd(Animator animation) {
61617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRule == Dependency.AFTER) {
61717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                startIfReady(animation);
61817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
61917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
62017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
62117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
62217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Ignore repeat events for now
62317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
624a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationRepeat(Animator animation) {
62517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
62617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
62817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * A start event is received - see if this is an event we are listening for
62917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
630a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationStart(Animator animation) {
63117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRule == Dependency.WITH) {
63217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                startIfReady(animation);
63317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
63417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
63517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
63617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
63717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Check whether the event received is one that the node was waiting for.
63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * If so, mark it as complete and see whether it's time to start
63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the animation.
64017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param dependencyAnimation the animation that sent the event.
64117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
642a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private void startIfReady(Animator dependencyAnimation) {
643a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mAnimatorSet.mCanceled) {
644a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // if the parent AnimatorSet was canceled, then don't start any dependent anims
645010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                return;
646010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            }
64717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependencyToRemove = null;
6487c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numDependencies = mNode.tmpDependencies.size();
6497c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numDependencies; ++i) {
6507c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Dependency dependency = mNode.tmpDependencies.get(i);
65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (dependency.rule == mRule &&
65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        dependency.node.animation == dependencyAnimation) {
65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // rule fired - remove the dependency and listener and check to
65417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // see whether it's time to start the animation
65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependencyToRemove = dependency;
65617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependencyAnimation.removeListener(this);
65717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    break;
65817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
65917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
66017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNode.tmpDependencies.remove(dependencyToRemove);
66117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mNode.tmpDependencies.size() == 0) {
66217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // all dependencies satisfied: start the animation
66317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNode.animation.start();
664a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mAnimatorSet.mPlayingSet.add(mNode.animation);
66517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
66617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
66817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
670a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private class AnimatorSetListener implements AnimatorListener {
67117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
672a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private AnimatorSet mAnimatorSet;
67317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
674a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        AnimatorSetListener(AnimatorSet animatorSet) {
675a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mAnimatorSet = animatorSet;
67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
678a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationCancel(Animator animation) {
67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mPlayingSet.size() == 0) {
68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (mListeners != null) {
6817c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numListeners = mListeners.size();
6827c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int i = 0; i < numListeners; ++i) {
6837c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        mListeners.get(i).onAnimationCancel(mAnimatorSet);
68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
68517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        @SuppressWarnings("unchecked")
690a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationEnd(Animator animation) {
69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            animation.removeListener(this);
69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mPlayingSet.remove(animation);
693a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Node animNode = mAnimatorSet.mNodeMap.get(animation);
6941e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            animNode.done = true;
695a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes;
696a6e4a1757728efc91627ece602b0899d75303659Chet Haase            boolean allDone = true;
6977c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numSortedNodes = sortedNodes.size();
6987c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numSortedNodes; ++i) {
6997c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                if (!sortedNodes.get(i).done) {
700a6e4a1757728efc91627ece602b0899d75303659Chet Haase                    allDone = false;
701a6e4a1757728efc91627ece602b0899d75303659Chet Haase                    break;
702a6e4a1757728efc91627ece602b0899d75303659Chet Haase                }
703a6e4a1757728efc91627ece602b0899d75303659Chet Haase            }
704a6e4a1757728efc91627ece602b0899d75303659Chet Haase            if (allDone) {
70517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // If this was the last child animation to end, then notify listeners that this
706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // AnimatorSet has ended
70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (mListeners != null) {
708a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    ArrayList<AnimatorListener> tmpListeners =
709a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            (ArrayList<AnimatorListener>) mListeners.clone();
7107c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numListeners = tmpListeners.size();
7117c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int i = 0; i < numListeners; ++i) {
7127c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        tmpListeners.get(i).onAnimationEnd(mAnimatorSet);
71317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
71417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
71517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
71617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
71717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
71817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Nothing to do
719a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationRepeat(Animator animation) {
72017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
72117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
72217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Nothing to do
723a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationStart(Animator animation) {
72417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
72817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method sorts the current set of nodes, if needed. The sort is a simple
73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * DependencyGraph sort, which goes like this:
73117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * - All nodes without dependencies become 'roots'
73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * - while roots list is not null
73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -   for each root r
73417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -     add r to sorted list
73517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -     remove r as a dependency from any other node
73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -   any nodes with no dependencies are added to the roots list
73717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void sortNodes() {
73917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mNeedsSort) {
74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mSortedNodes.clear();
74117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<Node> roots = new ArrayList<Node>();
7427c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numNodes = mNodes.size();
7437c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numNodes; ++i) {
7447c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Node node = mNodes.get(i);
74517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (node.dependencies == null || node.dependencies.size() == 0) {
74617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    roots.add(node);
74717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
74817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
74917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<Node> tmpRoots = new ArrayList<Node>();
75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            while (roots.size() > 0) {
7517c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                int numRoots = roots.size();
7527c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                for (int i = 0; i < numRoots; ++i) {
7537c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    Node root = roots.get(i);
75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    mSortedNodes.add(root);
75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (root.nodeDependents != null) {
7567c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numDependents = root.nodeDependents.size();
7577c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int j = 0; j < numDependents; ++j) {
7587c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            Node node = root.nodeDependents.get(j);
75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies.remove(root);
76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            if (node.nodeDependencies.size() == 0) {
76117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                tmpRoots.add(node);
76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            }
76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
76417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
76517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
766010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                roots.clear();
76717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                roots.addAll(tmpRoots);
76817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                tmpRoots.clear();
76917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
77017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = false;
77117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mSortedNodes.size() != mNodes.size()) {
77217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                throw new IllegalStateException("Circular dependencies cannot exist"
773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        + " in AnimatorSet");
77417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
77517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        } else {
77617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // Doesn't need sorting, but still need to add in the nodeDependencies list
77717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // because these get removed as the event listeners fire and the dependencies
77817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // are satisfied
7797c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numNodes = mNodes.size();
7807c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numNodes; ++i) {
7817c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Node node = mNodes.get(i);
78217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (node.dependencies != null && node.dependencies.size() > 0) {
7837c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numDependencies = node.dependencies.size();
7847c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int j = 0; j < numDependencies; ++j) {
7857c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        Dependency dependency = node.dependencies.get(j);
78617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (node.nodeDependencies == null) {
78717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies = new ArrayList<Node>();
78817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
78917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (!node.nodeDependencies.contains(dependency.node)) {
79017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies.add(dependency.node);
79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
7941e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase                node.done = false;
79517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
79717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
79917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Dependency holds information about the node that some other node is
80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * dependent upon and the nature of that dependency.
80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
80417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static class Dependency {
80517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        static final int WITH = 0; // dependent node must start with this dependency node
80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        static final int AFTER = 1; // dependent node must start when this dependency node finishes
80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The node that the other node with this Dependency is dependent upon
80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public Node node;
81017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
81117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The nature of the dependency (WITH or AFTER)
81217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public int rule;
81317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
81417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public Dependency(Node node, int rule) {
81517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.node = node;
81617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.rule = rule;
81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
81817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
81917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
82017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A Node is an embodiment of both the Animator that it wraps as well as
82217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * any dependencies that are associated with that Animation. This includes
82317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * both dependencies upon other nodes (in the dependencies list) as
82417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * well as dependencies of other nodes upon this (in the nodeDependents list).
82517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
82649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private static class Node implements Cloneable {
827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public Animator animation;
82817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
82917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
83017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  These are the dependencies that this node's animation has on other
83117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  nodes. For example, if this node's animation should begin with some
83217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  other animation ends, then there will be an item in this node's
83317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  dependencies list for that other animation's node.
83417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Dependency> dependencies = null;
83617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
83717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
83817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * tmpDependencies is a runtime detail. We use the dependencies list for sorting.
83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * But we also use the list to keep track of when multiple dependencies are satisfied,
84017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * but removing each dependency as it is satisfied. We do not want to remove
84117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the dependency itself from the list, because we need to retain that information
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * if the AnimatorSet is launched in the future. So we create a copy of the dependency
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * list when the AnimatorSet starts and use this tmpDependencies list to track the
84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * list of satisfied dependencies.
84517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Dependency> tmpDependencies = null;
84717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
84917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * nodeDependencies is just a list of the nodes that this Node is dependent upon.
85017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * This information is used in sortNodes(), to determine when a node is a root.
85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Node> nodeDependencies = null;
85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
85517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * nodeDepdendents is the list of nodes that have this node as a dependency. This
85617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * is a utility field used in sortNodes to facilitate removing this node as a
85717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * dependency when it is a root node.
85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
85917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Node> nodeDependents = null;
86017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
86117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
8621e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase         * Flag indicating whether the animation in this node is finished. This flag
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * is used by AnimatorSet to check, as each animation ends, whether all child animations
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * are done and it's time to send out an end event for the entire AnimatorSet.
8651e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase         */
8661e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase        public boolean done = false;
8671e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase
8681e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase        /**
86917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Constructs the Node with the animation that it encapsulates. A Node has no
87017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * dependencies by default; dependencies are added via the addDependency()
87117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * method.
87217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
87317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param animation The animation that the Node encapsulates.
87417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public Node(Animator animation) {
87617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.animation = animation;
87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
87817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
87917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
88017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Add a dependency to this Node. The dependency includes information about the
88117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * node that this node is dependency upon and the nature of the dependency.
88217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param dependency
88317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public void addDependency(Dependency dependency) {
88517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (dependencies == null) {
88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                dependencies = new ArrayList<Dependency>();
88717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodeDependencies = new ArrayList<Node>();
88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            dependencies.add(dependency);
89017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (!nodeDependencies.contains(dependency.node)) {
89117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodeDependencies.add(dependency.node);
89217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
89317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node dependencyNode = dependency.node;
89417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (dependencyNode.nodeDependents == null) {
89517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                dependencyNode.nodeDependents = new ArrayList<Node>();
89617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
89717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            dependencyNode.nodeDependents.add(this);
89817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
89949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
90049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        @Override
90121cd1389d2ef218b20994b617c57af120841a57fChet Haase        public Node clone() {
90221cd1389d2ef218b20994b617c57af120841a57fChet Haase            try {
90321cd1389d2ef218b20994b617c57af120841a57fChet Haase                Node node = (Node) super.clone();
904a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                node.animation = (Animator) animation.clone();
90521cd1389d2ef218b20994b617c57af120841a57fChet Haase                return node;
90621cd1389d2ef218b20994b617c57af120841a57fChet Haase            } catch (CloneNotSupportedException e) {
90721cd1389d2ef218b20994b617c57af120841a57fChet Haase               throw new AssertionError();
90821cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
90949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
91017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
91117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
91217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
91317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The <code>Builder</code> object is a utility class to facilitate adding animations to a
914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>AnimatorSet</code> along with the relationships between the various animations. The
91517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * intention of the <code>Builder</code> methods, along with the {@link
916a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet#play(Animator) play()} method of <code>AnimatorSet</code> is to make it possible to
91717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * express the dependency relationships of animations in a natural way. Developers can also use
918a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the {@link AnimatorSet#playTogether(Animator[]) playTogether()} and {@link
919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet#playSequentially(Animator[]) playSequentially()} methods if these suit the need,
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * but it might be easier in some situations to express the AnimatorSet of animations in pairs.
92117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
92217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>The <code>Builder</code> object cannot be constructed directly, but is rather constructed
923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * internally via a call to {@link AnimatorSet#play(Animator)}.</p>
92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
925a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to
92617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * play when anim2 finishes, and anim4 to play when anim3 finishes:</p>
92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *     AnimatorSet s = new AnimatorSet();
92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim1).with(anim2);
93017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim2).before(anim3);
93117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim4).after(anim3);
93217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
93317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note in the example that both {@link Builder#before(Animator)} and {@link
935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Builder#after(Animator)} are used. These are just different ways of expressing the same
93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * relationship and are provided to make it easier to say things in a way that is more natural,
93717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * depending on the situation.</p>
93817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
93917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>It is possible to make several calls into the same <code>Builder</code> object to express
94017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * multiple relationships. However, note that it is only the animation passed into the initial
941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorSet#play(Animator)} method that is the dependency in any of the successive
94217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * calls to the <code>Builder</code> object. For example, the following code starts both anim2
94317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and anim3 when anim1 ends; there is no direct dependency relationship between anim2 and
94417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * anim3:
94517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *   AnimatorSet s = new AnimatorSet();
94717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim1).before(anim2).before(anim3);
94817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
94917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * If the desired result is to play anim1 then anim2 then anim3, this code expresses the
95017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * relationship correctly:</p>
95117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
952a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *   AnimatorSet s = new AnimatorSet();
95317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim1).before(anim2);
95417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim2).before(anim3);
95517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
95617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
95717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that it is possible to express relationships that cannot be resolved and will not
95817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * result in sensible results. For example, <code>play(anim1).after(anim1)</code> makes no
95917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sense. In general, circular dependencies like this one (or more indirect ones where a depends
960a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * on b, which depends on c, which depends on a) should be avoided. Only create AnimatorSets
961a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * that can boil down to a simple, one-way relationship of animations starting with, before, and
96217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * after other, different, animations.</p>
96317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
96417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public class Builder {
96517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
96617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
96717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * This tracks the current node being processed. It is supplied to the play() method
968a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * of AnimatorSet and passed into the constructor of Builder.
96917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
97017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private Node mCurrentNode;
97117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
97217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
973a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * package-private constructor. Builders are only constructed by AnimatorSet, when the
97417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * play() method is called.
97517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
97617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that is the dependency for the other animations passed into
97717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the other methods of this Builder object.
97817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
979a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        Builder(Animator anim) {
98017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mCurrentNode = mNodeMap.get(anim);
98117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mCurrentNode == null) {
98217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mCurrentNode = new Node(anim);
98317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, mCurrentNode);
98417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(mCurrentNode);
98517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
98617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
98717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
98817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
98917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play at the same time as the animation supplied in the
990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object.
99117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
99217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that will play when the animation supplied to the
993a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method starts.
99417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
9952970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder with(Animator anim) {
99617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
99717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
99817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
99917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
100017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
100117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
100217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(mCurrentNode, Dependency.WITH);
100317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            node.addDependency(dependency);
10042970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
100517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
100617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
100717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
100817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play when the animation supplied in the
1009a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
101017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * ends.
101117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
101217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that will play when the animation supplied to the
1013a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method ends.
101417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
10152970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder before(Animator anim) {
101617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
101717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
101817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
101917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
102017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
102117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
102217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(mCurrentNode, Dependency.AFTER);
102317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            node.addDependency(dependency);
10242970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
102517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
102617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
102717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
102817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play when the animation supplied in the
1029a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
103017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * to start when the animation supplied in this method call ends.
103117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
103217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation whose end will cause the animation supplied to the
1033a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method to play.
103417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
10352970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder after(Animator anim) {
103617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
103717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
103817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
103917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
104017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
104117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
104217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(node, Dependency.AFTER);
104317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mCurrentNode.addDependency(dependency);
10442970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
104517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
104617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
104717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
104817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the animation supplied in the
1049a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
105017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * to play when the given amount of time elapses.
105117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
105217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param delay The number of milliseconds that should elapse before the
105317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * animation starts.
105417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
10552970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder after(long delay) {
1056a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // setup dummy ValueAnimator just to run the clock
10572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
10582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            anim.setDuration(delay);
10592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            after(anim);
10602970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
106117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
106217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
106317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
106417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
106517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}
1066