blob: 7291d0b2f10cc1402f0d490f3c9edda4e89ece82 [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;
23
24import android.annotation.Nullable;
25import android.graphics.Insets;
26import android.graphics.Matrix;
27import android.graphics.Rect;
28import android.os.UidProto.Sync;
29import android.util.ArraySet;
30import android.util.SparseArray;
31import android.util.SparseIntArray;
32import android.util.SparseSetArray;
33import android.view.InsetsState.InsetSide;
34import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams;
35import android.view.WindowInsets.Type.InsetType;
36
37import com.android.internal.annotations.VisibleForTesting;
38
39import java.util.ArrayList;
40import java.util.function.Function;
41import java.util.function.Supplier;
42
43/**
44 * Implements {@link WindowInsetsAnimationController}
45 * @hide
46 */
47@VisibleForTesting
Jorim Jaggi02a741f2018-12-12 17:40:19 -080048public class InsetsAnimationControlImpl implements WindowInsetsAnimationController {
49
50 private final Rect mTmpFrame = new Rect();
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010051
52 private final WindowInsetsAnimationControlListener mListener;
53 private final SparseArray<InsetsSourceConsumer> mConsumers;
54 private final SparseIntArray mTypeSideMap = new SparseIntArray();
55 private final SparseSetArray<InsetsSourceConsumer> mSideSourceMap = new SparseSetArray<>();
56
57 /** @see WindowInsetsAnimationController#getHiddenStateInsets */
58 private final Insets mHiddenInsets;
59
60 /** @see WindowInsetsAnimationController#getShownStateInsets */
61 private final Insets mShownInsets;
62 private final Matrix mTmpMatrix = new Matrix();
63 private final InsetsState mInitialInsetsState;
64 private final @InsetType int mTypes;
65 private final Supplier<SyncRtSurfaceTransactionApplier> mTransactionApplierSupplier;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080066 private final InsetsController mController;
67 private final WindowInsetsAnimationListener.InsetsAnimation mAnimation;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010068 private Insets mCurrentInsets;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080069 private Insets mPendingInsets;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010070
71 @VisibleForTesting
72 public InsetsAnimationControlImpl(SparseArray<InsetsSourceConsumer> consumers, Rect frame,
73 InsetsState state, WindowInsetsAnimationControlListener listener,
74 @InsetType int types,
Jorim Jaggi02a741f2018-12-12 17:40:19 -080075 Supplier<SyncRtSurfaceTransactionApplier> transactionApplierSupplier,
76 InsetsController controller) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010077 mConsumers = consumers;
78 mListener = listener;
79 mTypes = types;
80 mTransactionApplierSupplier = transactionApplierSupplier;
Jorim Jaggi02a741f2018-12-12 17:40:19 -080081 mController = controller;
82 mInitialInsetsState = new InsetsState(state, true /* copySources */);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010083 mCurrentInsets = getInsetsFromState(mInitialInsetsState, frame, null /* typeSideMap */);
84 mHiddenInsets = calculateInsets(mInitialInsetsState, frame, consumers, false /* shown */,
85 null /* typeSideMap */);
86 mShownInsets = calculateInsets(mInitialInsetsState, frame, consumers, true /* shown */,
87 mTypeSideMap);
88 buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mConsumers);
89
90 // TODO: Check for controllability first and wait for IME if needed.
91 listener.onReady(this, types);
Jorim Jaggi02a741f2018-12-12 17:40:19 -080092
93 mAnimation = new WindowInsetsAnimationListener.InsetsAnimation(mTypes, mHiddenInsets,
94 mShownInsets);
95 mController.dispatchAnimationStarted(mAnimation);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010096 }
97
98 @Override
99 public Insets getHiddenStateInsets() {
100 return mHiddenInsets;
101 }
102
103 @Override
104 public Insets getShownStateInsets() {
105 return mShownInsets;
106 }
107
108 @Override
109 public Insets getCurrentInsets() {
110 return mCurrentInsets;
111 }
112
113 @Override
114 @InsetType
115 public int getTypes() {
116 return mTypes;
117 }
118
119 @Override
120 public void changeInsets(Insets insets) {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800121 mPendingInsets = sanitize(insets);
122 mController.scheduleApplyChangeInsets();
123 }
124
Jorim Jaggifae3e272019-01-14 14:05:05 +0100125 @VisibleForTesting
126 public void applyChangeInsets(InsetsState state) {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800127 final Insets offset = Insets.subtract(mShownInsets, mPendingInsets);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100128 ArrayList<SurfaceParams> params = new ArrayList<>();
129 if (offset.left != 0) {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800130 updateLeashesForSide(INSET_SIDE_LEFT, offset.left, params, state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100131 }
132 if (offset.top != 0) {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800133 updateLeashesForSide(INSET_SIDE_TOP, offset.top, params, state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100134 }
135 if (offset.right != 0) {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800136 updateLeashesForSide(INSET_SIDE_RIGHT, offset.right, params, state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100137 }
138 if (offset.bottom != 0) {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800139 updateLeashesForSide(INSET_SIDE_BOTTOM, offset.bottom, params, state);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100140 }
141 SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get();
142 applier.scheduleApply(params.toArray(new SurfaceParams[params.size()]));
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800143 mCurrentInsets = mPendingInsets;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100144 }
145
146 @Override
147 public void finish(int shownTypes) {
148 // TODO
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800149
150 mController.dispatchAnimationFinished(mAnimation);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100151 }
152
153 private Insets calculateInsets(InsetsState state, Rect frame,
154 SparseArray<InsetsSourceConsumer> consumers, boolean shown,
155 @Nullable @InsetSide SparseIntArray typeSideMap) {
156 for (int i = consumers.size() - 1; i >= 0; i--) {
157 state.getSource(consumers.valueAt(i).getType()).setVisible(shown);
158 }
159 return getInsetsFromState(state, frame, typeSideMap);
160 }
161
162 private Insets getInsetsFromState(InsetsState state, Rect frame,
163 @Nullable @InsetSide SparseIntArray typeSideMap) {
164 return state.calculateInsets(frame, false /* isScreenRound */,
Jorim Jaggi73f3e8a2019-01-14 13:06:23 +0100165 false /* alwaysConsumerNavBar */, null /* displayCutout */,
166 null /* legacyContentInsets */, null /* legacyStableInsets */, typeSideMap)
167 .getInsets(mTypes);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100168 }
169
170 private Insets sanitize(Insets insets) {
171 return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets);
172 }
173
174 private void updateLeashesForSide(@InsetSide int side, int inset,
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800175 ArrayList<SurfaceParams> surfaceParams, InsetsState state) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100176 ArraySet<InsetsSourceConsumer> items = mSideSourceMap.get(side);
177 // TODO: Implement behavior when inset spans over multiple types
178 for (int i = items.size() - 1; i >= 0; i--) {
179 final InsetsSourceConsumer consumer = items.valueAt(i);
180 final InsetsSource source = mInitialInsetsState.getSource(consumer.getType());
181 final SurfaceControl leash = consumer.getControl().getLeash();
182 mTmpMatrix.setTranslate(source.getFrame().left, source.getFrame().top);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800183
184 mTmpFrame.set(source.getFrame());
185 addTranslationToMatrix(side, inset, mTmpMatrix, mTmpFrame);
186
187 state.getSource(source.getType()).setFrame(mTmpFrame);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100188 surfaceParams.add(new SurfaceParams(leash, 1f, mTmpMatrix, null, 0, 0f));
189 }
190 }
191
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800192 private void addTranslationToMatrix(@InsetSide int side, int inset, Matrix m, Rect frame) {
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100193 switch (side) {
194 case INSET_SIDE_LEFT:
195 m.postTranslate(-inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800196 frame.offset(-inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100197 break;
198 case INSET_SIDE_TOP:
199 m.postTranslate(0, -inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800200 frame.offset(0, -inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100201 break;
202 case INSET_SIDE_RIGHT:
203 m.postTranslate(inset, 0);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800204 frame.offset(inset, 0);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100205 break;
206 case INSET_SIDE_BOTTOM:
207 m.postTranslate(0, inset);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800208 frame.offset(0, inset);
Jorim Jaggi5bb571d2018-11-06 14:42:04 +0100209 break;
210 }
211 }
212
213 private static void buildTypeSourcesMap(SparseIntArray typeSideMap,
214 SparseSetArray<InsetsSourceConsumer> sideSourcesMap,
215 SparseArray<InsetsSourceConsumer> consumers) {
216 for (int i = typeSideMap.size() - 1; i >= 0; i--) {
217 int type = typeSideMap.keyAt(i);
218 int side = typeSideMap.valueAt(i);
219 sideSourcesMap.add(side, consumers.get(type));
220 }
221 }
222}
223