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; |
| 20 | import static android.view.InsetsState.INSET_SIDE_LEFT; |
| 21 | import static android.view.InsetsState.INSET_SIDE_RIGHT; |
| 22 | import static android.view.InsetsState.INSET_SIDE_TOP; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 23 | import static android.view.InsetsState.toPublicType; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 24 | |
| 25 | import android.annotation.Nullable; |
| 26 | import android.graphics.Insets; |
| 27 | import android.graphics.Matrix; |
| 28 | import android.graphics.Rect; |
| 29 | import android.os.UidProto.Sync; |
| 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; |
| 43 | import java.util.function.Function; |
| 44 | import java.util.function.Supplier; |
| 45 | |
| 46 | /** |
| 47 | * Implements {@link WindowInsetsAnimationController} |
| 48 | * @hide |
| 49 | */ |
| 50 | @VisibleForTesting |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 51 | public class InsetsAnimationControlImpl implements WindowInsetsAnimationController { |
| 52 | |
| 53 | private final Rect mTmpFrame = new Rect(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 54 | |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 69 | private final InsetsController mController; |
| 70 | private final WindowInsetsAnimationListener.InsetsAnimation mAnimation; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 71 | private final Rect mFrame; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 72 | private Insets mCurrentInsets; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 73 | private Insets mPendingInsets; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 74 | private boolean mFinished; |
| 75 | private boolean mCancelled; |
| 76 | private int mFinishedShownTypes; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 77 | |
| 78 | @VisibleForTesting |
| 79 | public InsetsAnimationControlImpl(SparseArray<InsetsSourceConsumer> consumers, Rect frame, |
| 80 | InsetsState state, WindowInsetsAnimationControlListener listener, |
| 81 | @InsetType int types, |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 82 | Supplier<SyncRtSurfaceTransactionApplier> transactionApplierSupplier, |
| 83 | InsetsController controller) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 84 | mConsumers = consumers; |
| 85 | mListener = listener; |
| 86 | mTypes = types; |
| 87 | mTransactionApplierSupplier = transactionApplierSupplier; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 88 | mController = controller; |
| 89 | mInitialInsetsState = new InsetsState(state, true /* copySources */); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 90 | 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 Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 95 | mFrame = new Rect(frame); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 96 | buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mConsumers); |
| 97 | |
| 98 | // TODO: Check for controllability first and wait for IME if needed. |
| 99 | listener.onReady(this, types); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 100 | |
| 101 | mAnimation = new WindowInsetsAnimationListener.InsetsAnimation(mTypes, mHiddenInsets, |
| 102 | mShownInsets); |
| 103 | mController.dispatchAnimationStarted(mAnimation); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 104 | } |
| 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 Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 129 | 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 137 | mPendingInsets = sanitize(insets); |
| 138 | mController.scheduleApplyChangeInsets(); |
| 139 | } |
| 140 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 141 | @VisibleForTesting |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 142 | /** |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 149 | final Insets offset = Insets.subtract(mShownInsets, mPendingInsets); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 150 | ArrayList<SurfaceParams> params = new ArrayList<>(); |
| 151 | if (offset.left != 0) { |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 152 | updateLeashesForSide(INSET_SIDE_LEFT, offset.left, mPendingInsets.left, params, state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 153 | } |
| 154 | if (offset.top != 0) { |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 155 | updateLeashesForSide(INSET_SIDE_TOP, offset.top, mPendingInsets.top, params, state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 156 | } |
| 157 | if (offset.right != 0) { |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 158 | updateLeashesForSide(INSET_SIDE_RIGHT, offset.right, mPendingInsets.right, params, |
| 159 | state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 160 | } |
| 161 | if (offset.bottom != 0) { |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 162 | updateLeashesForSide(INSET_SIDE_BOTTOM, offset.bottom, mPendingInsets.bottom, params, |
| 163 | state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 164 | } |
| 165 | SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get(); |
| 166 | applier.scheduleApply(params.toArray(new SurfaceParams[params.size()])); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 167 | mCurrentInsets = mPendingInsets; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 168 | if (mFinished) { |
| 169 | mController.notifyFinished(this, mFinishedShownTypes); |
| 170 | } |
| 171 | return mFinished; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void finish(int shownTypes) { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 176 | 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 190 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 191 | @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 Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 202 | } |
| 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 Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 216 | false /* alwaysConsumerNavBar */, null /* displayCutout */, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 217 | null /* legacyContentInsets */, null /* legacyStableInsets */, |
| 218 | LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, typeSideMap) |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 219 | .getInsets(mTypes); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private Insets sanitize(Insets insets) { |
| 223 | return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets); |
| 224 | } |
| 225 | |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 226 | private void updateLeashesForSide(@InsetSide int side, int offset, int inset, |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 227 | ArrayList<SurfaceParams> surfaceParams, InsetsState state) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 228 | 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 Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 233 | final InsetsSourceControl control = consumer.getControl(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 234 | final SurfaceControl leash = consumer.getControl().getLeash(); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 235 | |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 236 | mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 237 | mTmpFrame.set(source.getFrame()); |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 238 | addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 239 | |
| 240 | state.getSource(source.getType()).setFrame(mTmpFrame); |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 241 | surfaceParams.add(new SurfaceParams(leash, 1f, mTmpMatrix, null, 0, 0f, inset != 0)); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 245 | private void addTranslationToMatrix(@InsetSide int side, int inset, Matrix m, Rect frame) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 246 | switch (side) { |
| 247 | case INSET_SIDE_LEFT: |
| 248 | m.postTranslate(-inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 249 | frame.offset(-inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 250 | break; |
| 251 | case INSET_SIDE_TOP: |
| 252 | m.postTranslate(0, -inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 253 | frame.offset(0, -inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 254 | break; |
| 255 | case INSET_SIDE_RIGHT: |
| 256 | m.postTranslate(inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 257 | frame.offset(inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 258 | break; |
| 259 | case INSET_SIDE_BOTTOM: |
| 260 | m.postTranslate(0, inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 261 | frame.offset(0, inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 262 | 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 | } |