blob: b2029dda0277d7809d67907adfa991ca6704b93f [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
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
274 final Rect parentAppBounds = parentConfig.appBounds;
275
276 config.setAppBounds(!bounds.isEmpty() ? bounds : null);
277 boolean intersectParentBounds = false;
278
Winson Chungbdc646f2017-02-13 12:12:22 -0800279 if (StackId.tasksAreFloating(mStackId)) {
280 // Floating tasks should not be resized to the screen's bounds.
281
282 if (bounds.width() == mTmpDisplayBounds.width() &&
283 bounds.height() == mTmpDisplayBounds.height()) {
284 // 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);
Bryce Lee7566d762017-03-30 09:34:15 -0700289 intersectParentBounds = true;
Winson Chungbdc646f2017-02-13 12:12:22 -0800290 }
291 width = (int) (stableBounds.width() / density);
292 height = (int) (stableBounds.height() / density);
293 } else {
294 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
295 // area, i.e. the screen area without the system bars.
296 // Additionally task dimensions should not be bigger than its parents dimensions.
297 // The non decor inset are areas that could never be removed in Honeycomb. See
298 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
299 intersectDisplayBoundsExcludeInsets(nonDecorBounds,
300 insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets,
301 mTmpDisplayBounds, overrideWidth, overrideHeight);
302 intersectDisplayBoundsExcludeInsets(stableBounds,
303 insetBounds != null ? insetBounds : bounds, mTmpStableInsets,
304 mTmpDisplayBounds, overrideWidth, overrideHeight);
305 width = Math.min((int) (stableBounds.width() / density),
306 parentConfig.screenWidthDp);
307 height = Math.min((int) (stableBounds.height() / density),
308 parentConfig.screenHeightDp);
Bryce Lee7566d762017-03-30 09:34:15 -0700309 intersectParentBounds = true;
310 }
311
312 if (intersectParentBounds && config.appBounds != null) {
313 config.appBounds.intersect(parentAppBounds);
Winson Chungbdc646f2017-02-13 12:12:22 -0800314 }
315
316 config.screenWidthDp = width;
317 config.screenHeightDp = height;
318 config.smallestScreenWidthDp = getSmallestWidthForTaskBounds(
319 insetBounds != null ? insetBounds : bounds, density);
320 }
321 }
322
323 /**
324 * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable
325 * inset areas.
326 *
327 * @param inOutBounds The inOutBounds to subtract the stable inset areas from.
328 */
329 private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds,
330 Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) {
331 mTmpRect.set(inInsetBounds);
332 mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect);
333 int leftInset = mTmpRect.left - inInsetBounds.left;
334 int topInset = mTmpRect.top - inInsetBounds.top;
335 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right;
336 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom;
337 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
338 }
339
340 /**
341 * Calculates the smallest width for a task given the {@param bounds}.
342 *
343 * @return the smallest width to be used in the Configuration, in dips
344 */
345 private int getSmallestWidthForTaskBounds(Rect bounds, float density) {
346 final DisplayContent displayContent = mContainer.getDisplayContent();
347 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
348
349 if (bounds == null || (bounds.width() == displayInfo.logicalWidth &&
350 bounds.height() == displayInfo.logicalHeight)) {
351 // If the bounds are fullscreen, return the value of the fullscreen configuration
352 return displayContent.getConfiguration().smallestScreenWidthDp;
353 } else if (StackId.tasksAreFloating(mStackId)) {
354 // For floating tasks, calculate the smallest width from the bounds of the task
355 return (int) (Math.min(bounds.width(), bounds.height()) / density);
356 } else {
357 // Iterating across all screen orientations, and return the minimum of the task
358 // width taking into account that the bounds might change because the snap algorithm
359 // snaps to a different value
360 return displayContent.getDockedDividerController()
361 .getSmallestWidthDpForBounds(bounds);
362 }
363 }
364
Wale Ogunwale1666e312016-12-16 11:27:18 -0800365 void requestResize(Rect bounds) {
366 mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget();
367 }
368
369 @Override
370 public String toString() {
371 return "{StackWindowController stackId=" + mStackId + "}";
372 }
373
374 private static final class H extends Handler {
375
376 static final int REQUEST_RESIZE = 0;
377
378 private final WeakReference<StackWindowController> mController;
379
380 H(WeakReference<StackWindowController> controller, Looper looper) {
381 super(looper);
382 mController = controller;
383 }
384
385 @Override
386 public void handleMessage(Message msg) {
387 final StackWindowController controller = mController.get();
388 final StackWindowListener listener = (controller != null)
389 ? controller.mListener : null;
390 if (listener == null) {
391 return;
392 }
393 switch (msg.what) {
394 case REQUEST_RESIZE:
395 listener.requestResize((Rect) msg.obj);
396 break;
397 }
398 }
399 }
400}