Debian Codegraph @

core/java/android/view/InsetsResizeAnimationRunner.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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.view;
18
19 import static android.view.InsetsAnimationControlImplProto.CURRENT_ALPHA;
20 import static android.view.InsetsAnimationControlImplProto.IS_CANCELLED;
21 import static android.view.InsetsAnimationControlImplProto.IS_FINISHED;
22 import static android.view.InsetsAnimationControlImplProto.PENDING_ALPHA;
23 import static android.view.InsetsAnimationControlImplProto.PENDING_FRACTION;
24 import static android.view.InsetsAnimationControlImplProto.PENDING_INSETS;
25 import static android.view.InsetsAnimationControlImplProto.SHOWN_ON_FINISH;
26 import static android.view.InsetsAnimationControlImplProto.TMP_MATRIX;
27 import static android.view.InsetsController.ANIMATION_TYPE_RESIZE;
28
29 import android.animation.Animator;
30 import android.animation.AnimatorListenerAdapter;
31 import android.animation.ValueAnimator;
32 import android.graphics.Insets;
33 import android.graphics.Rect;
34 import android.util.SparseArray;
35 import android.util.proto.ProtoOutputStream;
36 import android.view.InsetsState.InternalInsetsType;
37 import android.view.WindowInsets.Type.InsetsType;
38 import android.view.WindowInsetsAnimation.Bounds;
39 import android.view.animation.Interpolator;
40
41 /**
42 * Runs a fake animation of resizing insets to produce insets animation callbacks.
43 * @hide
44 */
45 public class InsetsResizeAnimationRunner implements InsetsAnimationControlRunner,
46 InternalInsetsAnimationController, WindowInsetsAnimationControlListener {
47
48 private final InsetsState mFromState;
49 private final InsetsState mToState;
50 private final @InsetsType int mTypes;
51 private final WindowInsetsAnimation mAnimation;
52 private final InsetsAnimationControlCallbacks mController;
53 private ValueAnimator mAnimator;
54 private boolean mCancelled;
55 private boolean mFinished;
56
57 public InsetsResizeAnimationRunner(Rect frame, InsetsState fromState, InsetsState toState,
58 Interpolator interpolator, long duration, @InsetsType int types,
59 InsetsAnimationControlCallbacks controller) {
60 mFromState = fromState;
61 mToState = toState;
62 mTypes = types;
63 mController = controller;
64 mAnimation = new WindowInsetsAnimation(types, interpolator, duration);
65 mAnimation.setAlpha(1f);
66 final Insets fromInsets = fromState.calculateInsets(
67 frame, types, false /* ignoreVisibility */);
68 final Insets toInsets = toState.calculateInsets(
69 frame, types, false /* ignoreVisibility */);
70 controller.startAnimation(this, this, types, mAnimation,
71 new Bounds(Insets.min(fromInsets, toInsets), Insets.max(fromInsets, toInsets)));
72 }
73
74 @Override
75 public int getTypes() {
76 return mTypes;
77 }
78
79 @Override
80 public int getControllingTypes() {
81 return mTypes;
82 }
83
84 @Override
85 public WindowInsetsAnimation getAnimation() {
86 return mAnimation;
87 }
88
89 @Override
90 public int getAnimationType() {
91 return ANIMATION_TYPE_RESIZE;
92 }
93
94 @Override
95 public void cancel() {
96 if (mCancelled || mFinished) {
97 return;
98 }
99 mCancelled = true;
100 if (mAnimator != null) {
101 mAnimator.cancel();
102 }
103 }
104
105 @Override
106 public boolean isCancelled() {
107 return mCancelled;
108 }
109
110 @Override
111 public void onReady(WindowInsetsAnimationController controller, int types) {
112 if (mCancelled) {
113 return;
114 }
115 mAnimator = ValueAnimator.ofFloat(0f, 1f);
116 mAnimator.setDuration(mAnimation.getDurationMillis());
117 mAnimator.addUpdateListener(animation -> {
118 mAnimation.setFraction(animation.getAnimatedFraction());
119 mController.scheduleApplyChangeInsets(InsetsResizeAnimationRunner.this);
120 });
121 mAnimator.addListener(new AnimatorListenerAdapter() {
122
123 @Override
124 public void onAnimationEnd(Animator animation) {
125 mFinished = true;
126 mController.scheduleApplyChangeInsets(InsetsResizeAnimationRunner.this);
127 }
128 });
129 mAnimator.start();
130 }
131
132 @Override
133 public boolean applyChangeInsets(InsetsState outState) {
134 if (mCancelled) {
135 return false;
136 }
137 final float fraction = mAnimation.getInterpolatedFraction();
138 for (@InternalInsetsType int type = 0; type < InsetsState.SIZE; type++) {
139 final InsetsSource fromSource = mFromState.peekSource(type);
140 final InsetsSource toSource = mToState.peekSource(type);
141 if (fromSource == null || toSource == null) {
142 continue;
143 }
144 final Rect fromFrame = fromSource.getFrame();
145 final Rect toFrame = toSource.getFrame();
146 final Rect frame = new Rect(
147 (int) (fromFrame.left + fraction * (toFrame.left - fromFrame.left)),
148 (int) (fromFrame.top + fraction * (toFrame.top - fromFrame.top)),
149 (int) (fromFrame.right + fraction * (toFrame.right - fromFrame.right)),
150 (int) (fromFrame.bottom + fraction * (toFrame.bottom - fromFrame.bottom)));
151 final InsetsSource source = new InsetsSource(type);
152 source.setFrame(frame);
153 source.setVisible(toSource.isVisible());
154 outState.addSource(source);
155 }
156 if (mFinished) {
157 mController.notifyFinished(this, true /* shown */);
158 }
159 return mFinished;
160 }
161
162 @Override
163 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
164 final long token = proto.start(fieldId);
165 proto.write(IS_CANCELLED, mCancelled);
166 proto.write(IS_FINISHED, mFinished);
167 proto.write(TMP_MATRIX, "null");
168 proto.write(PENDING_INSETS, "null");
169 proto.write(PENDING_FRACTION, mAnimation.getInterpolatedFraction());
170 proto.write(SHOWN_ON_FINISH, true);
171 proto.write(CURRENT_ALPHA, 1f);
172 proto.write(PENDING_ALPHA, 1f);
173 proto.end(token);
174 }
175
176 @Override
177 public Insets getHiddenStateInsets() {
178 return Insets.NONE;
179 }
180
181 @Override
182 public Insets getShownStateInsets() {
183 return Insets.NONE;
184 }
185
186 @Override
187 public Insets getCurrentInsets() {
188 return Insets.NONE;
189 }
190
191 @Override
192 public float getCurrentFraction() {
193 return 0;
194 }
195
196 @Override
197 public float getCurrentAlpha() {
198 return 0;
199 }
200
201 @Override
202 public void setInsetsAndAlpha(Insets insets, float alpha, float fraction) {
203 }
204
205 @Override
206 public void finish(boolean shown) {
207 }
208
209 @Override
210 public boolean isFinished() {
211 return false;
212 }
213
214 @Override
215 public void notifyControlRevoked(int types) {
216 }
217
218 @Override
219 public void updateSurfacePosition(SparseArray<InsetsSourceControl> controls) {
220 }
221
222 @Override
223 public boolean hasZeroInsetsIme() {
224 return false;
225 }
226
227 @Override
228 public void setReadyDispatched(boolean dispatched) {
229 }
230
231 @Override
232 public void onFinished(WindowInsetsAnimationController controller) {
233 }
234
235 @Override
236 public void onCancelled(WindowInsetsAnimationController controller) {
237 }
238 }