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