AnimatorSet.java revision 37a7bec599e8d877d8a7f12ab2c2c160d1c2cf8a
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
9921cd1389d2ef218b20994b617c57af120841a57fChet Haase
10021cd1389d2ef218b20994b617c57af120841a57fChet Haase    // How long the child animations should last in ms. The default value is negative, which
101a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // simply means that there is no duration set on the AnimatorSet. When a real duration is
10221cd1389d2ef218b20994b617c57af120841a57fChet Haase    // set, it is passed along to the child animations.
10321cd1389d2ef218b20994b617c57af120841a57fChet Haase    private long mDuration = -1;
10421cd1389d2ef218b20994b617c57af120841a57fChet Haase
10521cd1389d2ef218b20994b617c57af120841a57fChet Haase
106010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase    /**
107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets up this AnimatorSet to play all of the supplied animations at the same time.
10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param items The animations that will be started simultaneously.
11017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void playTogether(Animator... items) {
112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (items != null) {
11317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Builder builder = play(items[0]);
115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 1; i < items.length; ++i) {
116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                builder.with(items[i]);
11717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
11817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
12237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * Sets up this AnimatorSet to play all of the supplied animations at the same time.
12337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     *
12437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started simultaneously.
12537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     */
12637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    public void playTogether(Collection<Animator> items) {
12737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        if (items != null && items.size() > 0) {
12837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            mNeedsSort = true;
12937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            Builder builder = null;
13037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            for (Animator anim : items) {
13137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                if (builder == null) {
13237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    builder = play(anim);
13337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                } else {
13437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    builder.with(anim);
13537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                }
13637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            }
13737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        }
13837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    }
13937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase
14037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    /**
141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets up this AnimatorSet to play each of the supplied animations when the
14217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * previous animation ends.
14317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
14437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started one after another.
14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void playSequentially(Animator... items) {
147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (items != null) {
14817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (items.length == 1) {
150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                play(items[0]);
15117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                for (int i = 0; i < items.length - 1; ++i) {
153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    play(items[i]).before(items[i+1]);
15417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
15517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
15617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
15717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
15817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
15917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
16037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * Sets up this AnimatorSet to play each of the supplied animations when the
16137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * previous animation ends.
16237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     *
16337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     * @param items The animations that will be started one after another.
16437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase     */
16537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    public void playSequentially(List<Animator> items) {
16637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        if (items != null && items.size() > 0) {
16737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            mNeedsSort = true;
16837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            if (items.size() == 1) {
16937a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                play(items.get(0));
17037a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            } else {
17137a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                for (int i = 0; i < items.size() - 1; ++i) {
17237a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                    play(items.get(i)).before(items.get(i+1));
17337a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase                }
17437a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase            }
17537a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase        }
17637a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    }
17737a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase
17837a7bec599e8d877d8a7f12ab2c2c160d1c2cf8aChet Haase    /**
179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the current list of child Animator objects controlled by this
180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet. This is a copy of the internal list; modifications to the returned list
181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will not affect the AnimatorSet, although changes to the underlying Animator objects
182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will affect those objects being managed by the AnimatorSet.
183f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     *
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return ArrayList<Animator> The list of child animations of this AnimatorSet.
185f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ArrayList<Animator> getChildAnimations() {
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        ArrayList<Animator> childList = new ArrayList<Animator>();
188f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        for (Node node : mNodes) {
189f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            childList.add(node.animation);
190f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        }
191f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        return childList;
192f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    }
193f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
194f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
195811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     * Sets the target object for all current {@link #getChildAnimations() child animations}
196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of this AnimatorSet that take targets ({@link ObjectAnimator} and
197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet).
198811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     *
199811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     * @param target The object being animated
200811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase     */
20121cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
202811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    public void setTarget(Object target) {
203811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase        for (Node node : mNodes) {
204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Animator animation = node.animation;
205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (animation instanceof AnimatorSet) {
206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ((AnimatorSet)animation).setTarget(target);
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            } else if (animation instanceof ObjectAnimator) {
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ((ObjectAnimator)animation).setTarget(target);
209811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            }
210811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase        }
211811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    }
212811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase
213811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase    /**
214e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase     * Sets the TimeInterpolator for all current {@link #getChildAnimations() child animations}
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of this AnimatorSet.
21621cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param interpolator the interpolator to be used by each child animation of this AnimatorSet
21821cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
21921cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
220e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator interpolator) {
22121cd1389d2ef218b20994b617c57af120841a57fChet Haase        for (Node node : mNodes) {
22221cd1389d2ef218b20994b617c57af120841a57fChet Haase            node.animation.setInterpolator(interpolator);
22321cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
22421cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
22521cd1389d2ef218b20994b617c57af120841a57fChet Haase
22621cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
22717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method creates a <code>Builder</code> object, which is used to
22817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * set up playing constraints. This initial <code>play()</code> method
22917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * tells the <code>Builder</code> the animation that is the dependency for
23017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the succeeding commands to the <code>Builder</code>. For example,
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calling <code>play(a1).with(a2)</code> sets up the AnimatorSet to play
23217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a1</code> and <code>a2</code> at the same time,
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>play(a1).before(a2)</code> sets up the AnimatorSet to play
23417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a1</code> first, followed by <code>a2</code>, and
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>play(a1).after(a2)</code> sets up the AnimatorSet to play
23617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a2</code> first, followed by <code>a1</code>.
23717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
23817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that <code>play()</code> is the only way to tell the
23917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Builder</code> the animation upon which the dependency is created,
24017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * so successive calls to the various functions in <code>Builder</code>
24117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * will all refer to the initial parameter supplied in <code>play()</code>
24217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * as the dependency of the other animations. For example, calling
24317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code>
24417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and <code>a3</code> when a1 ends; it does not set up a dependency between
24517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>a2</code> and <code>a3</code>.</p>
24617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
24717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param anim The animation that is the dependency used in later calls to the
24817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * methods in the returned <code>Builder</code> object. A null parameter will result
24917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * in a null <code>Builder</code> return value.
250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return Builder The object that constructs the AnimatorSet based on the dependencies
25117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * outlined in the calls to <code>play</code> and the other methods in the
25217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Builder</code object.
25317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Builder play(Animator anim) {
25517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (anim != null) {
25617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = true;
25717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            return new Builder(anim);
25817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
25917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return null;
26017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
26117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
26217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
26317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
26417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note that canceling a <code>AnimatorSet</code> also cancels all of the animations that it is
26617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * responsible for.</p>
26717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
26817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @SuppressWarnings("unchecked")
26917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
27017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void cancel() {
271010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        mCanceled = true;
27217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (AnimatorListener listener : tmpListeners) {
27617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationCancel(this);
27717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
27817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
279010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        if (mSortedNodes.size() > 0) {
280010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            for (Node node : mSortedNodes) {
281010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                node.animation.cancel();
28217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
28317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
28417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
28517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
28617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
28717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
28817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
289a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note that ending a <code>AnimatorSet</code> also ends all of the animations that it is
29017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * responsible for.</p>
29117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
29217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
29317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void end() {
294010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        mCanceled = true;
295f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase        if (mSortedNodes.size() != mNodes.size()) {
296f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase            // hasn't been started yet - sort the nodes now, then end them
297f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase            sortNodes();
2981e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            for (Node node : mSortedNodes) {
299a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                if (mSetListener == null) {
300a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mSetListener = new AnimatorSetListener(this);
3011e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase                }
302a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                node.animation.addListener(mSetListener);
3031e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            }
304f1b9b464216d2fac634be9c6aa8605d7c9ed5b68Chet Haase        }
305010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        if (mSortedNodes.size() > 0) {
306010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            for (Node node : mSortedNodes) {
307010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                node.animation.end();
30817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
30917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
31017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
31117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
31217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
313a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns true if any of the child animations of this AnimatorSet have been started and have not
314673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase     * yet ended.
315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return Whether this AnimatorSet has been started and has not yet ended.
316673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase     */
317673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    @Override
318673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    public boolean isRunning() {
319673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        for (Node node : mNodes) {
320673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase            if (node.animation.isRunning()) {
321673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase                return true;
322673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase            }
323673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        }
324673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase        return false;
325673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    }
326673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase
327673e42fafd4088970ec95e1f13c61dc83132c74eChet Haase    /**
32821cd1389d2ef218b20994b617c57af120841a57fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
32921cd1389d2ef218b20994b617c57af120841a57fChet Haase     * {@link #start()} is called.
33021cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
33121cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @return the number of milliseconds to delay running the animation
33221cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
33321cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
33421cd1389d2ef218b20994b617c57af120841a57fChet Haase    public long getStartDelay() {
33521cd1389d2ef218b20994b617c57af120841a57fChet Haase        return mStartDelay;
33621cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
33721cd1389d2ef218b20994b617c57af120841a57fChet Haase
33821cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
33921cd1389d2ef218b20994b617c57af120841a57fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
34021cd1389d2ef218b20994b617c57af120841a57fChet Haase     * {@link #start()} is called.
34121cd1389d2ef218b20994b617c57af120841a57fChet Haase
34221cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param startDelay The amount of the delay, in milliseconds
34321cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
34421cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
34521cd1389d2ef218b20994b617c57af120841a57fChet Haase    public void setStartDelay(long startDelay) {
34621cd1389d2ef218b20994b617c57af120841a57fChet Haase        mStartDelay = startDelay;
34721cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
34821cd1389d2ef218b20994b617c57af120841a57fChet Haase
34921cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
350a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the length of each of the child animations of this AnimatorSet. This value may
351a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be less than 0, which indicates that no duration has been set on this AnimatorSet
35221cd1389d2ef218b20994b617c57af120841a57fChet Haase     * and each of the child animations will use their own duration.
35321cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
35421cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @return The length of the animation, in milliseconds, of each of the child
355a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations of this AnimatorSet.
35621cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
35721cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
35821cd1389d2ef218b20994b617c57af120841a57fChet Haase    public long getDuration() {
35921cd1389d2ef218b20994b617c57af120841a57fChet Haase        return mDuration;
36021cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
36121cd1389d2ef218b20994b617c57af120841a57fChet Haase
36221cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
363a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the length of each of the current child animations of this AnimatorSet. By default,
364a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * each child animation will use its own duration. If the duration is set on the AnimatorSet,
36521cd1389d2ef218b20994b617c57af120841a57fChet Haase     * then each child animation inherits this duration.
36621cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
36721cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param duration The length of the animation, in milliseconds, of each of the child
368a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations of this AnimatorSet.
36921cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
37021cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public AnimatorSet setDuration(long duration) {
37221cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (duration < 0) {
37321cd1389d2ef218b20994b617c57af120841a57fChet Haase            throw new IllegalArgumentException("duration must be a value of zero or greater");
37421cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
37521cd1389d2ef218b20994b617c57af120841a57fChet Haase        for (Node node : mNodes) {
376a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to
37721cd1389d2ef218b20994b617c57af120841a57fChet Haase            // insert "play-after" delays
37821cd1389d2ef218b20994b617c57af120841a57fChet Haase            node.animation.setDuration(duration);
37921cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
38021cd1389d2ef218b20994b617c57af120841a57fChet Haase        mDuration = duration;
3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
38221cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
38321cd1389d2ef218b20994b617c57af120841a57fChet Haase
3842970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    @Override
3852970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    public void setupStartValues() {
3862970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        for (Node node : mNodes) {
3872970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            node.animation.setupStartValues();
3882970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
3892970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    }
3902970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase
3912970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    @Override
3922970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    public void setupEndValues() {
3932970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        for (Node node : mNodes) {
3942970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            node.animation.setupEndValues();
3952970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
3962970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase    }
3972970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase
39821cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
39917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@inheritDoc}
40017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
401a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Starting this <code>AnimatorSet</code> will, in turn, start the animations for which
40217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * it is responsible. The details of when exactly those animations are started depends on
40317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the dependency relationships that have been set up between the animations.
40417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
40517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @SuppressWarnings("unchecked")
40617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
40717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void start() {
408010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase        mCanceled = false;
409010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
41017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // First, sort the nodes (if necessary). This will ensure that sortedNodes
41117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // contains the animation nodes in the correct order.
41217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sortNodes();
41317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
41417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // nodesToStart holds the list of nodes to be started immediately. We don't want to
41517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // start the animations in the loop directly because we first need to set up
41617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // dependencies on all of the nodes. For example, we don't want to start an animation
41717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // when some other animation also wants to start when the first animation begins.
41821cd1389d2ef218b20994b617c57af120841a57fChet Haase        final ArrayList<Node> nodesToStart = new ArrayList<Node>();
4197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        int numSortedNodes = mSortedNodes.size();
4207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        for (int i = 0; i < numSortedNodes; ++i) {
4217c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            Node node = mSortedNodes.get(i);
422a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mSetListener == null) {
423a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mSetListener = new AnimatorSetListener(this);
42417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
42517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node.dependencies == null || node.dependencies.size() == 0) {
42617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodesToStart.add(node);
42717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
4287c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                int numDependencies = node.dependencies.size();
4297c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                for (int j = 0; j < numDependencies; ++j) {
4307c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    Dependency dependency = node.dependencies.get(j);
43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependency.node.animation.addListener(
432010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                            new DependencyListener(this, node, dependency.rule));
43317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
43417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node.tmpDependencies = (ArrayList<Dependency>) node.dependencies.clone();
43517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
436a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            node.animation.addListener(mSetListener);
43717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
43817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Now that all dependencies are set up, start the animations that should be started.
43921cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (mStartDelay <= 0) {
44021cd1389d2ef218b20994b617c57af120841a57fChet Haase            for (Node node : nodesToStart) {
44121cd1389d2ef218b20994b617c57af120841a57fChet Haase                node.animation.start();
44221cd1389d2ef218b20994b617c57af120841a57fChet Haase                mPlayingSet.add(node.animation);
44321cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
44421cd1389d2ef218b20994b617c57af120841a57fChet Haase        } else {
44521cd1389d2ef218b20994b617c57af120841a57fChet Haase            // TODO: Need to cancel out of the delay appropriately
4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            ValueAnimator delayAnim = ValueAnimator.ofFloat(0f, 1f);
4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            delayAnim.setDuration(mStartDelay);
448a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            delayAnim.addListener(new AnimatorListenerAdapter() {
449a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                public void onAnimationEnd(Animator anim) {
4507c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numNodes = nodesToStart.size();
4517c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int i = 0; i < numNodes; ++i) {
4527c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        Node node = nodesToStart.get(i);
45321cd1389d2ef218b20994b617c57af120841a57fChet Haase                        node.animation.start();
45421cd1389d2ef218b20994b617c57af120841a57fChet Haase                        mPlayingSet.add(node.animation);
45521cd1389d2ef218b20994b617c57af120841a57fChet Haase                    }
45621cd1389d2ef218b20994b617c57af120841a57fChet Haase                }
45721cd1389d2ef218b20994b617c57af120841a57fChet Haase            });
4582970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            delayAnim.start();
45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
46017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
461a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
4637c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
4647c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
4657c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationStart(this);
4662970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                if (mNodes.size() == 0) {
4672970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    // Handle unusual case where empty AnimatorSet is started - should send out
4682970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    // end event immediately since the event will not be sent out at all otherwise
4692970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                    tmpListeners.get(i).onAnimationEnd(this);
4702970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase                }
47117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
47217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
47317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
47417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
47549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    @Override
476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public AnimatorSet clone() {
477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final AnimatorSet anim = (AnimatorSet) super.clone();
47849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        /*
47949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * The basic clone() operation copies all items. This doesn't work very well for
480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * AnimatorSet, because it will copy references that need to be recreated and state
48149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * that may not apply. What we need to do now is put the clone in an uninitialized
48249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * state, with fresh, empty data structures. Then we will build up the nodes list
48349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * manually, as we clone each Node (and its animation). The clone will then be sorted,
48449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         * and will populate any appropriate lists, when it is started.
48549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase         */
48649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mNeedsSort = true;
48749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mCanceled = false;
488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingSet = new ArrayList<Animator>();
489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mNodeMap = new HashMap<Animator, Node>();
49049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mNodes = new ArrayList<Node>();
49149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mSortedNodes = new ArrayList<Node>();
49249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
49349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // Walk through the old nodes list, cloning each node and adding it to the new nodemap.
494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // One problem is that the old node dependencies point to nodes in the old AnimatorSet.
49549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // We need to track the old/new nodes in order to reconstruct the dependencies in the clone.
49649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        HashMap<Node, Node> nodeCloneMap = new HashMap<Node, Node>(); // <old, new>
49749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        for (Node node : mNodes) {
49849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            Node nodeClone = node.clone();
49949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeCloneMap.put(node, nodeClone);
50049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mNodes.add(nodeClone);
50149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mNodeMap.put(nodeClone.animation, nodeClone);
50249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            // Clear out the dependencies in the clone; we'll set these up manually later
50349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.dependencies = null;
50449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.tmpDependencies = null;
50549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.nodeDependents = null;
50649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            nodeClone.nodeDependencies = null;
507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // clear out any listeners that were set up by the AnimatorSet; these will
50849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            // be set up when the clone's nodes are sorted
509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> cloneListeners = nodeClone.animation.getListeners();
51049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            if (cloneListeners != null) {
511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                ArrayList<AnimatorListener> listenersToRemove = null;
512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                for (AnimatorListener listener : cloneListeners) {
513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (listener instanceof AnimatorSetListener) {
51449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        if (listenersToRemove == null) {
515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            listenersToRemove = new ArrayList<AnimatorListener>();
51649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        }
51749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        listenersToRemove.add(listener);
51849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    }
51949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
52049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                if (listenersToRemove != null) {
521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    for (AnimatorListener listener : listenersToRemove) {
52249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                        cloneListeners.remove(listener);
52349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    }
52449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
52549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
52649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
52749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // Now that we've cloned all of the nodes, we're ready to walk through their
52849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        // dependencies, mapping the old dependencies to the new nodes
52949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        for (Node node : mNodes) {
53049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            Node nodeClone = nodeCloneMap.get(node);
53149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            if (node.dependencies != null) {
53249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                for (Dependency dependency : node.dependencies) {
53349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    Node clonedDependencyNode = nodeCloneMap.get(dependency.node);
53449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    Dependency cloneDependency = new Dependency(clonedDependencyNode,
53549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                            dependency.rule);
53649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                    nodeClone.addDependency(cloneDependency);
53749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                }
53849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
53949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
54049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
54149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        return anim;
54249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    }
54349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
54417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
54517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This class is the mechanism by which animations are started based on events in other
54617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations. If an animation has multiple dependencies on other animations, then
54717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * all dependencies must be satisfied before the animation is started.
54817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
549a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static class DependencyListener implements AnimatorListener {
55017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private AnimatorSet mAnimatorSet;
552010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase
55317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The node upon which the dependency is based.
55417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private Node mNode;
55517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
55617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The Dependency rule (WITH or AFTER) that the listener should wait for on
55717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // the node
55817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private int mRule;
55917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
560a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public DependencyListener(AnimatorSet animatorSet, Node node, int rule) {
561a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            this.mAnimatorSet = animatorSet;
56217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.mNode = node;
56317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.mRule = rule;
56417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
56517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
56617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
567010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * Ignore cancel events for now. We may want to handle this eventually,
568010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * to prevent follow-on animations from running when some dependency
569010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase         * animation is canceled.
57017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
571a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationCancel(Animator animation) {
57217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
57317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
57417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * An end event is received - see if this is an event we are listening for
57617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
577a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationEnd(Animator animation) {
57817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRule == Dependency.AFTER) {
57917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                startIfReady(animation);
58017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
58117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Ignore repeat events for now
58517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
586a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationRepeat(Animator animation) {
58717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
58917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
59017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * A start event is received - see if this is an event we are listening for
59117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
592a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationStart(Animator animation) {
59317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRule == Dependency.WITH) {
59417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                startIfReady(animation);
59517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
59617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
59717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
59817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
59917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Check whether the event received is one that the node was waiting for.
60017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * If so, mark it as complete and see whether it's time to start
60117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the animation.
60217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param dependencyAnimation the animation that sent the event.
60317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
604a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private void startIfReady(Animator dependencyAnimation) {
605a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mAnimatorSet.mCanceled) {
606a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // if the parent AnimatorSet was canceled, then don't start any dependent anims
607010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                return;
608010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase            }
60917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependencyToRemove = null;
6107c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numDependencies = mNode.tmpDependencies.size();
6117c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numDependencies; ++i) {
6127c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Dependency dependency = mNode.tmpDependencies.get(i);
61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (dependency.rule == mRule &&
61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        dependency.node.animation == dependencyAnimation) {
61517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // rule fired - remove the dependency and listener and check to
61617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // see whether it's time to start the animation
61717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependencyToRemove = dependency;
61817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    dependencyAnimation.removeListener(this);
61917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    break;
62017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
62117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
62217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNode.tmpDependencies.remove(dependencyToRemove);
62317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mNode.tmpDependencies.size() == 0) {
62417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // all dependencies satisfied: start the animation
62517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNode.animation.start();
626a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mAnimatorSet.mPlayingSet.add(mNode.animation);
62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
62817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
62917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
63017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
63117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
632a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private class AnimatorSetListener implements AnimatorListener {
63317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
634a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        private AnimatorSet mAnimatorSet;
63517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
636a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        AnimatorSetListener(AnimatorSet animatorSet) {
637a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mAnimatorSet = animatorSet;
63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
640a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationCancel(Animator animation) {
64117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mPlayingSet.size() == 0) {
64217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (mListeners != null) {
6437c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numListeners = mListeners.size();
6447c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int i = 0; i < numListeners; ++i) {
6457c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        mListeners.get(i).onAnimationCancel(mAnimatorSet);
64617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
64717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
64817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
64917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        @SuppressWarnings("unchecked")
652a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationEnd(Animator animation) {
65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            animation.removeListener(this);
65417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mPlayingSet.remove(animation);
655a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Node animNode = mAnimatorSet.mNodeMap.get(animation);
6561e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase            animNode.done = true;
657a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<Node> sortedNodes = mAnimatorSet.mSortedNodes;
658a6e4a1757728efc91627ece602b0899d75303659Chet Haase            boolean allDone = true;
6597c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numSortedNodes = sortedNodes.size();
6607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numSortedNodes; ++i) {
6617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                if (!sortedNodes.get(i).done) {
662a6e4a1757728efc91627ece602b0899d75303659Chet Haase                    allDone = false;
663a6e4a1757728efc91627ece602b0899d75303659Chet Haase                    break;
664a6e4a1757728efc91627ece602b0899d75303659Chet Haase                }
665a6e4a1757728efc91627ece602b0899d75303659Chet Haase            }
666a6e4a1757728efc91627ece602b0899d75303659Chet Haase            if (allDone) {
66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // If this was the last child animation to end, then notify listeners that this
668a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // AnimatorSet has ended
66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (mListeners != null) {
670a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    ArrayList<AnimatorListener> tmpListeners =
671a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            (ArrayList<AnimatorListener>) mListeners.clone();
6727c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numListeners = tmpListeners.size();
6737c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int i = 0; i < numListeners; ++i) {
6747c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        tmpListeners.get(i).onAnimationEnd(mAnimatorSet);
67517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
67817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Nothing to do
681a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationRepeat(Animator animation) {
68217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
68317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Nothing to do
685a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void onAnimationStart(Animator animation) {
68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method sorts the current set of nodes, if needed. The sort is a simple
69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * DependencyGraph sort, which goes like this:
69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * - All nodes without dependencies become 'roots'
69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * - while roots list is not null
69517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -   for each root r
69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -     add r to sorted list
69717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -     remove r as a dependency from any other node
69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * -   any nodes with no dependencies are added to the roots list
69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
70017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void sortNodes() {
70117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mNeedsSort) {
70217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mSortedNodes.clear();
70317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<Node> roots = new ArrayList<Node>();
7047c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numNodes = mNodes.size();
7057c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numNodes; ++i) {
7067c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Node node = mNodes.get(i);
70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (node.dependencies == null || node.dependencies.size() == 0) {
70817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    roots.add(node);
70917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
71017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
71117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<Node> tmpRoots = new ArrayList<Node>();
71217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            while (roots.size() > 0) {
7137c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                int numRoots = roots.size();
7147c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                for (int i = 0; i < numRoots; ++i) {
7157c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    Node root = roots.get(i);
71617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    mSortedNodes.add(root);
71717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (root.nodeDependents != null) {
7187c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numDependents = root.nodeDependents.size();
7197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int j = 0; j < numDependents; ++j) {
7207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            Node node = root.nodeDependents.get(j);
72117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies.remove(root);
72217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            if (node.nodeDependencies.size() == 0) {
72317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                tmpRoots.add(node);
72417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            }
72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
728010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase                roots.clear();
72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                roots.addAll(tmpRoots);
73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                tmpRoots.clear();
73117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mNeedsSort = false;
73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mSortedNodes.size() != mNodes.size()) {
73417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                throw new IllegalStateException("Circular dependencies cannot exist"
735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        + " in AnimatorSet");
73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
73717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        } else {
73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // Doesn't need sorting, but still need to add in the nodeDependencies list
73917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // because these get removed as the event listeners fire and the dependencies
74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // are satisfied
7417c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numNodes = mNodes.size();
7427c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numNodes; ++i) {
7437c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                Node node = mNodes.get(i);
74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (node.dependencies != null && node.dependencies.size() > 0) {
7457c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    int numDependencies = node.dependencies.size();
7467c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                    for (int j = 0; j < numDependencies; ++j) {
7477c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        Dependency dependency = node.dependencies.get(j);
74817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (node.nodeDependencies == null) {
74917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies = new ArrayList<Node>();
75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
75117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (!node.nodeDependencies.contains(dependency.node)) {
75217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            node.nodeDependencies.add(dependency.node);
75317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
7561e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase                node.done = false;
75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
76117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Dependency holds information about the node that some other node is
76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * dependent upon and the nature of that dependency.
76417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
76517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
76617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static class Dependency {
76717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        static final int WITH = 0; // dependent node must start with this dependency node
76817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        static final int AFTER = 1; // dependent node must start when this dependency node finishes
76917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
77017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The node that the other node with this Dependency is dependent upon
77117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public Node node;
77217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
77317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // The nature of the dependency (WITH or AFTER)
77417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public int rule;
77517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
77617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public Dependency(Node node, int rule) {
77717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.node = node;
77817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.rule = rule;
77917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
78017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
78117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
78217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A Node is an embodiment of both the Animator that it wraps as well as
78417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * any dependencies that are associated with that Animation. This includes
78517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * both dependencies upon other nodes (in the dependencies list) as
78617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * well as dependencies of other nodes upon this (in the nodeDependents list).
78717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
78849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    private static class Node implements Cloneable {
789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public Animator animation;
79017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  These are the dependencies that this node's animation has on other
79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  nodes. For example, if this node's animation should begin with some
79417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  other animation ends, then there will be an item in this node's
79517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *  dependencies list for that other animation's node.
79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
79717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Dependency> dependencies = null;
79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
79917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * tmpDependencies is a runtime detail. We use the dependencies list for sorting.
80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * But we also use the list to keep track of when multiple dependencies are satisfied,
80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * but removing each dependency as it is satisfied. We do not want to remove
80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the dependency itself from the list, because we need to retain that information
804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * if the AnimatorSet is launched in the future. So we create a copy of the dependency
805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * list when the AnimatorSet starts and use this tmpDependencies list to track the
80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * list of satisfied dependencies.
80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Dependency> tmpDependencies = null;
80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
81017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
81117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * nodeDependencies is just a list of the nodes that this Node is dependent upon.
81217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * This information is used in sortNodes(), to determine when a node is a root.
81317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
81417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Node> nodeDependencies = null;
81517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
81617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * nodeDepdendents is the list of nodes that have this node as a dependency. This
81817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * is a utility field used in sortNodes to facilitate removing this node as a
81917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * dependency when it is a root node.
82017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
82117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public ArrayList<Node> nodeDependents = null;
82217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
82317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
8241e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase         * Flag indicating whether the animation in this node is finished. This flag
825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * is used by AnimatorSet to check, as each animation ends, whether all child animations
826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * are done and it's time to send out an end event for the entire AnimatorSet.
8271e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase         */
8281e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase        public boolean done = false;
8291e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase
8301e0ac5a1063d00670f4dc7ca5b120ef2836f9e8fChet Haase        /**
83117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Constructs the Node with the animation that it encapsulates. A Node has no
83217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * dependencies by default; dependencies are added via the addDependency()
83317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * method.
83417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param animation The animation that the Node encapsulates.
83617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public Node(Animator animation) {
83817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            this.animation = animation;
83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
84017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
84117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
84217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Add a dependency to this Node. The dependency includes information about the
84317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * node that this node is dependency upon and the nature of the dependency.
84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param dependency
84517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public void addDependency(Dependency dependency) {
84717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (dependencies == null) {
84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                dependencies = new ArrayList<Dependency>();
84917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodeDependencies = new ArrayList<Node>();
85017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            dependencies.add(dependency);
85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (!nodeDependencies.contains(dependency.node)) {
85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                nodeDependencies.add(dependency.node);
85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
85517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node dependencyNode = dependency.node;
85617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (dependencyNode.nodeDependents == null) {
85717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                dependencyNode.nodeDependents = new ArrayList<Node>();
85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
85917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            dependencyNode.nodeDependents.add(this);
86017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
86149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
86249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        @Override
86321cd1389d2ef218b20994b617c57af120841a57fChet Haase        public Node clone() {
86421cd1389d2ef218b20994b617c57af120841a57fChet Haase            try {
86521cd1389d2ef218b20994b617c57af120841a57fChet Haase                Node node = (Node) super.clone();
866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                node.animation = (Animator) animation.clone();
86721cd1389d2ef218b20994b617c57af120841a57fChet Haase                return node;
86821cd1389d2ef218b20994b617c57af120841a57fChet Haase            } catch (CloneNotSupportedException e) {
86921cd1389d2ef218b20994b617c57af120841a57fChet Haase               throw new AssertionError();
87021cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
87149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
87217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
87317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
87417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
87517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The <code>Builder</code> object is a utility class to facilitate adding animations to a
876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>AnimatorSet</code> along with the relationships between the various animations. The
87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * intention of the <code>Builder</code> methods, along with the {@link
878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet#play(Animator) play()} method of <code>AnimatorSet</code> is to make it possible to
87917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * express the dependency relationships of animations in a natural way. Developers can also use
880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the {@link AnimatorSet#playTogether(Animator[]) playTogether()} and {@link
881a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * AnimatorSet#playSequentially(Animator[]) playSequentially()} methods if these suit the need,
882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * but it might be easier in some situations to express the AnimatorSet of animations in pairs.
88317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>The <code>Builder</code> object cannot be constructed directly, but is rather constructed
885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * internally via a call to {@link AnimatorSet#play(Animator)}.</p>
88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>For example, this sets up a AnimatorSet to play anim1 and anim2 at the same time, anim3 to
88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * play when anim2 finishes, and anim4 to play when anim3 finishes:</p>
88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *     AnimatorSet s = new AnimatorSet();
89117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim1).with(anim2);
89217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim2).before(anim3);
89317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *     s.play(anim4).after(anim3);
89417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
89517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Note in the example that both {@link Builder#before(Animator)} and {@link
897a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Builder#after(Animator)} are used. These are just different ways of expressing the same
89817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * relationship and are provided to make it easier to say things in a way that is more natural,
89917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * depending on the situation.</p>
90017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
90117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>It is possible to make several calls into the same <code>Builder</code> object to express
90217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * multiple relationships. However, note that it is only the animation passed into the initial
903a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorSet#play(Animator)} method that is the dependency in any of the successive
90417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * calls to the <code>Builder</code> object. For example, the following code starts both anim2
90517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and anim3 when anim1 ends; there is no direct dependency relationship between anim2 and
90617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * anim3:
90717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
908a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *   AnimatorSet s = new AnimatorSet();
90917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim1).before(anim2).before(anim3);
91017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
91117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * If the desired result is to play anim1 then anim2 then anim3, this code expresses the
91217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * relationship correctly:</p>
91317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <pre>
914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *   AnimatorSet s = new AnimatorSet();
91517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim1).before(anim2);
91617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *   s.play(anim2).before(anim3);
91717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * </pre>
91817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p/>
91917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that it is possible to express relationships that cannot be resolved and will not
92017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * result in sensible results. For example, <code>play(anim1).after(anim1)</code> makes no
92117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sense. In general, circular dependencies like this one (or more indirect ones where a depends
922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * on b, which depends on c, which depends on a) should be avoided. Only create AnimatorSets
923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * that can boil down to a simple, one-way relationship of animations starting with, before, and
92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * after other, different, animations.</p>
92517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
92617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public class Builder {
92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
92817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * This tracks the current node being processed. It is supplied to the play() method
930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * of AnimatorSet and passed into the constructor of Builder.
93117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
93217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        private Node mCurrentNode;
93317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
93417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * package-private constructor. Builders are only constructed by AnimatorSet, when the
93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * play() method is called.
93717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
93817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that is the dependency for the other animations passed into
93917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * the other methods of this Builder object.
94017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        Builder(Animator anim) {
94217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mCurrentNode = mNodeMap.get(anim);
94317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mCurrentNode == null) {
94417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mCurrentNode = new Node(anim);
94517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, mCurrentNode);
94617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(mCurrentNode);
94717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
94817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
94917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
95017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
95117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play at the same time as the animation supplied in the
952a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object.
95317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
95417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that will play when the animation supplied to the
955a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method starts.
95617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
9572970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder with(Animator anim) {
95817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
95917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
96017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
96117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
96217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
96317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
96417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(mCurrentNode, Dependency.WITH);
96517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            node.addDependency(dependency);
9662970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
96717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
96817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
96917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
97017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play when the animation supplied in the
971a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
97217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * ends.
97317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
97417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation that will play when the animation supplied to the
975a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method ends.
97617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
9772970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder before(Animator anim) {
97817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
97917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
98017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
98117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
98217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
98317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
98417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(mCurrentNode, Dependency.AFTER);
98517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            node.addDependency(dependency);
9862970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
98717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
98817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
98917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
99017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the given animation to play when the animation supplied in the
991a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
99217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * to start when the animation supplied in this method call ends.
99317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
99417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param anim The animation whose end will cause the animation supplied to the
995a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} method to play.
99617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
9972970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder after(Animator anim) {
99817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Node node = mNodeMap.get(anim);
99917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (node == null) {
100017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                node = new Node(anim);
100117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodeMap.put(anim, node);
100217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mNodes.add(node);
100317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
100417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            Dependency dependency = new Dependency(node, Dependency.AFTER);
100517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mCurrentNode.addDependency(dependency);
10062970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
100717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
100817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
100917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
101017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * Sets up the animation supplied in the
1011a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
101217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * to play when the given amount of time elapses.
101317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
101417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param delay The number of milliseconds that should elapse before the
101517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * animation starts.
101617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
10172970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        public Builder after(long delay) {
1018a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // setup dummy ValueAnimator just to run the clock
10192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
10202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            anim.setDuration(delay);
10212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            after(anim);
10222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            return this;
102317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
102417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
102517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
102617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
102717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}
1028