Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/transition/TransitionAnimationHelper.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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 com.android.wm.shell.transition;
18
19 import static android.app.ActivityOptions.ANIM_FROM_STYLE;
20 import static android.app.ActivityOptions.ANIM_NONE;
21 import static android.app.WindowConfiguration.ACTIVITY_TYPE_DREAM;
22 import static android.view.WindowManager.TRANSIT_CLOSE;
23 import static android.view.WindowManager.TRANSIT_OPEN;
24 import static android.view.WindowManager.TRANSIT_TO_BACK;
25 import static android.view.WindowManager.TRANSIT_TO_FRONT;
26 import static android.view.WindowManager.transitTypeToString;
27 import static android.window.TransitionInfo.FLAG_STARTING_WINDOW_TRANSFER_RECIPIENT;
28 import static android.window.TransitionInfo.FLAG_TRANSLUCENT;
29
30 import static com.android.internal.policy.TransitionAnimation.WALLPAPER_TRANSITION_CLOSE;
31 import static com.android.internal.policy.TransitionAnimation.WALLPAPER_TRANSITION_INTRA_CLOSE;
32 import static com.android.internal.policy.TransitionAnimation.WALLPAPER_TRANSITION_INTRA_OPEN;
33 import static com.android.internal.policy.TransitionAnimation.WALLPAPER_TRANSITION_OPEN;
34
35 import android.annotation.ColorInt;
36 import android.annotation.NonNull;
37 import android.annotation.Nullable;
38 import android.graphics.BitmapShader;
39 import android.graphics.Canvas;
40 import android.graphics.Color;
41 import android.graphics.Insets;
42 import android.graphics.Paint;
43 import android.graphics.PixelFormat;
44 import android.graphics.Rect;
45 import android.graphics.Shader;
46 import android.os.SystemProperties;
47 import android.view.Surface;
48 import android.view.SurfaceControl;
49 import android.view.animation.Animation;
50 import android.view.animation.Transformation;
51 import android.window.TransitionInfo;
52
53 import com.android.internal.R;
54 import com.android.internal.policy.TransitionAnimation;
55 import com.android.internal.protolog.common.ProtoLog;
56 import com.android.wm.shell.protolog.ShellProtoLogGroup;
57
58 /** The helper class that provides methods for adding styles to transition animations. */
59 public class TransitionAnimationHelper {
60
61 /**
62 * Restrict ability of activities overriding transition animation in a way such that
63 * an activity can do it only when the transition happens within a same task.
64 *
65 * @see android.app.Activity#overridePendingTransition(int, int)
66 */
67 private static final String DISABLE_CUSTOM_TASK_ANIMATION_PROPERTY =
68 "persist.wm.disable_custom_task_animation";
69
70 /**
71 * @see #DISABLE_CUSTOM_TASK_ANIMATION_PROPERTY
72 */
73 static final boolean sDisableCustomTaskAnimationProperty =
74 SystemProperties.getBoolean(DISABLE_CUSTOM_TASK_ANIMATION_PROPERTY, true);
75
76 /** Loads the animation that is defined through attribute id for the given transition. */
77 @Nullable
78 public static Animation loadAttributeAnimation(@NonNull TransitionInfo info,
79 @NonNull TransitionInfo.Change change, int wallpaperTransit,
80 @NonNull TransitionAnimation transitionAnimation) {
81 final int type = info.getType();
82 final int changeMode = change.getMode();
83 final int changeFlags = change.getFlags();
84 final boolean enter = Transitions.isOpeningType(changeMode);
85 final boolean isTask = change.getTaskInfo() != null;
86 final TransitionInfo.AnimationOptions options = info.getAnimationOptions();
87 final int overrideType = options != null ? options.getType() : ANIM_NONE;
88 final boolean canCustomContainer = !isTask || !sDisableCustomTaskAnimationProperty;
89 final boolean isDream =
90 isTask && change.getTaskInfo().topActivityType == ACTIVITY_TYPE_DREAM;
91 int animAttr = 0;
92 boolean translucent = false;
93 if (isDream) {
94 if (type == TRANSIT_OPEN) {
95 animAttr = enter
96 ? R.styleable.WindowAnimation_dreamActivityOpenEnterAnimation
97 : R.styleable.WindowAnimation_dreamActivityOpenExitAnimation;
98 } else if (type == TRANSIT_CLOSE) {
99 animAttr = enter
100 ? 0
101 : R.styleable.WindowAnimation_dreamActivityCloseExitAnimation;
102 }
103 } else if (wallpaperTransit == WALLPAPER_TRANSITION_INTRA_OPEN) {
104 animAttr = enter
105 ? R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
106 : R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
107 } else if (wallpaperTransit == WALLPAPER_TRANSITION_INTRA_CLOSE) {
108 animAttr = enter
109 ? R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
110 : R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
111 } else if (wallpaperTransit == WALLPAPER_TRANSITION_OPEN) {
112 animAttr = enter
113 ? R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
114 : R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
115 } else if (wallpaperTransit == WALLPAPER_TRANSITION_CLOSE) {
116 animAttr = enter
117 ? R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
118 : R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
119 } else if (type == TRANSIT_OPEN) {
120 // We will translucent open animation for translucent activities and tasks. Choose
121 // WindowAnimation_activityOpenEnterAnimation and set translucent here, then
122 // TransitionAnimation loads appropriate animation later.
123 if ((changeFlags & FLAG_TRANSLUCENT) != 0 && enter) {
124 translucent = true;
125 }
126 if (isTask && !translucent) {
127 animAttr = enter
128 ? R.styleable.WindowAnimation_taskOpenEnterAnimation
129 : R.styleable.WindowAnimation_taskOpenExitAnimation;
130 } else {
131 animAttr = enter
132 ? R.styleable.WindowAnimation_activityOpenEnterAnimation
133 : R.styleable.WindowAnimation_activityOpenExitAnimation;
134 }
135 } else if (type == TRANSIT_TO_FRONT) {
136 animAttr = enter
137 ? R.styleable.WindowAnimation_taskToFrontEnterAnimation
138 : R.styleable.WindowAnimation_taskToFrontExitAnimation;
139 } else if (type == TRANSIT_CLOSE) {
140 if (isTask) {
141 animAttr = enter
142 ? R.styleable.WindowAnimation_taskCloseEnterAnimation
143 : R.styleable.WindowAnimation_taskCloseExitAnimation;
144 } else {
145 if ((changeFlags & FLAG_TRANSLUCENT) != 0 && !enter) {
146 translucent = true;
147 }
148 animAttr = enter
149 ? R.styleable.WindowAnimation_activityCloseEnterAnimation
150 : R.styleable.WindowAnimation_activityCloseExitAnimation;
151 }
152 } else if (type == TRANSIT_TO_BACK) {
153 animAttr = enter
154 ? R.styleable.WindowAnimation_taskToBackEnterAnimation
155 : R.styleable.WindowAnimation_taskToBackExitAnimation;
156 }
157
158 Animation a = null;
159 if (animAttr != 0) {
160 if (overrideType == ANIM_FROM_STYLE && canCustomContainer) {
161 a = transitionAnimation
162 .loadAnimationAttr(options.getPackageName(), options.getAnimations(),
163 animAttr, translucent);
164 } else {
165 a = transitionAnimation.loadDefaultAnimationAttr(animAttr, translucent);
166 }
167 }
168
169 ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS,
170 "loadAnimation: anim=%s animAttr=0x%x type=%s isEntrance=%b", a, animAttr,
171 transitTypeToString(type),
172 enter);
173 return a;
174 }
175
176 /**
177 * Gets the background {@link ColorInt} for the given transition animation if it is set.
178 *
179 * @param defaultColor {@link ColorInt} to return if there is no background color specified by
180 * the given transition animation.
181 */
182 @ColorInt
183 public static int getTransitionBackgroundColorIfSet(@NonNull TransitionInfo info,
184 @NonNull TransitionInfo.Change change, @NonNull Animation a,
185 @ColorInt int defaultColor) {
186 if (!a.getShowBackdrop()) {
187 return defaultColor;
188 }
189 if (info.getAnimationOptions() != null
190 && info.getAnimationOptions().getBackgroundColor() != 0) {
191 // If available use the background color provided through AnimationOptions
192 return info.getAnimationOptions().getBackgroundColor();
193 } else if (a.getBackdropColor() != 0) {
194 // Otherwise fallback on the background color provided through the animation
195 // definition.
196 return a.getBackdropColor();
197 } else if (change.getBackgroundColor() != 0) {
198 // Otherwise default to the window's background color if provided through
199 // the theme as the background color for the animation - the top most window
200 // with a valid background color and showBackground set takes precedence.
201 return change.getBackgroundColor();
202 }
203 return defaultColor;
204 }
205
206 /**
207 * Adds the given {@code backgroundColor} as the background color to the transition animation.
208 */
209 public static void addBackgroundToTransition(@NonNull SurfaceControl rootLeash,
210 @ColorInt int backgroundColor, @NonNull SurfaceControl.Transaction startTransaction,
211 @NonNull SurfaceControl.Transaction finishTransaction) {
212 if (backgroundColor == 0) {
213 // No background color.
214 return;
215 }
216 final Color bgColor = Color.valueOf(backgroundColor);
217 final float[] colorArray = new float[] { bgColor.red(), bgColor.green(), bgColor.blue() };
218 final SurfaceControl animationBackgroundSurface = new SurfaceControl.Builder()
219 .setName("Animation Background")
220 .setParent(rootLeash)
221 .setColorLayer()
222 .setOpaque(true)
223 .build();
224 startTransaction
225 .setLayer(animationBackgroundSurface, Integer.MIN_VALUE)
226 .setColor(animationBackgroundSurface, colorArray)
227 .show(animationBackgroundSurface);
228 finishTransaction.remove(animationBackgroundSurface);
229 }
230
231 /**
232 * Adds edge extension surface to the given {@code change} for edge extension animation.
233 */
234 public static void edgeExtendWindow(@NonNull TransitionInfo.Change change,
235 @NonNull Animation a, @NonNull SurfaceControl.Transaction startTransaction,
236 @NonNull SurfaceControl.Transaction finishTransaction) {
237 // Do not create edge extension surface for transfer starting window change.
238 // The app surface could be empty thus nothing can draw on the hardware renderer, which will
239 // block this thread when calling Surface#unlockCanvasAndPost.
240 if ((change.getFlags() & FLAG_STARTING_WINDOW_TRANSFER_RECIPIENT) != 0) {
241 return;
242 }
243 final Transformation transformationAtStart = new Transformation();
244 a.getTransformationAt(0, transformationAtStart);
245 final Transformation transformationAtEnd = new Transformation();
246 a.getTransformationAt(1, transformationAtEnd);
247
248 // We want to create an extension surface that is the maximal size and the animation will
249 // take care of cropping any part that overflows.
250 final Insets maxExtensionInsets = Insets.min(
251 transformationAtStart.getInsets(), transformationAtEnd.getInsets());
252
253 final int targetSurfaceHeight = Math.max(change.getStartAbsBounds().height(),
254 change.getEndAbsBounds().height());
255 final int targetSurfaceWidth = Math.max(change.getStartAbsBounds().width(),
256 change.getEndAbsBounds().width());
257 if (maxExtensionInsets.left < 0) {
258 final Rect edgeBounds = new Rect(0, 0, 1, targetSurfaceHeight);
259 final Rect extensionRect = new Rect(0, 0,
260 -maxExtensionInsets.left, targetSurfaceHeight);
261 final int xPos = maxExtensionInsets.left;
262 final int yPos = 0;
263 createExtensionSurface(change.getLeash(), edgeBounds, extensionRect, xPos, yPos,
264 "Left Edge Extension", startTransaction, finishTransaction);
265 }
266
267 if (maxExtensionInsets.top < 0) {
268 final Rect edgeBounds = new Rect(0, 0, targetSurfaceWidth, 1);
269 final Rect extensionRect = new Rect(0, 0,
270 targetSurfaceWidth, -maxExtensionInsets.top);
271 final int xPos = 0;
272 final int yPos = maxExtensionInsets.top;
273 createExtensionSurface(change.getLeash(), edgeBounds, extensionRect, xPos, yPos,
274 "Top Edge Extension", startTransaction, finishTransaction);
275 }
276
277 if (maxExtensionInsets.right < 0) {
278 final Rect edgeBounds = new Rect(targetSurfaceWidth - 1, 0,
279 targetSurfaceWidth, targetSurfaceHeight);
280 final Rect extensionRect = new Rect(0, 0,
281 -maxExtensionInsets.right, targetSurfaceHeight);
282 final int xPos = targetSurfaceWidth;
283 final int yPos = 0;
284 createExtensionSurface(change.getLeash(), edgeBounds, extensionRect, xPos, yPos,
285 "Right Edge Extension", startTransaction, finishTransaction);
286 }
287
288 if (maxExtensionInsets.bottom < 0) {
289 final Rect edgeBounds = new Rect(0, targetSurfaceHeight - 1,
290 targetSurfaceWidth, targetSurfaceHeight);
291 final Rect extensionRect = new Rect(0, 0,
292 targetSurfaceWidth, -maxExtensionInsets.bottom);
293 final int xPos = maxExtensionInsets.left;
294 final int yPos = targetSurfaceHeight;
295 createExtensionSurface(change.getLeash(), edgeBounds, extensionRect, xPos, yPos,
296 "Bottom Edge Extension", startTransaction, finishTransaction);
297 }
298 }
299
300 /**
301 * Takes a screenshot of {@code surfaceToExtend}'s edge and extends it for edge extension
302 * animation.
303 */
304 private static SurfaceControl createExtensionSurface(@NonNull SurfaceControl surfaceToExtend,
305 @NonNull Rect edgeBounds, @NonNull Rect extensionRect, int xPos, int yPos,
306 @NonNull String layerName, @NonNull SurfaceControl.Transaction startTransaction,
307 @NonNull SurfaceControl.Transaction finishTransaction) {
308 final SurfaceControl edgeExtensionLayer = new SurfaceControl.Builder()
309 .setName(layerName)
310 .setParent(surfaceToExtend)
311 .setHidden(true)
312 .setCallsite("TransitionAnimationHelper#createExtensionSurface")
313 .setOpaque(true)
314 .setBufferSize(extensionRect.width(), extensionRect.height())
315 .build();
316
317 final SurfaceControl.LayerCaptureArgs captureArgs =
318 new SurfaceControl.LayerCaptureArgs.Builder(surfaceToExtend)
319 .setSourceCrop(edgeBounds)
320 .setFrameScale(1)
321 .setPixelFormat(PixelFormat.RGBA_8888)
322 .setChildrenOnly(true)
323 .setAllowProtected(true)
324 .setCaptureSecureLayers(true)
325 .build();
326 final SurfaceControl.ScreenshotHardwareBuffer edgeBuffer =
327 SurfaceControl.captureLayers(captureArgs);
328
329 if (edgeBuffer == null) {
330 ProtoLog.e(ShellProtoLogGroup.WM_SHELL_TRANSITIONS,
331 "Failed to capture edge of window.");
332 return null;
333 }
334
335 final BitmapShader shader = new BitmapShader(edgeBuffer.asBitmap(),
336 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
337 final Paint paint = new Paint();
338 paint.setShader(shader);
339
340 final Surface surface = new Surface(edgeExtensionLayer);
341 final Canvas c = surface.lockHardwareCanvas();
342 c.drawRect(extensionRect, paint);
343 surface.unlockCanvasAndPost(c);
344 surface.release();
345
346 startTransaction.setLayer(edgeExtensionLayer, Integer.MIN_VALUE);
347 startTransaction.setPosition(edgeExtensionLayer, xPos, yPos);
348 startTransaction.setVisibility(edgeExtensionLayer, true);
349 finishTransaction.remove(edgeExtensionLayer);
350
351 return edgeExtensionLayer;
352 }
353 }