1 /*
2  * Copyright 2018 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 
17 package androidx.leanback.transition;
18 
19 import android.animation.Animator;
20 import android.transition.ChangeBounds;
21 import android.transition.TransitionValues;
22 import android.util.SparseIntArray;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 import androidx.annotation.RequiresApi;
27 
28 import java.util.HashMap;
29 
30 /**
31  * change bounds that support customized start delay.
32  */
33 @RequiresApi(19)
34 class CustomChangeBounds extends ChangeBounds {
35 
36     int mDefaultStartDelay;
37     // View -> delay
38     final HashMap<View, Integer> mViewStartDelays = new HashMap<View, Integer>();
39     // id -> delay
40     final SparseIntArray mIdStartDelays = new SparseIntArray();
41     // Class.getName() -> delay
42     final HashMap<String, Integer> mClassStartDelays = new HashMap<String, Integer>();
43 
getDelay(View view)44     private int getDelay(View view) {
45         Integer delay = mViewStartDelays.get(view);
46         if (delay != null) {
47             return delay;
48         }
49         int idStartDelay = mIdStartDelays.get(view.getId(), -1);
50         if (idStartDelay != -1) {
51             return idStartDelay;
52         }
53         delay = mClassStartDelays.get(view.getClass().getName());
54         if (delay != null) {
55             return delay;
56         }
57         return mDefaultStartDelay;
58     }
59 
60     @Override
createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues)61     public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
62             TransitionValues endValues) {
63         Animator animator = super.createAnimator(sceneRoot, startValues, endValues);
64         if (animator != null && endValues != null && endValues.view != null) {
65             animator.setStartDelay(getDelay(endValues.view));
66         }
67         return animator;
68     }
69 
setStartDelay(View view, int startDelay)70     public void setStartDelay(View view, int startDelay) {
71         mViewStartDelays.put(view, startDelay);
72     }
73 
setStartDelay(int viewId, int startDelay)74     public void setStartDelay(int viewId, int startDelay) {
75         mIdStartDelays.put(viewId, startDelay);
76     }
77 
setStartDelay(String className, int startDelay)78     public void setStartDelay(String className, int startDelay) {
79         mClassStartDelays.put(className, startDelay);
80     }
81 
setDefaultStartDelay(int startDelay)82     public void setDefaultStartDelay(int startDelay) {
83         mDefaultStartDelay = startDelay;
84     }
85 }
86