blob: aff1bc62563fc8d8b6082fb27700e877bdf0fbf4 [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
Wale Ogunwale66e16852017-10-19 13:35:52 -0700155 public void positionChildAtBottom(TaskWindowContainerController child,
156 boolean includingParents) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800157 if (child == null) {
158 // TODO: Fix the call-points that cause this to happen.
159 return;
160 }
161
162 synchronized(mWindowMap) {
163 final Task childTask = child.mContainer;
164 if (childTask == null) {
165 Slog.e(TAG_WM, "positionChildAtBottom: task=" + child + " not found");
166 return;
167 }
Wale Ogunwale66e16852017-10-19 13:35:52 -0700168 mContainer.positionChildAt(POSITION_BOTTOM, childTask, includingParents);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800169
170 if (mService.mAppTransition.isTransitionSet()) {
171 childTask.setSendingToBottom(true);
172 }
173 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
174 }
175 }
176
177 /**
178 * Re-sizes a stack and its containing tasks.
179 *
180 * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen.
181 * @param configs Configurations for tasks in the resized stack, keyed by task id.
182 * @param taskBounds Bounds for tasks in the resized stack, keyed by task id.
183 * @return True if the stack is now fullscreen.
184 */
185 public boolean resize(Rect bounds, SparseArray<Configuration> configs,
186 SparseArray<Rect> taskBounds, SparseArray<Rect> taskTempInsetBounds) {
187 synchronized (mWindowMap) {
188 if (mContainer == null) {
189 throw new IllegalArgumentException("resizeStack: stack " + this + " not found.");
190 }
191 // We might trigger a configuration change. Save the current task bounds for freezing.
192 mContainer.prepareFreezingTaskBounds();
193 if (mContainer.setBounds(bounds, configs, taskBounds, taskTempInsetBounds)
194 && mContainer.isVisible()) {
195 mContainer.getDisplayContent().setLayoutNeeded();
196 mService.mWindowPlacerLocked.performSurfacePlacement();
197 }
198 return mContainer.getRawFullscreen();
199 }
200 }
201
Matthew Ngaa2b6202017-02-10 14:48:21 -0800202 /**
203 * @see TaskStack.getStackDockedModeBoundsLocked(Rect, Rect, Rect, boolean)
204 */
205 public void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
206 Rect outTempTaskBounds, boolean ignoreVisibility) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800207 synchronized (mWindowMap) {
208 if (mContainer != null) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800209 mContainer.getStackDockedModeBoundsLocked(currentTempTaskBounds, outStackBounds,
210 outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800211 return;
212 }
Matthew Ngaa2b6202017-02-10 14:48:21 -0800213 outStackBounds.setEmpty();
214 outTempTaskBounds.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800215 }
216 }
217
218 public void prepareFreezingTaskBounds() {
219 synchronized (mWindowMap) {
220 if (mContainer == null) {
221 throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this
222 + " not found.");
223 }
224 mContainer.prepareFreezingTaskBounds();
225 }
226 }
227
Wale Ogunwale1666e312016-12-16 11:27:18 -0800228 private void getRawBounds(Rect outBounds) {
229 if (mContainer.getRawFullscreen()) {
230 outBounds.setEmpty();
231 } else {
232 mContainer.getRawBounds(outBounds);
233 }
234 }
235
236 public void getBounds(Rect outBounds) {
237 synchronized (mWindowMap) {
238 if (mContainer != null) {
239 mContainer.getBounds(outBounds);
240 return;
241 }
242 outBounds.setEmpty();
243 }
244 }
245
Matthew Ngaa2b6202017-02-10 14:48:21 -0800246 public void getBoundsForNewConfiguration(Rect outBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800247 synchronized(mWindowMap) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800248 mContainer.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800249 }
250 }
251
Winson Chungbdc646f2017-02-13 12:12:22 -0800252 /**
253 * Adjusts the screen size in dp's for the {@param config} for the given params.
254 */
255 public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds,
256 Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth,
257 boolean overrideHeight, float density, Configuration config,
258 Configuration parentConfig) {
259 synchronized (mWindowMap) {
260 final TaskStack stack = mContainer;
261 final DisplayContent displayContent = stack.getDisplayContent();
262 final DisplayInfo di = displayContent.getDisplayInfo();
263
264 // Get the insets and display bounds
265 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
266 mTmpStableInsets);
267 mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
268 mTmpNonDecorInsets);
269 mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight);
270
271 int width;
272 int height;
Bryce Lee7566d762017-03-30 09:34:15 -0700273
Wale Ogunwale822e5122017-07-26 06:02:24 -0700274 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
Bryce Lee7566d762017-03-30 09:34:15 -0700275
Wale Ogunwale822e5122017-07-26 06:02:24 -0700276 config.windowConfiguration.setAppBounds(!bounds.isEmpty() ? bounds : null);
Bryce Lee7566d762017-03-30 09:34:15 -0700277 boolean intersectParentBounds = false;
278
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700279 if (stack.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800280 // Floating tasks should not be resized to the screen's bounds.
281
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700282 if (stack.inPinnedWindowingMode()
283 && bounds.width() == mTmpDisplayBounds.width()
284 && bounds.height() == mTmpDisplayBounds.height()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800285 // If the bounds we are animating is the same as the fullscreen stack
286 // dimensions, then apply the same inset calculations that we normally do for
287 // the fullscreen stack, without intersecting it with the display bounds
288 stableBounds.inset(mTmpStableInsets);
289 nonDecorBounds.inset(mTmpNonDecorInsets);
Andrii Kulian1893a612017-05-04 14:45:09 -0700290 // Move app bounds to zero to apply intersection with parent correctly. They are
291 // used only for evaluating width and height, so it's OK to move them around.
Wale Ogunwale822e5122017-07-26 06:02:24 -0700292 config.windowConfiguration.getAppBounds().offsetTo(0, 0);
Bryce Lee7566d762017-03-30 09:34:15 -0700293 intersectParentBounds = true;
Winson Chungbdc646f2017-02-13 12:12:22 -0800294 }
295 width = (int) (stableBounds.width() / density);
296 height = (int) (stableBounds.height() / density);
297 } else {
298 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
299 // area, i.e. the screen area without the system bars.
300 // Additionally task dimensions should not be bigger than its parents dimensions.
301 // The non decor inset are areas that could never be removed in Honeycomb. See
302 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
303 intersectDisplayBoundsExcludeInsets(nonDecorBounds,
304 insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets,
305 mTmpDisplayBounds, overrideWidth, overrideHeight);
306 intersectDisplayBoundsExcludeInsets(stableBounds,
307 insetBounds != null ? insetBounds : bounds, mTmpStableInsets,
308 mTmpDisplayBounds, overrideWidth, overrideHeight);
309 width = Math.min((int) (stableBounds.width() / density),
310 parentConfig.screenWidthDp);
311 height = Math.min((int) (stableBounds.height() / density),
312 parentConfig.screenHeightDp);
Bryce Lee7566d762017-03-30 09:34:15 -0700313 intersectParentBounds = true;
314 }
315
Wale Ogunwale822e5122017-07-26 06:02:24 -0700316 if (intersectParentBounds && config.windowConfiguration.getAppBounds() != null) {
317 config.windowConfiguration.getAppBounds().intersect(parentAppBounds);
Winson Chungbdc646f2017-02-13 12:12:22 -0800318 }
319
320 config.screenWidthDp = width;
321 config.screenHeightDp = height;
322 config.smallestScreenWidthDp = getSmallestWidthForTaskBounds(
323 insetBounds != null ? insetBounds : bounds, density);
324 }
325 }
326
327 /**
328 * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable
329 * inset areas.
330 *
331 * @param inOutBounds The inOutBounds to subtract the stable inset areas from.
332 */
333 private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds,
334 Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) {
335 mTmpRect.set(inInsetBounds);
336 mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect);
337 int leftInset = mTmpRect.left - inInsetBounds.left;
338 int topInset = mTmpRect.top - inInsetBounds.top;
339 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right;
340 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom;
341 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
342 }
343
344 /**
345 * Calculates the smallest width for a task given the {@param bounds}.
346 *
347 * @return the smallest width to be used in the Configuration, in dips
348 */
349 private int getSmallestWidthForTaskBounds(Rect bounds, float density) {
350 final DisplayContent displayContent = mContainer.getDisplayContent();
351 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
352
353 if (bounds == null || (bounds.width() == displayInfo.logicalWidth &&
354 bounds.height() == displayInfo.logicalHeight)) {
355 // If the bounds are fullscreen, return the value of the fullscreen configuration
356 return displayContent.getConfiguration().smallestScreenWidthDp;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700357 } else if (mContainer.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800358 // For floating tasks, calculate the smallest width from the bounds of the task
359 return (int) (Math.min(bounds.width(), bounds.height()) / density);
360 } else {
361 // Iterating across all screen orientations, and return the minimum of the task
362 // width taking into account that the bounds might change because the snap algorithm
363 // snaps to a different value
364 return displayContent.getDockedDividerController()
365 .getSmallestWidthDpForBounds(bounds);
366 }
367 }
368
Wale Ogunwale1666e312016-12-16 11:27:18 -0800369 void requestResize(Rect bounds) {
370 mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget();
371 }
372
373 @Override
374 public String toString() {
375 return "{StackWindowController stackId=" + mStackId + "}";
376 }
377
378 private static final class H extends Handler {
379
380 static final int REQUEST_RESIZE = 0;
381
382 private final WeakReference<StackWindowController> mController;
383
384 H(WeakReference<StackWindowController> controller, Looper looper) {
385 super(looper);
386 mController = controller;
387 }
388
389 @Override
390 public void handleMessage(Message msg) {
391 final StackWindowController controller = mController.get();
392 final StackWindowListener listener = (controller != null)
393 ? controller.mListener : null;
394 if (listener == null) {
395 return;
396 }
397 switch (msg.what) {
398 case REQUEST_RESIZE:
399 listener.requestResize((Rect) msg.obj);
400 break;
401 }
402 }
403 }
404}