blob: 50ef91f90997715792b2c0018a927a230b14f191 [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;
20import static android.view.InsetsState.INSET_SIDE_LEFT;
21import static android.view.InsetsState.INSET_SIDE_RIGHT;
22import static android.view.InsetsState.INSET_SIDE_TOP;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010023import static android.view.InsetsState.toPublicType;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010024
25import android.annotation.Nullable;
26import android.graphics.Insets;
27import android.graphics.Matrix;
28import android.graphics.Rect;
29import android.os.UidProto.Sync;
30import 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;
43import java.util.function.Function;
44import java.util.function.Supplier;
45
46/**
47 * Implements {@link WindowInsetsAnimationController}
48 * @hide
49 */
50@VisibleForTesting
Jorim Jaggi02a741f2018-12-12 17:40:19 -080051public class InsetsAnimationControlImpl implements WindowInsetsAnimationController {
52
53 private final Rect mTmpFrame = new Rect();
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010054
55 private final WindowInsetsAnimationControlListener mListener;
56 private final SparseArray<InsetsSourceConsumer> mConsumers;
57 private final SparseIntArray mTypeSideMap = new SparseIntArray();
58 private final SparseSetArray<InsetsSourceConsumer> mSideSourceMap = new SparseSetArray<>();
59
60 /** @see WindowInsetsAnimationController#getHiddenStateInsets */
61 private final Insets mHiddenInsets;
62
63 /** @see WindowInsetsAnimationController#getShownStateInsets */
64 private final Insets mShownInsets;
65 private final Matrix mTmpMatrix = new Matrix();
66 private final InsetsState mInitialInsetsState;
67 private final @InsetType int mTypes;
68 private final Supplier<SyncRtSurfaceTransactionApplier> mTransactionApplierSupplier;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080069 private final InsetsController mController;
70 private final WindowInsetsAnimationListener.InsetsAnimation mAnimation;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010071 private final Rect mFrame;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010072 private Insets mCurrentInsets;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080073 private Insets mPendingInsets;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010074 private boolean mFinished;
75 private boolean mCancelled;
76 private int mFinishedShownTypes;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010077
78 @VisibleForTesting
79 public InsetsAnimationControlImpl(SparseArray<InsetsSourceConsumer> consumers, Rect frame,
80 InsetsState state, WindowInsetsAnimationControlListener listener,
81 @InsetType int types,
Jorim Jaggi02a741f2018-12-12 17:40:19 -080082 Supplier<SyncRtSurfaceTransactionApplier> transactionApplierSupplier,
83 InsetsController controller) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010084 mConsumers = consumers;
85 mListener = listener;
86 mTypes = types;
87 mTransactionApplierSupplier = transactionApplierSupplier;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080088 mController = controller;
89 mInitialInsetsState = new InsetsState(state, true /* copySources */);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010090 mCurrentInsets = getInsetsFromState(mInitialInsetsState, frame, null /* typeSideMap */);
91 mHiddenInsets = calculateInsets(mInitialInsetsState, frame, consumers, false /* shown */,
92 null /* typeSideMap */);
93 mShownInsets = calculateInsets(mInitialInsetsState, frame, consumers, true /* shown */,
94 mTypeSideMap);
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +010095 mFrame = new Rect(frame);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010096 buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mConsumers);
97
98 // TODO: Check for controllability first and wait for IME if needed.
99 listener.onReady(this, types);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800100
101 mAnimation = new WindowInsetsAnimationListener.InsetsAnimation(mTypes, mHiddenInsets,
102 mShownInsets);
103 mController.dispatchAnimationStarted(mAnimation);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100104 }
105
106 @Override
107 public Insets getHiddenStateInsets() {
108 return mHiddenInsets;
109 }
110
111 @Override
112 public Insets getShownStateInsets() {
113 return mShownInsets;
114 }
115
116 @Override
117 public Insets getCurrentInsets() {
118 return mCurrentInsets;
119 }
120
121 @Override
122 @InsetType
123 public int getTypes() {
124 return mTypes;
125 }
126
127 @Override
128 public void changeInsets(Insets insets) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100129 if (mFinished) {
130 throw new IllegalStateException(
131 "Can't change insets on an animation that is finished.");
132 }
133 if (mCancelled) {
134 throw new IllegalStateException(
135 "Can't change insets on an animation that is cancelled.");
136 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800137 mPendingInsets = sanitize(insets);
138 mController.scheduleApplyChangeInsets();
139 }
140
Jorim Jaggifae3e272019-01-14 14:05:05 +0100141 @VisibleForTesting
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100142 /**
143 * @return Whether the finish callback of this animation should be invoked.
144 */
145 public boolean applyChangeInsets(InsetsState state) {
146 if (mCancelled) {
147 return false;
148 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800149 final Insets offset = Insets.subtract(mShownInsets, mPendingInsets);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100150 ArrayList<SurfaceParams> params = new ArrayList<>();
151 if (offset.left != 0) {
Jorim Jaggi67684882019-01-22 17:36:34 +0100152 updateLeashesForSide(INSET_SIDE_LEFT, offset.left, mPendingInsets.left, params, state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100153 }
154 if (offset.top != 0) {
Jorim Jaggi67684882019-01-22 17:36:34 +0100155 updateLeashesForSide(INSET_SIDE_TOP, offset.top, mPendingInsets.top, params, state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100156 }
157 if (offset.right != 0) {
Jorim Jaggi67684882019-01-22 17:36:34 +0100158 updateLeashesForSide(INSET_SIDE_RIGHT, offset.right, mPendingInsets.right, params,
159 state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100160 }
161 if (offset.bottom != 0) {
Jorim Jaggi67684882019-01-22 17:36:34 +0100162 updateLeashesForSide(INSET_SIDE_BOTTOM, offset.bottom, mPendingInsets.bottom, params,
163 state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100164 }
165 SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get();
166 applier.scheduleApply(params.toArray(new SurfaceParams[params.size()]));
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800167 mCurrentInsets = mPendingInsets;
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100168 if (mFinished) {
169 mController.notifyFinished(this, mFinishedShownTypes);
170 }
171 return mFinished;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100172 }
173
174 @Override
175 public void finish(int shownTypes) {
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100176 if (mCancelled) {
177 return;
178 }
179 InsetsState state = new InsetsState(mController.getState());
180 for (int i = mConsumers.size() - 1; i >= 0; i--) {
181 InsetsSourceConsumer consumer = mConsumers.valueAt(i);
182 boolean visible = (shownTypes & toPublicType(consumer.getType())) != 0;
183 state.getSource(consumer.getType()).setVisible(visible);
184 }
185 Insets insets = getInsetsFromState(state, mFrame, null /* typeSideMap */);
186 changeInsets(insets);
187 mFinished = true;
188 mFinishedShownTypes = shownTypes;
189 }
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800190
Jorim Jaggi5ed50cc2019-01-23 16:59:42 +0100191 @VisibleForTesting
192 public void onCancelled() {
193 if (mFinished) {
194 return;
195 }
196 mCancelled = true;
197 mListener.onCancelled();
198 }
199
200 InsetsAnimation getAnimation() {
201 return mAnimation;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100202 }
203
204 private Insets calculateInsets(InsetsState state, Rect frame,
205 SparseArray<InsetsSourceConsumer> consumers, boolean shown,
206 @Nullable @InsetSide SparseIntArray typeSideMap) {
207 for (int i = consumers.size() - 1; i >= 0; i--) {
208 state.getSource(consumers.valueAt(i).getType()).setVisible(shown);
209 }
210 return getInsetsFromState(state, frame, typeSideMap);
211 }
212
213 private Insets getInsetsFromState(InsetsState state, Rect frame,
214 @Nullable @InsetSide SparseIntArray typeSideMap) {
215 return state.calculateInsets(frame, false /* isScreenRound */,
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100216 false /* alwaysConsumerNavBar */, null /* displayCutout */,
Jorim Jaggi648e5882019-01-24 13:24:02 +0100217 null /* legacyContentInsets */, null /* legacyStableInsets */,
218 LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, typeSideMap)
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100219 .getInsets(mTypes);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100220 }
221
222 private Insets sanitize(Insets insets) {
223 return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets);
224 }
225
Jorim Jaggi67684882019-01-22 17:36:34 +0100226 private void updateLeashesForSide(@InsetSide int side, int offset, int inset,
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800227 ArrayList<SurfaceParams> surfaceParams, InsetsState state) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100228 ArraySet<InsetsSourceConsumer> items = mSideSourceMap.get(side);
229 // TODO: Implement behavior when inset spans over multiple types
230 for (int i = items.size() - 1; i >= 0; i--) {
231 final InsetsSourceConsumer consumer = items.valueAt(i);
232 final InsetsSource source = mInitialInsetsState.getSource(consumer.getType());
Tarandeep Singh215929b2019-01-11 18:24:37 -0800233 final InsetsSourceControl control = consumer.getControl();
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100234 final SurfaceControl leash = consumer.getControl().getLeash();
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800235
Tarandeep Singh215929b2019-01-11 18:24:37 -0800236 mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800237 mTmpFrame.set(source.getFrame());
Jorim Jaggi67684882019-01-22 17:36:34 +0100238 addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800239
240 state.getSource(source.getType()).setFrame(mTmpFrame);
Jorim Jaggi67684882019-01-22 17:36:34 +0100241 surfaceParams.add(new SurfaceParams(leash, 1f, mTmpMatrix, null, 0, 0f, inset != 0));
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100242 }
243 }
244
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800245 private void addTranslationToMatrix(@InsetSide int side, int inset, Matrix m, Rect frame) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100246 switch (side) {
247 case INSET_SIDE_LEFT:
248 m.postTranslate(-inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800249 frame.offset(-inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100250 break;
251 case INSET_SIDE_TOP:
252 m.postTranslate(0, -inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800253 frame.offset(0, -inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100254 break;
255 case INSET_SIDE_RIGHT:
256 m.postTranslate(inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800257 frame.offset(inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100258 break;
259 case INSET_SIDE_BOTTOM:
260 m.postTranslate(0, inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800261 frame.offset(0, inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100262 break;
263 }
264 }
265
266 private static void buildTypeSourcesMap(SparseIntArray typeSideMap,
267 SparseSetArray<InsetsSourceConsumer> sideSourcesMap,
268 SparseArray<InsetsSourceConsumer> consumers) {
269 for (int i = typeSideMap.size() - 1; i >= 0; i--) {
270 int type = typeSideMap.keyAt(i);
271 int side = typeSideMap.valueAt(i);
272 sideSourcesMap.add(side, consumers.get(type));
273 }
274 }
275}