blob: e7547bf04c08bd8baa1dbbc56179bf1b997880e3 [file] [log] [blame]
Wale Ogunwale1666e312016-12-16 11:27:18 -08001/*
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
Wale Ogunwale1666e312016-12-16 11:27:18 -080019import android.content.res.Configuration;
20import android.graphics.Rect;
21import android.os.Handler;
22import android.os.Looper;
23import android.os.Message;
24import android.util.Slog;
25import android.util.SparseArray;
Winson Chungbdc646f2017-02-13 12:12:22 -080026import android.view.DisplayInfo;
27
Wale Ogunwale1666e312016-12-16 11:27:18 -080028import com.android.internal.annotations.VisibleForTesting;
29
30import java.lang.ref.WeakReference;
Wale Ogunwale1666e312016-12-16 11:27:18 -080031
32import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
33import static com.android.server.wm.WindowContainer.POSITION_TOP;
34import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
35import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
36
37/**
38 * Controller for the stack container. This is created by activity manager to link activity stacks
39 * to the stack container they use in window manager.
40 *
41 * Test class: {@link StackWindowControllerTests}
42 */
43public class StackWindowController
44 extends WindowContainerController<TaskStack, StackWindowListener> {
45
Wale Ogunwale61911492017-10-11 08:50:50 -070046 private final int mStackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -080047
48 private final H mHandler;
49
Winson Chungbdc646f2017-02-13 12:12:22 -080050 // Temp bounds only used in adjustConfigurationForBounds()
51 private final Rect mTmpRect = new Rect();
52 private final Rect mTmpStableInsets = new Rect();
53 private final Rect mTmpNonDecorInsets = new Rect();
54 private final Rect mTmpDisplayBounds = new Rect();
55
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070056 public StackWindowController(int stackId, StackWindowListener listener, int displayId,
57 boolean onTop, Rect outBounds) {
58 this(stackId, listener, displayId, onTop, outBounds, WindowManagerService.getInstance());
Wale Ogunwale1666e312016-12-16 11:27:18 -080059 }
60
61 @VisibleForTesting
62 public StackWindowController(int stackId, StackWindowListener listener,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070063 int displayId, boolean onTop, Rect outBounds, WindowManagerService service) {
Wale Ogunwale1666e312016-12-16 11:27:18 -080064 super(listener, service);
65 mStackId = stackId;
66 mHandler = new H(new WeakReference<>(this), service.mH.getLooper());
67
68 synchronized (mWindowMap) {
69 final DisplayContent dc = mRoot.getDisplayContent(displayId);
70 if (dc == null) {
71 throw new IllegalArgumentException("Trying to add stackId=" + stackId
72 + " to unknown displayId=" + displayId);
73 }
74
Wale Ogunwale61911492017-10-11 08:50:50 -070075 dc.createStack(stackId, onTop, this);
Wale Ogunwale1666e312016-12-16 11:27:18 -080076 getRawBounds(outBounds);
77 }
78 }
79
80 @Override
81 public void removeContainer() {
82 synchronized (mWindowMap) {
83 if (mContainer != null) {
84 mContainer.removeIfPossible();
85 super.removeContainer();
86 }
87 }
88 }
89
Andrii Kulian51c1b672017-04-07 18:39:32 -070090 public void reparent(int displayId, Rect outStackBounds, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -080091 synchronized (mWindowMap) {
92 if (mContainer == null) {
93 throw new IllegalArgumentException("Trying to move unknown stackId=" + mStackId
94 + " to displayId=" + displayId);
95 }
96
97 final DisplayContent targetDc = mRoot.getDisplayContent(displayId);
98 if (targetDc == null) {
99 throw new IllegalArgumentException("Trying to move stackId=" + mStackId
100 + " to unknown displayId=" + displayId);
101 }
102
Andrii Kulian51c1b672017-04-07 18:39:32 -0700103 targetDc.moveStackToDisplay(mContainer, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800104 getRawBounds(outStackBounds);
105 }
106 }
107
Bryce Leef3c6a472017-11-14 14:53:06 -0800108 public void positionChildAt(TaskWindowContainerController child, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800109 synchronized (mWindowMap) {
110 if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning task=" + child
111 + " at " + position);
112 if (child.mContainer == null) {
113 if (DEBUG_STACK) Slog.i(TAG_WM,
114 "positionChildAt: could not find task=" + this);
115 return;
116 }
117 if (mContainer == null) {
118 if (DEBUG_STACK) Slog.i(TAG_WM,
119 "positionChildAt: could not find stack for task=" + mContainer);
120 return;
121 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800122 child.mContainer.positionAt(position);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800123 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
124 }
125 }
126
127 public void positionChildAtTop(TaskWindowContainerController child, boolean includingParents) {
128 if (child == null) {
129 // TODO: Fix the call-points that cause this to happen.
130 return;
131 }
132
133 synchronized(mWindowMap) {
134 final Task childTask = child.mContainer;
135 if (childTask == null) {
136 Slog.e(TAG_WM, "positionChildAtTop: task=" + child + " not found");
137 return;
138 }
139 mContainer.positionChildAt(POSITION_TOP, childTask, includingParents);
140
141 if (mService.mAppTransition.isTransitionSet()) {
142 childTask.setSendingToBottom(false);
143 }
144 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
145 }
146 }
147
Wale Ogunwale66e16852017-10-19 13:35:52 -0700148 public void positionChildAtBottom(TaskWindowContainerController child,
149 boolean includingParents) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800150 if (child == null) {
151 // TODO: Fix the call-points that cause this to happen.
152 return;
153 }
154
155 synchronized(mWindowMap) {
156 final Task childTask = child.mContainer;
157 if (childTask == null) {
158 Slog.e(TAG_WM, "positionChildAtBottom: task=" + child + " not found");
159 return;
160 }
Wale Ogunwale66e16852017-10-19 13:35:52 -0700161 mContainer.positionChildAt(POSITION_BOTTOM, childTask, includingParents);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800162
163 if (mService.mAppTransition.isTransitionSet()) {
164 childTask.setSendingToBottom(true);
165 }
166 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
167 }
168 }
169
170 /**
171 * Re-sizes a stack and its containing tasks.
172 *
173 * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800174 * @param taskBounds Bounds for tasks in the resized stack, keyed by task id.
Bryce Leef3c6a472017-11-14 14:53:06 -0800175 * @param taskTempInsetBounds Inset bounds for individual tasks, keyed by task id.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800176 */
Bryce Leef3c6a472017-11-14 14:53:06 -0800177 public void resize(Rect bounds, SparseArray<Rect> taskBounds,
178 SparseArray<Rect> taskTempInsetBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800179 synchronized (mWindowMap) {
180 if (mContainer == null) {
181 throw new IllegalArgumentException("resizeStack: stack " + this + " not found.");
182 }
183 // We might trigger a configuration change. Save the current task bounds for freezing.
184 mContainer.prepareFreezingTaskBounds();
Bryce Leef3c6a472017-11-14 14:53:06 -0800185 if (mContainer.setBounds(bounds, taskBounds, taskTempInsetBounds)
Wale Ogunwale1666e312016-12-16 11:27:18 -0800186 && mContainer.isVisible()) {
187 mContainer.getDisplayContent().setLayoutNeeded();
188 mService.mWindowPlacerLocked.performSurfacePlacement();
189 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800190 }
191 }
192
Matthew Ngaa2b6202017-02-10 14:48:21 -0800193 /**
194 * @see TaskStack.getStackDockedModeBoundsLocked(Rect, Rect, Rect, boolean)
195 */
196 public void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
197 Rect outTempTaskBounds, boolean ignoreVisibility) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800198 synchronized (mWindowMap) {
199 if (mContainer != null) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800200 mContainer.getStackDockedModeBoundsLocked(currentTempTaskBounds, outStackBounds,
201 outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800202 return;
203 }
Matthew Ngaa2b6202017-02-10 14:48:21 -0800204 outStackBounds.setEmpty();
205 outTempTaskBounds.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800206 }
207 }
208
209 public void prepareFreezingTaskBounds() {
210 synchronized (mWindowMap) {
211 if (mContainer == null) {
212 throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this
213 + " not found.");
214 }
215 mContainer.prepareFreezingTaskBounds();
216 }
217 }
218
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800219 public void getRawBounds(Rect outBounds) {
220 synchronized (mWindowMap) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800221 if (mContainer.matchParentBounds()) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800222 outBounds.setEmpty();
223 } else {
224 mContainer.getRawBounds(outBounds);
225 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800226 }
227 }
228
229 public void getBounds(Rect outBounds) {
230 synchronized (mWindowMap) {
231 if (mContainer != null) {
232 mContainer.getBounds(outBounds);
233 return;
234 }
235 outBounds.setEmpty();
236 }
237 }
238
Matthew Ngaa2b6202017-02-10 14:48:21 -0800239 public void getBoundsForNewConfiguration(Rect outBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800240 synchronized(mWindowMap) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800241 mContainer.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800242 }
243 }
244
Winson Chungbdc646f2017-02-13 12:12:22 -0800245 /**
246 * Adjusts the screen size in dp's for the {@param config} for the given params.
247 */
248 public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds,
249 Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth,
250 boolean overrideHeight, float density, Configuration config,
251 Configuration parentConfig) {
252 synchronized (mWindowMap) {
253 final TaskStack stack = mContainer;
254 final DisplayContent displayContent = stack.getDisplayContent();
255 final DisplayInfo di = displayContent.getDisplayInfo();
256
257 // Get the insets and display bounds
258 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
259 mTmpStableInsets);
260 mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
261 mTmpNonDecorInsets);
262 mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight);
263
264 int width;
265 int height;
Bryce Lee7566d762017-03-30 09:34:15 -0700266
Wale Ogunwale822e5122017-07-26 06:02:24 -0700267 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
Bryce Lee7566d762017-03-30 09:34:15 -0700268
Bryce Leef3c6a472017-11-14 14:53:06 -0800269 config.windowConfiguration.setBounds(bounds);
Wale Ogunwale822e5122017-07-26 06:02:24 -0700270 config.windowConfiguration.setAppBounds(!bounds.isEmpty() ? bounds : null);
Bryce Lee7566d762017-03-30 09:34:15 -0700271 boolean intersectParentBounds = false;
272
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700273 if (stack.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800274 // Floating tasks should not be resized to the screen's bounds.
275
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700276 if (stack.inPinnedWindowingMode()
277 && bounds.width() == mTmpDisplayBounds.width()
278 && bounds.height() == mTmpDisplayBounds.height()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800279 // If the bounds we are animating is the same as the fullscreen stack
280 // dimensions, then apply the same inset calculations that we normally do for
281 // the fullscreen stack, without intersecting it with the display bounds
282 stableBounds.inset(mTmpStableInsets);
283 nonDecorBounds.inset(mTmpNonDecorInsets);
Andrii Kulian1893a612017-05-04 14:45:09 -0700284 // Move app bounds to zero to apply intersection with parent correctly. They are
285 // used only for evaluating width and height, so it's OK to move them around.
Wale Ogunwale822e5122017-07-26 06:02:24 -0700286 config.windowConfiguration.getAppBounds().offsetTo(0, 0);
Bryce Lee7566d762017-03-30 09:34:15 -0700287 intersectParentBounds = true;
Winson Chungbdc646f2017-02-13 12:12:22 -0800288 }
289 width = (int) (stableBounds.width() / density);
290 height = (int) (stableBounds.height() / density);
291 } else {
292 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
293 // area, i.e. the screen area without the system bars.
294 // Additionally task dimensions should not be bigger than its parents dimensions.
295 // The non decor inset are areas that could never be removed in Honeycomb. See
296 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
297 intersectDisplayBoundsExcludeInsets(nonDecorBounds,
298 insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets,
299 mTmpDisplayBounds, overrideWidth, overrideHeight);
300 intersectDisplayBoundsExcludeInsets(stableBounds,
301 insetBounds != null ? insetBounds : bounds, mTmpStableInsets,
302 mTmpDisplayBounds, overrideWidth, overrideHeight);
303 width = Math.min((int) (stableBounds.width() / density),
304 parentConfig.screenWidthDp);
305 height = Math.min((int) (stableBounds.height() / density),
306 parentConfig.screenHeightDp);
Bryce Lee7566d762017-03-30 09:34:15 -0700307 intersectParentBounds = true;
308 }
309
Wale Ogunwale822e5122017-07-26 06:02:24 -0700310 if (intersectParentBounds && config.windowConfiguration.getAppBounds() != null) {
311 config.windowConfiguration.getAppBounds().intersect(parentAppBounds);
Winson Chungbdc646f2017-02-13 12:12:22 -0800312 }
313
314 config.screenWidthDp = width;
315 config.screenHeightDp = height;
316 config.smallestScreenWidthDp = getSmallestWidthForTaskBounds(
317 insetBounds != null ? insetBounds : bounds, density);
318 }
319 }
320
321 /**
322 * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable
323 * inset areas.
324 *
325 * @param inOutBounds The inOutBounds to subtract the stable inset areas from.
326 */
327 private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds,
328 Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) {
329 mTmpRect.set(inInsetBounds);
330 mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect);
331 int leftInset = mTmpRect.left - inInsetBounds.left;
332 int topInset = mTmpRect.top - inInsetBounds.top;
333 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right;
334 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom;
335 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
336 }
337
338 /**
339 * Calculates the smallest width for a task given the {@param bounds}.
340 *
341 * @return the smallest width to be used in the Configuration, in dips
342 */
343 private int getSmallestWidthForTaskBounds(Rect bounds, float density) {
344 final DisplayContent displayContent = mContainer.getDisplayContent();
345 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
346
347 if (bounds == null || (bounds.width() == displayInfo.logicalWidth &&
348 bounds.height() == displayInfo.logicalHeight)) {
349 // If the bounds are fullscreen, return the value of the fullscreen configuration
350 return displayContent.getConfiguration().smallestScreenWidthDp;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700351 } else if (mContainer.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800352 // For floating tasks, calculate the smallest width from the bounds of the task
353 return (int) (Math.min(bounds.width(), bounds.height()) / density);
354 } else {
355 // Iterating across all screen orientations, and return the minimum of the task
356 // width taking into account that the bounds might change because the snap algorithm
357 // snaps to a different value
358 return displayContent.getDockedDividerController()
359 .getSmallestWidthDpForBounds(bounds);
360 }
361 }
362
Wale Ogunwale1666e312016-12-16 11:27:18 -0800363 void requestResize(Rect bounds) {
364 mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget();
365 }
366
367 @Override
368 public String toString() {
369 return "{StackWindowController stackId=" + mStackId + "}";
370 }
371
372 private static final class H extends Handler {
373
374 static final int REQUEST_RESIZE = 0;
375
376 private final WeakReference<StackWindowController> mController;
377
378 H(WeakReference<StackWindowController> controller, Looper looper) {
379 super(looper);
380 mController = controller;
381 }
382
383 @Override
384 public void handleMessage(Message msg) {
385 final StackWindowController controller = mController.get();
386 final StackWindowListener listener = (controller != null)
387 ? controller.mListener : null;
388 if (listener == null) {
389 return;
390 }
391 switch (msg.what) {
392 case REQUEST_RESIZE:
393 listener.requestResize((Rect) msg.obj);
394 break;
395 }
396 }
397 }
398}