blob: e863aa06ed268b771505ffacdaad43c6751502b9 [file] [log] [blame]
Jorim Jaggi5bb571d2018-11-06 14:42:04 +01001/*
2 * Copyright (C) 2018 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
17package android.view;
18
Jorim Jaggia51168a2019-12-27 15:17:44 +010019import static android.view.InsetsController.LAYOUT_INSETS_DURING_ANIMATION_HIDDEN;
20import static android.view.InsetsController.LAYOUT_INSETS_DURING_ANIMATION_SHOWN;
Tiger Huang332793b2019-10-29 23:21:27 +080021import static android.view.InsetsState.ISIDE_BOTTOM;
22import static android.view.InsetsState.ISIDE_FLOATING;
23import static android.view.InsetsState.ISIDE_LEFT;
24import static android.view.InsetsState.ISIDE_RIGHT;
25import static android.view.InsetsState.ISIDE_TOP;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010026
27import android.annotation.Nullable;
28import android.graphics.Insets;
29import android.graphics.Matrix;
30import android.graphics.Rect;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010031import android.util.ArraySet;
32import android.util.SparseArray;
33import android.util.SparseIntArray;
34import android.util.SparseSetArray;
Jorim Jaggia51168a2019-12-27 15:17:44 +010035import android.view.InsetsController.LayoutInsetsDuringAnimation;
Tiger Huang332793b2019-10-29 23:21:27 +080036import android.view.InsetsState.InternalInsetsSide;
Jorim Jaggi1f2c7eb2020-01-08 00:07:13 +010037import android.view.InsetsState.InternalInsetsType;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010038import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams;
Tiger Huang332793b2019-10-29 23:21:27 +080039import android.view.WindowInsets.Type.InsetsType;
Tarandeep Singh54554e22019-11-01 14:43:05 -070040import android.view.WindowInsetsAnimationCallback.AnimationBounds;
41import android.view.WindowInsetsAnimationCallback.InsetsAnimation;
Jorim Jaggi648e5882019-01-24 13:24:02 +010042import android.view.WindowManager.LayoutParams;
Jorim Jaggidd3304e2020-01-20 17:24:51 +010043import android.view.animation.Interpolator;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010044
45import com.android.internal.annotations.VisibleForTesting;
46
47import java.util.ArrayList;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010048
49/**
50 * Implements {@link WindowInsetsAnimationController}
51 * @hide
52 */
53@VisibleForTesting
Jorim Jaggi02a741f2018-12-12 17:40:19 -080054public class InsetsAnimationControlImpl implements WindowInsetsAnimationController {
55
56 private final Rect mTmpFrame = new Rect();
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010057
58 private final WindowInsetsAnimationControlListener mListener;
Yunfan Chen02abf552019-12-05 14:51:09 +090059 private final SparseArray<InsetsSourceControl> mControls;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010060 private final SparseIntArray mTypeSideMap = new SparseIntArray();
Yunfan Chen02abf552019-12-05 14:51:09 +090061 private final SparseSetArray<InsetsSourceControl> mSideSourceMap = new SparseSetArray<>();
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010062
63 /** @see WindowInsetsAnimationController#getHiddenStateInsets */
64 private final Insets mHiddenInsets;
65
66 /** @see WindowInsetsAnimationController#getShownStateInsets */
67 private final Insets mShownInsets;
68 private final Matrix mTmpMatrix = new Matrix();
69 private final InsetsState mInitialInsetsState;
Tiger Huang332793b2019-10-29 23:21:27 +080070 private final @InsetsType int mTypes;
Yunfan Chen02abf552019-12-05 14:51:09 +090071 private final InsetsAnimationControlCallbacks mController;
Tarandeep Singh54554e22019-11-01 14:43:05 -070072 private final WindowInsetsAnimationCallback.InsetsAnimation mAnimation;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010073 private final Rect mFrame;
Tarandeep Singh699aa672019-11-22 23:19:30 -080074 private final boolean mFade;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010075 private Insets mCurrentInsets;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080076 private Insets mPendingInsets;
Tarandeep Singh54554e22019-11-01 14:43:05 -070077 private float mPendingFraction;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010078 private boolean mFinished;
79 private boolean mCancelled;
Tarandeep Singh54554e22019-11-01 14:43:05 -070080 private boolean mShownOnFinish;
Tarandeep Singh699aa672019-11-22 23:19:30 -080081 private float mCurrentAlpha;
82 private float mPendingAlpha;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010083
84 @VisibleForTesting
Yunfan Chen02abf552019-12-05 14:51:09 +090085 public InsetsAnimationControlImpl(SparseArray<InsetsSourceControl> controls, Rect frame,
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010086 InsetsState state, WindowInsetsAnimationControlListener listener,
Tiger Huang332793b2019-10-29 23:21:27 +080087 @InsetsType int types,
Jorim Jaggidd3304e2020-01-20 17:24:51 +010088 InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator,
89 boolean fade, @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation) {
Yunfan Chen02abf552019-12-05 14:51:09 +090090 mControls = controls;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010091 mListener = listener;
92 mTypes = types;
Tarandeep Singh699aa672019-11-22 23:19:30 -080093 mFade = fade;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080094 mController = controller;
95 mInitialInsetsState = new InsetsState(state, true /* copySources */);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010096 mCurrentInsets = getInsetsFromState(mInitialInsetsState, frame, null /* typeSideMap */);
Jorim Jaggi1f2c7eb2020-01-08 00:07:13 +010097 mPendingInsets = mCurrentInsets;
Yunfan Chen02abf552019-12-05 14:51:09 +090098 mHiddenInsets = calculateInsets(mInitialInsetsState, frame, controls, false /* shown */,
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010099 null /* typeSideMap */);
Yunfan Chen02abf552019-12-05 14:51:09 +0900100 mShownInsets = calculateInsets(mInitialInsetsState, frame, controls, true /* shown */,
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100101 mTypeSideMap);
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100102 mFrame = new Rect(frame);
Yunfan Chen02abf552019-12-05 14:51:09 +0900103 buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mControls);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100104
Jorim Jaggidd3304e2020-01-20 17:24:51 +0100105 mAnimation = new WindowInsetsAnimationCallback.InsetsAnimation(mTypes, interpolator,
106 durationMs);
Tarandeep Singh699aa672019-11-22 23:19:30 -0800107 mAnimation.setAlpha(getCurrentAlpha());
Jorim Jaggia51168a2019-12-27 15:17:44 +0100108 mController.startAnimation(this, listener, types, mAnimation,
109 new AnimationBounds(mHiddenInsets, mShownInsets), layoutInsetsDuringAnimation);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100110 }
111
112 @Override
113 public Insets getHiddenStateInsets() {
114 return mHiddenInsets;
115 }
116
117 @Override
118 public Insets getShownStateInsets() {
119 return mShownInsets;
120 }
121
122 @Override
123 public Insets getCurrentInsets() {
124 return mCurrentInsets;
125 }
126
127 @Override
Tarandeep Singh699aa672019-11-22 23:19:30 -0800128 public float getCurrentAlpha() {
129 return mCurrentAlpha;
130 }
131
132 @Override
Tiger Huang332793b2019-10-29 23:21:27 +0800133 @InsetsType public int getTypes() {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100134 return mTypes;
135 }
136
Jorim Jaggi1f2c7eb2020-01-08 00:07:13 +0100137 boolean controlsInternalType(@InternalInsetsType int type) {
138 return InsetsState.toInternalType(mTypes).contains(type);
139 }
140
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100141 @Override
Tarandeep Singh54554e22019-11-01 14:43:05 -0700142 public void setInsetsAndAlpha(Insets insets, float alpha, float fraction) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100143 if (mFinished) {
144 throw new IllegalStateException(
145 "Can't change insets on an animation that is finished.");
146 }
147 if (mCancelled) {
148 throw new IllegalStateException(
149 "Can't change insets on an animation that is cancelled.");
150 }
Tarandeep Singh54554e22019-11-01 14:43:05 -0700151 mPendingFraction = sanitize(fraction);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800152 mPendingInsets = sanitize(insets);
Tarandeep Singh699aa672019-11-22 23:19:30 -0800153 mPendingAlpha = 1 - sanitize(alpha);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800154 mController.scheduleApplyChangeInsets();
155 }
156
Jorim Jaggifae3e272019-01-14 14:05:05 +0100157 @VisibleForTesting
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100158 /**
159 * @return Whether the finish callback of this animation should be invoked.
160 */
161 public boolean applyChangeInsets(InsetsState state) {
162 if (mCancelled) {
163 return false;
164 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800165 final Insets offset = Insets.subtract(mShownInsets, mPendingInsets);
Tarandeep Singh699aa672019-11-22 23:19:30 -0800166 final Float alphaOffset = 1 - mPendingAlpha;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100167 ArrayList<SurfaceParams> params = new ArrayList<>();
Tarandeep Singh699aa672019-11-22 23:19:30 -0800168 updateLeashesForSide(ISIDE_LEFT, offset.left, mShownInsets.left, mPendingInsets.left,
169 params, state, alphaOffset);
170 updateLeashesForSide(ISIDE_TOP, offset.top, mShownInsets.top, mPendingInsets.top, params,
171 state, alphaOffset);
172 updateLeashesForSide(ISIDE_RIGHT, offset.right, mShownInsets.right, mPendingInsets.right,
173 params, state, alphaOffset);
174 updateLeashesForSide(ISIDE_BOTTOM, offset.bottom, mShownInsets.bottom,
175 mPendingInsets.bottom, params, state, alphaOffset);
176 updateLeashesForSide(ISIDE_FLOATING, 0 /* offset */, 0 /* inset */, 0 /* maxInset */,
177 params, state, alphaOffset);
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -0700178
Yunfan Chen02abf552019-12-05 14:51:09 +0900179 mController.applySurfaceParams(params.toArray(new SurfaceParams[params.size()]));
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800180 mCurrentInsets = mPendingInsets;
Tarandeep Singh54554e22019-11-01 14:43:05 -0700181 mAnimation.setFraction(mPendingFraction);
Tarandeep Singh699aa672019-11-22 23:19:30 -0800182 mCurrentAlpha = 1 - alphaOffset;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100183 if (mFinished) {
Tarandeep Singh54554e22019-11-01 14:43:05 -0700184 mController.notifyFinished(this, mShownOnFinish);
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100185 }
186 return mFinished;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100187 }
188
189 @Override
Tarandeep Singh54554e22019-11-01 14:43:05 -0700190 public void finish(boolean shown) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100191 if (mCancelled) {
192 return;
193 }
Jorim Jaggi235728e2020-01-28 13:52:10 +0100194 setInsetsAndAlpha(shown ? mShownInsets : mHiddenInsets, 1f /* alpha */, 1f /* fraction */);
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100195 mFinished = true;
Tarandeep Singh54554e22019-11-01 14:43:05 -0700196 mShownOnFinish = shown;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100197 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800198
Tarandeep Singh54554e22019-11-01 14:43:05 -0700199 @Override
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100200 @VisibleForTesting
Tarandeep Singh54554e22019-11-01 14:43:05 -0700201 public float getCurrentFraction() {
202 return mAnimation.getFraction();
203 }
204
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100205 public void onCancelled() {
206 if (mFinished) {
207 return;
208 }
209 mCancelled = true;
210 mListener.onCancelled();
211 }
212
213 InsetsAnimation getAnimation() {
214 return mAnimation;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100215 }
216
Tarandeep Singh54554e22019-11-01 14:43:05 -0700217 WindowInsetsAnimationControlListener getListener() {
218 return mListener;
219 }
220
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100221 private Insets calculateInsets(InsetsState state, Rect frame,
Yunfan Chen02abf552019-12-05 14:51:09 +0900222 SparseArray<InsetsSourceControl> controls, boolean shown,
Tiger Huang332793b2019-10-29 23:21:27 +0800223 @Nullable @InternalInsetsSide SparseIntArray typeSideMap) {
Yunfan Chen02abf552019-12-05 14:51:09 +0900224 for (int i = controls.size() - 1; i >= 0; i--) {
225 // control may be null if it got revoked.
226 if (controls.valueAt(i) == null) continue;
227 state.getSource(controls.valueAt(i).getType()).setVisible(shown);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100228 }
229 return getInsetsFromState(state, frame, typeSideMap);
230 }
231
232 private Insets getInsetsFromState(InsetsState state, Rect frame,
Tiger Huang332793b2019-10-29 23:21:27 +0800233 @Nullable @InternalInsetsSide SparseIntArray typeSideMap) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100234 return state.calculateInsets(frame, false /* isScreenRound */,
Yunfan Chen02abf552019-12-05 14:51:09 +0900235 false /* alwaysConsumeSystemBars */, null /* displayCutout */,
Jorim Jaggi648e5882019-01-24 13:24:02 +0100236 null /* legacyContentInsets */, null /* legacyStableInsets */,
Jorim Jaggi7f761872020-01-10 18:24:27 +0100237 LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/,
238 0 /* legacySystemUiFlags */, typeSideMap)
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100239 .getInsets(mTypes);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100240 }
241
242 private Insets sanitize(Insets insets) {
Tarandeep Singh54554e22019-11-01 14:43:05 -0700243 if (insets == null) {
244 insets = getCurrentInsets();
245 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100246 return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets);
247 }
248
Tarandeep Singh54554e22019-11-01 14:43:05 -0700249 private static float sanitize(float alpha) {
250 return alpha >= 1 ? 1 : (alpha <= 0 ? 0 : alpha);
251 }
252
Tiger Huang332793b2019-10-29 23:21:27 +0800253 private void updateLeashesForSide(@InternalInsetsSide int side, int offset, int inset,
Tarandeep Singh699aa672019-11-22 23:19:30 -0800254 int maxInset, ArrayList<SurfaceParams> surfaceParams, InsetsState state, Float alpha) {
Yunfan Chen02abf552019-12-05 14:51:09 +0900255 ArraySet<InsetsSourceControl> items = mSideSourceMap.get(side);
Jorim Jaggi956ca412019-01-07 14:49:14 +0100256 if (items == null) {
257 return;
258 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100259 // TODO: Implement behavior when inset spans over multiple types
260 for (int i = items.size() - 1; i >= 0; i--) {
Yunfan Chen02abf552019-12-05 14:51:09 +0900261 final InsetsSourceControl control = items.valueAt(i);
262 final InsetsSource source = mInitialInsetsState.getSource(control.getType());
Yunfan Chen02abf552019-12-05 14:51:09 +0900263 final SurfaceControl leash = control.getLeash();
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800264
Tarandeep Singh215929b2019-01-11 18:24:37 -0800265 mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800266 mTmpFrame.set(source.getFrame());
Jorim Jaggi67684882019-01-22 17:36:34 +0100267 addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800268
269 state.getSource(source.getType()).setFrame(mTmpFrame);
Jorim Jaggia12ea562019-01-07 17:47:47 +0100270
271 // If the system is controlling the insets source, the leash can be null.
272 if (leash != null) {
Tarandeep Singh699aa672019-11-22 23:19:30 -0800273 // TODO: use a better interpolation for fade.
274 alpha = mFade ? ((float) maxInset / inset * 0.3f + 0.7f) : alpha;
275 surfaceParams.add(new SurfaceParams(leash, alpha, mTmpMatrix,
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -0700276 null /* windowCrop */, 0 /* layer */, 0f /* cornerRadius*/,
Yunfan Chen02abf552019-12-05 14:51:09 +0900277 side == ISIDE_FLOATING ? state.getSource(source.getType()).isVisible()
278 : inset != 0 /* visible */));
Jorim Jaggia12ea562019-01-07 17:47:47 +0100279 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100280 }
281 }
282
Tiger Huang332793b2019-10-29 23:21:27 +0800283 private void addTranslationToMatrix(@InternalInsetsSide int side, int inset, Matrix m,
284 Rect frame) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100285 switch (side) {
Tiger Huang332793b2019-10-29 23:21:27 +0800286 case ISIDE_LEFT:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100287 m.postTranslate(-inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800288 frame.offset(-inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100289 break;
Tiger Huang332793b2019-10-29 23:21:27 +0800290 case ISIDE_TOP:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100291 m.postTranslate(0, -inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800292 frame.offset(0, -inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100293 break;
Tiger Huang332793b2019-10-29 23:21:27 +0800294 case ISIDE_RIGHT:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100295 m.postTranslate(inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800296 frame.offset(inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100297 break;
Tiger Huang332793b2019-10-29 23:21:27 +0800298 case ISIDE_BOTTOM:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100299 m.postTranslate(0, inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800300 frame.offset(0, inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100301 break;
302 }
303 }
304
305 private static void buildTypeSourcesMap(SparseIntArray typeSideMap,
Yunfan Chen02abf552019-12-05 14:51:09 +0900306 SparseSetArray<InsetsSourceControl> sideSourcesMap,
307 SparseArray<InsetsSourceControl> controls) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100308 for (int i = typeSideMap.size() - 1; i >= 0; i--) {
Jorim Jaggi956ca412019-01-07 14:49:14 +0100309 final int type = typeSideMap.keyAt(i);
310 final int side = typeSideMap.valueAt(i);
Yunfan Chen02abf552019-12-05 14:51:09 +0900311 final InsetsSourceControl control = controls.get(type);
312 if (control == null) {
Jorim Jaggi956ca412019-01-07 14:49:14 +0100313 // If the types that we are controlling are less than the types that the system has,
Yunfan Chen02abf552019-12-05 14:51:09 +0900314 // there can be some null controllers.
Jorim Jaggi956ca412019-01-07 14:49:14 +0100315 continue;
316 }
Yunfan Chen02abf552019-12-05 14:51:09 +0900317 sideSourcesMap.add(side, control);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100318 }
319 }
320}