Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.server.wm; |
| 18 | |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 19 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND; |
| 20 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET; |
| 21 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 22 | import static android.content.pm.ActivityInfo.isFixedOrientationLandscape; |
| 23 | import static android.content.pm.ActivityInfo.isFixedOrientationPortrait; |
| 24 | import static android.content.res.Configuration.ORIENTATION_LANDSCAPE; |
| 25 | import static android.content.res.Configuration.ORIENTATION_PORTRAIT; |
| 26 | import static android.content.res.Configuration.ORIENTATION_UNDEFINED; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 27 | import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 28 | import static android.view.SurfaceControl.Transaction; |
Louis Chang | 7501e33 | 2018-08-20 13:08:39 +0800 | [diff] [blame] | 29 | |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 30 | import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER; |
| 31 | import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS; |
| 32 | import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS_ANIM; |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 33 | import static com.android.server.wm.WindowContainer.AnimationFlags.CHILDREN; |
| 34 | import static com.android.server.wm.WindowContainer.AnimationFlags.PARENTS; |
| 35 | import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION; |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 36 | import static com.android.server.wm.WindowContainerProto.CONFIGURATION_CONTAINER; |
| 37 | import static com.android.server.wm.WindowContainerProto.ORIENTATION; |
| 38 | import static com.android.server.wm.WindowContainerProto.SURFACE_ANIMATOR; |
| 39 | import static com.android.server.wm.WindowContainerProto.VISIBLE; |
Louis Chang | 7501e33 | 2018-08-20 13:08:39 +0800 | [diff] [blame] | 40 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; |
| 41 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 42 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 43 | import static com.android.server.wm.WindowManagerService.logWithStack; |
Yunfan Chen | 87b5a24 | 2019-10-01 17:53:59 +0900 | [diff] [blame] | 44 | import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 45 | import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_AFTER_ANIM; |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 46 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 47 | import android.annotation.CallSuper; |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 48 | import android.annotation.IntDef; |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 49 | import android.annotation.Nullable; |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 50 | import android.app.WindowConfiguration; |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 51 | import android.content.pm.ActivityInfo; |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 52 | import android.content.res.Configuration; |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 53 | import android.graphics.Point; |
| 54 | import android.graphics.Rect; |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 55 | import android.os.Debug; |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 56 | import android.os.IBinder; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 57 | import android.os.Trace; |
| 58 | import android.util.Pair; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 59 | import android.util.Pools; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 60 | import android.util.Slog; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 61 | import android.util.proto.ProtoOutputStream; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 62 | import android.view.DisplayInfo; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 63 | import android.view.MagnificationSpec; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 64 | import android.view.RemoteAnimationTarget; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 65 | import android.view.SurfaceControl; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 66 | import android.view.SurfaceControl.Builder; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 67 | import android.view.SurfaceSession; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 68 | import android.view.WindowManager; |
| 69 | import android.view.animation.Animation; |
Garfield Tan | 2f145f2 | 2018-11-01 15:27:03 -0700 | [diff] [blame] | 70 | |
Evan Rosky | 65dffa6 | 2019-02-04 14:09:53 -0800 | [diff] [blame] | 71 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 72 | import com.android.internal.util.ToBooleanFunction; |
Yunfan Chen | 87b5a24 | 2019-10-01 17:53:59 +0900 | [diff] [blame] | 73 | import com.android.server.policy.WindowManagerPolicy; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 74 | import com.android.server.protolog.common.ProtoLog; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 75 | import com.android.server.wm.SurfaceAnimator.Animatable; |
Garfield Tan | 2f145f2 | 2018-11-01 15:27:03 -0700 | [diff] [blame] | 76 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 77 | import java.io.PrintWriter; |
Yunfan Chen | 87b5a24 | 2019-10-01 17:53:59 +0900 | [diff] [blame] | 78 | import java.util.ArrayList; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 79 | import java.util.Comparator; |
| 80 | import java.util.LinkedList; |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 81 | import java.util.function.Consumer; |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 82 | import java.util.function.Function; |
Wale Ogunwale | d188096 | 2016-11-08 10:31:59 -0800 | [diff] [blame] | 83 | import java.util.function.Predicate; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Defines common functionality for classes that can hold windows directly or through their |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 87 | * children in a hierarchy form. |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 88 | * The test class is {@link WindowContainerTests} which must be kept up-to-date and ran anytime |
| 89 | * changes are made to this class. |
| 90 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 91 | class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<E> |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 92 | implements Comparable<WindowContainer>, Animatable { |
| 93 | |
| 94 | private static final String TAG = TAG_WITH_CLASS_NAME ? "WindowContainer" : TAG_WM; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 95 | |
Louis Chang | dc07727 | 2019-11-12 16:52:56 +0800 | [diff] [blame] | 96 | /** Animation layer that happens above all animating {@link ActivityStack}s. */ |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 97 | static final int ANIMATION_LAYER_STANDARD = 0; |
| 98 | |
Louis Chang | dc07727 | 2019-11-12 16:52:56 +0800 | [diff] [blame] | 99 | /** Animation layer that happens above all {@link ActivityStack}s. */ |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 100 | static final int ANIMATION_LAYER_BOOSTED = 1; |
| 101 | |
| 102 | /** |
| 103 | * Animation layer that is reserved for {@link WindowConfiguration#ACTIVITY_TYPE_HOME} |
Winson Chung | 732446a | 2018-09-19 13:15:17 -0700 | [diff] [blame] | 104 | * activities and all activities that are being controlled by the recents animation. This |
Louis Chang | dc07727 | 2019-11-12 16:52:56 +0800 | [diff] [blame] | 105 | * layer is generally below all {@link ActivityStack}s. |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 106 | */ |
| 107 | static final int ANIMATION_LAYER_HOME = 2; |
| 108 | |
| 109 | @IntDef(prefix = { "ANIMATION_LAYER_" }, value = { |
| 110 | ANIMATION_LAYER_STANDARD, |
| 111 | ANIMATION_LAYER_BOOSTED, |
| 112 | ANIMATION_LAYER_HOME, |
| 113 | }) |
| 114 | @interface AnimationLayer {} |
| 115 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 116 | static final int POSITION_TOP = Integer.MAX_VALUE; |
| 117 | static final int POSITION_BOTTOM = Integer.MIN_VALUE; |
| 118 | |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 119 | /** |
| 120 | * The parent of this window container. |
| 121 | * For removing or setting new parent {@link #setParent} should be used, because it also |
| 122 | * performs configuration updates based on new parent's settings. |
| 123 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 124 | private WindowContainer<WindowContainer> mParent = null; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 125 | |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 126 | // Set to true when we are performing a reparenting operation so we only send one |
| 127 | // onParentChanged() notification. |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 128 | boolean mReparenting; |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 129 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 130 | // List of children for this window container. List is in z-order as the children appear on |
| 131 | // screen with the top-most window container at the tail of the list. |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 132 | protected final WindowList<E> mChildren = new WindowList<E>(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 133 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 134 | // The specified orientation for this window container. |
| 135 | protected int mOrientation = SCREEN_ORIENTATION_UNSPECIFIED; |
| 136 | |
Wale Ogunwale | ee41d4a | 2016-11-21 08:41:10 -0800 | [diff] [blame] | 137 | private final Pools.SynchronizedPool<ForAllWindowsConsumerWrapper> mConsumerWrapperPool = |
| 138 | new Pools.SynchronizedPool<>(3); |
| 139 | |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 140 | // The display this window container is on. |
| 141 | protected DisplayContent mDisplayContent; |
| 142 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 143 | protected SurfaceControl mSurfaceControl; |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 144 | private int mLastLayer = 0; |
| 145 | private SurfaceControl mLastRelativeToLayer = null; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 146 | |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 147 | // TODO(b/132320879): Remove this from WindowContainers except DisplayContent. |
| 148 | private final Transaction mPendingTransaction; |
| 149 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 150 | /** |
Yunfan Chen | 87b5a24 | 2019-10-01 17:53:59 +0900 | [diff] [blame] | 151 | * Windows that clients are waiting to have drawn. |
| 152 | */ |
| 153 | final ArrayList<WindowState> mWaitingForDrawn = new ArrayList<>(); |
| 154 | |
| 155 | /** |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 156 | * Applied as part of the animation pass in "prepareSurfaces". |
| 157 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 158 | protected final SurfaceAnimator mSurfaceAnimator; |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 159 | protected final WindowManagerService mWmService; |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 160 | |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 161 | private final Point mTmpPos = new Point(); |
chaviw | 3e751af | 2018-01-11 11:22:39 -0800 | [diff] [blame] | 162 | protected final Point mLastSurfacePosition = new Point(); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 163 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 164 | /** Total number of elements in this subtree, including our own hierarchy element. */ |
| 165 | private int mTreeWeight = 1; |
| 166 | |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 167 | /** |
Vishnu Nair | ddd8074 | 2018-08-21 14:12:46 -0700 | [diff] [blame] | 168 | * Indicates whether we are animating and have committed the transaction to reparent our |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 169 | * surface to the animation leash |
| 170 | */ |
| 171 | private boolean mCommittedReparentToAnimationLeash; |
| 172 | |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 173 | private final Configuration mTmpConfig = new Configuration(); |
| 174 | |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 175 | /** Interface for {@link #isAnimating} to check which cases for the container is animating. */ |
| 176 | public interface AnimationFlags { |
| 177 | /** |
| 178 | * A bit flag indicates that {@link #isAnimating} should also return {@code true} |
| 179 | * even though the container is not yet animating, but the window container or its |
| 180 | * relatives as specified by PARENTS or CHILDREN are part of an {@link AppTransition} |
| 181 | * that is pending so an animation starts soon. |
| 182 | */ |
| 183 | int TRANSITION = 1; |
| 184 | |
| 185 | /** |
| 186 | * A bit flag indicates that {@link #isAnimating} should also check if one of the |
| 187 | * ancestors of the container are animating in addition to the container itself. |
| 188 | */ |
| 189 | int PARENTS = 2; |
| 190 | |
| 191 | /** |
| 192 | * A bit flag indicates that {@link #isAnimating} should also check if one of the |
| 193 | * descendants of the container are animating in addition to the container itself. |
| 194 | */ |
| 195 | int CHILDREN = 4; |
| 196 | } |
| 197 | |
Issei Suzuki | 25a9e2b | 2019-08-14 16:46:26 +0200 | [diff] [blame] | 198 | /** |
| 199 | * Callback which is triggered while changing the parent, after setting up the surface but |
| 200 | * before asking the parent to assign child layers. |
| 201 | */ |
| 202 | interface PreAssignChildLayersCallback { |
| 203 | void onPreAssignChildLayers(); |
| 204 | } |
| 205 | |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 206 | /** |
| 207 | * True if this an AppWindowToken and the activity which created this was launched with |
| 208 | * ActivityOptions.setLaunchTaskBehind. |
| 209 | * |
| 210 | * TODO(b/142617871): We run a special animation when the activity was launched with that |
| 211 | * flag, but it's not necessary anymore. Keep the window invisible until the task is explicitly |
| 212 | * selected to suppress an animation, and remove this flag. |
| 213 | */ |
| 214 | boolean mLaunchTaskBehind; |
| 215 | |
| 216 | /** |
| 217 | * If we are running an animation, this determines the transition type. Must be one of |
| 218 | * {@link AppTransition#TransitionFlags}. |
| 219 | */ |
| 220 | int mTransit; |
| 221 | |
| 222 | /** |
| 223 | * If we are running an animation, this determines the flags during this animation. Must be a |
| 224 | * bitwise combination of AppTransition.TRANSIT_FLAG_* constants. |
| 225 | */ |
| 226 | int mTransitFlags; |
| 227 | |
| 228 | /** Whether this container should be boosted at the top of all its siblings. */ |
| 229 | @VisibleForTesting boolean mNeedsZBoost; |
| 230 | |
| 231 | /** Layer used to constrain the animation to a container's stack bounds. */ |
| 232 | SurfaceControl mAnimationBoundsLayer; |
| 233 | |
| 234 | /** Whether this container needs to create mAnimationBoundsLayer for cropping animations. */ |
| 235 | boolean mNeedsAnimationBoundsLayer; |
| 236 | |
| 237 | /** |
| 238 | * This gets used during some open/close transitions as well as during a change transition |
| 239 | * where it represents the starting-state snapshot. |
| 240 | */ |
lumark | bc0032a | 2019-11-01 21:38:13 +0800 | [diff] [blame] | 241 | WindowContainerThumbnail mThumbnail; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 242 | final Rect mTransitStartRect = new Rect(); |
| 243 | final Point mTmpPoint = new Point(); |
| 244 | protected final Rect mTmpRect = new Rect(); |
| 245 | final Rect mTmpPrevBounds = new Rect(); |
| 246 | |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 247 | WindowContainer(WindowManagerService wms) { |
| 248 | mWmService = wms; |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 249 | mPendingTransaction = wms.mTransactionFactory.get(); |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 250 | mSurfaceAnimator = new SurfaceAnimator(this, this::onAnimationFinished, wms); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 251 | } |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 252 | |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 253 | @Override |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 254 | final protected WindowContainer getParent() { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 255 | return mParent; |
| 256 | } |
| 257 | |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 258 | @Override |
Wale Ogunwale | 6191149 | 2017-10-11 08:50:50 -0700 | [diff] [blame] | 259 | protected int getChildCount() { |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 260 | return mChildren.size(); |
| 261 | } |
| 262 | |
| 263 | @Override |
Wale Ogunwale | 6191149 | 2017-10-11 08:50:50 -0700 | [diff] [blame] | 264 | protected E getChildAt(int index) { |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 265 | return mChildren.get(index); |
| 266 | } |
| 267 | |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 268 | @Override |
| 269 | public void onConfigurationChanged(Configuration newParentConfig) { |
| 270 | super.onConfigurationChanged(newParentConfig); |
chaviw | 2f0567b | 2018-01-29 16:22:02 -0800 | [diff] [blame] | 271 | updateSurfacePosition(); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 272 | scheduleAnimation(); |
| 273 | } |
| 274 | |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 275 | void reparent(WindowContainer newParent, int position) { |
| 276 | if (newParent == null) { |
| 277 | throw new IllegalArgumentException("reparent: can't reparent to null " + this); |
| 278 | } |
| 279 | |
| 280 | final WindowContainer oldParent = mParent; |
| 281 | if (mParent == newParent) { |
| 282 | throw new IllegalArgumentException("WC=" + this + " already child of " + mParent); |
| 283 | } |
| 284 | |
| 285 | // The display object before reparenting as that might lead to old parent getting removed |
| 286 | // from the display if it no longer has any child. |
| 287 | final DisplayContent prevDc = oldParent.getDisplayContent(); |
| 288 | final DisplayContent dc = newParent.getDisplayContent(); |
| 289 | |
| 290 | mReparenting = true; |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 291 | oldParent.removeChild(this); |
Louis Chang | 2453d06 | 2019-11-19 22:30:48 +0800 | [diff] [blame] | 292 | newParent.addChild(this, position); |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 293 | mReparenting = false; |
| 294 | |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 295 | // Relayout display(s) |
| 296 | dc.setLayoutNeeded(); |
| 297 | if (prevDc != dc) { |
| 298 | onDisplayChanged(dc); |
| 299 | prevDc.setLayoutNeeded(); |
| 300 | } |
| 301 | getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 302 | |
| 303 | // Send onParentChanged notification here is we disabled sending it in setParent for |
| 304 | // reparenting case. |
| 305 | onParentChanged(newParent, oldParent); |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 308 | final protected void setParent(WindowContainer<WindowContainer> parent) { |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 309 | final WindowContainer oldParent = mParent; |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 310 | mParent = parent; |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 311 | |
| 312 | if (mParent != null) { |
| 313 | mParent.onChildAdded(this); |
| 314 | } |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 315 | if (!mReparenting) { |
| 316 | onParentChanged(mParent, oldParent); |
| 317 | } |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Callback that is triggered when @link WindowContainer#setParent(WindowContainer)} was called. |
| 322 | * Supposed to be overridden and contain actions that should be executed after parent was set. |
| 323 | */ |
Riddle Hsu | 3a4bb61 | 2019-01-31 00:02:22 +0800 | [diff] [blame] | 324 | @Override |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 325 | void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) { |
| 326 | onParentChanged(newParent, oldParent, null); |
Issei Suzuki | 25a9e2b | 2019-08-14 16:46:26 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Wale Ogunwale | c17418e | 2019-10-13 23:00:40 +0200 | [diff] [blame] | 329 | void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent, |
| 330 | PreAssignChildLayersCallback callback) { |
| 331 | super.onParentChanged(newParent, oldParent); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 332 | if (mParent == null) { |
| 333 | return; |
| 334 | } |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 335 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 336 | if (mSurfaceControl == null) { |
| 337 | // If we don't yet have a surface, but we now have a parent, we should |
| 338 | // build a surface. |
| 339 | mSurfaceControl = makeSurface().build(); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 340 | getPendingTransaction().show(mSurfaceControl); |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 341 | updateSurfacePosition(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 342 | } else { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 343 | // If we have a surface but a new parent, we just need to perform a reparent. Go through |
| 344 | // surface animator such that hierarchy is preserved when animating, i.e. |
| 345 | // mSurfaceControl stays attached to the leash and we just reparent the leash to the |
| 346 | // new parent. |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 347 | reparentSurfaceControl(getPendingTransaction(), mParent.mSurfaceControl); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Issei Suzuki | 25a9e2b | 2019-08-14 16:46:26 +0200 | [diff] [blame] | 350 | if (callback != null) { |
| 351 | callback.onPreAssignChildLayers(); |
| 352 | } |
| 353 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 354 | // Either way we need to ask the parent to assign us a Z-order. |
| 355 | mParent.assignChildLayers(); |
| 356 | scheduleAnimation(); |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 359 | // Temp. holders for a chain of containers we are currently processing. |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 360 | private final LinkedList<WindowContainer> mTmpChain1 = new LinkedList<>(); |
| 361 | private final LinkedList<WindowContainer> mTmpChain2 = new LinkedList<>(); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 362 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 363 | /** |
| 364 | * Adds the input window container has a child of this container in order based on the input |
| 365 | * comparator. |
| 366 | * @param child The window container to add as a child of this window container. |
| 367 | * @param comparator Comparator to use in determining the position the child should be added to. |
| 368 | * If null, the child will be added to the top. |
| 369 | */ |
| 370 | @CallSuper |
Wale Ogunwale | d90546a | 2016-09-09 23:28:03 -0700 | [diff] [blame] | 371 | protected void addChild(E child, Comparator<E> comparator) { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 372 | if (!child.mReparenting && child.getParent() != null) { |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 373 | throw new IllegalArgumentException("addChild: container=" + child.getName() |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 374 | + " is already a child of container=" + child.getParent().getName() |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 375 | + " can't add to container=" + getName()); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 376 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 377 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 378 | int positionToAdd = -1; |
| 379 | if (comparator != null) { |
| 380 | final int count = mChildren.size(); |
| 381 | for (int i = 0; i < count; i++) { |
| 382 | if (comparator.compare(child, mChildren.get(i)) < 0) { |
| 383 | positionToAdd = i; |
| 384 | break; |
| 385 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 389 | if (positionToAdd == -1) { |
| 390 | mChildren.add(child); |
| 391 | } else { |
| 392 | mChildren.add(positionToAdd, child); |
| 393 | } |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 394 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 395 | // Set the parent after we've actually added a child in case a subclass depends on this. |
| 396 | child.setParent(this); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 399 | /** Adds the input window container has a child of this container at the input index. */ |
| 400 | @CallSuper |
Wale Ogunwale | 72919d2 | 2016-12-08 18:58:50 -0800 | [diff] [blame] | 401 | void addChild(E child, int index) { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 402 | if (!child.mReparenting && child.getParent() != null) { |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 403 | throw new IllegalArgumentException("addChild: container=" + child.getName() |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 404 | + " is already a child of container=" + child.getParent().getName() |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 405 | + " can't add to container=" + getName() |
| 406 | + "\n callers=" + Debug.getCallers(15, "\n")); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 407 | } |
Kazuki Takise | f85197b | 2018-06-18 18:18:36 +0900 | [diff] [blame] | 408 | |
| 409 | if ((index < 0 && index != POSITION_BOTTOM) |
| 410 | || (index > mChildren.size() && index != POSITION_TOP)) { |
| 411 | throw new IllegalArgumentException("addChild: invalid position=" + index |
| 412 | + ", children number=" + mChildren.size()); |
| 413 | } |
| 414 | |
| 415 | if (index == POSITION_TOP) { |
| 416 | index = mChildren.size(); |
| 417 | } else if (index == POSITION_BOTTOM) { |
| 418 | index = 0; |
| 419 | } |
| 420 | |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 421 | mChildren.add(index, child); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 422 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 423 | // Set the parent after we've actually added a child in case a subclass depends on this. |
| 424 | child.setParent(this); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 427 | private void onChildAdded(WindowContainer child) { |
| 428 | mTreeWeight += child.mTreeWeight; |
| 429 | WindowContainer parent = getParent(); |
| 430 | while (parent != null) { |
| 431 | parent.mTreeWeight += child.mTreeWeight; |
| 432 | parent = parent.getParent(); |
| 433 | } |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 434 | onChildPositionChanged(child); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 437 | /** |
| 438 | * Removes the input child container from this container which is its parent. |
| 439 | * |
| 440 | * @return True if the container did contain the input child and it was detached. |
| 441 | */ |
| 442 | @CallSuper |
| 443 | void removeChild(E child) { |
| 444 | if (mChildren.remove(child)) { |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 445 | onChildRemoved(child); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 446 | if (!child.mReparenting) { |
| 447 | child.setParent(null); |
| 448 | } |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 449 | } else { |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 450 | throw new IllegalArgumentException("removeChild: container=" + child.getName() |
| 451 | + " is not a child of container=" + getName()); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 455 | private void onChildRemoved(WindowContainer child) { |
| 456 | mTreeWeight -= child.mTreeWeight; |
| 457 | WindowContainer parent = getParent(); |
| 458 | while (parent != null) { |
| 459 | parent.mTreeWeight -= child.mTreeWeight; |
| 460 | parent = parent.getParent(); |
| 461 | } |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 462 | onChildPositionChanged(child); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 465 | /** |
| 466 | * Removes this window container and its children with no regard for what else might be going on |
| 467 | * in the system. For example, the container will be removed during animation if this method is |
| 468 | * called which isn't desirable. For most cases you want to call {@link #removeIfPossible()} |
| 469 | * which allows the system to defer removal until a suitable time. |
| 470 | */ |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 471 | @CallSuper |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 472 | void removeImmediately() { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 473 | while (!mChildren.isEmpty()) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 474 | final E child = mChildren.peekLast(); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 475 | child.removeImmediately(); |
| 476 | // Need to do this after calling remove on the child because the child might try to |
| 477 | // remove/detach itself from its parent which will cause an exception if we remove |
| 478 | // it before calling remove on the child. |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 479 | if (mChildren.remove(child)) { |
| 480 | onChildRemoved(child); |
| 481 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 484 | if (mSurfaceControl != null) { |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 485 | getPendingTransaction().remove(mSurfaceControl); |
chaviw | 6728e2f | 2018-03-19 15:58:04 -0700 | [diff] [blame] | 486 | |
| 487 | // Merge to parent transaction to ensure the transactions on this WindowContainer are |
| 488 | // applied in native even if WindowContainer is removed. |
| 489 | if (mParent != null) { |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 490 | mParent.getPendingTransaction().merge(getPendingTransaction()); |
chaviw | 6728e2f | 2018-03-19 15:58:04 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 493 | mSurfaceControl = null; |
Garfield Tan | 265ab91a | 2019-11-12 16:30:49 -0800 | [diff] [blame] | 494 | mLastSurfacePosition.set(0, 0); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 495 | scheduleAnimation(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 498 | if (mParent != null) { |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 499 | mParent.removeChild(this); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | /** |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 504 | * @return The index of this element in the hierarchy tree in prefix order. |
| 505 | */ |
| 506 | int getPrefixOrderIndex() { |
| 507 | if (mParent == null) { |
| 508 | return 0; |
| 509 | } |
| 510 | return mParent.getPrefixOrderIndex(this); |
| 511 | } |
| 512 | |
| 513 | private int getPrefixOrderIndex(WindowContainer child) { |
| 514 | int order = 0; |
| 515 | for (int i = 0; i < mChildren.size(); i++) { |
| 516 | final WindowContainer childI = mChildren.get(i); |
| 517 | if (child == childI) { |
| 518 | break; |
| 519 | } |
| 520 | order += childI.mTreeWeight; |
| 521 | } |
| 522 | if (mParent != null) { |
| 523 | order += mParent.getPrefixOrderIndex(this); |
| 524 | } |
| 525 | |
| 526 | // We also need to count ourselves. |
| 527 | order++; |
| 528 | return order; |
| 529 | } |
| 530 | |
| 531 | /** |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 532 | * Removes this window container and its children taking care not to remove them during a |
| 533 | * critical stage in the system. For example, some containers will not be removed during |
| 534 | * animation if this method is called. |
| 535 | */ |
| 536 | // TODO: figure-out implementation that works best for this. |
| 537 | // E.g. when do we remove from parent list? maybe not... |
| 538 | void removeIfPossible() { |
| 539 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 540 | final WindowContainer wc = mChildren.get(i); |
| 541 | wc.removeIfPossible(); |
| 542 | } |
| 543 | } |
| 544 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 545 | /** Returns true if this window container has the input child. */ |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 546 | boolean hasChild(E child) { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 547 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 548 | final E current = mChildren.get(i); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 549 | if (current == child || current.hasChild(child)) { |
| 550 | return true; |
| 551 | } |
| 552 | } |
| 553 | return false; |
| 554 | } |
Wale Ogunwale | 9bc4773 | 2016-08-10 14:44:22 -0700 | [diff] [blame] | 555 | |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 556 | /** |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 557 | * Move a child from it's current place in siblings list to the specified position, |
| 558 | * with an option to move all its parents to top. |
| 559 | * @param position Target position to move the child to. |
| 560 | * @param child Child to move to selected position. |
| 561 | * @param includingParents Flag indicating whether we need to move the entire branch of the |
| 562 | * hierarchy when we're moving a child to {@link #POSITION_TOP} or |
| 563 | * {@link #POSITION_BOTTOM}. When moving to other intermediate positions |
| 564 | * this flag will do nothing. |
| 565 | */ |
| 566 | @CallSuper |
| 567 | void positionChildAt(int position, E child, boolean includingParents) { |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 568 | |
| 569 | if (child.getParent() != this) { |
| 570 | throw new IllegalArgumentException("removeChild: container=" + child.getName() |
| 571 | + " is not a child of container=" + getName() |
| 572 | + " current parent=" + child.getParent()); |
| 573 | } |
| 574 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 575 | if (position >= mChildren.size() - 1) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 576 | position = POSITION_TOP; |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 577 | } else if (position <= 0) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 578 | position = POSITION_BOTTOM; |
| 579 | } |
| 580 | |
| 581 | switch (position) { |
| 582 | case POSITION_TOP: |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 583 | if (mChildren.peekLast() != child) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 584 | mChildren.remove(child); |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 585 | mChildren.add(child); |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 586 | onChildPositionChanged(child); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 587 | } |
| 588 | if (includingParents && getParent() != null) { |
| 589 | getParent().positionChildAt(POSITION_TOP, this /* child */, |
| 590 | true /* includingParents */); |
| 591 | } |
| 592 | break; |
| 593 | case POSITION_BOTTOM: |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 594 | if (mChildren.peekFirst() != child) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 595 | mChildren.remove(child); |
| 596 | mChildren.addFirst(child); |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 597 | onChildPositionChanged(child); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 598 | } |
| 599 | if (includingParents && getParent() != null) { |
| 600 | getParent().positionChildAt(POSITION_BOTTOM, this /* child */, |
| 601 | true /* includingParents */); |
| 602 | } |
| 603 | break; |
| 604 | default: |
Winson Chung | 123e07a | 2018-02-27 11:47:16 -0800 | [diff] [blame] | 605 | // TODO: Removing the child before reinserting requires the caller to provide a |
| 606 | // position that takes into account the removed child (if the index of the |
| 607 | // child < position, then the position should be adjusted). We should consider |
| 608 | // doing this adjustment here and remove any adjustments in the callers. |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 609 | mChildren.remove(child); |
| 610 | mChildren.add(position, child); |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 611 | onChildPositionChanged(child); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
Tiger Huang | 1e5b10a | 2018-07-30 20:19:51 +0800 | [diff] [blame] | 616 | * Notify that a child's position has changed. Possible changes are adding or removing a child. |
| 617 | */ |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 618 | void onChildPositionChanged(WindowContainer child) { } |
Tiger Huang | 1e5b10a | 2018-07-30 20:19:51 +0800 | [diff] [blame] | 619 | |
| 620 | /** |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 621 | * Update override configuration and recalculate full config. |
Evan Rosky | dfe3da7 | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 622 | * @see #mRequestedOverrideConfiguration |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 623 | * @see #mFullConfiguration |
| 624 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 625 | @Override |
Evan Rosky | dfe3da7 | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 626 | public void onRequestedOverrideConfigurationChanged(Configuration overrideConfiguration) { |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 627 | // We must diff before the configuration is applied so that we can capture the change |
| 628 | // against the existing bounds. |
Evan Rosky | dfe3da7 | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 629 | final int diff = diffRequestedOverrideBounds( |
| 630 | overrideConfiguration.windowConfiguration.getBounds()); |
| 631 | super.onRequestedOverrideConfigurationChanged(overrideConfiguration); |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 632 | if (mParent != null) { |
| 633 | mParent.onDescendantOverrideConfigurationChanged(); |
| 634 | } |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 635 | |
| 636 | if (diff == BOUNDS_CHANGE_NONE) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | if ((diff & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) { |
| 641 | onResize(); |
| 642 | } else { |
| 643 | onMovedByResize(); |
| 644 | } |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Notify that a descendant's overrideConfiguration has changed. |
| 649 | */ |
| 650 | void onDescendantOverrideConfigurationChanged() { |
| 651 | if (mParent != null) { |
| 652 | mParent.onDescendantOverrideConfigurationChanged(); |
| 653 | } |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
Garfield Tan | 2f145f2 | 2018-11-01 15:27:03 -0700 | [diff] [blame] | 657 | * Notify that the display this container is on has changed. This could be either this container |
| 658 | * is moved to a new display, or some configurations on the display it is on changes. |
| 659 | * |
| 660 | * @param dc The display this container is on after changes. |
Wale Ogunwale | 02319a6 | 2016-09-26 15:21:22 -0700 | [diff] [blame] | 661 | */ |
| 662 | void onDisplayChanged(DisplayContent dc) { |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 663 | mDisplayContent = dc; |
| 664 | if (dc != null && dc != this) { |
| 665 | dc.getPendingTransaction().merge(mPendingTransaction); |
| 666 | } |
Wale Ogunwale | 02319a6 | 2016-09-26 15:21:22 -0700 | [diff] [blame] | 667 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 668 | final WindowContainer child = mChildren.get(i); |
| 669 | child.onDisplayChanged(dc); |
| 670 | } |
| 671 | } |
| 672 | |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 673 | DisplayContent getDisplayContent() { |
| 674 | return mDisplayContent; |
| 675 | } |
| 676 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 677 | void setWaitingForDrawnIfResizingChanged() { |
| 678 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 679 | final WindowContainer wc = mChildren.get(i); |
| 680 | wc.setWaitingForDrawnIfResizingChanged(); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | void onResize() { |
| 685 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 686 | final WindowContainer wc = mChildren.get(i); |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 687 | wc.onParentResize(); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 691 | void onParentResize() { |
| 692 | // In the case this container has specified its own bounds, a parent resize will not |
| 693 | // affect its bounds. Any relevant changes will be propagated through changes to the |
| 694 | // Configuration override. |
| 695 | if (hasOverrideBounds()) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | // Default implementation is to treat as resize on self. |
| 700 | onResize(); |
| 701 | } |
| 702 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 703 | void onMovedByResize() { |
| 704 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 705 | final WindowContainer wc = mChildren.get(i); |
| 706 | wc.onMovedByResize(); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | void resetDragResizingChangeReported() { |
| 711 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 712 | final WindowContainer wc = mChildren.get(i); |
| 713 | wc.resetDragResizingChangeReported(); |
| 714 | } |
| 715 | } |
| 716 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 717 | void forceWindowsScaleableInTransaction(boolean force) { |
| 718 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 719 | final WindowContainer wc = mChildren.get(i); |
| 720 | wc.forceWindowsScaleableInTransaction(force); |
| 721 | } |
| 722 | } |
| 723 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 724 | /** |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 725 | * @return {@code true} when this container or its related containers are running an |
| 726 | * animation, {@code false} otherwise. |
| 727 | * |
| 728 | * By default this predicate only checks if this container itself is actually running an |
| 729 | * animation, but you can extend the check target over its relatives, or relax the condition |
| 730 | * so that this can return {@code true} if an animation starts soon by giving a combination |
| 731 | * of {@link #AnimationFlags}. |
| 732 | * |
| 733 | * Note that you can give a combination of bitmask flags to specify targets and condition for |
| 734 | * checking animating status. |
| 735 | * e.g. {@code isAnimating(TRANSITION | PARENT)} returns {@code true} if either this |
| 736 | * container itself or one of its parents is running an animation or waiting for an app |
| 737 | * transition. |
| 738 | * |
| 739 | * Note that TRANSITION propagates to parents and children as well. |
| 740 | * |
| 741 | * {@see AnimationFlags#TRANSITION} |
| 742 | * {@see AnimationFlags#PARENTS} |
| 743 | * {@see AnimationFlags#CHILDREN} |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 744 | */ |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 745 | final boolean isAnimating(int flags) { |
| 746 | if (mSurfaceAnimator.isAnimating()) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 747 | return true; |
| 748 | } |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 749 | if ((flags & TRANSITION) != 0 && isWaitingForTransitionStart()) { |
| 750 | return true; |
| 751 | } |
| 752 | if ((flags & PARENTS) != 0) { |
| 753 | final WindowContainer parent = getParent(); |
| 754 | if (parent != null && parent.isAnimating(flags & ~CHILDREN)) { |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 755 | return true; |
| 756 | } |
| 757 | } |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 758 | if ((flags & CHILDREN) != 0) { |
| 759 | for (int i = 0; i < mChildren.size(); ++i) { |
| 760 | final WindowContainer wc = mChildren.get(i); |
| 761 | if (wc.isAnimating(flags & ~PARENTS)) { |
| 762 | return true; |
| 763 | } |
| 764 | } |
| 765 | } |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 766 | return false; |
| 767 | } |
| 768 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 769 | /** |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 770 | * @return {@code true} when the container is waiting the app transition start, {@code false} |
| 771 | * otherwise. |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 772 | */ |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 773 | boolean isWaitingForTransitionStart() { |
| 774 | return false; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /** |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 778 | * @return {@code true} if in this subtree of the hierarchy we have an |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 779 | * {@code ActivityRecord#isAnimating(TRANSITION)}, {@code false} otherwise. |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 780 | */ |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 781 | boolean isAppTransitioning() { |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 782 | return getActivity(app -> app.isAnimating(TRANSITION)) != null; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /** |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 786 | * @return Whether our own container running an animation at the moment. |
| 787 | */ |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 788 | final boolean isAnimating() { |
| 789 | return isAnimating(0 /* self only */); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 790 | } |
| 791 | |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 792 | /** |
| 793 | * @return {@code true} if the container is in changing app transition. |
| 794 | */ |
| 795 | boolean isChangingAppTransition() { |
| 796 | return false; |
| 797 | } |
| 798 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 799 | void sendAppVisibilityToClients() { |
| 800 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 801 | final WindowContainer wc = mChildren.get(i); |
| 802 | wc.sendAppVisibilityToClients(); |
| 803 | } |
| 804 | } |
| 805 | |
Wale Ogunwale | 44f2180 | 2016-09-02 12:49:48 -0700 | [diff] [blame] | 806 | /** |
| 807 | * Returns true if the container or one of its children as some content it can display or wants |
| 808 | * to display (e.g. app views or saved surface). |
| 809 | * |
| 810 | * NOTE: While this method will return true if the there is some content to display, it doesn't |
| 811 | * mean the container is visible. Use {@link #isVisible()} to determine if the container is |
| 812 | * visible. |
| 813 | */ |
| 814 | boolean hasContentToDisplay() { |
| 815 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 816 | final WindowContainer wc = mChildren.get(i); |
| 817 | if (wc.hasContentToDisplay()) { |
| 818 | return true; |
| 819 | } |
| 820 | } |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Returns true if the container or one of its children is considered visible from the |
| 826 | * WindowManager perspective which usually means valid surface and some other internal state |
| 827 | * are true. |
| 828 | * |
| 829 | * NOTE: While this method will return true if the surface is visible, it doesn't mean the |
| 830 | * client has actually displayed any content. Use {@link #hasContentToDisplay()} to determine if |
| 831 | * the container has any content to display. |
| 832 | */ |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 833 | boolean isVisible() { |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 834 | // TODO: Will this be more correct if it checks the visibility of its parents? |
| 835 | // It depends...For example, Tasks and Stacks are only visible if there children are visible |
| 836 | // but, WindowState are not visible if there parent are not visible. Maybe have the |
Andrii Kulian | cd5dcb8b | 2017-01-03 17:09:45 -0800 | [diff] [blame] | 837 | // container specify which direction to traverse for visibility? |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 838 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 839 | final WindowContainer wc = mChildren.get(i); |
| 840 | if (wc.isVisible()) { |
| 841 | return true; |
| 842 | } |
| 843 | } |
| 844 | return false; |
| 845 | } |
| 846 | |
Bryce Lee | 00d586d | 2017-07-28 20:48:43 -0700 | [diff] [blame] | 847 | /** |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 848 | * @return Whether this child is on top of the window hierarchy. |
Bryce Lee | 00d586d | 2017-07-28 20:48:43 -0700 | [diff] [blame] | 849 | */ |
| 850 | boolean isOnTop() { |
| 851 | return getParent().getTopChild() == this && getParent().isOnTop(); |
| 852 | } |
| 853 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 854 | /** Returns the top child container. */ |
| 855 | E getTopChild() { |
| 856 | return mChildren.peekLast(); |
Wale Ogunwale | 9bc4773 | 2016-08-10 14:44:22 -0700 | [diff] [blame] | 857 | } |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 858 | |
Wale Ogunwale | 3f4433d | 2016-08-18 20:42:42 -0700 | [diff] [blame] | 859 | /** Returns true if there is still a removal being deferred */ |
| 860 | boolean checkCompleteDeferredRemoval() { |
| 861 | boolean stillDeferringRemoval = false; |
| 862 | |
| 863 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 864 | final WindowContainer wc = mChildren.get(i); |
| 865 | stillDeferringRemoval |= wc.checkCompleteDeferredRemoval(); |
| 866 | } |
| 867 | |
| 868 | return stillDeferringRemoval; |
| 869 | } |
| 870 | |
| 871 | /** Checks if all windows in an app are all drawn and shows them if needed. */ |
Wale Ogunwale | b0f3b83 | 2016-10-17 10:13:07 -0700 | [diff] [blame] | 872 | void checkAppWindowsReadyToShow() { |
Wale Ogunwale | 3f4433d | 2016-08-18 20:42:42 -0700 | [diff] [blame] | 873 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 874 | final WindowContainer wc = mChildren.get(i); |
Wale Ogunwale | b0f3b83 | 2016-10-17 10:13:07 -0700 | [diff] [blame] | 875 | wc.checkAppWindowsReadyToShow(); |
Wale Ogunwale | 3f4433d | 2016-08-18 20:42:42 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 879 | void onAppTransitionDone() { |
| 880 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 881 | final WindowContainer wc = mChildren.get(i); |
| 882 | wc.onAppTransitionDone(); |
| 883 | } |
| 884 | } |
| 885 | |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 886 | /** |
| 887 | * Called when this container or one of its descendants changed its requested orientation, and |
| 888 | * wants this container to handle it or pass it to its parent. |
| 889 | * |
| 890 | * @param freezeDisplayToken freeze this app window token if display needs to freeze |
| 891 | * @param requestingContainer the container which orientation request has changed |
| 892 | * @return {@code true} if handled; {@code false} otherwise. |
| 893 | */ |
| 894 | boolean onDescendantOrientationChanged(@Nullable IBinder freezeDisplayToken, |
| 895 | @Nullable ConfigurationContainer requestingContainer) { |
| 896 | final WindowContainer parent = getParent(); |
| 897 | if (parent == null) { |
| 898 | return false; |
| 899 | } |
| 900 | return parent.onDescendantOrientationChanged(freezeDisplayToken, |
| 901 | requestingContainer); |
| 902 | } |
| 903 | |
| 904 | /** |
Garfield Tan | 49dae10 | 2019-02-04 09:51:59 -0800 | [diff] [blame] | 905 | * Check if this container or its parent will handle orientation changes from descendants. It's |
| 906 | * different from the return value of {@link #onDescendantOrientationChanged(IBinder, |
| 907 | * ConfigurationContainer)} in the sense that the return value of this method tells if this |
| 908 | * container or its parent will handle the request eventually, while the return value of the |
| 909 | * other method is if it handled the request synchronously. |
| 910 | * |
| 911 | * @return {@code true} if it handles or will handle orientation change in the future; {@code |
| 912 | * false} if it won't handle the change at anytime. |
| 913 | */ |
| 914 | boolean handlesOrientationChangeFromDescendant() { |
| 915 | final WindowContainer parent = getParent(); |
| 916 | return parent != null && parent.handlesOrientationChangeFromDescendant(); |
| 917 | } |
| 918 | |
| 919 | /** |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 920 | * Get the configuration orientation by the requested screen orientation |
| 921 | * ({@link ActivityInfo.ScreenOrientation}) of this activity. |
| 922 | * |
| 923 | * @return orientation in ({@link Configuration#ORIENTATION_LANDSCAPE}, |
| 924 | * {@link Configuration#ORIENTATION_PORTRAIT}, |
| 925 | * {@link Configuration#ORIENTATION_UNDEFINED}). |
| 926 | */ |
| 927 | int getRequestedConfigurationOrientation() { |
| 928 | if (mOrientation == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) { |
| 929 | // NOSENSOR means the display's "natural" orientation, so return that. |
| 930 | if (mDisplayContent != null) { |
| 931 | return mDisplayContent.getNaturalOrientation(); |
| 932 | } |
| 933 | } else if (mOrientation == ActivityInfo.SCREEN_ORIENTATION_LOCKED) { |
| 934 | // LOCKED means the activity's orientation remains unchanged, so return existing value. |
| 935 | return getConfiguration().orientation; |
| 936 | } else if (isFixedOrientationLandscape(mOrientation)) { |
| 937 | return ORIENTATION_LANDSCAPE; |
| 938 | } else if (isFixedOrientationPortrait(mOrientation)) { |
| 939 | return ORIENTATION_PORTRAIT; |
| 940 | } |
| 941 | return ORIENTATION_UNDEFINED; |
| 942 | } |
| 943 | |
| 944 | /** |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 945 | * Calls {@link #setOrientation(int, IBinder, ActivityRecord)} with {@code null} to the last 2 |
| 946 | * parameters. |
| 947 | * |
| 948 | * @param orientation the specified orientation. |
| 949 | */ |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 950 | void setOrientation(int orientation) { |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 951 | setOrientation(orientation, null /* freezeDisplayToken */, |
| 952 | null /* ActivityRecord */); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Sets the specified orientation of this container. It percolates this change upward along the |
| 957 | * hierarchy to let each level of the hierarchy a chance to respond to it. |
| 958 | * |
| 959 | * @param orientation the specified orientation. Needs to be one of {@link |
| 960 | * android.content.pm.ActivityInfo.ScreenOrientation}. |
| 961 | * @param freezeDisplayToken uses this token to freeze display if orientation change is not |
| 962 | * done. Display will not be frozen if this is {@code null}, which |
| 963 | * should only happen in tests. |
| 964 | * @param requestingContainer the container which orientation request has changed. Mostly used |
| 965 | * to ensure it gets correct configuration. |
| 966 | */ |
| 967 | void setOrientation(int orientation, @Nullable IBinder freezeDisplayToken, |
| 968 | @Nullable ConfigurationContainer requestingContainer) { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 969 | if (mOrientation == orientation) { |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 970 | return; |
| 971 | } |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 972 | |
| 973 | mOrientation = orientation; |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 974 | final int configOrientation = getRequestedConfigurationOrientation(); |
| 975 | if (getRequestedOverrideConfiguration().orientation != configOrientation) { |
| 976 | mTmpConfig.setTo(getRequestedOverrideConfiguration()); |
| 977 | mTmpConfig.orientation = configOrientation; |
| 978 | onRequestedOverrideConfigurationChanged(mTmpConfig); |
| 979 | } |
| 980 | |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 981 | final WindowContainer parent = getParent(); |
| 982 | if (parent != null) { |
| 983 | onDescendantOrientationChanged(freezeDisplayToken, requestingContainer); |
| 984 | } |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 987 | int getOrientation() { |
| 988 | return getOrientation(mOrientation); |
| 989 | } |
| 990 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 991 | /** |
| 992 | * Returns the specified orientation for this window container or one of its children is there |
| 993 | * is one set, or {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSET} if no |
| 994 | * specification is set. |
| 995 | * NOTE: {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED} is a |
| 996 | * specification... |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 997 | * |
| 998 | * @param candidate The current orientation candidate that will be returned if we don't find a |
| 999 | * better match. |
| 1000 | * @return The orientation as specified by this branch or the window hierarchy. |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 1001 | */ |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 1002 | int getOrientation(int candidate) { |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 1003 | if (!fillsParent()) { |
| 1004 | // Ignore containers that don't completely fill their parents. |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 1005 | return SCREEN_ORIENTATION_UNSET; |
| 1006 | } |
| 1007 | |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 1008 | // The container fills its parent so we can use it orientation if it has one |
| 1009 | // specified; otherwise we prefer to use the orientation of its topmost child that has one |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 1010 | // specified and fall back on this container's unset or unspecified value as a candidate |
| 1011 | // if none of the children have a better candidate for the orientation. |
| 1012 | if (mOrientation != SCREEN_ORIENTATION_UNSET |
| 1013 | && mOrientation != SCREEN_ORIENTATION_UNSPECIFIED) { |
| 1014 | return mOrientation; |
| 1015 | } |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 1016 | |
| 1017 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1018 | final WindowContainer wc = mChildren.get(i); |
| 1019 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 1020 | // TODO: Maybe mOrientation should default to SCREEN_ORIENTATION_UNSET vs. |
| 1021 | // SCREEN_ORIENTATION_UNSPECIFIED? |
| 1022 | final int orientation = wc.getOrientation(candidate == SCREEN_ORIENTATION_BEHIND |
| 1023 | ? SCREEN_ORIENTATION_BEHIND : SCREEN_ORIENTATION_UNSET); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 1024 | if (orientation == SCREEN_ORIENTATION_BEHIND) { |
| 1025 | // container wants us to use the orientation of the container behind it. See if we |
| 1026 | // can find one. Else return SCREEN_ORIENTATION_BEHIND so the caller can choose to |
| 1027 | // look behind this container. |
| 1028 | candidate = orientation; |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | if (orientation == SCREEN_ORIENTATION_UNSET) { |
| 1033 | continue; |
| 1034 | } |
| 1035 | |
| 1036 | if (wc.fillsParent() || orientation != SCREEN_ORIENTATION_UNSPECIFIED) { |
| 1037 | // Use the orientation if the container fills its parent or requested an explicit |
| 1038 | // orientation that isn't SCREEN_ORIENTATION_UNSPECIFIED. |
| 1039 | return orientation; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | return candidate; |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * Returns true if this container is opaque and fills all the space made available by its parent |
| 1048 | * container. |
| 1049 | * |
| 1050 | * NOTE: It is possible for this container to occupy more space than the parent has (or less), |
| 1051 | * this is just a signal from the client to window manager stating its intent, but not what it |
| 1052 | * actually does. |
| 1053 | */ |
| 1054 | boolean fillsParent() { |
| 1055 | return false; |
| 1056 | } |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 1057 | |
Wale Ogunwale | 6213caa | 2016-12-02 16:47:15 +0000 | [diff] [blame] | 1058 | // TODO: Users would have their own window containers under the display container? |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 1059 | void switchUser(int userId) { |
Wale Ogunwale | 6213caa | 2016-12-02 16:47:15 +0000 | [diff] [blame] | 1060 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 1061 | mChildren.get(i).switchUser(userId); |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 1062 | } |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 1065 | /** |
| 1066 | * For all windows at or below this container call the callback. |
| 1067 | * @param callback Calls the {@link ToBooleanFunction#apply} method for each window found and |
| 1068 | * stops the search if {@link ToBooleanFunction#apply} returns true. |
| 1069 | * @param traverseTopToBottom If true traverses the hierarchy from top-to-bottom in terms of |
| 1070 | * z-order, else from bottom-to-top. |
| 1071 | * @return True if the search ended before we reached the end of the hierarchy due to |
Wale Ogunwale | 1e129a4 | 2016-11-21 13:03:47 -0800 | [diff] [blame] | 1072 | * {@link ToBooleanFunction#apply} returning true. |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 1073 | */ |
| 1074 | boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) { |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 1075 | if (traverseTopToBottom) { |
| 1076 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 1077 | if (mChildren.get(i).forAllWindows(callback, traverseTopToBottom)) { |
| 1078 | return true; |
| 1079 | } |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } else { |
| 1082 | final int count = mChildren.size(); |
| 1083 | for (int i = 0; i < count; i++) { |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 1084 | if (mChildren.get(i).forAllWindows(callback, traverseTopToBottom)) { |
| 1085 | return true; |
| 1086 | } |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 1089 | return false; |
| 1090 | } |
| 1091 | |
| 1092 | void forAllWindows(Consumer<WindowState> callback, boolean traverseTopToBottom) { |
Wale Ogunwale | ee41d4a | 2016-11-21 08:41:10 -0800 | [diff] [blame] | 1093 | ForAllWindowsConsumerWrapper wrapper = obtainConsumerWrapper(callback); |
| 1094 | forAllWindows(wrapper, traverseTopToBottom); |
| 1095 | wrapper.release(); |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 1098 | boolean forAllActivities(Function<ActivityRecord, Boolean> callback) { |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 1099 | return forAllActivities(callback, true /*traverseTopToBottom*/); |
| 1100 | } |
| 1101 | |
| 1102 | boolean forAllActivities( |
| 1103 | Function<ActivityRecord, Boolean> callback, boolean traverseTopToBottom) { |
| 1104 | if (traverseTopToBottom) { |
| 1105 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1106 | if (mChildren.get(i).forAllActivities(callback, traverseTopToBottom)) return true; |
| 1107 | } |
| 1108 | } else { |
| 1109 | final int count = mChildren.size(); |
| 1110 | for (int i = 0; i < count; i++) { |
| 1111 | if (mChildren.get(i).forAllActivities(callback, traverseTopToBottom)) return true; |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 1112 | } |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 1113 | } |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 1114 | |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 1115 | return false; |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 1118 | void forAllActivities(Consumer<ActivityRecord> callback) { |
| 1119 | forAllActivities(callback, true /*traverseTopToBottom*/); |
| 1120 | } |
| 1121 | |
| 1122 | void forAllActivities(Consumer<ActivityRecord> callback, boolean traverseTopToBottom) { |
| 1123 | if (traverseTopToBottom) { |
| 1124 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1125 | mChildren.get(i).forAllActivities(callback, traverseTopToBottom); |
| 1126 | } |
| 1127 | } else { |
| 1128 | final int count = mChildren.size(); |
| 1129 | for (int i = 0; i < count; i++) { |
| 1130 | mChildren.get(i).forAllActivities(callback, traverseTopToBottom); |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 1135 | /** |
| 1136 | * Process all activities in this branch of the tree. |
| 1137 | * |
| 1138 | * @param callback Called for each activity found. |
| 1139 | * @param boundary We don't return activities via {@param callback} until we get to this node in |
| 1140 | * the tree. |
| 1141 | * @param includeBoundary If the boundary from be processed to return activities. |
| 1142 | * @param traverseTopToBottom direction to traverse the tree. |
| 1143 | * @return {@code true} if we ended the search before reaching the end of the tree. |
| 1144 | */ |
| 1145 | final boolean forAllActivities(Function<ActivityRecord, Boolean> callback, |
| 1146 | WindowContainer boundary, boolean includeBoundary, boolean traverseTopToBottom) { |
| 1147 | return forAllActivities( |
| 1148 | callback, boundary, includeBoundary, traverseTopToBottom, new boolean[1]); |
| 1149 | } |
| 1150 | |
| 1151 | private boolean forAllActivities(Function<ActivityRecord, Boolean> callback, |
| 1152 | WindowContainer boundary, boolean includeBoundary, boolean traverseTopToBottom, |
| 1153 | boolean[] boundaryFound) { |
| 1154 | if (traverseTopToBottom) { |
| 1155 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1156 | if (processForAllActivitiesWithBoundary(callback, boundary, includeBoundary, |
| 1157 | traverseTopToBottom, boundaryFound, mChildren.get(i))) { |
| 1158 | return true; |
| 1159 | } |
| 1160 | } |
| 1161 | } else { |
| 1162 | final int count = mChildren.size(); |
| 1163 | for (int i = 0; i < count; i++) { |
| 1164 | if (processForAllActivitiesWithBoundary(callback, boundary, includeBoundary, |
| 1165 | traverseTopToBottom, boundaryFound, mChildren.get(i))) { |
| 1166 | return true; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | private boolean processForAllActivitiesWithBoundary(Function<ActivityRecord, Boolean> callback, |
| 1175 | WindowContainer boundary, boolean includeBoundary, boolean traverseTopToBottom, |
| 1176 | boolean[] boundaryFound, WindowContainer wc) { |
| 1177 | if (wc == boundary) { |
| 1178 | boundaryFound[0] = true; |
| 1179 | if (!includeBoundary) return false; |
| 1180 | } |
| 1181 | |
| 1182 | if (boundaryFound[0]) { |
| 1183 | return wc.forAllActivities(callback, traverseTopToBottom); |
| 1184 | } |
| 1185 | |
| 1186 | return wc.forAllActivities( |
| 1187 | callback, boundary, includeBoundary, traverseTopToBottom, boundaryFound); |
| 1188 | } |
| 1189 | |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 1190 | /** @return {@code true} if this node or any of its children contains an activity. */ |
| 1191 | boolean hasActivity() { |
| 1192 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1193 | if (mChildren.get(i).hasActivity()) { |
| 1194 | return true; |
| 1195 | } |
| 1196 | } |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
| 1200 | ActivityRecord getActivity(Predicate<ActivityRecord> callback) { |
| 1201 | return getActivity(callback, true /*traverseTopToBottom*/); |
| 1202 | } |
| 1203 | |
| 1204 | ActivityRecord getActivity(Predicate<ActivityRecord> callback, boolean traverseTopToBottom) { |
| 1205 | if (traverseTopToBottom) { |
| 1206 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1207 | final ActivityRecord r = mChildren.get(i).getActivity(callback, traverseTopToBottom); |
| 1208 | if (r != null) { |
| 1209 | return r; |
| 1210 | } |
| 1211 | } |
| 1212 | } else { |
| 1213 | final int count = mChildren.size(); |
| 1214 | for (int i = 0; i < count; i++) { |
| 1215 | final ActivityRecord r = mChildren.get(i).getActivity(callback, traverseTopToBottom); |
| 1216 | if (r != null) { |
| 1217 | return r; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | return null; |
| 1223 | } |
| 1224 | |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 1225 | /** |
| 1226 | * Gets an activity in a branch of the tree. |
| 1227 | * |
| 1228 | * @param callback called to test if this is the activity that should be returned. |
| 1229 | * @param boundary We don't return activities via {@param callback} until we get to this node in |
| 1230 | * the tree. |
| 1231 | * @param includeBoundary If the boundary from be processed to return activities. |
| 1232 | * @param traverseTopToBottom direction to traverse the tree. |
| 1233 | * @return The activity if found or null. |
| 1234 | */ |
| 1235 | final ActivityRecord getActivity(Predicate<ActivityRecord> callback, |
| 1236 | WindowContainer boundary, boolean includeBoundary, boolean traverseTopToBottom) { |
| 1237 | return getActivity( |
| 1238 | callback, boundary, includeBoundary, traverseTopToBottom, new boolean[1]); |
| 1239 | } |
| 1240 | |
| 1241 | private ActivityRecord getActivity(Predicate<ActivityRecord> callback, |
| 1242 | WindowContainer boundary, boolean includeBoundary, boolean traverseTopToBottom, |
| 1243 | boolean[] boundaryFound) { |
| 1244 | if (traverseTopToBottom) { |
| 1245 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1246 | final ActivityRecord r = processGetActivityWithBoundary(callback, boundary, |
| 1247 | includeBoundary, traverseTopToBottom, boundaryFound, mChildren.get(i)); |
| 1248 | if (r != null) { |
| 1249 | return r; |
| 1250 | } |
| 1251 | } |
| 1252 | } else { |
| 1253 | final int count = mChildren.size(); |
| 1254 | for (int i = 0; i < count; i++) { |
| 1255 | final ActivityRecord r = processGetActivityWithBoundary(callback, boundary, |
| 1256 | includeBoundary, traverseTopToBottom, boundaryFound, mChildren.get(i)); |
| 1257 | if (r != null) { |
| 1258 | return r; |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | return null; |
| 1264 | } |
| 1265 | |
| 1266 | private ActivityRecord processGetActivityWithBoundary(Predicate<ActivityRecord> callback, |
| 1267 | WindowContainer boundary, boolean includeBoundary, boolean traverseTopToBottom, |
| 1268 | boolean[] boundaryFound, WindowContainer wc) { |
| 1269 | if (wc == boundary || boundary == null) { |
| 1270 | boundaryFound[0] = true; |
| 1271 | if (!includeBoundary) return null; |
| 1272 | } |
| 1273 | |
| 1274 | if (boundaryFound[0]) { |
| 1275 | return wc.getActivity(callback, traverseTopToBottom); |
| 1276 | } |
| 1277 | |
| 1278 | return wc.getActivity( |
| 1279 | callback, boundary, includeBoundary, traverseTopToBottom, boundaryFound); |
| 1280 | } |
| 1281 | |
| 1282 | ActivityRecord getActivityAbove(ActivityRecord r) { |
| 1283 | return getActivity((above) -> true, r, |
| 1284 | false /*includeBoundary*/, false /*traverseTopToBottom*/); |
| 1285 | } |
| 1286 | |
| 1287 | ActivityRecord getActivityBelow(ActivityRecord r) { |
| 1288 | return getActivity((below) -> true, r, |
| 1289 | false /*includeBoundary*/, true /*traverseTopToBottom*/); |
| 1290 | } |
| 1291 | |
| 1292 | ActivityRecord getBottomMostActivity() { |
| 1293 | return getActivity((r) -> true, false /*traverseTopToBottom*/); |
| 1294 | } |
| 1295 | |
| 1296 | ActivityRecord getTopMostActivity() { |
| 1297 | return getActivity((r) -> true, true /*traverseTopToBottom*/); |
| 1298 | } |
| 1299 | |
Wale Ogunwale | dfbeed7 | 2019-11-20 08:57:39 -0800 | [diff] [blame] | 1300 | ActivityRecord getTopActivity(boolean includeFinishing, boolean includeOverlays) { |
| 1301 | // Break down into 4 calls to avoid object creation due to capturing input params. |
| 1302 | if (includeFinishing) { |
| 1303 | if (includeOverlays) { |
| 1304 | return getActivity((r) -> true); |
| 1305 | } |
| 1306 | return getActivity((r) -> !r.mTaskOverlay); |
| 1307 | } else if (includeOverlays) { |
| 1308 | return getActivity((r) -> !r.finishing); |
| 1309 | } |
| 1310 | |
| 1311 | return getActivity((r) -> !r.finishing && !r.mTaskOverlay); |
| 1312 | } |
| 1313 | |
Winson Chung | d585219 | 2019-09-06 17:20:28 -0700 | [diff] [blame] | 1314 | void forAllWallpaperWindows(Consumer<WallpaperWindowToken> callback) { |
| 1315 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1316 | mChildren.get(i).forAllWallpaperWindows(callback); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1320 | /** |
| 1321 | * For all tasks at or below this container call the callback. |
| 1322 | * |
| 1323 | * @param callback Callback to be called for every task. |
| 1324 | */ |
| 1325 | void forAllTasks(Consumer<Task> callback) { |
| 1326 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1327 | mChildren.get(i).forAllTasks(callback); |
| 1328 | } |
| 1329 | } |
| 1330 | |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 1331 | Task getTask(Predicate<Task> callback) { |
| 1332 | return getTask(callback, true /*traverseTopToBottom*/); |
| 1333 | } |
| 1334 | |
| 1335 | Task getTask(Predicate<Task> callback, boolean traverseTopToBottom) { |
| 1336 | if (traverseTopToBottom) { |
| 1337 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1338 | final Task t = mChildren.get(i).getTask(callback, traverseTopToBottom); |
| 1339 | if (t != null) { |
| 1340 | return t; |
| 1341 | } |
| 1342 | } |
| 1343 | } else { |
| 1344 | final int count = mChildren.size(); |
| 1345 | for (int i = 0; i < count; i++) { |
| 1346 | final Task t = mChildren.get(i).getTask(callback, traverseTopToBottom); |
| 1347 | if (t != null) { |
| 1348 | return t; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | return null; |
| 1354 | } |
| 1355 | |
lumark | bc0032a | 2019-11-01 21:38:13 +0800 | [diff] [blame] | 1356 | /** |
| 1357 | * For all tasks at or below this container call the callback. |
| 1358 | * |
| 1359 | * @param callback Calls the {@link ToBooleanFunction#apply} method for each task found and |
| 1360 | * stops the search if {@link ToBooleanFunction#apply} returns {@code true}. |
| 1361 | */ |
| 1362 | boolean forAllTasks(ToBooleanFunction<Task> callback) { |
| 1363 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1364 | if (mChildren.get(i).forAllTasks(callback)) { |
| 1365 | return true; |
| 1366 | } |
| 1367 | } |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
Wale Ogunwale | d188096 | 2016-11-08 10:31:59 -0800 | [diff] [blame] | 1371 | WindowState getWindow(Predicate<WindowState> callback) { |
| 1372 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 1373 | final WindowState w = mChildren.get(i).getWindow(callback); |
| 1374 | if (w != null) { |
| 1375 | return w; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | return null; |
| 1380 | } |
| 1381 | |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 1382 | /** |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 1383 | * Returns 1, 0, or -1 depending on if this container is greater than, equal to, or lesser than |
| 1384 | * the input container in terms of z-order. |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 1385 | */ |
| 1386 | @Override |
| 1387 | public int compareTo(WindowContainer other) { |
| 1388 | if (this == other) { |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | if (mParent != null && mParent == other.mParent) { |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 1393 | final WindowList<WindowContainer> list = mParent.mChildren; |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 1394 | return list.indexOf(this) > list.indexOf(other) ? 1 : -1; |
| 1395 | } |
| 1396 | |
| 1397 | final LinkedList<WindowContainer> thisParentChain = mTmpChain1; |
| 1398 | final LinkedList<WindowContainer> otherParentChain = mTmpChain2; |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 1399 | try { |
| 1400 | getParents(thisParentChain); |
| 1401 | other.getParents(otherParentChain); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 1402 | |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 1403 | // Find the common ancestor of both containers. |
| 1404 | WindowContainer commonAncestor = null; |
| 1405 | WindowContainer thisTop = thisParentChain.peekLast(); |
| 1406 | WindowContainer otherTop = otherParentChain.peekLast(); |
| 1407 | while (thisTop != null && otherTop != null && thisTop == otherTop) { |
| 1408 | commonAncestor = thisParentChain.removeLast(); |
| 1409 | otherParentChain.removeLast(); |
| 1410 | thisTop = thisParentChain.peekLast(); |
| 1411 | otherTop = otherParentChain.peekLast(); |
| 1412 | } |
| 1413 | |
| 1414 | // Containers don't belong to the same hierarchy??? |
| 1415 | if (commonAncestor == null) { |
| 1416 | throw new IllegalArgumentException("No in the same hierarchy this=" |
| 1417 | + thisParentChain + " other=" + otherParentChain); |
| 1418 | } |
| 1419 | |
| 1420 | // Children are always considered greater than their parents, so if one of the containers |
| 1421 | // we are comparing it the parent of the other then whichever is the child is greater. |
| 1422 | if (commonAncestor == this) { |
| 1423 | return -1; |
| 1424 | } else if (commonAncestor == other) { |
| 1425 | return 1; |
| 1426 | } |
| 1427 | |
| 1428 | // The position of the first non-common ancestor in the common ancestor list determines |
| 1429 | // which is greater the which. |
| 1430 | final WindowList<WindowContainer> list = commonAncestor.mChildren; |
| 1431 | return list.indexOf(thisParentChain.peekLast()) > list.indexOf(otherParentChain.peekLast()) |
| 1432 | ? 1 : -1; |
| 1433 | } finally { |
| 1434 | mTmpChain1.clear(); |
| 1435 | mTmpChain2.clear(); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 1436 | } |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | private void getParents(LinkedList<WindowContainer> parents) { |
| 1440 | parents.clear(); |
| 1441 | WindowContainer current = this; |
| 1442 | do { |
| 1443 | parents.addLast(current); |
| 1444 | current = current.mParent; |
| 1445 | } while (current != null); |
| 1446 | } |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 1447 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1448 | SurfaceControl.Builder makeSurface() { |
| 1449 | final WindowContainer p = getParent(); |
| 1450 | return p.makeChildSurface(this); |
| 1451 | } |
| 1452 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1453 | /** |
| 1454 | * @param child The WindowContainer this child surface is for, or null if the Surface |
| 1455 | * is not assosciated with a WindowContainer (e.g. a surface used for Dimming). |
| 1456 | */ |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1457 | SurfaceControl.Builder makeChildSurface(WindowContainer child) { |
| 1458 | final WindowContainer p = getParent(); |
| 1459 | // Give the parent a chance to set properties. In hierarchy v1 we rely |
| 1460 | // on this to set full-screen dimensions on all our Surface-less Layers. |
Robert Carr | ee4d4b9 | 2017-11-22 12:21:46 -0800 | [diff] [blame] | 1461 | return p.makeChildSurface(child) |
| 1462 | .setParent(mSurfaceControl); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1463 | } |
Robert Carr | dea7bf4 | 2019-04-04 12:02:51 -0700 | [diff] [blame] | 1464 | /* |
| 1465 | * @return The SurfaceControl parent for this containers SurfaceControl. |
| 1466 | * The SurfaceControl must be valid if non-null. |
| 1467 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1468 | @Override |
| 1469 | public SurfaceControl getParentSurfaceControl() { |
| 1470 | final WindowContainer parent = getParent(); |
| 1471 | if (parent == null) { |
| 1472 | return null; |
| 1473 | } |
| 1474 | return parent.getSurfaceControl(); |
| 1475 | } |
| 1476 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1477 | /** |
| 1478 | * @return Whether this WindowContainer should be magnified by the accessibility magnifier. |
| 1479 | */ |
| 1480 | boolean shouldMagnify() { |
Yuki Awano | 4c36b55 | 2018-04-24 17:27:50 +0900 | [diff] [blame] | 1481 | if (mSurfaceControl == null) { |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1485 | for (int i = 0; i < mChildren.size(); i++) { |
| 1486 | if (!mChildren.get(i).shouldMagnify()) { |
| 1487 | return false; |
| 1488 | } |
| 1489 | } |
| 1490 | return true; |
| 1491 | } |
| 1492 | |
| 1493 | SurfaceSession getSession() { |
| 1494 | if (getParent() != null) { |
| 1495 | return getParent().getSession(); |
| 1496 | } |
| 1497 | return null; |
| 1498 | } |
| 1499 | |
| 1500 | void assignLayer(Transaction t, int layer) { |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1501 | final boolean changed = layer != mLastLayer || mLastRelativeToLayer != null; |
| 1502 | if (mSurfaceControl != null && changed) { |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 1503 | setLayer(t, layer); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1504 | mLastLayer = layer; |
| 1505 | mLastRelativeToLayer = null; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | void assignRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) { |
| 1510 | final boolean changed = layer != mLastLayer || mLastRelativeToLayer != relativeTo; |
| 1511 | if (mSurfaceControl != null && changed) { |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 1512 | setRelativeLayer(t, relativeTo, layer); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1513 | mLastLayer = layer; |
| 1514 | mLastRelativeToLayer = relativeTo; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 1518 | protected void setLayer(Transaction t, int layer) { |
| 1519 | |
| 1520 | // Route through surface animator to accommodate that our surface control might be |
| 1521 | // attached to the leash, and leash is attached to parent container. |
| 1522 | mSurfaceAnimator.setLayer(t, layer); |
| 1523 | } |
| 1524 | |
| 1525 | protected void setRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) { |
| 1526 | |
| 1527 | // Route through surface animator to accommodate that our surface control might be |
| 1528 | // attached to the leash, and leash is attached to parent container. |
| 1529 | mSurfaceAnimator.setRelativeLayer(t, relativeTo, layer); |
| 1530 | } |
| 1531 | |
| 1532 | protected void reparentSurfaceControl(Transaction t, SurfaceControl newParent) { |
| 1533 | mSurfaceAnimator.reparent(t, newParent); |
| 1534 | } |
| 1535 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1536 | void assignChildLayers(Transaction t) { |
| 1537 | int layer = 0; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1538 | |
| 1539 | // We use two passes as a way to promote children which |
| 1540 | // need Z-boosting to the end of the list. |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1541 | for (int j = 0; j < mChildren.size(); ++j) { |
| 1542 | final WindowContainer wc = mChildren.get(j); |
| 1543 | wc.assignChildLayers(t); |
| 1544 | if (!wc.needsZBoost()) { |
| 1545 | wc.assignLayer(t, layer++); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1546 | } |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1547 | } |
| 1548 | for (int j = 0; j < mChildren.size(); ++j) { |
| 1549 | final WindowContainer wc = mChildren.get(j); |
| 1550 | if (wc.needsZBoost()) { |
| 1551 | wc.assignLayer(t, layer++); |
| 1552 | } |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | void assignChildLayers() { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1557 | assignChildLayers(getPendingTransaction()); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1558 | scheduleAnimation(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | boolean needsZBoost() { |
| 1562 | for (int i = 0; i < mChildren.size(); i++) { |
| 1563 | if (mChildren.get(i).needsZBoost()) { |
| 1564 | return true; |
| 1565 | } |
| 1566 | } |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 1570 | /** |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1571 | * Write to a protocol buffer output stream. Protocol buffer message definition is at |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 1572 | * {@link com.android.server.wm.WindowContainerProto}. |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1573 | * |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1574 | * @param proto Stream to write the WindowContainer object to. |
| 1575 | * @param fieldId Field Id of the WindowContainer as defined in the parent message. |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 1576 | * @param logLevel Determines the amount of data to be written to the Protobuf. |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1577 | * @hide |
| 1578 | */ |
| 1579 | @CallSuper |
| 1580 | @Override |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame^] | 1581 | public void dumpDebug(ProtoOutputStream proto, long fieldId, |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 1582 | @WindowTraceLogLevel int logLevel) { |
| 1583 | boolean isVisible = isVisible(); |
| 1584 | if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible) { |
| 1585 | return; |
| 1586 | } |
| 1587 | |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1588 | final long token = proto.start(fieldId); |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame^] | 1589 | super.dumpDebug(proto, CONFIGURATION_CONTAINER, logLevel); |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1590 | proto.write(ORIENTATION, mOrientation); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 1591 | proto.write(VISIBLE, isVisible); |
| 1592 | if (mSurfaceAnimator.isAnimating()) { |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame^] | 1593 | mSurfaceAnimator.dumpDebug(proto, SURFACE_ANIMATOR); |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 1594 | } |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1595 | proto.end(token); |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
Wale Ogunwale | ee41d4a | 2016-11-21 08:41:10 -0800 | [diff] [blame] | 1598 | private ForAllWindowsConsumerWrapper obtainConsumerWrapper(Consumer<WindowState> consumer) { |
| 1599 | ForAllWindowsConsumerWrapper wrapper = mConsumerWrapperPool.acquire(); |
| 1600 | if (wrapper == null) { |
| 1601 | wrapper = new ForAllWindowsConsumerWrapper(); |
| 1602 | } |
| 1603 | wrapper.setConsumer(consumer); |
| 1604 | return wrapper; |
| 1605 | } |
| 1606 | |
| 1607 | private final class ForAllWindowsConsumerWrapper implements ToBooleanFunction<WindowState> { |
| 1608 | |
| 1609 | private Consumer<WindowState> mConsumer; |
| 1610 | |
| 1611 | void setConsumer(Consumer<WindowState> consumer) { |
| 1612 | mConsumer = consumer; |
| 1613 | } |
| 1614 | |
| 1615 | @Override |
| 1616 | public boolean apply(WindowState w) { |
| 1617 | mConsumer.accept(w); |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
| 1621 | void release() { |
| 1622 | mConsumer = null; |
| 1623 | mConsumerWrapperPool.release(this); |
| 1624 | } |
| 1625 | } |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1626 | |
| 1627 | // TODO(b/68336570): Should this really be on WindowContainer since it |
| 1628 | // can only be used on the top-level nodes that aren't animated? |
| 1629 | // (otherwise we would be fighting other callers of setMatrix). |
| 1630 | void applyMagnificationSpec(Transaction t, MagnificationSpec spec) { |
| 1631 | if (shouldMagnify()) { |
| 1632 | t.setMatrix(mSurfaceControl, spec.scale, 0, 0, spec.scale) |
| 1633 | .setPosition(mSurfaceControl, spec.offsetX, spec.offsetY); |
| 1634 | } else { |
| 1635 | for (int i = 0; i < mChildren.size(); i++) { |
| 1636 | mChildren.get(i).applyMagnificationSpec(t, spec); |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1641 | void prepareSurfaces() { |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 1642 | // If a leash has been set when the transaction was committed, then the leash reparent has |
| 1643 | // been committed. |
| 1644 | mCommittedReparentToAnimationLeash = mSurfaceAnimator.hasLeash(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1645 | for (int i = 0; i < mChildren.size(); i++) { |
| 1646 | mChildren.get(i).prepareSurfaces(); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /** |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 1651 | * @return true if the reparent to animation leash transaction has been committed, false |
| 1652 | * otherwise. |
| 1653 | */ |
| 1654 | boolean hasCommittedReparentToAnimationLeash() { |
| 1655 | return mCommittedReparentToAnimationLeash; |
| 1656 | } |
| 1657 | |
| 1658 | /** |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 1659 | * Trigger a call to prepareSurfaces from the animation thread, such that pending transactions |
| 1660 | * will be applied. |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1661 | */ |
| 1662 | void scheduleAnimation() { |
| 1663 | if (mParent != null) { |
| 1664 | mParent.scheduleAnimation(); |
| 1665 | } |
| 1666 | } |
| 1667 | |
Robert Carr | dea7bf4 | 2019-04-04 12:02:51 -0700 | [diff] [blame] | 1668 | /** |
| 1669 | * @return The SurfaceControl for this container. |
| 1670 | * The SurfaceControl must be valid if non-null. |
| 1671 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1672 | @Override |
| 1673 | public SurfaceControl getSurfaceControl() { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1674 | return mSurfaceControl; |
| 1675 | } |
| 1676 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1677 | @Override |
| 1678 | public Transaction getPendingTransaction() { |
Tiger Huang | ed6794e | 2019-05-07 20:07:59 +0800 | [diff] [blame] | 1679 | final DisplayContent displayContent = getDisplayContent(); |
| 1680 | if (displayContent != null && displayContent != this) { |
| 1681 | return displayContent.getPendingTransaction(); |
| 1682 | } |
| 1683 | // This WindowContainer has not attached to a display yet or this is a DisplayContent, so we |
| 1684 | // let the caller to save the surface operations within the local mPendingTransaction. |
| 1685 | // If this is not a DisplayContent, we will merge it to the pending transaction of its |
| 1686 | // display once it attaches to it. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1687 | return mPendingTransaction; |
| 1688 | } |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1689 | |
| 1690 | /** |
| 1691 | * Starts an animation on the container. |
| 1692 | * |
| 1693 | * @param anim The animation to run. |
| 1694 | * @param hidden Whether our container is currently hidden. TODO This should use isVisible at |
| 1695 | * some point but the meaning is too weird to work for all containers. |
| 1696 | */ |
| 1697 | void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden) { |
| 1698 | if (DEBUG_ANIM) Slog.v(TAG, "Starting animation on " + this + ": " + anim); |
| 1699 | |
| 1700 | // TODO: This should use isVisible() but because isVisible has a really weird meaning at |
| 1701 | // the moment this doesn't work for all animatable window containers. |
| 1702 | mSurfaceAnimator.startAnimation(t, anim, hidden); |
| 1703 | } |
| 1704 | |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 1705 | void transferAnimation(WindowContainer from) { |
| 1706 | mSurfaceAnimator.transferAnimation(from.mSurfaceAnimator); |
| 1707 | } |
| 1708 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1709 | void cancelAnimation() { |
| 1710 | mSurfaceAnimator.cancelAnimation(); |
| 1711 | } |
| 1712 | |
| 1713 | @Override |
| 1714 | public Builder makeAnimationLeash() { |
| 1715 | return makeSurface(); |
| 1716 | } |
| 1717 | |
Jorim Jaggi | 596a199 | 2017-12-29 14:48:02 +0100 | [diff] [blame] | 1718 | @Override |
| 1719 | public SurfaceControl getAnimationLeashParent() { |
| 1720 | return getParentSurfaceControl(); |
| 1721 | } |
| 1722 | |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 1723 | /** |
| 1724 | * @return The layer on which all app animations are happening. |
| 1725 | */ |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 1726 | SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) { |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 1727 | final WindowContainer parent = getParent(); |
| 1728 | if (parent != null) { |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 1729 | return parent.getAppAnimationLayer(animationLayer); |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 1730 | } |
| 1731 | return null; |
| 1732 | } |
| 1733 | |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 1734 | // TODO: Remove this and use #getBounds() instead once we set an app transition animation |
| 1735 | // on TaskStack. |
| 1736 | Rect getAnimationBounds(int appStackClipMode) { |
| 1737 | return getBounds(); |
| 1738 | } |
| 1739 | |
| 1740 | /** |
| 1741 | * Applies the app transition animation according the given the layout properties in the |
| 1742 | * window hierarchy. |
| 1743 | * |
| 1744 | * @param lp The layout parameters of the window. |
| 1745 | * @param transit The app transition type indicates what kind of transition to be applied. |
| 1746 | * @param enter Whether the app transition is entering transition or not. |
| 1747 | * @param isVoiceInteraction Whether the container is participating in voice interaction or not. |
| 1748 | * |
| 1749 | * @return {@code true} when the container applied the app transition, {@code false} if the |
| 1750 | * app transition is disabled or skipped. |
| 1751 | * |
| 1752 | * @see #getAnimationAdapter |
| 1753 | */ |
| 1754 | boolean applyAnimation(WindowManager.LayoutParams lp, int transit, boolean enter, |
| 1755 | boolean isVoiceInteraction) { |
| 1756 | if (mWmService.mDisableTransitionAnimation) { |
| 1757 | ProtoLog.v(WM_DEBUG_APP_TRANSITIONS_ANIM, |
| 1758 | "applyAnimation: transition animation is disabled or skipped. " |
| 1759 | + "container=%s", this); |
| 1760 | cancelAnimation(); |
| 1761 | return false; |
| 1762 | } |
| 1763 | |
| 1764 | // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason |
| 1765 | // to animate and it can cause strange artifacts when we unfreeze the display if some |
| 1766 | // different animation is running. |
| 1767 | try { |
| 1768 | Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "WC#applyAnimation"); |
| 1769 | if (okToAnimate()) { |
lumark | bc0032a | 2019-11-01 21:38:13 +0800 | [diff] [blame] | 1770 | final Pair<AnimationAdapter, AnimationAdapter> adapters = getAnimationAdapter(lp, |
| 1771 | transit, enter, isVoiceInteraction); |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 1772 | AnimationAdapter adapter = adapters.first; |
| 1773 | AnimationAdapter thumbnailAdapter = adapters.second; |
| 1774 | if (adapter != null) { |
| 1775 | startAnimation(getPendingTransaction(), adapter, !isVisible()); |
| 1776 | if (adapter.getShowWallpaper()) { |
| 1777 | mDisplayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER; |
| 1778 | } |
| 1779 | if (thumbnailAdapter != null) { |
| 1780 | mThumbnail.startAnimation( |
| 1781 | getPendingTransaction(), thumbnailAdapter, !isVisible()); |
| 1782 | } |
| 1783 | } |
| 1784 | } else { |
| 1785 | cancelAnimation(); |
| 1786 | } |
| 1787 | } finally { |
| 1788 | Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER); |
| 1789 | } |
| 1790 | |
| 1791 | return isAnimating(); |
| 1792 | } |
| 1793 | |
| 1794 | /** |
| 1795 | * Gets the {@link AnimationAdapter} according the given window layout properties in the window |
| 1796 | * hierarchy. |
| 1797 | * |
| 1798 | * @return The return value will always contain two elements, one for normal animations and the |
| 1799 | * other for thumbnail animation, both can be {@code null}. |
| 1800 | * |
| 1801 | * @See com.android.server.wm.RemoteAnimationController.RemoteAnimationRecord |
| 1802 | * @See LocalAnimationAdapter |
| 1803 | */ |
| 1804 | Pair<AnimationAdapter, AnimationAdapter> getAnimationAdapter(WindowManager.LayoutParams lp, |
| 1805 | int transit, boolean enter, boolean isVoiceInteraction) { |
| 1806 | final Pair<AnimationAdapter, AnimationAdapter> resultAdapters; |
| 1807 | final int appStackClipMode = getDisplayContent().mAppTransition.getAppStackClipMode(); |
| 1808 | |
| 1809 | // Separate position and size for use in animators. |
| 1810 | mTmpRect.set(getAnimationBounds(appStackClipMode)); |
| 1811 | mTmpPoint.set(mTmpRect.left, mTmpRect.top); |
| 1812 | mTmpRect.offsetTo(0, 0); |
| 1813 | |
| 1814 | final RemoteAnimationController controller = |
| 1815 | getDisplayContent().mAppTransition.getRemoteAnimationController(); |
| 1816 | final boolean isChanging = AppTransition.isChangeTransit(transit) && enter |
| 1817 | && isChangingAppTransition(); |
| 1818 | |
| 1819 | // Delaying animation start isn't compatible with remote animations at all. |
| 1820 | if (controller != null && !mSurfaceAnimator.isAnimationStartDelayed()) { |
| 1821 | final RemoteAnimationController.RemoteAnimationRecord adapters = |
| 1822 | controller.createRemoteAnimationRecord(this, mTmpPoint, mTmpRect, |
| 1823 | (isChanging ? mTransitStartRect : null)); |
| 1824 | resultAdapters = new Pair<>(adapters.mAdapter, adapters.mThumbnailAdapter); |
| 1825 | } else if (isChanging) { |
| 1826 | final float durationScale = mWmService.getTransitionAnimationScaleLocked(); |
| 1827 | final DisplayInfo displayInfo = getDisplayContent().getDisplayInfo(); |
| 1828 | mTmpRect.offsetTo(mTmpPoint.x, mTmpPoint.y); |
| 1829 | |
lumark | bc0032a | 2019-11-01 21:38:13 +0800 | [diff] [blame] | 1830 | final AnimationAdapter adapter = new LocalAnimationAdapter( |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 1831 | new WindowChangeAnimationSpec(mTransitStartRect, mTmpRect, displayInfo, |
| 1832 | durationScale, true /* isAppAnimation */, false /* isThumbnail */), |
| 1833 | getSurfaceAnimationRunner()); |
| 1834 | |
lumark | bc0032a | 2019-11-01 21:38:13 +0800 | [diff] [blame] | 1835 | final AnimationAdapter thumbnailAdapter = mThumbnail != null |
| 1836 | ? new LocalAnimationAdapter(new WindowChangeAnimationSpec(mTransitStartRect, |
| 1837 | mTmpRect, displayInfo, durationScale, true /* isAppAnimation */, |
| 1838 | true /* isThumbnail */), getSurfaceAnimationRunner()) |
| 1839 | : null; |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 1840 | resultAdapters = new Pair<>(adapter, thumbnailAdapter); |
| 1841 | mTransit = transit; |
| 1842 | mTransitFlags = getDisplayContent().mAppTransition.getTransitFlags(); |
| 1843 | } else { |
| 1844 | mNeedsAnimationBoundsLayer = (appStackClipMode == STACK_CLIP_AFTER_ANIM); |
| 1845 | final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction); |
| 1846 | |
| 1847 | if (a != null) { |
| 1848 | // Only apply corner radius to animation if we're not in multi window mode. |
| 1849 | // We don't want rounded corners when in pip or split screen. |
| 1850 | final float windowCornerRadius = !inMultiWindowMode() |
| 1851 | ? getDisplayContent().getWindowCornerRadius() |
| 1852 | : 0; |
| 1853 | AnimationAdapter adapter = new LocalAnimationAdapter( |
| 1854 | new WindowAnimationSpec(a, mTmpPoint, mTmpRect, |
| 1855 | getDisplayContent().mAppTransition.canSkipFirstFrame(), |
| 1856 | appStackClipMode, true /* isAppAnimation */, windowCornerRadius), |
| 1857 | getSurfaceAnimationRunner()); |
| 1858 | |
| 1859 | resultAdapters = new Pair<>(adapter, null); |
| 1860 | mNeedsZBoost = a.getZAdjustment() == Animation.ZORDER_TOP; |
| 1861 | mTransit = transit; |
| 1862 | mTransitFlags = getDisplayContent().mAppTransition.getTransitFlags(); |
| 1863 | } else { |
| 1864 | resultAdapters = new Pair<>(null, null); |
| 1865 | } |
| 1866 | } |
| 1867 | return resultAdapters; |
| 1868 | } |
| 1869 | |
| 1870 | final SurfaceAnimationRunner getSurfaceAnimationRunner() { |
| 1871 | return mWmService.mSurfaceAnimationRunner; |
| 1872 | } |
| 1873 | |
| 1874 | private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter, |
| 1875 | boolean isVoiceInteraction) { |
| 1876 | final DisplayContent displayContent = getDisplayContent(); |
| 1877 | final DisplayInfo displayInfo = displayContent.getDisplayInfo(); |
| 1878 | final int width = displayInfo.appWidth; |
| 1879 | final int height = displayInfo.appHeight; |
| 1880 | ProtoLog.v(WM_DEBUG_APP_TRANSITIONS_ANIM, "applyAnimation: container=%s", this); |
| 1881 | |
| 1882 | // Determine the visible rect to calculate the thumbnail clip with |
| 1883 | // getAnimationFrames. |
| 1884 | final Rect frame = new Rect(0, 0, width, height); |
| 1885 | final Rect displayFrame = new Rect(0, 0, |
| 1886 | displayInfo.logicalWidth, displayInfo.logicalHeight); |
| 1887 | final Rect insets = new Rect(); |
| 1888 | final Rect stableInsets = new Rect(); |
| 1889 | final Rect surfaceInsets = new Rect(); |
| 1890 | getAnimationFrames(frame, insets, stableInsets, surfaceInsets); |
| 1891 | |
| 1892 | if (mLaunchTaskBehind) { |
| 1893 | // Differentiate the two animations. This one which is briefly on the screen |
| 1894 | // gets the !enter animation, and the other one which remains on the |
| 1895 | // screen gets the enter animation. Both appear in the mOpeningApps set. |
| 1896 | enter = false; |
| 1897 | } |
| 1898 | ProtoLog.d(WM_DEBUG_APP_TRANSITIONS, |
| 1899 | "Loading animation for app transition. transit=%s enter=%b frame=%s insets=%s " |
| 1900 | + "surfaceInsets=%s", |
| 1901 | AppTransition.appTransitionToString(transit), enter, frame, insets, surfaceInsets); |
| 1902 | final Configuration displayConfig = displayContent.getConfiguration(); |
| 1903 | final Animation a = getDisplayContent().mAppTransition.loadAnimation(lp, transit, enter, |
| 1904 | displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets, |
| 1905 | surfaceInsets, stableInsets, isVoiceInteraction, inFreeformWindowingMode(), this); |
| 1906 | if (a != null) { |
| 1907 | if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this); |
| 1908 | final int containingWidth = frame.width(); |
| 1909 | final int containingHeight = frame.height(); |
| 1910 | a.initialize(containingWidth, containingHeight, width, height); |
| 1911 | a.scaleCurrentDuration(mWmService.getTransitionAnimationScaleLocked()); |
| 1912 | } |
| 1913 | return a; |
| 1914 | } |
| 1915 | |
| 1916 | RemoteAnimationTarget createRemoteAnimationTarget( |
| 1917 | RemoteAnimationController.RemoteAnimationRecord record) { |
| 1918 | return null; |
| 1919 | } |
| 1920 | |
| 1921 | boolean okToDisplay() { |
| 1922 | return mDisplayContent != null && mDisplayContent.okToDisplay(); |
| 1923 | } |
| 1924 | |
| 1925 | boolean okToAnimate() { |
| 1926 | return mDisplayContent != null && mDisplayContent.okToAnimate(); |
| 1927 | } |
| 1928 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1929 | @Override |
| 1930 | public void commitPendingTransaction() { |
| 1931 | scheduleAnimation(); |
| 1932 | } |
| 1933 | |
Robert Carr | 2f8aa39 | 2018-01-31 14:46:51 -0800 | [diff] [blame] | 1934 | void reassignLayer(Transaction t) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1935 | final WindowContainer parent = getParent(); |
| 1936 | if (parent != null) { |
| 1937 | parent.assignChildLayers(t); |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | @Override |
| 1942 | public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) { |
Robert Carr | f12f9d3 | 2018-03-01 15:24:36 -0800 | [diff] [blame] | 1943 | mLastLayer = -1; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1944 | reassignLayer(t); |
| 1945 | } |
| 1946 | |
| 1947 | @Override |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 1948 | public void onAnimationLeashLost(Transaction t) { |
Robert Carr | f12f9d3 | 2018-03-01 15:24:36 -0800 | [diff] [blame] | 1949 | mLastLayer = -1; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1950 | reassignLayer(t); |
| 1951 | } |
| 1952 | |
| 1953 | /** |
| 1954 | * Called when an animation has finished running. |
| 1955 | */ |
| 1956 | protected void onAnimationFinished() { |
chaviw | ccd5d50 | 2019-02-22 13:30:31 -0800 | [diff] [blame] | 1957 | mWmService.onAnimationFinished(); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * @return The currently running animation, if any, or {@code null} otherwise. |
| 1962 | */ |
| 1963 | AnimationAdapter getAnimation() { |
| 1964 | return mSurfaceAnimator.getAnimation(); |
| 1965 | } |
| 1966 | |
| 1967 | /** |
| 1968 | * @see SurfaceAnimator#startDelayingAnimationStart |
| 1969 | */ |
| 1970 | void startDelayingAnimationStart() { |
| 1971 | mSurfaceAnimator.startDelayingAnimationStart(); |
| 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * @see SurfaceAnimator#endDelayingAnimationStart |
| 1976 | */ |
| 1977 | void endDelayingAnimationStart() { |
| 1978 | mSurfaceAnimator.endDelayingAnimationStart(); |
| 1979 | } |
| 1980 | |
| 1981 | @Override |
| 1982 | public int getSurfaceWidth() { |
| 1983 | return mSurfaceControl.getWidth(); |
| 1984 | } |
| 1985 | |
| 1986 | @Override |
| 1987 | public int getSurfaceHeight() { |
| 1988 | return mSurfaceControl.getHeight(); |
| 1989 | } |
| 1990 | |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 1991 | @CallSuper |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1992 | void dump(PrintWriter pw, String prefix, boolean dumpAll) { |
| 1993 | if (mSurfaceAnimator.isAnimating()) { |
| 1994 | pw.print(prefix); pw.println("ContainerAnimator:"); |
| 1995 | mSurfaceAnimator.dump(pw, prefix + " "); |
| 1996 | } |
| 1997 | } |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 1998 | |
chaviw | 2f0567b | 2018-01-29 16:22:02 -0800 | [diff] [blame] | 1999 | void updateSurfacePosition() { |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 2000 | if (mSurfaceControl == null) { |
| 2001 | return; |
| 2002 | } |
| 2003 | |
Evan Rosky | ed6767f | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 2004 | getRelativeDisplayedPosition(mTmpPos); |
chaviw | 3e751af | 2018-01-11 11:22:39 -0800 | [diff] [blame] | 2005 | if (mTmpPos.equals(mLastSurfacePosition)) { |
| 2006 | return; |
| 2007 | } |
| 2008 | |
chaviw | 2f0567b | 2018-01-29 16:22:02 -0800 | [diff] [blame] | 2009 | getPendingTransaction().setPosition(mSurfaceControl, mTmpPos.x, mTmpPos.y); |
chaviw | 3e751af | 2018-01-11 11:22:39 -0800 | [diff] [blame] | 2010 | mLastSurfacePosition.set(mTmpPos.x, mTmpPos.y); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
Evan Rosky | 65dffa6 | 2019-02-04 14:09:53 -0800 | [diff] [blame] | 2013 | @VisibleForTesting |
Evan Rosky | 89f5c1d | 2019-01-29 10:04:05 -0800 | [diff] [blame] | 2014 | Point getLastSurfacePosition() { |
| 2015 | return mLastSurfacePosition; |
| 2016 | } |
| 2017 | |
Evan Rosky | ed6767f | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 2018 | /** |
| 2019 | * Displayed bounds specify where to display this container at. It differs from bounds during |
| 2020 | * certain operations (like animation or interactive dragging). |
| 2021 | * |
| 2022 | * @return the bounds to display this container at. |
| 2023 | */ |
| 2024 | Rect getDisplayedBounds() { |
| 2025 | return getBounds(); |
| 2026 | } |
| 2027 | |
lumark | 19a5d2e | 2019-10-11 16:19:30 +0800 | [diff] [blame] | 2028 | /** |
| 2029 | * The {@code outFrame} retrieved by this method specifies where the animation will finish |
| 2030 | * the entrance animation, as the next frame will display the window at these coordinates. In |
| 2031 | * case of exit animation, this is where the animation will start, as the frame before the |
| 2032 | * animation is displaying the window at these bounds. |
| 2033 | * |
| 2034 | * @param outFrame The bounds where entrance animation finishes or exit animation starts. |
| 2035 | * @param outInsets Insets that are covered by system windows. |
| 2036 | * @param outStableInsets Insets that determine the area covered by the stable system windows. |
| 2037 | * @param outSurfaceInsets Positive insets between the drawing surface and window content. |
| 2038 | */ |
| 2039 | void getAnimationFrames(Rect outFrame, Rect outInsets, Rect outStableInsets, |
| 2040 | Rect outSurfaceInsets) { |
| 2041 | final DisplayInfo displayInfo = getDisplayContent().getDisplayInfo(); |
| 2042 | outFrame.set(0, 0, displayInfo.appWidth, displayInfo.appHeight); |
| 2043 | outInsets.setEmpty(); |
| 2044 | outStableInsets.setEmpty(); |
| 2045 | outSurfaceInsets.setEmpty(); |
| 2046 | } |
| 2047 | |
Evan Rosky | ed6767f | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 2048 | void getRelativeDisplayedPosition(Point outPos) { |
| 2049 | final Rect dispBounds = getDisplayedBounds(); |
| 2050 | outPos.set(dispBounds.left, dispBounds.top); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 2051 | final WindowContainer parent = getParent(); |
| 2052 | if (parent != null) { |
Evan Rosky | ed6767f | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 2053 | final Rect parentBounds = parent.getDisplayedBounds(); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 2054 | outPos.offset(-parentBounds.left, -parentBounds.top); |
| 2055 | } |
| 2056 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 2057 | |
Yunfan Chen | 87b5a24 | 2019-10-01 17:53:59 +0900 | [diff] [blame] | 2058 | void waitForAllWindowsDrawn() { |
| 2059 | final WindowManagerPolicy policy = mWmService.mPolicy; |
| 2060 | forAllWindows(w -> { |
| 2061 | final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 2062 | if (w.isVisibleLw() && (w.mActivityRecord != null || keyguard)) { |
Yunfan Chen | 87b5a24 | 2019-10-01 17:53:59 +0900 | [diff] [blame] | 2063 | w.mWinAnimator.mDrawState = DRAW_PENDING; |
| 2064 | // Force add to mResizingWindows. |
| 2065 | w.resetLastContentInsets(); |
| 2066 | mWaitingForDrawn.add(w); |
| 2067 | } |
| 2068 | }, true /* traverseTopToBottom */); |
| 2069 | } |
| 2070 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 2071 | Dimmer getDimmer() { |
| 2072 | if (mParent == null) { |
| 2073 | return null; |
| 2074 | } |
| 2075 | return mParent.getDimmer(); |
| 2076 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 2077 | } |