blob: 95c1d536123fad0ea72153a475ff24ee1db95197 [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 Ogunwale30e441d2017-11-09 08:28:45 -0800228 public void getRawBounds(Rect outBounds) {
229 synchronized (mWindowMap) {
230 if (mContainer.getRawFullscreen()) {
231 outBounds.setEmpty();
232 } else {
233 mContainer.getRawBounds(outBounds);
234 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800235 }
236 }
237
238 public void getBounds(Rect outBounds) {
239 synchronized (mWindowMap) {
240 if (mContainer != null) {
241 mContainer.getBounds(outBounds);
242 return;
243 }
244 outBounds.setEmpty();
245 }
246 }
247
Matthew Ngaa2b6202017-02-10 14:48:21 -0800248 public void getBoundsForNewConfiguration(Rect outBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800249 synchronized(mWindowMap) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800250 mContainer.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800251 }
252 }
253
Winson Chungbdc646f2017-02-13 12:12:22 -0800254 /**
255 * Adjusts the screen size in dp's for the {@param config} for the given params.
256 */
257 public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds,
258 Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth,
259 boolean overrideHeight, float density, Configuration config,
260 Configuration parentConfig) {
261 synchronized (mWindowMap) {
262 final TaskStack stack = mContainer;
263 final DisplayContent displayContent = stack.getDisplayContent();
264 final DisplayInfo di = displayContent.getDisplayInfo();
265
266 // Get the insets and display bounds
267 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
268 mTmpStableInsets);
269 mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
270 mTmpNonDecorInsets);
271 mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight);
272
273 int width;
274 int height;
Bryce Lee7566d762017-03-30 09:34:15 -0700275
Wale Ogunwale822e5122017-07-26 06:02:24 -0700276 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
Bryce Lee7566d762017-03-30 09:34:15 -0700277
Wale Ogunwale822e5122017-07-26 06:02:24 -0700278 config.windowConfiguration.setAppBounds(!bounds.isEmpty() ? bounds : null);
Bryce Lee7566d762017-03-30 09:34:15 -0700279 boolean intersectParentBounds = false;
280
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700281 if (stack.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800282 // Floating tasks should not be resized to the screen's bounds.
283
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700284 if (stack.inPinnedWindowingMode()
285 && bounds.width() == mTmpDisplayBounds.width()
286 && bounds.height() == mTmpDisplayBounds.height()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800287 // 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 Kulian1893a612017-05-04 14:45:09 -0700292 // 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 Ogunwale822e5122017-07-26 06:02:24 -0700294 config.windowConfiguration.getAppBounds().offsetTo(0, 0);
Bryce Lee7566d762017-03-30 09:34:15 -0700295 intersectParentBounds = true;
Winson Chungbdc646f2017-02-13 12:12:22 -0800296 }
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 Lee7566d762017-03-30 09:34:15 -0700315 intersectParentBounds = true;
316 }
317
Wale Ogunwale822e5122017-07-26 06:02:24 -0700318 if (intersectParentBounds && config.windowConfiguration.getAppBounds() != null) {
319 config.windowConfiguration.getAppBounds().intersect(parentAppBounds);
Winson Chungbdc646f2017-02-13 12:12:22 -0800320 }
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;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700359 } else if (mContainer.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800360 // 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 Ogunwale1666e312016-12-16 11:27:18 -0800371 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}