blob: 0a0530c92a16e3370a95a04aaf5614c8b3f70ce3 [file] [log] [blame]
Robert Carr8a2f9132019-11-11 15:03:15 -08001/*
2 * Copyright (C) 2019 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
Evan Rosky0037e5f2019-11-05 10:26:24 -080019import static android.Manifest.permission.MANAGE_ACTIVITY_STACKS;
Robert Carr00c0dbe2020-01-24 15:30:24 -080020import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
Evan Rosky0037e5f2019-11-05 10:26:24 -080021import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
22import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
23import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Robert Carr8a2f9132019-11-11 15:03:15 -080024
Evan Rosky0037e5f2019-11-05 10:26:24 -080025import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
Evan Roskya8fde152020-01-07 19:09:13 -080026import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
27import static com.android.server.wm.WindowContainer.POSITION_TOP;
Evan Rosky0037e5f2019-11-05 10:26:24 -080028
29import android.annotation.Nullable;
30import android.app.ActivityManager.RunningTaskInfo;
31import android.app.ITaskOrganizerController;
32import android.app.WindowConfiguration;
33import android.content.pm.ActivityInfo;
34import android.content.res.Configuration;
35import android.graphics.Rect;
36import android.os.Binder;
Robert Carr8a2f9132019-11-11 15:03:15 -080037import android.os.IBinder;
38import android.os.RemoteException;
Evan Rosky0037e5f2019-11-05 10:26:24 -080039import android.util.ArraySet;
Robert Carr8a2f9132019-11-11 15:03:15 -080040import android.util.Slog;
41import android.view.ITaskOrganizer;
Evan Rosky0037e5f2019-11-05 10:26:24 -080042import android.view.IWindowContainer;
Robert Carre10ee3d2019-11-11 15:03:15 -080043import android.view.SurfaceControl;
Evan Rosky0037e5f2019-11-05 10:26:24 -080044import android.view.WindowContainerTransaction;
45
46import com.android.internal.util.function.pooled.PooledConsumer;
47import com.android.internal.util.function.pooled.PooledLambda;
Robert Carr8a2f9132019-11-11 15:03:15 -080048
49import java.util.ArrayList;
50import java.util.HashMap;
Evan Rosky0037e5f2019-11-05 10:26:24 -080051import java.util.Iterator;
Evan Roskya8fde152020-01-07 19:09:13 -080052import java.util.List;
Evan Rosky0037e5f2019-11-05 10:26:24 -080053import java.util.Map;
54import java.util.WeakHashMap;
Robert Carr8a2f9132019-11-11 15:03:15 -080055
56/**
57 * Stores the TaskOrganizers associated with a given windowing mode and
58 * their associated state.
59 */
Robert Carre10ee3d2019-11-11 15:03:15 -080060class TaskOrganizerController extends ITaskOrganizerController.Stub
61 implements BLASTSyncEngine.TransactionReadyListener {
Robert Carr8a2f9132019-11-11 15:03:15 -080062 private static final String TAG = "TaskOrganizerController";
63
Evan Rosky0037e5f2019-11-05 10:26:24 -080064 /** Flag indicating that an applied transaction may have effected lifecycle */
65 private static final int TRANSACT_EFFECTS_CLIENT_CONFIG = 1;
66 private static final int TRANSACT_EFFECTS_LIFECYCLE = 1 << 1;
67
68 private final WindowManagerGlobalLock mGlobalLock;
Robert Carr8a2f9132019-11-11 15:03:15 -080069
70 private class DeathRecipient implements IBinder.DeathRecipient {
71 int mWindowingMode;
72 ITaskOrganizer mTaskOrganizer;
73
74 DeathRecipient(ITaskOrganizer organizer, int windowingMode) {
75 mTaskOrganizer = organizer;
76 mWindowingMode = windowingMode;
77 }
78
79 @Override
80 public void binderDied() {
81 synchronized (mGlobalLock) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -080082 final TaskOrganizerState state =
83 mTaskOrganizerStates.get(mTaskOrganizer.asBinder());
84 state.releaseTasks();
85 mTaskOrganizerStates.remove(mTaskOrganizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -080086 if (mTaskOrganizersForWindowingMode.get(mWindowingMode) == mTaskOrganizer) {
87 mTaskOrganizersForWindowingMode.remove(mWindowingMode);
88 }
89 }
90 }
91 };
92
93 class TaskOrganizerState {
94 ITaskOrganizer mOrganizer;
95 DeathRecipient mDeathRecipient;
Robert Carr7d7c8ab2020-01-28 15:57:23 -080096 int mWindowingMode;
Robert Carr8a2f9132019-11-11 15:03:15 -080097
98 ArrayList<Task> mOrganizedTasks = new ArrayList<>();
99
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800100 // Save the TaskOrganizer which we replaced registration for
101 // so it can be re-registered if we unregister.
102 TaskOrganizerState mReplacementFor;
103 boolean mDisposed = false;
104
105
106 TaskOrganizerState(ITaskOrganizer organizer, int windowingMode,
107 TaskOrganizerState replacing) {
108 mOrganizer = organizer;
109 mDeathRecipient = new DeathRecipient(organizer, windowingMode);
110 try {
111 organizer.asBinder().linkToDeath(mDeathRecipient, 0);
112 } catch (RemoteException e) {
113 Slog.e(TAG, "TaskOrganizer failed to register death recipient");
114 }
115 mWindowingMode = windowingMode;
116 mReplacementFor = replacing;
117 }
118
Robert Carr8a2f9132019-11-11 15:03:15 -0800119 void addTask(Task t) {
120 mOrganizedTasks.add(t);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800121 try {
122 mOrganizer.taskAppeared(t.getTaskInfo());
123 } catch (Exception e) {
124 Slog.e(TAG, "Exception sending taskAppeared callback" + e);
125 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800126 }
127
128 void removeTask(Task t) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800129 try {
130 mOrganizer.taskVanished(t.getRemoteToken());
131 } catch (Exception e) {
132 Slog.e(TAG, "Exception sending taskVanished callback" + e);
133 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800134 mOrganizedTasks.remove(t);
135 }
136
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800137 void dispose() {
138 mDisposed = true;
139 releaseTasks();
140 handleReplacement();
141 }
142
143 void releaseTasks() {
144 for (int i = mOrganizedTasks.size() - 1; i >= 0; i--) {
145 final Task t = mOrganizedTasks.get(i);
146 t.taskOrganizerDied();
147 removeTask(t);
148 }
149 }
150
151 void handleReplacement() {
152 if (mReplacementFor != null && !mReplacementFor.mDisposed) {
153 mTaskOrganizersForWindowingMode.put(mWindowingMode, mReplacementFor);
154 }
155 }
156
157 void unlinkDeath() {
158 mDisposed = true;
159 mOrganizer.asBinder().unlinkToDeath(mDeathRecipient, 0);
Robert Carr8a2f9132019-11-11 15:03:15 -0800160 }
161 };
162
163
164 final HashMap<Integer, TaskOrganizerState> mTaskOrganizersForWindowingMode = new HashMap();
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800165 final HashMap<IBinder, TaskOrganizerState> mTaskOrganizerStates = new HashMap();
Robert Carr8a2f9132019-11-11 15:03:15 -0800166
167 final HashMap<Integer, ITaskOrganizer> mTaskOrganizersByPendingSyncId = new HashMap();
168
Evan Rosky0037e5f2019-11-05 10:26:24 -0800169 private final WeakHashMap<Task, RunningTaskInfo> mLastSentTaskInfos = new WeakHashMap<>();
170 private final ArrayList<Task> mPendingTaskInfoChanges = new ArrayList<>();
171
Robert Carre10ee3d2019-11-11 15:03:15 -0800172 private final BLASTSyncEngine mBLASTSyncEngine = new BLASTSyncEngine();
173
Robert Carr8a2f9132019-11-11 15:03:15 -0800174 final ActivityTaskManagerService mService;
175
Evan Rosky0037e5f2019-11-05 10:26:24 -0800176 RunningTaskInfo mTmpTaskInfo;
177
178 TaskOrganizerController(ActivityTaskManagerService atm) {
Robert Carr8a2f9132019-11-11 15:03:15 -0800179 mService = atm;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800180 mGlobalLock = atm.mGlobalLock;
181 }
182
183 private void enforceStackPermission(String func) {
184 mService.mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, func);
Robert Carr8a2f9132019-11-11 15:03:15 -0800185 }
186
Robert Carr8a2f9132019-11-11 15:03:15 -0800187 /**
188 * Register a TaskOrganizer to manage tasks as they enter the given windowing mode.
189 * If there was already a TaskOrganizer for this windowing mode it will be evicted
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800190 * but will continue to organize it's existing tasks.
Robert Carr8a2f9132019-11-11 15:03:15 -0800191 */
Evan Rosky0037e5f2019-11-05 10:26:24 -0800192 @Override
193 public void registerTaskOrganizer(ITaskOrganizer organizer, int windowingMode) {
194 if (windowingMode != WINDOWING_MODE_PINNED
195 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
196 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
197 && windowingMode != WINDOWING_MODE_MULTI_WINDOW) {
198 throw new UnsupportedOperationException("As of now only Pinned/Split/Multiwindow"
199 + " windowing modes are supported for registerTaskOrganizer");
Robert Carr8a2f9132019-11-11 15:03:15 -0800200 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800201 enforceStackPermission("registerTaskOrganizer()");
202 final long origId = Binder.clearCallingIdentity();
Robert Carr8a2f9132019-11-11 15:03:15 -0800203 try {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800204 synchronized (mGlobalLock) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800205 final TaskOrganizerState state = new TaskOrganizerState(organizer, windowingMode,
206 mTaskOrganizersForWindowingMode.get(windowingMode));
Evan Rosky0037e5f2019-11-05 10:26:24 -0800207 mTaskOrganizersForWindowingMode.put(windowingMode, state);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800208 mTaskOrganizerStates.put(organizer.asBinder(), state);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800209 }
210 } finally {
211 Binder.restoreCallingIdentity(origId);
Robert Carr8a2f9132019-11-11 15:03:15 -0800212 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800213 }
214
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800215 void unregisterTaskOrganizer(ITaskOrganizer organizer) {
216 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
217 state.unlinkDeath();
218 if (mTaskOrganizersForWindowingMode.get(state.mWindowingMode) == state) {
219 mTaskOrganizersForWindowingMode.remove(state.mWindowingMode);
220 }
221 state.dispose();
222 }
223
Robert Carr8a2f9132019-11-11 15:03:15 -0800224 ITaskOrganizer getTaskOrganizer(int windowingMode) {
225 final TaskOrganizerState state = mTaskOrganizersForWindowingMode.get(windowingMode);
226 if (state == null) {
227 return null;
228 }
229 return state.mOrganizer;
230 }
231
Robert Carr8a2f9132019-11-11 15:03:15 -0800232 void onTaskAppeared(ITaskOrganizer organizer, Task task) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800233 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800234 state.addTask(task);
Robert Carr8a2f9132019-11-11 15:03:15 -0800235 }
236
237 void onTaskVanished(ITaskOrganizer organizer, Task task) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800238 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800239 state.removeTask(task);
240 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800241
242 @Override
243 public RunningTaskInfo createRootTask(int displayId, int windowingMode) {
244 enforceStackPermission("createRootTask()");
245 final long origId = Binder.clearCallingIdentity();
246 try {
247 synchronized (mGlobalLock) {
248 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
249 if (display == null) {
250 return null;
251 }
252 final int nextId = display.getNextStackId();
253 TaskTile tile = new TaskTile(mService, nextId, windowingMode);
254 display.addTile(tile);
255 RunningTaskInfo out = new RunningTaskInfo();
256 tile.fillTaskInfo(out);
257 mLastSentTaskInfos.put(tile, out);
258 return out;
259 }
260 } finally {
261 Binder.restoreCallingIdentity(origId);
262 }
263 }
264
265 @Override
266 public boolean deleteRootTask(IWindowContainer token) {
267 enforceStackPermission("deleteRootTask()");
268 final long origId = Binder.clearCallingIdentity();
269 try {
270 synchronized (mGlobalLock) {
271 TaskTile tile = TaskTile.forToken(token.asBinder());
272 if (tile == null) {
273 return false;
274 }
275 tile.removeImmediately();
276 return true;
277 }
278 } finally {
279 Binder.restoreCallingIdentity(origId);
280 }
281 }
282
283 void dispatchPendingTaskInfoChanges() {
284 if (mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
285 return;
286 }
287 for (int i = 0, n = mPendingTaskInfoChanges.size(); i < n; ++i) {
288 dispatchTaskInfoChanged(mPendingTaskInfoChanges.get(i), false /* force */);
289 }
290 mPendingTaskInfoChanges.clear();
291 }
292
293 void dispatchTaskInfoChanged(Task task, boolean force) {
294 if (!force && mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
295 // Defer task info reporting while layout is deferred. This is because layout defer
296 // blocks tend to do lots of re-ordering which can mess up animations in receivers.
297 mPendingTaskInfoChanges.remove(task);
298 mPendingTaskInfoChanges.add(task);
299 return;
300 }
301 RunningTaskInfo lastInfo = mLastSentTaskInfos.get(task);
302 if (mTmpTaskInfo == null) {
303 mTmpTaskInfo = new RunningTaskInfo();
304 }
305 task.fillTaskInfo(mTmpTaskInfo);
306 boolean changed = lastInfo == null
307 || mTmpTaskInfo.topActivityType != lastInfo.topActivityType
308 || mTmpTaskInfo.isResizable() != lastInfo.isResizable();
309 if (!(changed || force)) {
310 return;
311 }
312 final RunningTaskInfo newInfo = mTmpTaskInfo;
313 mLastSentTaskInfos.put(task, mTmpTaskInfo);
314 // Since we've stored this, clean up the reference so a new one will be created next time.
315 // Transferring it this way means we only have to construct new RunningTaskInfos when they
316 // change.
317 mTmpTaskInfo = null;
318
319 if (task.mTaskOrganizer != null) {
320 try {
321 task.mTaskOrganizer.onTaskInfoChanged(newInfo);
322 } catch (RemoteException e) {
323 }
324 }
325 }
326
327 @Override
328 public IWindowContainer getImeTarget(int displayId) {
329 enforceStackPermission("getImeTarget()");
330 final long origId = Binder.clearCallingIdentity();
331 try {
332 synchronized (mGlobalLock) {
333 DisplayContent dc = mService.mWindowManager.mRoot
334 .getDisplayContent(displayId);
335 if (dc == null || dc.mInputMethodTarget == null) {
336 return null;
337 }
338 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
339 final Task task = dc.mInputMethodTarget.getTask();
340 if (task == null) {
341 return null;
342 }
343 ActivityStack rootTask = (ActivityStack) task.getRootTask();
344 final TaskTile tile = rootTask.getTile();
345 if (tile != null) {
346 rootTask = tile;
347 }
348 return rootTask.mRemoteToken;
349 }
350 } finally {
351 Binder.restoreCallingIdentity(origId);
352 }
353 }
354
355 @Override
356 public void setLaunchRoot(int displayId, @Nullable IWindowContainer tile) {
357 enforceStackPermission("setLaunchRoot()");
358 final long origId = Binder.clearCallingIdentity();
359 try {
360 synchronized (mGlobalLock) {
361 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
362 if (display == null) {
363 return;
364 }
365 TaskTile taskTile = tile == null ? null : TaskTile.forToken(tile.asBinder());
366 if (taskTile == null) {
367 display.mLaunchTile = null;
368 return;
369 }
370 if (taskTile.getDisplay() != display) {
371 throw new RuntimeException("Can't set launch root for display " + displayId
372 + " to task on display " + taskTile.getDisplay().getDisplayId());
373 }
374 display.mLaunchTile = taskTile;
375 }
376 } finally {
377 Binder.restoreCallingIdentity(origId);
378 }
379 }
380
Evan Roskya8fde152020-01-07 19:09:13 -0800381 @Override
382 public List<RunningTaskInfo> getChildTasks(IWindowContainer parent) {
383 enforceStackPermission("getChildTasks()");
384 final long ident = Binder.clearCallingIdentity();
385 try {
386 synchronized (mGlobalLock) {
387 if (parent == null) {
388 throw new IllegalArgumentException("Can't get children of null parent");
389 }
390 final WindowContainer container = WindowContainer.fromBinder(parent.asBinder());
391 if (container == null) {
392 Slog.e(TAG, "Can't get children of " + parent + " because it is not valid.");
393 return null;
394 }
395 // For now, only support returning children of persistent root tasks (of which the
396 // only current implementation is TaskTile).
397 if (!(container instanceof TaskTile)) {
398 Slog.w(TAG, "Can only get children of root tasks created via createRootTask");
399 return null;
400 }
401 ArrayList<RunningTaskInfo> out = new ArrayList<>();
402 // Tiles aren't real parents, so we need to go through stacks on the display to
403 // ensure correct ordering.
404 final DisplayContent dc = container.getDisplayContent();
405 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
406 final ActivityStack as = dc.getStackAt(i);
407 if (as.getTile() == container) {
408 final RunningTaskInfo info = new RunningTaskInfo();
409 as.fillTaskInfo(info);
410 out.add(info);
411 }
412 }
413 return out;
414 }
415 } finally {
416 Binder.restoreCallingIdentity(ident);
417 }
418 }
419
Evan Rosky0037e5f2019-11-05 10:26:24 -0800420 private int sanitizeAndApplyChange(WindowContainer container,
421 WindowContainerTransaction.Change change) {
422 if (!(container instanceof Task)) {
423 throw new RuntimeException("Invalid token in task transaction");
424 }
425 // The "client"-facing API should prevent bad changes; however, just in case, sanitize
426 // masks here.
427 int configMask = change.getConfigSetMask();
428 int windowMask = change.getWindowSetMask();
429 configMask &= ActivityInfo.CONFIG_WINDOW_CONFIGURATION
430 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
431 windowMask &= WindowConfiguration.WINDOW_CONFIG_BOUNDS;
432 int effects = 0;
433 if (configMask != 0) {
434 Configuration c = new Configuration(container.getRequestedOverrideConfiguration());
435 c.setTo(change.getConfiguration(), configMask, windowMask);
436 container.onRequestedOverrideConfigurationChanged(c);
437 // TODO(b/145675353): remove the following once we could apply new bounds to the
438 // pinned stack together with its children.
439 resizePinnedStackIfNeeded(container, configMask, windowMask, c);
440 effects |= TRANSACT_EFFECTS_CLIENT_CONFIG;
441 }
442 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_FOCUSABLE) != 0) {
443 if (container.setFocusable(change.getFocusable())) {
444 effects |= TRANSACT_EFFECTS_LIFECYCLE;
445 }
446 }
447 return effects;
448 }
449
Evan Roskya8fde152020-01-07 19:09:13 -0800450 private int sanitizeAndApplyHierarchyOp(WindowContainer container,
451 WindowContainerTransaction.HierarchyOp hop) {
452 if (!(container instanceof Task)) {
453 throw new IllegalArgumentException("Invalid container in hierarchy op");
454 }
455 if (hop.isReparent()) {
456 // special case for tiles since they are "virtual" parents
457 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
458 ActivityStack as = (ActivityStack) container;
459 TaskTile newParent = hop.getNewParent() == null ? null
460 : (TaskTile) WindowContainer.fromBinder(hop.getNewParent());
461 if (as.getTile() != newParent) {
462 if (as.getTile() != null) {
463 as.getTile().removeChild(as);
464 }
465 if (newParent != null) {
466 if (!as.affectedBySplitScreenResize()) {
467 return 0;
468 }
469 newParent.addChild(as, POSITION_TOP);
470 }
471 }
472 if (hop.getToTop()) {
473 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
474 } else {
475 as.getDisplay().positionStackAtBottom(as);
476 }
477 } else if (container instanceof Task) {
478 throw new RuntimeException("Reparenting leaf Tasks is not supported now.");
479 }
480 } else {
481 // Ugh, of course ActivityStack has its own special reorder logic...
482 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
483 ActivityStack as = (ActivityStack) container;
484 if (hop.getToTop()) {
485 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
486 } else {
487 as.getDisplay().positionStackAtBottom(as);
488 }
489 } else {
490 container.getParent().positionChildAt(
491 hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
492 container, false /* includingParents */);
493 }
494 }
495 return TRANSACT_EFFECTS_LIFECYCLE;
496 }
497
Evan Rosky0037e5f2019-11-05 10:26:24 -0800498 private void resizePinnedStackIfNeeded(ConfigurationContainer container, int configMask,
499 int windowMask, Configuration config) {
500 if ((container instanceof ActivityStack)
501 && ((configMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0)
502 && ((windowMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0)) {
503 final ActivityStack stack = (ActivityStack) container;
504 if (stack.inPinnedWindowingMode()) {
505 stack.resize(config.windowConfiguration.getBounds(),
506 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
507 PRESERVE_WINDOWS, true /* deferResume */);
508 }
509 }
510 }
511
512 private int applyWindowContainerChange(WindowContainer wc,
513 WindowContainerTransaction.Change c) {
514 int effects = sanitizeAndApplyChange(wc, c);
515
516 Rect enterPipBounds = c.getEnterPipBounds();
517 if (enterPipBounds != null) {
518 Task tr = (Task) wc;
519 mService.mStackSupervisor.updatePictureInPictureMode(tr,
520 enterPipBounds, true);
521 }
522 return effects;
523 }
524
525 @Override
Robert Carre10ee3d2019-11-11 15:03:15 -0800526 public int applyContainerTransaction(WindowContainerTransaction t, ITaskOrganizer organizer) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800527 enforceStackPermission("applyContainerTransaction()");
Robert Carre10ee3d2019-11-11 15:03:15 -0800528 int syncId = -1;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800529 if (t == null) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800530 throw new IllegalArgumentException(
531 "Null transaction passed to applyContainerTransaction");
Evan Rosky0037e5f2019-11-05 10:26:24 -0800532 }
533 long ident = Binder.clearCallingIdentity();
534 try {
535 synchronized (mGlobalLock) {
536 int effects = 0;
Robert Carre10ee3d2019-11-11 15:03:15 -0800537
538 /**
539 * If organizer is non-null we are looking to synchronize this transaction
540 * by collecting all the results in to a SurfaceFlinger transaction and
541 * then delivering that to the given organizers transaction ready callback.
542 * See {@link BLASTSyncEngine} for the details of the operation. But at
543 * a high level we create a sync operation with a given ID and an associated
544 * organizer. Then we notify each WindowContainer in this WindowContainer
545 * transaction that it is participating in a sync operation with that
546 * ID. Once everything is notified we tell the BLASTSyncEngine
547 * "setSyncReady" which means that we have added everything
548 * to the set. At any point after this, all the WindowContainers
549 * will eventually finish applying their changes and notify the
550 * BLASTSyncEngine which will deliver the Transaction to the organizer.
551 */
552 if (organizer != null) {
553 syncId = startSyncWithOrganizer(organizer);
554 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800555 mService.deferWindowLayout();
556 try {
557 ArraySet<WindowContainer> haveConfigChanges = new ArraySet<>();
558 Iterator<Map.Entry<IBinder, WindowContainerTransaction.Change>> entries =
559 t.getChanges().entrySet().iterator();
560 while (entries.hasNext()) {
561 final Map.Entry<IBinder, WindowContainerTransaction.Change> entry =
562 entries.next();
Evan Roskya8fde152020-01-07 19:09:13 -0800563 final WindowContainer wc = WindowContainer.fromBinder(entry.getKey());
Evan Rosky0037e5f2019-11-05 10:26:24 -0800564 int containerEffect = applyWindowContainerChange(wc, entry.getValue());
565 effects |= containerEffect;
Robert Carre10ee3d2019-11-11 15:03:15 -0800566
Evan Rosky0037e5f2019-11-05 10:26:24 -0800567 // Lifecycle changes will trigger ensureConfig for everything.
568 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) == 0
569 && (containerEffect & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
570 haveConfigChanges.add(wc);
571 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800572 if (syncId >= 0) {
573 mBLASTSyncEngine.addToSyncSet(syncId, wc);
574 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800575 }
Evan Roskya8fde152020-01-07 19:09:13 -0800576 // Hierarchy changes
577 final List<WindowContainerTransaction.HierarchyOp> hops = t.getHierarchyOps();
578 for (int i = 0, n = hops.size(); i < n; ++i) {
579 final WindowContainerTransaction.HierarchyOp hop = hops.get(i);
580 final WindowContainer wc = WindowContainer.fromBinder(hop.getContainer());
581 effects |= sanitizeAndApplyHierarchyOp(wc, hop);
582 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800583 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) != 0) {
584 // Already calls ensureActivityConfig
585 mService.mRootWindowContainer.ensureActivitiesVisible(
586 null, 0, PRESERVE_WINDOWS);
587 } else if ((effects & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
588 final PooledConsumer f = PooledLambda.obtainConsumer(
589 ActivityRecord::ensureActivityConfiguration,
590 PooledLambda.__(ActivityRecord.class), 0,
591 false /* preserveWindow */);
592 try {
593 for (int i = haveConfigChanges.size() - 1; i >= 0; --i) {
594 haveConfigChanges.valueAt(i).forAllActivities(f);
595 }
596 } finally {
597 f.recycle();
598 }
599 }
600 } finally {
601 mService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800602 if (syncId >= 0) {
603 setSyncReady(syncId);
604 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800605 }
606 }
607 } finally {
608 Binder.restoreCallingIdentity(ident);
609 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800610 return syncId;
611 }
612
613 @Override
614 public void transactionReady(int id, SurfaceControl.Transaction sc) {
615 final ITaskOrganizer organizer = mTaskOrganizersByPendingSyncId.get(id);
616 if (organizer == null) {
617 Slog.e(TAG, "Got transaction complete for unexpected ID");
618 }
619 try {
620 organizer.transactionReady(id, sc);
621 } catch (RemoteException e) {
622 }
623
624 mTaskOrganizersByPendingSyncId.remove(id);
625 }
626
627 int startSyncWithOrganizer(ITaskOrganizer organizer) {
628 int id = mBLASTSyncEngine.startSyncSet(this);
629 mTaskOrganizersByPendingSyncId.put(id, organizer);
630 return id;
631 }
632
633 void setSyncReady(int id) {
634 mBLASTSyncEngine.setReady(id);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800635 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800636}