blob: c2a4be55e30aea36808a6a9376765f9edd9a56aa [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
Bryce Leef3c6a472017-11-14 14:53:06 -0800114 public void positionChildAt(TaskWindowContainerController child, int position) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800115 synchronized (mWindowMap) {
116 if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning task=" + child
117 + " at " + position);
118 if (child.mContainer == null) {
119 if (DEBUG_STACK) Slog.i(TAG_WM,
120 "positionChildAt: could not find task=" + this);
121 return;
122 }
123 if (mContainer == null) {
124 if (DEBUG_STACK) Slog.i(TAG_WM,
125 "positionChildAt: could not find stack for task=" + mContainer);
126 return;
127 }
Bryce Leef3c6a472017-11-14 14:53:06 -0800128 child.mContainer.positionAt(position);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800129 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
130 }
131 }
132
133 public void positionChildAtTop(TaskWindowContainerController child, boolean includingParents) {
134 if (child == null) {
135 // TODO: Fix the call-points that cause this to happen.
136 return;
137 }
138
139 synchronized(mWindowMap) {
140 final Task childTask = child.mContainer;
141 if (childTask == null) {
142 Slog.e(TAG_WM, "positionChildAtTop: task=" + child + " not found");
143 return;
144 }
145 mContainer.positionChildAt(POSITION_TOP, childTask, includingParents);
146
147 if (mService.mAppTransition.isTransitionSet()) {
148 childTask.setSendingToBottom(false);
149 }
150 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
151 }
152 }
153
Wale Ogunwale66e16852017-10-19 13:35:52 -0700154 public void positionChildAtBottom(TaskWindowContainerController child,
155 boolean includingParents) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800156 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 }
Wale Ogunwale66e16852017-10-19 13:35:52 -0700167 mContainer.positionChildAt(POSITION_BOTTOM, childTask, includingParents);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800168
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.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800180 * @param taskBounds Bounds for tasks in the resized stack, keyed by task id.
Bryce Leef3c6a472017-11-14 14:53:06 -0800181 * @param taskTempInsetBounds Inset bounds for individual tasks, keyed by task id.
Wale Ogunwale1666e312016-12-16 11:27:18 -0800182 */
Bryce Leef3c6a472017-11-14 14:53:06 -0800183 public void resize(Rect bounds, SparseArray<Rect> taskBounds,
184 SparseArray<Rect> taskTempInsetBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800185 synchronized (mWindowMap) {
186 if (mContainer == null) {
187 throw new IllegalArgumentException("resizeStack: stack " + this + " not found.");
188 }
189 // We might trigger a configuration change. Save the current task bounds for freezing.
190 mContainer.prepareFreezingTaskBounds();
Bryce Leef3c6a472017-11-14 14:53:06 -0800191 if (mContainer.setBounds(bounds, taskBounds, taskTempInsetBounds)
Wale Ogunwale1666e312016-12-16 11:27:18 -0800192 && mContainer.isVisible()) {
193 mContainer.getDisplayContent().setLayoutNeeded();
194 mService.mWindowPlacerLocked.performSurfacePlacement();
195 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800196 }
197 }
198
Matthew Ngaa2b6202017-02-10 14:48:21 -0800199 /**
200 * @see TaskStack.getStackDockedModeBoundsLocked(Rect, Rect, Rect, boolean)
201 */
202 public void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
203 Rect outTempTaskBounds, boolean ignoreVisibility) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800204 synchronized (mWindowMap) {
205 if (mContainer != null) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800206 mContainer.getStackDockedModeBoundsLocked(currentTempTaskBounds, outStackBounds,
207 outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800208 return;
209 }
Matthew Ngaa2b6202017-02-10 14:48:21 -0800210 outStackBounds.setEmpty();
211 outTempTaskBounds.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800212 }
213 }
214
215 public void prepareFreezingTaskBounds() {
216 synchronized (mWindowMap) {
217 if (mContainer == null) {
218 throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this
219 + " not found.");
220 }
221 mContainer.prepareFreezingTaskBounds();
222 }
223 }
224
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800225 public void getRawBounds(Rect outBounds) {
226 synchronized (mWindowMap) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800227 if (mContainer.matchParentBounds()) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800228 outBounds.setEmpty();
229 } else {
230 mContainer.getRawBounds(outBounds);
231 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800232 }
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
Bryce Leef3c6a472017-11-14 14:53:06 -0800275 config.windowConfiguration.setBounds(bounds);
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}