Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [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 | |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 19 | import static android.view.InsetsState.ITYPE_CAPTION_BAR; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 20 | import static android.view.InsetsState.ITYPE_IME; |
Jorim Jaggi | 33a2183 | 2020-04-06 14:15:46 +0200 | [diff] [blame] | 21 | import static android.view.InsetsState.toInternalType; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 22 | import static android.view.InsetsState.toPublicType; |
| 23 | import static android.view.WindowInsets.Type.all; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 24 | import static android.view.WindowInsets.Type.ime; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 25 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 26 | import android.animation.AnimationHandler; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 27 | import android.animation.Animator; |
| 28 | import android.animation.AnimatorListenerAdapter; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 29 | import android.animation.TypeEvaluator; |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 30 | import android.animation.ValueAnimator; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 31 | import android.annotation.IntDef; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 32 | import android.annotation.NonNull; |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 33 | import android.annotation.Nullable; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 34 | import android.graphics.Insets; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 35 | import android.graphics.Rect; |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 36 | import android.os.CancellationSignal; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 37 | import android.os.Handler; |
Jorim Jaggi | cb28ae6 | 2020-05-14 17:46:32 +0200 | [diff] [blame^] | 38 | import android.os.Trace; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 39 | import android.util.ArraySet; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 40 | import android.util.Pair; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 41 | import android.util.SparseArray; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 42 | import android.view.InsetsSourceConsumer.ShowResult; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 43 | import android.view.InsetsState.InternalInsetsType; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 44 | import android.view.SurfaceControl.Transaction; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 45 | import android.view.WindowInsets.Type; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 46 | import android.view.WindowInsets.Type.InsetsType; |
Adrian Roos | db5b0c2 | 2020-02-12 15:05:27 -0800 | [diff] [blame] | 47 | import android.view.WindowInsetsAnimation.Bounds; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 48 | import android.view.WindowManager.LayoutParams.SoftInputModeFlags; |
Jorim Jaggi | 7974259 | 2019-01-18 17:36:10 +0100 | [diff] [blame] | 49 | import android.view.animation.Interpolator; |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 50 | import android.view.animation.LinearInterpolator; |
Jorim Jaggi | 7974259 | 2019-01-18 17:36:10 +0100 | [diff] [blame] | 51 | import android.view.animation.PathInterpolator; |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 52 | import android.view.inputmethod.InputMethodManager; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 53 | |
| 54 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 55 | import com.android.internal.graphics.SfVsyncFrameCallbackProvider; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 56 | |
| 57 | import java.io.PrintWriter; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 58 | import java.lang.annotation.Retention; |
| 59 | import java.lang.annotation.RetentionPolicy; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 60 | import java.util.ArrayList; |
Adrian Roos | db5b0c2 | 2020-02-12 15:05:27 -0800 | [diff] [blame] | 61 | import java.util.Collections; |
| 62 | import java.util.List; |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 63 | import java.util.Objects; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 64 | import java.util.function.BiFunction; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Implements {@link WindowInsetsController} on the client. |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 68 | * @hide |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 69 | */ |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 70 | public class InsetsController implements WindowInsetsController, InsetsAnimationControlCallbacks { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 71 | |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 72 | public interface Host { |
| 73 | |
| 74 | Handler getHandler(); |
| 75 | |
| 76 | /** |
| 77 | * Notifies host that {@link InsetsController#getState()} has changed. |
| 78 | */ |
| 79 | void notifyInsetsChanged(); |
| 80 | |
| 81 | void dispatchWindowInsetsAnimationPrepare(@NonNull WindowInsetsAnimation animation); |
| 82 | Bounds dispatchWindowInsetsAnimationStart( |
| 83 | @NonNull WindowInsetsAnimation animation, @NonNull Bounds bounds); |
| 84 | WindowInsets dispatchWindowInsetsAnimationProgress(@NonNull WindowInsets insets, |
| 85 | @NonNull List<WindowInsetsAnimation> runningAnimations); |
| 86 | void dispatchWindowInsetsAnimationEnd(@NonNull WindowInsetsAnimation animation); |
| 87 | |
| 88 | /** |
| 89 | * Requests host to apply surface params in synchronized manner. |
| 90 | */ |
| 91 | void applySurfaceParams(final SyncRtSurfaceTransactionApplier.SurfaceParams... params); |
| 92 | |
| 93 | /** |
| 94 | * @see ViewRootImpl#updateCompatSysUiVisibility(int, boolean, boolean) |
| 95 | */ |
| 96 | void updateCompatSysUiVisibility(@InternalInsetsType int type, boolean visible, |
| 97 | boolean hasControl); |
| 98 | |
| 99 | /** |
| 100 | * Called when insets have been modified by the client and should be reported back to WM. |
| 101 | */ |
| 102 | void onInsetsModified(InsetsState insetsState); |
| 103 | |
| 104 | /** |
| 105 | * @return Whether the host has any callbacks it wants to synchronize the animations with. |
| 106 | * If there are no callbacks, the animation will be off-loaded to another thread and |
| 107 | * slightly different animation curves are picked. |
| 108 | */ |
| 109 | boolean hasAnimationCallbacks(); |
| 110 | |
| 111 | /** |
| 112 | * @see WindowInsetsController#setSystemBarsAppearance |
| 113 | */ |
| 114 | void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask); |
| 115 | |
| 116 | /** |
| 117 | * @see WindowInsetsController#getSystemBarsAppearance() |
| 118 | */ |
| 119 | @Appearance int getSystemBarsAppearance(); |
| 120 | |
| 121 | /** |
| 122 | * @see WindowInsetsController#setSystemBarsBehavior |
| 123 | */ |
| 124 | void setSystemBarsBehavior(@Behavior int behavior); |
| 125 | |
| 126 | /** |
| 127 | * @see WindowInsetsController#getSystemBarsBehavior |
| 128 | */ |
| 129 | @Behavior int getSystemBarsBehavior(); |
| 130 | |
| 131 | /** |
| 132 | * Releases a surface and ensure that this is done after {@link #applySurfaceParams} has |
| 133 | * finished applying params. |
| 134 | */ |
| 135 | void releaseSurfaceControlFromRt(SurfaceControl surfaceControl); |
| 136 | |
| 137 | /** |
| 138 | * If this host is a view hierarchy, adds a pre-draw runnable to ensure proper ordering as |
| 139 | * described in {@link WindowInsetsAnimation.Callback#onPrepare}. |
| 140 | * |
| 141 | * If this host isn't a view hierarchy, the runnable can be executed immediately. |
| 142 | */ |
| 143 | void addOnPreDrawRunnable(Runnable r); |
| 144 | |
| 145 | /** |
| 146 | * Adds a runnbale to be executed during {@link Choreographer#CALLBACK_INSETS_ANIMATION} |
| 147 | * phase. |
| 148 | */ |
| 149 | void postInsetsAnimationCallback(Runnable r); |
| 150 | |
| 151 | /** |
| 152 | * Obtains {@link InputMethodManager} instance from host. |
| 153 | */ |
| 154 | InputMethodManager getInputMethodManager(); |
| 155 | } |
| 156 | |
Jorim Jaggi | 7974259 | 2019-01-18 17:36:10 +0100 | [diff] [blame] | 157 | private static final int ANIMATION_DURATION_SHOW_MS = 275; |
| 158 | private static final int ANIMATION_DURATION_HIDE_MS = 340; |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 159 | |
| 160 | private static final int ANIMATION_DURATION_SYNC_IME_MS = 285; |
| 161 | private static final int ANIMATION_DURATION_UNSYNC_IME_MS = 200; |
| 162 | |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 163 | private static final int PENDING_CONTROL_TIMEOUT_MS = 2000; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 164 | |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 165 | public static final Interpolator SYSTEM_BARS_INTERPOLATOR = |
| 166 | new PathInterpolator(0.4f, 0f, 0.2f, 1f); |
| 167 | private static final Interpolator SYNC_IME_INTERPOLATOR = |
| 168 | new PathInterpolator(0.2f, 0f, 0f, 1f); |
| 169 | private static final Interpolator LINEAR_OUT_SLOW_IN_INTERPOLATOR = |
| 170 | new PathInterpolator(0, 0, 0.2f, 1f); |
| 171 | private static final Interpolator FAST_OUT_LINEAR_IN_INTERPOLATOR = |
| 172 | new PathInterpolator(0.4f, 0f, 1f, 1f); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 173 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 174 | /** |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 175 | * Layout mode during insets animation: The views should be laid out as if the changing inset |
| 176 | * types are fully shown. Before starting the animation, {@link View#onApplyWindowInsets} will |
| 177 | * be called as if the changing insets types are shown, which will result in the views being |
| 178 | * laid out as if the insets are fully shown. |
| 179 | */ |
Yunfan Chen | b5d2db7 | 2019-12-06 15:43:43 +0900 | [diff] [blame] | 180 | public static final int LAYOUT_INSETS_DURING_ANIMATION_SHOWN = 0; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * Layout mode during insets animation: The views should be laid out as if the changing inset |
| 184 | * types are fully hidden. Before starting the animation, {@link View#onApplyWindowInsets} will |
| 185 | * be called as if the changing insets types are hidden, which will result in the views being |
| 186 | * laid out as if the insets are fully hidden. |
| 187 | */ |
Yunfan Chen | b5d2db7 | 2019-12-06 15:43:43 +0900 | [diff] [blame] | 188 | public static final int LAYOUT_INSETS_DURING_ANIMATION_HIDDEN = 1; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * Determines the behavior of how the views should be laid out during an insets animation that |
| 192 | * is controlled by the application by calling {@link #controlWindowInsetsAnimation}. |
| 193 | * <p> |
| 194 | * When the animation is system-initiated, the layout mode is always chosen such that the |
| 195 | * pre-animation layout will represent the opposite of the starting state, i.e. when insets |
| 196 | * are appearing, {@link #LAYOUT_INSETS_DURING_ANIMATION_SHOWN} will be used. When insets |
| 197 | * are disappearing, {@link #LAYOUT_INSETS_DURING_ANIMATION_HIDDEN} will be used. |
| 198 | */ |
| 199 | @Retention(RetentionPolicy.SOURCE) |
| 200 | @IntDef(value = {LAYOUT_INSETS_DURING_ANIMATION_SHOWN, |
| 201 | LAYOUT_INSETS_DURING_ANIMATION_HIDDEN}) |
| 202 | @interface LayoutInsetsDuringAnimation { |
| 203 | } |
| 204 | |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 205 | /** Not running an animation. */ |
| 206 | @VisibleForTesting |
| 207 | public static final int ANIMATION_TYPE_NONE = -1; |
| 208 | |
| 209 | /** Running animation will show insets */ |
| 210 | @VisibleForTesting |
| 211 | public static final int ANIMATION_TYPE_SHOW = 0; |
| 212 | |
| 213 | /** Running animation will hide insets */ |
| 214 | @VisibleForTesting |
| 215 | public static final int ANIMATION_TYPE_HIDE = 1; |
| 216 | |
| 217 | /** Running animation is controlled by user via {@link #controlWindowInsetsAnimation} */ |
| 218 | @VisibleForTesting |
| 219 | public static final int ANIMATION_TYPE_USER = 2; |
| 220 | |
| 221 | @Retention(RetentionPolicy.SOURCE) |
| 222 | @IntDef(value = {ANIMATION_TYPE_NONE, ANIMATION_TYPE_SHOW, ANIMATION_TYPE_HIDE, |
| 223 | ANIMATION_TYPE_USER}) |
| 224 | @interface AnimationType { |
| 225 | } |
| 226 | |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 227 | /** |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 228 | * Translation animation evaluator. |
| 229 | */ |
| 230 | private static TypeEvaluator<Insets> sEvaluator = (fraction, startValue, endValue) -> Insets.of( |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 231 | (int) (startValue.left + fraction * (endValue.left - startValue.left)), |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 232 | (int) (startValue.top + fraction * (endValue.top - startValue.top)), |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 233 | (int) (startValue.right + fraction * (endValue.right - startValue.right)), |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 234 | (int) (startValue.bottom + fraction * (endValue.bottom - startValue.bottom))); |
| 235 | |
| 236 | /** |
Yunfan Chen | 4523d5d | 2020-01-16 16:54:00 +0900 | [diff] [blame] | 237 | * The default implementation of listener, to be used by InsetsController and InsetsPolicy to |
| 238 | * animate insets. |
| 239 | */ |
| 240 | public static class InternalAnimationControlListener |
| 241 | implements WindowInsetsAnimationControlListener { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 242 | |
| 243 | private WindowInsetsAnimationController mController; |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 244 | private ValueAnimator mAnimator; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 245 | private final boolean mShow; |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 246 | private final boolean mHasAnimationCallbacks; |
| 247 | private final @InsetsType int mRequestedTypes; |
| 248 | private final long mDurationMs; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 249 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 250 | private ThreadLocal<AnimationHandler> mSfAnimationHandlerThreadLocal = |
| 251 | new ThreadLocal<AnimationHandler>() { |
| 252 | @Override |
| 253 | protected AnimationHandler initialValue() { |
| 254 | AnimationHandler handler = new AnimationHandler(); |
| 255 | handler.setProvider(new SfVsyncFrameCallbackProvider()); |
| 256 | return handler; |
| 257 | } |
| 258 | }; |
| 259 | |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 260 | public InternalAnimationControlListener(boolean show, boolean hasAnimationCallbacks, |
| 261 | int requestedTypes) { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 262 | mShow = show; |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 263 | mHasAnimationCallbacks = hasAnimationCallbacks; |
| 264 | mRequestedTypes = requestedTypes; |
| 265 | mDurationMs = calculateDurationMs(); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void onReady(WindowInsetsAnimationController controller, int types) { |
| 270 | mController = controller; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 271 | |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 272 | mAnimator = ValueAnimator.ofFloat(0f, 1f); |
| 273 | mAnimator.setDuration(mDurationMs); |
| 274 | mAnimator.setInterpolator(new LinearInterpolator()); |
| 275 | Insets start = mShow |
| 276 | ? controller.getHiddenStateInsets() |
| 277 | : controller.getShownStateInsets(); |
| 278 | Insets end = mShow |
| 279 | ? controller.getShownStateInsets() |
| 280 | : controller.getHiddenStateInsets(); |
| 281 | Interpolator insetsInterpolator = getInterpolator(); |
| 282 | Interpolator alphaInterpolator = getAlphaInterpolator(); |
| 283 | mAnimator.addUpdateListener(animation -> { |
| 284 | float rawFraction = animation.getAnimatedFraction(); |
| 285 | float alphaFraction = mShow |
| 286 | ? rawFraction |
| 287 | : 1 - rawFraction; |
| 288 | float insetsFraction = insetsInterpolator.getInterpolation(rawFraction); |
| 289 | controller.setInsetsAndAlpha( |
| 290 | sEvaluator.evaluate(insetsFraction, start, end), |
| 291 | alphaInterpolator.getInterpolation(alphaFraction), |
| 292 | rawFraction); |
| 293 | }); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 294 | mAnimator.addListener(new AnimatorListenerAdapter() { |
| 295 | |
| 296 | @Override |
| 297 | public void onAnimationEnd(Animator animation) { |
| 298 | onAnimationFinish(); |
| 299 | } |
| 300 | }); |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 301 | if (!mHasAnimationCallbacks) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 302 | mAnimator.setAnimationHandler(mSfAnimationHandlerThreadLocal.get()); |
| 303 | } |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 304 | mAnimator.start(); |
| 305 | } |
| 306 | |
| 307 | @Override |
Adrian Roos | 5d557ed | 2020-03-17 20:04:35 +0100 | [diff] [blame] | 308 | public void onFinished(WindowInsetsAnimationController controller) { |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void onCancelled(WindowInsetsAnimationController controller) { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 313 | // Animator can be null when it is cancelled before onReady() completes. |
| 314 | if (mAnimator != null) { |
| 315 | mAnimator.cancel(); |
| 316 | } |
| 317 | } |
| 318 | |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 319 | Interpolator getInterpolator() { |
| 320 | if ((mRequestedTypes & ime()) != 0) { |
| 321 | if (mHasAnimationCallbacks) { |
| 322 | return SYNC_IME_INTERPOLATOR; |
| 323 | } else if (mShow) { |
| 324 | return LINEAR_OUT_SLOW_IN_INTERPOLATOR; |
| 325 | } else { |
| 326 | return FAST_OUT_LINEAR_IN_INTERPOLATOR; |
| 327 | } |
| 328 | } else { |
| 329 | return SYSTEM_BARS_INTERPOLATOR; |
| 330 | } |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 333 | Interpolator getAlphaInterpolator() { |
| 334 | if ((mRequestedTypes & ime()) != 0) { |
| 335 | if (mHasAnimationCallbacks) { |
| 336 | return input -> 1f; |
| 337 | } else if (mShow) { |
| 338 | |
| 339 | // Alpha animation takes half the time with linear interpolation; |
| 340 | return input -> Math.min(1f, 2 * input); |
| 341 | } else { |
| 342 | return FAST_OUT_LINEAR_IN_INTERPOLATOR; |
| 343 | } |
| 344 | } else { |
| 345 | return input -> 1f; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | protected void onAnimationFinish() { |
| 350 | mController.finish(mShow); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Yunfan Chen | b5d2db7 | 2019-12-06 15:43:43 +0900 | [diff] [blame] | 353 | /** |
| 354 | * To get the animation duration in MS. |
| 355 | */ |
| 356 | public long getDurationMs() { |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 357 | return mDurationMs; |
| 358 | } |
| 359 | |
| 360 | private long calculateDurationMs() { |
| 361 | if ((mRequestedTypes & ime()) != 0) { |
| 362 | if (mHasAnimationCallbacks) { |
| 363 | return ANIMATION_DURATION_SYNC_IME_MS; |
| 364 | } else { |
| 365 | return ANIMATION_DURATION_UNSYNC_IME_MS; |
| 366 | } |
| 367 | } else { |
| 368 | return mShow ? ANIMATION_DURATION_SHOW_MS : ANIMATION_DURATION_HIDE_MS; |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 369 | } |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 373 | /** |
| 374 | * Represents a running animation |
| 375 | */ |
| 376 | private static class RunningAnimation { |
| 377 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 378 | RunningAnimation(InsetsAnimationControlRunner runner, int type) { |
| 379 | this.runner = runner; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 380 | this.type = type; |
| 381 | } |
| 382 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 383 | final InsetsAnimationControlRunner runner; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 384 | final @AnimationType int type; |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 385 | |
| 386 | /** |
| 387 | * Whether {@link WindowInsetsAnimation.Callback#onStart(WindowInsetsAnimation, Bounds)} has |
| 388 | * been dispatched already for this animation. |
| 389 | */ |
| 390 | boolean startDispatched; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 393 | /** |
| 394 | * Represents a control request that we had to defer because we are waiting for the IME to |
| 395 | * process our show request. |
| 396 | */ |
| 397 | private static class PendingControlRequest { |
| 398 | |
| 399 | PendingControlRequest(@InsetsType int types, WindowInsetsAnimationControlListener listener, |
| 400 | long durationMs, Interpolator interpolator, @AnimationType int animationType, |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 401 | @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation, |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 402 | CancellationSignal cancellationSignal, boolean useInsetsAnimationThread) { |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 403 | this.types = types; |
| 404 | this.listener = listener; |
| 405 | this.durationMs = durationMs; |
| 406 | this.interpolator = interpolator; |
| 407 | this.animationType = animationType; |
| 408 | this.layoutInsetsDuringAnimation = layoutInsetsDuringAnimation; |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 409 | this.cancellationSignal = cancellationSignal; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 410 | this.useInsetsAnimationThread = useInsetsAnimationThread; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | final @InsetsType int types; |
| 414 | final WindowInsetsAnimationControlListener listener; |
| 415 | final long durationMs; |
| 416 | final Interpolator interpolator; |
| 417 | final @AnimationType int animationType; |
| 418 | final @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation; |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 419 | final CancellationSignal cancellationSignal; |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 420 | final boolean useInsetsAnimationThread; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 421 | } |
Yunfan Chen | 4523d5d | 2020-01-16 16:54:00 +0900 | [diff] [blame] | 422 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 423 | private final String TAG = "InsetsControllerImpl"; |
| 424 | |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 425 | /** The local state */ |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 426 | private final InsetsState mState = new InsetsState(); |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 427 | |
| 428 | /** The state dispatched from server */ |
| 429 | private final InsetsState mLastDispatchedState = new InsetsState(); |
| 430 | |
| 431 | /** The state sent to server */ |
| 432 | private final InsetsState mRequestedState = new InsetsState(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 433 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 434 | private final Rect mFrame = new Rect(); |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 435 | private final BiFunction<InsetsController, Integer, InsetsSourceConsumer> mConsumerCreator; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 436 | private final SparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>(); |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 437 | private final Host mHost; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 438 | private final Handler mHandler; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 439 | |
| 440 | private final SparseArray<InsetsSourceControl> mTmpControlArray = new SparseArray<>(); |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 441 | private final ArrayList<RunningAnimation> mRunningAnimations = new ArrayList<>(); |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 442 | private final ArrayList<WindowInsetsAnimation> mTmpRunningAnims = new ArrayList<>(); |
| 443 | private final List<WindowInsetsAnimation> mUnmodifiableTmpRunningAnims = |
| 444 | Collections.unmodifiableList(mTmpRunningAnims); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 445 | private final ArrayList<InsetsAnimationControlImpl> mTmpFinishedControls = new ArrayList<>(); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 446 | private WindowInsets mLastInsets; |
| 447 | |
| 448 | private boolean mAnimCallbackScheduled; |
| 449 | |
| 450 | private final Runnable mAnimCallback; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 451 | |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 452 | /** Pending control request that is waiting on IME to be ready to be shown */ |
| 453 | private PendingControlRequest mPendingImeControlRequest; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 454 | |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 455 | private int mLastLegacySoftInputMode; |
Jorim Jaggi | 7f76187 | 2020-01-10 18:24:27 +0100 | [diff] [blame] | 456 | private int mLastLegacySystemUiFlags; |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 457 | private DisplayCutout mLastDisplayCutout; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 458 | private boolean mStartingAnimation; |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 459 | private int mCaptionInsetsHeight = 0; |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 460 | |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 461 | private Runnable mPendingControlTimeout = this::abortPendingImeControlRequest; |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 462 | private final ArrayList<OnControllableInsetsChangedListener> mControllableInsetsChangedListeners |
| 463 | = new ArrayList<>(); |
| 464 | |
| 465 | /** Set of inset types for which an animation was started since last resetting this field */ |
| 466 | private @InsetsType int mLastStartedAnimTypes; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 467 | |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 468 | public InsetsController(Host host) { |
| 469 | this(host, (controller, type) -> { |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 470 | if (type == ITYPE_IME) { |
| 471 | return new ImeInsetsSourceConsumer(controller.mState, Transaction::new, controller); |
| 472 | } else { |
| 473 | return new InsetsSourceConsumer(type, controller.mState, Transaction::new, |
| 474 | controller); |
| 475 | } |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 476 | }, host.getHandler()); |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | @VisibleForTesting |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 480 | public InsetsController(Host host, |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 481 | BiFunction<InsetsController, Integer, InsetsSourceConsumer> consumerCreator, |
| 482 | Handler handler) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 483 | mHost = host; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 484 | mConsumerCreator = consumerCreator; |
| 485 | mHandler = handler; |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 486 | mAnimCallback = () -> { |
| 487 | mAnimCallbackScheduled = false; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 488 | if (mRunningAnimations.isEmpty()) { |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 492 | mTmpFinishedControls.clear(); |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 493 | mTmpRunningAnims.clear(); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 494 | InsetsState state = new InsetsState(mState, true /* copySources */); |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 495 | for (int i = mRunningAnimations.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 496 | RunningAnimation runningAnimation = mRunningAnimations.get(i); |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 497 | InsetsAnimationControlRunner runner = runningAnimation.runner; |
| 498 | if (runner instanceof InsetsAnimationControlImpl) { |
| 499 | InsetsAnimationControlImpl control = (InsetsAnimationControlImpl) runner; |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 500 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 501 | // Keep track of running animation to be dispatched. Aggregate it here such that |
| 502 | // if it gets finished within applyChangeInsets we still dispatch it to |
| 503 | // onProgress. |
| 504 | if (runningAnimation.startDispatched) { |
| 505 | mTmpRunningAnims.add(control.getAnimation()); |
| 506 | } |
| 507 | |
| 508 | if (control.applyChangeInsets(state)) { |
| 509 | mTmpFinishedControls.add(control); |
| 510 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 511 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 512 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 513 | |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 514 | WindowInsets insets = state.calculateInsets(mFrame, mState /* ignoringVisibilityState*/, |
| 515 | mLastInsets.isRound(), mLastInsets.shouldAlwaysConsumeSystemBars(), |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 516 | mLastDisplayCutout, mLastLegacySoftInputMode, mLastLegacySystemUiFlags, |
| 517 | null /* typeSideMap */); |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 518 | mHost.dispatchWindowInsetsAnimationProgress(insets, mUnmodifiableTmpRunningAnims); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 519 | |
| 520 | for (int i = mTmpFinishedControls.size() - 1; i >= 0; i--) { |
Adrian Roos | db5b0c2 | 2020-02-12 15:05:27 -0800 | [diff] [blame] | 521 | dispatchAnimationEnd(mTmpFinishedControls.get(i).getAnimation()); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 522 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 523 | }; |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 526 | @VisibleForTesting |
| 527 | public void onFrameChanged(Rect frame) { |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 528 | if (mFrame.equals(frame)) { |
| 529 | return; |
| 530 | } |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 531 | mHost.notifyInsetsChanged(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 532 | mFrame.set(frame); |
| 533 | } |
| 534 | |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 535 | @Override |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 536 | public InsetsState getState() { |
| 537 | return mState; |
| 538 | } |
| 539 | |
Jorim Jaggi | 49b9f6c | 2020-03-24 22:28:38 +0100 | [diff] [blame] | 540 | @Override |
| 541 | public boolean isRequestedVisible(int type) { |
| 542 | return getSourceConsumer(type).isRequestedVisible(); |
| 543 | } |
| 544 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 545 | public InsetsState getLastDispatchedState() { |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 546 | return mLastDispatchedState; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | @VisibleForTesting |
| 550 | public boolean onStateChanged(InsetsState state) { |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 551 | boolean localStateChanged = !mState.equals(state, true /* excludingCaptionInsets */) |
| 552 | || !captionInsetsUnchanged(); |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 553 | if (!localStateChanged && mLastDispatchedState.equals(state)) { |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 554 | return false; |
| 555 | } |
Jorim Jaggi | 33a2183 | 2020-04-06 14:15:46 +0200 | [diff] [blame] | 556 | updateState(state); |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 557 | mLastDispatchedState.set(state, true /* copySources */); |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 558 | applyLocalVisibilityOverride(); |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 559 | if (localStateChanged) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 560 | mHost.notifyInsetsChanged(); |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 561 | } |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 562 | if (!mState.equals(mLastDispatchedState, true /* excludingCaptionInsets */)) { |
| 563 | updateRequestedState(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 564 | } |
Jorim Jaggi | 33a2183 | 2020-04-06 14:15:46 +0200 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
| 568 | private void updateState(InsetsState newState) { |
| 569 | mState.setDisplayFrame(newState.getDisplayFrame()); |
| 570 | for (int i = newState.getSourcesCount() - 1; i >= 0; i--) { |
| 571 | InsetsSource source = newState.sourceAt(i); |
| 572 | getSourceConsumer(source.getType()).updateSource(source); |
| 573 | } |
| 574 | for (int i = mState.getSourcesCount() - 1; i >= 0; i--) { |
| 575 | InsetsSource source = mState.sourceAt(i); |
| 576 | if (newState.peekSource(source.getType()) == null) { |
| 577 | mState.removeSource(source.getType()); |
| 578 | } |
| 579 | } |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 580 | if (mCaptionInsetsHeight != 0) { |
| 581 | mState.getSource(ITYPE_CAPTION_BAR).setFrame(new Rect(mFrame.left, mFrame.top, |
| 582 | mFrame.right, mFrame.top + mCaptionInsetsHeight)); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | private boolean captionInsetsUnchanged() { |
| 587 | if (mState.peekSource(ITYPE_CAPTION_BAR) == null |
| 588 | && mCaptionInsetsHeight == 0) { |
| 589 | return true; |
| 590 | } |
| 591 | if (mState.peekSource(ITYPE_CAPTION_BAR) != null |
| 592 | && mCaptionInsetsHeight |
| 593 | == mState.peekSource(ITYPE_CAPTION_BAR).getFrame().height()) { |
| 594 | return true; |
| 595 | } |
| 596 | return false; |
Jorim Jaggi | 2751c29 | 2020-03-20 23:46:38 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 599 | /** |
| 600 | * @see InsetsState#calculateInsets |
| 601 | */ |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 602 | @VisibleForTesting |
| 603 | public WindowInsets calculateInsets(boolean isScreenRound, |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 604 | boolean alwaysConsumeSystemBars, DisplayCutout cutout, |
| 605 | int legacySoftInputMode, int legacySystemUiFlags) { |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 606 | mLastLegacySoftInputMode = legacySoftInputMode; |
Jorim Jaggi | 7f76187 | 2020-01-10 18:24:27 +0100 | [diff] [blame] | 607 | mLastLegacySystemUiFlags = legacySystemUiFlags; |
Jorim Jaggi | 580aef5 | 2020-02-26 18:28:28 +0100 | [diff] [blame] | 608 | mLastDisplayCutout = cutout; |
| 609 | mLastInsets = mState.calculateInsets(mFrame, null /* ignoringVisibilityState*/, |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 610 | isScreenRound, alwaysConsumeSystemBars, cutout, |
| 611 | legacySoftInputMode, legacySystemUiFlags, |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 612 | null /* typeSideMap */); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 613 | return mLastInsets; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 616 | /** |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 617 | * @see InsetsState#calculateVisibleInsets(Rect, int) |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 618 | */ |
Jorim Jaggi | 22488d3 | 2020-03-19 01:12:44 +0100 | [diff] [blame] | 619 | public Rect calculateVisibleInsets(@SoftInputModeFlags int softInputMode) { |
| 620 | return mState.calculateVisibleInsets(mFrame, softInputMode); |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 624 | * Called when the server has dispatched us a new set of inset controls. |
| 625 | */ |
| 626 | public void onControlsChanged(InsetsSourceControl[] activeControls) { |
| 627 | if (activeControls != null) { |
| 628 | for (InsetsSourceControl activeControl : activeControls) { |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 629 | if (activeControl != null) { |
| 630 | // TODO(b/122982984): Figure out why it can be null. |
| 631 | mTmpControlArray.put(activeControl.getType(), activeControl); |
| 632 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 636 | final boolean hasControl = mTmpControlArray.size() > 0; |
| 637 | final int[] showTypes = new int[1]; |
| 638 | final int[] hideTypes = new int[1]; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 639 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 640 | // Ensure to update all existing source consumers |
| 641 | for (int i = mSourceConsumers.size() - 1; i >= 0; i--) { |
| 642 | final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i); |
| 643 | final InsetsSourceControl control = mTmpControlArray.get(consumer.getType()); |
| 644 | |
| 645 | // control may be null, but we still need to update the control to null if it got |
| 646 | // revoked. |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 647 | consumer.setControl(control, showTypes, hideTypes); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | // Ensure to create source consumers if not available yet. |
| 651 | for (int i = mTmpControlArray.size() - 1; i >= 0; i--) { |
| 652 | final InsetsSourceControl control = mTmpControlArray.valueAt(i); |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 653 | InsetsSourceConsumer consumer = getSourceConsumer(control.getType()); |
| 654 | consumer.setControl(control, showTypes, hideTypes); |
| 655 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 656 | } |
| 657 | mTmpControlArray.clear(); |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 658 | |
| 659 | // Do not override any animations that the app started in the OnControllableInsetsChanged |
| 660 | // listeners. |
| 661 | int animatingTypes = invokeControllableInsetsChangedListeners(); |
| 662 | showTypes[0] &= ~animatingTypes; |
| 663 | hideTypes[0] &= ~animatingTypes; |
| 664 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 665 | if (showTypes[0] != 0) { |
| 666 | applyAnimation(showTypes[0], true /* show */, false /* fromIme */); |
| 667 | } |
| 668 | if (hideTypes[0] != 0) { |
| 669 | applyAnimation(hideTypes[0], false /* show */, false /* fromIme */); |
| 670 | } |
Tiger Huang | fbfc3ab | 2020-05-06 01:02:31 +0800 | [diff] [blame] | 671 | if (hasControl && mRequestedState.getSourcesCount() > 0) { |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 672 | // We might have changed our requested visibilities while we don't have the control, |
| 673 | // so we need to update our requested state once we have control. Otherwise, our |
| 674 | // requested state at the server side might be incorrect. |
| 675 | updateRequestedState(); |
| 676 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | @Override |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 680 | public void show(@InsetsType int types) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 681 | show(types, false /* fromIme */); |
| 682 | } |
| 683 | |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 684 | @VisibleForTesting |
| 685 | public void show(@InsetsType int types, boolean fromIme) { |
| 686 | |
| 687 | // Handle pending request ready in case there was one set. |
| 688 | if (fromIme && mPendingImeControlRequest != null) { |
| 689 | PendingControlRequest pendingRequest = mPendingImeControlRequest; |
| 690 | mPendingImeControlRequest = null; |
| 691 | mHandler.removeCallbacks(mPendingControlTimeout); |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 692 | controlAnimationUnchecked( |
| 693 | pendingRequest.types, pendingRequest.cancellationSignal, |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 694 | pendingRequest.listener, mFrame, |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 695 | true /* fromIme */, pendingRequest.durationMs, pendingRequest.interpolator, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 696 | pendingRequest.animationType, |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 697 | pendingRequest.layoutInsetsDuringAnimation, |
| 698 | pendingRequest.useInsetsAnimationThread); |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 702 | // TODO: Support a ResultReceiver for IME. |
| 703 | // TODO(b/123718661): Make show() work for multi-session IME. |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 704 | int typesReady = 0; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 705 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 706 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 707 | @InternalInsetsType int internalType = internalTypes.valueAt(i); |
| 708 | @AnimationType int animationType = getAnimationType(internalType); |
| 709 | InsetsSourceConsumer consumer = getSourceConsumer(internalType); |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 710 | if (consumer.isRequestedVisible() && animationType == ANIMATION_TYPE_NONE |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 711 | || animationType == ANIMATION_TYPE_SHOW) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 712 | // no-op: already shown or animating in (because window visibility is |
| 713 | // applied before starting animation). |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 714 | continue; |
| 715 | } |
| 716 | typesReady |= InsetsState.toPublicType(consumer.getType()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 717 | } |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 718 | applyAnimation(typesReady, true /* show */, fromIme); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | @Override |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 722 | public void hide(@InsetsType int types) { |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 723 | hide(types, false /* fromIme */); |
| 724 | } |
| 725 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 726 | void hide(@InsetsType int types, boolean fromIme) { |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 727 | int typesReady = 0; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 728 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 729 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 730 | @InternalInsetsType int internalType = internalTypes.valueAt(i); |
| 731 | @AnimationType int animationType = getAnimationType(internalType); |
| 732 | InsetsSourceConsumer consumer = getSourceConsumer(internalType); |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 733 | if (!consumer.isRequestedVisible() && animationType == ANIMATION_TYPE_NONE |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 734 | || animationType == ANIMATION_TYPE_HIDE) { |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 735 | // no-op: already hidden or animating out. |
| 736 | continue; |
| 737 | } |
| 738 | typesReady |= InsetsState.toPublicType(consumer.getType()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 739 | } |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 740 | applyAnimation(typesReady, false /* show */, fromIme /* fromIme */); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 743 | @Override |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 744 | public void controlWindowInsetsAnimation(@InsetsType int types, long durationMillis, |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 745 | @Nullable Interpolator interpolator, |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 746 | @Nullable CancellationSignal cancellationSignal, |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 747 | @NonNull WindowInsetsAnimationControlListener listener) { |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 748 | controlWindowInsetsAnimation(types, cancellationSignal, listener, |
| 749 | false /* fromIme */, durationMillis, interpolator, ANIMATION_TYPE_USER); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 752 | private void controlWindowInsetsAnimation(@InsetsType int types, |
| 753 | @Nullable CancellationSignal cancellationSignal, |
| 754 | WindowInsetsAnimationControlListener listener, |
| 755 | boolean fromIme, long durationMs, @Nullable Interpolator interpolator, |
| 756 | @AnimationType int animationType) { |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 757 | if (!checkDisplayFramesForControlling()) { |
Adrian Roos | 5d557ed | 2020-03-17 20:04:35 +0100 | [diff] [blame] | 758 | listener.onCancelled(null); |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 759 | return; |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 760 | } |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 761 | |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 762 | controlAnimationUnchecked(types, cancellationSignal, listener, mFrame, fromIme, durationMs, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 763 | interpolator, animationType, getLayoutInsetsDuringAnimationMode(types), |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 764 | false /* useInsetsAnimationThread */); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 767 | private boolean checkDisplayFramesForControlling() { |
| 768 | |
| 769 | // If the frame of our window doesn't span the entire display, the control API makes very |
| 770 | // little sense, as we don't deal with negative insets. So just cancel immediately. |
| 771 | return mState.getDisplayFrame().equals(mFrame); |
| 772 | } |
| 773 | |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 774 | private void controlAnimationUnchecked(@InsetsType int types, |
| 775 | @Nullable CancellationSignal cancellationSignal, |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 776 | WindowInsetsAnimationControlListener listener, Rect frame, boolean fromIme, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 777 | long durationMs, Interpolator interpolator, |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 778 | @AnimationType int animationType, |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 779 | @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation, |
| 780 | boolean useInsetsAnimationThread) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 781 | if (types == 0) { |
| 782 | // nothing to animate. |
Adrian Roos | 5d557ed | 2020-03-17 20:04:35 +0100 | [diff] [blame] | 783 | listener.onCancelled(null); |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 784 | return; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 785 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 786 | cancelExistingControllers(types); |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 787 | mLastStartedAnimTypes |= types; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 788 | |
Tiger Huang | 969c608 | 2019-12-24 20:08:57 +0800 | [diff] [blame] | 789 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 790 | final SparseArray<InsetsSourceControl> controls = new SparseArray<>(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 791 | |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 792 | Pair<Integer, Boolean> typesReadyPair = collectSourceControls( |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 793 | fromIme, internalTypes, controls, animationType); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 794 | int typesReady = typesReadyPair.first; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 795 | boolean imeReady = typesReadyPair.second; |
| 796 | if (!imeReady) { |
| 797 | // IME isn't ready, all requested types will be animated once IME is ready |
| 798 | abortPendingImeControlRequest(); |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 799 | final PendingControlRequest request = new PendingControlRequest(types, |
| 800 | listener, durationMs, |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 801 | interpolator, animationType, layoutInsetsDuringAnimation, cancellationSignal, |
| 802 | useInsetsAnimationThread); |
Adrian Roos | 3406fb9 | 2020-02-10 18:38:59 -0800 | [diff] [blame] | 803 | mPendingImeControlRequest = request; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 804 | mHandler.postDelayed(mPendingControlTimeout, PENDING_CONTROL_TIMEOUT_MS); |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 805 | if (cancellationSignal != null) { |
| 806 | cancellationSignal.setOnCancelListener(() -> { |
| 807 | if (mPendingImeControlRequest == request) { |
| 808 | abortPendingImeControlRequest(); |
| 809 | } |
| 810 | }); |
| 811 | } |
| 812 | return; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 815 | if (typesReady == 0) { |
Adrian Roos | 5d557ed | 2020-03-17 20:04:35 +0100 | [diff] [blame] | 816 | listener.onCancelled(null); |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 817 | return; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 820 | |
| 821 | final InsetsAnimationControlRunner runner = useInsetsAnimationThread |
| 822 | ? new InsetsAnimationThreadControlRunner(controls, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 823 | frame, mState, listener, typesReady, this, durationMs, interpolator, |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 824 | animationType, mHost.getHandler()) |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 825 | : new InsetsAnimationControlImpl(controls, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 826 | frame, mState, listener, typesReady, this, durationMs, interpolator, |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 827 | animationType); |
| 828 | mRunningAnimations.add(new RunningAnimation(runner, animationType)); |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 829 | if (cancellationSignal != null) { |
| 830 | cancellationSignal.setOnCancelListener(runner::cancel); |
| 831 | } |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 832 | if (layoutInsetsDuringAnimation == LAYOUT_INSETS_DURING_ANIMATION_SHOWN) { |
| 833 | showDirectly(types); |
| 834 | } else { |
| 835 | hideDirectly(types, false /* animationFinished */, animationType); |
| 836 | } |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 840 | * @return Pair of (types ready to animate, IME ready to animate). |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 841 | */ |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 842 | private Pair<Integer, Boolean> collectSourceControls(boolean fromIme, |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 843 | ArraySet<Integer> internalTypes, SparseArray<InsetsSourceControl> controls, |
| 844 | @AnimationType int animationType) { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 845 | int typesReady = 0; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 846 | boolean imeReady = true; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 847 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 848 | final InsetsSourceConsumer consumer = getSourceConsumer(internalTypes.valueAt(i)); |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 849 | boolean show = animationType == ANIMATION_TYPE_SHOW |
| 850 | || animationType == ANIMATION_TYPE_USER; |
| 851 | boolean canRun = false; |
| 852 | if (show) { |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 853 | // Show request |
| 854 | switch(consumer.requestShow(fromIme)) { |
| 855 | case ShowResult.SHOW_IMMEDIATELY: |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 856 | canRun = true; |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 857 | break; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 858 | case ShowResult.IME_SHOW_DELAYED: |
| 859 | imeReady = false; |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 860 | break; |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 861 | case ShowResult.IME_SHOW_FAILED: |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 862 | // IME cannot be shown (since it didn't have focus), proceed |
| 863 | // with animation of other types. |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 864 | break; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 865 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 866 | } else { |
Taran Singh | d7fc586 | 2019-10-10 14:45:17 +0200 | [diff] [blame] | 867 | // Hide request |
| 868 | // TODO: Move notifyHidden() to beginning of the hide animation |
| 869 | // (when visibility actually changes using hideDirectly()). |
| 870 | if (!fromIme) { |
| 871 | consumer.notifyHidden(); |
| 872 | } |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 873 | canRun = true; |
| 874 | } |
| 875 | if (!canRun) { |
| 876 | continue; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 877 | } |
Tiger Huang | 969c608 | 2019-12-24 20:08:57 +0800 | [diff] [blame] | 878 | final InsetsSourceControl control = consumer.getControl(); |
| 879 | if (control != null) { |
Rob Carr | 3a367c4 | 2020-03-10 15:51:35 -0700 | [diff] [blame] | 880 | controls.put(consumer.getType(), new InsetsSourceControl(control)); |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 881 | typesReady |= toPublicType(consumer.getType()); |
| 882 | } else if (animationType == ANIMATION_TYPE_SHOW) { |
| 883 | |
| 884 | // We don't have a control at the moment. However, we still want to update requested |
| 885 | // visibility state such that in case we get control, we can apply show animation. |
| 886 | consumer.show(fromIme); |
| 887 | } else if (animationType == ANIMATION_TYPE_HIDE) { |
| 888 | consumer.hide(); |
Tiger Huang | 969c608 | 2019-12-24 20:08:57 +0800 | [diff] [blame] | 889 | } |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 890 | } |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 891 | return new Pair<>(typesReady, imeReady); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 892 | } |
| 893 | |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 894 | private @LayoutInsetsDuringAnimation int getLayoutInsetsDuringAnimationMode( |
| 895 | @InsetsType int types) { |
| 896 | |
| 897 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 898 | |
| 899 | // Generally, we want to layout the opposite of the current state. This is to make animation |
| 900 | // callbacks easy to use: The can capture the layout values and then treat that as end-state |
| 901 | // during the animation. |
| 902 | // |
| 903 | // However, if controlling multiple sources, we want to treat it as shown if any of the |
| 904 | // types is currently hidden. |
| 905 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
| 906 | InsetsSourceConsumer consumer = mSourceConsumers.get(internalTypes.valueAt(i)); |
| 907 | if (consumer == null) { |
| 908 | continue; |
| 909 | } |
| 910 | if (!consumer.isRequestedVisible()) { |
| 911 | return LAYOUT_INSETS_DURING_ANIMATION_SHOWN; |
| 912 | } |
| 913 | } |
| 914 | return LAYOUT_INSETS_DURING_ANIMATION_HIDDEN; |
| 915 | } |
| 916 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 917 | private void cancelExistingControllers(@InsetsType int types) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 918 | for (int i = mRunningAnimations.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 919 | InsetsAnimationControlRunner control = mRunningAnimations.get(i).runner; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 920 | if ((control.getTypes() & types) != 0) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 921 | cancelAnimation(control, true /* invokeCallback */); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 922 | } |
| 923 | } |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 924 | if ((types & ime()) != 0) { |
| 925 | abortPendingImeControlRequest(); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | private void abortPendingImeControlRequest() { |
| 930 | if (mPendingImeControlRequest != null) { |
Adrian Roos | 5d557ed | 2020-03-17 20:04:35 +0100 | [diff] [blame] | 931 | mPendingImeControlRequest.listener.onCancelled(null); |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 932 | mPendingImeControlRequest = null; |
| 933 | mHandler.removeCallbacks(mPendingControlTimeout); |
| 934 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | @VisibleForTesting |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 938 | @Override |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 939 | public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) { |
| 940 | cancelAnimation(runner, false /* invokeCallback */); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 941 | if (shown) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 942 | showDirectly(runner.getTypes()); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 943 | } else { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 944 | hideDirectly(runner.getTypes(), true /* animationFinished */, |
| 945 | runner.getAnimationType()); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 946 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 949 | @Override |
| 950 | public void applySurfaceParams(final SyncRtSurfaceTransactionApplier.SurfaceParams... params) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 951 | mHost.applySurfaceParams(params); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 952 | } |
| 953 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 954 | void notifyControlRevoked(InsetsSourceConsumer consumer) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 955 | for (int i = mRunningAnimations.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 956 | InsetsAnimationControlRunner control = mRunningAnimations.get(i).runner; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 957 | if ((control.getTypes() & toPublicType(consumer.getType())) != 0) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 958 | cancelAnimation(control, true /* invokeCallback */); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 959 | } |
| 960 | } |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 961 | if (consumer.getType() == ITYPE_IME) { |
| 962 | abortPendingImeControlRequest(); |
| 963 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 966 | private void cancelAnimation(InsetsAnimationControlRunner control, boolean invokeCallback) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 967 | if (invokeCallback) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 968 | control.cancel(); |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 969 | } |
| 970 | for (int i = mRunningAnimations.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 33a2183 | 2020-04-06 14:15:46 +0200 | [diff] [blame] | 971 | RunningAnimation runningAnimation = mRunningAnimations.get(i); |
| 972 | if (runningAnimation.runner == control) { |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 973 | mRunningAnimations.remove(i); |
Jorim Jaggi | 33a2183 | 2020-04-06 14:15:46 +0200 | [diff] [blame] | 974 | ArraySet<Integer> types = toInternalType(control.getTypes()); |
| 975 | for (int j = types.size() - 1; j >= 0; j--) { |
| 976 | if (getSourceConsumer(types.valueAt(j)).notifyAnimationFinished()) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 977 | mHost.notifyInsetsChanged(); |
Jorim Jaggi | 33a2183 | 2020-04-06 14:15:46 +0200 | [diff] [blame] | 978 | } |
| 979 | } |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 980 | break; |
| 981 | } |
| 982 | } |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 983 | } |
| 984 | |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 985 | private void applyLocalVisibilityOverride() { |
| 986 | for (int i = mSourceConsumers.size() - 1; i >= 0; i--) { |
| 987 | final InsetsSourceConsumer controller = mSourceConsumers.valueAt(i); |
| 988 | controller.applyLocalVisibilityOverride(); |
| 989 | } |
| 990 | } |
| 991 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 992 | @VisibleForTesting |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 993 | public @NonNull InsetsSourceConsumer getSourceConsumer(@InternalInsetsType int type) { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 994 | InsetsSourceConsumer controller = mSourceConsumers.get(type); |
| 995 | if (controller != null) { |
| 996 | return controller; |
| 997 | } |
Jorim Jaggi | d7f10ed | 2020-01-08 21:41:55 +0100 | [diff] [blame] | 998 | controller = mConsumerCreator.apply(this, type); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 999 | mSourceConsumers.put(type, controller); |
| 1000 | return controller; |
| 1001 | } |
| 1002 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 1003 | @VisibleForTesting |
| 1004 | public void notifyVisibilityChanged() { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1005 | mHost.notifyInsetsChanged(); |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1006 | updateRequestedState(); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /** |
Tiger Huang | 41ae5e8 | 2020-01-17 18:17:20 +0800 | [diff] [blame] | 1010 | * @see ViewRootImpl#updateCompatSysUiVisibility(int, boolean, boolean) |
| 1011 | */ |
| 1012 | public void updateCompatSysUiVisibility(@InternalInsetsType int type, boolean visible, |
| 1013 | boolean hasControl) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1014 | mHost.updateCompatSysUiVisibility(type, visible, hasControl); |
Tiger Huang | 41ae5e8 | 2020-01-17 18:17:20 +0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /** |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 1018 | * Called when current window gains focus. |
| 1019 | */ |
| 1020 | public void onWindowFocusGained() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 1021 | getSourceConsumer(ITYPE_IME).onWindowFocusGained(); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * Called when current window loses focus. |
| 1026 | */ |
| 1027 | public void onWindowFocusLost() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 1028 | getSourceConsumer(ITYPE_IME).onWindowFocusLost(); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 1031 | /** |
| 1032 | * Used by {@link ImeInsetsSourceConsumer} when IME decides to be shown/hidden. |
| 1033 | * @hide |
| 1034 | */ |
| 1035 | @VisibleForTesting |
| 1036 | public void applyImeVisibility(boolean setVisible) { |
| 1037 | if (setVisible) { |
| 1038 | show(Type.IME, true /* fromIme */); |
| 1039 | } else { |
| 1040 | hide(Type.IME); |
| 1041 | } |
| 1042 | } |
| 1043 | |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 1044 | @VisibleForTesting |
| 1045 | public @AnimationType int getAnimationType(@InternalInsetsType int type) { |
| 1046 | for (int i = mRunningAnimations.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 1047 | InsetsAnimationControlRunner control = mRunningAnimations.get(i).runner; |
Jorim Jaggi | 1f2c7eb | 2020-01-08 00:07:13 +0100 | [diff] [blame] | 1048 | if (control.controlsInternalType(type)) { |
| 1049 | return mRunningAnimations.get(i).type; |
| 1050 | } |
| 1051 | } |
| 1052 | return ANIMATION_TYPE_NONE; |
Tarandeep Singh | 93ea15a | 2019-11-26 11:09:14 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 1055 | /** |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1056 | * Sends the local visibility state back to window manager if it is changed. |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 1057 | */ |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1058 | private void updateRequestedState() { |
| 1059 | boolean changed = false; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 1060 | for (int i = mSourceConsumers.size() - 1; i >= 0; i--) { |
| 1061 | final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i); |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1062 | final @InternalInsetsType int type = consumer.getType(); |
| 1063 | if (type == ITYPE_CAPTION_BAR) { |
| 1064 | continue; |
| 1065 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 1066 | if (consumer.getControl() != null) { |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1067 | final InsetsSource localSource = mState.getSource(type); |
| 1068 | if (!localSource.equals(mRequestedState.peekSource(type))) { |
Tiger Huang | fbfc3ab | 2020-05-06 01:02:31 +0800 | [diff] [blame] | 1069 | // Our requested state is stale. Update it here and send it to window manager. |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1070 | mRequestedState.addSource(new InsetsSource(localSource)); |
| 1071 | changed = true; |
| 1072 | } |
Tiger Huang | fbfc3ab | 2020-05-06 01:02:31 +0800 | [diff] [blame] | 1073 | if (!localSource.equals(mLastDispatchedState.peekSource(type))) { |
| 1074 | // The server state is not what we expected. This can happen while we don't have |
| 1075 | // the control. Since we have the control now, we need to send our request again |
| 1076 | // to modify the server state. |
| 1077 | changed = true; |
| 1078 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 1079 | } |
| 1080 | } |
Tiger Huang | 173480f | 2020-04-29 01:05:42 +0800 | [diff] [blame] | 1081 | if (!changed) { |
| 1082 | return; |
| 1083 | } |
| 1084 | mHost.onInsetsModified(mRequestedState); |
Jorim Jaggi | c8d6038 | 2018-10-31 17:06:06 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
Taran Singh | 8321a2a4 | 2020-04-21 17:26:50 -0700 | [diff] [blame] | 1087 | @VisibleForTesting |
| 1088 | public void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme) { |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1089 | if (types == 0) { |
| 1090 | // nothing to animate. |
| 1091 | return; |
| 1092 | } |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 1093 | |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1094 | boolean hasAnimationCallbacks = mHost.hasAnimationCallbacks(); |
Adrian Roos | bbf4240 | 2020-01-28 18:23:39 +0100 | [diff] [blame] | 1095 | final InternalAnimationControlListener listener = |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 1096 | new InternalAnimationControlListener(show, hasAnimationCallbacks, types); |
| 1097 | |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 1098 | // Show/hide animations always need to be relative to the display frame, in order that shown |
| 1099 | // and hidden state insets are correct. |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 1100 | controlAnimationUnchecked( |
Adrian Roos | 5ad8cd2 | 2020-03-12 18:30:54 +0000 | [diff] [blame] | 1101 | types, null /* cancellationSignal */, listener, mState.getDisplayFrame(), fromIme, |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 1102 | listener.getDurationMs(), listener.getInterpolator(), |
| 1103 | show ? ANIMATION_TYPE_SHOW : ANIMATION_TYPE_HIDE, |
| 1104 | show ? LAYOUT_INSETS_DURING_ANIMATION_SHOWN : LAYOUT_INSETS_DURING_ANIMATION_HIDDEN, |
| 1105 | !hasAnimationCallbacks /* useInsetsAnimationThread */); |
| 1106 | |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 1109 | private void hideDirectly( |
| 1110 | @InsetsType int types, boolean animationFinished, @AnimationType int animationType) { |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1111 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 1112 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
Tarandeep Singh | b9538cd | 2020-02-20 17:51:18 -0800 | [diff] [blame] | 1113 | getSourceConsumer(internalTypes.valueAt(i)).hide(animationFinished, animationType); |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 1117 | private void showDirectly(@InsetsType int types) { |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1118 | final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types); |
| 1119 | for (int i = internalTypes.size() - 1; i >= 0; i--) { |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 1120 | getSourceConsumer(internalTypes.valueAt(i)).show(false /* fromIme */); |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | /** |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 1125 | * Cancel on-going animation to show/hide {@link InsetsType}. |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1126 | */ |
| 1127 | @VisibleForTesting |
Jorim Jaggi | 31e5254 | 2020-05-04 23:23:25 +0200 | [diff] [blame] | 1128 | public void cancelExistingAnimations() { |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 1129 | cancelExistingControllers(all()); |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 1132 | void dump(String prefix, PrintWriter pw) { |
| 1133 | pw.println(prefix); pw.println("InsetsController:"); |
| 1134 | mState.dump(prefix + " ", pw); |
| 1135 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 1136 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 1137 | @VisibleForTesting |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 1138 | @Override |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 1139 | public void startAnimation(InsetsAnimationControlImpl controller, |
Adrian Roos | db5b0c2 | 2020-02-12 15:05:27 -0800 | [diff] [blame] | 1140 | WindowInsetsAnimationControlListener listener, int types, |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 1141 | WindowInsetsAnimation animation, Bounds bounds) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1142 | mHost.dispatchWindowInsetsAnimationPrepare(animation); |
| 1143 | mHost.addOnPreDrawRunnable(() -> { |
| 1144 | if (controller.isCancelled()) { |
| 1145 | return; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 1146 | } |
Jorim Jaggi | cb28ae6 | 2020-05-14 17:46:32 +0200 | [diff] [blame^] | 1147 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, |
| 1148 | "InsetsAnimation: " + WindowInsets.Type.toString(types), types); |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1149 | for (int i = mRunningAnimations.size() - 1; i >= 0; i--) { |
| 1150 | RunningAnimation runningAnimation = mRunningAnimations.get(i); |
| 1151 | if (runningAnimation.runner == controller) { |
| 1152 | runningAnimation.startDispatched = true; |
| 1153 | } |
| 1154 | } |
| 1155 | mHost.dispatchWindowInsetsAnimationStart(animation, bounds); |
| 1156 | mStartingAnimation = true; |
| 1157 | controller.mReadyDispatched = true; |
| 1158 | listener.onReady(controller, types); |
| 1159 | mStartingAnimation = false; |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 1160 | }); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 1163 | @VisibleForTesting |
Adrian Roos | db5b0c2 | 2020-02-12 15:05:27 -0800 | [diff] [blame] | 1164 | public void dispatchAnimationEnd(WindowInsetsAnimation animation) { |
Jorim Jaggi | cb28ae6 | 2020-05-14 17:46:32 +0200 | [diff] [blame^] | 1165 | Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, |
| 1166 | "InsetsAnimation: " + WindowInsets.Type.toString(animation.getTypeMask()), |
| 1167 | animation.getTypeMask()); |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1168 | mHost.dispatchWindowInsetsAnimationEnd(animation); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 1171 | @VisibleForTesting |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 1172 | @Override |
Adrian Roos | 6a4448f | 2020-04-01 15:01:08 +0200 | [diff] [blame] | 1173 | public void scheduleApplyChangeInsets(InsetsAnimationControlRunner runner) { |
| 1174 | if (mStartingAnimation || runner.getAnimationType() == ANIMATION_TYPE_USER) { |
Jorim Jaggi | a51168a | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 1175 | mAnimCallback.run(); |
| 1176 | mAnimCallbackScheduled = false; |
| 1177 | return; |
| 1178 | } |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 1179 | if (!mAnimCallbackScheduled) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1180 | mHost.postInsetsAnimationCallback(mAnimCallback); |
Jorim Jaggi | 02a741f | 2018-12-12 17:40:19 -0800 | [diff] [blame] | 1181 | mAnimCallbackScheduled = true; |
| 1182 | } |
| 1183 | } |
Jorim Jaggi | b7848b7 | 2018-12-28 14:38:21 +0100 | [diff] [blame] | 1184 | |
| 1185 | @Override |
Tiger Huang | 7238a05 | 2020-01-10 20:37:01 +0800 | [diff] [blame] | 1186 | public void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1187 | mHost.setSystemBarsAppearance(appearance, mask); |
Jorim Jaggi | b7848b7 | 2018-12-28 14:38:21 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | @Override |
Tiger Huang | 7238a05 | 2020-01-10 20:37:01 +0800 | [diff] [blame] | 1191 | public @Appearance int getSystemBarsAppearance() { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1192 | return mHost.getSystemBarsAppearance(); |
Tiger Huang | 7238a05 | 2020-01-10 20:37:01 +0800 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | @Override |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 1196 | public void setCaptionInsetsHeight(int height) { |
| 1197 | mCaptionInsetsHeight = height; |
| 1198 | } |
| 1199 | |
| 1200 | @Override |
Jorim Jaggi | b7848b7 | 2018-12-28 14:38:21 +0100 | [diff] [blame] | 1201 | public void setSystemBarsBehavior(@Behavior int behavior) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1202 | mHost.setSystemBarsBehavior(behavior); |
Jorim Jaggi | b7848b7 | 2018-12-28 14:38:21 +0100 | [diff] [blame] | 1203 | } |
Tiger Huang | 7238a05 | 2020-01-10 20:37:01 +0800 | [diff] [blame] | 1204 | |
| 1205 | @Override |
| 1206 | public @Appearance int getSystemBarsBehavior() { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1207 | return mHost.getSystemBarsBehavior(); |
Tiger Huang | 7238a05 | 2020-01-10 20:37:01 +0800 | [diff] [blame] | 1208 | } |
Rob Carr | 521e363 | 2020-03-09 11:22:30 -0700 | [diff] [blame] | 1209 | |
Jorim Jaggi | ed35b17 | 2020-03-06 00:13:57 +0100 | [diff] [blame] | 1210 | private @InsetsType int calculateControllableTypes() { |
| 1211 | if (!checkDisplayFramesForControlling()) { |
| 1212 | return 0; |
| 1213 | } |
| 1214 | @InsetsType int result = 0; |
| 1215 | for (int i = mSourceConsumers.size() - 1; i >= 0; i--) { |
| 1216 | InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i); |
| 1217 | if (consumer.getControl() != null) { |
| 1218 | result |= toPublicType(consumer.mType); |
| 1219 | } |
| 1220 | } |
| 1221 | return result; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * @return The types that are now animating due to a listener invoking control/show/hide |
| 1226 | */ |
| 1227 | private @InsetsType int invokeControllableInsetsChangedListeners() { |
| 1228 | mLastStartedAnimTypes = 0; |
| 1229 | @InsetsType int types = calculateControllableTypes(); |
| 1230 | int size = mControllableInsetsChangedListeners.size(); |
| 1231 | for (int i = 0; i < size; i++) { |
| 1232 | mControllableInsetsChangedListeners.get(i).onControllableInsetsChanged(this, types); |
| 1233 | } |
| 1234 | return mLastStartedAnimTypes; |
| 1235 | } |
| 1236 | |
| 1237 | @Override |
| 1238 | public void addOnControllableInsetsChangedListener( |
| 1239 | OnControllableInsetsChangedListener listener) { |
| 1240 | Objects.requireNonNull(listener); |
| 1241 | mControllableInsetsChangedListeners.add(listener); |
| 1242 | listener.onControllableInsetsChanged(this, calculateControllableTypes()); |
| 1243 | } |
| 1244 | |
| 1245 | @Override |
| 1246 | public void removeOnControllableInsetsChangedListener( |
| 1247 | OnControllableInsetsChangedListener listener) { |
| 1248 | Objects.requireNonNull(listener); |
| 1249 | mControllableInsetsChangedListeners.remove(listener); |
| 1250 | } |
| 1251 | |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1252 | @Override |
Rob Carr | 521e363 | 2020-03-09 11:22:30 -0700 | [diff] [blame] | 1253 | public void releaseSurfaceControlFromRt(SurfaceControl sc) { |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 1254 | mHost.releaseSurfaceControlFromRt(sc); |
| 1255 | } |
| 1256 | |
| 1257 | Host getHost() { |
| 1258 | return mHost; |
Rob Carr | 521e363 | 2020-03-09 11:22:30 -0700 | [diff] [blame] | 1259 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 1260 | } |