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 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 19 | import static android.view.InsetsState.ISIDE_BOTTOM; |
| 20 | import static android.view.InsetsState.ISIDE_FLOATING; |
| 21 | import static android.view.InsetsState.ISIDE_LEFT; |
| 22 | import static android.view.InsetsState.ISIDE_RIGHT; |
| 23 | import static android.view.InsetsState.ISIDE_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; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 34 | import android.view.InsetsState.InternalInsetsSide; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 35 | import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 36 | import android.view.WindowInsets.Type.InsetsType; |
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; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 66 | private final @InsetsType int mTypes; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 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, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 80 | @InsetsType 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 |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 121 | @InsetsType public int getTypes() { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 122 | return mTypes; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void changeInsets(Insets insets) { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 127 | if (mFinished) { |
| 128 | throw new IllegalStateException( |
| 129 | "Can't change insets on an animation that is finished."); |
| 130 | } |
| 131 | if (mCancelled) { |
| 132 | throw new IllegalStateException( |
| 133 | "Can't change insets on an animation that is cancelled."); |
| 134 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 135 | mPendingInsets = sanitize(insets); |
| 136 | mController.scheduleApplyChangeInsets(); |
| 137 | } |
| 138 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 139 | @VisibleForTesting |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 140 | /** |
| 141 | * @return Whether the finish callback of this animation should be invoked. |
| 142 | */ |
| 143 | public boolean applyChangeInsets(InsetsState state) { |
| 144 | if (mCancelled) { |
| 145 | return false; |
| 146 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 147 | final Insets offset = Insets.subtract(mShownInsets, mPendingInsets); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 148 | ArrayList<SurfaceParams> params = new ArrayList<>(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 149 | updateLeashesForSide(ISIDE_LEFT, offset.left, mPendingInsets.left, params, state); |
| 150 | updateLeashesForSide(ISIDE_TOP, offset.top, mPendingInsets.top, params, state); |
| 151 | updateLeashesForSide(ISIDE_RIGHT, offset.right, mPendingInsets.right, params, state); |
| 152 | updateLeashesForSide(ISIDE_BOTTOM, offset.bottom, mPendingInsets.bottom, params, state); |
| 153 | updateLeashesForSide(ISIDE_FLOATING, 0 /* offset */, 0 /* inset */, params, state); |
Tarandeep Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 154 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 155 | SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get(); |
| 156 | applier.scheduleApply(params.toArray(new SurfaceParams[params.size()])); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 157 | mCurrentInsets = mPendingInsets; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 158 | if (mFinished) { |
| 159 | mController.notifyFinished(this, mFinishedShownTypes); |
| 160 | } |
| 161 | return mFinished; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void finish(int shownTypes) { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 166 | if (mCancelled) { |
| 167 | return; |
| 168 | } |
| 169 | InsetsState state = new InsetsState(mController.getState()); |
| 170 | for (int i = mConsumers.size() - 1; i >= 0; i--) { |
| 171 | InsetsSourceConsumer consumer = mConsumers.valueAt(i); |
| 172 | boolean visible = (shownTypes & toPublicType(consumer.getType())) != 0; |
| 173 | state.getSource(consumer.getType()).setVisible(visible); |
| 174 | } |
| 175 | Insets insets = getInsetsFromState(state, mFrame, null /* typeSideMap */); |
| 176 | changeInsets(insets); |
| 177 | mFinished = true; |
| 178 | mFinishedShownTypes = shownTypes; |
| 179 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 180 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 181 | @VisibleForTesting |
| 182 | public void onCancelled() { |
| 183 | if (mFinished) { |
| 184 | return; |
| 185 | } |
| 186 | mCancelled = true; |
| 187 | mListener.onCancelled(); |
| 188 | } |
| 189 | |
| 190 | InsetsAnimation getAnimation() { |
| 191 | return mAnimation; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private Insets calculateInsets(InsetsState state, Rect frame, |
| 195 | SparseArray<InsetsSourceConsumer> consumers, boolean shown, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 196 | @Nullable @InternalInsetsSide SparseIntArray typeSideMap) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 197 | for (int i = consumers.size() - 1; i >= 0; i--) { |
| 198 | state.getSource(consumers.valueAt(i).getType()).setVisible(shown); |
| 199 | } |
| 200 | return getInsetsFromState(state, frame, typeSideMap); |
| 201 | } |
| 202 | |
| 203 | private Insets getInsetsFromState(InsetsState state, Rect frame, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 204 | @Nullable @InternalInsetsSide SparseIntArray typeSideMap) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 205 | return state.calculateInsets(frame, false /* isScreenRound */, |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 206 | false /* alwaysConsumerNavBar */, null /* displayCutout */, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 207 | null /* legacyContentInsets */, null /* legacyStableInsets */, |
| 208 | LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, typeSideMap) |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 209 | .getInsets(mTypes); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | private Insets sanitize(Insets insets) { |
| 213 | return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets); |
| 214 | } |
| 215 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 216 | private void updateLeashesForSide(@InternalInsetsSide int side, int offset, int inset, |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 217 | ArrayList<SurfaceParams> surfaceParams, InsetsState state) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 218 | ArraySet<InsetsSourceConsumer> items = mSideSourceMap.get(side); |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 219 | if (items == null) { |
| 220 | return; |
| 221 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 222 | // TODO: Implement behavior when inset spans over multiple types |
| 223 | for (int i = items.size() - 1; i >= 0; i--) { |
| 224 | final InsetsSourceConsumer consumer = items.valueAt(i); |
| 225 | final InsetsSource source = mInitialInsetsState.getSource(consumer.getType()); |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 226 | final InsetsSourceControl control = consumer.getControl(); |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 227 | if (control == null) { |
| 228 | // Control may not be available for consumer yet or revoked. |
| 229 | continue; |
| 230 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 231 | final SurfaceControl leash = consumer.getControl().getLeash(); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 232 | |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 233 | mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 234 | mTmpFrame.set(source.getFrame()); |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 235 | addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 236 | |
| 237 | state.getSource(source.getType()).setFrame(mTmpFrame); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 238 | |
| 239 | // If the system is controlling the insets source, the leash can be null. |
| 240 | if (leash != null) { |
| 241 | surfaceParams.add(new SurfaceParams(leash, 1f /* alpha */, mTmpMatrix, |
Tarandeep Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 242 | null /* windowCrop */, 0 /* layer */, 0f /* cornerRadius*/, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 243 | side == ISIDE_FLOATING ? consumer.isVisible() : inset != 0 /* visible */)); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 244 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 248 | private void addTranslationToMatrix(@InternalInsetsSide int side, int inset, Matrix m, |
| 249 | Rect frame) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 250 | switch (side) { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 251 | case ISIDE_LEFT: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 252 | m.postTranslate(-inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 253 | frame.offset(-inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 254 | break; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 255 | case ISIDE_TOP: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 256 | m.postTranslate(0, -inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 257 | frame.offset(0, -inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 258 | break; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 259 | case ISIDE_RIGHT: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 260 | m.postTranslate(inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 261 | frame.offset(inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 262 | break; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 263 | case ISIDE_BOTTOM: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 264 | m.postTranslate(0, inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 265 | frame.offset(0, inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | private static void buildTypeSourcesMap(SparseIntArray typeSideMap, |
| 271 | SparseSetArray<InsetsSourceConsumer> sideSourcesMap, |
| 272 | SparseArray<InsetsSourceConsumer> consumers) { |
| 273 | for (int i = typeSideMap.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 274 | final int type = typeSideMap.keyAt(i); |
| 275 | final int side = typeSideMap.valueAt(i); |
| 276 | final InsetsSourceConsumer consumer = consumers.get(type); |
| 277 | if (consumer == null) { |
| 278 | // If the types that we are controlling are less than the types that the system has, |
| 279 | // there can be some null consumers. |
| 280 | continue; |
| 281 | } |
| 282 | sideSourcesMap.add(side, consumer); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |