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