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.TransitionInflater; 25import android.transition.TransitionManager; 26import android.transition.TransitionSet; 27import android.transition.TransitionValues; 28import android.util.SparseBooleanArray; 29import android.util.SparseIntArray; 30import android.view.View; 31import android.view.ViewGroup; 32 33import java.util.ArrayList; 34import java.util.HashMap; 35 36final class TransitionHelperKitkat { 37 38 TransitionHelperKitkat() { 39 } 40 41 static Object createScene(ViewGroup sceneRoot, Runnable enterAction) { 42 Scene scene = new Scene(sceneRoot); 43 scene.setEnterAction(enterAction); 44 return scene; 45 } 46 47 static Object createTransitionSet(boolean sequential) { 48 TransitionSet set = new TransitionSet(); 49 set.setOrdering(sequential ? TransitionSet.ORDERING_SEQUENTIAL : 50 TransitionSet.ORDERING_TOGETHER); 51 return set; 52 } 53 54 static void addTransition(Object transitionSet, Object transition) { 55 ((TransitionSet) transitionSet).addTransition((Transition) transition); 56 } 57 58 static Object createAutoTransition() { 59 return new AutoTransition(); 60 } 61 62 static Object createSlide(int slideEdge) { 63 SlideKitkat slide = new SlideKitkat(); 64 slide.setSlideEdge(slideEdge); 65 return slide; 66 } 67 68 static Object createScale() { 69 Scale scale = new Scale(); 70 return scale; 71 } 72 73 static Object createFadeTransition(int fadingMode) { 74 Fade fade = new Fade(fadingMode); 75 return fade; 76 } 77 78 /** 79 * change bounds that support customized start delay. 80 */ 81 static class CustomChangeBounds extends ChangeBounds { 82 83 int mDefaultStartDelay; 84 // View -> delay 85 final HashMap<View, Integer> mViewStartDelays = new HashMap<View, Integer>(); 86 // id -> delay 87 final SparseIntArray mIdStartDelays = new SparseIntArray(); 88 // Class.getName() -> delay 89 final HashMap<String, Integer> mClassStartDelays = new HashMap<String, Integer>(); 90 91 private int getDelay(View view) { 92 Integer delay = mViewStartDelays.get(view); 93 if (delay != null) { 94 return delay; 95 } 96 int idStartDelay = mIdStartDelays.get(view.getId(), -1); 97 if (idStartDelay != -1) { 98 return idStartDelay; 99 } 100 delay = mClassStartDelays.get(view.getClass().getName()); 101 if (delay != null) { 102 return delay; 103 } 104 return mDefaultStartDelay; 105 } 106 107 @Override 108 public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, 109 TransitionValues endValues) { 110 Animator animator = super.createAnimator(sceneRoot, startValues, endValues); 111 if (animator != null && endValues != null && endValues.view != null) { 112 animator.setStartDelay(getDelay(endValues.view)); 113 } 114 return animator; 115 } 116 117 public void setStartDelay(View view, int startDelay) { 118 mViewStartDelays.put(view, startDelay); 119 } 120 121 public void setStartDelay(int viewId, int startDelay) { 122 mIdStartDelays.put(viewId, startDelay); 123 } 124 125 public void setStartDelay(String className, int startDelay) { 126 mClassStartDelays.put(className, startDelay); 127 } 128 129 public void setDefaultStartDelay(int startDelay) { 130 mDefaultStartDelay = startDelay; 131 } 132 } 133 134 static Object createChangeBounds(boolean reparent) { 135 CustomChangeBounds changeBounds = new CustomChangeBounds(); 136 changeBounds.setReparent(reparent); 137 return changeBounds; 138 } 139 140 static void setChangeBoundsStartDelay(Object changeBounds, int viewId, int startDelay) { 141 ((CustomChangeBounds) changeBounds).setStartDelay(viewId, startDelay); 142 } 143 144 static void setChangeBoundsStartDelay(Object changeBounds, View view, int startDelay) { 145 ((CustomChangeBounds) changeBounds).setStartDelay(view, startDelay); 146 } 147 148 static void setChangeBoundsStartDelay(Object changeBounds, String className, int startDelay) { 149 ((CustomChangeBounds) changeBounds).setStartDelay(className, startDelay); 150 } 151 152 static void setChangeBoundsDefaultStartDelay(Object changeBounds, int startDelay) { 153 ((CustomChangeBounds) changeBounds).setDefaultStartDelay(startDelay); 154 } 155 156 static void setStartDelay(Object transition, long startDelay) { 157 ((Transition)transition).setStartDelay(startDelay); 158 } 159 160 static void setDuration(Object transition, long duration) { 161 ((Transition)transition).setDuration(duration); 162 } 163 164 static void exclude(Object transition, int targetId, boolean exclude) { 165 ((Transition) transition).excludeTarget(targetId, exclude); 166 } 167 168 static void exclude(Object transition, View targetView, boolean exclude) { 169 ((Transition) transition).excludeTarget(targetView, exclude); 170 } 171 172 static void excludeChildren(Object transition, int targetId, boolean exclude) { 173 ((Transition) transition).excludeChildren(targetId, exclude); 174 } 175 176 static void excludeChildren(Object transition, View targetView, boolean exclude) { 177 ((Transition) transition).excludeChildren(targetView, exclude); 178 } 179 180 static void include(Object transition, int targetId) { 181 ((Transition) transition).addTarget(targetId); 182 } 183 184 static void include(Object transition, View targetView) { 185 ((Transition) transition).addTarget(targetView); 186 } 187 188 static void setTransitionListener(Object transition, final TransitionListener listener) { 189 Transition t = (Transition) transition; 190 t.addListener(new Transition.TransitionListener() { 191 192 @Override 193 public void onTransitionStart(Transition transition) { 194 listener.onTransitionStart(transition); 195 } 196 197 @Override 198 public void onTransitionResume(Transition transition) { 199 } 200 201 @Override 202 public void onTransitionPause(Transition transition) { 203 } 204 205 @Override 206 public void onTransitionEnd(Transition transition) { 207 listener.onTransitionEnd(transition); 208 } 209 210 @Override 211 public void onTransitionCancel(Transition transition) { 212 } 213 }); 214 } 215 216 static void runTransition(Object scene, Object transition) { 217 TransitionManager.go((Scene) scene, (Transition) transition); 218 } 219 220 static void setInterpolator(Object transition, Object timeInterpolator) { 221 ((Transition) transition).setInterpolator((TimeInterpolator) timeInterpolator); 222 } 223 224 static void addTarget(Object transition, View view) { 225 ((Transition) transition).addTarget(view); 226 } 227 228 static Object loadTransition(Context context, int resId) { 229 return TransitionInflater.from(context).inflateTransition(resId); 230 } 231} 232