Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 android.view.transition; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.res.Resources; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.content.res.XmlResourceParser; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.util.SparseArray; |
| 24 | import android.util.Xml; |
| 25 | import android.view.InflateException; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.view.animation.AnimationUtils; |
| 28 | import org.xmlpull.v1.XmlPullParser; |
| 29 | import org.xmlpull.v1.XmlPullParserException; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.HashMap; |
| 34 | |
| 35 | /** |
| 36 | * This class inflates scenes and transitions from resource files. |
| 37 | */ |
| 38 | public class TransitionInflater { |
| 39 | |
| 40 | // We only need one inflater for any given context. Also, this allows us to associate |
| 41 | // ids with unique instances per-Context, used to avoid re-inflating |
| 42 | // already-inflated resources into new/different instances |
| 43 | private static final HashMap<Context, TransitionInflater> sInflaterMap = |
| 44 | new HashMap<Context, TransitionInflater>(); |
| 45 | |
| 46 | private Context mContext; |
| 47 | // TODO: do we need id maps for transitions and transitionMgrs as well? |
| 48 | SparseArray<Scene> mScenes = new SparseArray<Scene>(); |
| 49 | |
| 50 | private TransitionInflater(Context context) { |
| 51 | mContext = context; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Obtains the TransitionInflater from the given context. |
| 56 | */ |
| 57 | public static TransitionInflater from(Context context) { |
| 58 | TransitionInflater inflater = sInflaterMap.get(context); |
| 59 | if (inflater != null) { |
| 60 | return inflater; |
| 61 | } |
| 62 | inflater = new TransitionInflater(context); |
| 63 | sInflaterMap.put(context, inflater); |
| 64 | return inflater; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Loads a {@link Transition} object from a resource |
| 69 | * |
| 70 | * @param resource The resource id of the transition to load |
| 71 | * @return The loaded Transition object |
| 72 | * @throws android.content.res.Resources.NotFoundException when the |
| 73 | * transition cannot be loaded |
| 74 | */ |
| 75 | public Transition inflateTransition(int resource) { |
| 76 | XmlResourceParser parser = mContext.getResources().getXml(resource); |
| 77 | try { |
| 78 | return createTransitionFromXml(parser, Xml.asAttributeSet(parser), null); |
| 79 | } catch (XmlPullParserException e) { |
| 80 | InflateException ex = new InflateException(e.getMessage()); |
| 81 | ex.initCause(e); |
| 82 | throw ex; |
| 83 | } catch (IOException e) { |
| 84 | InflateException ex = new InflateException( |
| 85 | parser.getPositionDescription() |
| 86 | + ": " + e.getMessage()); |
| 87 | ex.initCause(e); |
| 88 | throw ex; |
| 89 | } finally { |
| 90 | parser.close(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Loads a {@link TransitionManager} object from a resource |
| 96 | * |
| 97 | * |
| 98 | * |
| 99 | * @param resource The resource id of the transition manager to load |
| 100 | * @return The loaded TransitionManager object |
| 101 | * @throws android.content.res.Resources.NotFoundException when the |
| 102 | * transition manager cannot be loaded |
| 103 | */ |
| 104 | public TransitionManager inflateTransitionManager(int resource, ViewGroup sceneRoot) { |
| 105 | XmlResourceParser parser = mContext.getResources().getXml(resource); |
| 106 | try { |
| 107 | return createTransitionManagerFromXml(parser, Xml.asAttributeSet(parser), sceneRoot); |
| 108 | } catch (XmlPullParserException e) { |
| 109 | InflateException ex = new InflateException(e.getMessage()); |
| 110 | ex.initCause(e); |
| 111 | throw ex; |
| 112 | } catch (IOException e) { |
| 113 | InflateException ex = new InflateException( |
| 114 | parser.getPositionDescription() |
| 115 | + ": " + e.getMessage()); |
| 116 | ex.initCause(e); |
| 117 | throw ex; |
| 118 | } finally { |
| 119 | parser.close(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Loads a {@link Scene} object from a resource |
| 125 | * |
| 126 | * @param resource The resource id of the scene to load |
| 127 | * @return The loaded Scene object |
| 128 | * @throws android.content.res.Resources.NotFoundException when the scene |
| 129 | * cannot be loaded |
| 130 | */ |
| 131 | public Scene inflateScene(int resource, ViewGroup parent) { |
| 132 | Scene scene = mScenes.get(resource); |
| 133 | if (scene != null) { |
| 134 | return scene; |
| 135 | } |
| 136 | XmlResourceParser parser = mContext.getResources().getXml(resource); |
| 137 | try { |
| 138 | scene = createSceneFromXml(parser, Xml.asAttributeSet(parser), parent); |
| 139 | mScenes.put(resource, scene); |
| 140 | return scene; |
| 141 | } catch (XmlPullParserException e) { |
| 142 | InflateException ex = new InflateException(e.getMessage()); |
| 143 | ex.initCause(e); |
| 144 | throw ex; |
| 145 | } catch (IOException e) { |
| 146 | InflateException ex = new InflateException( |
| 147 | parser.getPositionDescription() |
| 148 | + ": " + e.getMessage()); |
| 149 | ex.initCause(e); |
| 150 | throw ex; |
| 151 | } finally { |
| 152 | parser.close(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | // |
| 158 | // Transition loading |
| 159 | // |
| 160 | |
| 161 | private Transition createTransitionFromXml(XmlPullParser parser, |
| 162 | AttributeSet attrs, TransitionGroup transitionGroup) |
| 163 | throws XmlPullParserException, IOException { |
| 164 | |
| 165 | Transition transition = null; |
| 166 | |
| 167 | // Make sure we are on a start tag. |
| 168 | int type; |
| 169 | int depth = parser.getDepth(); |
| 170 | |
| 171 | while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 172 | && type != XmlPullParser.END_DOCUMENT) { |
| 173 | |
| 174 | boolean newTransition = false; |
| 175 | |
| 176 | if (type != XmlPullParser.START_TAG) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | String name = parser.getName(); |
| 181 | if ("fade".equals(name)) { |
| 182 | transition = new Fade(); |
| 183 | newTransition = true; |
| 184 | } else if ("move".equals(name)) { |
| 185 | transition = new Move(); |
| 186 | newTransition = true; |
| 187 | } else if ("slide".equals(name)) { |
| 188 | transition = new Slide(); |
| 189 | newTransition = true; |
| 190 | } else if ("autoTransition".equals(name)) { |
| 191 | transition = new AutoTransition(); |
| 192 | newTransition = true; |
| 193 | } else if ("recolor".equals(name)) { |
| 194 | transition = new Recolor(); |
| 195 | newTransition = true; |
| 196 | } else if ("transitionGroup".equals(name)) { |
| 197 | transition = new TransitionGroup(); |
| 198 | createTransitionFromXml(parser, attrs, ((TransitionGroup) transition)); |
| 199 | newTransition = true; |
| 200 | } else if ("targets".equals(name)) { |
| 201 | if (parser.getDepth() - 1 > depth && transition != null) { |
| 202 | // We're inside the child tag - add targets to the child |
| 203 | getTargetIDs(parser, attrs, transition); |
| 204 | } else if (parser.getDepth() - 1 == depth && transitionGroup != null) { |
| 205 | // add targets to the group |
| 206 | getTargetIDs(parser, attrs, transitionGroup); |
| 207 | } |
| 208 | } |
| 209 | if (transition != null || "targets".equals(name)) { |
| 210 | if (newTransition) { |
| 211 | loadTransition(transition, attrs); |
| 212 | if (transitionGroup != null) { |
| 213 | transitionGroup.addTransitions(transition); |
| 214 | } |
| 215 | } |
| 216 | } else { |
| 217 | throw new RuntimeException("Unknown scene name: " + parser.getName()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return transition; |
| 222 | } |
| 223 | |
| 224 | private void getTargetIDs(XmlPullParser parser, |
| 225 | AttributeSet attrs, Transition transition) throws XmlPullParserException, IOException { |
| 226 | |
| 227 | // Make sure we are on a start tag. |
| 228 | int type; |
| 229 | int depth = parser.getDepth(); |
| 230 | |
| 231 | ArrayList<Integer> targetIds = new ArrayList<Integer>(); |
| 232 | while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 233 | && type != XmlPullParser.END_DOCUMENT) { |
| 234 | |
| 235 | if (type != XmlPullParser.START_TAG) { |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | String name = parser.getName(); |
| 240 | if (name.equals("target")) { |
| 241 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 242 | com.android.internal.R.styleable.Transition); |
| 243 | int id = a.getResourceId(com.android.internal.R.styleable.Transition_targetID, -1); |
| 244 | if (id >= 0) { |
| 245 | targetIds.add(id); |
| 246 | } |
| 247 | } else { |
| 248 | throw new RuntimeException("Unknown scene name: " + parser.getName()); |
| 249 | } |
| 250 | } |
| 251 | int numTargets = targetIds.size(); |
| 252 | if (numTargets > 0) { |
| 253 | int[] targetsArray = new int[numTargets]; |
| 254 | for (int i = 0; i < targetIds.size(); ++i) { |
| 255 | targetsArray[i] = targetIds.get(i); |
| 256 | } |
| 257 | transition.setTargetIds(targetsArray); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private Transition loadTransition(Transition transition, AttributeSet attrs) |
| 262 | throws Resources.NotFoundException { |
| 263 | |
| 264 | TypedArray a = |
| 265 | mContext.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Transition); |
| 266 | long duration = a.getInt(com.android.internal.R.styleable.Transition_duration, -1); |
| 267 | if (duration >= 0) { |
| 268 | transition.setDuration(duration); |
| 269 | } |
| 270 | long startOffset = a.getInt(com.android.internal.R.styleable.Transition_startOffset, -1); |
| 271 | if (startOffset > 0) { |
| 272 | transition.setStartDelay(startOffset); |
| 273 | } |
| 274 | final int resID = |
| 275 | a.getResourceId(com.android.internal.R.styleable.Animator_interpolator, 0); |
| 276 | if (resID > 0) { |
| 277 | transition.setInterpolator(AnimationUtils.loadInterpolator(mContext, resID)); |
| 278 | } |
| 279 | a.recycle(); |
| 280 | return transition; |
| 281 | } |
| 282 | |
| 283 | // |
| 284 | // TransitionManager loading |
| 285 | // |
| 286 | |
| 287 | private TransitionManager createTransitionManagerFromXml(XmlPullParser parser, |
| 288 | AttributeSet attrs, ViewGroup sceneRoot) throws XmlPullParserException, IOException { |
| 289 | |
| 290 | // Make sure we are on a start tag. |
| 291 | int type; |
| 292 | int depth = parser.getDepth(); |
| 293 | TransitionManager transitionManager = null; |
| 294 | |
| 295 | while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 296 | && type != XmlPullParser.END_DOCUMENT) { |
| 297 | |
| 298 | if (type != XmlPullParser.START_TAG) { |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | String name = parser.getName(); |
| 303 | if (name.equals("transitionManager")) { |
| 304 | transitionManager = new TransitionManager(); |
| 305 | } else if (name.equals("transition") && (transitionManager != null)) { |
| 306 | loadTransition(attrs, sceneRoot, transitionManager); |
| 307 | } else { |
| 308 | throw new RuntimeException("Unknown scene name: " + parser.getName()); |
| 309 | } |
| 310 | } |
| 311 | return transitionManager; |
| 312 | } |
| 313 | |
| 314 | private void loadTransition(AttributeSet attrs, ViewGroup sceneRoot, |
| 315 | TransitionManager transitionManager) |
| 316 | throws Resources.NotFoundException { |
| 317 | |
| 318 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 319 | com.android.internal.R.styleable.TransitionManager); |
| 320 | int transitionId = attrs.getAttributeResourceValue( |
| 321 | com.android.internal.R.styleable.TransitionManager_transition, -1); |
| 322 | Scene fromScene = null, toScene = null; |
| 323 | int fromId = attrs.getAttributeResourceValue( |
| 324 | com.android.internal.R.styleable.TransitionManager_fromScene, -1); |
| 325 | if (fromId >= 0) fromScene = inflateScene(fromId, sceneRoot); |
| 326 | int toId = attrs.getAttributeResourceValue( |
| 327 | com.android.internal.R.styleable.TransitionManager_toScene, -1); |
| 328 | if (toId >= 0) toScene = inflateScene(toId, sceneRoot); |
| 329 | if (transitionId >= 0) { |
| 330 | Transition transition = inflateTransition(transitionId); |
| 331 | if (transition != null) { |
| 332 | if (fromScene != null) { |
| 333 | if (toScene == null){ |
| 334 | throw new RuntimeException("No matching toScene for given fromScene " + |
| 335 | "for transition ID " + transitionId); |
| 336 | } else { |
| 337 | transitionManager.setTransition(fromScene, toScene, transition); |
| 338 | } |
| 339 | } else if (toId >= 0) { |
| 340 | transitionManager.setTransition(toScene, transition); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | a.recycle(); |
| 345 | } |
| 346 | |
| 347 | // |
| 348 | // Scene loading |
| 349 | // |
| 350 | |
| 351 | private Scene createSceneFromXml(XmlPullParser parser, AttributeSet attrs, ViewGroup parent) |
| 352 | throws XmlPullParserException, IOException { |
| 353 | Scene scene = null; |
| 354 | |
| 355 | // Make sure we are on a start tag. |
| 356 | int type; |
| 357 | int depth = parser.getDepth(); |
| 358 | |
| 359 | while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 360 | && type != XmlPullParser.END_DOCUMENT) { |
| 361 | |
| 362 | if (type != XmlPullParser.START_TAG) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | String name = parser.getName(); |
| 367 | if (name.equals("scene")) { |
| 368 | scene = loadScene(attrs, parent); |
| 369 | } else { |
| 370 | throw new RuntimeException("Unknown scene name: " + parser.getName()); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return scene; |
| 375 | } |
| 376 | |
| 377 | private Scene loadScene(AttributeSet attrs, ViewGroup parent) |
| 378 | throws Resources.NotFoundException { |
| 379 | |
| 380 | Scene scene; |
| 381 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 382 | com.android.internal.R.styleable.Scene); |
| 383 | int layoutId = a.getResourceId(com.android.internal.R.styleable.Scene_layout, -1); |
| 384 | if (layoutId >= 0) { |
| 385 | scene = new Scene(parent, layoutId, mContext); |
| 386 | } else { |
| 387 | scene = new Scene(parent); |
| 388 | } |
| 389 | a.recycle(); |
| 390 | return scene; |
| 391 | } |
| 392 | } |