blob: 19c5a3d6452a951c170dc4cf7e30664f69e82a30 [file] [log] [blame]
Wale Ogunwaled63594a2016-07-18 07:48:30 -07001/*
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
17package com.android.server.wm;
18
Jorim Jaggi612bb882017-05-16 17:11:18 +020019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
21import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Vishnu Nair04ab4392018-01-10 11:00:06 -080022import static android.view.SurfaceControl.Transaction;
Jorim Jaggia5e10572017-11-15 14:36:26 +010023import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
25import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070026import static com.android.server.wm.WindowContainerProto.CONFIGURATION_CONTAINER;
27import static com.android.server.wm.WindowContainerProto.ORIENTATION;
28import static com.android.server.wm.WindowContainerProto.SURFACE_ANIMATOR;
29import static com.android.server.wm.WindowContainerProto.VISIBLE;
Jorim Jaggi612bb882017-05-16 17:11:18 +020030
Wale Ogunwaled63594a2016-07-18 07:48:30 -070031import android.annotation.CallSuper;
Jorim Jaggi391790622018-04-18 15:30:44 +020032import android.annotation.IntDef;
33import android.app.WindowConfiguration;
Andrii Kulian441e4492016-09-29 15:25:00 -070034import android.content.res.Configuration;
chaviwe07246a2017-12-12 16:18:29 -080035import android.graphics.Point;
36import android.graphics.Rect;
Vishnu Nair04ab4392018-01-10 11:00:06 -080037import android.util.Pools;
Jorim Jaggia5e10572017-11-15 14:36:26 +010038import android.util.Slog;
Vishnu Nair04ab4392018-01-10 11:00:06 -080039import android.util.proto.ProtoOutputStream;
Robert Carrb1579c82017-09-05 14:54:47 -070040import android.view.MagnificationSpec;
41import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +010042import android.view.SurfaceControl.Builder;
Robert Carrb1579c82017-09-05 14:54:47 -070043import android.view.SurfaceSession;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070044
Jorim Jaggia5e10572017-11-15 14:36:26 +010045import com.android.internal.util.ToBooleanFunction;
46import com.android.server.wm.SurfaceAnimator.Animatable;
47
48import java.io.PrintWriter;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070049import java.util.Comparator;
50import java.util.LinkedList;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070051import java.util.function.Consumer;
Wale Ogunwaled1880962016-11-08 10:31:59 -080052import java.util.function.Predicate;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070053
54/**
55 * Defines common functionality for classes that can hold windows directly or through their
Wale Ogunwale51362492016-09-08 17:49:17 -070056 * children in a hierarchy form.
Wale Ogunwaled63594a2016-07-18 07:48:30 -070057 * 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 Ogunwale98d62312017-07-12 09:24:56 -070060class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<E>
Jorim Jaggia5e10572017-11-15 14:36:26 +010061 implements Comparable<WindowContainer>, Animatable {
62
63 private static final String TAG = TAG_WITH_CLASS_NAME ? "WindowContainer" : TAG_WM;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070064
Jorim Jaggi391790622018-04-18 15:30:44 +020065 /** 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 Kuliand2765632016-12-12 22:26:34 -080084 static final int POSITION_TOP = Integer.MAX_VALUE;
85 static final int POSITION_BOTTOM = Integer.MIN_VALUE;
86
Andrii Kulian441e4492016-09-29 15:25:00 -070087 /**
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 Jaggia5e10572017-11-15 14:36:26 +010092 private WindowContainer<WindowContainer> mParent = null;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070093
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 Jaggi612bb882017-05-16 17:11:18 +020096 protected final WindowList<E> mChildren = new WindowList<E>();
Wale Ogunwaled63594a2016-07-18 07:48:30 -070097
Wale Ogunwale51362492016-09-08 17:49:17 -070098 // The specified orientation for this window container.
99 protected int mOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
100
Wale Ogunwaleee41d4a2016-11-21 08:41:10 -0800101 private final Pools.SynchronizedPool<ForAllWindowsConsumerWrapper> mConsumerWrapperPool =
102 new Pools.SynchronizedPool<>(3);
103
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800104 // The owner/creator for this container. No controller if null.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100105 WindowContainerController mController;
Robert Carrb1579c82017-09-05 14:54:47 -0700106
107 protected SurfaceControl mSurfaceControl;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100108 private int mLastLayer = 0;
109 private SurfaceControl mLastRelativeToLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -0700110
111 /**
112 * Applied as part of the animation pass in "prepareSurfaces".
113 */
Jorim Jaggia5e10572017-11-15 14:36:26 +0100114 protected final Transaction mPendingTransaction;
115 protected final SurfaceAnimator mSurfaceAnimator;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100116 protected final WindowManagerService mService;
117
chaviwe07246a2017-12-12 16:18:29 -0800118 private final Point mTmpPos = new Point();
chaviw3e751af2018-01-11 11:22:39 -0800119 protected final Point mLastSurfacePosition = new Point();
chaviwe07246a2017-12-12 16:18:29 -0800120
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100121 /** Total number of elements in this subtree, including our own hierarchy element. */
122 private int mTreeWeight = 1;
123
chaviw7f1fa992018-01-10 13:52:12 -0800124 /**
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 Jaggiffe128d2017-11-30 13:54:36 +0100130 WindowContainer(WindowManagerService service) {
131 mService = service;
132 mPendingTransaction = service.mTransactionFactory.make();
Chavi Weingartenb736e322018-02-23 00:27:54 +0000133 mSurfaceAnimator = new SurfaceAnimator(this, this::onAnimationFinished, service);
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100134 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800135
Wale Ogunwale98d62312017-07-12 09:24:56 -0700136 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700137 final protected WindowContainer getParent() {
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700138 return mParent;
139 }
140
Wale Ogunwale98d62312017-07-12 09:24:56 -0700141 @Override
Wale Ogunwale61911492017-10-11 08:50:50 -0700142 protected int getChildCount() {
Wale Ogunwale98d62312017-07-12 09:24:56 -0700143 return mChildren.size();
144 }
145
146 @Override
Wale Ogunwale61911492017-10-11 08:50:50 -0700147 protected E getChildAt(int index) {
Wale Ogunwale98d62312017-07-12 09:24:56 -0700148 return mChildren.get(index);
149 }
150
chaviwe07246a2017-12-12 16:18:29 -0800151 @Override
152 public void onConfigurationChanged(Configuration newParentConfig) {
153 super.onConfigurationChanged(newParentConfig);
chaviw2f0567b2018-01-29 16:22:02 -0800154 updateSurfacePosition();
chaviwe07246a2017-12-12 16:18:29 -0800155 scheduleAnimation();
156 }
157
Jorim Jaggia5e10572017-11-15 14:36:26 +0100158 final protected void setParent(WindowContainer<WindowContainer> parent) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700159 mParent = parent;
Andrii Kulianb94292e2016-10-19 13:30:58 -0700160 // 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 Ogunwale98d62312017-07-12 09:24:56 -0700164 onConfigurationChanged(mParent.getConfiguration());
Andrii Kulianb94292e2016-10-19 13:30:58 -0700165 // Update merged override configuration of this container and all its children.
166 onMergedOverrideConfigurationChanged();
167 }
Andrii Kuliand2765632016-12-12 22:26:34 -0800168
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 Carrb1579c82017-09-05 14:54:47 -0700177 if (mParent == null) {
178 return;
179 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100180
Robert Carrb1579c82017-09-05 14:54:47 -0700181 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 Carrf59b8dd2017-10-02 18:58:36 -0700185 getPendingTransaction().show(mSurfaceControl);
Evan Roskyb1ea7ca2018-04-05 17:17:35 -0700186 updateSurfacePosition();
Robert Carrb1579c82017-09-05 14:54:47 -0700187 } else {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100188 // 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 Jaggi619c9f72017-12-19 18:04:29 +0100192 reparentSurfaceControl(getPendingTransaction(), mParent.mSurfaceControl);
Robert Carrb1579c82017-09-05 14:54:47 -0700193 }
194
195 // Either way we need to ask the parent to assign us a Z-order.
196 mParent.assignChildLayers();
197 scheduleAnimation();
Andrii Kulian441e4492016-09-29 15:25:00 -0700198 }
199
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700200 // Temp. holders for a chain of containers we are currently processing.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100201 private final LinkedList<WindowContainer> mTmpChain1 = new LinkedList<>();
202 private final LinkedList<WindowContainer> mTmpChain2 = new LinkedList<>();
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700203
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700204 /**
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 Ogunwaled90546a2016-09-09 23:28:03 -0700212 protected void addChild(E child, Comparator<E> comparator) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700213 if (child.getParent() != null) {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700214 throw new IllegalArgumentException("addChild: container=" + child.getName()
Andrii Kulian441e4492016-09-29 15:25:00 -0700215 + " is already a child of container=" + child.getParent().getName()
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700216 + " can't add to container=" + getName());
Wale Ogunwalef6192862016-09-10 13:42:30 -0700217 }
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700218
Andrii Kulianb94292e2016-10-19 13:30:58 -0700219 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 Ogunwaled63594a2016-07-18 07:48:30 -0700227 }
228 }
229
Andrii Kulianb94292e2016-10-19 13:30:58 -0700230 if (positionToAdd == -1) {
231 mChildren.add(child);
232 } else {
233 mChildren.add(positionToAdd, child);
234 }
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100235 onChildAdded(child);
236
Andrii Kulianb94292e2016-10-19 13:30:58 -0700237 // Set the parent after we've actually added a child in case a subclass depends on this.
238 child.setParent(this);
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700239 }
240
Wale Ogunwalef6192862016-09-10 13:42:30 -0700241 /** Adds the input window container has a child of this container at the input index. */
242 @CallSuper
Wale Ogunwale72919d22016-12-08 18:58:50 -0800243 void addChild(E child, int index) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700244 if (child.getParent() != null) {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700245 throw new IllegalArgumentException("addChild: container=" + child.getName()
Andrii Kulian441e4492016-09-29 15:25:00 -0700246 + " is already a child of container=" + child.getParent().getName()
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700247 + " can't add to container=" + getName());
Wale Ogunwalef6192862016-09-10 13:42:30 -0700248 }
Wale Ogunwalef6192862016-09-10 13:42:30 -0700249 mChildren.add(index, child);
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100250 onChildAdded(child);
251
Andrii Kulianb94292e2016-10-19 13:30:58 -0700252 // Set the parent after we've actually added a child in case a subclass depends on this.
253 child.setParent(this);
Wale Ogunwalef6192862016-09-10 13:42:30 -0700254 }
255
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100256 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 Ogunwalef6192862016-09-10 13:42:30 -0700265 /**
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 Jaggi619c9f72017-12-19 18:04:29 +0100273 onChildRemoved(child);
Andrii Kulian441e4492016-09-29 15:25:00 -0700274 child.setParent(null);
Wale Ogunwalef6192862016-09-10 13:42:30 -0700275 } else {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700276 throw new IllegalArgumentException("removeChild: container=" + child.getName()
277 + " is not a child of container=" + getName());
Wale Ogunwalef6192862016-09-10 13:42:30 -0700278 }
279 }
280
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100281 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 Ogunwale571771c2016-08-26 13:18:50 -0700290 /**
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 Ogunwaled63594a2016-07-18 07:48:30 -0700296 @CallSuper
Wale Ogunwale571771c2016-08-26 13:18:50 -0700297 void removeImmediately() {
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700298 while (!mChildren.isEmpty()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100299 final E child = mChildren.peekLast();
Wale Ogunwale571771c2016-08-26 13:18:50 -0700300 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 Jaggi619c9f72017-12-19 18:04:29 +0100304 if (mChildren.remove(child)) {
305 onChildRemoved(child);
306 }
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700307 }
308
Robert Carrb1579c82017-09-05 14:54:47 -0700309 if (mSurfaceControl != null) {
chaviw6728e2f2018-03-19 15:58:04 -0700310 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 Carrb1579c82017-09-05 14:54:47 -0700318 mSurfaceControl = null;
Chavi Weingartenb736e322018-02-23 00:27:54 +0000319 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -0700320 }
321
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700322 if (mParent != null) {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700323 mParent.removeChild(this);
Wale Ogunwale571771c2016-08-26 13:18:50 -0700324 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800325
326 if (mController != null) {
327 setController(null);
328 }
Robert Carrb1579c82017-09-05 14:54:47 -0700329
Wale Ogunwale571771c2016-08-26 13:18:50 -0700330 }
331
332 /**
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100333 * @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 Ogunwale571771c2016-08-26 13:18:50 -0700361 * 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 Ogunwaled63594a2016-07-18 07:48:30 -0700374 /** Returns true if this window container has the input child. */
Winson Chunge2d72172018-01-25 17:46:20 +0000375 boolean hasChild(E child) {
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700376 for (int i = mChildren.size() - 1; i >= 0; --i) {
Winson Chunge2d72172018-01-25 17:46:20 +0000377 final E current = mChildren.get(i);
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700378 if (current == child || current.hasChild(child)) {
379 return true;
380 }
381 }
382 return false;
383 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700384
Andrii Kulian441e4492016-09-29 15:25:00 -0700385 /**
Andrii Kuliand2765632016-12-12 22:26:34 -0800386 * 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 Ogunwalec5cc3012017-01-13 13:26:16 -0800397
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 Kuliand2765632016-12-12 22:26:34 -0800404 if ((position < 0 && position != POSITION_BOTTOM)
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800405 || (position > mChildren.size() && position != POSITION_TOP)) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800406 throw new IllegalArgumentException("positionAt: invalid position=" + position
407 + ", children number=" + mChildren.size());
408 }
409
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800410 if (position >= mChildren.size() - 1) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800411 position = POSITION_TOP;
412 } else if (position == 0) {
413 position = POSITION_BOTTOM;
414 }
415
416 switch (position) {
417 case POSITION_TOP:
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800418 if (mChildren.peekLast() != child) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800419 mChildren.remove(child);
Jorim Jaggi612bb882017-05-16 17:11:18 +0200420 mChildren.add(child);
Andrii Kuliand2765632016-12-12 22:26:34 -0800421 }
422 if (includingParents && getParent() != null) {
423 getParent().positionChildAt(POSITION_TOP, this /* child */,
424 true /* includingParents */);
425 }
426 break;
427 case POSITION_BOTTOM:
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800428 if (mChildren.peekFirst() != child) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800429 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 Chung123e07a2018-02-27 11:47:16 -0800438 // 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 Kuliand2765632016-12-12 22:26:34 -0800442 mChildren.remove(child);
443 mChildren.add(position, child);
444 }
445 }
446
447 /**
Andrii Kulian441e4492016-09-29 15:25:00 -0700448 * Update override configuration and recalculate full config.
449 * @see #mOverrideConfiguration
450 * @see #mFullConfiguration
451 */
Wale Ogunwale98d62312017-07-12 09:24:56 -0700452 @Override
Bryce Leef3c6a472017-11-14 14:53:06 -0800453 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 Ogunwale98d62312017-07-12 09:24:56 -0700457 super.onOverrideConfigurationChanged(overrideConfiguration);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700458 if (mParent != null) {
459 mParent.onDescendantOverrideConfigurationChanged();
460 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800461
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 Ogunwale55ddf8f2017-03-20 08:56:38 -0700471 }
472
473 /**
474 * Notify that a descendant's overrideConfiguration has changed.
475 */
476 void onDescendantOverrideConfigurationChanged() {
477 if (mParent != null) {
478 mParent.onDescendantOverrideConfigurationChanged();
479 }
Andrii Kulian441e4492016-09-29 15:25:00 -0700480 }
481
482 /**
Wale Ogunwale02319a62016-09-26 15:21:22 -0700483 * 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 Ogunwaled1c37912016-08-16 03:19:39 -0700493 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 Leed92ae482018-01-22 13:56:23 -0800503 wc.onParentResize();
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700504 }
505 }
506
Bryce Leed92ae482018-01-22 13:56:23 -0800507 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 Ogunwaled1c37912016-08-16 03:19:39 -0700519 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 Ogunwaled1c37912016-08-16 03:19:39 -0700533 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 Jaggia5e10572017-11-15 14:36:26 +0100540 /**
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 Ogunwaled1c37912016-08-16 03:19:39 -0700548 for (int j = mChildren.size() - 1; j >= 0; j--) {
549 final WindowContainer wc = mChildren.get(j);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100550 if (wc.isSelfOrChildAnimating()) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700551 return true;
552 }
553 }
554 return false;
555 }
556
Jorim Jaggia5e10572017-11-15 14:36:26 +0100557 /**
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 Jaggif5f9e122017-10-24 18:21:09 +0200568 * @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 Jaggia5e10572017-11-15 14:36:26 +0100582 * @return Whether our own container running an animation at the moment.
583 */
584 boolean isSelfAnimating() {
585 return mSurfaceAnimator.isAnimating();
586 }
587
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700588 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 Ogunwale44f21802016-09-02 12:49:48 -0700595 /**
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 Ogunwaled1c37912016-08-16 03:19:39 -0700622 boolean isVisible() {
Wale Ogunwalef6192862016-09-10 13:42:30 -0700623 // 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 Kuliancd5dcb8b2017-01-03 17:09:45 -0800626 // container specify which direction to traverse for visibility?
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700627 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 Lee00d586d2017-07-28 20:48:43 -0700636 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700637 * @return Whether this child is on top of the window hierarchy.
Bryce Lee00d586d2017-07-28 20:48:43 -0700638 */
639 boolean isOnTop() {
640 return getParent().getTopChild() == this && getParent().isOnTop();
641 }
642
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100643 /** Returns the top child container. */
644 E getTopChild() {
645 return mChildren.peekLast();
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700646 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700647
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700648 /** 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 Ogunwaleb0f3b832016-10-17 10:13:07 -0700661 void checkAppWindowsReadyToShow() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700662 for (int i = mChildren.size() - 1; i >= 0; --i) {
663 final WindowContainer wc = mChildren.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700664 wc.checkAppWindowsReadyToShow();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700665 }
666 }
667
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700668 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 Ogunwale51362492016-09-08 17:49:17 -0700675 void setOrientation(int orientation) {
676 mOrientation = orientation;
677 }
678
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700679 int getOrientation() {
680 return getOrientation(mOrientation);
681 }
682
Wale Ogunwale51362492016-09-08 17:49:17 -0700683 /**
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 Ogunwale5e5a68d2017-03-24 17:36:38 -0700689 *
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 Ogunwale51362492016-09-08 17:49:17 -0700693 */
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700694 int getOrientation(int candidate) {
Bryce Leea163b762017-01-24 11:05:01 -0800695 if (!fillsParent()) {
696 // Ignore containers that don't completely fill their parents.
Wale Ogunwale51362492016-09-08 17:49:17 -0700697 return SCREEN_ORIENTATION_UNSET;
698 }
699
Bryce Leea163b762017-01-24 11:05:01 -0800700 // 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 Ogunwale51362492016-09-08 17:49:17 -0700702 // 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 Ogunwale51362492016-09-08 17:49:17 -0700708
709 for (int i = mChildren.size() - 1; i >= 0; --i) {
710 final WindowContainer wc = mChildren.get(i);
711
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700712 // 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 Ogunwale51362492016-09-08 17:49:17 -0700716 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 /**
739 * Returns true if this container is opaque and fills all the space made available by its parent
740 * container.
741 *
742 * NOTE: It is possible for this container to occupy more space than the parent has (or less),
743 * this is just a signal from the client to window manager stating its intent, but not what it
744 * actually does.
745 */
746 boolean fillsParent() {
747 return false;
748 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700749
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000750 // TODO: Users would have their own window containers under the display container?
751 void switchUser() {
752 for (int i = mChildren.size() - 1; i >= 0; --i) {
753 mChildren.get(i).switchUser();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700754 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700755 }
756
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800757 /**
758 * For all windows at or below this container call the callback.
759 * @param callback Calls the {@link ToBooleanFunction#apply} method for each window found and
760 * stops the search if {@link ToBooleanFunction#apply} returns true.
761 * @param traverseTopToBottom If true traverses the hierarchy from top-to-bottom in terms of
762 * z-order, else from bottom-to-top.
763 * @return True if the search ended before we reached the end of the hierarchy due to
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800764 * {@link ToBooleanFunction#apply} returning true.
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800765 */
766 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700767 if (traverseTopToBottom) {
768 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800769 if (mChildren.get(i).forAllWindows(callback, traverseTopToBottom)) {
770 return true;
771 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700772 }
773 } else {
774 final int count = mChildren.size();
775 for (int i = 0; i < count; i++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800776 if (mChildren.get(i).forAllWindows(callback, traverseTopToBottom)) {
777 return true;
778 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700779 }
780 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800781 return false;
782 }
783
784 void forAllWindows(Consumer<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleee41d4a2016-11-21 08:41:10 -0800785 ForAllWindowsConsumerWrapper wrapper = obtainConsumerWrapper(callback);
786 forAllWindows(wrapper, traverseTopToBottom);
787 wrapper.release();
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700788 }
789
Jorim Jaggi51304d72017-05-17 17:25:32 +0200790 /**
791 * For all tasks at or below this container call the callback.
792 *
793 * @param callback Callback to be called for every task.
794 */
795 void forAllTasks(Consumer<Task> callback) {
796 for (int i = mChildren.size() - 1; i >= 0; --i) {
797 mChildren.get(i).forAllTasks(callback);
798 }
799 }
800
Wale Ogunwaled1880962016-11-08 10:31:59 -0800801 WindowState getWindow(Predicate<WindowState> callback) {
802 for (int i = mChildren.size() - 1; i >= 0; --i) {
803 final WindowState w = mChildren.get(i).getWindow(callback);
804 if (w != null) {
805 return w;
806 }
807 }
808
809 return null;
810 }
811
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700812 /**
Wale Ogunwalef6192862016-09-10 13:42:30 -0700813 * Returns 1, 0, or -1 depending on if this container is greater than, equal to, or lesser than
814 * the input container in terms of z-order.
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700815 */
816 @Override
817 public int compareTo(WindowContainer other) {
818 if (this == other) {
819 return 0;
820 }
821
822 if (mParent != null && mParent == other.mParent) {
Jorim Jaggi612bb882017-05-16 17:11:18 +0200823 final WindowList<WindowContainer> list = mParent.mChildren;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700824 return list.indexOf(this) > list.indexOf(other) ? 1 : -1;
825 }
826
827 final LinkedList<WindowContainer> thisParentChain = mTmpChain1;
828 final LinkedList<WindowContainer> otherParentChain = mTmpChain2;
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200829 try {
830 getParents(thisParentChain);
831 other.getParents(otherParentChain);
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700832
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200833 // Find the common ancestor of both containers.
834 WindowContainer commonAncestor = null;
835 WindowContainer thisTop = thisParentChain.peekLast();
836 WindowContainer otherTop = otherParentChain.peekLast();
837 while (thisTop != null && otherTop != null && thisTop == otherTop) {
838 commonAncestor = thisParentChain.removeLast();
839 otherParentChain.removeLast();
840 thisTop = thisParentChain.peekLast();
841 otherTop = otherParentChain.peekLast();
842 }
843
844 // Containers don't belong to the same hierarchy???
845 if (commonAncestor == null) {
846 throw new IllegalArgumentException("No in the same hierarchy this="
847 + thisParentChain + " other=" + otherParentChain);
848 }
849
850 // Children are always considered greater than their parents, so if one of the containers
851 // we are comparing it the parent of the other then whichever is the child is greater.
852 if (commonAncestor == this) {
853 return -1;
854 } else if (commonAncestor == other) {
855 return 1;
856 }
857
858 // The position of the first non-common ancestor in the common ancestor list determines
859 // which is greater the which.
860 final WindowList<WindowContainer> list = commonAncestor.mChildren;
861 return list.indexOf(thisParentChain.peekLast()) > list.indexOf(otherParentChain.peekLast())
862 ? 1 : -1;
863 } finally {
864 mTmpChain1.clear();
865 mTmpChain2.clear();
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700866 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700867 }
868
869 private void getParents(LinkedList<WindowContainer> parents) {
870 parents.clear();
871 WindowContainer current = this;
872 do {
873 parents.addLast(current);
874 current = current.mParent;
875 } while (current != null);
876 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700877
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800878 WindowContainerController getController() {
879 return mController;
880 }
881
882 void setController(WindowContainerController controller) {
883 if (mController != null && controller != null) {
884 throw new IllegalArgumentException("Can't set controller=" + mController
885 + " for container=" + this + " Already set to=" + mController);
886 }
887 if (controller != null) {
888 controller.setContainer(this);
889 } else if (mController != null) {
890 mController.setContainer(null);
891 }
892 mController = controller;
893 }
894
Robert Carrb1579c82017-09-05 14:54:47 -0700895 SurfaceControl.Builder makeSurface() {
896 final WindowContainer p = getParent();
897 return p.makeChildSurface(this);
898 }
899
Robert Carrf59b8dd2017-10-02 18:58:36 -0700900 /**
901 * @param child The WindowContainer this child surface is for, or null if the Surface
902 * is not assosciated with a WindowContainer (e.g. a surface used for Dimming).
903 */
Robert Carrb1579c82017-09-05 14:54:47 -0700904 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
905 final WindowContainer p = getParent();
906 // Give the parent a chance to set properties. In hierarchy v1 we rely
907 // on this to set full-screen dimensions on all our Surface-less Layers.
Robert Carree4d4b92017-11-22 12:21:46 -0800908 return p.makeChildSurface(child)
909 .setParent(mSurfaceControl);
Robert Carrb1579c82017-09-05 14:54:47 -0700910 }
911
Jorim Jaggia5e10572017-11-15 14:36:26 +0100912 @Override
913 public SurfaceControl getParentSurfaceControl() {
914 final WindowContainer parent = getParent();
915 if (parent == null) {
916 return null;
917 }
918 return parent.getSurfaceControl();
919 }
920
Robert Carrb1579c82017-09-05 14:54:47 -0700921 /**
922 * @return Whether this WindowContainer should be magnified by the accessibility magnifier.
923 */
924 boolean shouldMagnify() {
Yuki Awano4c36b552018-04-24 17:27:50 +0900925 if (mSurfaceControl == null) {
926 return false;
927 }
928
Robert Carrb1579c82017-09-05 14:54:47 -0700929 for (int i = 0; i < mChildren.size(); i++) {
930 if (!mChildren.get(i).shouldMagnify()) {
931 return false;
932 }
933 }
934 return true;
935 }
936
937 SurfaceSession getSession() {
938 if (getParent() != null) {
939 return getParent().getSession();
940 }
941 return null;
942 }
943
944 void assignLayer(Transaction t, int layer) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100945 final boolean changed = layer != mLastLayer || mLastRelativeToLayer != null;
946 if (mSurfaceControl != null && changed) {
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100947 setLayer(t, layer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100948 mLastLayer = layer;
949 mLastRelativeToLayer = null;
950 }
951 }
952
953 void assignRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) {
954 final boolean changed = layer != mLastLayer || mLastRelativeToLayer != relativeTo;
955 if (mSurfaceControl != null && changed) {
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100956 setRelativeLayer(t, relativeTo, layer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100957 mLastLayer = layer;
958 mLastRelativeToLayer = relativeTo;
Robert Carrb1579c82017-09-05 14:54:47 -0700959 }
960 }
961
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100962 protected void setLayer(Transaction t, int layer) {
963
964 // Route through surface animator to accommodate that our surface control might be
965 // attached to the leash, and leash is attached to parent container.
966 mSurfaceAnimator.setLayer(t, layer);
967 }
968
969 protected void setRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) {
970
971 // Route through surface animator to accommodate that our surface control might be
972 // attached to the leash, and leash is attached to parent container.
973 mSurfaceAnimator.setRelativeLayer(t, relativeTo, layer);
974 }
975
976 protected void reparentSurfaceControl(Transaction t, SurfaceControl newParent) {
977 mSurfaceAnimator.reparent(t, newParent);
978 }
979
Robert Carrb1579c82017-09-05 14:54:47 -0700980 void assignChildLayers(Transaction t) {
981 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -0700982
983 // We use two passes as a way to promote children which
984 // need Z-boosting to the end of the list.
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100985 for (int j = 0; j < mChildren.size(); ++j) {
986 final WindowContainer wc = mChildren.get(j);
987 wc.assignChildLayers(t);
988 if (!wc.needsZBoost()) {
989 wc.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -0700990 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100991 }
992 for (int j = 0; j < mChildren.size(); ++j) {
993 final WindowContainer wc = mChildren.get(j);
994 if (wc.needsZBoost()) {
995 wc.assignLayer(t, layer++);
996 }
Robert Carrb1579c82017-09-05 14:54:47 -0700997 }
998 }
999
1000 void assignChildLayers() {
Robert Carrf59b8dd2017-10-02 18:58:36 -07001001 assignChildLayers(getPendingTransaction());
Jorim Jaggiffe128d2017-11-30 13:54:36 +01001002 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07001003 }
1004
1005 boolean needsZBoost() {
1006 for (int i = 0; i < mChildren.size(); i++) {
1007 if (mChildren.get(i).needsZBoost()) {
1008 return true;
1009 }
1010 }
1011 return false;
1012 }
1013
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001014 /**
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001015 * Write to a protocol buffer output stream. Protocol buffer message definition is at
Yi Jin6c6e9ca2018-03-20 16:53:35 -07001016 * {@link com.android.server.wm.WindowContainerProto}.
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001017 *
Adrian Roos4921ccf2017-09-28 16:54:06 +02001018 * @param proto Stream to write the WindowContainer object to.
1019 * @param fieldId Field Id of the WindowContainer as defined in the parent message.
1020 * @param trim If true, reduce the amount of data written.
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001021 * @hide
1022 */
1023 @CallSuper
1024 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001025 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
1026 final long token = proto.start(fieldId);
1027 super.writeToProto(proto, CONFIGURATION_CONTAINER, trim);
1028 proto.write(ORIENTATION, mOrientation);
Wale Ogunwaledf262f52017-12-07 18:17:12 -08001029 proto.write(VISIBLE, isVisible());
Vishnu Nair04ab4392018-01-10 11:00:06 -08001030 mSurfaceAnimator.writeToProto(proto, SURFACE_ANIMATOR);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001031 proto.end(token);
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001032 }
1033
Wale Ogunwaleee41d4a2016-11-21 08:41:10 -08001034 private ForAllWindowsConsumerWrapper obtainConsumerWrapper(Consumer<WindowState> consumer) {
1035 ForAllWindowsConsumerWrapper wrapper = mConsumerWrapperPool.acquire();
1036 if (wrapper == null) {
1037 wrapper = new ForAllWindowsConsumerWrapper();
1038 }
1039 wrapper.setConsumer(consumer);
1040 return wrapper;
1041 }
1042
1043 private final class ForAllWindowsConsumerWrapper implements ToBooleanFunction<WindowState> {
1044
1045 private Consumer<WindowState> mConsumer;
1046
1047 void setConsumer(Consumer<WindowState> consumer) {
1048 mConsumer = consumer;
1049 }
1050
1051 @Override
1052 public boolean apply(WindowState w) {
1053 mConsumer.accept(w);
1054 return false;
1055 }
1056
1057 void release() {
1058 mConsumer = null;
1059 mConsumerWrapperPool.release(this);
1060 }
1061 }
Robert Carrb1579c82017-09-05 14:54:47 -07001062
1063 // TODO(b/68336570): Should this really be on WindowContainer since it
1064 // can only be used on the top-level nodes that aren't animated?
1065 // (otherwise we would be fighting other callers of setMatrix).
1066 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
1067 if (shouldMagnify()) {
1068 t.setMatrix(mSurfaceControl, spec.scale, 0, 0, spec.scale)
1069 .setPosition(mSurfaceControl, spec.offsetX, spec.offsetY);
1070 } else {
1071 for (int i = 0; i < mChildren.size(); i++) {
1072 mChildren.get(i).applyMagnificationSpec(t, spec);
1073 }
1074 }
1075 }
1076
1077 /**
1078 * TODO: Once we totally eliminate global transaction we will pass transaction in here
1079 * rather than merging to global.
1080 */
1081 void prepareSurfaces() {
Robert Carrf59b8dd2017-10-02 18:58:36 -07001082 SurfaceControl.mergeToGlobalTransaction(getPendingTransaction());
chaviw7f1fa992018-01-10 13:52:12 -08001083
1084 // If a leash has been set when the transaction was committed, then the leash reparent has
1085 // been committed.
1086 mCommittedReparentToAnimationLeash = mSurfaceAnimator.hasLeash();
Robert Carrb1579c82017-09-05 14:54:47 -07001087 for (int i = 0; i < mChildren.size(); i++) {
1088 mChildren.get(i).prepareSurfaces();
1089 }
1090 }
1091
1092 /**
chaviw7f1fa992018-01-10 13:52:12 -08001093 * @return true if the reparent to animation leash transaction has been committed, false
1094 * otherwise.
1095 */
1096 boolean hasCommittedReparentToAnimationLeash() {
1097 return mCommittedReparentToAnimationLeash;
1098 }
1099
1100 /**
Robert Carrb1579c82017-09-05 14:54:47 -07001101 * Trigger a call to prepareSurfaces from the animation thread, such that
1102 * mPendingTransaction will be applied.
1103 */
1104 void scheduleAnimation() {
1105 if (mParent != null) {
1106 mParent.scheduleAnimation();
1107 }
1108 }
1109
Jorim Jaggia5e10572017-11-15 14:36:26 +01001110 @Override
1111 public SurfaceControl getSurfaceControl() {
Robert Carrb1579c82017-09-05 14:54:47 -07001112 return mSurfaceControl;
1113 }
1114
Jorim Jaggia5e10572017-11-15 14:36:26 +01001115 @Override
1116 public Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -07001117 return mPendingTransaction;
1118 }
Jorim Jaggia5e10572017-11-15 14:36:26 +01001119
1120 /**
1121 * Starts an animation on the container.
1122 *
1123 * @param anim The animation to run.
1124 * @param hidden Whether our container is currently hidden. TODO This should use isVisible at
1125 * some point but the meaning is too weird to work for all containers.
1126 */
1127 void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden) {
1128 if (DEBUG_ANIM) Slog.v(TAG, "Starting animation on " + this + ": " + anim);
1129
1130 // TODO: This should use isVisible() but because isVisible has a really weird meaning at
1131 // the moment this doesn't work for all animatable window containers.
1132 mSurfaceAnimator.startAnimation(t, anim, hidden);
1133 }
1134
Jorim Jaggi980c9de2017-11-17 01:41:37 +01001135 void transferAnimation(WindowContainer from) {
1136 mSurfaceAnimator.transferAnimation(from.mSurfaceAnimator);
1137 }
1138
Jorim Jaggia5e10572017-11-15 14:36:26 +01001139 void cancelAnimation() {
1140 mSurfaceAnimator.cancelAnimation();
1141 }
1142
1143 @Override
1144 public Builder makeAnimationLeash() {
1145 return makeSurface();
1146 }
1147
Jorim Jaggi596a1992017-12-29 14:48:02 +01001148 @Override
1149 public SurfaceControl getAnimationLeashParent() {
1150 return getParentSurfaceControl();
1151 }
1152
chaviw23ee71c2017-12-18 11:29:41 -08001153 /**
1154 * @return The layer on which all app animations are happening.
1155 */
Jorim Jaggi391790622018-04-18 15:30:44 +02001156 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
chaviw23ee71c2017-12-18 11:29:41 -08001157 final WindowContainer parent = getParent();
1158 if (parent != null) {
Jorim Jaggi391790622018-04-18 15:30:44 +02001159 return parent.getAppAnimationLayer(animationLayer);
chaviw23ee71c2017-12-18 11:29:41 -08001160 }
1161 return null;
1162 }
1163
Jorim Jaggia5e10572017-11-15 14:36:26 +01001164 @Override
1165 public void commitPendingTransaction() {
1166 scheduleAnimation();
1167 }
1168
Robert Carr2f8aa392018-01-31 14:46:51 -08001169 void reassignLayer(Transaction t) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01001170 final WindowContainer parent = getParent();
1171 if (parent != null) {
1172 parent.assignChildLayers(t);
1173 }
1174 }
1175
1176 @Override
1177 public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
Robert Carrf12f9d32018-03-01 15:24:36 -08001178 mLastLayer = -1;
Jorim Jaggia5e10572017-11-15 14:36:26 +01001179 reassignLayer(t);
1180 }
1181
1182 @Override
1183 public void onAnimationLeashDestroyed(Transaction t) {
Robert Carrf12f9d32018-03-01 15:24:36 -08001184 mLastLayer = -1;
Jorim Jaggia5e10572017-11-15 14:36:26 +01001185 reassignLayer(t);
1186 }
1187
1188 /**
1189 * Called when an animation has finished running.
1190 */
1191 protected void onAnimationFinished() {
1192 }
1193
1194 /**
1195 * @return The currently running animation, if any, or {@code null} otherwise.
1196 */
1197 AnimationAdapter getAnimation() {
1198 return mSurfaceAnimator.getAnimation();
1199 }
1200
1201 /**
1202 * @see SurfaceAnimator#startDelayingAnimationStart
1203 */
1204 void startDelayingAnimationStart() {
1205 mSurfaceAnimator.startDelayingAnimationStart();
1206 }
1207
1208 /**
1209 * @see SurfaceAnimator#endDelayingAnimationStart
1210 */
1211 void endDelayingAnimationStart() {
1212 mSurfaceAnimator.endDelayingAnimationStart();
1213 }
1214
1215 @Override
1216 public int getSurfaceWidth() {
1217 return mSurfaceControl.getWidth();
1218 }
1219
1220 @Override
1221 public int getSurfaceHeight() {
1222 return mSurfaceControl.getHeight();
1223 }
1224
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001225 @CallSuper
Jorim Jaggia5e10572017-11-15 14:36:26 +01001226 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1227 if (mSurfaceAnimator.isAnimating()) {
1228 pw.print(prefix); pw.println("ContainerAnimator:");
1229 mSurfaceAnimator.dump(pw, prefix + " ");
1230 }
1231 }
chaviwe07246a2017-12-12 16:18:29 -08001232
chaviw2f0567b2018-01-29 16:22:02 -08001233 void updateSurfacePosition() {
chaviwe07246a2017-12-12 16:18:29 -08001234 if (mSurfaceControl == null) {
1235 return;
1236 }
1237
1238 getRelativePosition(mTmpPos);
chaviw3e751af2018-01-11 11:22:39 -08001239 if (mTmpPos.equals(mLastSurfacePosition)) {
1240 return;
1241 }
1242
chaviw2f0567b2018-01-29 16:22:02 -08001243 getPendingTransaction().setPosition(mSurfaceControl, mTmpPos.x, mTmpPos.y);
chaviw3e751af2018-01-11 11:22:39 -08001244 mLastSurfacePosition.set(mTmpPos.x, mTmpPos.y);
chaviwe07246a2017-12-12 16:18:29 -08001245 }
1246
1247 void getRelativePosition(Point outPos) {
1248 final Rect bounds = getBounds();
1249 outPos.set(bounds.left, bounds.top);
1250 final WindowContainer parent = getParent();
1251 if (parent != null) {
1252 final Rect parentBounds = parent.getBounds();
1253 outPos.offset(-parentBounds.left, -parentBounds.top);
1254 }
1255 }
chaviw2fb06bc2018-01-19 17:09:15 -08001256
1257 Dimmer getDimmer() {
1258 if (mParent == null) {
1259 return null;
1260 }
1261 return mParent.getDimmer();
1262 }
Wale Ogunwaled63594a2016-07-18 07:48:30 -07001263}