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