Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/compatui/DialogAnimationController.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.compatui;
18
19 import static com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
20 import static com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
21 import static com.android.wm.shell.transition.Transitions.ENABLE_SHELL_TRANSITIONS;
22
23 import android.animation.Animator;
24 import android.animation.AnimatorListenerAdapter;
25 import android.animation.ObjectAnimator;
26 import android.annotation.AnyRes;
27 import android.annotation.NonNull;
28 import android.annotation.Nullable;
29 import android.content.Context;
30 import android.graphics.drawable.Drawable;
31 import android.util.IntProperty;
32 import android.util.Log;
33 import android.util.Property;
34 import android.view.ContextThemeWrapper;
35 import android.view.View;
36 import android.view.animation.Animation;
37
38 import com.android.internal.policy.TransitionAnimation;
39
40 /**
41 * Controls the enter/exit a dialog.
42 *
43 * @param <T> The {@link DialogContainerSupplier} to use
44 */
45 public class DialogAnimationController<T extends DialogContainerSupplier> {
46
47 // The alpha of a background is a number between 0 (fully transparent) to 255 (fully opaque).
48 // 204 is simply 255 * 0.8.
49 static final int BACKGROUND_DIM_ALPHA = 204;
50
51 // If shell transitions are enabled, startEnterAnimation will be called after all transitions
52 // have finished, and therefore the start delay should be shorter.
53 private static final int ENTER_ANIM_START_DELAY_MILLIS = ENABLE_SHELL_TRANSITIONS ? 300 : 500;
54
55 private final TransitionAnimation mTransitionAnimation;
56 private final String mPackageName;
57 private final String mTag;
58 @AnyRes
59 private final int mAnimStyleResId;
60
61 @Nullable
62 private Animation mDialogAnimation;
63 @Nullable
64 private Animator mBackgroundDimAnimator;
65
66 public DialogAnimationController(Context context, String tag) {
67 mTransitionAnimation = new TransitionAnimation(context, /* debug= */ false, tag);
68 mAnimStyleResId = (new ContextThemeWrapper(context,
69 android.R.style.ThemeOverlay_Material_Dialog).getTheme()).obtainStyledAttributes(
70 com.android.internal.R.styleable.Window).getResourceId(
71 com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
72 mPackageName = context.getPackageName();
73 mTag = tag;
74 }
75
76 /**
77 * Starts both background dim fade-in animation and the dialog enter animation.
78 */
79 public void startEnterAnimation(@NonNull T layout, Runnable endCallback) {
80 // Cancel any previous animation if it's still running.
81 cancelAnimation();
82
83 final View dialogContainer = layout.getDialogContainerView();
84 mDialogAnimation = loadAnimation(WindowAnimation_windowEnterAnimation);
85 if (mDialogAnimation == null) {
86 endCallback.run();
87 return;
88 }
89 mDialogAnimation.setAnimationListener(getAnimationListener(
90 /* startCallback= */ () -> dialogContainer.setAlpha(1),
91 /* endCallback= */ () -> {
92 mDialogAnimation = null;
93 endCallback.run();
94 }));
95
96 mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDimDrawable(),
97 /* endAlpha= */ BACKGROUND_DIM_ALPHA,
98 mDialogAnimation.getDuration());
99 mBackgroundDimAnimator.addListener(getDimAnimatorListener());
100
101 mDialogAnimation.setStartOffset(ENTER_ANIM_START_DELAY_MILLIS);
102 mBackgroundDimAnimator.setStartDelay(ENTER_ANIM_START_DELAY_MILLIS);
103
104 dialogContainer.startAnimation(mDialogAnimation);
105 mBackgroundDimAnimator.start();
106 }
107
108 /**
109 * Starts both the background dim fade-out animation and the dialog exit animation.
110 */
111 public void startExitAnimation(@NonNull T layout, Runnable endCallback) {
112 // Cancel any previous animation if it's still running.
113 cancelAnimation();
114
115 final View dialogContainer = layout.getDialogContainerView();
116 mDialogAnimation = loadAnimation(WindowAnimation_windowExitAnimation);
117 if (mDialogAnimation == null) {
118 endCallback.run();
119 return;
120 }
121 mDialogAnimation.setAnimationListener(getAnimationListener(
122 /* startCallback= */ () -> {},
123 /* endCallback= */ () -> {
124 dialogContainer.setAlpha(0);
125 mDialogAnimation = null;
126 endCallback.run();
127 }));
128
129 mBackgroundDimAnimator = getAlphaAnimator(layout.getBackgroundDimDrawable(),
130 /* endAlpha= */ 0, mDialogAnimation.getDuration());
131 mBackgroundDimAnimator.addListener(getDimAnimatorListener());
132
133 dialogContainer.startAnimation(mDialogAnimation);
134 mBackgroundDimAnimator.start();
135 }
136
137 /**
138 * Cancels all animations and resets the state of the controller.
139 */
140 public void cancelAnimation() {
141 if (mDialogAnimation != null) {
142 mDialogAnimation.cancel();
143 mDialogAnimation = null;
144 }
145 if (mBackgroundDimAnimator != null) {
146 mBackgroundDimAnimator.cancel();
147 mBackgroundDimAnimator = null;
148 }
149 }
150
151 private Animation loadAnimation(int animAttr) {
152 Animation animation = mTransitionAnimation.loadAnimationAttr(mPackageName, mAnimStyleResId,
153 animAttr, /* translucent= */ false);
154 if (animation == null) {
155 Log.e(mTag, "Failed to load animation " + animAttr);
156 }
157 return animation;
158 }
159
160 private Animation.AnimationListener getAnimationListener(Runnable startCallback,
161 Runnable endCallback) {
162 return new Animation.AnimationListener() {
163 @Override
164 public void onAnimationStart(Animation animation) {
165 startCallback.run();
166 }
167
168 @Override
169 public void onAnimationEnd(Animation animation) {
170 endCallback.run();
171 }
172
173 @Override
174 public void onAnimationRepeat(Animation animation) {}
175 };
176 }
177
178 private AnimatorListenerAdapter getDimAnimatorListener() {
179 return new AnimatorListenerAdapter() {
180 @Override
181 public void onAnimationEnd(Animator animation) {
182 mBackgroundDimAnimator = null;
183 }
184 };
185 }
186
187 private static Animator getAlphaAnimator(
188 Drawable drawable, int endAlpha, long duration) {
189 Animator animator = ObjectAnimator.ofInt(drawable, DRAWABLE_ALPHA, endAlpha);
190 animator.setDuration(duration);
191 return animator;
192 }
193
194 private static final Property<Drawable, Integer> DRAWABLE_ALPHA = new IntProperty<Drawable>(
195 "alpha") {
196 @Override
197 public void setValue(Drawable object, int value) {
198 object.setAlpha(value);
199 }
200
201 @Override
202 public Integer get(Drawable object) {
203 return object.getAlpha();
204 }
205 };
206 }