Debian Codegraph @

core/java/android/transition/Scene.java

raw symbols SCIP
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
17 package android.transition;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.content.Context;
23 import android.os.Build;
24 import android.util.SparseArray;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28
29 /**
30 * A scene represents the collection of values that various properties in the
31 * View hierarchy will have when the scene is applied. A Scene can be
32 * configured to automatically run a Transition when it is applied, which will
33 * animate the various property changes that take place during the
34 * scene change.
35 */
36 public final class Scene {
37
38 private Context mContext;
39 private int mLayoutId = -1;
40 private ViewGroup mSceneRoot;
41 private View mLayout; // alternative to layoutId
42 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
43 Runnable mEnterAction;
44 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
45 Runnable mExitAction;
46
47 /**
48 * Returns a Scene described by the resource file associated with the given
49 * <code>layoutId</code> parameter. If such a Scene has already been created for
50 * the given <code>sceneRoot</code>, that same Scene will be returned.
51 * This caching of layoutId-based scenes enables sharing of common scenes
52 * between those created in code and those referenced by {@link TransitionManager}
53 * XML resource files.
54 *
55 * @param sceneRoot The root of the hierarchy in which scene changes
56 * and transitions will take place.
57 * @param layoutId The id of a standard layout resource file.
58 * @param context The context used in the process of inflating
59 * the layout resource.
60 * @return The scene for the given root and layout id
61 */
62 public static Scene getSceneForLayout(ViewGroup sceneRoot, int layoutId, Context context) {
63 SparseArray<Scene> scenes = (SparseArray<Scene>) sceneRoot.getTag(
64 com.android.internal.R.id.scene_layoutid_cache);
65 if (scenes == null) {
66 scenes = new SparseArray<Scene>();
67 sceneRoot.setTagInternal(com.android.internal.R.id.scene_layoutid_cache, scenes);
68 }
69 Scene scene = scenes.get(layoutId);
70 if (scene != null) {
71 return scene;
72 } else {
73 scene = new Scene(sceneRoot, layoutId, context);
74 scenes.put(layoutId, scene);
75 return scene;
76 }
77 }
78
79 /**
80 * Constructs a Scene with no information about how values will change
81 * when this scene is applied. This constructor might be used when
82 * a Scene is created with the intention of being dynamically configured,
83 * through setting {@link #setEnterAction(Runnable)} and possibly
84 * {@link #setExitAction(Runnable)}.
85 *
86 * @param sceneRoot The root of the hierarchy in which scene changes
87 * and transitions will take place.
88 */
89 public Scene(ViewGroup sceneRoot) {
90 mSceneRoot = sceneRoot;
91 }
92
93 /**
94 * Constructs a Scene which, when entered, will remove any
95 * children from the sceneRoot container and will inflate and add
96 * the hierarchy specified by the layoutId resource file.
97 *
98 * <p>This method is hidden because layoutId-based scenes should be
99 * created by the caching factory method {@link Scene#getCurrentScene(ViewGroup)}.</p>
100 *
101 * @param sceneRoot The root of the hierarchy in which scene changes
102 * and transitions will take place.
103 * @param layoutId The id of a resource file that defines the view
104 * hierarchy of this scene.
105 * @param context The context used in the process of inflating
106 * the layout resource.
107 */
108 private Scene(ViewGroup sceneRoot, int layoutId, Context context) {
109 mContext = context;
110 mSceneRoot = sceneRoot;
111 mLayoutId = layoutId;
112 }
113
114 /**
115 * Constructs a Scene which, when entered, will remove any
116 * children from the sceneRoot container and add the layout
117 * object as a new child of that container.
118 *
119 * @param sceneRoot The root of the hierarchy in which scene changes
120 * and transitions will take place.
121 * @param layout The view hierarchy of this scene, added as a child
122 * of sceneRoot when this scene is entered.
123 */
124 public Scene(ViewGroup sceneRoot, View layout) {
125 mSceneRoot = sceneRoot;
126 mLayout = layout;
127 }
128
129 /**
130 * @deprecated use {@link #Scene(ViewGroup, View)}.
131 */
132 @Deprecated
133 public Scene(ViewGroup sceneRoot, ViewGroup layout) {
134 mSceneRoot = sceneRoot;
135 mLayout = layout;
136 }
137
138 /**
139 * Gets the root of the scene, which is the root of the view hierarchy
140 * affected by changes due to this scene, and which will be animated
141 * when this scene is entered.
142 *
143 * @return The root of the view hierarchy affected by this scene.
144 */
145 public ViewGroup getSceneRoot() {
146 return mSceneRoot;
147 }
148
149 /**
150 * Exits this scene, if it is the current scene
151 * on the scene's {@link #getSceneRoot() scene root}. The current scene is
152 * set when {@link #enter() entering} a scene.
153 * Exiting a scene runs the {@link #setExitAction(Runnable) exit action}
154 * if there is one.
155 */
156 public void exit() {
157 if (getCurrentScene(mSceneRoot) == this) {
158 if (mExitAction != null) {
159 mExitAction.run();
160 }
161 }
162 }
163
164 /**
165 * Enters this scene, which entails changing all values that
166 * are specified by this scene. These may be values associated
167 * with a layout view group or layout resource file which will
168 * now be added to the scene root, or it may be values changed by
169 * an {@link #setEnterAction(Runnable)} enter action}, or a
170 * combination of the these. No transition will be run when the
171 * scene is entered. To get transition behavior in scene changes,
172 * use one of the methods in {@link TransitionManager} instead.
173 */
174 public void enter() {
175
176 // Apply layout change, if any
177 if (mLayoutId > 0 || mLayout != null) {
178 // empty out parent container before adding to it
179 getSceneRoot().removeAllViews();
180
181 if (mLayoutId > 0) {
182 LayoutInflater.from(mContext).inflate(mLayoutId, mSceneRoot);
183 } else {
184 mSceneRoot.addView(mLayout);
185 }
186 }
187
188 // Notify next scene that it is entering. Subclasses may override to configure scene.
189 if (mEnterAction != null) {
190 mEnterAction.run();
191 }
192
193 setCurrentScene(mSceneRoot, this);
194 }
195
196 /**
197 * Set the scene that the given ViewGroup is in. The current scene is set only
198 * on the root ViewGroup of a scene, not for every view in that hierarchy. This
199 * information is used by Scene to determine whether there is a previous
200 * scene which should be exited before the new scene is entered.
201 *
202 * @param sceneRoot The ViewGroup on which the current scene is being set
203 */
204 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
205 static void setCurrentScene(@NonNull ViewGroup sceneRoot, @Nullable Scene scene) {
206 sceneRoot.setTagInternal(com.android.internal.R.id.current_scene, scene);
207 }
208
209 /**
210 * Gets the current {@link Scene} set on the given ViewGroup. A scene is set on a ViewGroup
211 * only if that ViewGroup is the scene root.
212 *
213 * @param sceneRoot The ViewGroup on which the current scene will be returned
214 * @return The current Scene set on this ViewGroup. A value of null indicates that
215 * no Scene is currently set.
216 */
217 @Nullable
218 public static Scene getCurrentScene(@NonNull ViewGroup sceneRoot) {
219 return (Scene) sceneRoot.getTag(com.android.internal.R.id.current_scene);
220 }
221
222 /**
223 * Scenes that are not defined with layout resources or
224 * hierarchies, or which need to perform additional steps
225 * after those hierarchies are changed to, should set an enter
226 * action, and possibly an exit action as well. An enter action
227 * will cause Scene to call back into application code to do
228 * anything else the application needs after transitions have
229 * captured pre-change values and after any other scene changes
230 * have been applied, such as the layout (if any) being added to
231 * the view hierarchy. After this method is called, Transitions will
232 * be played.
233 *
234 * @param action The runnable whose {@link Runnable#run() run()} method will
235 * be called when this scene is entered
236 * @see #setExitAction(Runnable)
237 * @see Scene#Scene(ViewGroup, View)
238 */
239 public void setEnterAction(Runnable action) {
240 mEnterAction = action;
241 }
242
243 /**
244 * Scenes that are not defined with layout resources or
245 * hierarchies, or which need to perform additional steps
246 * after those hierarchies are changed to, should set an enter
247 * action, and possibly an exit action as well. An exit action
248 * will cause Scene to call back into application code to do
249 * anything the application needs to do after applicable transitions have
250 * captured pre-change values, but before any other scene changes
251 * have been applied, such as the new layout (if any) being added to
252 * the view hierarchy. After this method is called, the next scene
253 * will be entered, including a call to {@link #setEnterAction(Runnable)}
254 * if an enter action is set.
255 *
256 * @see #setEnterAction(Runnable)
257 * @see Scene#Scene(ViewGroup, View)
258 */
259 public void setExitAction(Runnable action) {
260 mExitAction = action;
261 }
262
263
264 /**
265 * Returns whether this Scene was created by a layout resource file, determined
266 * by the layoutId passed into
267 * {@link #getSceneForLayout(android.view.ViewGroup, int, android.content.Context)}.
268 * This is called by TransitionManager to determine whether it is safe for views from
269 * this scene to be removed from their parents when the scene is exited, which is
270 * used by {@link Fade} to fade these views out (the views must be removed from
271 * their parent in order to add them to the overlay for fading purposes). If a
272 * Scene is not based on a resource file, then the impact of removing views
273 * arbitrarily is unknown and should be avoided.
274 */
275 boolean isCreatedFromLayoutResource() {
276 return (mLayoutId > 0);
277 }
278 }