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 | |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 19 | import static android.view.InsetsController.LAYOUT_INSETS_DURING_ANIMATION_HIDDEN; |
| 20 | import static android.view.InsetsController.LAYOUT_INSETS_DURING_ANIMATION_SHOWN; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 21 | import static android.view.InsetsState.ISIDE_BOTTOM; |
| 22 | import static android.view.InsetsState.ISIDE_FLOATING; |
| 23 | import static android.view.InsetsState.ISIDE_LEFT; |
| 24 | import static android.view.InsetsState.ISIDE_RIGHT; |
| 25 | import static android.view.InsetsState.ISIDE_TOP; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 26 | |
| 27 | import android.annotation.Nullable; |
| 28 | import android.graphics.Insets; |
| 29 | import android.graphics.Matrix; |
| 30 | import android.graphics.Rect; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 31 | import android.util.ArraySet; |
| 32 | import android.util.SparseArray; |
| 33 | import android.util.SparseIntArray; |
| 34 | import android.util.SparseSetArray; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 35 | import android.view.InsetsController.LayoutInsetsDuringAnimation; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 36 | import android.view.InsetsState.InternalInsetsSide; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 37 | import android.view.InsetsState.InternalInsetsType; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 38 | import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 39 | import android.view.WindowInsets.Type.InsetsType; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 40 | import android.view.WindowInsetsAnimationCallback.AnimationBounds; |
| 41 | import android.view.WindowInsetsAnimationCallback.InsetsAnimation; |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 42 | import android.view.WindowManager.LayoutParams; |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 43 | import android.view.animation.Interpolator; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 44 | |
| 45 | import com.android.internal.annotations.VisibleForTesting; |
| 46 | |
| 47 | import java.util.ArrayList; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Implements {@link WindowInsetsAnimationController} |
| 51 | * @hide |
| 52 | */ |
| 53 | @VisibleForTesting |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 54 | public class InsetsAnimationControlImpl implements WindowInsetsAnimationController { |
| 55 | |
| 56 | private final Rect mTmpFrame = new Rect(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 57 | |
| 58 | private final WindowInsetsAnimationControlListener mListener; |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 59 | private final SparseArray<InsetsSourceControl> mControls; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 60 | private final SparseIntArray mTypeSideMap = new SparseIntArray(); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 61 | private final SparseSetArray<InsetsSourceControl> mSideSourceMap = new SparseSetArray<>(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 62 | |
| 63 | /** @see WindowInsetsAnimationController#getHiddenStateInsets */ |
| 64 | private final Insets mHiddenInsets; |
| 65 | |
| 66 | /** @see WindowInsetsAnimationController#getShownStateInsets */ |
| 67 | private final Insets mShownInsets; |
| 68 | private final Matrix mTmpMatrix = new Matrix(); |
| 69 | private final InsetsState mInitialInsetsState; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 70 | private final @InsetsType int mTypes; |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 71 | private final InsetsAnimationControlCallbacks mController; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 72 | private final WindowInsetsAnimationCallback.InsetsAnimation mAnimation; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 73 | private final Rect mFrame; |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 74 | private final boolean mFade; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 75 | private Insets mCurrentInsets; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 76 | private Insets mPendingInsets; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 77 | private float mPendingFraction; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 78 | private boolean mFinished; |
| 79 | private boolean mCancelled; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 80 | private boolean mShownOnFinish; |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 81 | private float mCurrentAlpha; |
| 82 | private float mPendingAlpha; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 83 | |
| 84 | @VisibleForTesting |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 85 | public InsetsAnimationControlImpl(SparseArray<InsetsSourceControl> controls, Rect frame, |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 86 | InsetsState state, WindowInsetsAnimationControlListener listener, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 87 | @InsetsType int types, |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 88 | InsetsAnimationControlCallbacks controller, long durationMs, Interpolator interpolator, |
| 89 | boolean fade, @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation) { |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 90 | mControls = controls; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 91 | mListener = listener; |
| 92 | mTypes = types; |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 93 | mFade = fade; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 94 | mController = controller; |
| 95 | mInitialInsetsState = new InsetsState(state, true /* copySources */); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 96 | mCurrentInsets = getInsetsFromState(mInitialInsetsState, frame, null /* typeSideMap */); |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 97 | mPendingInsets = mCurrentInsets; |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 98 | mHiddenInsets = calculateInsets(mInitialInsetsState, frame, controls, false /* shown */, |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 99 | null /* typeSideMap */); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 100 | mShownInsets = calculateInsets(mInitialInsetsState, frame, controls, true /* shown */, |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 101 | mTypeSideMap); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 102 | mFrame = new Rect(frame); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 103 | buildTypeSourcesMap(mTypeSideMap, mSideSourceMap, mControls); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 104 | |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 105 | mAnimation = new WindowInsetsAnimationCallback.InsetsAnimation(mTypes, interpolator, |
| 106 | durationMs); |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 107 | mAnimation.setAlpha(getCurrentAlpha()); |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 108 | mController.startAnimation(this, listener, types, mAnimation, |
| 109 | new AnimationBounds(mHiddenInsets, mShownInsets), layoutInsetsDuringAnimation); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Insets getHiddenStateInsets() { |
| 114 | return mHiddenInsets; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Insets getShownStateInsets() { |
| 119 | return mShownInsets; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Insets getCurrentInsets() { |
| 124 | return mCurrentInsets; |
| 125 | } |
| 126 | |
| 127 | @Override |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 128 | public float getCurrentAlpha() { |
| 129 | return mCurrentAlpha; |
| 130 | } |
| 131 | |
| 132 | @Override |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 133 | @InsetsType public int getTypes() { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 134 | return mTypes; |
| 135 | } |
| 136 | |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 137 | boolean controlsInternalType(@InternalInsetsType int type) { |
| 138 | return InsetsState.toInternalType(mTypes).contains(type); |
| 139 | } |
| 140 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 141 | @Override |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 142 | public void setInsetsAndAlpha(Insets insets, float alpha, float fraction) { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 143 | if (mFinished) { |
| 144 | throw new IllegalStateException( |
| 145 | "Can't change insets on an animation that is finished."); |
| 146 | } |
| 147 | if (mCancelled) { |
| 148 | throw new IllegalStateException( |
| 149 | "Can't change insets on an animation that is cancelled."); |
| 150 | } |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 151 | mPendingFraction = sanitize(fraction); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 152 | mPendingInsets = sanitize(insets); |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 153 | mPendingAlpha = 1 - sanitize(alpha); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 154 | mController.scheduleApplyChangeInsets(); |
| 155 | } |
| 156 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 157 | @VisibleForTesting |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 158 | /** |
| 159 | * @return Whether the finish callback of this animation should be invoked. |
| 160 | */ |
| 161 | public boolean applyChangeInsets(InsetsState state) { |
| 162 | if (mCancelled) { |
| 163 | return false; |
| 164 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 165 | final Insets offset = Insets.subtract(mShownInsets, mPendingInsets); |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 166 | final Float alphaOffset = 1 - mPendingAlpha; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 167 | ArrayList<SurfaceParams> params = new ArrayList<>(); |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 168 | updateLeashesForSide(ISIDE_LEFT, offset.left, mShownInsets.left, mPendingInsets.left, |
| 169 | params, state, alphaOffset); |
| 170 | updateLeashesForSide(ISIDE_TOP, offset.top, mShownInsets.top, mPendingInsets.top, params, |
| 171 | state, alphaOffset); |
| 172 | updateLeashesForSide(ISIDE_RIGHT, offset.right, mShownInsets.right, mPendingInsets.right, |
| 173 | params, state, alphaOffset); |
| 174 | updateLeashesForSide(ISIDE_BOTTOM, offset.bottom, mShownInsets.bottom, |
| 175 | mPendingInsets.bottom, params, state, alphaOffset); |
| 176 | updateLeashesForSide(ISIDE_FLOATING, 0 /* offset */, 0 /* inset */, 0 /* maxInset */, |
| 177 | params, state, alphaOffset); |
Tarandeep Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 178 | |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 179 | mController.applySurfaceParams(params.toArray(new SurfaceParams[params.size()])); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 180 | mCurrentInsets = mPendingInsets; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 181 | mAnimation.setFraction(mPendingFraction); |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 182 | mCurrentAlpha = 1 - alphaOffset; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 183 | if (mFinished) { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 184 | mController.notifyFinished(this, mShownOnFinish); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 185 | } |
| 186 | return mFinished; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 190 | public void finish(boolean shown) { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 191 | if (mCancelled) { |
| 192 | return; |
| 193 | } |
Jorim Jaggi | 235728e | 2020-01-28 13:52:10 +0100 | [diff] [blame] | 194 | setInsetsAndAlpha(shown ? mShownInsets : mHiddenInsets, 1f /* alpha */, 1f /* fraction */); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 195 | mFinished = true; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 196 | mShownOnFinish = shown; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 197 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 198 | |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 199 | @Override |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 200 | @VisibleForTesting |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 201 | public float getCurrentFraction() { |
| 202 | return mAnimation.getFraction(); |
| 203 | } |
| 204 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 205 | public void onCancelled() { |
| 206 | if (mFinished) { |
| 207 | return; |
| 208 | } |
| 209 | mCancelled = true; |
| 210 | mListener.onCancelled(); |
| 211 | } |
| 212 | |
| 213 | InsetsAnimation getAnimation() { |
| 214 | return mAnimation; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 217 | WindowInsetsAnimationControlListener getListener() { |
| 218 | return mListener; |
| 219 | } |
| 220 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 221 | private Insets calculateInsets(InsetsState state, Rect frame, |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 222 | SparseArray<InsetsSourceControl> controls, boolean shown, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 223 | @Nullable @InternalInsetsSide SparseIntArray typeSideMap) { |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 224 | for (int i = controls.size() - 1; i >= 0; i--) { |
| 225 | // control may be null if it got revoked. |
| 226 | if (controls.valueAt(i) == null) continue; |
| 227 | state.getSource(controls.valueAt(i).getType()).setVisible(shown); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 228 | } |
| 229 | return getInsetsFromState(state, frame, typeSideMap); |
| 230 | } |
| 231 | |
| 232 | private Insets getInsetsFromState(InsetsState state, Rect frame, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 233 | @Nullable @InternalInsetsSide SparseIntArray typeSideMap) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 234 | return state.calculateInsets(frame, false /* isScreenRound */, |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 235 | false /* alwaysConsumeSystemBars */, null /* displayCutout */, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 236 | null /* legacyContentInsets */, null /* legacyStableInsets */, |
Jorim Jaggi | 7f76187 | 2020-01-10 18:24:27 +0100 | [diff] [blame] | 237 | LayoutParams.SOFT_INPUT_ADJUST_RESIZE /* legacySoftInputMode*/, |
| 238 | 0 /* legacySystemUiFlags */, typeSideMap) |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 239 | .getInsets(mTypes); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private Insets sanitize(Insets insets) { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 243 | if (insets == null) { |
| 244 | insets = getCurrentInsets(); |
| 245 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 246 | return Insets.max(Insets.min(insets, mShownInsets), mHiddenInsets); |
| 247 | } |
| 248 | |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 249 | private static float sanitize(float alpha) { |
| 250 | return alpha >= 1 ? 1 : (alpha <= 0 ? 0 : alpha); |
| 251 | } |
| 252 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 253 | private void updateLeashesForSide(@InternalInsetsSide int side, int offset, int inset, |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 254 | int maxInset, ArrayList<SurfaceParams> surfaceParams, InsetsState state, Float alpha) { |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 255 | ArraySet<InsetsSourceControl> items = mSideSourceMap.get(side); |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 256 | if (items == null) { |
| 257 | return; |
| 258 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 259 | // TODO: Implement behavior when inset spans over multiple types |
| 260 | for (int i = items.size() - 1; i >= 0; i--) { |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 261 | final InsetsSourceControl control = items.valueAt(i); |
| 262 | final InsetsSource source = mInitialInsetsState.getSource(control.getType()); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 263 | final SurfaceControl leash = control.getLeash(); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 264 | |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 265 | mTmpMatrix.setTranslate(control.getSurfacePosition().x, control.getSurfacePosition().y); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 266 | mTmpFrame.set(source.getFrame()); |
Jorim Jaggi | 6768488 | 2019-01-22 17:36:34 +0100 | [diff] [blame] | 267 | addTranslationToMatrix(side, offset, mTmpMatrix, mTmpFrame); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 268 | |
| 269 | state.getSource(source.getType()).setFrame(mTmpFrame); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 270 | |
| 271 | // If the system is controlling the insets source, the leash can be null. |
| 272 | if (leash != null) { |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 273 | // TODO: use a better interpolation for fade. |
| 274 | alpha = mFade ? ((float) maxInset / inset * 0.3f + 0.7f) : alpha; |
| 275 | surfaceParams.add(new SurfaceParams(leash, alpha, mTmpMatrix, |
Tarandeep Singh | 95a3dbf | 2019-10-22 10:39:24 -0700 | [diff] [blame] | 276 | null /* windowCrop */, 0 /* layer */, 0f /* cornerRadius*/, |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 277 | side == ISIDE_FLOATING ? state.getSource(source.getType()).isVisible() |
| 278 | : inset != 0 /* visible */)); |
Jorim Jaggi | a12ea56 | 2019-01-07 17:47:47 +0100 | [diff] [blame] | 279 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 283 | private void addTranslationToMatrix(@InternalInsetsSide int side, int inset, Matrix m, |
| 284 | Rect frame) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 285 | switch (side) { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 286 | case ISIDE_LEFT: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 287 | m.postTranslate(-inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 288 | frame.offset(-inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 289 | break; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 290 | case ISIDE_TOP: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 291 | m.postTranslate(0, -inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 292 | frame.offset(0, -inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 293 | break; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 294 | case ISIDE_RIGHT: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 295 | m.postTranslate(inset, 0); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 296 | frame.offset(inset, 0); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 297 | break; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 298 | case ISIDE_BOTTOM: |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 299 | m.postTranslate(0, inset); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 300 | frame.offset(0, inset); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private static void buildTypeSourcesMap(SparseIntArray typeSideMap, |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 306 | SparseSetArray<InsetsSourceControl> sideSourcesMap, |
| 307 | SparseArray<InsetsSourceControl> controls) { |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 308 | for (int i = typeSideMap.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 309 | final int type = typeSideMap.keyAt(i); |
| 310 | final int side = typeSideMap.valueAt(i); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 311 | final InsetsSourceControl control = controls.get(type); |
| 312 | if (control == null) { |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 313 | // If the types that we are controlling are less than the types that the system has, |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 314 | // there can be some null controllers. |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 315 | continue; |
| 316 | } |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 317 | sideSourcesMap.add(side, control); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |