1 /*
2  * Copyright (C) 2016 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 package com.android.compatibility.common.util.transition;
17 
18 import android.animation.Animator;
19 import android.graphics.Rect;
20 import android.transition.Transition;
21 import android.transition.TransitionValues;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 import java.util.ArrayList;
26 
27 /**
28  * A transition that tracks which targets are applied to it.
29  * It will assume any target that it applies to will have differences
30  * between the start and end state, regardless of the differences
31  * that actually exist. In other words, it doesn't actually check
32  * any size or position differences or any other property of the view.
33  * It just records the difference.
34  * <p>
35  * Both start and end value Views are recorded, but no actual animation
36  * is created.
37  */
38 public class TrackingTransition extends Transition implements TargetTracking {
39     public final ArrayList<View> targets = new ArrayList<>();
40     private final Rect[] mEpicenter = new Rect[1];
41     private static String PROP = "tracking:prop";
42     private static String[] PROPS = { PROP };
43 
44     @Override
getTransitionProperties()45     public String[] getTransitionProperties() {
46         return PROPS;
47     }
48 
49     @Override
captureStartValues(TransitionValues transitionValues)50     public void captureStartValues(TransitionValues transitionValues) {
51         transitionValues.values.put(PROP, 0);
52     }
53 
54     @Override
captureEndValues(TransitionValues transitionValues)55     public void captureEndValues(TransitionValues transitionValues) {
56         transitionValues.values.put(PROP, 1);
57     }
58 
59     @Override
createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues)60     public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
61             TransitionValues endValues) {
62         if (startValues != null) {
63             targets.add(startValues.view);
64         }
65         if (endValues != null) {
66             targets.add(endValues.view);
67         }
68         Rect epicenter = getEpicenter();
69         if (epicenter != null) {
70             mEpicenter[0] = new Rect(epicenter);
71         } else {
72             mEpicenter[0] = null;
73         }
74         return null;
75     }
76 
77     @Override
getTrackedTargets()78     public ArrayList<View> getTrackedTargets() {
79         return targets;
80     }
81 
82     @Override
clearTargets()83     public void clearTargets() {
84         targets.clear();
85     }
86 
87     @Override
getCapturedEpicenter()88     public Rect getCapturedEpicenter() {
89         return mEpicenter[0];
90     }
91 }
92