blob: aaacef5c5e579fc3fccdc1c7056c5359e0915ca4 [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
Andrii Kulian1893a612017-05-04 14:45:09 -070019import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Winson Chungbdc646f2017-02-13 12:12:22 -080020
21import android.app.ActivityManager.StackId;
Wale Ogunwale3382ab12017-07-27 08:55:03 -070022import android.app.WindowConfiguration;
Wale Ogunwale1666e312016-12-16 11:27:18 -080023import 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.internal.annotations.VisibleForTesting;
33
34import java.lang.ref.WeakReference;
Wale Ogunwale1666e312016-12-16 11:27:18 -080035
36import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
37import static com.android.server.wm.WindowContainer.POSITION_TOP;
38import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
39import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
40
41/**
42 * Controller for the stack container. This is created by activity manager to link activity stacks
43 * to the stack container they use in window manager.
44 *
45 * Test class: {@link StackWindowControllerTests}
46 */
47public class StackWindowController
48 extends WindowContainerController<TaskStack, StackWindowListener> {
49
50 final int mStackId;
51
52 private final H mHandler;
53
Winson Chungbdc646f2017-02-13 12:12:22 -080054 // Temp bounds only used in adjustConfigurationForBounds()
55 private final Rect mTmpRect = new Rect();
56 private final Rect mTmpStableInsets = new Rect();
57 private final Rect mTmpNonDecorInsets = new Rect();
58 private final Rect mTmpDisplayBounds = new Rect();
59
Wale Ogunwale1666e312016-12-16 11:27:18 -080060 public StackWindowController(int stackId, StackWindowListener listener,
Wale Ogunwalef75962a2017-08-23 14:58:04 -070061 int displayId, boolean onTop, Rect outBounds, Configuration overriderConfig) {
62 this(stackId, listener, displayId, onTop, outBounds, overriderConfig,
Wale Ogunwale687b4272017-07-27 02:56:23 -070063 WindowManagerService.getInstance());
Wale Ogunwale1666e312016-12-16 11:27:18 -080064 }
65
66 @VisibleForTesting
67 public StackWindowController(int stackId, StackWindowListener listener,
Wale Ogunwalef75962a2017-08-23 14:58:04 -070068 int displayId, boolean onTop, Rect outBounds, Configuration overrideConfig,
Wale Ogunwale687b4272017-07-27 02:56:23 -070069 WindowManagerService service) {
Wale Ogunwale1666e312016-12-16 11:27:18 -080070 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);
Wale Ogunwalef75962a2017-08-23 14:58:04 -070082 if (overrideConfig != null) {
83 stack.onOverrideConfigurationChanged(overrideConfig);
84 }
Wale Ogunwale1666e312016-12-16 11:27:18 -080085 stack.setController(this);
86 getRawBounds(outBounds);
87 }
88 }
89
90 @Override
91 public void removeContainer() {
92 synchronized (mWindowMap) {
93 if (mContainer != null) {
94 mContainer.removeIfPossible();
95 super.removeContainer();
96 }
97 }
98 }
99
Wale Ogunwalecd501ec2017-04-07 08:53:41 -0700100 public boolean isVisible() {
101 synchronized (mWindowMap) {
102 return mContainer != null && mContainer.isVisible();
103 }
104 }
105
Andrii Kulian51c1b672017-04-07 18:39:32 -0700106 public void reparent(int displayId, Rect outStackBounds, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800107 synchronized (mWindowMap) {
108 if (mContainer == null) {
109 throw new IllegalArgumentException("Trying to move unknown stackId=" + mStackId
110 + " to displayId=" + displayId);
111 }
112
113 final DisplayContent targetDc = mRoot.getDisplayContent(displayId);
114 if (targetDc == null) {
115 throw new IllegalArgumentException("Trying to move stackId=" + mStackId
116 + " to unknown displayId=" + displayId);
117 }
118
Andrii Kulian51c1b672017-04-07 18:39:32 -0700119 targetDc.moveStackToDisplay(mContainer, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800120 getRawBounds(outStackBounds);
121 }
122 }
123
124 public void positionChildAt(TaskWindowContainerController child, int position, Rect bounds,
125 Configuration overrideConfig) {
126 synchronized (mWindowMap) {
127 if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning task=" + child
128 + " at " + position);
129 if (child.mContainer == null) {
130 if (DEBUG_STACK) Slog.i(TAG_WM,
131 "positionChildAt: could not find task=" + this);
132 return;
133 }
134 if (mContainer == null) {
135 if (DEBUG_STACK) Slog.i(TAG_WM,
136 "positionChildAt: could not find stack for task=" + mContainer);
137 return;
138 }
139 child.mContainer.positionAt(position, bounds, overrideConfig);
140 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
141 }
142 }
143
144 public void positionChildAtTop(TaskWindowContainerController child, boolean includingParents) {
145 if (child == null) {
146 // TODO: Fix the call-points that cause this to happen.
147 return;
148 }
149
150 synchronized(mWindowMap) {
151 final Task childTask = child.mContainer;
152 if (childTask == null) {
153 Slog.e(TAG_WM, "positionChildAtTop: task=" + child + " not found");
154 return;
155 }
156 mContainer.positionChildAt(POSITION_TOP, childTask, includingParents);
157
158 if (mService.mAppTransition.isTransitionSet()) {
159 childTask.setSendingToBottom(false);
160 }
161 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
162 }
163 }
164
165 public void positionChildAtBottom(TaskWindowContainerController child) {
166 if (child == null) {
167 // TODO: Fix the call-points that cause this to happen.
168 return;
169 }
170
171 synchronized(mWindowMap) {
172 final Task childTask = child.mContainer;
173 if (childTask == null) {
174 Slog.e(TAG_WM, "positionChildAtBottom: task=" + child + " not found");
175 return;
176 }
177 mContainer.positionChildAt(POSITION_BOTTOM, childTask, false /* includingParents */);
178
179 if (mService.mAppTransition.isTransitionSet()) {
180 childTask.setSendingToBottom(true);
181 }
182 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
183 }
184 }
185
186 /**
187 * Re-sizes a stack and its containing tasks.
188 *
189 * @param bounds New stack bounds. Passing in null sets the bounds to fullscreen.
190 * @param configs Configurations for tasks in the resized stack, keyed by task id.
191 * @param taskBounds Bounds for tasks in the resized stack, keyed by task id.
192 * @return True if the stack is now fullscreen.
193 */
194 public boolean resize(Rect bounds, SparseArray<Configuration> configs,
195 SparseArray<Rect> taskBounds, SparseArray<Rect> taskTempInsetBounds) {
196 synchronized (mWindowMap) {
197 if (mContainer == null) {
198 throw new IllegalArgumentException("resizeStack: stack " + this + " not found.");
199 }
200 // We might trigger a configuration change. Save the current task bounds for freezing.
201 mContainer.prepareFreezingTaskBounds();
202 if (mContainer.setBounds(bounds, configs, taskBounds, taskTempInsetBounds)
203 && mContainer.isVisible()) {
204 mContainer.getDisplayContent().setLayoutNeeded();
205 mService.mWindowPlacerLocked.performSurfacePlacement();
206 }
207 return mContainer.getRawFullscreen();
208 }
209 }
210
Matthew Ngaa2b6202017-02-10 14:48:21 -0800211 /**
212 * @see TaskStack.getStackDockedModeBoundsLocked(Rect, Rect, Rect, boolean)
213 */
214 public void getStackDockedModeBounds(Rect currentTempTaskBounds, Rect outStackBounds,
215 Rect outTempTaskBounds, boolean ignoreVisibility) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800216 synchronized (mWindowMap) {
217 if (mContainer != null) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800218 mContainer.getStackDockedModeBoundsLocked(currentTempTaskBounds, outStackBounds,
219 outTempTaskBounds, ignoreVisibility);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800220 return;
221 }
Matthew Ngaa2b6202017-02-10 14:48:21 -0800222 outStackBounds.setEmpty();
223 outTempTaskBounds.setEmpty();
Wale Ogunwale1666e312016-12-16 11:27:18 -0800224 }
225 }
226
227 public void prepareFreezingTaskBounds() {
228 synchronized (mWindowMap) {
229 if (mContainer == null) {
230 throw new IllegalArgumentException("prepareFreezingTaskBounds: stack " + this
231 + " not found.");
232 }
233 mContainer.prepareFreezingTaskBounds();
234 }
235 }
236
Wale Ogunwale1666e312016-12-16 11:27:18 -0800237 private void getRawBounds(Rect outBounds) {
238 if (mContainer.getRawFullscreen()) {
239 outBounds.setEmpty();
240 } else {
241 mContainer.getRawBounds(outBounds);
242 }
243 }
244
245 public void getBounds(Rect outBounds) {
246 synchronized (mWindowMap) {
247 if (mContainer != null) {
248 mContainer.getBounds(outBounds);
249 return;
250 }
251 outBounds.setEmpty();
252 }
253 }
254
Matthew Ngaa2b6202017-02-10 14:48:21 -0800255 public void getBoundsForNewConfiguration(Rect outBounds) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800256 synchronized(mWindowMap) {
Matthew Ngaa2b6202017-02-10 14:48:21 -0800257 mContainer.getBoundsForNewConfiguration(outBounds);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800258 }
259 }
260
Winson Chungbdc646f2017-02-13 12:12:22 -0800261 /**
262 * Adjusts the screen size in dp's for the {@param config} for the given params.
263 */
264 public void adjustConfigurationForBounds(Rect bounds, Rect insetBounds,
265 Rect nonDecorBounds, Rect stableBounds, boolean overrideWidth,
266 boolean overrideHeight, float density, Configuration config,
267 Configuration parentConfig) {
268 synchronized (mWindowMap) {
269 final TaskStack stack = mContainer;
270 final DisplayContent displayContent = stack.getDisplayContent();
271 final DisplayInfo di = displayContent.getDisplayInfo();
272
273 // Get the insets and display bounds
274 mService.mPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
275 mTmpStableInsets);
276 mService.mPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
277 mTmpNonDecorInsets);
278 mTmpDisplayBounds.set(0, 0, di.logicalWidth, di.logicalHeight);
279
280 int width;
281 int height;
Bryce Lee7566d762017-03-30 09:34:15 -0700282
Wale Ogunwale822e5122017-07-26 06:02:24 -0700283 final Rect parentAppBounds = parentConfig.windowConfiguration.getAppBounds();
Bryce Lee7566d762017-03-30 09:34:15 -0700284
Wale Ogunwale822e5122017-07-26 06:02:24 -0700285 config.windowConfiguration.setAppBounds(!bounds.isEmpty() ? bounds : null);
Bryce Lee7566d762017-03-30 09:34:15 -0700286 boolean intersectParentBounds = false;
287
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700288 if (stack.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800289 // Floating tasks should not be resized to the screen's bounds.
290
Andrii Kulian1893a612017-05-04 14:45:09 -0700291 if (mStackId == PINNED_STACK_ID && bounds.width() == mTmpDisplayBounds.width() &&
Winson Chungbdc646f2017-02-13 12:12:22 -0800292 bounds.height() == mTmpDisplayBounds.height()) {
293 // If the bounds we are animating is the same as the fullscreen stack
294 // dimensions, then apply the same inset calculations that we normally do for
295 // the fullscreen stack, without intersecting it with the display bounds
296 stableBounds.inset(mTmpStableInsets);
297 nonDecorBounds.inset(mTmpNonDecorInsets);
Andrii Kulian1893a612017-05-04 14:45:09 -0700298 // Move app bounds to zero to apply intersection with parent correctly. They are
299 // used only for evaluating width and height, so it's OK to move them around.
Wale Ogunwale822e5122017-07-26 06:02:24 -0700300 config.windowConfiguration.getAppBounds().offsetTo(0, 0);
Bryce Lee7566d762017-03-30 09:34:15 -0700301 intersectParentBounds = true;
Winson Chungbdc646f2017-02-13 12:12:22 -0800302 }
303 width = (int) (stableBounds.width() / density);
304 height = (int) (stableBounds.height() / density);
305 } else {
306 // For calculating screenWidthDp, screenWidthDp, we use the stable inset screen
307 // area, i.e. the screen area without the system bars.
308 // Additionally task dimensions should not be bigger than its parents dimensions.
309 // The non decor inset are areas that could never be removed in Honeycomb. See
310 // {@link WindowManagerPolicy#getNonDecorInsetsLw}.
311 intersectDisplayBoundsExcludeInsets(nonDecorBounds,
312 insetBounds != null ? insetBounds : bounds, mTmpNonDecorInsets,
313 mTmpDisplayBounds, overrideWidth, overrideHeight);
314 intersectDisplayBoundsExcludeInsets(stableBounds,
315 insetBounds != null ? insetBounds : bounds, mTmpStableInsets,
316 mTmpDisplayBounds, overrideWidth, overrideHeight);
317 width = Math.min((int) (stableBounds.width() / density),
318 parentConfig.screenWidthDp);
319 height = Math.min((int) (stableBounds.height() / density),
320 parentConfig.screenHeightDp);
Bryce Lee7566d762017-03-30 09:34:15 -0700321 intersectParentBounds = true;
322 }
323
Wale Ogunwale822e5122017-07-26 06:02:24 -0700324 if (intersectParentBounds && config.windowConfiguration.getAppBounds() != null) {
325 config.windowConfiguration.getAppBounds().intersect(parentAppBounds);
Winson Chungbdc646f2017-02-13 12:12:22 -0800326 }
327
328 config.screenWidthDp = width;
329 config.screenHeightDp = height;
330 config.smallestScreenWidthDp = getSmallestWidthForTaskBounds(
331 insetBounds != null ? insetBounds : bounds, density);
332 }
333 }
334
335 /**
336 * Intersects the specified {@code inOutBounds} with the display frame that excludes the stable
337 * inset areas.
338 *
339 * @param inOutBounds The inOutBounds to subtract the stable inset areas from.
340 */
341 private void intersectDisplayBoundsExcludeInsets(Rect inOutBounds, Rect inInsetBounds,
342 Rect stableInsets, Rect displayBounds, boolean overrideWidth, boolean overrideHeight) {
343 mTmpRect.set(inInsetBounds);
344 mService.intersectDisplayInsetBounds(displayBounds, stableInsets, mTmpRect);
345 int leftInset = mTmpRect.left - inInsetBounds.left;
346 int topInset = mTmpRect.top - inInsetBounds.top;
347 int rightInset = overrideWidth ? 0 : inInsetBounds.right - mTmpRect.right;
348 int bottomInset = overrideHeight ? 0 : inInsetBounds.bottom - mTmpRect.bottom;
349 inOutBounds.inset(leftInset, topInset, rightInset, bottomInset);
350 }
351
352 /**
353 * Calculates the smallest width for a task given the {@param bounds}.
354 *
355 * @return the smallest width to be used in the Configuration, in dips
356 */
357 private int getSmallestWidthForTaskBounds(Rect bounds, float density) {
358 final DisplayContent displayContent = mContainer.getDisplayContent();
359 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
360
361 if (bounds == null || (bounds.width() == displayInfo.logicalWidth &&
362 bounds.height() == displayInfo.logicalHeight)) {
363 // If the bounds are fullscreen, return the value of the fullscreen configuration
364 return displayContent.getConfiguration().smallestScreenWidthDp;
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700365 } else if (mContainer.getWindowConfiguration().tasksAreFloating()) {
Winson Chungbdc646f2017-02-13 12:12:22 -0800366 // For floating tasks, calculate the smallest width from the bounds of the task
367 return (int) (Math.min(bounds.width(), bounds.height()) / density);
368 } else {
369 // Iterating across all screen orientations, and return the minimum of the task
370 // width taking into account that the bounds might change because the snap algorithm
371 // snaps to a different value
372 return displayContent.getDockedDividerController()
373 .getSmallestWidthDpForBounds(bounds);
374 }
375 }
376
Wale Ogunwale1666e312016-12-16 11:27:18 -0800377 void requestResize(Rect bounds) {
378 mHandler.obtainMessage(H.REQUEST_RESIZE, bounds).sendToTarget();
379 }
380
381 @Override
382 public String toString() {
383 return "{StackWindowController stackId=" + mStackId + "}";
384 }
385
386 private static final class H extends Handler {
387
388 static final int REQUEST_RESIZE = 0;
389
390 private final WeakReference<StackWindowController> mController;
391
392 H(WeakReference<StackWindowController> controller, Looper looper) {
393 super(looper);
394 mController = controller;
395 }
396
397 @Override
398 public void handleMessage(Message msg) {
399 final StackWindowController controller = mController.get();
400 final StackWindowListener listener = (controller != null)
401 ? controller.mListener : null;
402 if (listener == null) {
403 return;
404 }
405 switch (msg.what) {
406 case REQUEST_RESIZE:
407 listener.requestResize((Rect) msg.obj);
408 break;
409 }
410 }
411 }
412}