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; |
| 23 | |
| 24 | import android.annotation.Nullable; |
| 25 | import android.graphics.Insets; |
| 26 | import android.graphics.Matrix; |
| 27 | import android.graphics.Rect; |
| 28 | import android.os.UidProto.Sync; |
| 29 | import android.util.ArraySet; |
| 30 | import android.util.SparseArray; |
| 31 | import android.util.SparseIntArray; |
| 32 | import android.util.SparseSetArray; |
| 33 | import android.view.InsetsState.InsetSide; |
| 34 | import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; |
| 35 | import android.view.WindowInsets.Type.InsetType; |
| 36 | |
| 37 | import com.android.internal.annotations.VisibleForTesting; |
| 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.function.Function; |
| 41 | import java.util.function.Supplier; |
| 42 | |
| 43 | /** |
| 44 | * Implements {@link WindowInsetsAnimationController} |
| 45 | * @hide |
| 46 | */ |
| 47 | @VisibleForTesting |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 48 | public class InsetsAnimationControlImpl implements WindowInsetsAnimationController { |
| 49 | |
| 50 | private final Rect mTmpFrame = new Rect(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 51 | |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 66 | private final InsetsController mController; |
| 67 | private final WindowInsetsAnimationListener.InsetsAnimation mAnimation; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 68 | private Insets mCurrentInsets; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 69 | private Insets mPendingInsets; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 70 | |
| 71 | @VisibleForTesting |
| 72 | public InsetsAnimationControlImpl(SparseArray<InsetsSourceConsumer> consumers, Rect frame, |
| 73 | InsetsState state, WindowInsetsAnimationControlListener listener, |
| 74 | @InsetType int types, |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 75 | Supplier<SyncRtSurfaceTransactionApplier> transactionApplierSupplier, |
| 76 | InsetsController controller) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 77 | mConsumers = consumers; |
| 78 | mListener = listener; |
| 79 | mTypes = types; |
| 80 | mTransactionApplierSupplier = transactionApplierSupplier; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 81 | mController = controller; |
| 82 | mInitialInsetsState = new InsetsState(state, true /* copySources */); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 83 | 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 92 | |
| 93 | mAnimation = new WindowInsetsAnimationListener.InsetsAnimation(mTypes, mHiddenInsets, |
| 94 | mShownInsets); |
| 95 | mController.dispatchAnimationStarted(mAnimation); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 96 | } |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 121 | mPendingInsets = sanitize(insets); |
| 122 | mController.scheduleApplyChangeInsets(); |
| 123 | } |
| 124 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 125 | @VisibleForTesting |
| 126 | public void applyChangeInsets(InsetsState state) { |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 127 | final Insets offset = Insets.subtract(mShownInsets, mPendingInsets); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 128 | ArrayList<SurfaceParams> params = new ArrayList<>(); |
| 129 | if (offset.left != 0) { |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 130 | updateLeashesForSide(INSET_SIDE_LEFT, offset.left, params, state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 131 | } |
| 132 | if (offset.top != 0) { |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 133 | updateLeashesForSide(INSET_SIDE_TOP, offset.top, params, state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 134 | } |
| 135 | if (offset.right != 0) { |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 136 | updateLeashesForSide(INSET_SIDE_RIGHT, offset.right, params, state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 137 | } |
| 138 | if (offset.bottom != 0) { |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 139 | updateLeashesForSide(INSET_SIDE_BOTTOM, offset.bottom, params, state); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 140 | } |
| 141 | SyncRtSurfaceTransactionApplier applier = mTransactionApplierSupplier.get(); |
| 142 | applier.scheduleApply(params.toArray(new SurfaceParams[params.size()])); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 143 | mCurrentInsets = mPendingInsets; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void finish(int shownTypes) { |
| 148 | // TODO |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 149 | |
| 150 | mController.dispatchAnimationFinished(mAnimation); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 151 | } |
| 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 Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 165 | false /* alwaysConsumerNavBar */, null /* displayCutout */, |
| 166 | null /* legacyContentInsets */, null /* legacyStableInsets */, typeSideMap) |
| 167 | .getInsets(mTypes); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 168 | } |
| 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 175 | ArrayList<SurfaceParams> surfaceParams, InsetsState state) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 176 | 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 Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 183 | |
| 184 | mTmpFrame.set(source.getFrame()); |
| 185 | addTranslationToMatrix(side, inset, mTmpMatrix, mTmpFrame); |
| 186 | |
| 187 | state.getSource(source.getType()).setFrame(mTmpFrame); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 188 | surfaceParams.add(new SurfaceParams(leash, 1f, mTmpMatrix, null, 0, 0f)); |
| 189 | } |
| 190 | } |
| 191 | |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 192 | private void addTranslationToMatrix(@InsetSide int side, int inset, Matrix m, Rect frame) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 193 | switch (side) { |
| 194 | case INSET_SIDE_LEFT: |
| 195 | m.postTranslate(-inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 196 | frame.offset(-inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 197 | break; |
| 198 | case INSET_SIDE_TOP: |
| 199 | m.postTranslate(0, -inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 200 | frame.offset(0, -inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 201 | break; |
| 202 | case INSET_SIDE_RIGHT: |
| 203 | m.postTranslate(inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 204 | frame.offset(inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 205 | break; |
| 206 | case INSET_SIDE_BOTTOM: |
| 207 | m.postTranslate(0, inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 208 | frame.offset(0, inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 209 | 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 | |