Transition.java revision 21f54f39408d43705e4aa7060c562271ca2886f1
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.transition;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.TimeInterpolator;
22import android.content.Context;
23import android.content.res.TypedArray;
24import android.graphics.Path;
25import android.graphics.Rect;
26import android.util.ArrayMap;
27import android.util.AttributeSet;
28import android.util.Log;
29import android.util.LongSparseArray;
30import android.util.SparseArray;
31import android.util.SparseLongArray;
32import android.view.InflateException;
33import android.view.SurfaceView;
34import android.view.TextureView;
35import android.view.View;
36import android.view.ViewGroup;
37import android.view.ViewOverlay;
38import android.view.WindowId;
39import android.view.animation.AnimationUtils;
40import android.widget.ListView;
41import android.widget.Spinner;
42
43import java.util.ArrayList;
44import java.util.List;
45import java.util.StringTokenizer;
46
47import com.android.internal.R;
48
49/**
50 * A Transition holds information about animations that will be run on its
51 * targets during a scene change. Subclasses of this abstract class may
52 * choreograph several child transitions ({@link TransitionSet} or they may
53 * perform custom animations themselves. Any Transition has two main jobs:
54 * (1) capture property values, and (2) play animations based on changes to
55 * captured property values. A custom transition knows what property values
56 * on View objects are of interest to it, and also knows how to animate
57 * changes to those values. For example, the {@link Fade} transition tracks
58 * changes to visibility-related properties and is able to construct and run
59 * animations that fade items in or out based on changes to those properties.
60 *
61 * <p>Note: Transitions may not work correctly with either {@link SurfaceView}
62 * or {@link TextureView}, due to the way that these views are displayed
63 * on the screen. For SurfaceView, the problem is that the view is updated from
64 * a non-UI thread, so changes to the view due to transitions (such as moving
65 * and resizing the view) may be out of sync with the display inside those bounds.
66 * TextureView is more compatible with transitions in general, but some
67 * specific transitions (such as {@link Fade}) may not be compatible
68 * with TextureView because they rely on {@link ViewOverlay} functionality,
69 * which does not currently work with TextureView.</p>
70 *
71 * <p>Transitions can be declared in XML resource files inside the <code>res/transition</code>
72 * directory. Transition resources consist of a tag name for one of the Transition
73 * subclasses along with attributes to define some of the attributes of that transition.
74 * For example, here is a minimal resource file that declares a {@link ChangeBounds} transition:
75 *
76 * {@sample development/samples/ApiDemos/res/transition/changebounds.xml ChangeBounds}
77 *
78 * <p>This TransitionSet contains {@link android.transition.Explode} for visibility,
79 * {@link android.transition.ChangeBounds}, {@link android.transition.ChangeTransform},
80 * and {@link android.transition.ChangeClipBounds} and
81 * {@link android.transition.ChangeImageTransform}:</p>
82 *
83 * {@sample development/samples/ApiDemos/res/transition/explode_move_together.xml MultipleTransform}
84 *
85 * <p>Custom transition classes may be instantiated with a <code>transition</code> tag:</p>
86 * <pre>&lt;transition class="my.app.transition.CustomTransition"/></pre>
87 * <p>Custom transition classes loaded from XML must have a public nullary (no argument)
88 * constructor.</p>
89 *
90 * <p>Note that attributes for the transition are not required, just as they are
91 * optional when declared in code; Transitions created from XML resources will use
92 * the same defaults as their code-created equivalents. Here is a slightly more
93 * elaborate example which declares a {@link TransitionSet} transition with
94 * {@link ChangeBounds} and {@link Fade} child transitions:</p>
95 *
96 * {@sample
97 * development/samples/ApiDemos/res/transition/changebounds_fadeout_sequential.xml TransitionSet}
98 *
99 * <p>In this example, the transitionOrdering attribute is used on the TransitionSet
100 * object to change from the default {@link TransitionSet#ORDERING_TOGETHER} behavior
101 * to be {@link TransitionSet#ORDERING_SEQUENTIAL} instead. Also, the {@link Fade}
102 * transition uses a fadingMode of {@link Fade#OUT} instead of the default
103 * out-in behavior. Finally, note the use of the <code>targets</code> sub-tag, which
104 * takes a set of {@link android.R.styleable#TransitionTarget target} tags, each
105 * of which lists a specific <code>targetId</code>, <code>targetClass</code>,
106 * <code>targetName</code>, <code>excludeId</code>, <code>excludeClass</code>, or
107 * <code>excludeName</code>, which this transition acts upon.
108 * Use of targets is optional, but can be used to either limit the time spent checking
109 * attributes on unchanging views, or limiting the types of animations run on specific views.
110 * In this case, we know that only the <code>grayscaleContainer</code> will be
111 * disappearing, so we choose to limit the {@link Fade} transition to only that view.</p>
112 *
113 * Further information on XML resource descriptions for transitions can be found for
114 * {@link android.R.styleable#Transition}, {@link android.R.styleable#TransitionSet},
115 * {@link android.R.styleable#TransitionTarget}, {@link android.R.styleable#Fade},
116 * {@link android.R.styleable#Slide}, and {@link android.R.styleable#ChangeTransform}.
117 *
118 */
119public abstract class Transition implements Cloneable {
120
121    private static final String LOG_TAG = "Transition";
122    static final boolean DBG = false;
123
124    /**
125     * With {@link #setMatchOrder(int...)}, chooses to match by View instance.
126     */
127    public static final int MATCH_INSTANCE = 0x1;
128    private static final int MATCH_FIRST = MATCH_INSTANCE;
129
130    /**
131     * With {@link #setMatchOrder(int...)}, chooses to match by
132     * {@link android.view.View#getTransitionName()}. Null names will not be matched.
133     */
134    public static final int MATCH_NAME = 0x2;
135
136    /**
137     * With {@link #setMatchOrder(int...)}, chooses to match by
138     * {@link android.view.View#getId()}. Negative IDs will not be matched.
139     */
140    public static final int MATCH_ID = 0x3;
141
142    /**
143     * With {@link #setMatchOrder(int...)}, chooses to match by the {@link android.widget.Adapter}
144     * item id. When {@link android.widget.Adapter#hasStableIds()} returns false, no match
145     * will be made for items.
146     */
147    public static final int MATCH_ITEM_ID = 0x4;
148
149    private static final int MATCH_LAST = MATCH_ITEM_ID;
150
151    private static final String MATCH_INSTANCE_STR = "instance";
152    private static final String MATCH_NAME_STR = "name";
153    /** To be removed before L release */
154    private static final String MATCH_VIEW_NAME_STR = "viewName";
155    private static final String MATCH_ID_STR = "id";
156    private static final String MATCH_ITEM_ID_STR = "itemId";
157
158    private static final int[] DEFAULT_MATCH_ORDER = {
159        MATCH_NAME,
160        MATCH_INSTANCE,
161        MATCH_ID,
162        MATCH_ITEM_ID,
163    };
164
165    private static final PathMotion STRAIGHT_PATH_MOTION = new PathMotion() {
166        @Override
167        public Path getPath(float startX, float startY, float endX, float endY) {
168            Path path = new Path();
169            path.moveTo(startX, startY);
170            path.lineTo(endX, endY);
171            return path;
172        }
173    };
174
175    private String mName = getClass().getName();
176
177    long mStartDelay = -1;
178    long mDuration = -1;
179    TimeInterpolator mInterpolator = null;
180    ArrayList<Integer> mTargetIds = new ArrayList<Integer>();
181    ArrayList<View> mTargets = new ArrayList<View>();
182    ArrayList<String> mTargetNames = null;
183    ArrayList<Class> mTargetTypes = null;
184    ArrayList<Integer> mTargetIdExcludes = null;
185    ArrayList<View> mTargetExcludes = null;
186    ArrayList<Class> mTargetTypeExcludes = null;
187    ArrayList<String> mTargetNameExcludes = null;
188    ArrayList<Integer> mTargetIdChildExcludes = null;
189    ArrayList<View> mTargetChildExcludes = null;
190    ArrayList<Class> mTargetTypeChildExcludes = null;
191    private TransitionValuesMaps mStartValues = new TransitionValuesMaps();
192    private TransitionValuesMaps mEndValues = new TransitionValuesMaps();
193    TransitionSet mParent = null;
194    private int[] mMatchOrder = DEFAULT_MATCH_ORDER;
195    ArrayList<TransitionValues> mStartValuesList; // only valid after playTransition starts
196    ArrayList<TransitionValues> mEndValuesList; // only valid after playTransitions starts
197
198    // Per-animator information used for later canceling when future transitions overlap
199    private static ThreadLocal<ArrayMap<Animator, AnimationInfo>> sRunningAnimators =
200            new ThreadLocal<ArrayMap<Animator, AnimationInfo>>();
201
202    // Scene Root is set at createAnimator() time in the cloned Transition
203    ViewGroup mSceneRoot = null;
204
205    // Whether removing views from their parent is possible. This is only for views
206    // in the start scene, which are no longer in the view hierarchy. This property
207    // is determined by whether the previous Scene was created from a layout
208    // resource, and thus the views from the exited scene are going away anyway
209    // and can be removed as necessary to achieve a particular effect, such as
210    // removing them from parents to add them to overlays.
211    boolean mCanRemoveViews = false;
212
213    // Track all animators in use in case the transition gets canceled and needs to
214    // cancel running animators
215    private ArrayList<Animator> mCurrentAnimators = new ArrayList<Animator>();
216
217    // Number of per-target instances of this Transition currently running. This count is
218    // determined by calls to start() and end()
219    int mNumInstances = 0;
220
221    // Whether this transition is currently paused, due to a call to pause()
222    boolean mPaused = false;
223
224    // Whether this transition has ended. Used to avoid pause/resume on transitions
225    // that have completed
226    private boolean mEnded = false;
227
228    // The set of listeners to be sent transition lifecycle events.
229    ArrayList<TransitionListener> mListeners = null;
230
231    // The set of animators collected from calls to createAnimator(),
232    // to be run in runAnimators()
233    ArrayList<Animator> mAnimators = new ArrayList<Animator>();
234
235    // The function for calculating the Animation start delay.
236    TransitionPropagation mPropagation;
237
238    // The rectangular region for Transitions like Explode and TransitionPropagations
239    // like CircularPropagation
240    EpicenterCallback mEpicenterCallback;
241
242    // For Fragment shared element transitions, linking views explicitly by mismatching
243    // transitionNames.
244    ArrayMap<String, String> mNameOverrides;
245
246    // The function used to interpolate along two-dimensional points. Typically used
247    // for adding curves to x/y View motion.
248    private PathMotion mPathMotion = STRAIGHT_PATH_MOTION;
249
250    /**
251     * Constructs a Transition object with no target objects. A transition with
252     * no targets defaults to running on all target objects in the scene hierarchy
253     * (if the transition is not contained in a TransitionSet), or all target
254     * objects passed down from its parent (if it is in a TransitionSet).
255     */
256    public Transition() {}
257
258    /**
259     * Perform inflation from XML and apply a class-specific base style from a
260     * theme attribute or style resource. This constructor of Transition allows
261     * subclasses to use their own base style when they are inflating.
262     *
263     * @param context The Context the transition is running in, through which it can
264     *        access the current theme, resources, etc.
265     * @param attrs The attributes of the XML tag that is inflating the transition.
266     */
267    public Transition(Context context, AttributeSet attrs) {
268
269        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Transition);
270        long duration = a.getInt(R.styleable.Transition_duration, -1);
271        if (duration >= 0) {
272            setDuration(duration);
273        }
274        long startDelay = a.getInt(R.styleable.Transition_startDelay, -1);
275        if (startDelay > 0) {
276            setStartDelay(startDelay);
277        }
278        final int resID = a.getResourceId(com.android.internal.R.styleable.Animator_interpolator, 0);
279        if (resID > 0) {
280            setInterpolator(AnimationUtils.loadInterpolator(context, resID));
281        }
282        String matchOrder = a.getString(R.styleable.Transition_matchOrder);
283        if (matchOrder != null) {
284            setMatchOrder(parseMatchOrder(matchOrder));
285        }
286        a.recycle();
287    }
288
289    private static int[] parseMatchOrder(String matchOrderString) {
290        StringTokenizer st = new StringTokenizer(matchOrderString, ",");
291        int matches[] = new int[st.countTokens()];
292        int index = 0;
293        while (st.hasMoreTokens()) {
294            String token = st.nextToken().trim();
295            if (MATCH_ID_STR.equalsIgnoreCase(token)) {
296                matches[index] = Transition.MATCH_ID;
297            } else if (MATCH_INSTANCE_STR.equalsIgnoreCase(token)) {
298                matches[index] = Transition.MATCH_INSTANCE;
299            } else if (MATCH_NAME_STR.equalsIgnoreCase(token)) {
300                matches[index] = Transition.MATCH_NAME;
301            } else if (MATCH_VIEW_NAME_STR.equalsIgnoreCase(token)) {
302                matches[index] = Transition.MATCH_NAME;
303            } else if (MATCH_ITEM_ID_STR.equalsIgnoreCase(token)) {
304                matches[index] = Transition.MATCH_ITEM_ID;
305            } else if (token.isEmpty()) {
306                int[] smallerMatches = new int[matches.length - 1];
307                System.arraycopy(matches, 0, smallerMatches, 0, index);
308                matches = smallerMatches;
309                index--;
310            } else {
311                throw new InflateException("Unknown match type in matchOrder: '" + token + "'");
312            }
313            index++;
314        }
315        return matches;
316    }
317
318    /**
319     * Sets the duration of this transition. By default, there is no duration
320     * (indicated by a negative number), which means that the Animator created by
321     * the transition will have its own specified duration. If the duration of a
322     * Transition is set, that duration will override the Animator duration.
323     *
324     * @param duration The length of the animation, in milliseconds.
325     * @return This transition object.
326     * @attr ref android.R.styleable#Transition_duration
327     */
328    public Transition setDuration(long duration) {
329        mDuration = duration;
330        return this;
331    }
332
333    /**
334     * Returns the duration set on this transition. If no duration has been set,
335     * the returned value will be negative, indicating that resulting animators will
336     * retain their own durations.
337     *
338     * @return The duration set on this transition, in milliseconds, if one has been
339     * set, otherwise returns a negative number.
340     */
341    public long getDuration() {
342        return mDuration;
343    }
344
345    /**
346     * Sets the startDelay of this transition. By default, there is no delay
347     * (indicated by a negative number), which means that the Animator created by
348     * the transition will have its own specified startDelay. If the delay of a
349     * Transition is set, that delay will override the Animator delay.
350     *
351     * @param startDelay The length of the delay, in milliseconds.
352     * @return This transition object.
353     * @attr ref android.R.styleable#Transition_startDelay
354     */
355    public Transition setStartDelay(long startDelay) {
356        mStartDelay = startDelay;
357        return this;
358    }
359
360    /**
361     * Returns the startDelay set on this transition. If no startDelay has been set,
362     * the returned value will be negative, indicating that resulting animators will
363     * retain their own startDelays.
364     *
365     * @return The startDelay set on this transition, in milliseconds, if one has
366     * been set, otherwise returns a negative number.
367     */
368    public long getStartDelay() {
369        return mStartDelay;
370    }
371
372    /**
373     * Sets the interpolator of this transition. By default, the interpolator
374     * is null, which means that the Animator created by the transition
375     * will have its own specified interpolator. If the interpolator of a
376     * Transition is set, that interpolator will override the Animator interpolator.
377     *
378     * @param interpolator The time interpolator used by the transition
379     * @return This transition object.
380     * @attr ref android.R.styleable#Transition_interpolator
381     */
382    public Transition setInterpolator(TimeInterpolator interpolator) {
383        mInterpolator = interpolator;
384        return this;
385    }
386
387    /**
388     * Returns the interpolator set on this transition. If no interpolator has been set,
389     * the returned value will be null, indicating that resulting animators will
390     * retain their own interpolators.
391     *
392     * @return The interpolator set on this transition, if one has been set, otherwise
393     * returns null.
394     */
395    public TimeInterpolator getInterpolator() {
396        return mInterpolator;
397    }
398
399    /**
400     * Returns the set of property names used stored in the {@link TransitionValues}
401     * object passed into {@link #captureStartValues(TransitionValues)} that
402     * this transition cares about for the purposes of canceling overlapping animations.
403     * When any transition is started on a given scene root, all transitions
404     * currently running on that same scene root are checked to see whether the
405     * properties on which they based their animations agree with the end values of
406     * the same properties in the new transition. If the end values are not equal,
407     * then the old animation is canceled since the new transition will start a new
408     * animation to these new values. If the values are equal, the old animation is
409     * allowed to continue and no new animation is started for that transition.
410     *
411     * <p>A transition does not need to override this method. However, not doing so
412     * will mean that the cancellation logic outlined in the previous paragraph
413     * will be skipped for that transition, possibly leading to artifacts as
414     * old transitions and new transitions on the same targets run in parallel,
415     * animating views toward potentially different end values.</p>
416     *
417     * @return An array of property names as described in the class documentation for
418     * {@link TransitionValues}. The default implementation returns <code>null</code>.
419     */
420    public String[] getTransitionProperties() {
421        return null;
422    }
423
424    /**
425     * This method creates an animation that will be run for this transition
426     * given the information in the startValues and endValues structures captured
427     * earlier for the start and end scenes. Subclasses of Transition should override
428     * this method. The method should only be called by the transition system; it is
429     * not intended to be called from external classes.
430     *
431     * <p>This method is called by the transition's parent (all the way up to the
432     * topmost Transition in the hierarchy) with the sceneRoot and start/end
433     * values that the transition may need to set up initial target values
434     * and construct an appropriate animation. For example, if an overall
435     * Transition is a {@link TransitionSet} consisting of several
436     * child transitions in sequence, then some of the child transitions may
437     * want to set initial values on target views prior to the overall
438     * Transition commencing, to put them in an appropriate state for the
439     * delay between that start and the child Transition start time. For
440     * example, a transition that fades an item in may wish to set the starting
441     * alpha value to 0, to avoid it blinking in prior to the transition
442     * actually starting the animation. This is necessary because the scene
443     * change that triggers the Transition will automatically set the end-scene
444     * on all target views, so a Transition that wants to animate from a
445     * different value should set that value prior to returning from this method.</p>
446     *
447     * <p>Additionally, a Transition can perform logic to determine whether
448     * the transition needs to run on the given target and start/end values.
449     * For example, a transition that resizes objects on the screen may wish
450     * to avoid running for views which are not present in either the start
451     * or end scenes.</p>
452     *
453     * <p>If there is an animator created and returned from this method, the
454     * transition mechanism will apply any applicable duration, startDelay,
455     * and interpolator to that animation and start it. A return value of
456     * <code>null</code> indicates that no animation should run. The default
457     * implementation returns null.</p>
458     *
459     * <p>The method is called for every applicable target object, which is
460     * stored in the {@link TransitionValues#view} field.</p>
461     *
462     *
463     * @param sceneRoot The root of the transition hierarchy.
464     * @param startValues The values for a specific target in the start scene.
465     * @param endValues The values for the target in the end scene.
466     * @return A Animator to be started at the appropriate time in the
467     * overall transition for this scene change. A null value means no animation
468     * should be run.
469     */
470    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
471            TransitionValues endValues) {
472        return null;
473    }
474
475    /**
476     * Sets the order in which Transition matches View start and end values.
477     * <p>
478     * The default behavior is to match first by {@link android.view.View#getTransitionName()},
479     * then by View instance, then by {@link android.view.View#getId()} and finally
480     * by its item ID if it is in a direct child of ListView. The caller can
481     * choose to have only some or all of the values of {@link #MATCH_INSTANCE},
482     * {@link #MATCH_NAME}, {@link #MATCH_ITEM_ID}, and {@link #MATCH_ID}. Only
483     * the match algorithms supplied will be used to determine whether Views are the
484     * the same in both the start and end Scene. Views that do not match will be considered
485     * as entering or leaving the Scene.
486     * </p>
487     * @param matches A list of zero or more of {@link #MATCH_INSTANCE},
488     *                {@link #MATCH_NAME}, {@link #MATCH_ITEM_ID}, and {@link #MATCH_ID}.
489     *                If none are provided, then the default match order will be set.
490     */
491    public void setMatchOrder(int... matches) {
492        if (matches == null || matches.length == 0) {
493            mMatchOrder = DEFAULT_MATCH_ORDER;
494        } else {
495            for (int i = 0; i < matches.length; i++) {
496                int match = matches[i];
497                if (!isValidMatch(match)) {
498                    throw new IllegalArgumentException("matches contains invalid value");
499                }
500                if (alreadyContains(matches, i)) {
501                    throw new IllegalArgumentException("matches contains a duplicate value");
502                }
503            }
504            mMatchOrder = matches.clone();
505        }
506    }
507
508    private static boolean isValidMatch(int match) {
509        return (match >= MATCH_FIRST && match <= MATCH_LAST);
510    }
511
512    private static boolean alreadyContains(int[] array, int searchIndex) {
513        int value = array[searchIndex];
514        for (int i = 0; i < searchIndex; i++) {
515            if (array[i] == value) {
516                return true;
517            }
518        }
519        return false;
520    }
521
522    /**
523     * Match start/end values by View instance. Adds matched values to mStartValuesList
524     * and mEndValuesList and removes them from unmatchedStart and unmatchedEnd.
525     */
526    private void matchInstances(ArrayMap<View, TransitionValues> unmatchedStart,
527            ArrayMap<View, TransitionValues> unmatchedEnd) {
528        for (int i = unmatchedStart.size() - 1; i >= 0; i--) {
529            View view = unmatchedStart.keyAt(i);
530            TransitionValues end = unmatchedEnd.remove(view);
531            if (end != null) {
532                TransitionValues start = unmatchedStart.removeAt(i);
533                mStartValuesList.add(start);
534                mEndValuesList.add(end);
535            }
536        }
537    }
538
539    /**
540     * Match start/end values by Adapter item ID. Adds matched values to mStartValuesList
541     * and mEndValuesList and removes them from unmatchedStart and unmatchedEnd, using
542     * startItemIds and endItemIds as a guide for which Views have unique item IDs.
543     */
544    private void matchItemIds(ArrayMap<View, TransitionValues> unmatchedStart,
545            ArrayMap<View, TransitionValues> unmatchedEnd,
546            LongSparseArray<View> startItemIds, LongSparseArray<View> endItemIds) {
547        int numStartIds = startItemIds.size();
548        for (int i = 0; i < numStartIds; i++) {
549            View startView = startItemIds.valueAt(i);
550            if (startView != null) {
551                View endView = endItemIds.get(startItemIds.keyAt(i));
552                if (endView != null) {
553                    TransitionValues startValues = unmatchedStart.get(startView);
554                    TransitionValues endValues = unmatchedEnd.get(endView);
555                    if (startValues != null && endValues != null) {
556                        mStartValuesList.add(startValues);
557                        mEndValuesList.add(endValues);
558                        unmatchedStart.remove(startView);
559                        unmatchedEnd.remove(endView);
560                    }
561                }
562            }
563        }
564    }
565
566    /**
567     * Match start/end values by Adapter view ID. Adds matched values to mStartValuesList
568     * and mEndValuesList and removes them from unmatchedStart and unmatchedEnd, using
569     * startIds and endIds as a guide for which Views have unique IDs.
570     */
571    private void matchIds(ArrayMap<View, TransitionValues> unmatchedStart,
572            ArrayMap<View, TransitionValues> unmatchedEnd,
573            SparseArray<View> startIds, SparseArray<View> endIds) {
574        int numStartIds = startIds.size();
575        for (int i = 0; i < numStartIds; i++) {
576            View startView = startIds.valueAt(i);
577            if (startView != null && isValidTarget(startView)) {
578                View endView = endIds.get(startIds.keyAt(i));
579                if (endView != null && isValidTarget(endView)) {
580                    TransitionValues startValues = unmatchedStart.get(startView);
581                    TransitionValues endValues = unmatchedEnd.get(endView);
582                    if (startValues != null && endValues != null) {
583                        mStartValuesList.add(startValues);
584                        mEndValuesList.add(endValues);
585                        unmatchedStart.remove(startView);
586                        unmatchedEnd.remove(endView);
587                    }
588                }
589            }
590        }
591    }
592
593    /**
594     * Match start/end values by Adapter transitionName. Adds matched values to mStartValuesList
595     * and mEndValuesList and removes them from unmatchedStart and unmatchedEnd, using
596     * startNames and endNames as a guide for which Views have unique transitionNames.
597     */
598    private void matchNames(ArrayMap<View, TransitionValues> unmatchedStart,
599            ArrayMap<View, TransitionValues> unmatchedEnd,
600            ArrayMap<String, View> startNames, ArrayMap<String, View> endNames) {
601        int numStartNames = startNames.size();
602        for (int i = 0; i < numStartNames; i++) {
603            View startView = startNames.valueAt(i);
604            if (startView != null && isValidTarget(startView)) {
605                View endView = endNames.get(startNames.keyAt(i));
606                if (endView != null && isValidTarget(endView)) {
607                    TransitionValues startValues = unmatchedStart.get(startView);
608                    TransitionValues endValues = unmatchedEnd.get(endView);
609                    if (startValues != null && endValues != null) {
610                        mStartValuesList.add(startValues);
611                        mEndValuesList.add(endValues);
612                        unmatchedStart.remove(startView);
613                        unmatchedEnd.remove(endView);
614                    }
615                }
616            }
617        }
618    }
619
620    /**
621     * Adds all values from unmatchedStart and unmatchedEnd to mStartValuesList and mEndValuesList,
622     * assuming that there is no match between values in the list.
623     */
624    private void addUnmatched(ArrayMap<View, TransitionValues> unmatchedStart,
625            ArrayMap<View, TransitionValues> unmatchedEnd) {
626        // Views that only exist in the start Scene
627        for (int i = 0; i < unmatchedStart.size(); i++) {
628            mStartValuesList.add(unmatchedStart.valueAt(i));
629            mEndValuesList.add(null);
630        }
631
632        // Views that only exist in the end Scene
633        for (int i = 0; i < unmatchedEnd.size(); i++) {
634            mEndValuesList.add(unmatchedEnd.valueAt(i));
635            mStartValuesList.add(null);
636        }
637    }
638
639    private void matchStartAndEnd(TransitionValuesMaps startValues,
640            TransitionValuesMaps endValues) {
641        ArrayMap<View, TransitionValues> unmatchedStart =
642                new ArrayMap<View, TransitionValues>(startValues.viewValues);
643        ArrayMap<View, TransitionValues> unmatchedEnd =
644                new ArrayMap<View, TransitionValues>(endValues.viewValues);
645
646        for (int i = 0; i < mMatchOrder.length; i++) {
647            switch (mMatchOrder[i]) {
648                case MATCH_INSTANCE:
649                    matchInstances(unmatchedStart, unmatchedEnd);
650                    break;
651                case MATCH_NAME:
652                    matchNames(unmatchedStart, unmatchedEnd,
653                            startValues.nameValues, endValues.nameValues);
654                    break;
655                case MATCH_ID:
656                    matchIds(unmatchedStart, unmatchedEnd,
657                            startValues.idValues, endValues.idValues);
658                    break;
659                case MATCH_ITEM_ID:
660                    matchItemIds(unmatchedStart, unmatchedEnd,
661                            startValues.itemIdValues, endValues.itemIdValues);
662                    break;
663            }
664        }
665        addUnmatched(unmatchedStart, unmatchedEnd);
666    }
667
668    /**
669     * This method, essentially a wrapper around all calls to createAnimator for all
670     * possible target views, is called with the entire set of start/end
671     * values. The implementation in Transition iterates through these lists
672     * and calls {@link #createAnimator(ViewGroup, TransitionValues, TransitionValues)}
673     * with each set of start/end values on this transition. The
674     * TransitionSet subclass overrides this method and delegates it to
675     * each of its children in succession.
676     *
677     * @hide
678     */
679    protected void createAnimators(ViewGroup sceneRoot, TransitionValuesMaps startValues,
680            TransitionValuesMaps endValues, ArrayList<TransitionValues> startValuesList,
681            ArrayList<TransitionValues> endValuesList) {
682        if (DBG) {
683            Log.d(LOG_TAG, "createAnimators() for " + this);
684        }
685        ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators();
686        long minStartDelay = Long.MAX_VALUE;
687        int minAnimator = mAnimators.size();
688        SparseLongArray startDelays = new SparseLongArray();
689        int startValuesListCount = startValuesList.size();
690        for (int i = 0; i < startValuesListCount; ++i) {
691            TransitionValues start = startValuesList.get(i);
692            TransitionValues end = endValuesList.get(i);
693            // Only bother trying to animate with valid values that differ between start/end
694            boolean isInvalidStart = start != null && !isValidTarget(start.view);
695            boolean isInvalidEnd = end != null && !isValidTarget(end.view);
696            boolean isChanged = start != end && (start == null || !start.equals(end));
697            if (isChanged && !isInvalidStart && !isInvalidEnd) {
698                if (DBG) {
699                    View view = (end != null) ? end.view : start.view;
700                    Log.d(LOG_TAG, "  differing start/end values for view " + view);
701                    if (start == null || end == null) {
702                        Log.d(LOG_TAG, "    " + ((start == null) ?
703                                "start null, end non-null" : "start non-null, end null"));
704                    } else {
705                        for (String key : start.values.keySet()) {
706                            Object startValue = start.values.get(key);
707                            Object endValue = end.values.get(key);
708                            if (startValue != endValue && !startValue.equals(endValue)) {
709                                Log.d(LOG_TAG, "    " + key + ": start(" + startValue +
710                                        "), end(" + endValue + ")");
711                            }
712                        }
713                    }
714                }
715                // TODO: what to do about targetIds and itemIds?
716                Animator animator = createAnimator(sceneRoot, start, end);
717                if (animator != null) {
718                    // Save animation info for future cancellation purposes
719                    View view = null;
720                    TransitionValues infoValues = null;
721                    if (end != null) {
722                        view = end.view;
723                        String[] properties = getTransitionProperties();
724                        if (view != null && properties != null && properties.length > 0) {
725                            infoValues = new TransitionValues();
726                            infoValues.view = view;
727                            TransitionValues newValues = endValues.viewValues.get(view);
728                            if (newValues != null) {
729                                for (int j = 0; j < properties.length; ++j) {
730                                    infoValues.values.put(properties[j],
731                                            newValues.values.get(properties[j]));
732                                }
733                            }
734                            int numExistingAnims = runningAnimators.size();
735                            for (int j = 0; j < numExistingAnims; ++j) {
736                                Animator anim = runningAnimators.keyAt(j);
737                                AnimationInfo info = runningAnimators.get(anim);
738                                if (info.values != null && info.view == view &&
739                                        ((info.name == null && getName() == null) ||
740                                                info.name.equals(getName()))) {
741                                    if (info.values.equals(infoValues)) {
742                                        // Favor the old animator
743                                        animator = null;
744                                        break;
745                                    }
746                                }
747                            }
748                        }
749                    } else {
750                        view = (start != null) ? start.view : null;
751                    }
752                    if (animator != null) {
753                        if (mPropagation != null) {
754                            long delay = mPropagation
755                                    .getStartDelay(sceneRoot, this, start, end);
756                            startDelays.put(mAnimators.size(), delay);
757                            minStartDelay = Math.min(delay, minStartDelay);
758                        }
759                        AnimationInfo info = new AnimationInfo(view, getName(), this,
760                                sceneRoot.getWindowId(), infoValues);
761                        runningAnimators.put(animator, info);
762                        mAnimators.add(animator);
763                    }
764                }
765            }
766        }
767        if (minStartDelay != 0) {
768            for (int i = 0; i < startDelays.size(); i++) {
769                int index = startDelays.keyAt(i);
770                Animator animator = mAnimators.get(index);
771                long delay = startDelays.valueAt(i) - minStartDelay + animator.getStartDelay();
772                animator.setStartDelay(delay);
773            }
774        }
775    }
776
777    /**
778     * Internal utility method for checking whether a given view/id
779     * is valid for this transition, where "valid" means that either
780     * the Transition has no target/targetId list (the default, in which
781     * cause the transition should act on all views in the hiearchy), or
782     * the given view is in the target list or the view id is in the
783     * targetId list. If the target parameter is null, then the target list
784     * is not checked (this is in the case of ListView items, where the
785     * views are ignored and only the ids are used).
786     */
787    boolean isValidTarget(View target) {
788        int targetId = target.getId();
789        if (mTargetIdExcludes != null && mTargetIdExcludes.contains(targetId)) {
790            return false;
791        }
792        if (mTargetExcludes != null && mTargetExcludes.contains(target)) {
793            return false;
794        }
795        if (mTargetTypeExcludes != null && target != null) {
796            int numTypes = mTargetTypeExcludes.size();
797            for (int i = 0; i < numTypes; ++i) {
798                Class type = mTargetTypeExcludes.get(i);
799                if (type.isInstance(target)) {
800                    return false;
801                }
802            }
803        }
804        if (mTargetNameExcludes != null && target != null && target.getTransitionName() != null) {
805            if (mTargetNameExcludes.contains(target.getTransitionName())) {
806                return false;
807            }
808        }
809        if (mTargetIds.size() == 0 && mTargets.size() == 0 &&
810                (mTargetTypes == null || mTargetTypes.isEmpty() &&
811                (mTargetNames == null || mTargetNames.isEmpty()))) {
812            return true;
813        }
814        if (mTargetIds.contains(targetId) || mTargets.contains(target)) {
815            return true;
816        }
817        if (mTargetNames != null && mTargetNames.contains(target.getTransitionName())) {
818            return true;
819        }
820        if (mTargetTypes != null) {
821            for (int i = 0; i < mTargetTypes.size(); ++i) {
822                if (mTargetTypes.get(i).isInstance(target)) {
823                    return true;
824                }
825            }
826        }
827        return false;
828    }
829
830    private static ArrayMap<Animator, AnimationInfo> getRunningAnimators() {
831        ArrayMap<Animator, AnimationInfo> runningAnimators = sRunningAnimators.get();
832        if (runningAnimators == null) {
833            runningAnimators = new ArrayMap<Animator, AnimationInfo>();
834            sRunningAnimators.set(runningAnimators);
835        }
836        return runningAnimators;
837    }
838
839    /**
840     * This is called internally once all animations have been set up by the
841     * transition hierarchy.
842     *
843     * @hide
844     */
845    protected void runAnimators() {
846        if (DBG) {
847            Log.d(LOG_TAG, "runAnimators() on " + this);
848        }
849        start();
850        ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators();
851        // Now start every Animator that was previously created for this transition
852        for (Animator anim : mAnimators) {
853            if (DBG) {
854                Log.d(LOG_TAG, "  anim: " + anim);
855            }
856            if (runningAnimators.containsKey(anim)) {
857                start();
858                runAnimator(anim, runningAnimators);
859            }
860        }
861        mAnimators.clear();
862        end();
863    }
864
865    private void runAnimator(Animator animator,
866            final ArrayMap<Animator, AnimationInfo> runningAnimators) {
867        if (animator != null) {
868            // TODO: could be a single listener instance for all of them since it uses the param
869            animator.addListener(new AnimatorListenerAdapter() {
870                @Override
871                public void onAnimationStart(Animator animation) {
872                    mCurrentAnimators.add(animation);
873                }
874                @Override
875                public void onAnimationEnd(Animator animation) {
876                    runningAnimators.remove(animation);
877                    mCurrentAnimators.remove(animation);
878                }
879            });
880            animate(animator);
881        }
882    }
883
884    /**
885     * Captures the values in the start scene for the properties that this
886     * transition monitors. These values are then passed as the startValues
887     * structure in a later call to
888     * {@link #createAnimator(ViewGroup, TransitionValues, TransitionValues)}.
889     * The main concern for an implementation is what the
890     * properties are that the transition cares about and what the values are
891     * for all of those properties. The start and end values will be compared
892     * later during the
893     * {@link #createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues)}
894     * method to determine what, if any, animations, should be run.
895     *
896     * <p>Subclasses must implement this method. The method should only be called by the
897     * transition system; it is not intended to be called from external classes.</p>
898     *
899     * @param transitionValues The holder for any values that the Transition
900     * wishes to store. Values are stored in the <code>values</code> field
901     * of this TransitionValues object and are keyed from
902     * a String value. For example, to store a view's rotation value,
903     * a transition might call
904     * <code>transitionValues.values.put("appname:transitionname:rotation",
905     * view.getRotation())</code>. The target view will already be stored in
906     * the transitionValues structure when this method is called.
907     *
908     * @see #captureEndValues(TransitionValues)
909     * @see #createAnimator(ViewGroup, TransitionValues, TransitionValues)
910     */
911    public abstract void captureStartValues(TransitionValues transitionValues);
912
913    /**
914     * Captures the values in the end scene for the properties that this
915     * transition monitors. These values are then passed as the endValues
916     * structure in a later call to
917     * {@link #createAnimator(ViewGroup, TransitionValues, TransitionValues)}.
918     * The main concern for an implementation is what the
919     * properties are that the transition cares about and what the values are
920     * for all of those properties. The start and end values will be compared
921     * later during the
922     * {@link #createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues)}
923     * method to determine what, if any, animations, should be run.
924     *
925     * <p>Subclasses must implement this method. The method should only be called by the
926     * transition system; it is not intended to be called from external classes.</p>
927     *
928     * @param transitionValues The holder for any values that the Transition
929     * wishes to store. Values are stored in the <code>values</code> field
930     * of this TransitionValues object and are keyed from
931     * a String value. For example, to store a view's rotation value,
932     * a transition might call
933     * <code>transitionValues.values.put("appname:transitionname:rotation",
934     * view.getRotation())</code>. The target view will already be stored in
935     * the transitionValues structure when this method is called.
936     *
937     * @see #captureStartValues(TransitionValues)
938     * @see #createAnimator(ViewGroup, TransitionValues, TransitionValues)
939     */
940    public abstract void captureEndValues(TransitionValues transitionValues);
941
942    /**
943     * Adds the id of a target view that this Transition is interested in
944     * animating. By default, there are no targetIds, and a Transition will
945     * listen for changes on every view in the hierarchy below the sceneRoot
946     * of the Scene being transitioned into. Setting targetIds constrains
947     * the Transition to only listen for, and act on, views with these IDs.
948     * Views with different IDs, or no IDs whatsoever, will be ignored.
949     *
950     * <p>Note that using ids to specify targets implies that ids should be unique
951     * within the view hierarchy underneat the scene root.</p>
952     *
953     * @see View#getId()
954     * @param targetId The id of a target view, must be a positive number.
955     * @return The Transition to which the targetId is added.
956     * Returning the same object makes it easier to chain calls during
957     * construction, such as
958     * <code>transitionSet.addTransitions(new Fade()).addTarget(someId);</code>
959     */
960    public Transition addTarget(int targetId) {
961        if (targetId > 0) {
962            mTargetIds.add(targetId);
963        }
964        return this;
965    }
966
967    /**
968     * Adds the transitionName of a target view that this Transition is interested in
969     * animating. By default, there are no targetNames, and a Transition will
970     * listen for changes on every view in the hierarchy below the sceneRoot
971     * of the Scene being transitioned into. Setting targetNames constrains
972     * the Transition to only listen for, and act on, views with these transitionNames.
973     * Views with different transitionNames, or no transitionName whatsoever, will be ignored.
974     *
975     * <p>Note that transitionNames should be unique within the view hierarchy.</p>
976     *
977     * @see android.view.View#getTransitionName()
978     * @param targetName The transitionName of a target view, must be non-null.
979     * @return The Transition to which the target transitionName is added.
980     * Returning the same object makes it easier to chain calls during
981     * construction, such as
982     * <code>transitionSet.addTransitions(new Fade()).addTarget(someName);</code>
983     */
984    public Transition addTarget(String targetName) {
985        if (targetName != null) {
986            if (mTargetNames == null) {
987                mTargetNames = new ArrayList<String>();
988            }
989            mTargetNames.add(targetName);
990        }
991        return this;
992    }
993
994    /**
995     * Adds the Class of a target view that this Transition is interested in
996     * animating. By default, there are no targetTypes, and a Transition will
997     * listen for changes on every view in the hierarchy below the sceneRoot
998     * of the Scene being transitioned into. Setting targetTypes constrains
999     * the Transition to only listen for, and act on, views with these classes.
1000     * Views with different classes will be ignored.
1001     *
1002     * <p>Note that any View that can be cast to targetType will be included, so
1003     * if targetType is <code>View.class</code>, all Views will be included.</p>
1004     *
1005     * @see #addTarget(int)
1006     * @see #addTarget(android.view.View)
1007     * @see #excludeTarget(Class, boolean)
1008     * @see #excludeChildren(Class, boolean)
1009     *
1010     * @param targetType The type to include when running this transition.
1011     * @return The Transition to which the target class was added.
1012     * Returning the same object makes it easier to chain calls during
1013     * construction, such as
1014     * <code>transitionSet.addTransitions(new Fade()).addTarget(ImageView.class);</code>
1015     */
1016    public Transition addTarget(Class targetType) {
1017        if (targetType != null) {
1018            if (mTargetTypes == null) {
1019                mTargetTypes = new ArrayList<Class>();
1020            }
1021            mTargetTypes.add(targetType);
1022        }
1023        return this;
1024    }
1025
1026    /**
1027     * Removes the given targetId from the list of ids that this Transition
1028     * is interested in animating.
1029     *
1030     * @param targetId The id of a target view, must be a positive number.
1031     * @return The Transition from which the targetId is removed.
1032     * Returning the same object makes it easier to chain calls during
1033     * construction, such as
1034     * <code>transitionSet.addTransitions(new Fade()).removeTargetId(someId);</code>
1035     */
1036    public Transition removeTarget(int targetId) {
1037        if (targetId > 0) {
1038            mTargetIds.remove(targetId);
1039        }
1040        return this;
1041    }
1042
1043    /**
1044     * Removes the given targetName from the list of transitionNames that this Transition
1045     * is interested in animating.
1046     *
1047     * @param targetName The transitionName of a target view, must not be null.
1048     * @return The Transition from which the targetName is removed.
1049     * Returning the same object makes it easier to chain calls during
1050     * construction, such as
1051     * <code>transitionSet.addTransitions(new Fade()).removeTargetName(someName);</code>
1052     */
1053    public Transition removeTarget(String targetName) {
1054        if (targetName != null && mTargetNames != null) {
1055            mTargetNames.remove(targetName);
1056        }
1057        return this;
1058    }
1059
1060    /**
1061     * Whether to add the given id to the list of target ids to exclude from this
1062     * transition. The <code>exclude</code> parameter specifies whether the target
1063     * should be added to or removed from the excluded list.
1064     *
1065     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1066     * a view hierarchy while skipping target views that should not be part of
1067     * the transition. For example, you may want to avoid animating children
1068     * of a specific ListView or Spinner. Views can be excluded either by their
1069     * id, or by their instance reference, or by the Class of that view
1070     * (eg, {@link Spinner}).</p>
1071     *
1072     * @see #excludeChildren(int, boolean)
1073     * @see #excludeTarget(View, boolean)
1074     * @see #excludeTarget(Class, boolean)
1075     *
1076     * @param targetId The id of a target to ignore when running this transition.
1077     * @param exclude Whether to add the target to or remove the target from the
1078     * current list of excluded targets.
1079     * @return This transition object.
1080     */
1081    public Transition excludeTarget(int targetId, boolean exclude) {
1082        if (targetId >= 0) {
1083            mTargetIdExcludes = excludeObject(mTargetIdExcludes, targetId, exclude);
1084        }
1085        return this;
1086    }
1087
1088    /**
1089     * Whether to add the given transitionName to the list of target transitionNames to exclude
1090     * from this transition. The <code>exclude</code> parameter specifies whether the target
1091     * should be added to or removed from the excluded list.
1092     *
1093     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1094     * a view hierarchy while skipping target views that should not be part of
1095     * the transition. For example, you may want to avoid animating children
1096     * of a specific ListView or Spinner. Views can be excluded by their
1097     * id, their instance reference, their transitionName, or by the Class of that view
1098     * (eg, {@link Spinner}).</p>
1099     *
1100     * @see #excludeTarget(View, boolean)
1101     * @see #excludeTarget(int, boolean)
1102     * @see #excludeTarget(Class, boolean)
1103     *
1104     * @param targetName The name of a target to ignore when running this transition.
1105     * @param exclude Whether to add the target to or remove the target from the
1106     * current list of excluded targets.
1107     * @return This transition object.
1108     */
1109    public Transition excludeTarget(String targetName, boolean exclude) {
1110        mTargetNameExcludes = excludeObject(mTargetNameExcludes, targetName, exclude);
1111        return this;
1112    }
1113
1114    /**
1115     * Whether to add the children of the given id to the list of targets to exclude
1116     * from this transition. The <code>exclude</code> parameter specifies whether
1117     * the children of the target should be added to or removed from the excluded list.
1118     * Excluding children in this way provides a simple mechanism for excluding all
1119     * children of specific targets, rather than individually excluding each
1120     * child individually.
1121     *
1122     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1123     * a view hierarchy while skipping target views that should not be part of
1124     * the transition. For example, you may want to avoid animating children
1125     * of a specific ListView or Spinner. Views can be excluded either by their
1126     * id, or by their instance reference, or by the Class of that view
1127     * (eg, {@link Spinner}).</p>
1128     *
1129     * @see #excludeTarget(int, boolean)
1130     * @see #excludeChildren(View, boolean)
1131     * @see #excludeChildren(Class, boolean)
1132     *
1133     * @param targetId The id of a target whose children should be ignored when running
1134     * this transition.
1135     * @param exclude Whether to add the target to or remove the target from the
1136     * current list of excluded-child targets.
1137     * @return This transition object.
1138     */
1139    public Transition excludeChildren(int targetId, boolean exclude) {
1140        if (targetId >= 0) {
1141            mTargetIdChildExcludes = excludeObject(mTargetIdChildExcludes, targetId, exclude);
1142        }
1143        return this;
1144    }
1145
1146    /**
1147     * Whether to add the given target to the list of targets to exclude from this
1148     * transition. The <code>exclude</code> parameter specifies whether the target
1149     * should be added to or removed from the excluded list.
1150     *
1151     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1152     * a view hierarchy while skipping target views that should not be part of
1153     * the transition. For example, you may want to avoid animating children
1154     * of a specific ListView or Spinner. Views can be excluded either by their
1155     * id, or by their instance reference, or by the Class of that view
1156     * (eg, {@link Spinner}).</p>
1157     *
1158     * @see #excludeChildren(View, boolean)
1159     * @see #excludeTarget(int, boolean)
1160     * @see #excludeTarget(Class, boolean)
1161     *
1162     * @param target The target to ignore when running this transition.
1163     * @param exclude Whether to add the target to or remove the target from the
1164     * current list of excluded targets.
1165     * @return This transition object.
1166     */
1167    public Transition excludeTarget(View target, boolean exclude) {
1168        mTargetExcludes = excludeObject(mTargetExcludes, target, exclude);
1169        return this;
1170    }
1171
1172    /**
1173     * Whether to add the children of given target to the list of target children
1174     * to exclude from this transition. The <code>exclude</code> parameter specifies
1175     * whether the target should be added to or removed from the excluded list.
1176     *
1177     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1178     * a view hierarchy while skipping target views that should not be part of
1179     * the transition. For example, you may want to avoid animating children
1180     * of a specific ListView or Spinner. Views can be excluded either by their
1181     * id, or by their instance reference, or by the Class of that view
1182     * (eg, {@link Spinner}).</p>
1183     *
1184     * @see #excludeTarget(View, boolean)
1185     * @see #excludeChildren(int, boolean)
1186     * @see #excludeChildren(Class, boolean)
1187     *
1188     * @param target The target to ignore when running this transition.
1189     * @param exclude Whether to add the target to or remove the target from the
1190     * current list of excluded targets.
1191     * @return This transition object.
1192     */
1193    public Transition excludeChildren(View target, boolean exclude) {
1194        mTargetChildExcludes = excludeObject(mTargetChildExcludes, target, exclude);
1195        return this;
1196    }
1197
1198    /**
1199     * Utility method to manage the boilerplate code that is the same whether we
1200     * are excluding targets or their children.
1201     */
1202    private static <T> ArrayList<T> excludeObject(ArrayList<T> list, T target, boolean exclude) {
1203        if (target != null) {
1204            if (exclude) {
1205                list = ArrayListManager.add(list, target);
1206            } else {
1207                list = ArrayListManager.remove(list, target);
1208            }
1209        }
1210        return list;
1211    }
1212
1213    /**
1214     * Whether to add the given type to the list of types to exclude from this
1215     * transition. The <code>exclude</code> parameter specifies whether the target
1216     * type should be added to or removed from the excluded list.
1217     *
1218     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1219     * a view hierarchy while skipping target views that should not be part of
1220     * the transition. For example, you may want to avoid animating children
1221     * of a specific ListView or Spinner. Views can be excluded either by their
1222     * id, or by their instance reference, or by the Class of that view
1223     * (eg, {@link Spinner}).</p>
1224     *
1225     * @see #excludeChildren(Class, boolean)
1226     * @see #excludeTarget(int, boolean)
1227     * @see #excludeTarget(View, boolean)
1228     *
1229     * @param type The type to ignore when running this transition.
1230     * @param exclude Whether to add the target type to or remove it from the
1231     * current list of excluded target types.
1232     * @return This transition object.
1233     */
1234    public Transition excludeTarget(Class type, boolean exclude) {
1235        mTargetTypeExcludes = excludeObject(mTargetTypeExcludes, type, exclude);
1236        return this;
1237    }
1238
1239    /**
1240     * Whether to add the given type to the list of types whose children should
1241     * be excluded from this transition. The <code>exclude</code> parameter
1242     * specifies whether the target type should be added to or removed from
1243     * the excluded list.
1244     *
1245     * <p>Excluding targets is a general mechanism for allowing transitions to run on
1246     * a view hierarchy while skipping target views that should not be part of
1247     * the transition. For example, you may want to avoid animating children
1248     * of a specific ListView or Spinner. Views can be excluded either by their
1249     * id, or by their instance reference, or by the Class of that view
1250     * (eg, {@link Spinner}).</p>
1251     *
1252     * @see #excludeTarget(Class, boolean)
1253     * @see #excludeChildren(int, boolean)
1254     * @see #excludeChildren(View, boolean)
1255     *
1256     * @param type The type to ignore when running this transition.
1257     * @param exclude Whether to add the target type to or remove it from the
1258     * current list of excluded target types.
1259     * @return This transition object.
1260     */
1261    public Transition excludeChildren(Class type, boolean exclude) {
1262        mTargetTypeChildExcludes = excludeObject(mTargetTypeChildExcludes, type, exclude);
1263        return this;
1264    }
1265
1266    /**
1267     * Sets the target view instances that this Transition is interested in
1268     * animating. By default, there are no targets, and a Transition will
1269     * listen for changes on every view in the hierarchy below the sceneRoot
1270     * of the Scene being transitioned into. Setting targets constrains
1271     * the Transition to only listen for, and act on, these views.
1272     * All other views will be ignored.
1273     *
1274     * <p>The target list is like the {@link #addTarget(int) targetId}
1275     * list except this list specifies the actual View instances, not the ids
1276     * of the views. This is an important distinction when scene changes involve
1277     * view hierarchies which have been inflated separately; different views may
1278     * share the same id but not actually be the same instance. If the transition
1279     * should treat those views as the same, then {@link #addTarget(int)} should be used
1280     * instead of {@link #addTarget(View)}. If, on the other hand, scene changes involve
1281     * changes all within the same view hierarchy, among views which do not
1282     * necessarily have ids set on them, then the target list of views may be more
1283     * convenient.</p>
1284     *
1285     * @see #addTarget(int)
1286     * @param target A View on which the Transition will act, must be non-null.
1287     * @return The Transition to which the target is added.
1288     * Returning the same object makes it easier to chain calls during
1289     * construction, such as
1290     * <code>transitionSet.addTransitions(new Fade()).addTarget(someView);</code>
1291     */
1292    public Transition addTarget(View target) {
1293        mTargets.add(target);
1294        return this;
1295    }
1296
1297    /**
1298     * Removes the given target from the list of targets that this Transition
1299     * is interested in animating.
1300     *
1301     * @param target The target view, must be non-null.
1302     * @return Transition The Transition from which the target is removed.
1303     * Returning the same object makes it easier to chain calls during
1304     * construction, such as
1305     * <code>transitionSet.addTransitions(new Fade()).removeTarget(someView);</code>
1306     */
1307    public Transition removeTarget(View target) {
1308        if (target != null) {
1309            mTargets.remove(target);
1310        }
1311        return this;
1312    }
1313
1314    /**
1315     * Removes the given target from the list of targets that this Transition
1316     * is interested in animating.
1317     *
1318     * @param target The type of the target view, must be non-null.
1319     * @return Transition The Transition from which the target is removed.
1320     * Returning the same object makes it easier to chain calls during
1321     * construction, such as
1322     * <code>transitionSet.addTransitions(new Fade()).removeTarget(someType);</code>
1323     */
1324    public Transition removeTarget(Class target) {
1325        if (target != null) {
1326            mTargetTypes.remove(target);
1327        }
1328        return this;
1329    }
1330
1331    /**
1332     * Returns the list of target IDs that this transition limits itself to
1333     * tracking and animating. If the list is null or empty for
1334     * {@link #getTargetIds()}, {@link #getTargets()}, {@link #getTargetNames()}, and
1335     * {@link #getTargetTypes()} then this transition is
1336     * not limited to specific views, and will handle changes to any views
1337     * in the hierarchy of a scene change.
1338     *
1339     * @return the list of target IDs
1340     */
1341    public List<Integer> getTargetIds() {
1342        return mTargetIds;
1343    }
1344
1345    /**
1346     * Returns the list of target views that this transition limits itself to
1347     * tracking and animating. If the list is null or empty for
1348     * {@link #getTargetIds()}, {@link #getTargets()}, {@link #getTargetNames()}, and
1349     * {@link #getTargetTypes()} then this transition is
1350     * not limited to specific views, and will handle changes to any views
1351     * in the hierarchy of a scene change.
1352     *
1353     * @return the list of target views
1354     */
1355    public List<View> getTargets() {
1356        return mTargets;
1357    }
1358
1359    /**
1360     * Returns the list of target transitionNames that this transition limits itself to
1361     * tracking and animating. If the list is null or empty for
1362     * {@link #getTargetIds()}, {@link #getTargets()}, {@link #getTargetNames()}, and
1363     * {@link #getTargetTypes()} then this transition is
1364     * not limited to specific views, and will handle changes to any views
1365     * in the hierarchy of a scene change.
1366     *
1367     * @return the list of target transitionNames
1368     */
1369    public List<String> getTargetNames() {
1370        return mTargetNames;
1371    }
1372
1373    /**
1374     * To be removed before L release.
1375     * @hide
1376     */
1377    public List<String> getTargetViewNames() {
1378        return mTargetNames;
1379    }
1380
1381    /**
1382     * Returns the list of target transitionNames that this transition limits itself to
1383     * tracking and animating. If the list is null or empty for
1384     * {@link #getTargetIds()}, {@link #getTargets()}, {@link #getTargetNames()}, and
1385     * {@link #getTargetTypes()} then this transition is
1386     * not limited to specific views, and will handle changes to any views
1387     * in the hierarchy of a scene change.
1388     *
1389     * @return the list of target Types
1390     */
1391    public List<Class> getTargetTypes() {
1392        return mTargetTypes;
1393    }
1394
1395    /**
1396     * Recursive method that captures values for the given view and the
1397     * hierarchy underneath it.
1398     * @param sceneRoot The root of the view hierarchy being captured
1399     * @param start true if this capture is happening before the scene change,
1400     * false otherwise
1401     */
1402    void captureValues(ViewGroup sceneRoot, boolean start) {
1403        clearValues(start);
1404        if ((mTargetIds.size() > 0 || mTargets.size() > 0)
1405                && (mTargetNames == null || mTargetNames.isEmpty())
1406                && (mTargetTypes == null || mTargetTypes.isEmpty())) {
1407            for (int i = 0; i < mTargetIds.size(); ++i) {
1408                int id = mTargetIds.get(i);
1409                View view = sceneRoot.findViewById(id);
1410                if (view != null) {
1411                    TransitionValues values = new TransitionValues();
1412                    values.view = view;
1413                    if (start) {
1414                        captureStartValues(values);
1415                    } else {
1416                        captureEndValues(values);
1417                    }
1418                    capturePropagationValues(values);
1419                    if (start) {
1420                        addViewValues(mStartValues, view, values, true);
1421                    } else {
1422                        addViewValues(mEndValues, view, values, true);
1423                    }
1424                }
1425            }
1426            for (int i = 0; i < mTargets.size(); ++i) {
1427                View view = mTargets.get(i);
1428                TransitionValues values = new TransitionValues();
1429                values.view = view;
1430                if (start) {
1431                    captureStartValues(values);
1432                } else {
1433                    captureEndValues(values);
1434                }
1435                capturePropagationValues(values);
1436                if (start) {
1437                    mStartValues.viewValues.put(view, values);
1438                } else {
1439                    mEndValues.viewValues.put(view, values);
1440                }
1441            }
1442        } else {
1443            captureHierarchy(sceneRoot, start);
1444        }
1445        if (!start && mNameOverrides != null) {
1446            int numOverrides = mNameOverrides.size();
1447            ArrayList<View> overriddenViews = new ArrayList<View>(numOverrides);
1448            for (int i = 0; i < numOverrides; i++) {
1449                String fromName = mNameOverrides.keyAt(i);
1450                overriddenViews.add(mStartValues.nameValues.remove(fromName));
1451            }
1452            for (int i = 0; i < numOverrides; i++) {
1453                View view = overriddenViews.get(i);
1454                if (view != null) {
1455                    String toName = mNameOverrides.valueAt(i);
1456                    mStartValues.nameValues.put(toName, view);
1457                }
1458            }
1459        }
1460    }
1461
1462    static void addViewValues(TransitionValuesMaps transitionValuesMaps,
1463            View view, TransitionValues transitionValues, boolean setTransientState) {
1464        transitionValuesMaps.viewValues.put(view, transitionValues);
1465        int id = view.getId();
1466        if (id >= 0) {
1467            if (transitionValuesMaps.idValues.indexOfKey(id) >= 0) {
1468                // Duplicate IDs cannot match by ID.
1469                transitionValuesMaps.idValues.put(id, null);
1470            } else {
1471                transitionValuesMaps.idValues.put(id, view);
1472            }
1473        }
1474        String name = view.getTransitionName();
1475        if (name != null) {
1476            if (transitionValuesMaps.nameValues.containsKey(name)) {
1477                // Duplicate transitionNames: cannot match by transitionName.
1478                transitionValuesMaps.nameValues.put(name, null);
1479            } else {
1480                transitionValuesMaps.nameValues.put(name, view);
1481            }
1482        }
1483        if (view.getParent() instanceof ListView) {
1484            ListView listview = (ListView) view.getParent();
1485            if (listview.getAdapter().hasStableIds()) {
1486                int position = listview.getPositionForView(view);
1487                long itemId = listview.getItemIdAtPosition(position);
1488                if (transitionValuesMaps.itemIdValues.indexOfKey(itemId) >= 0) {
1489                    // Duplicate item IDs: cannot match by item ID.
1490                    View alreadyMatched = transitionValuesMaps.itemIdValues.get(itemId);
1491                    if (alreadyMatched != null) {
1492                        if (setTransientState) {
1493                            alreadyMatched.setHasTransientState(false);
1494                        }
1495                        transitionValuesMaps.itemIdValues.put(itemId, null);
1496                    }
1497                } else {
1498                    if (setTransientState) {
1499                        view.setHasTransientState(true);
1500                    }
1501                    transitionValuesMaps.itemIdValues.put(itemId, view);
1502                }
1503            }
1504        }
1505    }
1506
1507    /**
1508     * Clear valuesMaps for specified start/end state
1509     *
1510     * @param start true if the start values should be cleared, false otherwise
1511     */
1512    void clearValues(boolean start) {
1513        if (start) {
1514            mStartValues.viewValues.clear();
1515            mStartValues.idValues.clear();
1516            mStartValues.itemIdValues.clear();
1517            mStartValues.nameValues.clear();
1518            mStartValuesList = null;
1519        } else {
1520            mEndValues.viewValues.clear();
1521            mEndValues.idValues.clear();
1522            mEndValues.itemIdValues.clear();
1523            mEndValues.nameValues.clear();
1524            mEndValuesList = null;
1525        }
1526    }
1527
1528    /**
1529     * Recursive method which captures values for an entire view hierarchy,
1530     * starting at some root view. Transitions without targetIDs will use this
1531     * method to capture values for all possible views.
1532     *
1533     * @param view The view for which to capture values. Children of this View
1534     * will also be captured, recursively down to the leaf nodes.
1535     * @param start true if values are being captured in the start scene, false
1536     * otherwise.
1537     */
1538    private void captureHierarchy(View view, boolean start) {
1539        if (view == null) {
1540            return;
1541        }
1542        int id = view.getId();
1543        if (mTargetIdExcludes != null && mTargetIdExcludes.contains(id)) {
1544            return;
1545        }
1546        if (mTargetExcludes != null && mTargetExcludes.contains(view)) {
1547            return;
1548        }
1549        if (mTargetTypeExcludes != null && view != null) {
1550            int numTypes = mTargetTypeExcludes.size();
1551            for (int i = 0; i < numTypes; ++i) {
1552                if (mTargetTypeExcludes.get(i).isInstance(view)) {
1553                    return;
1554                }
1555            }
1556        }
1557        if (view.getParent() instanceof ViewGroup) {
1558            TransitionValues values = new TransitionValues();
1559            values.view = view;
1560            if (start) {
1561                captureStartValues(values);
1562            } else {
1563                captureEndValues(values);
1564            }
1565            capturePropagationValues(values);
1566            if (start) {
1567                addViewValues(mStartValues, view, values, true);
1568            } else {
1569                addViewValues(mEndValues, view, values, true);
1570            }
1571        }
1572        if (view instanceof ViewGroup) {
1573            // Don't traverse child hierarchy if there are any child-excludes on this view
1574            if (mTargetIdChildExcludes != null && mTargetIdChildExcludes.contains(id)) {
1575                return;
1576            }
1577            if (mTargetChildExcludes != null && mTargetChildExcludes.contains(view)) {
1578                return;
1579            }
1580            if (mTargetTypeChildExcludes != null) {
1581                int numTypes = mTargetTypeChildExcludes.size();
1582                for (int i = 0; i < numTypes; ++i) {
1583                    if (mTargetTypeChildExcludes.get(i).isInstance(view)) {
1584                        return;
1585                    }
1586                }
1587            }
1588            ViewGroup parent = (ViewGroup) view;
1589            for (int i = 0; i < parent.getChildCount(); ++i) {
1590                captureHierarchy(parent.getChildAt(i), start);
1591            }
1592        }
1593    }
1594
1595    /**
1596     * This method can be called by transitions to get the TransitionValues for
1597     * any particular view during the transition-playing process. This might be
1598     * necessary, for example, to query the before/after state of related views
1599     * for a given transition.
1600     */
1601    public TransitionValues getTransitionValues(View view, boolean start) {
1602        if (mParent != null) {
1603            return mParent.getTransitionValues(view, start);
1604        }
1605        TransitionValuesMaps valuesMaps = start ? mStartValues : mEndValues;
1606        return valuesMaps.viewValues.get(view);
1607    }
1608
1609    /**
1610     * Find the matched start or end value for a given View. This is only valid
1611     * after playTransition starts. For example, it will be valid in
1612     * {@link #createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues)}, but not
1613     * in {@link #captureStartValues(TransitionValues)}.
1614     *
1615     * @param view The view to find the match for.
1616     * @param viewInStart Is View from the start values or end values.
1617     * @return The matching TransitionValues for view in either start or end values, depending
1618     * on viewInStart or null if there is no match for the given view.
1619     */
1620    TransitionValues getMatchedTransitionValues(View view, boolean viewInStart) {
1621        if (mParent != null) {
1622            return mParent.getMatchedTransitionValues(view, viewInStart);
1623        }
1624        ArrayList<TransitionValues> lookIn = viewInStart ? mStartValuesList : mEndValuesList;
1625        if (lookIn == null) {
1626            return null;
1627        }
1628        int count = lookIn.size();
1629        int index = -1;
1630        for (int i = 0; i < count; i++) {
1631            TransitionValues values = lookIn.get(i);
1632            if (values == null) {
1633                return null;
1634            }
1635            if (values.view == view) {
1636                index = i;
1637                break;
1638            }
1639        }
1640        TransitionValues values = null;
1641        if (index >= 0) {
1642            ArrayList<TransitionValues> matchIn = viewInStart ? mEndValuesList : mStartValuesList;
1643            values = matchIn.get(index);
1644        }
1645        return values;
1646    }
1647
1648    /**
1649     * Pauses this transition, sending out calls to {@link
1650     * TransitionListener#onTransitionPause(Transition)} to all listeners
1651     * and pausing all running animators started by this transition.
1652     *
1653     * @hide
1654     */
1655    public void pause(View sceneRoot) {
1656        if (!mEnded) {
1657            ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators();
1658            int numOldAnims = runningAnimators.size();
1659            if (sceneRoot != null) {
1660                WindowId windowId = sceneRoot.getWindowId();
1661                for (int i = numOldAnims - 1; i >= 0; i--) {
1662                    AnimationInfo info = runningAnimators.valueAt(i);
1663                    if (info.view != null && windowId != null && windowId.equals(info.windowId)) {
1664                        Animator anim = runningAnimators.keyAt(i);
1665                        anim.pause();
1666                    }
1667                }
1668            }
1669            if (mListeners != null && mListeners.size() > 0) {
1670                ArrayList<TransitionListener> tmpListeners =
1671                        (ArrayList<TransitionListener>) mListeners.clone();
1672                int numListeners = tmpListeners.size();
1673                for (int i = 0; i < numListeners; ++i) {
1674                    tmpListeners.get(i).onTransitionPause(this);
1675                }
1676            }
1677            mPaused = true;
1678        }
1679    }
1680
1681    /**
1682     * Resumes this transition, sending out calls to {@link
1683     * TransitionListener#onTransitionPause(Transition)} to all listeners
1684     * and pausing all running animators started by this transition.
1685     *
1686     * @hide
1687     */
1688    public void resume(View sceneRoot) {
1689        if (mPaused) {
1690            if (!mEnded) {
1691                ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators();
1692                int numOldAnims = runningAnimators.size();
1693                WindowId windowId = sceneRoot.getWindowId();
1694                for (int i = numOldAnims - 1; i >= 0; i--) {
1695                    AnimationInfo info = runningAnimators.valueAt(i);
1696                    if (info.view != null && windowId != null && windowId.equals(info.windowId)) {
1697                        Animator anim = runningAnimators.keyAt(i);
1698                        anim.resume();
1699                    }
1700                }
1701                if (mListeners != null && mListeners.size() > 0) {
1702                    ArrayList<TransitionListener> tmpListeners =
1703                            (ArrayList<TransitionListener>) mListeners.clone();
1704                    int numListeners = tmpListeners.size();
1705                    for (int i = 0; i < numListeners; ++i) {
1706                        tmpListeners.get(i).onTransitionResume(this);
1707                    }
1708                }
1709            }
1710            mPaused = false;
1711        }
1712    }
1713
1714    /**
1715     * Called by TransitionManager to play the transition. This calls
1716     * createAnimators() to set things up and create all of the animations and then
1717     * runAnimations() to actually start the animations.
1718     */
1719    void playTransition(ViewGroup sceneRoot) {
1720        mStartValuesList = new ArrayList<TransitionValues>();
1721        mEndValuesList = new ArrayList<TransitionValues>();
1722        matchStartAndEnd(mStartValues, mEndValues);
1723
1724        ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators();
1725        int numOldAnims = runningAnimators.size();
1726        WindowId windowId = sceneRoot.getWindowId();
1727        for (int i = numOldAnims - 1; i >= 0; i--) {
1728            Animator anim = runningAnimators.keyAt(i);
1729            if (anim != null) {
1730                AnimationInfo oldInfo = runningAnimators.get(anim);
1731                if (oldInfo != null && oldInfo.view != null && oldInfo.windowId == windowId) {
1732                    TransitionValues oldValues = oldInfo.values;
1733                    View oldView = oldInfo.view;
1734                    TransitionValues newValues = getMatchedTransitionValues(oldView, true);
1735                    boolean cancel = oldInfo.transition.areValuesChanged(oldValues, newValues);
1736                    if (cancel) {
1737                        if (anim.isRunning() || anim.isStarted()) {
1738                            if (DBG) {
1739                                Log.d(LOG_TAG, "Canceling anim " + anim);
1740                            }
1741                            anim.cancel();
1742                        } else {
1743                            if (DBG) {
1744                                Log.d(LOG_TAG, "removing anim from info list: " + anim);
1745                            }
1746                            runningAnimators.remove(anim);
1747                        }
1748                    }
1749                }
1750            }
1751        }
1752
1753        createAnimators(sceneRoot, mStartValues, mEndValues, mStartValuesList, mEndValuesList);
1754        runAnimators();
1755    }
1756
1757    boolean areValuesChanged(TransitionValues oldValues, TransitionValues newValues) {
1758        boolean valuesChanged = false;
1759        // if oldValues null, then transition didn't care to stash values,
1760        // and won't get canceled
1761        if (oldValues != null && newValues != null) {
1762            String[] properties = getTransitionProperties();
1763            if (properties != null) {
1764                int count = properties.length;
1765                for (int i = 0; i < count; i++) {
1766                    if (isValueChanged(oldValues, newValues, properties[i])) {
1767                        valuesChanged = true;
1768                        break;
1769                    }
1770                }
1771            } else {
1772                for (String key : oldValues.values.keySet()) {
1773                    if (isValueChanged(oldValues, newValues, key)) {
1774                        valuesChanged = true;
1775                        break;
1776                    }
1777                }
1778            }
1779        }
1780        return valuesChanged;
1781    }
1782
1783    private static boolean isValueChanged(TransitionValues oldValues, TransitionValues newValues,
1784            String key) {
1785        Object oldValue = oldValues.values.get(key);
1786        Object newValue = newValues.values.get(key);
1787        boolean changed = (oldValue != null && newValue != null && !oldValue.equals(newValue));
1788        if (DBG && changed) {
1789            Log.d(LOG_TAG, "Transition.playTransition: " +
1790                    "oldValue != newValue for " + key +
1791                    ": old, new = " + oldValue + ", " + newValue);
1792        }
1793        return changed;
1794    }
1795
1796    /**
1797     * This is a utility method used by subclasses to handle standard parts of
1798     * setting up and running an Animator: it sets the {@link #getDuration()
1799     * duration} and the {@link #getStartDelay() startDelay}, starts the
1800     * animation, and, when the animator ends, calls {@link #end()}.
1801     *
1802     * @param animator The Animator to be run during this transition.
1803     *
1804     * @hide
1805     */
1806    protected void animate(Animator animator) {
1807        // TODO: maybe pass auto-end as a boolean parameter?
1808        if (animator == null) {
1809            end();
1810        } else {
1811            if (getDuration() >= 0) {
1812                animator.setDuration(getDuration());
1813            }
1814            if (getStartDelay() >= 0) {
1815                animator.setStartDelay(getStartDelay() + animator.getStartDelay());
1816            }
1817            if (getInterpolator() != null) {
1818                animator.setInterpolator(getInterpolator());
1819            }
1820            animator.addListener(new AnimatorListenerAdapter() {
1821                @Override
1822                public void onAnimationEnd(Animator animation) {
1823                    end();
1824                    animation.removeListener(this);
1825                }
1826            });
1827            animator.start();
1828        }
1829    }
1830
1831    /**
1832     * This method is called automatically by the transition and
1833     * TransitionSet classes prior to a Transition subclass starting;
1834     * subclasses should not need to call it directly.
1835     *
1836     * @hide
1837     */
1838    protected void start() {
1839        if (mNumInstances == 0) {
1840            if (mListeners != null && mListeners.size() > 0) {
1841                ArrayList<TransitionListener> tmpListeners =
1842                        (ArrayList<TransitionListener>) mListeners.clone();
1843                int numListeners = tmpListeners.size();
1844                for (int i = 0; i < numListeners; ++i) {
1845                    tmpListeners.get(i).onTransitionStart(this);
1846                }
1847            }
1848            mEnded = false;
1849        }
1850        mNumInstances++;
1851    }
1852
1853    /**
1854     * This method is called automatically by the Transition and
1855     * TransitionSet classes when a transition finishes, either because
1856     * a transition did nothing (returned a null Animator from
1857     * {@link Transition#createAnimator(ViewGroup, TransitionValues,
1858     * TransitionValues)}) or because the transition returned a valid
1859     * Animator and end() was called in the onAnimationEnd()
1860     * callback of the AnimatorListener.
1861     *
1862     * @hide
1863     */
1864    protected void end() {
1865        --mNumInstances;
1866        if (mNumInstances == 0) {
1867            if (mListeners != null && mListeners.size() > 0) {
1868                ArrayList<TransitionListener> tmpListeners =
1869                        (ArrayList<TransitionListener>) mListeners.clone();
1870                int numListeners = tmpListeners.size();
1871                for (int i = 0; i < numListeners; ++i) {
1872                    tmpListeners.get(i).onTransitionEnd(this);
1873                }
1874            }
1875            for (int i = 0; i < mStartValues.itemIdValues.size(); ++i) {
1876                View view = mStartValues.itemIdValues.valueAt(i);
1877                if (view != null) {
1878                    view.setHasTransientState(false);
1879                }
1880            }
1881            for (int i = 0; i < mEndValues.itemIdValues.size(); ++i) {
1882                View view = mEndValues.itemIdValues.valueAt(i);
1883                if (view != null) {
1884                    view.setHasTransientState(false);
1885                }
1886            }
1887            mEnded = true;
1888        }
1889    }
1890
1891    /**
1892     * This method cancels a transition that is currently running.
1893     *
1894     * @hide
1895     */
1896    protected void cancel() {
1897        int numAnimators = mCurrentAnimators.size();
1898        for (int i = numAnimators - 1; i >= 0; i--) {
1899            Animator animator = mCurrentAnimators.get(i);
1900            animator.cancel();
1901        }
1902        if (mListeners != null && mListeners.size() > 0) {
1903            ArrayList<TransitionListener> tmpListeners =
1904                    (ArrayList<TransitionListener>) mListeners.clone();
1905            int numListeners = tmpListeners.size();
1906            for (int i = 0; i < numListeners; ++i) {
1907                tmpListeners.get(i).onTransitionCancel(this);
1908            }
1909        }
1910    }
1911
1912    /**
1913     * Adds a listener to the set of listeners that are sent events through the
1914     * life of an animation, such as start, repeat, and end.
1915     *
1916     * @param listener the listener to be added to the current set of listeners
1917     * for this animation.
1918     * @return This transition object.
1919     */
1920    public Transition addListener(TransitionListener listener) {
1921        if (mListeners == null) {
1922            mListeners = new ArrayList<TransitionListener>();
1923        }
1924        mListeners.add(listener);
1925        return this;
1926    }
1927
1928    /**
1929     * Removes a listener from the set listening to this animation.
1930     *
1931     * @param listener the listener to be removed from the current set of
1932     * listeners for this transition.
1933     * @return This transition object.
1934     */
1935    public Transition removeListener(TransitionListener listener) {
1936        if (mListeners == null) {
1937            return this;
1938        }
1939        mListeners.remove(listener);
1940        if (mListeners.size() == 0) {
1941            mListeners = null;
1942        }
1943        return this;
1944    }
1945
1946    /**
1947     * Sets the callback to use to find the epicenter of a Transition. A null value indicates
1948     * that there is no epicenter in the Transition and onGetEpicenter() will return null.
1949     * Transitions like {@link android.transition.Explode} use a point or Rect to orient
1950     * the direction of travel. This is called the epicenter of the Transition and is
1951     * typically centered on a touched View. The
1952     * {@link android.transition.Transition.EpicenterCallback} allows a Transition to
1953     * dynamically retrieve the epicenter during a Transition.
1954     * @param epicenterCallback The callback to use to find the epicenter of the Transition.
1955     */
1956    public void setEpicenterCallback(EpicenterCallback epicenterCallback) {
1957        mEpicenterCallback = epicenterCallback;
1958    }
1959
1960    /**
1961     * Returns the callback used to find the epicenter of the Transition.
1962     * Transitions like {@link android.transition.Explode} use a point or Rect to orient
1963     * the direction of travel. This is called the epicenter of the Transition and is
1964     * typically centered on a touched View. The
1965     * {@link android.transition.Transition.EpicenterCallback} allows a Transition to
1966     * dynamically retrieve the epicenter during a Transition.
1967     * @return the callback used to find the epicenter of the Transition.
1968     */
1969    public EpicenterCallback getEpicenterCallback() {
1970        return mEpicenterCallback;
1971    }
1972
1973    /**
1974     * Returns the epicenter as specified by the
1975     * {@link android.transition.Transition.EpicenterCallback} or null if no callback exists.
1976     * @return the epicenter as specified by the
1977     * {@link android.transition.Transition.EpicenterCallback} or null if no callback exists.
1978     * @see #setEpicenterCallback(android.transition.Transition.EpicenterCallback)
1979     */
1980    public Rect getEpicenter() {
1981        if (mEpicenterCallback == null) {
1982            return null;
1983        }
1984        return mEpicenterCallback.onGetEpicenter(this);
1985    }
1986
1987    /**
1988     * Sets the algorithm used to calculate two-dimensional interpolation.
1989     * <p>
1990     *     Transitions such as {@link android.transition.ChangeBounds} move Views, typically
1991     *     in a straight path between the start and end positions. Applications that desire to
1992     *     have these motions move in a curve can change how Views interpolate in two dimensions
1993     *     by extending PathMotion and implementing
1994     *     {@link android.transition.PathMotion#getPath(float, float, float, float)}.
1995     * </p>
1996     * <p>
1997     *     When describing in XML, use a nested XML tag for the path motion. It can be one of
1998     *     the built-in tags <code>arcMotion</code> or <code>patternPathMotion</code> or it can
1999     *     be a custom PathMotion using <code>pathMotion</code> with the <code>class</code>
2000     *     attributed with the fully-described class name. For example:</p>
2001     * <pre>
2002     * {@code
2003     * &lt;changeBounds>
2004     *     &lt;pathMotion class="my.app.transition.MyPathMotion"/>
2005     * &lt;/changeBounds>
2006     * }
2007     * </pre>
2008     * <p>or</p>
2009     * <pre>
2010     * {@code
2011     * &lt;changeBounds>
2012     *   &lt;arcMotion android:minimumHorizontalAngle="15"
2013     *     android:minimumVerticalAngle="0" android:maximumAngle="90"/>
2014     * &lt;/changeBounds>
2015     * }
2016     * </pre>
2017     *
2018     * @param pathMotion Algorithm object to use for determining how to interpolate in two
2019     *                   dimensions. If null, a straight-path algorithm will be used.
2020     * @see android.transition.ArcMotion
2021     * @see PatternPathMotion
2022     * @see android.transition.PathMotion
2023     */
2024    public void setPathMotion(PathMotion pathMotion) {
2025        if (pathMotion == null) {
2026            mPathMotion = STRAIGHT_PATH_MOTION;
2027        } else {
2028            mPathMotion = pathMotion;
2029        }
2030    }
2031
2032    /**
2033     * Returns the algorithm object used to interpolate along two dimensions. This is typically
2034     * used to determine the View motion between two points.
2035     *
2036     * <p>
2037     *     When describing in XML, use a nested XML tag for the path motion. It can be one of
2038     *     the built-in tags <code>arcMotion</code> or <code>patternPathMotion</code> or it can
2039     *     be a custom PathMotion using <code>pathMotion</code> with the <code>class</code>
2040     *     attributed with the fully-described class name. For example:</p>
2041     * <pre>
2042     * {@code
2043     * &lt;changeBounds>
2044     *     &lt;pathMotion class="my.app.transition.MyPathMotion"/>
2045     * &lt;/changeBounds>}
2046     * </pre>
2047     * <p>or</p>
2048     * <pre>
2049     * {@code
2050     * &lt;changeBounds>
2051     *   &lt;arcMotion android:minimumHorizontalAngle="15"
2052     *              android:minimumVerticalAngle="0"
2053     *              android:maximumAngle="90"/>
2054     * &lt;/changeBounds>}
2055     * </pre>
2056     *
2057     * @return The algorithm object used to interpolate along two dimensions.
2058     * @see android.transition.ArcMotion
2059     * @see PatternPathMotion
2060     * @see android.transition.PathMotion
2061     */
2062    public PathMotion getPathMotion() {
2063        return mPathMotion;
2064    }
2065
2066    /**
2067     * Sets the method for determining Animator start delays.
2068     * When a Transition affects several Views like {@link android.transition.Explode} or
2069     * {@link android.transition.Slide}, there may be a desire to have a "wave-front" effect
2070     * such that the Animator start delay depends on position of the View. The
2071     * TransitionPropagation specifies how the start delays are calculated.
2072     * @param transitionPropagation The class used to determine the start delay of
2073     *                              Animators created by this Transition. A null value
2074     *                              indicates that no delay should be used.
2075     */
2076    public void setPropagation(TransitionPropagation transitionPropagation) {
2077        mPropagation = transitionPropagation;
2078    }
2079
2080    /**
2081     * Returns the {@link android.transition.TransitionPropagation} used to calculate Animator start
2082     * delays.
2083     * When a Transition affects several Views like {@link android.transition.Explode} or
2084     * {@link android.transition.Slide}, there may be a desire to have a "wave-front" effect
2085     * such that the Animator start delay depends on position of the View. The
2086     * TransitionPropagation specifies how the start delays are calculated.
2087     * @return the {@link android.transition.TransitionPropagation} used to calculate Animator start
2088     * delays. This is null by default.
2089     */
2090    public TransitionPropagation getPropagation() {
2091        return mPropagation;
2092    }
2093
2094    /**
2095     * Captures TransitionPropagation values for the given view and the
2096     * hierarchy underneath it.
2097     */
2098    void capturePropagationValues(TransitionValues transitionValues) {
2099        if (mPropagation != null && !transitionValues.values.isEmpty()) {
2100            String[] propertyNames = mPropagation.getPropagationProperties();
2101            if (propertyNames == null) {
2102                return;
2103            }
2104            boolean containsAll = true;
2105            for (int i = 0; i < propertyNames.length; i++) {
2106                if (!transitionValues.values.containsKey(propertyNames[i])) {
2107                    containsAll = false;
2108                    break;
2109                }
2110            }
2111            if (!containsAll) {
2112                mPropagation.captureValues(transitionValues);
2113            }
2114        }
2115    }
2116
2117    Transition setSceneRoot(ViewGroup sceneRoot) {
2118        mSceneRoot = sceneRoot;
2119        return this;
2120    }
2121
2122    void setCanRemoveViews(boolean canRemoveViews) {
2123        mCanRemoveViews = canRemoveViews;
2124    }
2125
2126    public boolean canRemoveViews() {
2127        return mCanRemoveViews;
2128    }
2129
2130    /**
2131     * Sets the shared element names -- a mapping from a name at the start state to
2132     * a different name at the end state.
2133     * @hide
2134     */
2135    public void setNameOverrides(ArrayMap<String, String> overrides) {
2136        mNameOverrides = overrides;
2137    }
2138
2139    /** @hide */
2140    public ArrayMap<String, String> getNameOverrides() {
2141        return mNameOverrides;
2142    }
2143
2144    /** @hide */
2145    public void forceVisibility(int visibility, boolean isStartValue) {}
2146
2147    @Override
2148    public String toString() {
2149        return toString("");
2150    }
2151
2152    @Override
2153    public Transition clone() {
2154        Transition clone = null;
2155        try {
2156            clone = (Transition) super.clone();
2157            clone.mAnimators = new ArrayList<Animator>();
2158            clone.mStartValues = new TransitionValuesMaps();
2159            clone.mEndValues = new TransitionValuesMaps();
2160            clone.mStartValuesList = null;
2161            clone.mEndValuesList = null;
2162        } catch (CloneNotSupportedException e) {}
2163
2164        return clone;
2165    }
2166
2167    /**
2168     * Returns the name of this Transition. This name is used internally to distinguish
2169     * between different transitions to determine when interrupting transitions overlap.
2170     * For example, a ChangeBounds running on the same target view as another ChangeBounds
2171     * should determine whether the old transition is animating to different end values
2172     * and should be canceled in favor of the new transition.
2173     *
2174     * <p>By default, a Transition's name is simply the value of {@link Class#getName()},
2175     * but subclasses are free to override and return something different.</p>
2176     *
2177     * @return The name of this transition.
2178     */
2179    public String getName() {
2180        return mName;
2181    }
2182
2183    String toString(String indent) {
2184        String result = indent + getClass().getSimpleName() + "@" +
2185                Integer.toHexString(hashCode()) + ": ";
2186        if (mDuration != -1) {
2187            result += "dur(" + mDuration + ") ";
2188        }
2189        if (mStartDelay != -1) {
2190            result += "dly(" + mStartDelay + ") ";
2191        }
2192        if (mInterpolator != null) {
2193            result += "interp(" + mInterpolator + ") ";
2194        }
2195        if (mTargetIds.size() > 0 || mTargets.size() > 0) {
2196            result += "tgts(";
2197            if (mTargetIds.size() > 0) {
2198                for (int i = 0; i < mTargetIds.size(); ++i) {
2199                    if (i > 0) {
2200                        result += ", ";
2201                    }
2202                    result += mTargetIds.get(i);
2203                }
2204            }
2205            if (mTargets.size() > 0) {
2206                for (int i = 0; i < mTargets.size(); ++i) {
2207                    if (i > 0) {
2208                        result += ", ";
2209                    }
2210                    result += mTargets.get(i);
2211                }
2212            }
2213            result += ")";
2214        }
2215        return result;
2216    }
2217
2218    /**
2219     * A transition listener receives notifications from a transition.
2220     * Notifications indicate transition lifecycle events.
2221     */
2222    public static interface TransitionListener {
2223        /**
2224         * Notification about the start of the transition.
2225         *
2226         * @param transition The started transition.
2227         */
2228        void onTransitionStart(Transition transition);
2229
2230        /**
2231         * Notification about the end of the transition. Canceled transitions
2232         * will always notify listeners of both the cancellation and end
2233         * events. That is, {@link #onTransitionEnd(Transition)} is always called,
2234         * regardless of whether the transition was canceled or played
2235         * through to completion.
2236         *
2237         * @param transition The transition which reached its end.
2238         */
2239        void onTransitionEnd(Transition transition);
2240
2241        /**
2242         * Notification about the cancellation of the transition.
2243         * Note that cancel may be called by a parent {@link TransitionSet} on
2244         * a child transition which has not yet started. This allows the child
2245         * transition to restore state on target objects which was set at
2246         * {@link #createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues)
2247         * createAnimator()} time.
2248         *
2249         * @param transition The transition which was canceled.
2250         */
2251        void onTransitionCancel(Transition transition);
2252
2253        /**
2254         * Notification when a transition is paused.
2255         * Note that createAnimator() may be called by a parent {@link TransitionSet} on
2256         * a child transition which has not yet started. This allows the child
2257         * transition to restore state on target objects which was set at
2258         * {@link #createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues)
2259         * createAnimator()} time.
2260         *
2261         * @param transition The transition which was paused.
2262         */
2263        void onTransitionPause(Transition transition);
2264
2265        /**
2266         * Notification when a transition is resumed.
2267         * Note that resume() may be called by a parent {@link TransitionSet} on
2268         * a child transition which has not yet started. This allows the child
2269         * transition to restore state which may have changed in an earlier call
2270         * to {@link #onTransitionPause(Transition)}.
2271         *
2272         * @param transition The transition which was resumed.
2273         */
2274        void onTransitionResume(Transition transition);
2275    }
2276
2277    /**
2278     * Utility adapter class to avoid having to override all three methods
2279     * whenever someone just wants to listen for a single event.
2280     *
2281     * @hide
2282     * */
2283    public static class TransitionListenerAdapter implements TransitionListener {
2284        @Override
2285        public void onTransitionStart(Transition transition) {
2286        }
2287
2288        @Override
2289        public void onTransitionEnd(Transition transition) {
2290        }
2291
2292        @Override
2293        public void onTransitionCancel(Transition transition) {
2294        }
2295
2296        @Override
2297        public void onTransitionPause(Transition transition) {
2298        }
2299
2300        @Override
2301        public void onTransitionResume(Transition transition) {
2302        }
2303    }
2304
2305    /**
2306     * Holds information about each animator used when a new transition starts
2307     * while other transitions are still running to determine whether a running
2308     * animation should be canceled or a new animation noop'd. The structure holds
2309     * information about the state that an animation is going to, to be compared to
2310     * end state of a new animation.
2311     * @hide
2312     */
2313    public static class AnimationInfo {
2314        public View view;
2315        String name;
2316        TransitionValues values;
2317        WindowId windowId;
2318        Transition transition;
2319
2320        AnimationInfo(View view, String name, Transition transition,
2321                WindowId windowId, TransitionValues values) {
2322            this.view = view;
2323            this.name = name;
2324            this.values = values;
2325            this.windowId = windowId;
2326            this.transition = transition;
2327        }
2328    }
2329
2330    /**
2331     * Utility class for managing typed ArrayLists efficiently. In particular, this
2332     * can be useful for lists that we don't expect to be used often (eg, the exclude
2333     * lists), so we'd like to keep them nulled out by default. This causes the code to
2334     * become tedious, with constant null checks, code to allocate when necessary,
2335     * and code to null out the reference when the list is empty. This class encapsulates
2336     * all of that functionality into simple add()/remove() methods which perform the
2337     * necessary checks, allocation/null-out as appropriate, and return the
2338     * resulting list.
2339     */
2340    private static class ArrayListManager {
2341
2342        /**
2343         * Add the specified item to the list, returning the resulting list.
2344         * The returned list can either the be same list passed in or, if that
2345         * list was null, the new list that was created.
2346         *
2347         * Note that the list holds unique items; if the item already exists in the
2348         * list, the list is not modified.
2349         */
2350        static <T> ArrayList<T> add(ArrayList<T> list, T item) {
2351            if (list == null) {
2352                list = new ArrayList<T>();
2353            }
2354            if (!list.contains(item)) {
2355                list.add(item);
2356            }
2357            return list;
2358        }
2359
2360        /**
2361         * Remove the specified item from the list, returning the resulting list.
2362         * The returned list can either the be same list passed in or, if that
2363         * list becomes empty as a result of the remove(), the new list was created.
2364         */
2365        static <T> ArrayList<T> remove(ArrayList<T> list, T item) {
2366            if (list != null) {
2367                list.remove(item);
2368                if (list.isEmpty()) {
2369                    list = null;
2370                }
2371            }
2372            return list;
2373        }
2374    }
2375
2376    /**
2377     * Class to get the epicenter of Transition. Use
2378     * {@link #setEpicenterCallback(android.transition.Transition.EpicenterCallback)} to
2379     * set the callback used to calculate the epicenter of the Transition. Override
2380     * {@link #getEpicenter()} to return the rectangular region in screen coordinates of
2381     * the epicenter of the transition.
2382     * @see #setEpicenterCallback(android.transition.Transition.EpicenterCallback)
2383     */
2384    public static abstract class EpicenterCallback {
2385
2386        /**
2387         * Implementers must override to return the epicenter of the Transition in screen
2388         * coordinates. Transitions like {@link android.transition.Explode} depend upon
2389         * an epicenter for the Transition. In Explode, Views move toward or away from the
2390         * center of the epicenter Rect along the vector between the epicenter and the center
2391         * of the View appearing and disappearing. Some Transitions, such as
2392         * {@link android.transition.Fade} pay no attention to the epicenter.
2393         *
2394         * @param transition The transition for which the epicenter applies.
2395         * @return The Rect region of the epicenter of <code>transition</code> or null if
2396         * there is no epicenter.
2397         */
2398        public abstract Rect onGetEpicenter(Transition transition);
2399    }
2400}
2401