blob: 1fda832dd5be6a96f64a8aacd1571986255a4e85 [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
Wale Ogunwalecd501ec2017-04-07 08:53:41 -070090 public boolean isVisible() {
91 synchronized (mWindowMap) {
92 return mContainer != null && mContainer.isVisible();
93 }
94 }
95
Andrii Kulian51c1b672017-04-07 18:39:32 -070096 public void reparent(int displayId, Rect outStackBounds, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -080097 synchronized (mWindowMap) {
98 if (mContainer == null) {
99 throw new IllegalArgumentException("Trying to move unknown stackId=" + mStackId
100 + " to displayId=" + displayId);
101 }
102
103 final DisplayContent targetDc = mRoot.getDisplayContent(displayId);
104 if (targetDc == null) {
105 throw new IllegalArgumentException("Trying to move stackId=" + mStackId
106 + " to unknown displayId=" + displayId);
107 }
108
Andrii Kulian51c1b672017-04-07 18:39:32 -0700109 targetDc.moveStackToDisplay(mContainer, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800110 getRawBounds(outStackBounds);
111 }
112 }
113
114 public void positionChildAt(TaskWindowContainerController child, int position, Rect bounds,
115 Configuration overrideConfig) {
116 synchronized (mWindowMap) {
117 if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning task=" + child
118 + " at " + position);
119 if (child.mContainer == null) {
120 if (DEBUG_STACK) Slog.i(TAG_WM,
121 "positionChildAt: could not find task=" + this);
122 return;
123 }
124 if (mContainer == null) {
125 if (DEBUG_STACK) Slog.i(TAG_WM,
126 "positionChildAt: could not find stack for task=" + mContainer);
127 return;
128 }
129 child.mContainer.positionAt(position, bounds, overrideConfig);
130 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
131 }
132 }
133
134 public void positionChildAtTop(TaskWindowContainerController child, boolean includingParents) {
135 if (child == null) {
136 // TODO: Fix the call-points that cause this to happen.
137 return;
138 }
139
140 synchronized(mWindowMap) {
141 final Task childTask = child.mContainer;
142 if (childTask == null) {
143 Slog.e(TAG_WM, "positionChildAtTop: task=" + child + " not found");
144 return;
145 }
146 mContainer.positionChildAt(POSITION_TOP, childTask, includingParents);
147
148 if (mService.mAppTransition.isTransitionSet()) {
149 childTask.setSendingToBottom(false);
150 }
151 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
152 }
153 }
154
155 public void positionChildAtBottom(TaskWindowContainerController child) {
156 if (child == null) {
157 // TODO: Fix the call-points that cause this to happen.
158 return;
159 }
160
161 synchronized(mWindowMap) {
162 final Task childTask = child.mContainer;
163 if (childTask == null) {
164 Slog.e(TAG_WM, "positionChildAtBottom: task=" + child + " not found");
165 return;
166 }
167 mContainer.positionChildAt(POSITION_BOTTOM, childTask, false /* includingParents */);
168
169 if (mService.mAppTransition.isTransitionSet()) {
170 childTask.setSendingToBottom(true);
171 }
172 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
173 }
174 }
175
176 /**
177 * Re-sizes a stack and its containing tasks.
178 *
179 * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen.
180 * @param configs Configurations for tasks in the resized stack, keyed by task id.
181 * @param taskBounds Bounds for tasks in the resized stack, keyed by task id.
182 * @return True if the stack is now fullscreen.
183 */
184 public boolean resize(Rect bounds, SparseArray<Configuration> configs,
185 SparseArray<Rect> taskBounds, SparseArray<Rect> taskTempInsetBounds) {
186 synchronized (mWindowMap) {
187 if (mContainer == null) {
188 throw new IllegalArgumentException("resizeStack: stack " + this + " not found.");
189 }
190 // We might trigger a configuration change. Save the current task bounds for freezing.
191 mContainer.prepareFreezingTaskBounds();
192 if (mContainer.setBounds(bounds, configs, taskBounds, taskTempInsetBounds)
193 && mContainer.isVisible()) {
194 mContainer.getDisplayContent().setLayoutNeeded();
195 mService.mWindowPlacerLocked.performSurfacePlacement();
196 }
197 return mContainer.getRawFullscreen();
198 }
199 }
200
Matthew Ngaa2b6202017-02-10 14:48:21 -0800201 /**
202 * @see TaskStack.getStackDockedModeBoundsLocked(Rect, Rect, Rect, boolean)
203 */
204 public void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
205 Rect outTempTaskBounds, boolean ignoreVisibility) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800206 synchronized (mWindowMap) {
207 if (mContainer != null) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800208 mContainer.getStackDockedModeBoundsLocked(currentTempTaskBounds, outStackBounds,
209 outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800210 return;
211 }
Matthew Ngaa2b6202017-02-10 14:48:21 -0800212 outStackBounds.setEmpty();
213 outTempTaskBounds.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800214 }
215 }
216
217 public void prepareFreezingTaskBounds() {
218 synchronized (mWindowMap) {
219 if (mContainer == null) {
220 throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this
221 + " not found.");
222 }
223 mContainer.prepareFreezingTaskBounds();
224 }
225 }
226
Wale Ogunwale1666e312016-12-16 11:27:18 -0800227 private void getRawBounds(Rect outBounds) {
228 if (mContainer.getRawFullscreen()) {
229 outBounds.setEmpty();
230 } else {
231 mContainer.getRawBounds(outBounds);
232 }
233 }
234
235 public void getBounds(Rect outBounds) {
236 synchronized (mWindowMap) {
237 if (mContainer != null) {
238 mContainer.getBounds(outBounds);
239 return;
240 }
241 outBounds.setEmpty();
242 }
243 }
244
Matthew Ngaa2b6202017-02-10 14:48:21 -0800245 public void getBoundsForNewConfiguration(Rect outBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800246 synchronized(mWindowMap) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800247 mContainer.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800248 }
249 }
250
Winson Chungbdc646f2017-02-13 12:12:22 -0800251 /**
252 * Adjusts the screen size in dp's for the {@param config} for the given params.
253 */
254 public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds,
255 Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth,
256 boolean overrideHeight, float density, Configuration config,
257 Configuration parentConfig) {
258 synchronized (mWindowMap) {
259 final TaskStack stack = mContainer;
260 final DisplayContent displayContent = stack.getDisplayContent();
261 final DisplayInfo di = displayContent.getDisplayInfo();
262
263 // Get the insets and display bounds
264 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
265 mTmpStableInsets);
266 mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
267 mTmpNonDecorInsets);
268 mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight);
269
270 int width;
271 int height;
Bryce Lee7566d762017-03-30 09:34:15 -0700272
Wale Ogunwale822e5122017-07-26 06:02:24 -0700273 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
Bryce Lee7566d762017-03-30 09:34:15 -0700274
Wale Ogunwale822e5122017-07-26 06:02:24 -0700275 config.windowConfiguration.setAppBounds(!bounds.isEmpty() ? bounds : null);
Bryce Lee7566d762017-03-30 09:34:15 -0700276 boolean intersectParentBounds = false;
277
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700278 if (stack.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800279 // Floating tasks should not be resized to the screen's bounds.
280
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700281 if (stack.inPinnedWindowingMode()
282 && bounds.width() == mTmpDisplayBounds.width()
283 && bounds.height() == mTmpDisplayBounds.height()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800284 // If the bounds we are animating is the same as the fullscreen stack
285 // dimensions, then apply the same inset calculations that we normally do for
286 // the fullscreen stack, without intersecting it with the display bounds
287 stableBounds.inset(mTmpStableInsets);
288 nonDecorBounds.inset(mTmpNonDecorInsets);
Andrii Kulian1893a612017-05-04 14:45:09 -0700289 // Move app bounds to zero to apply intersection with parent correctly. They are
290 // used only for evaluating width and height, so it's OK to move them around.
Wale Ogunwale822e5122017-07-26 06:02:24 -0700291 config.windowConfiguration.getAppBounds().offsetTo(0, 0);
Bryce Lee7566d762017-03-30 09:34:15 -0700292 intersectParentBounds = true;
Winson Chungbdc646f2017-02-13 12:12:22 -0800293 }
294 width = (int) (stableBounds.width() / density);
295 height = (int) (stableBounds.height() / density);
296 } else {
297 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
298 // area, i.e. the screen area without the system bars.
299 // Additionally task dimensions should not be bigger than its parents dimensions.
300 // The non decor inset are areas that could never be removed in Honeycomb. See
301 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
302 intersectDisplayBoundsExcludeInsets(nonDecorBounds,
303 insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets,
304 mTmpDisplayBounds, overrideWidth, overrideHeight);
305 intersectDisplayBoundsExcludeInsets(stableBounds,
306 insetBounds != null ? insetBounds : bounds, mTmpStableInsets,
307 mTmpDisplayBounds, overrideWidth, overrideHeight);
308 width = Math.min((int) (stableBounds.width() / density),
309 parentConfig.screenWidthDp);
310 height = Math.min((int) (stableBounds.height() / density),
311 parentConfig.screenHeightDp);
Bryce Lee7566d762017-03-30 09:34:15 -0700312 intersectParentBounds = true;
313 }
314
Wale Ogunwale822e5122017-07-26 06:02:24 -0700315 if (intersectParentBounds && config.windowConfiguration.getAppBounds() != null) {
316 config.windowConfiguration.getAppBounds().intersect(parentAppBounds);
Winson Chungbdc646f2017-02-13 12:12:22 -0800317 }
318
319 config.screenWidthDp = width;
320 config.screenHeightDp = height;
321 config.smallestScreenWidthDp = getSmallestWidthForTaskBounds(
322 insetBounds != null ? insetBounds : bounds, density);
323 }
324 }
325
326 /**
327 * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable
328 * inset areas.
329 *
330 * @param inOutBounds The inOutBounds to subtract the stable inset areas from.
331 */
332 private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds,
333 Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) {
334 mTmpRect.set(inInsetBounds);
335 mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect);
336 int leftInset = mTmpRect.left - inInsetBounds.left;
337 int topInset = mTmpRect.top - inInsetBounds.top;
338 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right;
339 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom;
340 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
341 }
342
343 /**
344 * Calculates the smallest width for a task given the {@param bounds}.
345 *
346 * @return the smallest width to be used in the Configuration, in dips
347 */
348 private int getSmallestWidthForTaskBounds(Rect bounds, float density) {
349 final DisplayContent displayContent = mContainer.getDisplayContent();
350 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
351
352 if (bounds == null || (bounds.width() == displayInfo.logicalWidth &&
353 bounds.height() == displayInfo.logicalHeight)) {
354 // If the bounds are fullscreen, return the value of the fullscreen configuration
355 return displayContent.getConfiguration().smallestScreenWidthDp;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700356 } else if (mContainer.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800357 // For floating tasks, calculate the smallest width from the bounds of the task
358 return (int) (Math.min(bounds.width(), bounds.height()) / density);
359 } else {
360 // Iterating across all screen orientations, and return the minimum of the task
361 // width taking into account that the bounds might change because the snap algorithm
362 // snaps to a different value
363 return displayContent.getDockedDividerController()
364 .getSmallestWidthDpForBounds(bounds);
365 }
366 }
367
Wale Ogunwale1666e312016-12-16 11:27:18 -0800368 void requestResize(Rect bounds) {
369 mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget();
370 }
371
372 @Override
373 public String toString() {
374 return "{StackWindowController stackId=" + mStackId + "}";
375 }
376
377 private static final class H extends Handler {
378
379 static final int REQUEST_RESIZE = 0;
380
381 private final WeakReference<StackWindowController> mController;
382
383 H(WeakReference<StackWindowController> controller, Looper looper) {
384 super(looper);
385 mController = controller;
386 }
387
388 @Override
389 public void handleMessage(Message msg) {
390 final StackWindowController controller = mController.get();
391 final StackWindowListener listener = (controller != null)
392 ? controller.mListener : null;
393 if (listener == null) {
394 return;
395 }
396 switch (msg.what) {
397 case REQUEST_RESIZE:
398 listener.requestResize((Rect) msg.obj);
399 break;
400 }
401 }
402 }
403}