blob: 142f69a3e5838b756d7270db3b67580fbde0514c [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
Winson Chungbdc646f2017-02-13 12:12:22 -080019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20
21import android.app.ActivityManager.StackId;
Wale Ogunwale1666e312016-12-16 11:27:18 -080022import android.app.RemoteAction;
23import android.content.res.Configuration;
24import android.graphics.Rect;
25import android.os.Handler;
26import android.os.Looper;
27import android.os.Message;
28import android.util.Slog;
29import android.util.SparseArray;
Winson Chungbdc646f2017-02-13 12:12:22 -080030import android.view.DisplayInfo;
31
Wale Ogunwale1666e312016-12-16 11:27:18 -080032import com.android.server.UiThread;
33import com.android.internal.annotations.VisibleForTesting;
34
35import java.lang.ref.WeakReference;
36import java.util.List;
37
38import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
39import static com.android.server.wm.WindowContainer.POSITION_TOP;
40import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
41import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
42
43/**
44 * Controller for the stack container. This is created by activity manager to link activity stacks
45 * to the stack container they use in window manager.
46 *
47 * Test class: {@link StackWindowControllerTests}
48 */
49public class StackWindowController
50 extends WindowContainerController<TaskStack, StackWindowListener> {
51
52 final int mStackId;
53
54 private final H mHandler;
55
Winson Chungbdc646f2017-02-13 12:12:22 -080056 // Temp bounds only used in adjustConfigurationForBounds()
57 private final Rect mTmpRect = new Rect();
58 private final Rect mTmpStableInsets = new Rect();
59 private final Rect mTmpNonDecorInsets = new Rect();
60 private final Rect mTmpDisplayBounds = new Rect();
61
Wale Ogunwale1666e312016-12-16 11:27:18 -080062 public StackWindowController(int stackId, StackWindowListener listener,
63 int displayId, boolean onTop, Rect outBounds) {
64 this(stackId, listener, displayId, onTop, outBounds, WindowManagerService.getInstance());
65 }
66
67 @VisibleForTesting
68 public StackWindowController(int stackId, StackWindowListener listener,
69 int displayId, boolean onTop, Rect outBounds, WindowManagerService service) {
70 super(listener, service);
71 mStackId = stackId;
72 mHandler = new H(new WeakReference<>(this), service.mH.getLooper());
73
74 synchronized (mWindowMap) {
75 final DisplayContent dc = mRoot.getDisplayContent(displayId);
76 if (dc == null) {
77 throw new IllegalArgumentException("Trying to add stackId=" + stackId
78 + " to unknown displayId=" + displayId);
79 }
80
81 final TaskStack stack = dc.addStackToDisplay(stackId, onTop);
82 stack.setController(this);
83 getRawBounds(outBounds);
84 }
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
97 public void reparent(int displayId, Rect outStackBounds) {
98 synchronized (mWindowMap) {
99 if (mContainer == null) {
100 throw new IllegalArgumentException("Trying to move unknown stackId=" + mStackId
101 + " to displayId=" + displayId);
102 }
103
104 final DisplayContent targetDc = mRoot.getDisplayContent(displayId);
105 if (targetDc == null) {
106 throw new IllegalArgumentException("Trying to move stackId=" + mStackId
107 + " to unknown displayId=" + displayId);
108 }
109
110 targetDc.moveStackToDisplay(mContainer);
111 getRawBounds(outStackBounds);
112 }
113 }
114
115 public void positionChildAt(TaskWindowContainerController child, int position, Rect bounds,
116 Configuration overrideConfig) {
117 synchronized (mWindowMap) {
118 if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning task=" + child
119 + " at " + position);
120 if (child.mContainer == null) {
121 if (DEBUG_STACK) Slog.i(TAG_WM,
122 "positionChildAt: could not find task=" + this);
123 return;
124 }
125 if (mContainer == null) {
126 if (DEBUG_STACK) Slog.i(TAG_WM,
127 "positionChildAt: could not find stack for task=" + mContainer);
128 return;
129 }
130 child.mContainer.positionAt(position, bounds, overrideConfig);
131 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
132 }
133 }
134
135 public void positionChildAtTop(TaskWindowContainerController child, boolean includingParents) {
136 if (child == null) {
137 // TODO: Fix the call-points that cause this to happen.
138 return;
139 }
140
141 synchronized(mWindowMap) {
142 final Task childTask = child.mContainer;
143 if (childTask == null) {
144 Slog.e(TAG_WM, "positionChildAtTop: task=" + child + " not found");
145 return;
146 }
147 mContainer.positionChildAt(POSITION_TOP, childTask, includingParents);
148
149 if (mService.mAppTransition.isTransitionSet()) {
150 childTask.setSendingToBottom(false);
151 }
152 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
153 }
154 }
155
156 public void positionChildAtBottom(TaskWindowContainerController child) {
157 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 }
168 mContainer.positionChildAt(POSITION_BOTTOM, childTask, false /* includingParents */);
169
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
202 // TODO: This and similar methods should be separated into PinnedStackWindowContainerController
203 public void animateResizePinnedStack(final Rect bounds, final int animationDuration) {
204 synchronized (mWindowMap) {
205 if (mContainer == null) {
206 throw new IllegalArgumentException("Pinned stack container not found :(");
207 }
208 final Rect originalBounds = new Rect();
209 mContainer.getBounds(originalBounds);
210 mContainer.setAnimatingBounds(bounds);
211 UiThread.getHandler().post(new Runnable() {
212 @Override
213 public void run() {
214 mService.mBoundsAnimationController.animateBounds(
215 mContainer, originalBounds, bounds, animationDuration);
216 }
217 });
218 }
219 }
220
221 /** Sets the current picture-in-picture aspect ratio. */
222 public void setPictureInPictureAspectRatio(float aspectRatio) {
223 synchronized (mWindowMap) {
224 if (!mService.mSupportsPictureInPicture || mContainer == null) {
225 return;
226 }
227
228 final int displayId = mContainer.getDisplayContent().getDisplayId();
229 final Rect toBounds = mService.getPictureInPictureBounds(displayId, aspectRatio);
Winson Chung2a82fe52017-02-02 14:43:34 -0800230 final Rect targetBounds = new Rect();
231 mContainer.getAnimatingBounds(targetBounds);
232 if (!toBounds.equals(targetBounds)) {
233 animateResizePinnedStack(toBounds, -1 /* duration */);
234 }
235
236 final PinnedStackController pinnedStackController =
237 mContainer.getDisplayContent().getPinnedStackController();
238 pinnedStackController.setAspectRatio(
239 pinnedStackController.isValidPictureInPictureAspectRatio(aspectRatio)
240 ? aspectRatio : -1f);
241 }
242 }
243
244 /** Sets the current picture-in-picture actions. */
245 public void setPictureInPictureActions(List<RemoteAction> actions) {
246 synchronized (mWindowMap) {
247 if (!mService.mSupportsPictureInPicture || mContainer == null) {
248 return;
249 }
250
251 mContainer.getDisplayContent().getPinnedStackController().setActions(actions);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800252 }
253 }
254
Matthew Nge15352e2016-12-20 15:36:29 -0800255 public void getStackDockedModeBounds(Rect outBounds, Rect outTempBounds,
256 Rect outTempInsetBounds, boolean ignoreVisibility) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800257 synchronized (mWindowMap) {
258 if (mContainer != null) {
Matthew Nge15352e2016-12-20 15:36:29 -0800259 mContainer.getStackDockedModeBoundsLocked(outBounds, outTempBounds,
260 outTempInsetBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800261 return;
262 }
263 outBounds.setEmpty();
Matthew Nge15352e2016-12-20 15:36:29 -0800264 outTempBounds.setEmpty();
265 outTempInsetBounds.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800266 }
267 }
268
269 public void prepareFreezingTaskBounds() {
270 synchronized (mWindowMap) {
271 if (mContainer == null) {
272 throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this
273 + " not found.");
274 }
275 mContainer.prepareFreezingTaskBounds();
276 }
277 }
278
Wale Ogunwale1666e312016-12-16 11:27:18 -0800279 private void getRawBounds(Rect outBounds) {
280 if (mContainer.getRawFullscreen()) {
281 outBounds.setEmpty();
282 } else {
283 mContainer.getRawBounds(outBounds);
284 }
285 }
286
287 public void getBounds(Rect outBounds) {
288 synchronized (mWindowMap) {
289 if (mContainer != null) {
290 mContainer.getBounds(outBounds);
291 return;
292 }
293 outBounds.setEmpty();
294 }
295 }
296
Matthew Nge15352e2016-12-20 15:36:29 -0800297 public void getBoundsForNewConfiguration(Rect outBounds, Rect outTempBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800298 synchronized(mWindowMap) {
Matthew Nge15352e2016-12-20 15:36:29 -0800299 mContainer.getBoundsForNewConfiguration(outBounds, outTempBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800300 }
301 }
302
Winson Chungbdc646f2017-02-13 12:12:22 -0800303 /**
304 * Adjusts the screen size in dp's for the {@param config} for the given params.
305 */
306 public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds,
307 Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth,
308 boolean overrideHeight, float density, Configuration config,
309 Configuration parentConfig) {
310 synchronized (mWindowMap) {
311 final TaskStack stack = mContainer;
312 final DisplayContent displayContent = stack.getDisplayContent();
313 final DisplayInfo di = displayContent.getDisplayInfo();
314
315 // Get the insets and display bounds
316 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
317 mTmpStableInsets);
318 mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
319 mTmpNonDecorInsets);
320 mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight);
321
322 int width;
323 int height;
324 if (StackId.tasksAreFloating(mStackId)) {
325 // Floating tasks should not be resized to the screen's bounds.
326
327 if (bounds.width() == mTmpDisplayBounds.width() &&
328 bounds.height() == mTmpDisplayBounds.height()) {
329 // If the bounds we are animating is the same as the fullscreen stack
330 // dimensions, then apply the same inset calculations that we normally do for
331 // the fullscreen stack, without intersecting it with the display bounds
332 stableBounds.inset(mTmpStableInsets);
333 nonDecorBounds.inset(mTmpNonDecorInsets);
334 }
335 width = (int) (stableBounds.width() / density);
336 height = (int) (stableBounds.height() / density);
337 } else {
338 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
339 // area, i.e. the screen area without the system bars.
340 // Additionally task dimensions should not be bigger than its parents dimensions.
341 // The non decor inset are areas that could never be removed in Honeycomb. See
342 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
343 intersectDisplayBoundsExcludeInsets(nonDecorBounds,
344 insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets,
345 mTmpDisplayBounds, overrideWidth, overrideHeight);
346 intersectDisplayBoundsExcludeInsets(stableBounds,
347 insetBounds != null ? insetBounds : bounds, mTmpStableInsets,
348 mTmpDisplayBounds, overrideWidth, overrideHeight);
349 width = Math.min((int) (stableBounds.width() / density),
350 parentConfig.screenWidthDp);
351 height = Math.min((int) (stableBounds.height() / density),
352 parentConfig.screenHeightDp);
353 }
354
355 config.screenWidthDp = width;
356 config.screenHeightDp = height;
357 config.smallestScreenWidthDp = getSmallestWidthForTaskBounds(
358 insetBounds != null ? insetBounds : bounds, density);
359 }
360 }
361
362 /**
363 * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable
364 * inset areas.
365 *
366 * @param inOutBounds The inOutBounds to subtract the stable inset areas from.
367 */
368 private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds,
369 Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) {
370 mTmpRect.set(inInsetBounds);
371 mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect);
372 int leftInset = mTmpRect.left - inInsetBounds.left;
373 int topInset = mTmpRect.top - inInsetBounds.top;
374 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right;
375 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom;
376 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
377 }
378
379 /**
380 * Calculates the smallest width for a task given the {@param bounds}.
381 *
382 * @return the smallest width to be used in the Configuration, in dips
383 */
384 private int getSmallestWidthForTaskBounds(Rect bounds, float density) {
385 final DisplayContent displayContent = mContainer.getDisplayContent();
386 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
387
388 if (bounds == null || (bounds.width() == displayInfo.logicalWidth &&
389 bounds.height() == displayInfo.logicalHeight)) {
390 // If the bounds are fullscreen, return the value of the fullscreen configuration
391 return displayContent.getConfiguration().smallestScreenWidthDp;
392 } else if (StackId.tasksAreFloating(mStackId)) {
393 // For floating tasks, calculate the smallest width from the bounds of the task
394 return (int) (Math.min(bounds.width(), bounds.height()) / density);
395 } else {
396 // Iterating across all screen orientations, and return the minimum of the task
397 // width taking into account that the bounds might change because the snap algorithm
398 // snaps to a different value
399 return displayContent.getDockedDividerController()
400 .getSmallestWidthDpForBounds(bounds);
401 }
402 }
403
Wale Ogunwale1666e312016-12-16 11:27:18 -0800404 void requestResize(Rect bounds) {
405 mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget();
406 }
407
408 @Override
409 public String toString() {
410 return "{StackWindowController stackId=" + mStackId + "}";
411 }
412
413 private static final class H extends Handler {
414
415 static final int REQUEST_RESIZE = 0;
416
417 private final WeakReference<StackWindowController> mController;
418
419 H(WeakReference<StackWindowController> controller, Looper looper) {
420 super(looper);
421 mController = controller;
422 }
423
424 @Override
425 public void handleMessage(Message msg) {
426 final StackWindowController controller = mController.get();
427 final StackWindowListener listener = (controller != null)
428 ? controller.mListener : null;
429 if (listener == null) {
430 return;
431 }
432 switch (msg.what) {
433 case REQUEST_RESIZE:
434 listener.requestResize((Rect) msg.obj);
435 break;
436 }
437 }
438 }
439}