Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/activityembedding/ActivityEmbeddingAnimationSpec.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.activityembedding;
18
19
20 import static com.android.internal.policy.TransitionAnimation.WALLPAPER_TRANSITION_NONE;
21 import static com.android.wm.shell.transition.TransitionAnimationHelper.loadAttributeAnimation;
22
23 import android.content.Context;
24 import android.graphics.Rect;
25 import android.view.animation.AlphaAnimation;
26 import android.view.animation.Animation;
27 import android.view.animation.AnimationSet;
28 import android.view.animation.AnimationUtils;
29 import android.view.animation.ClipRectAnimation;
30 import android.view.animation.Interpolator;
31 import android.view.animation.LinearInterpolator;
32 import android.view.animation.ScaleAnimation;
33 import android.view.animation.TranslateAnimation;
34 import android.window.TransitionInfo;
35
36 import androidx.annotation.NonNull;
37
38 import com.android.internal.policy.TransitionAnimation;
39 import com.android.wm.shell.transition.Transitions;
40
41 /** Animation spec for ActivityEmbedding transition. */
42 // TODO(b/206557124): provide an easier way to customize animation
43 class ActivityEmbeddingAnimationSpec {
44
45 private static final String TAG = "ActivityEmbeddingAnimSpec";
46 private static final int CHANGE_ANIMATION_DURATION = 517;
47 private static final int CHANGE_ANIMATION_FADE_DURATION = 80;
48 private static final int CHANGE_ANIMATION_FADE_OFFSET = 30;
49
50 private final Context mContext;
51 private final TransitionAnimation mTransitionAnimation;
52 private final Interpolator mFastOutExtraSlowInInterpolator;
53 private final LinearInterpolator mLinearInterpolator;
54 private float mTransitionAnimationScaleSetting;
55
56 ActivityEmbeddingAnimationSpec(@NonNull Context context) {
57 mContext = context;
58 mTransitionAnimation = new TransitionAnimation(mContext, false /* debug */, TAG);
59 mFastOutExtraSlowInInterpolator = AnimationUtils.loadInterpolator(
60 mContext, android.R.interpolator.fast_out_extra_slow_in);
61 mLinearInterpolator = new LinearInterpolator();
62 }
63
64 /**
65 * Sets transition animation scale settings value.
66 * @param scale The setting value of transition animation scale.
67 */
68 void setAnimScaleSetting(float scale) {
69 mTransitionAnimationScaleSetting = scale;
70 }
71
72 /** For window that doesn't need to be animated. */
73 @NonNull
74 static Animation createNoopAnimation(@NonNull TransitionInfo.Change change) {
75 // Noop but just keep the window showing/hiding.
76 final float alpha = Transitions.isClosingType(change.getMode()) ? 0f : 1f;
77 return new AlphaAnimation(alpha, alpha);
78 }
79
80 /** Animation for window that is opening in a change transition. */
81 @NonNull
82 Animation createChangeBoundsOpenAnimation(@NonNull TransitionInfo.Change change,
83 @NonNull Rect parentBounds) {
84 // Use end bounds for opening.
85 final Rect bounds = change.getEndAbsBounds();
86 final int startLeft;
87 final int startTop;
88 if (parentBounds.top == bounds.top && parentBounds.bottom == bounds.bottom) {
89 // The window will be animated in from left or right depending on its position.
90 startTop = 0;
91 startLeft = parentBounds.left == bounds.left ? -bounds.width() : bounds.width();
92 } else {
93 // The window will be animated in from top or bottom depending on its position.
94 startTop = parentBounds.top == bounds.top ? -bounds.height() : bounds.height();
95 startLeft = 0;
96 }
97
98 // The position should be 0-based as we will post translate in
99 // ActivityEmbeddingAnimationAdapter#onAnimationUpdate
100 final Animation animation = new TranslateAnimation(startLeft, 0, startTop, 0);
101 animation.setInterpolator(mFastOutExtraSlowInInterpolator);
102 animation.setDuration(CHANGE_ANIMATION_DURATION);
103 animation.initialize(bounds.width(), bounds.height(), bounds.width(), bounds.height());
104 animation.scaleCurrentDuration(mTransitionAnimationScaleSetting);
105 return animation;
106 }
107
108 /** Animation for window that is closing in a change transition. */
109 @NonNull
110 Animation createChangeBoundsCloseAnimation(@NonNull TransitionInfo.Change change,
111 @NonNull Rect parentBounds) {
112 // Use start bounds for closing.
113 final Rect bounds = change.getStartAbsBounds();
114 final int endTop;
115 final int endLeft;
116 if (parentBounds.top == bounds.top && parentBounds.bottom == bounds.bottom) {
117 // The window will be animated out to left or right depending on its position.
118 endTop = 0;
119 endLeft = parentBounds.left == bounds.left ? -bounds.width() : bounds.width();
120 } else {
121 // The window will be animated out to top or bottom depending on its position.
122 endTop = parentBounds.top == bounds.top ? -bounds.height() : bounds.height();
123 endLeft = 0;
124 }
125
126 // The position should be 0-based as we will post translate in
127 // ActivityEmbeddingAnimationAdapter#onAnimationUpdate
128 final Animation animation = new TranslateAnimation(0, endLeft, 0, endTop);
129 animation.setInterpolator(mFastOutExtraSlowInInterpolator);
130 animation.setDuration(CHANGE_ANIMATION_DURATION);
131 animation.initialize(bounds.width(), bounds.height(), bounds.width(), bounds.height());
132 animation.scaleCurrentDuration(mTransitionAnimationScaleSetting);
133 return animation;
134 }
135
136 /**
137 * Animation for window that is changing (bounds change) in a change transition.
138 * @return the return array always has two elements. The first one is for the start leash, and
139 * the second one is for the end leash.
140 */
141 @NonNull
142 Animation[] createChangeBoundsChangeAnimations(@NonNull TransitionInfo.Change change,
143 @NonNull Rect parentBounds) {
144 // Both start bounds and end bounds are in screen coordinates. We will post translate
145 // to the local coordinates in ActivityEmbeddingAnimationAdapter#onAnimationUpdate
146 final Rect startBounds = change.getStartAbsBounds();
147 final Rect endBounds = change.getEndAbsBounds();
148 float scaleX = ((float) startBounds.width()) / endBounds.width();
149 float scaleY = ((float) startBounds.height()) / endBounds.height();
150 // Start leash is a child of the end leash. Reverse the scale so that the start leash won't
151 // be scaled up with its parent.
152 float startScaleX = 1.f / scaleX;
153 float startScaleY = 1.f / scaleY;
154
155 // The start leash will be fade out.
156 final AnimationSet startSet = new AnimationSet(false /* shareInterpolator */);
157 final Animation startAlpha = new AlphaAnimation(1f, 0f);
158 startAlpha.setInterpolator(mLinearInterpolator);
159 startAlpha.setDuration(CHANGE_ANIMATION_FADE_DURATION);
160 startAlpha.setStartOffset(CHANGE_ANIMATION_FADE_OFFSET);
161 startSet.addAnimation(startAlpha);
162 final Animation startScale = new ScaleAnimation(startScaleX, startScaleX, startScaleY,
163 startScaleY);
164 startScale.setInterpolator(mFastOutExtraSlowInInterpolator);
165 startScale.setDuration(CHANGE_ANIMATION_DURATION);
166 startSet.addAnimation(startScale);
167 startSet.initialize(startBounds.width(), startBounds.height(), endBounds.width(),
168 endBounds.height());
169 startSet.scaleCurrentDuration(mTransitionAnimationScaleSetting);
170
171 // The end leash will be moved into the end position while scaling.
172 final AnimationSet endSet = new AnimationSet(true /* shareInterpolator */);
173 endSet.setInterpolator(mFastOutExtraSlowInInterpolator);
174 final Animation endScale = new ScaleAnimation(scaleX, 1, scaleY, 1);
175 endScale.setDuration(CHANGE_ANIMATION_DURATION);
176 endSet.addAnimation(endScale);
177 // The position should be 0-based as we will post translate in
178 // ActivityEmbeddingAnimationAdapter#onAnimationUpdate
179 final Animation endTranslate = new TranslateAnimation(startBounds.left - endBounds.left, 0,
180 startBounds.top - endBounds.top, 0);
181 endTranslate.setDuration(CHANGE_ANIMATION_DURATION);
182 endSet.addAnimation(endTranslate);
183 // The end leash is resizing, we should update the window crop based on the clip rect.
184 final Rect startClip = new Rect(startBounds);
185 final Rect endClip = new Rect(endBounds);
186 startClip.offsetTo(0, 0);
187 endClip.offsetTo(0, 0);
188 final Animation clipAnim = new ClipRectAnimation(startClip, endClip);
189 clipAnim.setDuration(CHANGE_ANIMATION_DURATION);
190 endSet.addAnimation(clipAnim);
191 endSet.initialize(startBounds.width(), startBounds.height(), parentBounds.width(),
192 parentBounds.height());
193 endSet.scaleCurrentDuration(mTransitionAnimationScaleSetting);
194
195 return new Animation[]{startSet, endSet};
196 }
197
198 @NonNull
199 Animation loadOpenAnimation(@NonNull TransitionInfo info,
200 @NonNull TransitionInfo.Change change, @NonNull Rect wholeAnimationBounds) {
201 final boolean isEnter = Transitions.isOpeningType(change.getMode());
202 final Animation animation;
203 if (shouldShowBackdrop(info, change)) {
204 animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter
205 ? com.android.internal.R.anim.task_fragment_clear_top_open_enter
206 : com.android.internal.R.anim.task_fragment_clear_top_open_exit);
207 } else {
208 // Use the same edge extension animation as regular activity open.
209 animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter
210 ? com.android.internal.R.anim.activity_open_enter
211 : com.android.internal.R.anim.activity_open_exit);
212 }
213 // Use the whole animation bounds instead of the change bounds, so that when multiple change
214 // targets are opening at the same time, the animation applied to each will be the same.
215 // Otherwise, we may see gap between the activities that are launching together.
216 animation.initialize(wholeAnimationBounds.width(), wholeAnimationBounds.height(),
217 wholeAnimationBounds.width(), wholeAnimationBounds.height());
218 animation.scaleCurrentDuration(mTransitionAnimationScaleSetting);
219 return animation;
220 }
221
222 @NonNull
223 Animation loadCloseAnimation(@NonNull TransitionInfo info,
224 @NonNull TransitionInfo.Change change, @NonNull Rect wholeAnimationBounds) {
225 final boolean isEnter = Transitions.isOpeningType(change.getMode());
226 final Animation animation;
227 if (shouldShowBackdrop(info, change)) {
228 animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter
229 ? com.android.internal.R.anim.task_fragment_clear_top_close_enter
230 : com.android.internal.R.anim.task_fragment_clear_top_close_exit);
231 } else {
232 // Use the same edge extension animation as regular activity close.
233 animation = mTransitionAnimation.loadDefaultAnimationRes(isEnter
234 ? com.android.internal.R.anim.activity_close_enter
235 : com.android.internal.R.anim.activity_close_exit);
236 }
237 // Use the whole animation bounds instead of the change bounds, so that when multiple change
238 // targets are closing at the same time, the animation applied to each will be the same.
239 // Otherwise, we may see gap between the activities that are finishing together.
240 animation.initialize(wholeAnimationBounds.width(), wholeAnimationBounds.height(),
241 wholeAnimationBounds.width(), wholeAnimationBounds.height());
242 animation.scaleCurrentDuration(mTransitionAnimationScaleSetting);
243 return animation;
244 }
245
246 private boolean shouldShowBackdrop(@NonNull TransitionInfo info,
247 @NonNull TransitionInfo.Change change) {
248 final Animation a = loadAttributeAnimation(info, change, WALLPAPER_TRANSITION_NONE,
249 mTransitionAnimation);
250 return a != null && a.getShowBackdrop();
251 }
252 }