blob: 786dbb088e2b457c38f78197f10a891f263d9577 [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
19import static android.view.InsetsState.INSET_SIDE_BOTTOM;
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -070020import static android.view.InsetsState.INSET_SIDE_FLOATING;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010021import static android.view.InsetsState.INSET_SIDE_LEFT;
22import static android.view.InsetsState.INSET_SIDE_RIGHT;
23import static android.view.InsetsState.INSET_SIDE_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;
34import android.view.InsetsState.InsetSide;
35import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams;
36import android.view.WindowInsets.Type.InsetType;
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;
66 private final @InsetType int mTypes;
67 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,
80 @InsetType 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
121 @InsetType
122 public int getTypes() {
123 return mTypes;
124 }
125
126 @Override
127 public void changeInsets(Insets insets) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100128 if (mFinished) {
129 throw new IllegalStateException(
130 "Can't change insets on an animation that is finished.");
131 }
132 if (mCancelled) {
133 throw new IllegalStateException(
134 "Can't change insets on an animation that is cancelled.");
135 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800136 mPendingInsets = sanitize(insets);
137 mController.scheduleApplyChangeInsets();
138 }
139
Jorim Jaggifae3e272019-01-14 14:05:05 +0100140 @VisibleForTesting
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100141 /**
142 * @return Whether the finish callback of this animation should be invoked.
143 */
144 public boolean applyChangeInsets(InsetsState state) {
145 if (mCancelled) {
146 return false;
147 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800148 final Insets offset = Insets.subtract(mShownInsets, mPendingInsets);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100149 ArrayList<SurfaceParams> params = new ArrayList<>();
Jorim Jaggi956ca412019-01-07 14:49:14 +0100150 updateLeashesForSide(INSET_SIDE_LEFT, offset.left, mPendingInsets.left, params, state);
151 updateLeashesForSide(INSET_SIDE_TOP, offset.top, mPendingInsets.top, params, state);
152 updateLeashesForSide(INSET_SIDE_RIGHT, offset.right, mPendingInsets.right, params, state);
153 updateLeashesForSide(INSET_SIDE_BOTTOM, offset.bottom, mPendingInsets.bottom, params,
154 state);
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -0700155 updateLeashesForSide(INSET_SIDE_FLOATING, 0 /* offset */, 0 /* inset */, params, state);
156
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100157 SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get();
158 applier.scheduleApply(params.toArray(new SurfaceParams[params.size()]));
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800159 mCurrentInsets = mPendingInsets;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100160 if (mFinished) {
161 mController.notifyFinished(this, mFinishedShownTypes);
162 }
163 return mFinished;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100164 }
165
166 @Override
167 public void finish(int shownTypes) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100168 if (mCancelled) {
169 return;
170 }
171 InsetsState state = new InsetsState(mController.getState());
172 for (int i = mConsumers.size() - 1; i >= 0; i--) {
173 InsetsSourceConsumer consumer = mConsumers.valueAt(i);
174 boolean visible = (shownTypes & toPublicType(consumer.getType())) != 0;
175 state.getSource(consumer.getType()).setVisible(visible);
176 }
177 Insets insets = getInsetsFromState(state, mFrame, null /* typeSideMap */);
178 changeInsets(insets);
179 mFinished = true;
180 mFinishedShownTypes = shownTypes;
181 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800182
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100183 @VisibleForTesting
184 public void onCancelled() {
185 if (mFinished) {
186 return;
187 }
188 mCancelled = true;
189 mListener.onCancelled();
190 }
191
192 InsetsAnimation getAnimation() {
193 return mAnimation;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100194 }
195
196 private Insets calculateInsets(InsetsState state, Rect frame,
197 SparseArray<InsetsSourceConsumer> consumers, boolean shown,
198 @Nullable @InsetSide SparseIntArray typeSideMap) {
199 for (int i = consumers.size() - 1; i >= 0; i--) {
200 state.getSource(consumers.valueAt(i).getType()).setVisible(shown);
201 }
202 return getInsetsFromState(state, frame, typeSideMap);
203 }
204
205 private Insets getInsetsFromState(InsetsState state, Rect frame,
206 @Nullable @InsetSide SparseIntArray typeSideMap) {
207 return state.calculateInsets(frame, false /* isScreenRound */,
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100208 false /* alwaysConsumerNavBar */, null /* displayCutout */,
Jorim Jaggi648e5882019-01-24 13:24:02 +0100209 null /* legacyContentInsets */, null /* legacyStableInsets */,
210 LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, typeSideMap)
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100211 .getInsets(mTypes);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100212 }
213
214 private Insets sanitize(Insets insets) {
215 return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets);
216 }
217
Jorim Jaggi67684882019-01-22 17:36:34 +0100218 private void updateLeashesForSide(@InsetSide int side, int offset, int inset,
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800219 ArrayList<SurfaceParams> surfaceParams, InsetsState state) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100220 ArraySet<InsetsSourceConsumer> items = mSideSourceMap.get(side);
Jorim Jaggi956ca412019-01-07 14:49:14 +0100221 if (items == null) {
222 return;
223 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100224 // TODO: Implement behavior when inset spans over multiple types
225 for (int i = items.size() - 1; i >= 0; i--) {
226 final InsetsSourceConsumer consumer = items.valueAt(i);
227 final InsetsSource source = mInitialInsetsState.getSource(consumer.getType());
Tarandeep Singh215929b2019-01-11 18:24:37 -0800228 final InsetsSourceControl control = consumer.getControl();
Taran Singhd7fc5862019-10-10 14:45:17 +0200229 if (control == null) {
230 // Control may not be available for consumer yet or revoked.
231 continue;
232 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100233 final SurfaceControl leash = consumer.getControl().getLeash();
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800234
Tarandeep Singh215929b2019-01-11 18:24:37 -0800235 mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800236 mTmpFrame.set(source.getFrame());
Jorim Jaggi67684882019-01-22 17:36:34 +0100237 addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800238
239 state.getSource(source.getType()).setFrame(mTmpFrame);
Jorim Jaggia12ea562019-01-07 17:47:47 +0100240
241 // If the system is controlling the insets source, the leash can be null.
242 if (leash != null) {
243 surfaceParams.add(new SurfaceParams(leash, 1f /* alpha */, mTmpMatrix,
Tarandeep Singh95a3dbf2019-10-22 10:39:24 -0700244 null /* windowCrop */, 0 /* layer */, 0f /* cornerRadius*/,
245 side == INSET_SIDE_FLOATING
246 ? consumer.isVisible() : inset != 0 /* visible */));
Jorim Jaggia12ea562019-01-07 17:47:47 +0100247 }
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100248 }
249 }
250
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800251 private void addTranslationToMatrix(@InsetSide int side, int inset, Matrix m, Rect frame) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100252 switch (side) {
253 case INSET_SIDE_LEFT:
254 m.postTranslate(-inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800255 frame.offset(-inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100256 break;
257 case INSET_SIDE_TOP:
258 m.postTranslate(0, -inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800259 frame.offset(0, -inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100260 break;
261 case INSET_SIDE_RIGHT:
262 m.postTranslate(inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800263 frame.offset(inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100264 break;
265 case INSET_SIDE_BOTTOM:
266 m.postTranslate(0, inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800267 frame.offset(0, inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100268 break;
269 }
270 }
271
272 private static void buildTypeSourcesMap(SparseIntArray typeSideMap,
273 SparseSetArray<InsetsSourceConsumer> sideSourcesMap,
274 SparseArray<InsetsSourceConsumer> consumers) {
275 for (int i = typeSideMap.size() - 1; i >= 0; i--) {
Jorim Jaggi956ca412019-01-07 14:49:14 +0100276 final int type = typeSideMap.keyAt(i);
277 final int side = typeSideMap.valueAt(i);
278 final InsetsSourceConsumer consumer = consumers.get(type);
279 if (consumer == null) {
280 // If the types that we are controlling are less than the types that the system has,
281 // there can be some null consumers.
282 continue;
283 }
284 sideSourcesMap.add(side, consumer);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100285 }
286 }
287}