blob: 3d139cd7351870f9a72d0a8d27cc5f9122974446 [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
Tiger Huang332793b2019-10-29 23:21:27 +080019import static android.view.InsetsState.ISIDE_BOTTOM;
20import static android.view.InsetsState.ISIDE_FLOATING;
21import static android.view.InsetsState.ISIDE_LEFT;
22import static android.view.InsetsState.ISIDE_RIGHT;
23import static android.view.InsetsState.ISIDE_TOP;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010024import static android.view.InsetsState.toPublicType;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010025
26import android.annotation.Nullable;
27import android.graphics.Insets;
28import android.graphics.Matrix;
29import android.graphics.Rect;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010030import android.util.ArraySet;
31import android.util.SparseArray;
32import android.util.SparseIntArray;
33import android.util.SparseSetArray;
Tiger Huang332793b2019-10-29 23:21:27 +080034import android.view.InsetsState.InternalInsetsSide;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010035import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams;
Tiger Huang332793b2019-10-29 23:21:27 +080036import android.view.WindowInsets.Type.InsetsType;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010037import android.view.WindowInsetsAnimationListener.InsetsAnimation;
Jorim Jaggi648e5882019-01-24 13:24:02 +010038import android.view.WindowManager.LayoutParams;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010039
40import com.android.internal.annotations.VisibleForTesting;
41
42import java.util.ArrayList;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010043import java.util.function.Supplier;
44
45/**
46 * Implements {@link WindowInsetsAnimationController}
47 * @hide
48 */
49@VisibleForTesting
Jorim Jaggi02a741f2018-12-12 17:40:19 -080050public class InsetsAnimationControlImpl implements WindowInsetsAnimationController {
51
52 private final Rect mTmpFrame = new Rect();
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010053
54 private final WindowInsetsAnimationControlListener mListener;
55 private final SparseArray<InsetsSourceConsumer> mConsumers;
56 private final SparseIntArray mTypeSideMap = new SparseIntArray();
57 private final SparseSetArray<InsetsSourceConsumer> mSideSourceMap = new SparseSetArray<>();
58
59 /** @see WindowInsetsAnimationController#getHiddenStateInsets */
60 private final Insets mHiddenInsets;
61
62 /** @see WindowInsetsAnimationController#getShownStateInsets */
63 private final Insets mShownInsets;
64 private final Matrix mTmpMatrix = new Matrix();
65 private final InsetsState mInitialInsetsState;
Tiger Huang332793b2019-10-29 23:21:27 +080066 private final @InsetsType int mTypes;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010067 private final Supplier<SyncRtSurfaceTransactionApplier> mTransactionApplierSupplier;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080068 private final InsetsController mController;
69 private final WindowInsetsAnimationListener.InsetsAnimation mAnimation;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010070 private final Rect mFrame;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010071 private Insets mCurrentInsets;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080072 private Insets mPendingInsets;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010073 private boolean mFinished;
74 private boolean mCancelled;
75 private int mFinishedShownTypes;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010076
77 @VisibleForTesting
78 public InsetsAnimationControlImpl(SparseArray<InsetsSourceConsumer> consumers, Rect frame,
79 InsetsState state, WindowInsetsAnimationControlListener listener,
Tiger Huang332793b2019-10-29 23:21:27 +080080 @InsetsType int types,
Jorim Jaggi02a741f2018-12-12 17:40:19 -080081 Supplier<SyncRtSurfaceTransactionApplier> transactionApplierSupplier,
82 InsetsController controller) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010083 mConsumers = consumers;
84 mListener = listener;
85 mTypes = types;
86 mTransactionApplierSupplier = transactionApplierSupplier;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080087 mController = controller;
88 mInitialInsetsState = new InsetsState(state, true /* copySources */);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010089 mCurrentInsets = getInsetsFromState(mInitialInsetsState, frame, null /* typeSideMap */);
90 mHiddenInsets = calculateInsets(mInitialInsetsState, frame, consumers, false /* shown */,
91 null /* typeSideMap */);
92 mShownInsets = calculateInsets(mInitialInsetsState, frame, consumers, true /* shown */,
93 mTypeSideMap);
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010094 mFrame = new Rect(frame);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010095 buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mConsumers);
96
97 // TODO: Check for controllability first and wait for IME if needed.
98 listener.onReady(this, types);
Jorim Jaggi02a741f2018-12-12 17:40:19 -080099
100 mAnimation = new WindowInsetsAnimationListener.InsetsAnimation(mTypes, mHiddenInsets,
101 mShownInsets);
102 mController.dispatchAnimationStarted(mAnimation);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100103 }
104
105 @Override
106 public Insets getHiddenStateInsets() {
107 return mHiddenInsets;
108 }
109
110 @Override
111 public Insets getShownStateInsets() {
112 return mShownInsets;
113 }
114
115 @Override
116 public Insets getCurrentInsets() {
117 return mCurrentInsets;
118 }
119
120 @Override
Tiger Huang332793b2019-10-29 23:21:27 +0800121 @InsetsType public int getTypes() {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100122 return mTypes;
123 }
124
125 @Override
126 public void changeInsets(Insets insets) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100127 if (mFinished) {
128 throw new IllegalStateException(
129 "Can't change insets on an animation that is finished.");
130 }
131 if (mCancelled) {
132 throw new IllegalStateException(
133 "Can't change insets on an animation that is cancelled.");
134 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800135 mPendingInsets = sanitize(insets);
136 mController.scheduleApplyChangeInsets();
137 }
138
Jorim Jaggifae3e272019-01-14 14:05:05 +0100139 @VisibleForTesting
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100140 /**
141 * @return Whether the finish callback of this animation should be invoked.
142 */
143 public boolean applyChangeInsets(InsetsState state) {
144 if (mCancelled) {
145 return false;
146 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800147 final Insets offset = Insets.subtract(mShownInsets, mPendingInsets);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100148 ArrayList<SurfaceParams> params = new ArrayList<>();
Tiger Huang332793b2019-10-29 23:21:27 +0800149 updateLeashesForSide(ISIDE_LEFT, offset.left, mPendingInsets.left, params, state);
150 updateLeashesForSide(ISIDE_TOP, offset.top, mPendingInsets.top, params, state);
151 updateLeashesForSide(ISIDE_RIGHT, offset.right, mPendingInsets.right, params, state);
152 updateLeashesForSide(ISIDE_BOTTOM, offset.bottom, mPendingInsets.bottom, params, state);
153 updateLeashesForSide(ISIDE_FLOATING, 0 /* offset */, 0 /* inset */, params, state);
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -0700154
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100155 SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get();
156 applier.scheduleApply(params.toArray(new SurfaceParams[params.size()]));
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800157 mCurrentInsets = mPendingInsets;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100158 if (mFinished) {
159 mController.notifyFinished(this, mFinishedShownTypes);
160 }
161 return mFinished;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100162 }
163
164 @Override
165 public void finish(int shownTypes) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100166 if (mCancelled) {
167 return;
168 }
169 InsetsState state = new InsetsState(mController.getState());
170 for (int i = mConsumers.size() - 1; i >= 0; i--) {
171 InsetsSourceConsumer consumer = mConsumers.valueAt(i);
172 boolean visible = (shownTypes & toPublicType(consumer.getType())) != 0;
173 state.getSource(consumer.getType()).setVisible(visible);
174 }
175 Insets insets = getInsetsFromState(state, mFrame, null /* typeSideMap */);
176 changeInsets(insets);
177 mFinished = true;
178 mFinishedShownTypes = shownTypes;
179 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800180
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100181 @VisibleForTesting
182 public void onCancelled() {
183 if (mFinished) {
184 return;
185 }
186 mCancelled = true;
187 mListener.onCancelled();
188 }
189
190 InsetsAnimation getAnimation() {
191 return mAnimation;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100192 }
193
194 private Insets calculateInsets(InsetsState state, Rect frame,
195 SparseArray<InsetsSourceConsumer> consumers, boolean shown,
Tiger Huang332793b2019-10-29 23:21:27 +0800196 @Nullable @InternalInsetsSide SparseIntArray typeSideMap) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100197 for (int i = consumers.size() - 1; i >= 0; i--) {
198 state.getSource(consumers.valueAt(i).getType()).setVisible(shown);
199 }
200 return getInsetsFromState(state, frame, typeSideMap);
201 }
202
203 private Insets getInsetsFromState(InsetsState state, Rect frame,
Tiger Huang332793b2019-10-29 23:21:27 +0800204 @Nullable @InternalInsetsSide SparseIntArray typeSideMap) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100205 return state.calculateInsets(frame, false /* isScreenRound */,
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100206 false /* alwaysConsumerNavBar */, null /* displayCutout */,
Jorim Jaggi648e5882019-01-24 13:24:02 +0100207 null /* legacyContentInsets */, null /* legacyStableInsets */,
208 LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, typeSideMap)
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100209 .getInsets(mTypes);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100210 }
211
212 private Insets sanitize(Insets insets) {
213 return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets);
214 }
215
Tiger Huang332793b2019-10-29 23:21:27 +0800216 private void updateLeashesForSide(@InternalInsetsSide int side, int offset, int inset,
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800217 ArrayList<SurfaceParams> surfaceParams, InsetsState state) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100218 ArraySet<InsetsSourceConsumer> items = mSideSourceMap.get(side);
Jorim Jaggi956ca412019-01-07 14:49:14 +0100219 if (items == null) {
220 return;
221 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100222 // TODO: Implement behavior when inset spans over multiple types
223 for (int i = items.size() - 1; i >= 0; i--) {
224 final InsetsSourceConsumer consumer = items.valueAt(i);
225 final InsetsSource source = mInitialInsetsState.getSource(consumer.getType());
Tarandeep Singh215929b2019-01-11 18:24:37 -0800226 final InsetsSourceControl control = consumer.getControl();
Taran Singhd7fc5862019-10-10 14:45:17 +0200227 if (control == null) {
228 // Control may not be available for consumer yet or revoked.
229 continue;
230 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100231 final SurfaceControl leash = consumer.getControl().getLeash();
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800232
Tarandeep Singh215929b2019-01-11 18:24:37 -0800233 mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800234 mTmpFrame.set(source.getFrame());
Jorim Jaggi67684882019-01-22 17:36:34 +0100235 addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800236
237 state.getSource(source.getType()).setFrame(mTmpFrame);
Jorim Jaggia12ea562019-01-07 17:47:47 +0100238
239 // If the system is controlling the insets source, the leash can be null.
240 if (leash != null) {
241 surfaceParams.add(new SurfaceParams(leash, 1f /* alpha */, mTmpMatrix,
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -0700242 null /* windowCrop */, 0 /* layer */, 0f /* cornerRadius*/,
Tiger Huang332793b2019-10-29 23:21:27 +0800243 side == ISIDE_FLOATING ? consumer.isVisible() : inset != 0 /* visible */));
Jorim Jaggia12ea562019-01-07 17:47:47 +0100244 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100245 }
246 }
247
Tiger Huang332793b2019-10-29 23:21:27 +0800248 private void addTranslationToMatrix(@InternalInsetsSide int side, int inset, Matrix m,
249 Rect frame) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100250 switch (side) {
Tiger Huang332793b2019-10-29 23:21:27 +0800251 case ISIDE_LEFT:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100252 m.postTranslate(-inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800253 frame.offset(-inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100254 break;
Tiger Huang332793b2019-10-29 23:21:27 +0800255 case ISIDE_TOP:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100256 m.postTranslate(0, -inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800257 frame.offset(0, -inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100258 break;
Tiger Huang332793b2019-10-29 23:21:27 +0800259 case ISIDE_RIGHT:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100260 m.postTranslate(inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800261 frame.offset(inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100262 break;
Tiger Huang332793b2019-10-29 23:21:27 +0800263 case ISIDE_BOTTOM:
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100264 m.postTranslate(0, inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800265 frame.offset(0, inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100266 break;
267 }
268 }
269
270 private static void buildTypeSourcesMap(SparseIntArray typeSideMap,
271 SparseSetArray<InsetsSourceConsumer> sideSourcesMap,
272 SparseArray<InsetsSourceConsumer> consumers) {
273 for (int i = typeSideMap.size() - 1; i >= 0; i--) {
Jorim Jaggi956ca412019-01-07 14:49:14 +0100274 final int type = typeSideMap.keyAt(i);
275 final int side = typeSideMap.valueAt(i);
276 final InsetsSourceConsumer consumer = consumers.get(type);
277 if (consumer == null) {
278 // If the types that we are controlling are less than the types that the system has,
279 // there can be some null consumers.
280 continue;
281 }
282 sideSourcesMap.add(side, consumer);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100283 }
284 }
285}