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; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 22 | import static android.view.SurfaceControl.Transaction; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 23 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 25 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 26 | import static com.android.server.wm.WindowContainerProto.CONFIGURATION_CONTAINER; |
| 27 | import static com.android.server.wm.WindowContainerProto.ORIENTATION; |
| 28 | import static com.android.server.wm.WindowContainerProto.SURFACE_ANIMATOR; |
| 29 | import static com.android.server.wm.WindowContainerProto.VISIBLE; |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 30 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 31 | import android.annotation.CallSuper; |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 32 | import android.annotation.IntDef; |
| 33 | import android.app.WindowConfiguration; |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 34 | import android.content.res.Configuration; |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 35 | import android.graphics.Point; |
| 36 | import android.graphics.Rect; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 37 | import android.util.Pools; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 38 | import android.util.Slog; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 39 | import android.util.proto.ProtoOutputStream; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 40 | import android.view.MagnificationSpec; |
| 41 | import android.view.SurfaceControl; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 42 | import android.view.SurfaceControl.Builder; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 43 | import android.view.SurfaceSession; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 44 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 45 | import com.android.internal.util.ToBooleanFunction; |
| 46 | import com.android.server.wm.SurfaceAnimator.Animatable; |
| 47 | |
| 48 | import java.io.PrintWriter; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 49 | import java.util.Comparator; |
| 50 | import java.util.LinkedList; |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 51 | import java.util.function.Consumer; |
Wale Ogunwale | d188096 | 2016-11-08 10:31:59 -0800 | [diff] [blame] | 52 | import java.util.function.Predicate; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * 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] | 56 | * children in a hierarchy form. |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 57 | * The test class is {@link WindowContainerTests} which must be kept up-to-date and ran anytime |
| 58 | * changes are made to this class. |
| 59 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 60 | class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<E> |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 61 | implements Comparable<WindowContainer>, Animatable { |
| 62 | |
| 63 | private static final String TAG = TAG_WITH_CLASS_NAME ? "WindowContainer" : TAG_WM; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 64 | |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 65 | /** Animation layer that happens above all animating {@link TaskStack}s. */ |
| 66 | static final int ANIMATION_LAYER_STANDARD = 0; |
| 67 | |
| 68 | /** Animation layer that happens above all {@link TaskStack}s. */ |
| 69 | static final int ANIMATION_LAYER_BOOSTED = 1; |
| 70 | |
| 71 | /** |
| 72 | * Animation layer that is reserved for {@link WindowConfiguration#ACTIVITY_TYPE_HOME} |
| 73 | * activities that happens below all {@link TaskStack}s. |
| 74 | */ |
| 75 | static final int ANIMATION_LAYER_HOME = 2; |
| 76 | |
| 77 | @IntDef(prefix = { "ANIMATION_LAYER_" }, value = { |
| 78 | ANIMATION_LAYER_STANDARD, |
| 79 | ANIMATION_LAYER_BOOSTED, |
| 80 | ANIMATION_LAYER_HOME, |
| 81 | }) |
| 82 | @interface AnimationLayer {} |
| 83 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 84 | static final int POSITION_TOP = Integer.MAX_VALUE; |
| 85 | static final int POSITION_BOTTOM = Integer.MIN_VALUE; |
| 86 | |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 87 | /** |
| 88 | * The parent of this window container. |
| 89 | * For removing or setting new parent {@link #setParent} should be used, because it also |
| 90 | * performs configuration updates based on new parent's settings. |
| 91 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 92 | private WindowContainer<WindowContainer> mParent = null; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 93 | |
| 94 | // List of children for this window container. List is in z-order as the children appear on |
| 95 | // 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] | 96 | protected final WindowList<E> mChildren = new WindowList<E>(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 97 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 98 | // The specified orientation for this window container. |
| 99 | protected int mOrientation = SCREEN_ORIENTATION_UNSPECIFIED; |
| 100 | |
Wale Ogunwale | ee41d4a | 2016-11-21 08:41:10 -0800 | [diff] [blame] | 101 | private final Pools.SynchronizedPool<ForAllWindowsConsumerWrapper> mConsumerWrapperPool = |
| 102 | new Pools.SynchronizedPool<>(3); |
| 103 | |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 104 | // The owner/creator for this container. No controller if null. |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 105 | WindowContainerController mController; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 106 | |
| 107 | protected SurfaceControl mSurfaceControl; |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 108 | private int mLastLayer = 0; |
| 109 | private SurfaceControl mLastRelativeToLayer = null; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Applied as part of the animation pass in "prepareSurfaces". |
| 113 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 114 | protected final Transaction mPendingTransaction; |
| 115 | protected final SurfaceAnimator mSurfaceAnimator; |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 116 | protected final WindowManagerService mService; |
| 117 | |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 118 | private final Point mTmpPos = new Point(); |
chaviw | 3e751af | 2018-01-11 11:22:39 -0800 | [diff] [blame] | 119 | protected final Point mLastSurfacePosition = new Point(); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 120 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 121 | /** Total number of elements in this subtree, including our own hierarchy element. */ |
| 122 | private int mTreeWeight = 1; |
| 123 | |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 124 | /** |
| 125 | * Indicates whether we are animating and have committed the transaction to reparent our |
| 126 | * surface to the animation leash |
| 127 | */ |
| 128 | private boolean mCommittedReparentToAnimationLeash; |
| 129 | |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 130 | WindowContainer(WindowManagerService service) { |
| 131 | mService = service; |
| 132 | mPendingTransaction = service.mTransactionFactory.make(); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 133 | mSurfaceAnimator = new SurfaceAnimator(this, this::onAnimationFinished, service); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 134 | } |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 135 | |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 136 | @Override |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 137 | final protected WindowContainer getParent() { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 138 | return mParent; |
| 139 | } |
| 140 | |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 141 | @Override |
Wale Ogunwale | 6191149 | 2017-10-11 08:50:50 -0700 | [diff] [blame] | 142 | protected int getChildCount() { |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 143 | return mChildren.size(); |
| 144 | } |
| 145 | |
| 146 | @Override |
Wale Ogunwale | 6191149 | 2017-10-11 08:50:50 -0700 | [diff] [blame] | 147 | protected E getChildAt(int index) { |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 148 | return mChildren.get(index); |
| 149 | } |
| 150 | |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 151 | @Override |
| 152 | public void onConfigurationChanged(Configuration newParentConfig) { |
| 153 | super.onConfigurationChanged(newParentConfig); |
chaviw | 2f0567b | 2018-01-29 16:22:02 -0800 | [diff] [blame] | 154 | updateSurfacePosition(); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 155 | scheduleAnimation(); |
| 156 | } |
| 157 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 158 | final protected void setParent(WindowContainer<WindowContainer> parent) { |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 159 | mParent = parent; |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 160 | // Removing parent usually means that we've detached this entity to destroy it or to attach |
| 161 | // to another parent. In both cases we don't need to update the configuration now. |
| 162 | if (mParent != null) { |
| 163 | // Update full configuration of this container and all its children. |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 164 | onConfigurationChanged(mParent.getConfiguration()); |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 165 | // Update merged override configuration of this container and all its children. |
| 166 | onMergedOverrideConfigurationChanged(); |
| 167 | } |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 168 | |
| 169 | onParentSet(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Callback that is triggered when @link WindowContainer#setParent(WindowContainer)} was called. |
| 174 | * Supposed to be overridden and contain actions that should be executed after parent was set. |
| 175 | */ |
| 176 | void onParentSet() { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 177 | if (mParent == null) { |
| 178 | return; |
| 179 | } |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 180 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 181 | if (mSurfaceControl == null) { |
| 182 | // If we don't yet have a surface, but we now have a parent, we should |
| 183 | // build a surface. |
| 184 | mSurfaceControl = makeSurface().build(); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 185 | getPendingTransaction().show(mSurfaceControl); |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 186 | updateSurfacePosition(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 187 | } else { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 188 | // If we have a surface but a new parent, we just need to perform a reparent. Go through |
| 189 | // surface animator such that hierarchy is preserved when animating, i.e. |
| 190 | // mSurfaceControl stays attached to the leash and we just reparent the leash to the |
| 191 | // new parent. |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 192 | reparentSurfaceControl(getPendingTransaction(), mParent.mSurfaceControl); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Either way we need to ask the parent to assign us a Z-order. |
| 196 | mParent.assignChildLayers(); |
| 197 | scheduleAnimation(); |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 200 | // Temp. holders for a chain of containers we are currently processing. |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 201 | private final LinkedList<WindowContainer> mTmpChain1 = new LinkedList<>(); |
| 202 | private final LinkedList<WindowContainer> mTmpChain2 = new LinkedList<>(); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 203 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Adds the input window container has a child of this container in order based on the input |
| 206 | * comparator. |
| 207 | * @param child The window container to add as a child of this window container. |
| 208 | * @param comparator Comparator to use in determining the position the child should be added to. |
| 209 | * If null, the child will be added to the top. |
| 210 | */ |
| 211 | @CallSuper |
Wale Ogunwale | d90546a | 2016-09-09 23:28:03 -0700 | [diff] [blame] | 212 | protected void addChild(E child, Comparator<E> comparator) { |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 213 | if (child.getParent() != null) { |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 214 | throw new IllegalArgumentException("addChild: container=" + child.getName() |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 215 | + " is already a child of container=" + child.getParent().getName() |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 216 | + " can't add to container=" + getName()); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 217 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 218 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 219 | int positionToAdd = -1; |
| 220 | if (comparator != null) { |
| 221 | final int count = mChildren.size(); |
| 222 | for (int i = 0; i < count; i++) { |
| 223 | if (comparator.compare(child, mChildren.get(i)) < 0) { |
| 224 | positionToAdd = i; |
| 225 | break; |
| 226 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 230 | if (positionToAdd == -1) { |
| 231 | mChildren.add(child); |
| 232 | } else { |
| 233 | mChildren.add(positionToAdd, child); |
| 234 | } |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 235 | onChildAdded(child); |
| 236 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 237 | // Set the parent after we've actually added a child in case a subclass depends on this. |
| 238 | child.setParent(this); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 241 | /** Adds the input window container has a child of this container at the input index. */ |
| 242 | @CallSuper |
Wale Ogunwale | 72919d2 | 2016-12-08 18:58:50 -0800 | [diff] [blame] | 243 | void addChild(E child, int index) { |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 244 | if (child.getParent() != null) { |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 245 | throw new IllegalArgumentException("addChild: container=" + child.getName() |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 246 | + " is already a child of container=" + child.getParent().getName() |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 247 | + " can't add to container=" + getName()); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 248 | } |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 249 | mChildren.add(index, child); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 250 | onChildAdded(child); |
| 251 | |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 252 | // Set the parent after we've actually added a child in case a subclass depends on this. |
| 253 | child.setParent(this); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 256 | private void onChildAdded(WindowContainer child) { |
| 257 | mTreeWeight += child.mTreeWeight; |
| 258 | WindowContainer parent = getParent(); |
| 259 | while (parent != null) { |
| 260 | parent.mTreeWeight += child.mTreeWeight; |
| 261 | parent = parent.getParent(); |
| 262 | } |
| 263 | } |
| 264 | |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Removes the input child container from this container which is its parent. |
| 267 | * |
| 268 | * @return True if the container did contain the input child and it was detached. |
| 269 | */ |
| 270 | @CallSuper |
| 271 | void removeChild(E child) { |
| 272 | if (mChildren.remove(child)) { |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 273 | onChildRemoved(child); |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 274 | child.setParent(null); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 275 | } else { |
Wale Ogunwale | ba51ca2 | 2016-09-23 06:06:54 -0700 | [diff] [blame] | 276 | throw new IllegalArgumentException("removeChild: container=" + child.getName() |
| 277 | + " is not a child of container=" + getName()); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 281 | private void onChildRemoved(WindowContainer child) { |
| 282 | mTreeWeight -= child.mTreeWeight; |
| 283 | WindowContainer parent = getParent(); |
| 284 | while (parent != null) { |
| 285 | parent.mTreeWeight -= child.mTreeWeight; |
| 286 | parent = parent.getParent(); |
| 287 | } |
| 288 | } |
| 289 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Removes this window container and its children with no regard for what else might be going on |
| 292 | * in the system. For example, the container will be removed during animation if this method is |
| 293 | * called which isn't desirable. For most cases you want to call {@link #removeIfPossible()} |
| 294 | * which allows the system to defer removal until a suitable time. |
| 295 | */ |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 296 | @CallSuper |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 297 | void removeImmediately() { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 298 | while (!mChildren.isEmpty()) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 299 | final E child = mChildren.peekLast(); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 300 | child.removeImmediately(); |
| 301 | // Need to do this after calling remove on the child because the child might try to |
| 302 | // remove/detach itself from its parent which will cause an exception if we remove |
| 303 | // it before calling remove on the child. |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 304 | if (mChildren.remove(child)) { |
| 305 | onChildRemoved(child); |
| 306 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 309 | if (mSurfaceControl != null) { |
chaviw | 6728e2f | 2018-03-19 15:58:04 -0700 | [diff] [blame] | 310 | mPendingTransaction.destroy(mSurfaceControl); |
| 311 | |
| 312 | // Merge to parent transaction to ensure the transactions on this WindowContainer are |
| 313 | // applied in native even if WindowContainer is removed. |
| 314 | if (mParent != null) { |
| 315 | mParent.getPendingTransaction().merge(mPendingTransaction); |
| 316 | } |
| 317 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 318 | mSurfaceControl = null; |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 319 | scheduleAnimation(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 322 | if (mParent != null) { |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 323 | mParent.removeChild(this); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 324 | } |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 325 | |
| 326 | if (mController != null) { |
| 327 | setController(null); |
| 328 | } |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 329 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 333 | * @return The index of this element in the hierarchy tree in prefix order. |
| 334 | */ |
| 335 | int getPrefixOrderIndex() { |
| 336 | if (mParent == null) { |
| 337 | return 0; |
| 338 | } |
| 339 | return mParent.getPrefixOrderIndex(this); |
| 340 | } |
| 341 | |
| 342 | private int getPrefixOrderIndex(WindowContainer child) { |
| 343 | int order = 0; |
| 344 | for (int i = 0; i < mChildren.size(); i++) { |
| 345 | final WindowContainer childI = mChildren.get(i); |
| 346 | if (child == childI) { |
| 347 | break; |
| 348 | } |
| 349 | order += childI.mTreeWeight; |
| 350 | } |
| 351 | if (mParent != null) { |
| 352 | order += mParent.getPrefixOrderIndex(this); |
| 353 | } |
| 354 | |
| 355 | // We also need to count ourselves. |
| 356 | order++; |
| 357 | return order; |
| 358 | } |
| 359 | |
| 360 | /** |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 361 | * Removes this window container and its children taking care not to remove them during a |
| 362 | * critical stage in the system. For example, some containers will not be removed during |
| 363 | * animation if this method is called. |
| 364 | */ |
| 365 | // TODO: figure-out implementation that works best for this. |
| 366 | // E.g. when do we remove from parent list? maybe not... |
| 367 | void removeIfPossible() { |
| 368 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 369 | final WindowContainer wc = mChildren.get(i); |
| 370 | wc.removeIfPossible(); |
| 371 | } |
| 372 | } |
| 373 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 374 | /** Returns true if this window container has the input child. */ |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 375 | boolean hasChild(E child) { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 376 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 377 | final E current = mChildren.get(i); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 378 | if (current == child || current.hasChild(child)) { |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | return false; |
| 383 | } |
Wale Ogunwale | 9bc4773 | 2016-08-10 14:44:22 -0700 | [diff] [blame] | 384 | |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 385 | /** |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 386 | * Move a child from it's current place in siblings list to the specified position, |
| 387 | * with an option to move all its parents to top. |
| 388 | * @param position Target position to move the child to. |
| 389 | * @param child Child to move to selected position. |
| 390 | * @param includingParents Flag indicating whether we need to move the entire branch of the |
| 391 | * hierarchy when we're moving a child to {@link #POSITION_TOP} or |
| 392 | * {@link #POSITION_BOTTOM}. When moving to other intermediate positions |
| 393 | * this flag will do nothing. |
| 394 | */ |
| 395 | @CallSuper |
| 396 | void positionChildAt(int position, E child, boolean includingParents) { |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 397 | |
| 398 | if (child.getParent() != this) { |
| 399 | throw new IllegalArgumentException("removeChild: container=" + child.getName() |
| 400 | + " is not a child of container=" + getName() |
| 401 | + " current parent=" + child.getParent()); |
| 402 | } |
| 403 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 404 | if ((position < 0 && position != POSITION_BOTTOM) |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 405 | || (position > mChildren.size() && position != POSITION_TOP)) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 406 | throw new IllegalArgumentException("positionAt: invalid position=" + position |
| 407 | + ", children number=" + mChildren.size()); |
| 408 | } |
| 409 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 410 | if (position >= mChildren.size() - 1) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 411 | position = POSITION_TOP; |
| 412 | } else if (position == 0) { |
| 413 | position = POSITION_BOTTOM; |
| 414 | } |
| 415 | |
| 416 | switch (position) { |
| 417 | case POSITION_TOP: |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 418 | if (mChildren.peekLast() != child) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 419 | mChildren.remove(child); |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 420 | mChildren.add(child); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 421 | } |
| 422 | if (includingParents && getParent() != null) { |
| 423 | getParent().positionChildAt(POSITION_TOP, this /* child */, |
| 424 | true /* includingParents */); |
| 425 | } |
| 426 | break; |
| 427 | case POSITION_BOTTOM: |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 428 | if (mChildren.peekFirst() != child) { |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 429 | mChildren.remove(child); |
| 430 | mChildren.addFirst(child); |
| 431 | } |
| 432 | if (includingParents && getParent() != null) { |
| 433 | getParent().positionChildAt(POSITION_BOTTOM, this /* child */, |
| 434 | true /* includingParents */); |
| 435 | } |
| 436 | break; |
| 437 | default: |
Winson Chung | 123e07a | 2018-02-27 11:47:16 -0800 | [diff] [blame] | 438 | // TODO: Removing the child before reinserting requires the caller to provide a |
| 439 | // position that takes into account the removed child (if the index of the |
| 440 | // child < position, then the position should be adjusted). We should consider |
| 441 | // doing this adjustment here and remove any adjustments in the callers. |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 442 | mChildren.remove(child); |
| 443 | mChildren.add(position, child); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 448 | * Update override configuration and recalculate full config. |
| 449 | * @see #mOverrideConfiguration |
| 450 | * @see #mFullConfiguration |
| 451 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 452 | @Override |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 453 | public void onOverrideConfigurationChanged(Configuration overrideConfiguration) { |
| 454 | // We must diff before the configuration is applied so that we can capture the change |
| 455 | // against the existing bounds. |
| 456 | final int diff = diffOverrideBounds(overrideConfiguration.windowConfiguration.getBounds()); |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 457 | super.onOverrideConfigurationChanged(overrideConfiguration); |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 458 | if (mParent != null) { |
| 459 | mParent.onDescendantOverrideConfigurationChanged(); |
| 460 | } |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 461 | |
| 462 | if (diff == BOUNDS_CHANGE_NONE) { |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | if ((diff & BOUNDS_CHANGE_SIZE) == BOUNDS_CHANGE_SIZE) { |
| 467 | onResize(); |
| 468 | } else { |
| 469 | onMovedByResize(); |
| 470 | } |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Notify that a descendant's overrideConfiguration has changed. |
| 475 | */ |
| 476 | void onDescendantOverrideConfigurationChanged() { |
| 477 | if (mParent != null) { |
| 478 | mParent.onDescendantOverrideConfigurationChanged(); |
| 479 | } |
Andrii Kulian | 441e449 | 2016-09-29 15:25:00 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
Wale Ogunwale | 02319a6 | 2016-09-26 15:21:22 -0700 | [diff] [blame] | 483 | * Notify that the display this container is on has changed. |
| 484 | * @param dc The new display this container is on. |
| 485 | */ |
| 486 | void onDisplayChanged(DisplayContent dc) { |
| 487 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 488 | final WindowContainer child = mChildren.get(i); |
| 489 | child.onDisplayChanged(dc); |
| 490 | } |
| 491 | } |
| 492 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 493 | void setWaitingForDrawnIfResizingChanged() { |
| 494 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 495 | final WindowContainer wc = mChildren.get(i); |
| 496 | wc.setWaitingForDrawnIfResizingChanged(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void onResize() { |
| 501 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 502 | final WindowContainer wc = mChildren.get(i); |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 503 | wc.onParentResize(); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 507 | void onParentResize() { |
| 508 | // In the case this container has specified its own bounds, a parent resize will not |
| 509 | // affect its bounds. Any relevant changes will be propagated through changes to the |
| 510 | // Configuration override. |
| 511 | if (hasOverrideBounds()) { |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | // Default implementation is to treat as resize on self. |
| 516 | onResize(); |
| 517 | } |
| 518 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 519 | void onMovedByResize() { |
| 520 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 521 | final WindowContainer wc = mChildren.get(i); |
| 522 | wc.onMovedByResize(); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void resetDragResizingChangeReported() { |
| 527 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 528 | final WindowContainer wc = mChildren.get(i); |
| 529 | wc.resetDragResizingChangeReported(); |
| 530 | } |
| 531 | } |
| 532 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 533 | void forceWindowsScaleableInTransaction(boolean force) { |
| 534 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 535 | final WindowContainer wc = mChildren.get(i); |
| 536 | wc.forceWindowsScaleableInTransaction(force); |
| 537 | } |
| 538 | } |
| 539 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 540 | /** |
| 541 | * @return Whether our own container is running an animation or any child, no matter how deep in |
| 542 | * the hierarchy, is animating. |
| 543 | */ |
| 544 | boolean isSelfOrChildAnimating() { |
| 545 | if (isSelfAnimating()) { |
| 546 | return true; |
| 547 | } |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 548 | for (int j = mChildren.size() - 1; j >= 0; j--) { |
| 549 | final WindowContainer wc = mChildren.get(j); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 550 | if (wc.isSelfOrChildAnimating()) { |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | } |
| 554 | return false; |
| 555 | } |
| 556 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 557 | /** |
| 558 | * @return Whether our own container is running an animation or our parent is animating. This |
| 559 | * doesn't consider whether children are animating. |
| 560 | */ |
| 561 | boolean isAnimating() { |
| 562 | |
| 563 | // We are animating if we ourselves are animating or if our parent is animating. |
| 564 | return isSelfAnimating() || mParent != null && mParent.isAnimating(); |
| 565 | } |
| 566 | |
| 567 | /** |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 568 | * @return {@code true} if in this subtree of the hierarchy we have an {@link AppWindowToken} |
| 569 | * that is {@link #isSelfAnimating}; {@code false} otherwise. |
| 570 | */ |
| 571 | boolean isAppAnimating() { |
| 572 | for (int j = mChildren.size() - 1; j >= 0; j--) { |
| 573 | final WindowContainer wc = mChildren.get(j); |
| 574 | if (wc.isAppAnimating()) { |
| 575 | return true; |
| 576 | } |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | /** |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 582 | * @return Whether our own container running an animation at the moment. |
| 583 | */ |
| 584 | boolean isSelfAnimating() { |
| 585 | return mSurfaceAnimator.isAnimating(); |
| 586 | } |
| 587 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 588 | void sendAppVisibilityToClients() { |
| 589 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 590 | final WindowContainer wc = mChildren.get(i); |
| 591 | wc.sendAppVisibilityToClients(); |
| 592 | } |
| 593 | } |
| 594 | |
Wale Ogunwale | 44f2180 | 2016-09-02 12:49:48 -0700 | [diff] [blame] | 595 | /** |
| 596 | * Returns true if the container or one of its children as some content it can display or wants |
| 597 | * to display (e.g. app views or saved surface). |
| 598 | * |
| 599 | * NOTE: While this method will return true if the there is some content to display, it doesn't |
| 600 | * mean the container is visible. Use {@link #isVisible()} to determine if the container is |
| 601 | * visible. |
| 602 | */ |
| 603 | boolean hasContentToDisplay() { |
| 604 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 605 | final WindowContainer wc = mChildren.get(i); |
| 606 | if (wc.hasContentToDisplay()) { |
| 607 | return true; |
| 608 | } |
| 609 | } |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Returns true if the container or one of its children is considered visible from the |
| 615 | * WindowManager perspective which usually means valid surface and some other internal state |
| 616 | * are true. |
| 617 | * |
| 618 | * NOTE: While this method will return true if the surface is visible, it doesn't mean the |
| 619 | * client has actually displayed any content. Use {@link #hasContentToDisplay()} to determine if |
| 620 | * the container has any content to display. |
| 621 | */ |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 622 | boolean isVisible() { |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 623 | // TODO: Will this be more correct if it checks the visibility of its parents? |
| 624 | // It depends...For example, Tasks and Stacks are only visible if there children are visible |
| 625 | // 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] | 626 | // container specify which direction to traverse for visibility? |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 627 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 628 | final WindowContainer wc = mChildren.get(i); |
| 629 | if (wc.isVisible()) { |
| 630 | return true; |
| 631 | } |
| 632 | } |
| 633 | return false; |
| 634 | } |
| 635 | |
Bryce Lee | 00d586d | 2017-07-28 20:48:43 -0700 | [diff] [blame] | 636 | /** |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 637 | * @return Whether this child is on top of the window hierarchy. |
Bryce Lee | 00d586d | 2017-07-28 20:48:43 -0700 | [diff] [blame] | 638 | */ |
| 639 | boolean isOnTop() { |
| 640 | return getParent().getTopChild() == this && getParent().isOnTop(); |
| 641 | } |
| 642 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 643 | /** Returns the top child container. */ |
| 644 | E getTopChild() { |
| 645 | return mChildren.peekLast(); |
Wale Ogunwale | 9bc4773 | 2016-08-10 14:44:22 -0700 | [diff] [blame] | 646 | } |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 647 | |
Wale Ogunwale | 3f4433d | 2016-08-18 20:42:42 -0700 | [diff] [blame] | 648 | /** Returns true if there is still a removal being deferred */ |
| 649 | boolean checkCompleteDeferredRemoval() { |
| 650 | boolean stillDeferringRemoval = false; |
| 651 | |
| 652 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 653 | final WindowContainer wc = mChildren.get(i); |
| 654 | stillDeferringRemoval |= wc.checkCompleteDeferredRemoval(); |
| 655 | } |
| 656 | |
| 657 | return stillDeferringRemoval; |
| 658 | } |
| 659 | |
| 660 | /** 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] | 661 | void checkAppWindowsReadyToShow() { |
Wale Ogunwale | 3f4433d | 2016-08-18 20:42:42 -0700 | [diff] [blame] | 662 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 663 | final WindowContainer wc = mChildren.get(i); |
Wale Ogunwale | b0f3b83 | 2016-10-17 10:13:07 -0700 | [diff] [blame] | 664 | wc.checkAppWindowsReadyToShow(); |
Wale Ogunwale | 3f4433d | 2016-08-18 20:42:42 -0700 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 668 | void onAppTransitionDone() { |
| 669 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 670 | final WindowContainer wc = mChildren.get(i); |
| 671 | wc.onAppTransitionDone(); |
| 672 | } |
| 673 | } |
| 674 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 675 | void setOrientation(int orientation) { |
| 676 | mOrientation = orientation; |
| 677 | } |
| 678 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 679 | int getOrientation() { |
| 680 | return getOrientation(mOrientation); |
| 681 | } |
| 682 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 683 | /** |
| 684 | * Returns the specified orientation for this window container or one of its children is there |
| 685 | * is one set, or {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSET} if no |
| 686 | * specification is set. |
| 687 | * NOTE: {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED} is a |
| 688 | * specification... |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 689 | * |
| 690 | * @param candidate The current orientation candidate that will be returned if we don't find a |
| 691 | * better match. |
| 692 | * @return The orientation as specified by this branch or the window hierarchy. |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 693 | */ |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 694 | int getOrientation(int candidate) { |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 695 | if (!fillsParent()) { |
| 696 | // Ignore containers that don't completely fill their parents. |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 697 | return SCREEN_ORIENTATION_UNSET; |
| 698 | } |
| 699 | |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 700 | // The container fills its parent so we can use it orientation if it has one |
| 701 | // 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] | 702 | // specified and fall back on this container's unset or unspecified value as a candidate |
| 703 | // if none of the children have a better candidate for the orientation. |
| 704 | if (mOrientation != SCREEN_ORIENTATION_UNSET |
| 705 | && mOrientation != SCREEN_ORIENTATION_UNSPECIFIED) { |
| 706 | return mOrientation; |
| 707 | } |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 708 | |
| 709 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 710 | final WindowContainer wc = mChildren.get(i); |
| 711 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 712 | // TODO: Maybe mOrientation should default to SCREEN_ORIENTATION_UNSET vs. |
| 713 | // SCREEN_ORIENTATION_UNSPECIFIED? |
| 714 | final int orientation = wc.getOrientation(candidate == SCREEN_ORIENTATION_BEHIND |
| 715 | ? SCREEN_ORIENTATION_BEHIND : SCREEN_ORIENTATION_UNSET); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 716 | if (orientation == SCREEN_ORIENTATION_BEHIND) { |
| 717 | // container wants us to use the orientation of the container behind it. See if we |
| 718 | // can find one. Else return SCREEN_ORIENTATION_BEHIND so the caller can choose to |
| 719 | // look behind this container. |
| 720 | candidate = orientation; |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | if (orientation == SCREEN_ORIENTATION_UNSET) { |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | if (wc.fillsParent() || orientation != SCREEN_ORIENTATION_UNSPECIFIED) { |
| 729 | // Use the orientation if the container fills its parent or requested an explicit |
| 730 | // orientation that isn't SCREEN_ORIENTATION_UNSPECIFIED. |
| 731 | return orientation; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | return candidate; |
| 736 | } |
| 737 | |
| 738 | /** |
Adrian Roos | 41f7e9d | 2018-06-07 15:29:34 +0200 | [diff] [blame] | 739 | * Seamlessly rotates the container, by recomputing the location in the new |
| 740 | * rotation, and rotating buffers until they are updated for the new rotation. |
| 741 | * |
| 742 | * @param t the transaction to perform the seamless rotation in |
| 743 | * @param oldRotation the rotation we are rotating from |
| 744 | * @param newRotation the rotation we are rotating to |
| 745 | */ |
| 746 | void seamlesslyRotate(Transaction t, int oldRotation, int newRotation) { |
| 747 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 748 | mChildren.get(i).seamlesslyRotate(t, oldRotation, newRotation); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /** |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 753 | * Returns true if this container is opaque and fills all the space made available by its parent |
| 754 | * container. |
| 755 | * |
| 756 | * NOTE: It is possible for this container to occupy more space than the parent has (or less), |
| 757 | * this is just a signal from the client to window manager stating its intent, but not what it |
| 758 | * actually does. |
| 759 | */ |
| 760 | boolean fillsParent() { |
| 761 | return false; |
| 762 | } |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 763 | |
Wale Ogunwale | 6213caa | 2016-12-02 16:47:15 +0000 | [diff] [blame] | 764 | // TODO: Users would have their own window containers under the display container? |
| 765 | void switchUser() { |
| 766 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 767 | mChildren.get(i).switchUser(); |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 768 | } |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 771 | /** |
| 772 | * For all windows at or below this container call the callback. |
| 773 | * @param callback Calls the {@link ToBooleanFunction#apply} method for each window found and |
| 774 | * stops the search if {@link ToBooleanFunction#apply} returns true. |
| 775 | * @param traverseTopToBottom If true traverses the hierarchy from top-to-bottom in terms of |
| 776 | * z-order, else from bottom-to-top. |
| 777 | * @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] | 778 | * {@link ToBooleanFunction#apply} returning true. |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 779 | */ |
| 780 | boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) { |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 781 | if (traverseTopToBottom) { |
| 782 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 783 | if (mChildren.get(i).forAllWindows(callback, traverseTopToBottom)) { |
| 784 | return true; |
| 785 | } |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 786 | } |
| 787 | } else { |
| 788 | final int count = mChildren.size(); |
| 789 | for (int i = 0; i < count; i++) { |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 790 | if (mChildren.get(i).forAllWindows(callback, traverseTopToBottom)) { |
| 791 | return true; |
| 792 | } |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 795 | return false; |
| 796 | } |
| 797 | |
| 798 | void forAllWindows(Consumer<WindowState> callback, boolean traverseTopToBottom) { |
Wale Ogunwale | ee41d4a | 2016-11-21 08:41:10 -0800 | [diff] [blame] | 799 | ForAllWindowsConsumerWrapper wrapper = obtainConsumerWrapper(callback); |
| 800 | forAllWindows(wrapper, traverseTopToBottom); |
| 801 | wrapper.release(); |
Wale Ogunwale | b783fd8 | 2016-11-04 09:51:54 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 804 | /** |
| 805 | * For all tasks at or below this container call the callback. |
| 806 | * |
| 807 | * @param callback Callback to be called for every task. |
| 808 | */ |
| 809 | void forAllTasks(Consumer<Task> callback) { |
| 810 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 811 | mChildren.get(i).forAllTasks(callback); |
| 812 | } |
| 813 | } |
| 814 | |
Wale Ogunwale | d188096 | 2016-11-08 10:31:59 -0800 | [diff] [blame] | 815 | WindowState getWindow(Predicate<WindowState> callback) { |
| 816 | for (int i = mChildren.size() - 1; i >= 0; --i) { |
| 817 | final WindowState w = mChildren.get(i).getWindow(callback); |
| 818 | if (w != null) { |
| 819 | return w; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | return null; |
| 824 | } |
| 825 | |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 826 | /** |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 827 | * Returns 1, 0, or -1 depending on if this container is greater than, equal to, or lesser than |
| 828 | * the input container in terms of z-order. |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 829 | */ |
| 830 | @Override |
| 831 | public int compareTo(WindowContainer other) { |
| 832 | if (this == other) { |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | if (mParent != null && mParent == other.mParent) { |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 837 | final WindowList<WindowContainer> list = mParent.mChildren; |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 838 | return list.indexOf(this) > list.indexOf(other) ? 1 : -1; |
| 839 | } |
| 840 | |
| 841 | final LinkedList<WindowContainer> thisParentChain = mTmpChain1; |
| 842 | final LinkedList<WindowContainer> otherParentChain = mTmpChain2; |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 843 | try { |
| 844 | getParents(thisParentChain); |
| 845 | other.getParents(otherParentChain); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 846 | |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 847 | // Find the common ancestor of both containers. |
| 848 | WindowContainer commonAncestor = null; |
| 849 | WindowContainer thisTop = thisParentChain.peekLast(); |
| 850 | WindowContainer otherTop = otherParentChain.peekLast(); |
| 851 | while (thisTop != null && otherTop != null && thisTop == otherTop) { |
| 852 | commonAncestor = thisParentChain.removeLast(); |
| 853 | otherParentChain.removeLast(); |
| 854 | thisTop = thisParentChain.peekLast(); |
| 855 | otherTop = otherParentChain.peekLast(); |
| 856 | } |
| 857 | |
| 858 | // Containers don't belong to the same hierarchy??? |
| 859 | if (commonAncestor == null) { |
| 860 | throw new IllegalArgumentException("No in the same hierarchy this=" |
| 861 | + thisParentChain + " other=" + otherParentChain); |
| 862 | } |
| 863 | |
| 864 | // Children are always considered greater than their parents, so if one of the containers |
| 865 | // we are comparing it the parent of the other then whichever is the child is greater. |
| 866 | if (commonAncestor == this) { |
| 867 | return -1; |
| 868 | } else if (commonAncestor == other) { |
| 869 | return 1; |
| 870 | } |
| 871 | |
| 872 | // The position of the first non-common ancestor in the common ancestor list determines |
| 873 | // which is greater the which. |
| 874 | final WindowList<WindowContainer> list = commonAncestor.mChildren; |
| 875 | return list.indexOf(thisParentChain.peekLast()) > list.indexOf(otherParentChain.peekLast()) |
| 876 | ? 1 : -1; |
| 877 | } finally { |
| 878 | mTmpChain1.clear(); |
| 879 | mTmpChain2.clear(); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 880 | } |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | private void getParents(LinkedList<WindowContainer> parents) { |
| 884 | parents.clear(); |
| 885 | WindowContainer current = this; |
| 886 | do { |
| 887 | parents.addLast(current); |
| 888 | current = current.mParent; |
| 889 | } while (current != null); |
| 890 | } |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 891 | |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 892 | WindowContainerController getController() { |
| 893 | return mController; |
| 894 | } |
| 895 | |
| 896 | void setController(WindowContainerController controller) { |
| 897 | if (mController != null && controller != null) { |
| 898 | throw new IllegalArgumentException("Can't set controller=" + mController |
| 899 | + " for container=" + this + " Already set to=" + mController); |
| 900 | } |
| 901 | if (controller != null) { |
| 902 | controller.setContainer(this); |
| 903 | } else if (mController != null) { |
| 904 | mController.setContainer(null); |
| 905 | } |
| 906 | mController = controller; |
| 907 | } |
| 908 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 909 | SurfaceControl.Builder makeSurface() { |
| 910 | final WindowContainer p = getParent(); |
| 911 | return p.makeChildSurface(this); |
| 912 | } |
| 913 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 914 | /** |
| 915 | * @param child The WindowContainer this child surface is for, or null if the Surface |
| 916 | * is not assosciated with a WindowContainer (e.g. a surface used for Dimming). |
| 917 | */ |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 918 | SurfaceControl.Builder makeChildSurface(WindowContainer child) { |
| 919 | final WindowContainer p = getParent(); |
| 920 | // Give the parent a chance to set properties. In hierarchy v1 we rely |
| 921 | // 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] | 922 | return p.makeChildSurface(child) |
| 923 | .setParent(mSurfaceControl); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 926 | @Override |
| 927 | public SurfaceControl getParentSurfaceControl() { |
| 928 | final WindowContainer parent = getParent(); |
| 929 | if (parent == null) { |
| 930 | return null; |
| 931 | } |
| 932 | return parent.getSurfaceControl(); |
| 933 | } |
| 934 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 935 | /** |
| 936 | * @return Whether this WindowContainer should be magnified by the accessibility magnifier. |
| 937 | */ |
| 938 | boolean shouldMagnify() { |
Yuki Awano | 4c36b55 | 2018-04-24 17:27:50 +0900 | [diff] [blame] | 939 | if (mSurfaceControl == null) { |
| 940 | return false; |
| 941 | } |
| 942 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 943 | for (int i = 0; i < mChildren.size(); i++) { |
| 944 | if (!mChildren.get(i).shouldMagnify()) { |
| 945 | return false; |
| 946 | } |
| 947 | } |
| 948 | return true; |
| 949 | } |
| 950 | |
| 951 | SurfaceSession getSession() { |
| 952 | if (getParent() != null) { |
| 953 | return getParent().getSession(); |
| 954 | } |
| 955 | return null; |
| 956 | } |
| 957 | |
| 958 | void assignLayer(Transaction t, int layer) { |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 959 | final boolean changed = layer != mLastLayer || mLastRelativeToLayer != null; |
| 960 | if (mSurfaceControl != null && changed) { |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 961 | setLayer(t, layer); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 962 | mLastLayer = layer; |
| 963 | mLastRelativeToLayer = null; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | void assignRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) { |
| 968 | final boolean changed = layer != mLastLayer || mLastRelativeToLayer != relativeTo; |
| 969 | if (mSurfaceControl != null && changed) { |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 970 | setRelativeLayer(t, relativeTo, layer); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 971 | mLastLayer = layer; |
| 972 | mLastRelativeToLayer = relativeTo; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 976 | protected void setLayer(Transaction t, int layer) { |
| 977 | |
| 978 | // Route through surface animator to accommodate that our surface control might be |
| 979 | // attached to the leash, and leash is attached to parent container. |
| 980 | mSurfaceAnimator.setLayer(t, layer); |
| 981 | } |
| 982 | |
| 983 | protected void setRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) { |
| 984 | |
| 985 | // Route through surface animator to accommodate that our surface control might be |
| 986 | // attached to the leash, and leash is attached to parent container. |
| 987 | mSurfaceAnimator.setRelativeLayer(t, relativeTo, layer); |
| 988 | } |
| 989 | |
| 990 | protected void reparentSurfaceControl(Transaction t, SurfaceControl newParent) { |
| 991 | mSurfaceAnimator.reparent(t, newParent); |
| 992 | } |
| 993 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 994 | void assignChildLayers(Transaction t) { |
| 995 | int layer = 0; |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 996 | |
| 997 | // We use two passes as a way to promote children which |
| 998 | // need Z-boosting to the end of the list. |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 999 | for (int j = 0; j < mChildren.size(); ++j) { |
| 1000 | final WindowContainer wc = mChildren.get(j); |
| 1001 | wc.assignChildLayers(t); |
| 1002 | if (!wc.needsZBoost()) { |
| 1003 | wc.assignLayer(t, layer++); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1004 | } |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1005 | } |
| 1006 | for (int j = 0; j < mChildren.size(); ++j) { |
| 1007 | final WindowContainer wc = mChildren.get(j); |
| 1008 | if (wc.needsZBoost()) { |
| 1009 | wc.assignLayer(t, layer++); |
| 1010 | } |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | void assignChildLayers() { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1015 | assignChildLayers(getPendingTransaction()); |
Jorim Jaggi | ffe128d | 2017-11-30 13:54:36 +0100 | [diff] [blame] | 1016 | scheduleAnimation(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | boolean needsZBoost() { |
| 1020 | for (int i = 0; i < mChildren.size(); i++) { |
| 1021 | if (mChildren.get(i).needsZBoost()) { |
| 1022 | return true; |
| 1023 | } |
| 1024 | } |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
Wale Ogunwale | 9adfe57 | 2016-09-08 20:43:58 -0700 | [diff] [blame] | 1028 | /** |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1029 | * 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] | 1030 | * {@link com.android.server.wm.WindowContainerProto}. |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1031 | * |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1032 | * @param proto Stream to write the WindowContainer object to. |
| 1033 | * @param fieldId Field Id of the WindowContainer as defined in the parent message. |
| 1034 | * @param trim If true, reduce the amount of data written. |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1035 | * @hide |
| 1036 | */ |
| 1037 | @CallSuper |
| 1038 | @Override |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1039 | public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) { |
| 1040 | final long token = proto.start(fieldId); |
| 1041 | super.writeToProto(proto, CONFIGURATION_CONTAINER, trim); |
| 1042 | proto.write(ORIENTATION, mOrientation); |
Wale Ogunwale | df262f5 | 2017-12-07 18:17:12 -0800 | [diff] [blame] | 1043 | proto.write(VISIBLE, isVisible()); |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 1044 | mSurfaceAnimator.writeToProto(proto, SURFACE_ANIMATOR); |
Adrian Roos | 4921ccf | 2017-09-28 16:54:06 +0200 | [diff] [blame] | 1045 | proto.end(token); |
Wale Ogunwale | 0d5609b | 2017-09-13 05:55:07 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Wale Ogunwale | ee41d4a | 2016-11-21 08:41:10 -0800 | [diff] [blame] | 1048 | private ForAllWindowsConsumerWrapper obtainConsumerWrapper(Consumer<WindowState> consumer) { |
| 1049 | ForAllWindowsConsumerWrapper wrapper = mConsumerWrapperPool.acquire(); |
| 1050 | if (wrapper == null) { |
| 1051 | wrapper = new ForAllWindowsConsumerWrapper(); |
| 1052 | } |
| 1053 | wrapper.setConsumer(consumer); |
| 1054 | return wrapper; |
| 1055 | } |
| 1056 | |
| 1057 | private final class ForAllWindowsConsumerWrapper implements ToBooleanFunction<WindowState> { |
| 1058 | |
| 1059 | private Consumer<WindowState> mConsumer; |
| 1060 | |
| 1061 | void setConsumer(Consumer<WindowState> consumer) { |
| 1062 | mConsumer = consumer; |
| 1063 | } |
| 1064 | |
| 1065 | @Override |
| 1066 | public boolean apply(WindowState w) { |
| 1067 | mConsumer.accept(w); |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | void release() { |
| 1072 | mConsumer = null; |
| 1073 | mConsumerWrapperPool.release(this); |
| 1074 | } |
| 1075 | } |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1076 | |
| 1077 | // TODO(b/68336570): Should this really be on WindowContainer since it |
| 1078 | // can only be used on the top-level nodes that aren't animated? |
| 1079 | // (otherwise we would be fighting other callers of setMatrix). |
| 1080 | void applyMagnificationSpec(Transaction t, MagnificationSpec spec) { |
| 1081 | if (shouldMagnify()) { |
| 1082 | t.setMatrix(mSurfaceControl, spec.scale, 0, 0, spec.scale) |
| 1083 | .setPosition(mSurfaceControl, spec.offsetX, spec.offsetY); |
| 1084 | } else { |
| 1085 | for (int i = 0; i < mChildren.size(); i++) { |
| 1086 | mChildren.get(i).applyMagnificationSpec(t, spec); |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * TODO: Once we totally eliminate global transaction we will pass transaction in here |
| 1093 | * rather than merging to global. |
| 1094 | */ |
| 1095 | void prepareSurfaces() { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1096 | SurfaceControl.mergeToGlobalTransaction(getPendingTransaction()); |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 1097 | |
| 1098 | // If a leash has been set when the transaction was committed, then the leash reparent has |
| 1099 | // been committed. |
| 1100 | mCommittedReparentToAnimationLeash = mSurfaceAnimator.hasLeash(); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1101 | for (int i = 0; i < mChildren.size(); i++) { |
| 1102 | mChildren.get(i).prepareSurfaces(); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /** |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 1107 | * @return true if the reparent to animation leash transaction has been committed, false |
| 1108 | * otherwise. |
| 1109 | */ |
| 1110 | boolean hasCommittedReparentToAnimationLeash() { |
| 1111 | return mCommittedReparentToAnimationLeash; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1115 | * Trigger a call to prepareSurfaces from the animation thread, such that |
| 1116 | * mPendingTransaction will be applied. |
| 1117 | */ |
| 1118 | void scheduleAnimation() { |
| 1119 | if (mParent != null) { |
| 1120 | mParent.scheduleAnimation(); |
| 1121 | } |
| 1122 | } |
| 1123 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1124 | @Override |
| 1125 | public SurfaceControl getSurfaceControl() { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 1126 | return mSurfaceControl; |
| 1127 | } |
| 1128 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1129 | @Override |
| 1130 | public Transaction getPendingTransaction() { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1131 | return mPendingTransaction; |
| 1132 | } |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1133 | |
| 1134 | /** |
| 1135 | * Starts an animation on the container. |
| 1136 | * |
| 1137 | * @param anim The animation to run. |
| 1138 | * @param hidden Whether our container is currently hidden. TODO This should use isVisible at |
| 1139 | * some point but the meaning is too weird to work for all containers. |
| 1140 | */ |
| 1141 | void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden) { |
| 1142 | if (DEBUG_ANIM) Slog.v(TAG, "Starting animation on " + this + ": " + anim); |
| 1143 | |
| 1144 | // TODO: This should use isVisible() but because isVisible has a really weird meaning at |
| 1145 | // the moment this doesn't work for all animatable window containers. |
| 1146 | mSurfaceAnimator.startAnimation(t, anim, hidden); |
| 1147 | } |
| 1148 | |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 1149 | void transferAnimation(WindowContainer from) { |
| 1150 | mSurfaceAnimator.transferAnimation(from.mSurfaceAnimator); |
| 1151 | } |
| 1152 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1153 | void cancelAnimation() { |
| 1154 | mSurfaceAnimator.cancelAnimation(); |
| 1155 | } |
| 1156 | |
| 1157 | @Override |
| 1158 | public Builder makeAnimationLeash() { |
| 1159 | return makeSurface(); |
| 1160 | } |
| 1161 | |
Jorim Jaggi | 596a199 | 2017-12-29 14:48:02 +0100 | [diff] [blame] | 1162 | @Override |
| 1163 | public SurfaceControl getAnimationLeashParent() { |
| 1164 | return getParentSurfaceControl(); |
| 1165 | } |
| 1166 | |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 1167 | /** |
| 1168 | * @return The layer on which all app animations are happening. |
| 1169 | */ |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 1170 | SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) { |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 1171 | final WindowContainer parent = getParent(); |
| 1172 | if (parent != null) { |
Jorim Jaggi | 39179062 | 2018-04-18 15:30:44 +0200 | [diff] [blame] | 1173 | return parent.getAppAnimationLayer(animationLayer); |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 1174 | } |
| 1175 | return null; |
| 1176 | } |
| 1177 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1178 | @Override |
| 1179 | public void commitPendingTransaction() { |
| 1180 | scheduleAnimation(); |
| 1181 | } |
| 1182 | |
Robert Carr | 2f8aa39 | 2018-01-31 14:46:51 -0800 | [diff] [blame] | 1183 | void reassignLayer(Transaction t) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1184 | final WindowContainer parent = getParent(); |
| 1185 | if (parent != null) { |
| 1186 | parent.assignChildLayers(t); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | @Override |
| 1191 | public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) { |
Robert Carr | f12f9d3 | 2018-03-01 15:24:36 -0800 | [diff] [blame] | 1192 | mLastLayer = -1; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1193 | reassignLayer(t); |
| 1194 | } |
| 1195 | |
| 1196 | @Override |
| 1197 | public void onAnimationLeashDestroyed(Transaction t) { |
Robert Carr | f12f9d3 | 2018-03-01 15:24:36 -0800 | [diff] [blame] | 1198 | mLastLayer = -1; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1199 | reassignLayer(t); |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * Called when an animation has finished running. |
| 1204 | */ |
| 1205 | protected void onAnimationFinished() { |
| 1206 | } |
| 1207 | |
| 1208 | /** |
| 1209 | * @return The currently running animation, if any, or {@code null} otherwise. |
| 1210 | */ |
| 1211 | AnimationAdapter getAnimation() { |
| 1212 | return mSurfaceAnimator.getAnimation(); |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * @see SurfaceAnimator#startDelayingAnimationStart |
| 1217 | */ |
| 1218 | void startDelayingAnimationStart() { |
| 1219 | mSurfaceAnimator.startDelayingAnimationStart(); |
| 1220 | } |
| 1221 | |
| 1222 | /** |
| 1223 | * @see SurfaceAnimator#endDelayingAnimationStart |
| 1224 | */ |
| 1225 | void endDelayingAnimationStart() { |
| 1226 | mSurfaceAnimator.endDelayingAnimationStart(); |
| 1227 | } |
| 1228 | |
| 1229 | @Override |
| 1230 | public int getSurfaceWidth() { |
| 1231 | return mSurfaceControl.getWidth(); |
| 1232 | } |
| 1233 | |
| 1234 | @Override |
| 1235 | public int getSurfaceHeight() { |
| 1236 | return mSurfaceControl.getHeight(); |
| 1237 | } |
| 1238 | |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 1239 | @CallSuper |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 1240 | void dump(PrintWriter pw, String prefix, boolean dumpAll) { |
| 1241 | if (mSurfaceAnimator.isAnimating()) { |
| 1242 | pw.print(prefix); pw.println("ContainerAnimator:"); |
| 1243 | mSurfaceAnimator.dump(pw, prefix + " "); |
| 1244 | } |
| 1245 | } |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 1246 | |
chaviw | 2f0567b | 2018-01-29 16:22:02 -0800 | [diff] [blame] | 1247 | void updateSurfacePosition() { |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 1248 | if (mSurfaceControl == null) { |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | getRelativePosition(mTmpPos); |
chaviw | 3e751af | 2018-01-11 11:22:39 -0800 | [diff] [blame] | 1253 | if (mTmpPos.equals(mLastSurfacePosition)) { |
| 1254 | return; |
| 1255 | } |
| 1256 | |
chaviw | 2f0567b | 2018-01-29 16:22:02 -0800 | [diff] [blame] | 1257 | getPendingTransaction().setPosition(mSurfaceControl, mTmpPos.x, mTmpPos.y); |
chaviw | 3e751af | 2018-01-11 11:22:39 -0800 | [diff] [blame] | 1258 | mLastSurfacePosition.set(mTmpPos.x, mTmpPos.y); |
chaviw | e07246a | 2017-12-12 16:18:29 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | void getRelativePosition(Point outPos) { |
| 1262 | final Rect bounds = getBounds(); |
| 1263 | outPos.set(bounds.left, bounds.top); |
| 1264 | final WindowContainer parent = getParent(); |
| 1265 | if (parent != null) { |
| 1266 | final Rect parentBounds = parent.getBounds(); |
| 1267 | outPos.offset(-parentBounds.left, -parentBounds.top); |
| 1268 | } |
| 1269 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 1270 | |
| 1271 | Dimmer getDimmer() { |
| 1272 | if (mParent == null) { |
| 1273 | return null; |
| 1274 | } |
| 1275 | return mParent.getDimmer(); |
| 1276 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 1277 | } |