1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.transition;
15
16import android.animation.Animator;
17import android.animation.TimeInterpolator;
18import android.content.Context;
19import android.transition.AutoTransition;
20import android.transition.ChangeBounds;
21import android.transition.Fade;
22import android.transition.Scene;
23import android.transition.Transition;
24import android.transition.TransitionManager;
25import android.transition.TransitionSet;
26import android.transition.TransitionValues;
27import android.util.SparseBooleanArray;
28import android.util.SparseIntArray;
29import android.view.View;
30import android.view.ViewGroup;
31
32import java.util.ArrayList;
33import java.util.HashMap;
34
35final class TransitionHelperKitkat {
36
37    TransitionHelperKitkat() {
38    }
39
40    static Object createScene(ViewGroup sceneRoot, Runnable enterAction) {
41        Scene scene = new Scene(sceneRoot);
42        scene.setEnterAction(enterAction);
43        return scene;
44    }
45
46    static Object createTransitionSet(boolean sequential) {
47        TransitionSet set = new TransitionSet();
48        set.setOrdering(sequential ? TransitionSet.ORDERING_SEQUENTIAL :
49            TransitionSet.ORDERING_TOGETHER);
50        return set;
51    }
52
53    static void addTransition(Object transitionSet, Object transition) {
54        ((TransitionSet) transitionSet).addTransition((Transition) transition);
55    }
56
57    static Object createAutoTransition() {
58        return new AutoTransition();
59    }
60
61    static Object createSlide(SlideCallback callback) {
62        Slide slide = new Slide();
63        slide.setCallback(callback);
64        return slide;
65    }
66
67    static Object createScale() {
68        Scale scale = new Scale();
69        return scale;
70    }
71
72    static Object createFadeTransition(int fadingMode) {
73        Fade fade = new Fade(fadingMode);
74        return fade;
75    }
76
77    /**
78     * change bounds that support customized start delay.
79     */
80    static class CustomChangeBounds extends ChangeBounds {
81
82        int mDefaultStartDelay;
83        // View -> delay
84        final HashMap<View, Integer> mViewStartDelays = new HashMap<View, Integer>();
85        // id -> delay
86        final SparseIntArray mIdStartDelays = new SparseIntArray();
87        // Class.getName() -> delay
88        final HashMap<String, Integer> mClassStartDelays = new HashMap<String, Integer>();
89
90        private int getDelay(View view) {
91            Integer delay = mViewStartDelays.get(view);
92            if (delay != null) {
93                return delay;
94            }
95            int idStartDelay = mIdStartDelays.get(view.getId(), -1);
96            if (idStartDelay != -1) {
97                return idStartDelay;
98            }
99            delay = mClassStartDelays.get(view.getClass().getName());
100            if (delay != null) {
101                return delay;
102            }
103            return mDefaultStartDelay;
104        }
105
106        @Override
107        public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
108                TransitionValues endValues) {
109            Animator animator = super.createAnimator(sceneRoot, startValues, endValues);
110            if (animator != null && endValues != null && endValues.view != null) {
111                animator.setStartDelay(getDelay(endValues.view));
112            }
113            return animator;
114        }
115
116        public void setStartDelay(View view, int startDelay) {
117            mViewStartDelays.put(view, startDelay);
118        }
119
120        public void setStartDelay(int viewId, int startDelay) {
121            mIdStartDelays.put(viewId, startDelay);
122        }
123
124        public void setStartDelay(String className, int startDelay) {
125            mClassStartDelays.put(className, startDelay);
126        }
127
128        public void setDefaultStartDelay(int startDelay) {
129            mDefaultStartDelay = startDelay;
130        }
131    }
132
133    static Object createChangeBounds(boolean reparent) {
134        CustomChangeBounds changeBounds = new CustomChangeBounds();
135        changeBounds.setReparent(reparent);
136        return changeBounds;
137    }
138
139    static void setChangeBoundsStartDelay(Object changeBounds, int viewId, int startDelay) {
140        ((CustomChangeBounds) changeBounds).setStartDelay(viewId, startDelay);
141    }
142
143    static void setChangeBoundsStartDelay(Object changeBounds, View view, int startDelay) {
144        ((CustomChangeBounds) changeBounds).setStartDelay(view, startDelay);
145    }
146
147    static void setChangeBoundsStartDelay(Object changeBounds, String className, int startDelay) {
148        ((CustomChangeBounds) changeBounds).setStartDelay(className, startDelay);
149    }
150
151    static void setChangeBoundsDefaultStartDelay(Object changeBounds, int startDelay) {
152        ((CustomChangeBounds) changeBounds).setDefaultStartDelay(startDelay);
153    }
154
155    static void setStartDelay(Object transition, long startDelay) {
156        ((Transition)transition).setStartDelay(startDelay);
157    }
158
159    static void setDuration(Object transition, long duration) {
160        ((Transition)transition).setDuration(duration);
161    }
162
163    static void exclude(Object transition, int targetId, boolean exclude) {
164        ((Transition) transition).excludeTarget(targetId, exclude);
165    }
166
167    static void exclude(Object transition, View targetView, boolean exclude) {
168        ((Transition) transition).excludeTarget(targetView, exclude);
169    }
170
171    static void excludeChildren(Object transition, int targetId, boolean exclude) {
172        ((Transition) transition).excludeChildren(targetId, exclude);
173    }
174
175    static void excludeChildren(Object transition, View targetView, boolean exclude) {
176        ((Transition) transition).excludeChildren(targetView, exclude);
177    }
178
179    static void include(Object transition, int targetId) {
180        ((Transition) transition).addTarget(targetId);
181    }
182
183    static void include(Object transition, View targetView) {
184        ((Transition) transition).addTarget(targetView);
185    }
186
187    static void setTransitionListener(Object transition, final TransitionListener listener) {
188        Transition t = (Transition) transition;
189        t.addListener(new Transition.TransitionListener() {
190
191            @Override
192            public void onTransitionStart(Transition transition) {
193                listener.onTransitionStart(transition);
194            }
195
196            @Override
197            public void onTransitionResume(Transition transition) {
198            }
199
200            @Override
201            public void onTransitionPause(Transition transition) {
202            }
203
204            @Override
205            public void onTransitionEnd(Transition transition) {
206                listener.onTransitionEnd(transition);
207            }
208
209            @Override
210            public void onTransitionCancel(Transition transition) {
211            }
212        });
213    }
214
215    static void runTransition(Object scene, Object transition) {
216        TransitionManager.go((Scene) scene, (Transition) transition);
217    }
218
219    static void setInterpolator(Object transition, Object timeInterpolator) {
220        ((Transition) transition).setInterpolator((TimeInterpolator) timeInterpolator);
221    }
222
223    static void addTarget(Object transition, View view) {
224        ((Transition) transition).addTarget(view);
225    }
226}
227