blob: d71e56106e1806b4ba3f19ab3278f1b84e7c6214 [file] [log] [blame]
Andrii Kulian526ad432020-03-27 12:19:51 -07001/*
2 * Copyright (C) 2020 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
19import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Andrii Kulian9ea12da2020-03-27 17:16:38 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
Andrii Kulian526ad432020-03-27 12:19:51 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Andrii Kulian9ea12da2020-03-27 17:16:38 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
23import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
24import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY;
25import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
Andrii Kulian526ad432020-03-27 12:19:51 -070026import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
27import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Andrii Kulian9ea12da2020-03-27 17:16:38 -070028import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
29import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
30import static android.app.WindowConfiguration.isSplitScreenWindowingMode;
Andrii Kulian526ad432020-03-27 12:19:51 -070031import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
32import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
33import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Andrii Kulian526ad432020-03-27 12:19:51 -070034
Andrii Kulian9ea12da2020-03-27 17:16:38 -070035import static com.android.server.wm.ActivityStack.ActivityState.RESUMED;
36import static com.android.server.wm.ActivityStack.STACK_VISIBILITY_VISIBLE;
37import static com.android.server.wm.ActivityStackSupervisor.TAG_TASKS;
38import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_STATES;
39import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS;
40import static com.android.server.wm.ActivityTaskManagerService.TAG_STACK;
41import static com.android.server.wm.DisplayContent.alwaysCreateStack;
Andrii Kulian526ad432020-03-27 12:19:51 -070042import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ADD_REMOVE;
43import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
Andrii Kulian9ea12da2020-03-27 17:16:38 -070044import static com.android.server.wm.RootWindowContainer.TAG_STATES;
Andrii Kulian86d676c2020-03-27 19:34:54 -070045import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Andrii Kulian526ad432020-03-27 12:19:51 -070046import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
47
Andrii Kulian9ea12da2020-03-27 17:16:38 -070048import android.annotation.Nullable;
49import android.app.ActivityOptions;
50import android.app.WindowConfiguration;
51import android.content.Intent;
52import android.content.pm.ActivityInfo;
53import android.content.pm.ApplicationInfo;
54import android.os.UserHandle;
Andrii Kulian526ad432020-03-27 12:19:51 -070055import android.util.Slog;
56import android.view.SurfaceControl;
Andrii Kulian9ea12da2020-03-27 17:16:38 -070057import android.window.WindowContainerTransaction;
Andrii Kulian526ad432020-03-27 12:19:51 -070058
59import com.android.internal.annotations.VisibleForTesting;
60import com.android.internal.util.ToBooleanFunction;
Andrii Kulian9ea12da2020-03-27 17:16:38 -070061import com.android.internal.util.function.pooled.PooledLambda;
62import com.android.internal.util.function.pooled.PooledPredicate;
Andrii Kulian526ad432020-03-27 12:19:51 -070063import com.android.server.protolog.common.ProtoLog;
64
Andrii Kulianf9df4a82020-03-31 12:09:27 -070065import java.io.PrintWriter;
Andrii Kulian526ad432020-03-27 12:19:51 -070066import java.util.ArrayList;
67import java.util.List;
68
69/**
Andrii Kulian86d676c2020-03-27 19:34:54 -070070 * {@link DisplayArea} that represents a section of a screen that contains app window containers.
Andrii Kulian526ad432020-03-27 12:19:51 -070071 */
Andrii Kulian86d676c2020-03-27 19:34:54 -070072final class TaskDisplayArea extends DisplayArea<ActivityStack> {
Andrii Kulian1cb59dd2020-04-10 12:17:17 -070073
Andrii Kulian86d676c2020-03-27 19:34:54 -070074 DisplayContent mDisplayContent;
Andrii Kulian1cb59dd2020-04-10 12:17:17 -070075
Andrii Kulian526ad432020-03-27 12:19:51 -070076 /**
77 * A control placed at the appropriate level for transitions to occur.
78 */
79 private SurfaceControl mAppAnimationLayer;
80 private SurfaceControl mBoostedAppAnimationLayer;
81 private SurfaceControl mHomeAppAnimationLayer;
82
83 /**
84 * Given that the split-screen divider does not have an AppWindowToken, it
85 * will have to live inside of a "NonAppWindowContainer". However, in visual Z order
86 * it will need to be interleaved with some of our children, appearing on top of
87 * both docked stacks but underneath any assistant stacks.
88 *
89 * To solve this problem we have this anchor control, which will always exist so
90 * we can always assign it the correct value in our {@link #assignChildLayers}.
91 * Likewise since it always exists, we can always
92 * assign the divider a layer relative to it. This way we prevent linking lifecycle
93 * events between tasks and the divider window.
94 */
95 private SurfaceControl mSplitScreenDividerAnchor;
96
97 // Cached reference to some special tasks we tend to get a lot so we don't need to loop
98 // through the list to find them.
99 private ActivityStack mRootHomeTask;
100 private ActivityStack mRootPinnedTask;
101 private ActivityStack mRootSplitScreenPrimaryTask;
102
103 private final ArrayList<ActivityStack> mTmpAlwaysOnTopStacks = new ArrayList<>();
104 private final ArrayList<ActivityStack> mTmpNormalStacks = new ArrayList<>();
105 private final ArrayList<ActivityStack> mTmpHomeStacks = new ArrayList<>();
106
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700107 private ArrayList<Task> mTmpTasks = new ArrayList<>();
108
109 private ActivityTaskManagerService mAtmService;
110
111 private RootWindowContainer mRootWindowContainer;
112
113 // When non-null, new tasks get put into this root task.
Andrii Kulian86d676c2020-03-27 19:34:54 -0700114 Task mLaunchRootTask = null;
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700115
116 /**
117 * A focusable stack that is purposely to be positioned at the top. Although the stack may not
118 * have the topmost index, it is used as a preferred candidate to prevent being unable to resume
119 * target stack properly when there are other focusable always-on-top stacks.
120 */
Andrii Kulian86d676c2020-03-27 19:34:54 -0700121 ActivityStack mPreferredTopFocusableStack;
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700122
123 private final RootWindowContainer.FindTaskResult
124 mTmpFindTaskResult = new RootWindowContainer.FindTaskResult();
125
126 /**
127 * If this is the same as {@link #getFocusedStack} then the activity on the top of the focused
128 * stack has been resumed. If stacks are changing position this will hold the old stack until
129 * the new stack becomes resumed after which it will be set to current focused stack.
130 */
131 ActivityStack mLastFocusedStack;
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700132 /**
133 * All of the stacks on this display. Order matters, topmost stack is in front of all other
134 * stacks, bottommost behind. Accessed directly by ActivityManager package classes. Any calls
135 * changing the list should also call {@link #onStackOrderChanged()}.
136 */
137 private ArrayList<OnStackOrderChangedListener> mStackOrderChangedCallbacks = new ArrayList<>();
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700138
Andrii Kulianf9df4a82020-03-31 12:09:27 -0700139 /**
140 * The task display area is removed from the system and we are just waiting for all activities
141 * on it to be finished before removing this object.
142 */
143 private boolean mRemoved;
144
Andrii Kulian44b3c562020-04-01 12:49:56 -0700145 TaskDisplayArea(DisplayContent displayContent, WindowManagerService service, String name,
146 int displayAreaFeature) {
147 super(service, Type.ANY, name, displayAreaFeature);
Andrii Kulian526ad432020-03-27 12:19:51 -0700148 mDisplayContent = displayContent;
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700149 mRootWindowContainer = service.mRoot;
150 mAtmService = service.mAtmService;
Andrii Kulian526ad432020-03-27 12:19:51 -0700151 }
152
153 /**
154 * Returns the topmost stack on the display that is compatible with the input windowing mode
155 * and activity type. Null is no compatible stack on the display.
156 */
157 ActivityStack getStack(int windowingMode, int activityType) {
158 if (activityType == ACTIVITY_TYPE_HOME) {
159 return mRootHomeTask;
160 }
161 if (windowingMode == WINDOWING_MODE_PINNED) {
162 return mRootPinnedTask;
163 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
164 return mRootSplitScreenPrimaryTask;
165 }
166 for (int i = getChildCount() - 1; i >= 0; --i) {
167 final ActivityStack stack = getChildAt(i);
168 if (activityType == ACTIVITY_TYPE_UNDEFINED
169 && windowingMode == stack.getWindowingMode()) {
170 // Passing in undefined type means we want to match the topmost stack with the
171 // windowing mode.
172 return stack;
173 }
174 if (stack.isCompatible(windowingMode, activityType)) {
175 return stack;
176 }
177 }
178 return null;
179 }
180
181 @VisibleForTesting
182 ActivityStack getTopStack() {
183 final int count = getChildCount();
184 return count > 0 ? getChildAt(count - 1) : null;
185 }
186
187 int getIndexOf(ActivityStack stack) {
188 return mChildren.indexOf(stack);
189 }
190
191 ActivityStack getRootHomeTask() {
192 return mRootHomeTask;
193 }
194
195 ActivityStack getRootPinnedTask() {
196 return mRootPinnedTask;
197 }
198
199 ActivityStack getRootSplitScreenPrimaryTask() {
200 return mRootSplitScreenPrimaryTask;
201 }
202
203 ArrayList<Task> getVisibleTasks() {
204 final ArrayList<Task> visibleTasks = new ArrayList<>();
205 forAllTasks(task -> {
206 if (task.isLeafTask() && task.isVisible()) {
207 visibleTasks.add(task);
208 }
209 });
210 return visibleTasks;
211 }
212
213 void onStackWindowingModeChanged(ActivityStack stack) {
214 removeStackReferenceIfNeeded(stack);
215 addStackReferenceIfNeeded(stack);
216 if (stack == mRootPinnedTask && getTopStack() != stack) {
217 // Looks like this stack changed windowing mode to pinned. Move it to the top.
218 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
219 }
220 }
221
222 void addStackReferenceIfNeeded(ActivityStack stack) {
223 if (stack.isActivityTypeHome()) {
224 if (mRootHomeTask != null) {
225 if (!stack.isDescendantOf(mRootHomeTask)) {
226 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
227 + mRootHomeTask + " already exist on display=" + this
228 + " stack=" + stack);
229 }
230 } else {
231 mRootHomeTask = stack;
232 }
233 }
234
235 if (!stack.isRootTask()) {
236 return;
237 }
238 final int windowingMode = stack.getWindowingMode();
239 if (windowingMode == WINDOWING_MODE_PINNED) {
240 if (mRootPinnedTask != null) {
241 throw new IllegalArgumentException(
242 "addStackReferenceIfNeeded: pinned stack=" + mRootPinnedTask
243 + " already exist on display=" + this + " stack=" + stack);
244 }
245 mRootPinnedTask = stack;
246 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
247 if (mRootSplitScreenPrimaryTask != null) {
248 throw new IllegalArgumentException(
249 "addStackReferenceIfNeeded: split screen primary stack="
250 + mRootSplitScreenPrimaryTask
251 + " already exist on display=" + this + " stack=" + stack);
252 }
253 mRootSplitScreenPrimaryTask = stack;
254 }
255 }
256
257 void removeStackReferenceIfNeeded(ActivityStack stack) {
258 if (stack == mRootHomeTask) {
259 mRootHomeTask = null;
260 } else if (stack == mRootPinnedTask) {
261 mRootPinnedTask = null;
262 } else if (stack == mRootSplitScreenPrimaryTask) {
263 mRootSplitScreenPrimaryTask = null;
264 }
265 }
266
267 @Override
268 void addChild(ActivityStack stack, int position) {
269 addStackReferenceIfNeeded(stack);
270 position = findPositionForStack(position, stack, true /* adding */);
271
272 super.addChild(stack, position);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700273 mAtmService.updateSleepIfNeededLocked();
Andrii Kulian526ad432020-03-27 12:19:51 -0700274
275 // The reparenting case is handled in WindowContainer.
276 if (!stack.mReparenting) {
277 mDisplayContent.setLayoutNeeded();
278 }
279 }
280
281 @Override
282 protected void removeChild(ActivityStack stack) {
283 super.removeChild(stack);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700284 onStackRemoved(stack);
285 mAtmService.updateSleepIfNeededLocked();
Andrii Kulian526ad432020-03-27 12:19:51 -0700286 removeStackReferenceIfNeeded(stack);
287 }
288
289 @Override
290 boolean isOnTop() {
291 // Considered always on top
292 return true;
293 }
294
295 @Override
296 void positionChildAt(int position, ActivityStack child, boolean includingParents) {
297 final boolean moveToTop = (position == POSITION_TOP || position == getChildCount());
298 final boolean moveToBottom = (position == POSITION_BOTTOM || position == 0);
299 if (child.getWindowConfiguration().isAlwaysOnTop() && !moveToTop) {
300 // This stack is always-on-top, override the default behavior.
301 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
302
303 // Moving to its current position, as we must call super but we don't want to
304 // perform any meaningful action.
305 final int currentPosition = mChildren.indexOf(child);
306 super.positionChildAt(currentPosition, child, false /* includingParents */);
307 return;
308 }
309 // We don't allow untrusted display to top when task stack moves to top,
310 // until user tapping this display to change display position as top intentionally.
311 if (mDisplayContent.isUntrustedVirtualDisplay() && !getParent().isOnTop()) {
312 includingParents = false;
313 }
314 final int targetPosition = findPositionForStack(position, child, false /* adding */);
315 super.positionChildAt(targetPosition, child, false /* includingParents */);
316
317 if (includingParents && (moveToTop || moveToBottom)) {
318 // The DisplayContent children do not re-order, but we still want to move the
319 // display of this stack container because the intention of positioning is to have
320 // higher z-order to gain focus.
321 mDisplayContent.positionDisplayAt(moveToTop ? POSITION_TOP : POSITION_BOTTOM,
322 true /* includingParents */);
323 }
324
325 child.updateTaskMovement(moveToTop);
326
327 mDisplayContent.setLayoutNeeded();
328 }
329
330 /**
331 * When stack is added or repositioned, find a proper position for it.
332 * This will make sure that pinned stack always stays on top.
333 * @param requestedPosition Position requested by caller.
334 * @param stack Stack to be added or positioned.
335 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
336 * @return The proper position for the stack.
337 */
338 private int findPositionForStack(int requestedPosition, ActivityStack stack,
339 boolean adding) {
340 if (stack.isActivityTypeDream()) {
341 return POSITION_TOP;
342 }
343
344 if (stack.inPinnedWindowingMode()) {
345 return POSITION_TOP;
346 }
347
348 final int topChildPosition = mChildren.size() - 1;
349 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
350 for (int i = topChildPosition; i >= 0; --i) {
351 // Since a stack could be repositioned while being one of the child, return
352 // current index if that's the same stack we are positioning and it is always on
353 // top.
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700354 final boolean sameStack = mChildren.get(i) == stack;
Andrii Kulian526ad432020-03-27 12:19:51 -0700355 if ((sameStack && stack.isAlwaysOnTop())
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700356 || (!sameStack && !mChildren.get(i).isAlwaysOnTop())) {
Andrii Kulian526ad432020-03-27 12:19:51 -0700357 belowAlwaysOnTopPosition = i;
358 break;
359 }
360 }
361
362 // The max possible position we can insert the stack at.
363 int maxPosition = POSITION_TOP;
364 // The min possible position we can insert the stack at.
365 int minPosition = POSITION_BOTTOM;
366
367 if (stack.isAlwaysOnTop()) {
Andrii Kulian86d676c2020-03-27 19:34:54 -0700368 if (hasPinnedTask()) {
Andrii Kulian526ad432020-03-27 12:19:51 -0700369 // Always-on-top stacks go below the pinned stack.
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700370 maxPosition = mChildren.indexOf(mRootPinnedTask) - 1;
Andrii Kulian526ad432020-03-27 12:19:51 -0700371 }
372 // Always-on-top stacks need to be above all other stacks.
373 minPosition = belowAlwaysOnTopPosition
374 != POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
375 } else {
376 // Other stacks need to be below the always-on-top stacks.
377 maxPosition = belowAlwaysOnTopPosition
378 != POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
379 }
380
381 // Cap the requested position to something reasonable for the previous position check
382 // below.
383 if (requestedPosition == POSITION_TOP) {
384 requestedPosition = mChildren.size();
385 } else if (requestedPosition == POSITION_BOTTOM) {
386 requestedPosition = 0;
387 }
388
389 int targetPosition = requestedPosition;
390 targetPosition = Math.min(targetPosition, maxPosition);
391 targetPosition = Math.max(targetPosition, minPosition);
392
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700393 int prevPosition = mChildren.indexOf(stack);
Andrii Kulian526ad432020-03-27 12:19:51 -0700394 // The positions we calculated above (maxPosition, minPosition) do not take into
395 // consideration the following edge cases.
396 // 1) We need to adjust the position depending on the value "adding".
397 // 2) When we are moving a stack to another position, we also need to adjust the
398 // position depending on whether the stack is moving to a higher or lower position.
399 if ((targetPosition != requestedPosition) && (adding || targetPosition < prevPosition)) {
400 targetPosition++;
401 }
402
403 return targetPosition;
404 }
405
406 @Override
407 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
408 boolean traverseTopToBottom) {
409 if (traverseTopToBottom) {
410 if (super.forAllWindows(callback, traverseTopToBottom)) {
411 return true;
412 }
413 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
414 return true;
415 }
416 } else {
417 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
418 return true;
419 }
420 if (super.forAllWindows(callback, traverseTopToBottom)) {
421 return true;
422 }
423 }
424 return false;
425 }
426
427 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
428 boolean traverseTopToBottom) {
429 // For legacy reasons we process the TaskStack.mExitingActivities first here before the
430 // app tokens.
431 // TODO: Investigate if we need to continue to do this or if we can just process them
432 // in-order.
433 if (traverseTopToBottom) {
434 for (int i = mChildren.size() - 1; i >= 0; --i) {
435 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
436 for (int j = activities.size() - 1; j >= 0; --j) {
437 if (activities.get(j).forAllWindowsUnchecked(callback,
438 traverseTopToBottom)) {
439 return true;
440 }
441 }
442 }
443 } else {
444 final int count = mChildren.size();
445 for (int i = 0; i < count; ++i) {
446 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
447 final int appTokensCount = activities.size();
448 for (int j = 0; j < appTokensCount; j++) {
449 if (activities.get(j).forAllWindowsUnchecked(callback,
450 traverseTopToBottom)) {
451 return true;
452 }
453 }
454 }
455 }
456 return false;
457 }
458
459 void setExitingTokensHasVisible(boolean hasVisible) {
460 for (int i = mChildren.size() - 1; i >= 0; --i) {
461 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
462 for (int j = activities.size() - 1; j >= 0; --j) {
463 activities.get(j).hasVisible = hasVisible;
464 }
465 }
466 }
467
468 void removeExistingAppTokensIfPossible() {
469 for (int i = mChildren.size() - 1; i >= 0; --i) {
470 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
471 for (int j = activities.size() - 1; j >= 0; --j) {
472 final ActivityRecord activity = activities.get(j);
473 if (!activity.hasVisible && !mDisplayContent.mClosingApps.contains(activity)
474 && (!activity.mIsExiting || activity.isEmpty())) {
475 // Make sure there is no animation running on this activity, so any windows
476 // associated with it will be removed as soon as their animations are
477 // complete.
478 cancelAnimation();
479 ProtoLog.v(WM_DEBUG_ADD_REMOVE,
480 "performLayout: Activity exiting now removed %s", activity);
481 activity.removeIfPossible();
482 }
483 }
484 }
485 }
486
487 @Override
488 int getOrientation(int candidate) {
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700489 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)) {
Andrii Kulian526ad432020-03-27 12:19:51 -0700490 // Apps and their containers are not allowed to specify an orientation while using
491 // root tasks...except for the home stack if it is not resizable and currently
492 // visible (top of) its root task.
493 if (mRootHomeTask != null && mRootHomeTask.isVisible()) {
494 final Task topMost = mRootHomeTask.getTopMostTask();
495 final boolean resizable = topMost != null && topMost.isResizeable();
496 if (!(resizable && mRootHomeTask.matchParentBounds())) {
497 final int orientation = mRootHomeTask.getOrientation();
498 if (orientation != SCREEN_ORIENTATION_UNSET) {
499 return orientation;
500 }
501 }
502 }
503 return SCREEN_ORIENTATION_UNSPECIFIED;
504 }
505
506 final int orientation = super.getOrientation(candidate);
507 if (orientation != SCREEN_ORIENTATION_UNSET
508 && orientation != SCREEN_ORIENTATION_BEHIND) {
509 ProtoLog.v(WM_DEBUG_ORIENTATION,
510 "App is requesting an orientation, return %d for display id=%d",
511 orientation, mDisplayContent.mDisplayId);
512 return orientation;
513 }
514
515 ProtoLog.v(WM_DEBUG_ORIENTATION,
516 "No app is requesting an orientation, return %d for display id=%d",
517 mDisplayContent.getLastOrientation(), mDisplayContent.mDisplayId);
518 // The next app has not been requested to be visible, so we keep the current orientation
519 // to prevent freezing/unfreezing the display too early.
520 return mDisplayContent.getLastOrientation();
521 }
522
523 @Override
524 void assignChildLayers(SurfaceControl.Transaction t) {
525 assignStackOrdering(t);
526
527 for (int i = 0; i < mChildren.size(); i++) {
528 final ActivityStack s = mChildren.get(i);
529 s.assignChildLayers(t);
530 }
531 }
532
533 void assignStackOrdering(SurfaceControl.Transaction t) {
534 if (getParent() == null) {
535 return;
536 }
537 mTmpAlwaysOnTopStacks.clear();
538 mTmpHomeStacks.clear();
539 mTmpNormalStacks.clear();
540 for (int i = 0; i < mChildren.size(); ++i) {
541 final ActivityStack s = mChildren.get(i);
542 if (s.isAlwaysOnTop()) {
543 mTmpAlwaysOnTopStacks.add(s);
544 } else if (s.isActivityTypeHome()) {
545 mTmpHomeStacks.add(s);
546 } else {
547 mTmpNormalStacks.add(s);
548 }
549 }
550
551 int layer = 0;
552 // Place home stacks to the bottom.
553 for (int i = 0; i < mTmpHomeStacks.size(); i++) {
554 mTmpHomeStacks.get(i).assignLayer(t, layer++);
555 }
556 // The home animation layer is between the home stacks and the normal stacks.
557 final int layerForHomeAnimationLayer = layer++;
558 int layerForSplitScreenDividerAnchor = layer++;
559 int layerForAnimationLayer = layer++;
560 for (int i = 0; i < mTmpNormalStacks.size(); i++) {
561 final ActivityStack s = mTmpNormalStacks.get(i);
562 s.assignLayer(t, layer++);
563 if (s.inSplitScreenWindowingMode()) {
564 // The split screen divider anchor is located above the split screen window.
565 layerForSplitScreenDividerAnchor = layer++;
566 }
567 if (s.isTaskAnimating() || s.isAppTransitioning()) {
568 // The animation layer is located above the highest animating stack and no
569 // higher.
570 layerForAnimationLayer = layer++;
571 }
572 }
573 // The boosted animation layer is between the normal stacks and the always on top
574 // stacks.
575 final int layerForBoostedAnimationLayer = layer++;
576 for (int i = 0; i < mTmpAlwaysOnTopStacks.size(); i++) {
577 mTmpAlwaysOnTopStacks.get(i).assignLayer(t, layer++);
578 }
579
580 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
581 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
582 t.setLayer(mSplitScreenDividerAnchor, layerForSplitScreenDividerAnchor);
583 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
584 }
585
586 @Override
587 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
588 switch (animationLayer) {
589 case ANIMATION_LAYER_BOOSTED:
590 return mBoostedAppAnimationLayer;
591 case ANIMATION_LAYER_HOME:
592 return mHomeAppAnimationLayer;
593 case ANIMATION_LAYER_STANDARD:
594 default:
595 return mAppAnimationLayer;
596 }
597 }
598
599 SurfaceControl getSplitScreenDividerAnchor() {
600 return mSplitScreenDividerAnchor;
601 }
602
603 @Override
604 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
605 if (getParent() != null) {
606 super.onParentChanged(newParent, oldParent, () -> {
607 mAppAnimationLayer = makeChildSurface(null)
608 .setName("animationLayer")
609 .build();
610 mBoostedAppAnimationLayer = makeChildSurface(null)
611 .setName("boostedAnimationLayer")
612 .build();
613 mHomeAppAnimationLayer = makeChildSurface(null)
614 .setName("homeAnimationLayer")
615 .build();
616 mSplitScreenDividerAnchor = makeChildSurface(null)
617 .setName("splitScreenDividerAnchor")
618 .build();
619 getPendingTransaction()
620 .show(mAppAnimationLayer)
621 .show(mBoostedAppAnimationLayer)
622 .show(mHomeAppAnimationLayer)
623 .show(mSplitScreenDividerAnchor);
624 });
625 } else {
626 super.onParentChanged(newParent, oldParent);
627 mWmService.mTransactionFactory.get()
628 .remove(mAppAnimationLayer)
629 .remove(mBoostedAppAnimationLayer)
630 .remove(mHomeAppAnimationLayer)
631 .remove(mSplitScreenDividerAnchor)
632 .apply();
633 mAppAnimationLayer = null;
634 mBoostedAppAnimationLayer = null;
635 mHomeAppAnimationLayer = null;
636 mSplitScreenDividerAnchor = null;
637 }
638 }
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700639
640 void addStack(ActivityStack stack, int position) {
Andrii Kulian86d676c2020-03-27 19:34:54 -0700641 if (DEBUG_STACK) Slog.d(TAG_WM, "Set stack=" + stack + " on taskDisplayArea=" + this);
642 addChild(stack, position);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700643 positionStackAt(stack, position);
644 }
645
646 void onStackRemoved(ActivityStack stack) {
647 if (ActivityTaskManagerDebugConfig.DEBUG_STACK) {
648 Slog.v(TAG_STACK, "removeStack: detaching " + stack + " from displayId="
649 + mDisplayContent.mDisplayId);
650 }
651 if (mPreferredTopFocusableStack == stack) {
652 mPreferredTopFocusableStack = null;
653 }
654 mDisplayContent.releaseSelfIfNeeded();
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700655 onStackOrderChanged(stack);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700656 }
657
658 void positionStackAt(int position, ActivityStack child, boolean includingParents) {
659 positionChildAt(position, child, includingParents);
660 mDisplayContent.layoutAndAssignWindowLayersIfNeeded();
661 }
662
663 void positionStackAtTop(ActivityStack stack, boolean includingParents) {
664 positionStackAtTop(stack, includingParents, null /* updateLastFocusedStackReason */);
665 }
666
667 void positionStackAtTop(ActivityStack stack, boolean includingParents,
668 String updateLastFocusedStackReason) {
669 positionStackAt(stack, getStackCount(), includingParents,
670 updateLastFocusedStackReason);
671 }
672
673 void positionStackAtBottom(ActivityStack stack) {
674 positionStackAtBottom(stack, null /* updateLastFocusedStackReason */);
675 }
676
677 void positionStackAtBottom(ActivityStack stack, String updateLastFocusedStackReason) {
678 positionStackAt(stack, 0, false /* includingParents */,
679 updateLastFocusedStackReason);
680 }
681
682 void positionStackAt(ActivityStack stack, int position) {
683 positionStackAt(stack, position, false /* includingParents */,
684 null /* updateLastFocusedStackReason */);
685 }
686
687 void positionStackAt(ActivityStack stack, int position, boolean includingParents,
688 String updateLastFocusedStackReason) {
689 // TODO: Keep in sync with WindowContainer.positionChildAt(), once we change that to adjust
690 // the position internally, also update the logic here
691 final ActivityStack prevFocusedStack = updateLastFocusedStackReason != null
692 ? getFocusedStack() : null;
693 final boolean wasContained = getIndexOf(stack) >= 0;
694 if (mDisplayContent.mSingleTaskInstance && getStackCount() == 1 && !wasContained) {
695 throw new IllegalStateException(
696 "positionStackAt: Can only have one task on display=" + this);
697 }
698
699 final boolean movingToTop = wasContained && position >= getStackCount() - 1;
700 // Reset mPreferredTopFocusableStack before positioning to top or {@link
701 // ActivityStackSupervisor#updateTopResumedActivityIfNeeded()} won't update the top
702 // resumed activity.
703 if (movingToTop && stack.isFocusable()) {
704 mPreferredTopFocusableStack = null;
705 }
706
707 // Since positionChildAt() is called during the creation process of pinned stacks,
708 // ActivityStack#getStack() can be null.
709 positionStackAt(position, stack, includingParents);
710
711 // The insert position may be adjusted to non-top when there is always-on-top stack. Since
712 // the original position is preferred to be top, the stack should have higher priority when
713 // we are looking for top focusable stack. The condition {@code wasContained} restricts the
714 // preferred stack is set only when moving an existing stack to top instead of adding a new
715 // stack that may be too early (e.g. in the middle of launching or reparenting).
716 if (movingToTop && stack.isFocusableAndVisible()) {
717 mPreferredTopFocusableStack = stack;
718 } else if (mPreferredTopFocusableStack == stack) {
719 mPreferredTopFocusableStack = null;
720 }
721
722 if (updateLastFocusedStackReason != null) {
723 final ActivityStack currentFocusedStack = getFocusedStack();
724 if (currentFocusedStack != prevFocusedStack) {
725 mLastFocusedStack = prevFocusedStack;
726 EventLogTags.writeWmFocusedStack(mRootWindowContainer.mCurrentUser,
727 mDisplayContent.mDisplayId,
728 currentFocusedStack == null ? -1 : currentFocusedStack.getRootTaskId(),
729 mLastFocusedStack == null ? -1 : mLastFocusedStack.getRootTaskId(),
730 updateLastFocusedStackReason);
731 }
732 }
733
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700734 onStackOrderChanged(stack);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700735 }
736
737 ActivityStack getStack(int rootTaskId) {
738 for (int i = getStackCount() - 1; i >= 0; --i) {
739 final ActivityStack stack = getStackAt(i);
740 if (stack.getRootTaskId() == rootTaskId) {
741 return stack;
742 }
743 }
744 return null;
745 }
746
747 /**
748 * Returns an existing stack compatible with the windowing mode and activity type or creates one
749 * if a compatible stack doesn't exist.
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700750 * @see #getOrCreateStack(int, int, boolean, Intent, Task)
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700751 */
752 ActivityStack getOrCreateStack(int windowingMode, int activityType, boolean onTop) {
753 return getOrCreateStack(windowingMode, activityType, onTop, null /* intent */,
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700754 null /* candidateTask */);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700755 }
756
757 /**
758 * When two level tasks are required for given windowing mode and activity type, returns an
759 * existing compatible root task or creates a new one.
760 * For one level task, the candidate task would be reused to also be the root task or create
761 * a new root task if no candidate task.
762 * @see #getStack(int, int)
763 * @see #createStack(int, int, boolean)
764 */
765 ActivityStack getOrCreateStack(int windowingMode, int activityType, boolean onTop,
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700766 Intent intent, Task candidateTask) {
Louis Chang6c5d7252020-03-30 15:42:01 +0800767 // Need to pass in a determined windowing mode to see if a new stack should be created,
768 // so use its parent's windowing mode if it is undefined.
769 if (!alwaysCreateStack(
770 windowingMode != WINDOWING_MODE_UNDEFINED ? windowingMode : getWindowingMode(),
771 activityType)) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700772 ActivityStack stack = getStack(windowingMode, activityType);
773 if (stack != null) {
774 return stack;
775 }
776 } else if (candidateTask != null) {
777 final ActivityStack stack = (ActivityStack) candidateTask;
778 final int position = onTop ? POSITION_TOP : POSITION_BOTTOM;
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700779 Task launchRootTask = updateLaunchRootTask(windowingMode);
780
781 if (launchRootTask != null) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700782 if (stack.getParent() == null) {
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700783 launchRootTask.addChild(stack, position);
784 } else if (stack.getParent() != launchRootTask) {
785 stack.reparent(launchRootTask, position);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700786 }
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700787 } else if (stack.getDisplayArea() != this || !stack.isRootTask()) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700788 if (stack.getParent() == null) {
789 addStack(stack, position);
790 } else {
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700791 stack.reparent(this, onTop);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700792 }
793 }
794 // Update windowing mode if necessary, e.g. moving a pinned task to fullscreen.
795 if (candidateTask.getWindowingMode() != windowingMode) {
796 candidateTask.setWindowingMode(windowingMode);
797 }
798 return stack;
799 }
800 return createStack(windowingMode, activityType, onTop, null /*info*/, intent,
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700801 false /* createdByOrganizer */);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700802 }
803
804 /**
805 * Returns an existing stack compatible with the input params or creates one
806 * if a compatible stack doesn't exist.
807 * @see #getOrCreateStack(int, int, boolean)
808 */
809 ActivityStack getOrCreateStack(@Nullable ActivityRecord r,
810 @Nullable ActivityOptions options, @Nullable Task candidateTask, int activityType,
811 boolean onTop) {
812 // First preference is the windowing mode in the activity options if set.
813 int windowingMode = (options != null)
814 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
815 // Validate that our desired windowingMode will work under the current conditions.
816 // UNDEFINED windowing mode is a valid result and means that the new stack will inherit
817 // it's display's windowing mode.
818 windowingMode = validateWindowingMode(windowingMode, r, candidateTask, activityType);
819 return getOrCreateStack(windowingMode, activityType, onTop, null /* intent */,
Wale Ogunwalec27e1ac2020-03-31 13:18:47 -0700820 candidateTask);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700821 }
822
823 @VisibleForTesting
824 int getNextStackId() {
825 return mAtmService.mStackSupervisor.getNextTaskIdForUser();
826 }
827
828 ActivityStack createStack(int windowingMode, int activityType, boolean onTop) {
829 return createStack(windowingMode, activityType, onTop, null /* info */, null /* intent */,
830 false /* createdByOrganizer */);
831 }
832
833 /**
834 * Creates a stack matching the input windowing mode and activity type on this display.
835 * @param windowingMode The windowing mode the stack should be created in. If
836 * {@link WindowConfiguration#WINDOWING_MODE_UNDEFINED} then the stack will
837 * inherit its parent's windowing mode.
838 * @param activityType The activityType the stack should be created in. If
839 * {@link WindowConfiguration#ACTIVITY_TYPE_UNDEFINED} then the stack will
840 * be created in {@link WindowConfiguration#ACTIVITY_TYPE_STANDARD}.
841 * @param onTop If true the stack will be created at the top of the display, else at the bottom.
842 * @param info The started activity info.
843 * @param intent The intent that started this task.
844 * @param createdByOrganizer @{code true} if this is created by task organizer, @{code false}
845 * otherwise.
846 * @return The newly created stack.
847 */
848 ActivityStack createStack(int windowingMode, int activityType, boolean onTop, ActivityInfo info,
849 Intent intent, boolean createdByOrganizer) {
850 if (mDisplayContent.mSingleTaskInstance && getStackCount() > 0) {
851 // Create stack on default display instead since this display can only contain 1 stack.
852 // TODO: Kinda a hack, but better that having the decision at each call point. Hoping
853 // this goes away once ActivityView is no longer using virtual displays.
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700854 return mRootWindowContainer.getDefaultTaskDisplayArea().createStack(
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700855 windowingMode, activityType, onTop, info, intent, createdByOrganizer);
856 }
857
858 if (activityType == ACTIVITY_TYPE_UNDEFINED && !createdByOrganizer) {
859 // Can't have an undefined stack type yet...so re-map to standard. Anyone that wants
860 // anything else should be passing it in anyways...except for the task organizer.
861 activityType = ACTIVITY_TYPE_STANDARD;
862 }
863
864 if (activityType != ACTIVITY_TYPE_STANDARD && activityType != ACTIVITY_TYPE_UNDEFINED) {
865 // For now there can be only one stack of a particular non-standard activity type on a
866 // display. So, get that ignoring whatever windowing mode it is currently in.
867 ActivityStack stack = getStack(WINDOWING_MODE_UNDEFINED, activityType);
868 if (stack != null) {
869 throw new IllegalArgumentException("Stack=" + stack + " of activityType="
870 + activityType + " already on display=" + this + ". Can't have multiple.");
871 }
872 }
873
874 if (!isWindowingModeSupported(windowingMode, mAtmService.mSupportsMultiWindow,
875 mAtmService.mSupportsSplitScreenMultiWindow,
876 mAtmService.mSupportsFreeformWindowManagement,
877 mAtmService.mSupportsPictureInPicture, activityType)) {
878 throw new IllegalArgumentException("Can't create stack for unsupported windowingMode="
879 + windowingMode);
880 }
881
Evan Rosky688c8382020-04-03 17:27:08 -0700882 if (windowingMode == WINDOWING_MODE_PINNED && getRootPinnedTask() != null) {
883 // Only 1 stack can be PINNED at a time, so dismiss the existing one
884 getRootPinnedTask().dismissPip();
885 }
886
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700887 final int stackId = getNextStackId();
888 return createStackUnchecked(windowingMode, activityType, stackId, onTop, info, intent,
889 createdByOrganizer);
890 }
891
892 /** @return the root task to create the next task in. */
893 private Task updateLaunchRootTask(int windowingMode) {
894 if (!isSplitScreenWindowingMode(windowingMode)) {
895 // Only split-screen windowing modes can do this currently...
896 return null;
897 }
898 for (int i = getStackCount() - 1; i >= 0; --i) {
899 final Task t = getStackAt(i);
900 if (!t.mCreatedByOrganizer || t.getRequestedOverrideWindowingMode() != windowingMode) {
901 continue;
902 }
903 // If not already set, pick a launch root which is not the one we are launching into.
904 if (mLaunchRootTask == null) {
905 for (int j = 0, n = getStackCount(); j < n; ++j) {
906 final Task tt = getStackAt(j);
907 if (tt.mCreatedByOrganizer && tt != t) {
908 mLaunchRootTask = tt;
909 break;
910 }
911 }
912 }
913 return t;
914 }
915 return mLaunchRootTask;
916 }
917
918 @VisibleForTesting
919 ActivityStack createStackUnchecked(int windowingMode, int activityType, int stackId,
920 boolean onTop, ActivityInfo info, Intent intent, boolean createdByOrganizer) {
921 if (windowingMode == WINDOWING_MODE_PINNED && activityType != ACTIVITY_TYPE_STANDARD) {
922 throw new IllegalArgumentException("Stack with windowing mode cannot with non standard "
923 + "activity type.");
924 }
925 if (info == null) {
926 info = new ActivityInfo();
927 info.applicationInfo = new ApplicationInfo();
928 }
929
930 // Task created by organizer are added as root.
931 Task launchRootTask = createdByOrganizer ? null : updateLaunchRootTask(windowingMode);
932 if (launchRootTask != null) {
933 // Since this stack will be put into a root task, its windowingMode will be inherited.
934 windowingMode = WINDOWING_MODE_UNDEFINED;
935 }
936
937 final ActivityStack stack = (ActivityStack) Task.create(mAtmService, stackId, activityType,
938 info, intent, createdByOrganizer);
939 if (launchRootTask != null) {
940 launchRootTask.addChild(stack, onTop ? POSITION_TOP : POSITION_BOTTOM);
941 if (onTop) {
942 positionStackAtTop((ActivityStack) launchRootTask, false /* includingParents */);
943 }
944 } else {
945 addStack(stack, onTop ? POSITION_TOP : POSITION_BOTTOM);
Evan Rosky50ea8c12020-04-02 20:49:05 -0700946 stack.setWindowingMode(windowingMode, true /* creating */);
Andrii Kulian9ea12da2020-03-27 17:16:38 -0700947 }
948 return stack;
949 }
950
951 /**
952 * Get the preferred focusable stack in priority. If the preferred stack does not exist, find a
953 * focusable and visible stack from the top of stacks in this display.
954 */
955 ActivityStack getFocusedStack() {
956 if (mPreferredTopFocusableStack != null) {
957 return mPreferredTopFocusableStack;
958 }
959
960 for (int i = getStackCount() - 1; i >= 0; --i) {
961 final ActivityStack stack = getStackAt(i);
962 if (stack.isFocusableAndVisible()) {
963 return stack;
964 }
965 }
966
967 return null;
968 }
969
970 ActivityStack getNextFocusableStack(ActivityStack currentFocus, boolean ignoreCurrent) {
971 final int currentWindowingMode = currentFocus != null
972 ? currentFocus.getWindowingMode() : WINDOWING_MODE_UNDEFINED;
973
974 ActivityStack candidate = null;
975 for (int i = getStackCount() - 1; i >= 0; --i) {
976 final ActivityStack stack = getStackAt(i);
977 if (ignoreCurrent && stack == currentFocus) {
978 continue;
979 }
980 if (!stack.isFocusableAndVisible()) {
981 continue;
982 }
983
984 if (currentWindowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
985 && candidate == null && stack.inSplitScreenPrimaryWindowingMode()) {
986 // If the currently focused stack is in split-screen secondary we save off the
987 // top primary split-screen stack as a candidate for focus because we might
988 // prefer focus to move to an other stack to avoid primary split-screen stack
989 // overlapping with a fullscreen stack when a fullscreen stack is higher in z
990 // than the next split-screen stack. Assistant stack, I am looking at you...
991 // We only move the focus to the primary-split screen stack if there isn't a
992 // better alternative.
993 candidate = stack;
994 continue;
995 }
996 if (candidate != null && stack.inSplitScreenSecondaryWindowingMode()) {
997 // Use the candidate stack since we are now at the secondary split-screen.
998 return candidate;
999 }
1000 return stack;
1001 }
1002 return candidate;
1003 }
1004
Andrii Kulianf9df4a82020-03-31 12:09:27 -07001005 ActivityRecord getFocusedActivity() {
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001006 final ActivityStack focusedStack = getFocusedStack();
1007 if (focusedStack == null) {
1008 return null;
1009 }
1010 // TODO(b/111541062): Move this into ActivityStack#getResumedActivity()
1011 // Check if the focused stack has the resumed activity
1012 ActivityRecord resumedActivity = focusedStack.getResumedActivity();
1013 if (resumedActivity == null || resumedActivity.app == null) {
1014 // If there is no registered resumed activity in the stack or it is not running -
1015 // try to use previously resumed one.
1016 resumedActivity = focusedStack.mPausingActivity;
1017 if (resumedActivity == null || resumedActivity.app == null) {
1018 // If previously resumed activity doesn't work either - find the topmost running
1019 // activity that can be focused.
1020 resumedActivity = focusedStack.topRunningActivity(true /* focusableOnly */);
1021 }
1022 }
1023 return resumedActivity;
1024 }
1025
1026 ActivityStack getLastFocusedStack() {
1027 return mLastFocusedStack;
1028 }
1029
1030 boolean allResumedActivitiesComplete() {
1031 for (int stackNdx = getStackCount() - 1; stackNdx >= 0; --stackNdx) {
1032 final ActivityRecord r = getStackAt(stackNdx).getResumedActivity();
1033 if (r != null && !r.isState(RESUMED)) {
1034 return false;
1035 }
1036 }
1037 final ActivityStack currentFocusedStack = getFocusedStack();
1038 if (ActivityTaskManagerDebugConfig.DEBUG_STACK) {
1039 Slog.d(TAG_STACK, "allResumedActivitiesComplete: mLastFocusedStack changing from="
1040 + mLastFocusedStack + " to=" + currentFocusedStack);
1041 }
1042 mLastFocusedStack = currentFocusedStack;
1043 return true;
1044 }
1045
1046 /**
1047 * Pause all activities in either all of the stacks or just the back stacks. This is done before
1048 * resuming a new activity and to make sure that previously active activities are
1049 * paused in stacks that are no longer visible or in pinned windowing mode. This does not
1050 * pause activities in visible stacks, so if an activity is launched within the same stack/task,
1051 * then we should explicitly pause that stack's top activity.
1052 * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
1053 * @param resuming The resuming activity.
1054 * @return {@code true} if any activity was paused as a result of this call.
1055 */
1056 boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming) {
1057 boolean someActivityPaused = false;
1058 for (int stackNdx = getStackCount() - 1; stackNdx >= 0; --stackNdx) {
1059 final ActivityStack stack = getStackAt(stackNdx);
1060 final ActivityRecord resumedActivity = stack.getResumedActivity();
1061 if (resumedActivity != null
1062 && (stack.getVisibility(resuming) != STACK_VISIBILITY_VISIBLE
1063 || !stack.isTopActivityFocusable())) {
1064 if (DEBUG_STATES) {
1065 Slog.d(TAG_STATES, "pauseBackStacks: stack=" + stack
1066 + " mResumedActivity=" + resumedActivity);
1067 }
1068 someActivityPaused |= stack.startPausingLocked(userLeaving, false /* uiSleeping*/,
1069 resuming);
1070 }
1071 }
1072 return someActivityPaused;
1073 }
1074
1075 /**
1076 * Find task for putting the Activity in.
1077 */
Andrii Kulian1cb59dd2020-04-10 12:17:17 -07001078 void findTaskLocked(final ActivityRecord r, final boolean isPreferredDisplayArea,
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001079 RootWindowContainer.FindTaskResult result) {
1080 mTmpFindTaskResult.clear();
1081 for (int stackNdx = getStackCount() - 1; stackNdx >= 0; --stackNdx) {
1082 final ActivityStack stack = getStackAt(stackNdx);
1083 if (!r.hasCompatibleActivityType(stack) && stack.isLeafTask()) {
1084 if (DEBUG_TASKS) {
1085 Slog.d(TAG_TASKS, "Skipping stack: (mismatch activity/stack) " + stack);
1086 }
1087 continue;
1088 }
1089
1090 mTmpFindTaskResult.process(r, stack);
1091 // It is possible to have tasks in multiple stacks with the same root affinity, so
1092 // we should keep looking after finding an affinity match to see if there is a
1093 // better match in another stack. Also, task affinity isn't a good enough reason
1094 // to target a display which isn't the source of the intent, so skip any affinity
1095 // matches not on the specified display.
1096 if (mTmpFindTaskResult.mRecord != null) {
1097 if (mTmpFindTaskResult.mIdealMatch) {
1098 result.setTo(mTmpFindTaskResult);
1099 return;
Andrii Kulian1cb59dd2020-04-10 12:17:17 -07001100 } else if (isPreferredDisplayArea) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001101 // Note: since the traversing through the stacks is top down, the floating
1102 // tasks should always have lower priority than any affinity-matching tasks
1103 // in the fullscreen stacks
1104 result.setTo(mTmpFindTaskResult);
1105 }
1106 }
1107 }
1108 }
1109
1110 /**
1111 * Removes stacks in the input windowing modes from the system if they are of activity type
1112 * ACTIVITY_TYPE_STANDARD or ACTIVITY_TYPE_UNDEFINED
1113 */
1114 void removeStacksInWindowingModes(int... windowingModes) {
1115 if (windowingModes == null || windowingModes.length == 0) {
1116 return;
1117 }
1118
1119 // Collect the stacks that are necessary to be removed instead of performing the removal
1120 // by looping mStacks, so that we don't miss any stacks after the stack size changed or
1121 // stacks reordered.
1122 final ArrayList<ActivityStack> stacks = new ArrayList<>();
1123 for (int j = windowingModes.length - 1; j >= 0; --j) {
1124 final int windowingMode = windowingModes[j];
1125 for (int i = getStackCount() - 1; i >= 0; --i) {
1126 final ActivityStack stack = getStackAt(i);
1127 if (!stack.isActivityTypeStandardOrUndefined()) {
1128 continue;
1129 }
1130 if (stack.getWindowingMode() != windowingMode) {
1131 continue;
1132 }
1133 stacks.add(stack);
1134 }
1135 }
1136
1137 for (int i = stacks.size() - 1; i >= 0; --i) {
1138 mRootWindowContainer.mStackSupervisor.removeStack(stacks.get(i));
1139 }
1140 }
1141
1142 void removeStacksWithActivityTypes(int... activityTypes) {
1143 if (activityTypes == null || activityTypes.length == 0) {
1144 return;
1145 }
1146
1147 // Collect the stacks that are necessary to be removed instead of performing the removal
1148 // by looping mStacks, so that we don't miss any stacks after the stack size changed or
1149 // stacks reordered.
1150 final ArrayList<ActivityStack> stacks = new ArrayList<>();
1151 for (int j = activityTypes.length - 1; j >= 0; --j) {
1152 final int activityType = activityTypes[j];
1153 for (int i = getStackCount() - 1; i >= 0; --i) {
1154 final ActivityStack stack = getStackAt(i);
1155 // Collect the root tasks that are currently being organized.
Evan Rosky50ea8c12020-04-02 20:49:05 -07001156 if (stack.mCreatedByOrganizer) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001157 for (int k = stack.getChildCount() - 1; k >= 0; --k) {
1158 final ActivityStack childStack = (ActivityStack) stack.getChildAt(k);
1159 if (childStack.getActivityType() == activityType) {
1160 stacks.add(childStack);
1161 }
1162 }
1163 } else if (stack.getActivityType() == activityType) {
1164 stacks.add(stack);
1165 }
1166 }
1167 }
1168
1169 for (int i = stacks.size() - 1; i >= 0; --i) {
1170 mRootWindowContainer.mStackSupervisor.removeStack(stacks.get(i));
1171 }
1172 }
1173
1174 void onSplitScreenModeDismissed() {
Wale Ogunwalefbb81c92020-04-09 18:01:24 -07001175 onSplitScreenModeDismissed(null /* toTop */);
1176 }
1177
1178 void onSplitScreenModeDismissed(ActivityStack toTop) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001179 mAtmService.deferWindowLayout();
1180 try {
1181 mLaunchRootTask = null;
1182 moveSplitScreenTasksToFullScreen();
1183 } finally {
Wale Ogunwalefbb81c92020-04-09 18:01:24 -07001184 final ActivityStack topFullscreenStack = toTop != null
1185 ? toTop : getTopStackInWindowingMode(WINDOWING_MODE_FULLSCREEN);
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001186 final ActivityStack homeStack = getOrCreateRootHomeTask();
Wale Ogunwalefbb81c92020-04-09 18:01:24 -07001187 if (homeStack != null && ((topFullscreenStack != null && !isTopStack(homeStack))
1188 || toTop != null)) {
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001189 // Whenever split-screen is dismissed we want the home stack directly behind the
1190 // current top fullscreen stack so it shows up when the top stack is finished.
Wale Ogunwalefbb81c92020-04-09 18:01:24 -07001191 // Or, if the caller specified a stack to be on top after split-screen is dismissed.
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001192 // TODO: Would be better to use ActivityDisplay.positionChildAt() for this, however
1193 // ActivityDisplay doesn't have a direct controller to WM side yet. We can switch
1194 // once we have that.
1195 homeStack.moveToFront("onSplitScreenModeDismissed");
1196 topFullscreenStack.moveToFront("onSplitScreenModeDismissed");
1197 }
1198 mAtmService.continueWindowLayout();
1199 }
1200 }
1201
1202 private void moveSplitScreenTasksToFullScreen() {
1203 final WindowContainerTransaction wct = new WindowContainerTransaction();
1204 mTmpTasks.clear();
1205 forAllTasks(task -> {
1206 if (task.mCreatedByOrganizer && task.inSplitScreenWindowingMode() && task.hasChild()) {
1207 mTmpTasks.add(task);
1208 }
1209 });
1210
1211 for (int i = mTmpTasks.size() - 1; i >= 0; i--) {
1212 final Task root = mTmpTasks.get(i);
1213 for (int j = 0; j < root.getChildCount(); j++) {
Wale Ogunwaleadf116e2020-03-27 16:36:01 -07001214 wct.reparent(root.getChildAt(j).mRemoteToken.toWindowContainerToken(),
1215 null, true /* toTop */);
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001216 }
1217 }
1218 mAtmService.mWindowOrganizerController.applyTransaction(wct);
1219 }
1220
1221 /**
1222 * Returns true if the {@param windowingMode} is supported based on other parameters passed in.
1223 * @param windowingMode The windowing mode we are checking support for.
1224 * @param supportsMultiWindow If we should consider support for multi-window mode in general.
1225 * @param supportsSplitScreen If we should consider support for split-screen multi-window.
1226 * @param supportsFreeform If we should consider support for freeform multi-window.
1227 * @param supportsPip If we should consider support for picture-in-picture mutli-window.
1228 * @param activityType The activity type under consideration.
1229 * @return true if the windowing mode is supported.
1230 */
1231 private boolean isWindowingModeSupported(int windowingMode, boolean supportsMultiWindow,
1232 boolean supportsSplitScreen, boolean supportsFreeform, boolean supportsPip,
1233 int activityType) {
1234
1235 if (windowingMode == WINDOWING_MODE_UNDEFINED
1236 || windowingMode == WINDOWING_MODE_FULLSCREEN) {
1237 return true;
1238 }
1239 if (!supportsMultiWindow) {
1240 return false;
1241 }
1242
1243 if (windowingMode == WINDOWING_MODE_MULTI_WINDOW) {
1244 return true;
1245 }
1246
1247 final int displayWindowingMode = getWindowingMode();
1248 if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
1249 || windowingMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) {
1250 return supportsSplitScreen
1251 && WindowConfiguration.supportSplitScreenWindowingMode(activityType)
1252 // Freeform windows and split-screen windows don't mix well, so prevent
1253 // split windowing modes on freeform displays.
1254 && displayWindowingMode != WINDOWING_MODE_FREEFORM;
1255 }
1256
1257 if (!supportsFreeform && windowingMode == WINDOWING_MODE_FREEFORM) {
1258 return false;
1259 }
1260
1261 if (!supportsPip && windowingMode == WINDOWING_MODE_PINNED) {
1262 return false;
1263 }
1264 return true;
1265 }
1266
1267 /**
1268 * Resolves the windowing mode that an {@link ActivityRecord} would be in if started on this
1269 * display with the provided parameters.
1270 *
1271 * @param r The ActivityRecord in question.
1272 * @param options Options to start with.
1273 * @param task The task within-which the activity would start.
1274 * @param activityType The type of activity to start.
1275 * @return The resolved (not UNDEFINED) windowing-mode that the activity would be in.
1276 */
1277 int resolveWindowingMode(@Nullable ActivityRecord r, @Nullable ActivityOptions options,
1278 @Nullable Task task, int activityType) {
1279
1280 // First preference if the windowing mode in the activity options if set.
1281 int windowingMode = (options != null)
1282 ? options.getLaunchWindowingMode() : WINDOWING_MODE_UNDEFINED;
1283
1284 // If windowing mode is unset, then next preference is the candidate task, then the
1285 // activity record.
1286 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
1287 if (task != null) {
1288 windowingMode = task.getWindowingMode();
1289 }
1290 if (windowingMode == WINDOWING_MODE_UNDEFINED && r != null) {
1291 windowingMode = r.getWindowingMode();
1292 }
1293 if (windowingMode == WINDOWING_MODE_UNDEFINED) {
1294 // Use the display's windowing mode.
1295 windowingMode = getWindowingMode();
1296 }
1297 }
1298 windowingMode = validateWindowingMode(windowingMode, r, task, activityType);
1299 return windowingMode != WINDOWING_MODE_UNDEFINED
1300 ? windowingMode : WINDOWING_MODE_FULLSCREEN;
1301 }
1302
1303 /**
1304 * Check that the requested windowing-mode is appropriate for the specified task and/or activity
1305 * on this display.
1306 *
1307 * @param windowingMode The windowing-mode to validate.
1308 * @param r The {@link ActivityRecord} to check against.
1309 * @param task The {@link Task} to check against.
1310 * @param activityType An activity type.
1311 * @return The provided windowingMode or the closest valid mode which is appropriate.
1312 */
1313 int validateWindowingMode(int windowingMode, @Nullable ActivityRecord r, @Nullable Task task,
1314 int activityType) {
1315 // Make sure the windowing mode we are trying to use makes sense for what is supported.
1316 boolean supportsMultiWindow = mAtmService.mSupportsMultiWindow;
1317 boolean supportsSplitScreen = mAtmService.mSupportsSplitScreenMultiWindow;
1318 boolean supportsFreeform = mAtmService.mSupportsFreeformWindowManagement;
1319 boolean supportsPip = mAtmService.mSupportsPictureInPicture;
1320 if (supportsMultiWindow) {
1321 if (task != null) {
1322 supportsMultiWindow = task.isResizeable();
1323 supportsSplitScreen = task.supportsSplitScreenWindowingMode();
1324 // TODO: Do we need to check for freeform and Pip support here?
1325 } else if (r != null) {
1326 supportsMultiWindow = r.isResizeable();
1327 supportsSplitScreen = r.supportsSplitScreenWindowingMode();
1328 supportsFreeform = r.supportsFreeform();
1329 supportsPip = r.supportsPictureInPicture();
1330 }
1331 }
1332
1333 final boolean inSplitScreenMode = isSplitScreenModeActivated();
1334 if (!inSplitScreenMode
1335 && windowingMode == WINDOWING_MODE_FULLSCREEN_OR_SPLIT_SCREEN_SECONDARY) {
1336 // Switch to the display's windowing mode if we are not in split-screen mode and we are
1337 // trying to launch in split-screen secondary.
1338 windowingMode = WINDOWING_MODE_UNDEFINED;
1339 } else if (inSplitScreenMode && (windowingMode == WINDOWING_MODE_FULLSCREEN
1340 || windowingMode == WINDOWING_MODE_UNDEFINED)
1341 && supportsSplitScreen) {
1342 windowingMode = WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
1343 }
1344
1345 if (windowingMode != WINDOWING_MODE_UNDEFINED
1346 && isWindowingModeSupported(windowingMode, supportsMultiWindow, supportsSplitScreen,
1347 supportsFreeform, supportsPip, activityType)) {
1348 return windowingMode;
1349 }
1350 return WINDOWING_MODE_UNDEFINED;
1351 }
1352
1353 boolean isTopStack(ActivityStack stack) {
1354 return stack == getTopStack();
1355 }
1356
1357 boolean isTopNotPinnedStack(ActivityStack stack) {
1358 for (int i = getStackCount() - 1; i >= 0; --i) {
1359 final ActivityStack current = getStackAt(i);
1360 if (!current.inPinnedWindowingMode()) {
1361 return current == stack;
1362 }
1363 }
1364 return false;
1365 }
1366
Andrii Kulian86d676c2020-03-27 19:34:54 -07001367 ActivityRecord topRunningActivity() {
1368 return topRunningActivity(false /* considerKeyguardState */);
1369 }
1370
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001371 /**
1372 * Returns the top running activity in the focused stack. In the case the focused stack has no
1373 * such activity, the next focusable stack on this display is returned.
1374 *
1375 * @param considerKeyguardState Indicates whether the locked state should be considered. if
1376 * {@code true} and the keyguard is locked, only activities that
1377 * can be shown on top of the keyguard will be considered.
1378 * @return The top running activity. {@code null} if none is available.
1379 */
1380 ActivityRecord topRunningActivity(boolean considerKeyguardState) {
1381 ActivityRecord topRunning = null;
1382 final ActivityStack focusedStack = getFocusedStack();
1383 if (focusedStack != null) {
1384 topRunning = focusedStack.topRunningActivity();
1385 }
1386
1387 // Look in other focusable stacks.
1388 if (topRunning == null) {
1389 for (int i = getStackCount() - 1; i >= 0; --i) {
1390 final ActivityStack stack = getStackAt(i);
1391 // Only consider focusable stacks other than the current focused one.
1392 if (stack == focusedStack || !stack.isTopActivityFocusable()) {
1393 continue;
1394 }
1395 topRunning = stack.topRunningActivity();
1396 if (topRunning != null) {
1397 break;
1398 }
1399 }
1400 }
1401
1402 // This activity can be considered the top running activity if we are not considering
1403 // the locked state, the keyguard isn't locked, or we can show when locked.
1404 if (topRunning != null && considerKeyguardState
1405 && mRootWindowContainer.mStackSupervisor.getKeyguardController()
1406 .isKeyguardLocked()
1407 && !topRunning.canShowWhenLocked()) {
1408 return null;
1409 }
1410
1411 return topRunning;
1412 }
1413
1414 protected int getStackCount() {
1415 return mChildren.size();
1416 }
1417
1418 protected ActivityStack getStackAt(int index) {
1419 return mChildren.get(index);
1420 }
1421
1422 /**
1423 * Returns the existing home stack or creates and returns a new one if it should exist for the
1424 * display.
1425 */
1426 @Nullable
1427 ActivityStack getOrCreateRootHomeTask() {
1428 ActivityStack homeTask = getRootHomeTask();
1429 if (homeTask == null && mDisplayContent.supportsSystemDecorations()
1430 && !mDisplayContent.isUntrustedVirtualDisplay()) {
Louis Changdff902b2020-03-31 13:32:47 +08001431 homeTask = createStack(WINDOWING_MODE_UNDEFINED, ACTIVITY_TYPE_HOME, false /* onTop */);
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001432 }
1433 return homeTask;
1434 }
1435
1436 boolean isSplitScreenModeActivated() {
1437 Task task = getRootSplitScreenPrimaryTask();
1438 return task != null && task.hasChild();
1439 }
1440
1441 /**
1442 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1443 * Null is no compatible stack on the display.
1444 */
1445 ActivityStack getTopStackInWindowingMode(int windowingMode) {
1446 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1447 }
1448
1449 void moveHomeStackToFront(String reason) {
1450 final ActivityStack homeStack = getOrCreateRootHomeTask();
1451 if (homeStack != null) {
1452 homeStack.moveToFront(reason);
1453 }
1454 }
1455
1456 /**
1457 * Moves the focusable home activity to top. If there is no such activity, the home stack will
1458 * still move to top.
1459 */
1460 void moveHomeActivityToTop(String reason) {
1461 final ActivityRecord top = getHomeActivity();
1462 if (top == null) {
1463 moveHomeStackToFront(reason);
1464 return;
1465 }
1466 top.moveFocusableActivityToTop(reason);
1467 }
1468
1469 @Nullable
1470 ActivityRecord getHomeActivity() {
1471 return getHomeActivityForUser(mRootWindowContainer.mCurrentUser);
1472 }
1473
1474 @Nullable
1475 ActivityRecord getHomeActivityForUser(int userId) {
1476 final ActivityStack homeStack = getRootHomeTask();
1477 if (homeStack == null) {
1478 return null;
1479 }
1480
1481 final PooledPredicate p = PooledLambda.obtainPredicate(
Andrii Kulian86d676c2020-03-27 19:34:54 -07001482 TaskDisplayArea::isHomeActivityForUser, PooledLambda.__(ActivityRecord.class),
Andrii Kulian9ea12da2020-03-27 17:16:38 -07001483 userId);
1484 final ActivityRecord r = homeStack.getActivity(p);
1485 p.recycle();
1486 return r;
1487 }
1488
1489 private static boolean isHomeActivityForUser(ActivityRecord r, int userId) {
1490 return r.isActivityTypeHome() && (userId == UserHandle.USER_ALL || r.mUserId == userId);
1491 }
1492
1493 /**
1494 * Adjusts the {@param stack} behind the last visible stack in the display if necessary.
1495 * Generally used in conjunction with {@link #moveStackBehindStack}.
1496 */
1497 // TODO(b/151575894): Remove special stack movement methods.
1498 void moveStackBehindBottomMostVisibleStack(ActivityStack stack) {
1499 if (stack.shouldBeVisible(null)) {
1500 // Skip if the stack is already visible
1501 return;
1502 }
1503
1504 final boolean isRootTask = stack.isRootTask();
1505 if (isRootTask) {
1506 // Move the stack to the bottom to not affect the following visibility checks
1507 positionStackAtBottom(stack);
1508 } else {
1509 stack.getParent().positionChildAt(POSITION_BOTTOM, stack, false /* includingParents */);
1510 }
1511
1512 // Find the next position where the stack should be placed
1513 final int numStacks = isRootTask ? getStackCount() : stack.getParent().getChildCount();
1514 for (int stackNdx = 0; stackNdx < numStacks; stackNdx++) {
1515 final ActivityStack s = isRootTask ? getStackAt(stackNdx)
1516 : (ActivityStack) stack.getParent().getChildAt(stackNdx);
1517 if (s == stack) {
1518 continue;
1519 }
1520 final int winMode = s.getWindowingMode();
1521 final boolean isValidWindowingMode = winMode == WINDOWING_MODE_FULLSCREEN
1522 || winMode == WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
1523 if (s.shouldBeVisible(null) && isValidWindowingMode) {
1524 // Move the provided stack to behind this stack
1525 final int position = Math.max(0, stackNdx - 1);
1526 if (isRootTask) {
1527 positionStackAt(stack, position);
1528 } else {
1529 stack.getParent().positionChildAt(position, stack, false /*includingParents */);
1530 }
1531 break;
1532 }
1533 }
1534 }
1535
1536 /**
1537 * Moves the {@param stack} behind the given {@param behindStack} if possible. If
1538 * {@param behindStack} is not currently in the display, then then the stack is moved to the
1539 * back. Generally used in conjunction with {@link #moveStackBehindBottomMostVisibleStack}.
1540 */
1541 void moveStackBehindStack(ActivityStack stack, ActivityStack behindStack) {
1542 if (behindStack == null || behindStack == stack) {
1543 return;
1544 }
1545
1546 final WindowContainer parent = stack.getParent();
1547 if (parent == null || parent != behindStack.getParent()) {
1548 return;
1549 }
1550
1551 // Note that positionChildAt will first remove the given stack before inserting into the
1552 // list, so we need to adjust the insertion index to account for the removed index
1553 // TODO: Remove this logic when WindowContainer.positionChildAt() is updated to adjust the
1554 // position internally
1555 final int stackIndex = parent.mChildren.indexOf(stack);
1556 final int behindStackIndex = parent.mChildren.indexOf(behindStack);
1557 final int insertIndex = stackIndex <= behindStackIndex
1558 ? behindStackIndex - 1 : behindStackIndex;
1559 final int position = Math.max(0, insertIndex);
1560 if (stack.isRootTask()) {
1561 positionStackAt(stack, position);
1562 } else {
1563 parent.positionChildAt(position, stack, false /* includingParents */);
1564 }
1565 }
Andrii Kulian86d676c2020-03-27 19:34:54 -07001566
1567 boolean hasPinnedTask() {
1568 return getRootPinnedTask() != null;
1569 }
1570
1571 /**
1572 * @return the stack currently above the {@param stack}. Can be null if the {@param stack} is
1573 * already top-most.
1574 */
1575 static ActivityStack getStackAbove(ActivityStack stack) {
1576 final WindowContainer wc = stack.getParent();
1577 final int index = wc.mChildren.indexOf(stack) + 1;
1578 return (index < wc.mChildren.size()) ? (ActivityStack) wc.mChildren.get(index) : null;
1579 }
1580
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -07001581 /** Returns true if the stack in the windowing mode is visible. */
1582 boolean isStackVisible(int windowingMode) {
1583 final ActivityStack stack = getTopStackInWindowingMode(windowingMode);
1584 return stack != null && stack.isVisible();
1585 }
1586
1587 void removeStack(ActivityStack stack) {
1588 removeChild(stack);
1589 }
1590
Andrii Kulian86d676c2020-03-27 19:34:54 -07001591 int getDisplayId() {
1592 return mDisplayContent.getDisplayId();
1593 }
1594
1595 boolean isRemoved() {
Andrii Kulianf9df4a82020-03-31 12:09:27 -07001596 return mRemoved;
Andrii Kulian86d676c2020-03-27 19:34:54 -07001597 }
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -07001598
1599 /**
1600 * Adds a listener to be notified whenever the stack order in the display changes. Currently
1601 * only used by the {@link RecentsAnimation} to determine whether to interrupt and cancel the
1602 * current animation when the system state changes.
1603 */
1604 void registerStackOrderChangedListener(OnStackOrderChangedListener listener) {
1605 if (!mStackOrderChangedCallbacks.contains(listener)) {
1606 mStackOrderChangedCallbacks.add(listener);
1607 }
1608 }
1609
1610 /**
1611 * Removes a previously registered stack order change listener.
1612 */
1613 void unregisterStackOrderChangedListener(OnStackOrderChangedListener listener) {
1614 mStackOrderChangedCallbacks.remove(listener);
1615 }
1616
1617 /**
1618 * Notifies of a stack order change
1619 * @param stack The stack which triggered the order change
1620 */
1621 void onStackOrderChanged(ActivityStack stack) {
1622 for (int i = mStackOrderChangedCallbacks.size() - 1; i >= 0; i--) {
1623 mStackOrderChangedCallbacks.get(i).onStackOrderChanged(stack);
1624 }
1625 }
1626
Evan Rosky0ad8d2c2020-03-16 12:09:58 -07001627 @Override
1628 boolean canCreateRemoteAnimationTarget() {
1629 return true;
1630 }
1631
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -07001632 /**
1633 * Callback for when the order of the stacks in the display changes.
1634 */
1635 interface OnStackOrderChangedListener {
1636 void onStackOrderChanged(ActivityStack stack);
1637 }
Andrii Kulianf9df4a82020-03-31 12:09:27 -07001638
1639 void ensureActivitiesVisible(ActivityRecord starting, int configChanges,
1640 boolean preserveWindows, boolean notifyClients) {
1641 for (int stackNdx = getStackCount() - 1; stackNdx >= 0; --stackNdx) {
1642 final ActivityStack stack = getStackAt(stackNdx);
1643 stack.ensureActivitiesVisible(starting, configChanges, preserveWindows,
1644 notifyClients);
1645 }
1646 }
1647
1648 void prepareFreezingTaskBounds() {
1649 for (int stackNdx = getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1650 final ActivityStack stack = getChildAt(stackNdx);
1651 stack.prepareFreezingTaskBounds();
1652 }
1653 }
1654
1655 /**
1656 * Removes the stacks in the node applying the content removal node from the display.
1657 * @return last reparented stack, or {@code null} if the stacks had to be destroyed.
1658 */
1659 ActivityStack remove() {
1660 mPreferredTopFocusableStack = null;
1661 // TODO(b/153090332): Allow setting content removal mode per task display area
1662 final boolean destroyContentOnRemoval = mDisplayContent.shouldDestroyContentOnRemove();
1663 final TaskDisplayArea toDisplayArea = mRootWindowContainer.getDefaultTaskDisplayArea();
1664 ActivityStack lastReparentedStack = null;
1665
1666 // Stacks could be reparented from the removed display area to other display area. After
1667 // reparenting the last stack of the removed display area, the display area becomes ready to
1668 // be released (no more ActivityStack-s). But, we cannot release it at that moment or the
1669 // related WindowContainer will also be removed. So, we set display area as removed after
1670 // reparenting stack finished.
1671 // Keep the order from bottom to top.
1672 int numStacks = getStackCount();
1673 for (int stackNdx = 0; stackNdx < numStacks; stackNdx++) {
1674 final ActivityStack stack = getStackAt(stackNdx);
1675 // Always finish non-standard type stacks.
1676 if (destroyContentOnRemoval || !stack.isActivityTypeStandardOrUndefined()) {
1677 stack.finishAllActivitiesImmediately();
1678 } else {
1679 // If default display is in split-window mode, set windowing mode of the
1680 // stack to split-screen secondary. Otherwise, set the windowing mode to
1681 // undefined by default to let stack inherited the windowing mode from the
1682 // new display.
1683 final int windowingMode = toDisplayArea.isSplitScreenModeActivated()
1684 ? WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
1685 : WINDOWING_MODE_UNDEFINED;
1686 stack.reparent(toDisplayArea, true /* onTop */);
1687 stack.setWindowingMode(windowingMode);
1688 lastReparentedStack = stack;
1689 }
1690 // Stacks may be removed from this display. Ensure each stack will be processed
1691 // and the loop will end.
1692 stackNdx -= numStacks - getStackCount();
1693 numStacks = getStackCount();
1694 }
1695 mRemoved = true;
1696
1697 return lastReparentedStack;
1698 }
1699
1700
1701 @Override
1702 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1703 pw.println(prefix + "TaskDisplayArea " + getName());
1704 if (mPreferredTopFocusableStack != null) {
1705 pw.println(prefix + " mPreferredTopFocusableStack=" + mPreferredTopFocusableStack);
1706 }
1707 if (mLastFocusedStack != null) {
1708 pw.println(prefix + " mLastFocusedStack=" + mLastFocusedStack);
1709 }
1710 pw.println(prefix + " Application tokens in top down Z order:");
1711 for (int stackNdx = getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1712 final ActivityStack stack = getChildAt(stackNdx);
1713 stack.dump(pw, prefix + " ", dumpAll);
1714 }
1715 }
Andrii Kulian526ad432020-03-27 12:19:51 -07001716}