Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
| 19 | import static android.view.InsetsState.INSET_SIDE_BOTTOM; |
Tarandeep Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 20 | import static android.view.InsetsState.INSET_SIDE_FLOATING; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 21 | import static android.view.InsetsState.INSET_SIDE_LEFT; |
| 22 | import static android.view.InsetsState.INSET_SIDE_RIGHT; |
| 23 | import static android.view.InsetsState.INSET_SIDE_TOP; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 24 | import static android.view.InsetsState.toPublicType; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 25 | |
| 26 | import android.annotation.Nullable; |
| 27 | import android.graphics.Insets; |
| 28 | import android.graphics.Matrix; |
| 29 | import android.graphics.Rect; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 30 | import android.util.ArraySet; |
| 31 | import android.util.SparseArray; |
| 32 | import android.util.SparseIntArray; |
| 33 | import android.util.SparseSetArray; |
| 34 | import android.view.InsetsState.InsetSide; |
| 35 | import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; |
| 36 | import android.view.WindowInsets.Type.InsetType; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 37 | import android.view.WindowInsetsAnimationListener.InsetsAnimation; |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 38 | import android.view.WindowManager.LayoutParams; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 39 | |
| 40 | import com.android.internal.annotations.VisibleForTesting; |
| 41 | |
| 42 | import java.util.ArrayList; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 43 | import java.util.function.Supplier; |
| 44 | |
| 45 | /** |
| 46 | * Implements {@link WindowInsetsAnimationController} |
| 47 | * @hide |
| 48 | */ |
| 49 | @VisibleForTesting |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 50 | public class InsetsAnimationControlImpl implements WindowInsetsAnimationController { |
| 51 | |
| 52 | private final Rect mTmpFrame = new Rect(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 53 | |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 68 | private final InsetsController mController; |
| 69 | private final WindowInsetsAnimationListener.InsetsAnimation mAnimation; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 70 | private final Rect mFrame; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 71 | private Insets mCurrentInsets; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 72 | private Insets mPendingInsets; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 73 | private boolean mFinished; |
| 74 | private boolean mCancelled; |
| 75 | private int mFinishedShownTypes; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 76 | |
| 77 | @VisibleForTesting |
| 78 | public InsetsAnimationControlImpl(SparseArray<InsetsSourceConsumer> consumers, Rect frame, |
| 79 | InsetsState state, WindowInsetsAnimationControlListener listener, |
| 80 | @InsetType int types, |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 81 | Supplier<SyncRtSurfaceTransactionApplier> transactionApplierSupplier, |
| 82 | InsetsController controller) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 83 | mConsumers = consumers; |
| 84 | mListener = listener; |
| 85 | mTypes = types; |
| 86 | mTransactionApplierSupplier = transactionApplierSupplier; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 87 | mController = controller; |
| 88 | mInitialInsetsState = new InsetsState(state, true /* copySources */); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 89 | 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 Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 94 | mFrame = new Rect(frame); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 95 | buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mConsumers); |
| 96 | |
| 97 | // TODO: Check for controllability first and wait for IME if needed. |
| 98 | listener.onReady(this, types); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 99 | |
| 100 | mAnimation = new WindowInsetsAnimationListener.InsetsAnimation(mTypes, mHiddenInsets, |
| 101 | mShownInsets); |
| 102 | mController.dispatchAnimationStarted(mAnimation); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 103 | } |
| 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 Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 128 | 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 136 | mPendingInsets = sanitize(insets); |
| 137 | mController.scheduleApplyChangeInsets(); |
| 138 | } |
| 139 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 140 | @VisibleForTesting |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 141 | /** |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 148 | final Insets offset = Insets.subtract(mShownInsets, mPendingInsets); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 149 | ArrayList<SurfaceParams> params = new ArrayList<>(); |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 150 | 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 Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 155 | updateLeashesForSide(INSET_SIDE_FLOATING, 0 /* offset */, 0 /* inset */, params, state); |
| 156 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 157 | SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get(); |
| 158 | applier.scheduleApply(params.toArray(new SurfaceParams[params.size()])); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 159 | mCurrentInsets = mPendingInsets; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 160 | if (mFinished) { |
| 161 | mController.notifyFinished(this, mFinishedShownTypes); |
| 162 | } |
| 163 | return mFinished; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void finish(int shownTypes) { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 168 | 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 182 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 183 | @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 Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 194 | } |
| 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 Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 208 | false /* alwaysConsumerNavBar */, null /* displayCutout */, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 209 | null /* legacyContentInsets */, null /* legacyStableInsets */, |
| 210 | LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, typeSideMap) |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 211 | .getInsets(mTypes); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private Insets sanitize(Insets insets) { |
| 215 | return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets); |
| 216 | } |
| 217 | |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 218 | private void updateLeashesForSide(@InsetSide int side, int offset, int inset, |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 219 | ArrayList<SurfaceParams> surfaceParams, InsetsState state) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 220 | ArraySet<InsetsSourceConsumer> items = mSideSourceMap.get(side); |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 221 | if (items == null) { |
| 222 | return; |
| 223 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 224 | // 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 Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 228 | final InsetsSourceControl control = consumer.getControl(); |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 229 | if (control == null) { |
| 230 | // Control may not be available for consumer yet or revoked. |
| 231 | continue; |
| 232 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 233 | final SurfaceControl leash = consumer.getControl().getLeash(); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 234 | |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 235 | mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 236 | mTmpFrame.set(source.getFrame()); |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 237 | addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 238 | |
| 239 | state.getSource(source.getType()).setFrame(mTmpFrame); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 240 | |
| 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 Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 244 | null /* windowCrop */, 0 /* layer */, 0f /* cornerRadius*/, |
| 245 | side == INSET_SIDE_FLOATING |
| 246 | ? consumer.isVisible() : inset != 0 /* visible */)); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 247 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 251 | private void addTranslationToMatrix(@InsetSide int side, int inset, Matrix m, Rect frame) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 252 | switch (side) { |
| 253 | case INSET_SIDE_LEFT: |
| 254 | m.postTranslate(-inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 255 | frame.offset(-inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 256 | break; |
| 257 | case INSET_SIDE_TOP: |
| 258 | m.postTranslate(0, -inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 259 | frame.offset(0, -inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 260 | break; |
| 261 | case INSET_SIDE_RIGHT: |
| 262 | m.postTranslate(inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 263 | frame.offset(inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 264 | break; |
| 265 | case INSET_SIDE_BOTTOM: |
| 266 | m.postTranslate(0, inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 267 | frame.offset(0, inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 268 | 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 Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 276 | 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 Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |