1faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase/*
2faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * Copyright (C) 2013 The Android Open Source Project
3faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase *
4faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * you may not use this file except in compliance with the License.
6faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * You may obtain a copy of the License at
7faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase *
8faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase *
10faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * Unless required by applicable law or agreed to in writing, software
11faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * See the License for the specific language governing permissions and
14faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * limitations under the License.
15faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase */
166ebe3de331efd00ba23bc4191d4a82cfa4c39160Chet Haase
17d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haasepackage android.transition;
18faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
19faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haaseimport android.content.Context;
20d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haaseimport android.util.SparseArray;
21faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haaseimport android.view.LayoutInflater;
22d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haaseimport android.view.View;
23faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haaseimport android.view.ViewGroup;
24faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
25faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase/**
26faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * A scene represents the collection of values that various properties in the
27faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * View hierarchy will have when the scene is applied. A Scene can be
28faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * configured to automatically run a Transition when it is applied, which will
29faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * animate the various property changes that take place during the
30faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase * scene change.
31faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase */
32faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haasepublic final class Scene {
33faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
34faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    private Context mContext;
35faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    private int mLayoutId = -1;
36faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    private ViewGroup mSceneRoot;
3718e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell    private View mLayout; // alternative to layoutId
38faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    Runnable mEnterAction, mExitAction;
39d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase
40d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    /**
41d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * Returns a Scene described by the resource file associated with the given
4240a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell     * <code>layoutId</code> parameter. If such a Scene has already been created for
4340a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell     * the given <code>sceneRoot</code>, that same Scene will be returned.
4440a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell     * This caching of layoutId-based scenes enables sharing of common scenes
4540a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell     * between those created in code and those referenced by {@link TransitionManager}
4640a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell     * XML resource files.
47d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     *
48d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * @param sceneRoot The root of the hierarchy in which scene changes
49d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * and transitions will take place.
50d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * @param layoutId The id of a standard layout resource file.
51d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * @param context The context used in the process of inflating
52d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * the layout resource.
5340a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell     * @return The scene for the given root and layout id
54d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     */
55d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    public static Scene getSceneForLayout(ViewGroup sceneRoot, int layoutId, Context context) {
5640a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell        SparseArray<Scene> scenes = (SparseArray<Scene>) sceneRoot.getTag(
5740a67888f91c2bb94c8654fbfe402f84795d17cfAdam Powell                com.android.internal.R.id.scene_layoutid_cache);
58d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        if (scenes == null) {
59d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase            scenes = new SparseArray<Scene>();
60f3c157721260b3cfc45874fc4f613ba2bd2e7f9eAdam Powell            sceneRoot.setTagInternal(com.android.internal.R.id.scene_layoutid_cache, scenes);
61d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        }
62d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        Scene scene = scenes.get(layoutId);
63d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        if (scene != null) {
64d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase            return scene;
65d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        } else {
66d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase            scene = new Scene(sceneRoot, layoutId, context);
67d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase            scenes.put(layoutId, scene);
68d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase            return scene;
69d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        }
70d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    }
71faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
72faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
73faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Constructs a Scene with no information about how values will change
74faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * when this scene is applied. This constructor might be used when
75faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * a Scene is created with the intention of being dynamically configured,
76faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * through setting {@link #setEnterAction(Runnable)} and possibly
77faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * {@link #setExitAction(Runnable)}.
78faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     *
79faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @param sceneRoot The root of the hierarchy in which scene changes
80faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * and transitions will take place.
81faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
82faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public Scene(ViewGroup sceneRoot) {
83faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mSceneRoot = sceneRoot;
84faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
85faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
86faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
87faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Constructs a Scene which, when entered, will remove any
88faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * children from the sceneRoot container and will inflate and add
89faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * the hierarchy specified by the layoutId resource file.
90faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     *
91d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * <p>This method is hidden because layoutId-based scenes should be
92d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * created by the caching factory method {@link Scene#getCurrentScene(View)}.</p>
93d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     *
94faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @param sceneRoot The root of the hierarchy in which scene changes
95faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * and transitions will take place.
96faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @param layoutId The id of a resource file that defines the view
97faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * hierarchy of this scene.
98faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @param context The context used in the process of inflating
99faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * the layout resource.
100faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
101d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    private Scene(ViewGroup sceneRoot, int layoutId, Context context) {
102faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mContext = context;
103faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mSceneRoot = sceneRoot;
104faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mLayoutId = layoutId;
105faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
106faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
107faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
108faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Constructs a Scene which, when entered, will remove any
109faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * children from the sceneRoot container and add the layout
110faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * object as a new child of that container.
111faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     *
112faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @param sceneRoot The root of the hierarchy in which scene changes
113faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * and transitions will take place.
114d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * @param layout The view hierarchy of this scene, added as a child
115faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * of sceneRoot when this scene is entered.
116faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
11718e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell    public Scene(ViewGroup sceneRoot, View layout) {
11818e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell        mSceneRoot = sceneRoot;
11918e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell        mLayout = layout;
12018e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell    }
12118e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell
12218e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell    /**
123ab42aec4bc0311b2d36aaa85ad73dd91768d7e4cAdam Powell     * @deprecated use {@link #Scene(ViewGroup, View)}.
12418e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell     */
12518e905f42d017c4721d33bd25d7d39ef8d64b5d5Adam Powell    @Deprecated
126faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public Scene(ViewGroup sceneRoot, ViewGroup layout) {
127faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mSceneRoot = sceneRoot;
128faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mLayout = layout;
129faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
130faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
131faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
132faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Gets the root of the scene, which is the root of the view hierarchy
133faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * affected by changes due to this scene, and which will be animated
134faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * when this scene is entered.
135faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     *
136faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @return The root of the view hierarchy affected by this scene.
137faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
138faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public ViewGroup getSceneRoot() {
139faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        return mSceneRoot;
140faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
141faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
142faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
143d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * Exits this scene, if it is the current scene
144d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * on the scene's {@link #getSceneRoot() scene root}. The current scene is
145d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * set when {@link #enter() entering} a scene.
146d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * Exiting a scene runs the {@link #setExitAction(Runnable) exit action}
147faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * if there is one.
148faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
149faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public void exit() {
150d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        if (getCurrentScene(mSceneRoot) == this) {
151faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase            if (mExitAction != null) {
152faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase                mExitAction.run();
153faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase            }
154faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        }
155faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
156faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
157faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
158faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Enters this scene, which entails changing all values that
159faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * are specified by this scene. These may be values associated
160faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * with a layout view group or layout resource file which will
161faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * now be added to the scene root, or it may be values changed by
162faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * an {@link #setEnterAction(Runnable)} enter action}, or a
163faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * combination of the these. No transition will be run when the
164faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * scene is entered. To get transition behavior in scene changes,
165faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * use one of the methods in {@link TransitionManager} instead.
166faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
167faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public void enter() {
168faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
169faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        // Apply layout change, if any
170b7a7fc9d233bad507ce893882352618b13647058Chet Haase        if (mLayoutId > 0 || mLayout != null) {
171d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase            // empty out parent container before adding to it
172faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase            getSceneRoot().removeAllViews();
173faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
174b7a7fc9d233bad507ce893882352618b13647058Chet Haase            if (mLayoutId > 0) {
175faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase                LayoutInflater.from(mContext).inflate(mLayoutId, mSceneRoot);
176faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase            } else {
177faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase                mSceneRoot.addView(mLayout);
178faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase            }
179faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        }
180faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
181faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        // Notify next scene that it is entering. Subclasses may override to configure scene.
182faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        if (mEnterAction != null) {
183faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase            mEnterAction.run();
184faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        }
185faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
186d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        setCurrentScene(mSceneRoot, this);
187d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    }
188d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase
189d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    /**
190d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * Set the scene that the given view is in. The current scene is set only
191d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * on the root view of a scene, not for every view in that hierarchy. This
192d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * information is used by Scene to determine whether there is a previous
193d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * scene which should be exited before the new scene is entered.
194d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     *
195d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * @param view The view on which the current scene is being set
196d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     */
197d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    static void setCurrentScene(View view, Scene scene) {
198d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        view.setTagInternal(com.android.internal.R.id.current_scene, scene);
199d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    }
200d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase
201d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    /**
202d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * Gets the current {@link Scene} set on the given view. A scene is set on a view
203d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * only if that view is the scene root.
204d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     *
205d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * @return The current Scene set on this view. A value of null indicates that
206d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     * no Scene is currently set.
207d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase     */
208d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase    static Scene getCurrentScene(View view) {
209d82c8ac4db7091d2e976af4c89a1734465d20cd2Chet Haase        return (Scene) view.getTag(com.android.internal.R.id.current_scene);
210faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
211faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
212faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
213faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Scenes that are not defined with layout resources or
214faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * hierarchies, or which need to perform additional steps
215faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * after those hierarchies are changed to, should set an enter
216faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * action, and possibly an exit action as well. An enter action
217faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * will cause Scene to call back into application code to do
218faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * anything else the application needs after transitions have
219faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * captured pre-change values and after any other scene changes
220faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * have been applied, such as the layout (if any) being added to
221faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * the view hierarchy. After this method is called, Transitions will
222faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * be played.
223faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     *
224faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @param action The runnable whose {@link Runnable#run() run()} method will
225faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * be called when this scene is entered
226faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @see #setExitAction(Runnable)
227faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @see Scene#Scene(ViewGroup, int, Context)
228faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @see Scene#Scene(ViewGroup, ViewGroup)
229faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
230faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public void setEnterAction(Runnable action) {
231faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mEnterAction = action;
232faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
233faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
234faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    /**
235faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * Scenes that are not defined with layout resources or
236faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * hierarchies, or which need to perform additional steps
237faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * after those hierarchies are changed to, should set an enter
238faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * action, and possibly an exit action as well. An exit action
239faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * will cause Scene to call back into application code to do
240faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * anything the application needs to do after applicable transitions have
241faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * captured pre-change values, but before any other scene changes
242faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * have been applied, such as the new layout (if any) being added to
243faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * the view hierarchy. After this method is called, the next scene
244faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * will be entered, including a call to {@link #setEnterAction(Runnable)}
245faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * if an enter action is set.
246faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     *
247faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @see #setEnterAction(Runnable)
248faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @see Scene#Scene(ViewGroup, int, Context)
249faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     * @see Scene#Scene(ViewGroup, ViewGroup)
250faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase     */
251faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    public void setExitAction(Runnable action) {
252faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase        mExitAction = action;
253faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase    }
254faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase
255b7a7fc9d233bad507ce893882352618b13647058Chet Haase
256b7a7fc9d233bad507ce893882352618b13647058Chet Haase    /**
257b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * Returns whether this Scene was created by a layout resource file, determined
258b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * by the layoutId passed into
259b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * {@link #getSceneForLayout(android.view.ViewGroup, int, android.content.Context)}.
260b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * This is called by TransitionManager to determine whether it is safe for views from
261b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * this scene to be removed from their parents when the scene is exited, which is
262b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * used by {@link Fade} to fade these views out (the views must be removed from
263b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * their parent in order to add them to the overlay for fading purposes). If a
264b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * Scene is not based on a resource file, then the impact of removing views
265b7a7fc9d233bad507ce893882352618b13647058Chet Haase     * arbitrarily is unknown and should be avoided.
266b7a7fc9d233bad507ce893882352618b13647058Chet Haase     */
267b7a7fc9d233bad507ce893882352618b13647058Chet Haase    boolean isCreatedFromLayoutResource() {
268b7a7fc9d233bad507ce893882352618b13647058Chet Haase        return (mLayoutId > 0);
269b7a7fc9d233bad507ce893882352618b13647058Chet Haase    }
270faebd8f0795b7d275fb4e503533c8c0c4a9acc21Chet Haase}