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