Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [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 | |
Andrii Kulian | 1893a61 | 2017-05-04 14:45:09 -0700 | [diff] [blame] | 19 | import static android.app.ActivityManager.StackId.PINNED_STACK_ID; |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 20 | |
| 21 | import android.app.ActivityManager.StackId; |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 22 | import android.content.res.Configuration; |
| 23 | import android.graphics.Rect; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Message; |
| 27 | import android.util.Slog; |
| 28 | import android.util.SparseArray; |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 29 | import android.view.DisplayInfo; |
| 30 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 31 | import com.android.internal.annotations.VisibleForTesting; |
| 32 | |
| 33 | import java.lang.ref.WeakReference; |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 34 | |
| 35 | import static com.android.server.wm.WindowContainer.POSITION_BOTTOM; |
| 36 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
| 37 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK; |
| 38 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 39 | |
| 40 | /** |
| 41 | * Controller for the stack container. This is created by activity manager to link activity stacks |
| 42 | * to the stack container they use in window manager. |
| 43 | * |
| 44 | * Test class: {@link StackWindowControllerTests} |
| 45 | */ |
| 46 | public class StackWindowController |
| 47 | extends WindowContainerController<TaskStack, StackWindowListener> { |
| 48 | |
| 49 | final int mStackId; |
| 50 | |
| 51 | private final H mHandler; |
| 52 | |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 53 | // Temp bounds only used in adjustConfigurationForBounds() |
| 54 | private final Rect mTmpRect = new Rect(); |
| 55 | private final Rect mTmpStableInsets = new Rect(); |
| 56 | private final Rect mTmpNonDecorInsets = new Rect(); |
| 57 | private final Rect mTmpDisplayBounds = new Rect(); |
| 58 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 59 | public StackWindowController(int stackId, StackWindowListener listener, |
| 60 | int displayId, boolean onTop, Rect outBounds) { |
| 61 | this(stackId, listener, displayId, onTop, outBounds, WindowManagerService.getInstance()); |
| 62 | } |
| 63 | |
| 64 | @VisibleForTesting |
| 65 | public StackWindowController(int stackId, StackWindowListener listener, |
| 66 | int displayId, boolean onTop, Rect outBounds, WindowManagerService service) { |
| 67 | super(listener, service); |
| 68 | mStackId = stackId; |
| 69 | mHandler = new H(new WeakReference<>(this), service.mH.getLooper()); |
| 70 | |
| 71 | synchronized (mWindowMap) { |
| 72 | final DisplayContent dc = mRoot.getDisplayContent(displayId); |
| 73 | if (dc == null) { |
| 74 | throw new IllegalArgumentException("Trying to add stackId=" + stackId |
| 75 | + " to unknown displayId=" + displayId); |
| 76 | } |
| 77 | |
| 78 | final TaskStack stack = dc.addStackToDisplay(stackId, onTop); |
| 79 | stack.setController(this); |
| 80 | getRawBounds(outBounds); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void removeContainer() { |
| 86 | synchronized (mWindowMap) { |
| 87 | if (mContainer != null) { |
| 88 | mContainer.removeIfPossible(); |
| 89 | super.removeContainer(); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Wale Ogunwale | cd501ec | 2017-04-07 08:53:41 -0700 | [diff] [blame] | 94 | public boolean isVisible() { |
| 95 | synchronized (mWindowMap) { |
| 96 | return mContainer != null && mContainer.isVisible(); |
| 97 | } |
| 98 | } |
| 99 | |
Andrii Kulian | 51c1b67 | 2017-04-07 18:39:32 -0700 | [diff] [blame] | 100 | public void reparent(int displayId, Rect outStackBounds, boolean onTop) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 101 | synchronized (mWindowMap) { |
| 102 | if (mContainer == null) { |
| 103 | throw new IllegalArgumentException("Trying to move unknown stackId=" + mStackId |
| 104 | + " to displayId=" + displayId); |
| 105 | } |
| 106 | |
| 107 | final DisplayContent targetDc = mRoot.getDisplayContent(displayId); |
| 108 | if (targetDc == null) { |
| 109 | throw new IllegalArgumentException("Trying to move stackId=" + mStackId |
| 110 | + " to unknown displayId=" + displayId); |
| 111 | } |
| 112 | |
Andrii Kulian | 51c1b67 | 2017-04-07 18:39:32 -0700 | [diff] [blame] | 113 | targetDc.moveStackToDisplay(mContainer, onTop); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 114 | getRawBounds(outStackBounds); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public void positionChildAt(TaskWindowContainerController child, int position, Rect bounds, |
| 119 | Configuration overrideConfig) { |
| 120 | synchronized (mWindowMap) { |
| 121 | if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning task=" + child |
| 122 | + " at " + position); |
| 123 | if (child.mContainer == null) { |
| 124 | if (DEBUG_STACK) Slog.i(TAG_WM, |
| 125 | "positionChildAt: could not find task=" + this); |
| 126 | return; |
| 127 | } |
| 128 | if (mContainer == null) { |
| 129 | if (DEBUG_STACK) Slog.i(TAG_WM, |
| 130 | "positionChildAt: could not find stack for task=" + mContainer); |
| 131 | return; |
| 132 | } |
| 133 | child.mContainer.positionAt(position, bounds, overrideConfig); |
| 134 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public void positionChildAtTop(TaskWindowContainerController child, boolean includingParents) { |
| 139 | if (child == null) { |
| 140 | // TODO: Fix the call-points that cause this to happen. |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | synchronized(mWindowMap) { |
| 145 | final Task childTask = child.mContainer; |
| 146 | if (childTask == null) { |
| 147 | Slog.e(TAG_WM, "positionChildAtTop: task=" + child + " not found"); |
| 148 | return; |
| 149 | } |
| 150 | mContainer.positionChildAt(POSITION_TOP, childTask, includingParents); |
| 151 | |
| 152 | if (mService.mAppTransition.isTransitionSet()) { |
| 153 | childTask.setSendingToBottom(false); |
| 154 | } |
| 155 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public void positionChildAtBottom(TaskWindowContainerController child) { |
| 160 | if (child == null) { |
| 161 | // TODO: Fix the call-points that cause this to happen. |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | synchronized(mWindowMap) { |
| 166 | final Task childTask = child.mContainer; |
| 167 | if (childTask == null) { |
| 168 | Slog.e(TAG_WM, "positionChildAtBottom: task=" + child + " not found"); |
| 169 | return; |
| 170 | } |
| 171 | mContainer.positionChildAt(POSITION_BOTTOM, childTask, false /* includingParents */); |
| 172 | |
| 173 | if (mService.mAppTransition.isTransitionSet()) { |
| 174 | childTask.setSendingToBottom(true); |
| 175 | } |
| 176 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Re-sizes a stack and its containing tasks. |
| 182 | * |
| 183 | * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen. |
| 184 | * @param configs Configurations for tasks in the resized stack, keyed by task id. |
| 185 | * @param taskBounds Bounds for tasks in the resized stack, keyed by task id. |
| 186 | * @return True if the stack is now fullscreen. |
| 187 | */ |
| 188 | public boolean resize(Rect bounds, SparseArray<Configuration> configs, |
| 189 | SparseArray<Rect> taskBounds, SparseArray<Rect> taskTempInsetBounds) { |
| 190 | synchronized (mWindowMap) { |
| 191 | if (mContainer == null) { |
| 192 | throw new IllegalArgumentException("resizeStack: stack " + this + " not found."); |
| 193 | } |
| 194 | // We might trigger a configuration change. Save the current task bounds for freezing. |
| 195 | mContainer.prepareFreezingTaskBounds(); |
| 196 | if (mContainer.setBounds(bounds, configs, taskBounds, taskTempInsetBounds) |
| 197 | && mContainer.isVisible()) { |
| 198 | mContainer.getDisplayContent().setLayoutNeeded(); |
| 199 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 200 | } |
| 201 | return mContainer.getRawFullscreen(); |
| 202 | } |
| 203 | } |
| 204 | |
Matthew Ng | aa2b620 | 2017-02-10 14:48:21 -0800 | [diff] [blame] | 205 | /** |
| 206 | * @see TaskStack.getStackDockedModeBoundsLocked(Rect, Rect, Rect, boolean) |
| 207 | */ |
| 208 | public void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds, |
| 209 | Rect outTempTaskBounds, boolean ignoreVisibility) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 210 | synchronized (mWindowMap) { |
| 211 | if (mContainer != null) { |
Matthew Ng | aa2b620 | 2017-02-10 14:48:21 -0800 | [diff] [blame] | 212 | mContainer.getStackDockedModeBoundsLocked(currentTempTaskBounds, outStackBounds, |
| 213 | outTempTaskBounds, ignoreVisibility); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 214 | return; |
| 215 | } |
Matthew Ng | aa2b620 | 2017-02-10 14:48:21 -0800 | [diff] [blame] | 216 | outStackBounds.setEmpty(); |
| 217 | outTempTaskBounds.setEmpty(); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | public void prepareFreezingTaskBounds() { |
| 222 | synchronized (mWindowMap) { |
| 223 | if (mContainer == null) { |
| 224 | throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this |
| 225 | + " not found."); |
| 226 | } |
| 227 | mContainer.prepareFreezingTaskBounds(); |
| 228 | } |
| 229 | } |
| 230 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 231 | private void getRawBounds(Rect outBounds) { |
| 232 | if (mContainer.getRawFullscreen()) { |
| 233 | outBounds.setEmpty(); |
| 234 | } else { |
| 235 | mContainer.getRawBounds(outBounds); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public void getBounds(Rect outBounds) { |
| 240 | synchronized (mWindowMap) { |
| 241 | if (mContainer != null) { |
| 242 | mContainer.getBounds(outBounds); |
| 243 | return; |
| 244 | } |
| 245 | outBounds.setEmpty(); |
| 246 | } |
| 247 | } |
| 248 | |
Matthew Ng | aa2b620 | 2017-02-10 14:48:21 -0800 | [diff] [blame] | 249 | public void getBoundsForNewConfiguration(Rect outBounds) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 250 | synchronized(mWindowMap) { |
Matthew Ng | aa2b620 | 2017-02-10 14:48:21 -0800 | [diff] [blame] | 251 | mContainer.getBoundsForNewConfiguration(outBounds); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Adjusts the screen size in dp's for the {@param config} for the given params. |
| 257 | */ |
| 258 | public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds, |
| 259 | Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth, |
| 260 | boolean overrideHeight, float density, Configuration config, |
| 261 | Configuration parentConfig) { |
| 262 | synchronized (mWindowMap) { |
| 263 | final TaskStack stack = mContainer; |
| 264 | final DisplayContent displayContent = stack.getDisplayContent(); |
| 265 | final DisplayInfo di = displayContent.getDisplayInfo(); |
| 266 | |
| 267 | // Get the insets and display bounds |
| 268 | mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight, |
| 269 | mTmpStableInsets); |
| 270 | mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight, |
| 271 | mTmpNonDecorInsets); |
| 272 | mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight); |
| 273 | |
| 274 | int width; |
| 275 | int height; |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 276 | |
Wale Ogunwale | 822e512 | 2017-07-26 06:02:24 -0700 | [diff] [blame^] | 277 | final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds(); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 278 | |
Wale Ogunwale | 822e512 | 2017-07-26 06:02:24 -0700 | [diff] [blame^] | 279 | config.windowConfiguration.setAppBounds(!bounds.isEmpty() ? bounds : null); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 280 | boolean intersectParentBounds = false; |
| 281 | |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 282 | if (StackId.tasksAreFloating(mStackId)) { |
| 283 | // Floating tasks should not be resized to the screen's bounds. |
| 284 | |
Andrii Kulian | 1893a61 | 2017-05-04 14:45:09 -0700 | [diff] [blame] | 285 | if (mStackId == PINNED_STACK_ID && bounds.width() == mTmpDisplayBounds.width() && |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 286 | bounds.height() == mTmpDisplayBounds.height()) { |
| 287 | // If the bounds we are animating is the same as the fullscreen stack |
| 288 | // dimensions, then apply the same inset calculations that we normally do for |
| 289 | // the fullscreen stack, without intersecting it with the display bounds |
| 290 | stableBounds.inset(mTmpStableInsets); |
| 291 | nonDecorBounds.inset(mTmpNonDecorInsets); |
Andrii Kulian | 1893a61 | 2017-05-04 14:45:09 -0700 | [diff] [blame] | 292 | // Move app bounds to zero to apply intersection with parent correctly. They are |
| 293 | // used only for evaluating width and height, so it's OK to move them around. |
Wale Ogunwale | 822e512 | 2017-07-26 06:02:24 -0700 | [diff] [blame^] | 294 | config.windowConfiguration.getAppBounds().offsetTo(0, 0); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 295 | intersectParentBounds = true; |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 296 | } |
| 297 | width = (int) (stableBounds.width() / density); |
| 298 | height = (int) (stableBounds.height() / density); |
| 299 | } else { |
| 300 | // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen |
| 301 | // area, i.e. the screen area without the system bars. |
| 302 | // Additionally task dimensions should not be bigger than its parents dimensions. |
| 303 | // The non decor inset are areas that could never be removed in Honeycomb. See |
| 304 | // {@link WindowManagerPolicy#getNonDecorInsetsLw}. |
| 305 | intersectDisplayBoundsExcludeInsets(nonDecorBounds, |
| 306 | insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets, |
| 307 | mTmpDisplayBounds, overrideWidth, overrideHeight); |
| 308 | intersectDisplayBoundsExcludeInsets(stableBounds, |
| 309 | insetBounds != null ? insetBounds : bounds, mTmpStableInsets, |
| 310 | mTmpDisplayBounds, overrideWidth, overrideHeight); |
| 311 | width = Math.min((int) (stableBounds.width() / density), |
| 312 | parentConfig.screenWidthDp); |
| 313 | height = Math.min((int) (stableBounds.height() / density), |
| 314 | parentConfig.screenHeightDp); |
Bryce Lee | 7566d76 | 2017-03-30 09:34:15 -0700 | [diff] [blame] | 315 | intersectParentBounds = true; |
| 316 | } |
| 317 | |
Wale Ogunwale | 822e512 | 2017-07-26 06:02:24 -0700 | [diff] [blame^] | 318 | if (intersectParentBounds && config.windowConfiguration.getAppBounds() != null) { |
| 319 | config.windowConfiguration.getAppBounds().intersect(parentAppBounds); |
Winson Chung | bdc646f | 2017-02-13 12:12:22 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | config.screenWidthDp = width; |
| 323 | config.screenHeightDp = height; |
| 324 | config.smallestScreenWidthDp = getSmallestWidthForTaskBounds( |
| 325 | insetBounds != null ? insetBounds : bounds, density); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable |
| 331 | * inset areas. |
| 332 | * |
| 333 | * @param inOutBounds The inOutBounds to subtract the stable inset areas from. |
| 334 | */ |
| 335 | private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds, |
| 336 | Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) { |
| 337 | mTmpRect.set(inInsetBounds); |
| 338 | mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect); |
| 339 | int leftInset = mTmpRect.left - inInsetBounds.left; |
| 340 | int topInset = mTmpRect.top - inInsetBounds.top; |
| 341 | int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right; |
| 342 | int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom; |
| 343 | inOutBounds.inset(leftInset, topInset, rightInset, bottomInset); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Calculates the smallest width for a task given the {@param bounds}. |
| 348 | * |
| 349 | * @return the smallest width to be used in the Configuration, in dips |
| 350 | */ |
| 351 | private int getSmallestWidthForTaskBounds(Rect bounds, float density) { |
| 352 | final DisplayContent displayContent = mContainer.getDisplayContent(); |
| 353 | final DisplayInfo displayInfo = displayContent.getDisplayInfo(); |
| 354 | |
| 355 | if (bounds == null || (bounds.width() == displayInfo.logicalWidth && |
| 356 | bounds.height() == displayInfo.logicalHeight)) { |
| 357 | // If the bounds are fullscreen, return the value of the fullscreen configuration |
| 358 | return displayContent.getConfiguration().smallestScreenWidthDp; |
| 359 | } else if (StackId.tasksAreFloating(mStackId)) { |
| 360 | // For floating tasks, calculate the smallest width from the bounds of the task |
| 361 | return (int) (Math.min(bounds.width(), bounds.height()) / density); |
| 362 | } else { |
| 363 | // Iterating across all screen orientations, and return the minimum of the task |
| 364 | // width taking into account that the bounds might change because the snap algorithm |
| 365 | // snaps to a different value |
| 366 | return displayContent.getDockedDividerController() |
| 367 | .getSmallestWidthDpForBounds(bounds); |
| 368 | } |
| 369 | } |
| 370 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 371 | void requestResize(Rect bounds) { |
| 372 | mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget(); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public String toString() { |
| 377 | return "{StackWindowController stackId=" + mStackId + "}"; |
| 378 | } |
| 379 | |
| 380 | private static final class H extends Handler { |
| 381 | |
| 382 | static final int REQUEST_RESIZE = 0; |
| 383 | |
| 384 | private final WeakReference<StackWindowController> mController; |
| 385 | |
| 386 | H(WeakReference<StackWindowController> controller, Looper looper) { |
| 387 | super(looper); |
| 388 | mController = controller; |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void handleMessage(Message msg) { |
| 393 | final StackWindowController controller = mController.get(); |
| 394 | final StackWindowListener listener = (controller != null) |
| 395 | ? controller.mListener : null; |
| 396 | if (listener == null) { |
| 397 | return; |
| 398 | } |
| 399 | switch (msg.what) { |
| 400 | case REQUEST_RESIZE: |
| 401 | listener.requestResize((Rect) msg.obj); |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |