blob: 4b13a0c1f75dd64f47aeb5927cc4167b76671145 [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
Evan Rosky29d4a0a2020-02-04 16:40:44 -080046import com.android.internal.util.ArrayUtils;
Evan Rosky0037e5f2019-11-05 10:26:24 -080047import com.android.internal.util.function.pooled.PooledConsumer;
48import com.android.internal.util.function.pooled.PooledLambda;
Robert Carr8a2f9132019-11-11 15:03:15 -080049
50import java.util.ArrayList;
51import java.util.HashMap;
Evan Rosky0037e5f2019-11-05 10:26:24 -080052import java.util.Iterator;
Evan Roskya8fde152020-01-07 19:09:13 -080053import java.util.List;
Evan Rosky0037e5f2019-11-05 10:26:24 -080054import java.util.Map;
55import java.util.WeakHashMap;
Robert Carr8a2f9132019-11-11 15:03:15 -080056
57/**
58 * Stores the TaskOrganizers associated with a given windowing mode and
59 * their associated state.
60 */
Robert Carre10ee3d2019-11-11 15:03:15 -080061class TaskOrganizerController extends ITaskOrganizerController.Stub
62 implements BLASTSyncEngine.TransactionReadyListener {
Robert Carr8a2f9132019-11-11 15:03:15 -080063 private static final String TAG = "TaskOrganizerController";
64
Evan Rosky0037e5f2019-11-05 10:26:24 -080065 /** Flag indicating that an applied transaction may have effected lifecycle */
66 private static final int TRANSACT_EFFECTS_CLIENT_CONFIG = 1;
67 private static final int TRANSACT_EFFECTS_LIFECYCLE = 1 << 1;
68
69 private final WindowManagerGlobalLock mGlobalLock;
Robert Carr8a2f9132019-11-11 15:03:15 -080070
71 private class DeathRecipient implements IBinder.DeathRecipient {
72 int mWindowingMode;
73 ITaskOrganizer mTaskOrganizer;
74
75 DeathRecipient(ITaskOrganizer organizer, int windowingMode) {
76 mTaskOrganizer = organizer;
77 mWindowingMode = windowingMode;
78 }
79
80 @Override
81 public void binderDied() {
82 synchronized (mGlobalLock) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -080083 final TaskOrganizerState state =
84 mTaskOrganizerStates.get(mTaskOrganizer.asBinder());
85 state.releaseTasks();
86 mTaskOrganizerStates.remove(mTaskOrganizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -080087 if (mTaskOrganizersForWindowingMode.get(mWindowingMode) == mTaskOrganizer) {
88 mTaskOrganizersForWindowingMode.remove(mWindowingMode);
89 }
90 }
91 }
92 };
93
94 class TaskOrganizerState {
95 ITaskOrganizer mOrganizer;
96 DeathRecipient mDeathRecipient;
Robert Carr7d7c8ab2020-01-28 15:57:23 -080097 int mWindowingMode;
Robert Carr8a2f9132019-11-11 15:03:15 -080098
99 ArrayList<Task> mOrganizedTasks = new ArrayList<>();
100
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800101 // Save the TaskOrganizer which we replaced registration for
102 // so it can be re-registered if we unregister.
103 TaskOrganizerState mReplacementFor;
104 boolean mDisposed = false;
105
106
107 TaskOrganizerState(ITaskOrganizer organizer, int windowingMode,
108 TaskOrganizerState replacing) {
109 mOrganizer = organizer;
110 mDeathRecipient = new DeathRecipient(organizer, windowingMode);
111 try {
112 organizer.asBinder().linkToDeath(mDeathRecipient, 0);
113 } catch (RemoteException e) {
114 Slog.e(TAG, "TaskOrganizer failed to register death recipient");
115 }
116 mWindowingMode = windowingMode;
117 mReplacementFor = replacing;
118 }
119
Robert Carr8a2f9132019-11-11 15:03:15 -0800120 void addTask(Task t) {
121 mOrganizedTasks.add(t);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800122 try {
123 mOrganizer.taskAppeared(t.getTaskInfo());
124 } catch (Exception e) {
125 Slog.e(TAG, "Exception sending taskAppeared callback" + e);
126 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800127 }
128
129 void removeTask(Task t) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800130 try {
131 mOrganizer.taskVanished(t.getRemoteToken());
132 } catch (Exception e) {
133 Slog.e(TAG, "Exception sending taskVanished callback" + e);
134 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800135 mOrganizedTasks.remove(t);
136 }
137
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800138 void dispose() {
139 mDisposed = true;
140 releaseTasks();
141 handleReplacement();
142 }
143
144 void releaseTasks() {
145 for (int i = mOrganizedTasks.size() - 1; i >= 0; i--) {
146 final Task t = mOrganizedTasks.get(i);
147 t.taskOrganizerDied();
148 removeTask(t);
149 }
150 }
151
152 void handleReplacement() {
153 if (mReplacementFor != null && !mReplacementFor.mDisposed) {
154 mTaskOrganizersForWindowingMode.put(mWindowingMode, mReplacementFor);
155 }
156 }
157
158 void unlinkDeath() {
159 mDisposed = true;
160 mOrganizer.asBinder().unlinkToDeath(mDeathRecipient, 0);
Robert Carr8a2f9132019-11-11 15:03:15 -0800161 }
162 };
163
164
165 final HashMap<Integer, TaskOrganizerState> mTaskOrganizersForWindowingMode = new HashMap();
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800166 final HashMap<IBinder, TaskOrganizerState> mTaskOrganizerStates = new HashMap();
Robert Carr8a2f9132019-11-11 15:03:15 -0800167
168 final HashMap<Integer, ITaskOrganizer> mTaskOrganizersByPendingSyncId = new HashMap();
169
Evan Rosky0037e5f2019-11-05 10:26:24 -0800170 private final WeakHashMap<Task, RunningTaskInfo> mLastSentTaskInfos = new WeakHashMap<>();
171 private final ArrayList<Task> mPendingTaskInfoChanges = new ArrayList<>();
172
Robert Carre10ee3d2019-11-11 15:03:15 -0800173 private final BLASTSyncEngine mBLASTSyncEngine = new BLASTSyncEngine();
174
Robert Carr8a2f9132019-11-11 15:03:15 -0800175 final ActivityTaskManagerService mService;
176
Evan Rosky0037e5f2019-11-05 10:26:24 -0800177 RunningTaskInfo mTmpTaskInfo;
178
179 TaskOrganizerController(ActivityTaskManagerService atm) {
Robert Carr8a2f9132019-11-11 15:03:15 -0800180 mService = atm;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800181 mGlobalLock = atm.mGlobalLock;
182 }
183
184 private void enforceStackPermission(String func) {
185 mService.mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, func);
Robert Carr8a2f9132019-11-11 15:03:15 -0800186 }
187
Robert Carr8a2f9132019-11-11 15:03:15 -0800188 /**
189 * Register a TaskOrganizer to manage tasks as they enter the given windowing mode.
190 * If there was already a TaskOrganizer for this windowing mode it will be evicted
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800191 * but will continue to organize it's existing tasks.
Robert Carr8a2f9132019-11-11 15:03:15 -0800192 */
Evan Rosky0037e5f2019-11-05 10:26:24 -0800193 @Override
194 public void registerTaskOrganizer(ITaskOrganizer organizer, int windowingMode) {
195 if (windowingMode != WINDOWING_MODE_PINNED
196 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
197 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
198 && windowingMode != WINDOWING_MODE_MULTI_WINDOW) {
199 throw new UnsupportedOperationException("As of now only Pinned/Split/Multiwindow"
200 + " windowing modes are supported for registerTaskOrganizer");
Robert Carr8a2f9132019-11-11 15:03:15 -0800201 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800202 enforceStackPermission("registerTaskOrganizer()");
203 final long origId = Binder.clearCallingIdentity();
Robert Carr8a2f9132019-11-11 15:03:15 -0800204 try {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800205 synchronized (mGlobalLock) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800206 final TaskOrganizerState state = new TaskOrganizerState(organizer, windowingMode,
207 mTaskOrganizersForWindowingMode.get(windowingMode));
Evan Rosky0037e5f2019-11-05 10:26:24 -0800208 mTaskOrganizersForWindowingMode.put(windowingMode, state);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800209 mTaskOrganizerStates.put(organizer.asBinder(), state);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800210 }
211 } finally {
212 Binder.restoreCallingIdentity(origId);
Robert Carr8a2f9132019-11-11 15:03:15 -0800213 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800214 }
215
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800216 void unregisterTaskOrganizer(ITaskOrganizer organizer) {
217 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
218 state.unlinkDeath();
219 if (mTaskOrganizersForWindowingMode.get(state.mWindowingMode) == state) {
220 mTaskOrganizersForWindowingMode.remove(state.mWindowingMode);
221 }
222 state.dispose();
223 }
224
Robert Carr8a2f9132019-11-11 15:03:15 -0800225 ITaskOrganizer getTaskOrganizer(int windowingMode) {
226 final TaskOrganizerState state = mTaskOrganizersForWindowingMode.get(windowingMode);
227 if (state == null) {
228 return null;
229 }
230 return state.mOrganizer;
231 }
232
Robert Carr8a2f9132019-11-11 15:03:15 -0800233 void onTaskAppeared(ITaskOrganizer organizer, Task task) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800234 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800235 state.addTask(task);
Robert Carr8a2f9132019-11-11 15:03:15 -0800236 }
237
238 void onTaskVanished(ITaskOrganizer organizer, Task task) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800239 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800240 state.removeTask(task);
241 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800242
243 @Override
244 public RunningTaskInfo createRootTask(int displayId, int windowingMode) {
245 enforceStackPermission("createRootTask()");
246 final long origId = Binder.clearCallingIdentity();
247 try {
248 synchronized (mGlobalLock) {
249 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
250 if (display == null) {
251 return null;
252 }
253 final int nextId = display.getNextStackId();
254 TaskTile tile = new TaskTile(mService, nextId, windowingMode);
255 display.addTile(tile);
256 RunningTaskInfo out = new RunningTaskInfo();
257 tile.fillTaskInfo(out);
258 mLastSentTaskInfos.put(tile, out);
259 return out;
260 }
261 } finally {
262 Binder.restoreCallingIdentity(origId);
263 }
264 }
265
266 @Override
267 public boolean deleteRootTask(IWindowContainer token) {
268 enforceStackPermission("deleteRootTask()");
269 final long origId = Binder.clearCallingIdentity();
270 try {
271 synchronized (mGlobalLock) {
272 TaskTile tile = TaskTile.forToken(token.asBinder());
273 if (tile == null) {
274 return false;
275 }
276 tile.removeImmediately();
277 return true;
278 }
279 } finally {
280 Binder.restoreCallingIdentity(origId);
281 }
282 }
283
284 void dispatchPendingTaskInfoChanges() {
285 if (mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
286 return;
287 }
288 for (int i = 0, n = mPendingTaskInfoChanges.size(); i < n; ++i) {
289 dispatchTaskInfoChanged(mPendingTaskInfoChanges.get(i), false /* force */);
290 }
291 mPendingTaskInfoChanges.clear();
292 }
293
294 void dispatchTaskInfoChanged(Task task, boolean force) {
295 if (!force && mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
296 // Defer task info reporting while layout is deferred. This is because layout defer
297 // blocks tend to do lots of re-ordering which can mess up animations in receivers.
298 mPendingTaskInfoChanges.remove(task);
299 mPendingTaskInfoChanges.add(task);
300 return;
301 }
302 RunningTaskInfo lastInfo = mLastSentTaskInfos.get(task);
303 if (mTmpTaskInfo == null) {
304 mTmpTaskInfo = new RunningTaskInfo();
305 }
306 task.fillTaskInfo(mTmpTaskInfo);
307 boolean changed = lastInfo == null
308 || mTmpTaskInfo.topActivityType != lastInfo.topActivityType
309 || mTmpTaskInfo.isResizable() != lastInfo.isResizable();
310 if (!(changed || force)) {
311 return;
312 }
313 final RunningTaskInfo newInfo = mTmpTaskInfo;
314 mLastSentTaskInfos.put(task, mTmpTaskInfo);
315 // Since we've stored this, clean up the reference so a new one will be created next time.
316 // Transferring it this way means we only have to construct new RunningTaskInfos when they
317 // change.
318 mTmpTaskInfo = null;
319
320 if (task.mTaskOrganizer != null) {
321 try {
322 task.mTaskOrganizer.onTaskInfoChanged(newInfo);
323 } catch (RemoteException e) {
324 }
325 }
326 }
327
328 @Override
329 public IWindowContainer getImeTarget(int displayId) {
330 enforceStackPermission("getImeTarget()");
331 final long origId = Binder.clearCallingIdentity();
332 try {
333 synchronized (mGlobalLock) {
334 DisplayContent dc = mService.mWindowManager.mRoot
335 .getDisplayContent(displayId);
336 if (dc == null || dc.mInputMethodTarget == null) {
337 return null;
338 }
339 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
340 final Task task = dc.mInputMethodTarget.getTask();
341 if (task == null) {
342 return null;
343 }
344 ActivityStack rootTask = (ActivityStack) task.getRootTask();
345 final TaskTile tile = rootTask.getTile();
346 if (tile != null) {
347 rootTask = tile;
348 }
349 return rootTask.mRemoteToken;
350 }
351 } finally {
352 Binder.restoreCallingIdentity(origId);
353 }
354 }
355
356 @Override
357 public void setLaunchRoot(int displayId, @Nullable IWindowContainer tile) {
358 enforceStackPermission("setLaunchRoot()");
359 final long origId = Binder.clearCallingIdentity();
360 try {
361 synchronized (mGlobalLock) {
362 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
363 if (display == null) {
364 return;
365 }
366 TaskTile taskTile = tile == null ? null : TaskTile.forToken(tile.asBinder());
367 if (taskTile == null) {
368 display.mLaunchTile = null;
369 return;
370 }
371 if (taskTile.getDisplay() != display) {
372 throw new RuntimeException("Can't set launch root for display " + displayId
373 + " to task on display " + taskTile.getDisplay().getDisplayId());
374 }
375 display.mLaunchTile = taskTile;
376 }
377 } finally {
378 Binder.restoreCallingIdentity(origId);
379 }
380 }
381
Evan Roskya8fde152020-01-07 19:09:13 -0800382 @Override
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800383 public List<RunningTaskInfo> getChildTasks(IWindowContainer parent,
384 @Nullable int[] activityTypes) {
Evan Roskya8fde152020-01-07 19:09:13 -0800385 enforceStackPermission("getChildTasks()");
386 final long ident = Binder.clearCallingIdentity();
387 try {
388 synchronized (mGlobalLock) {
389 if (parent == null) {
390 throw new IllegalArgumentException("Can't get children of null parent");
391 }
392 final WindowContainer container = WindowContainer.fromBinder(parent.asBinder());
393 if (container == null) {
394 Slog.e(TAG, "Can't get children of " + parent + " because it is not valid.");
395 return null;
396 }
397 // For now, only support returning children of persistent root tasks (of which the
398 // only current implementation is TaskTile).
399 if (!(container instanceof TaskTile)) {
400 Slog.w(TAG, "Can only get children of root tasks created via createRootTask");
401 return null;
402 }
403 ArrayList<RunningTaskInfo> out = new ArrayList<>();
404 // Tiles aren't real parents, so we need to go through stacks on the display to
405 // ensure correct ordering.
406 final DisplayContent dc = container.getDisplayContent();
407 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
408 final ActivityStack as = dc.getStackAt(i);
409 if (as.getTile() == container) {
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800410 if (activityTypes != null
411 && !ArrayUtils.contains(activityTypes, as.getActivityType())) {
412 continue;
413 }
Evan Roskya8fde152020-01-07 19:09:13 -0800414 final RunningTaskInfo info = new RunningTaskInfo();
415 as.fillTaskInfo(info);
416 out.add(info);
417 }
418 }
419 return out;
420 }
421 } finally {
422 Binder.restoreCallingIdentity(ident);
423 }
424 }
425
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800426 @Override
427 public List<RunningTaskInfo> getRootTasks(int displayId, @Nullable int[] activityTypes) {
428 enforceStackPermission("getRootTasks()");
429 final long ident = Binder.clearCallingIdentity();
430 try {
431 synchronized (mGlobalLock) {
432 final DisplayContent dc =
433 mService.mRootWindowContainer.getDisplayContent(displayId);
434 if (dc == null) {
435 throw new IllegalArgumentException("Display " + displayId + " doesn't exist");
436 }
437 ArrayList<RunningTaskInfo> out = new ArrayList<>();
438 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
439 final ActivityStack task = dc.getStackAt(i);
440 if (task.getTile() != null) {
441 // a tile is supposed to look like a parent, so don't include their
442 // "children" here. They can be accessed via getChildTasks()
443 continue;
444 }
445 if (activityTypes != null
446 && !ArrayUtils.contains(activityTypes, task.getActivityType())) {
447 continue;
448 }
449 final RunningTaskInfo info = new RunningTaskInfo();
450 task.fillTaskInfo(info);
451 out.add(info);
452 }
453 return out;
454 }
455 } finally {
456 Binder.restoreCallingIdentity(ident);
457 }
458 }
459
Evan Rosky0037e5f2019-11-05 10:26:24 -0800460 private int sanitizeAndApplyChange(WindowContainer container,
461 WindowContainerTransaction.Change change) {
462 if (!(container instanceof Task)) {
463 throw new RuntimeException("Invalid token in task transaction");
464 }
465 // The "client"-facing API should prevent bad changes; however, just in case, sanitize
466 // masks here.
467 int configMask = change.getConfigSetMask();
468 int windowMask = change.getWindowSetMask();
469 configMask &= ActivityInfo.CONFIG_WINDOW_CONFIGURATION
470 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
471 windowMask &= WindowConfiguration.WINDOW_CONFIG_BOUNDS;
472 int effects = 0;
473 if (configMask != 0) {
474 Configuration c = new Configuration(container.getRequestedOverrideConfiguration());
475 c.setTo(change.getConfiguration(), configMask, windowMask);
476 container.onRequestedOverrideConfigurationChanged(c);
477 // TODO(b/145675353): remove the following once we could apply new bounds to the
478 // pinned stack together with its children.
479 resizePinnedStackIfNeeded(container, configMask, windowMask, c);
480 effects |= TRANSACT_EFFECTS_CLIENT_CONFIG;
481 }
482 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_FOCUSABLE) != 0) {
483 if (container.setFocusable(change.getFocusable())) {
484 effects |= TRANSACT_EFFECTS_LIFECYCLE;
485 }
486 }
487 return effects;
488 }
489
Evan Roskya8fde152020-01-07 19:09:13 -0800490 private int sanitizeAndApplyHierarchyOp(WindowContainer container,
491 WindowContainerTransaction.HierarchyOp hop) {
492 if (!(container instanceof Task)) {
493 throw new IllegalArgumentException("Invalid container in hierarchy op");
494 }
495 if (hop.isReparent()) {
496 // special case for tiles since they are "virtual" parents
497 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
498 ActivityStack as = (ActivityStack) container;
499 TaskTile newParent = hop.getNewParent() == null ? null
500 : (TaskTile) WindowContainer.fromBinder(hop.getNewParent());
501 if (as.getTile() != newParent) {
502 if (as.getTile() != null) {
503 as.getTile().removeChild(as);
504 }
505 if (newParent != null) {
506 if (!as.affectedBySplitScreenResize()) {
507 return 0;
508 }
509 newParent.addChild(as, POSITION_TOP);
510 }
511 }
512 if (hop.getToTop()) {
513 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
514 } else {
515 as.getDisplay().positionStackAtBottom(as);
516 }
517 } else if (container instanceof Task) {
518 throw new RuntimeException("Reparenting leaf Tasks is not supported now.");
519 }
520 } else {
521 // Ugh, of course ActivityStack has its own special reorder logic...
522 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
523 ActivityStack as = (ActivityStack) container;
524 if (hop.getToTop()) {
525 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
526 } else {
527 as.getDisplay().positionStackAtBottom(as);
528 }
529 } else {
530 container.getParent().positionChildAt(
531 hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
532 container, false /* includingParents */);
533 }
534 }
535 return TRANSACT_EFFECTS_LIFECYCLE;
536 }
537
Evan Rosky0037e5f2019-11-05 10:26:24 -0800538 private void resizePinnedStackIfNeeded(ConfigurationContainer container, int configMask,
539 int windowMask, Configuration config) {
540 if ((container instanceof ActivityStack)
541 && ((configMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0)
542 && ((windowMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0)) {
543 final ActivityStack stack = (ActivityStack) container;
544 if (stack.inPinnedWindowingMode()) {
545 stack.resize(config.windowConfiguration.getBounds(),
546 null /* tempTaskBounds */, null /* tempTaskInsetBounds */,
547 PRESERVE_WINDOWS, true /* deferResume */);
548 }
549 }
550 }
551
552 private int applyWindowContainerChange(WindowContainer wc,
553 WindowContainerTransaction.Change c) {
554 int effects = sanitizeAndApplyChange(wc, c);
555
556 Rect enterPipBounds = c.getEnterPipBounds();
557 if (enterPipBounds != null) {
558 Task tr = (Task) wc;
559 mService.mStackSupervisor.updatePictureInPictureMode(tr,
560 enterPipBounds, true);
561 }
562 return effects;
563 }
564
565 @Override
Robert Carre10ee3d2019-11-11 15:03:15 -0800566 public int applyContainerTransaction(WindowContainerTransaction t, ITaskOrganizer organizer) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800567 enforceStackPermission("applyContainerTransaction()");
Robert Carre10ee3d2019-11-11 15:03:15 -0800568 int syncId = -1;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800569 if (t == null) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800570 throw new IllegalArgumentException(
571 "Null transaction passed to applyContainerTransaction");
Evan Rosky0037e5f2019-11-05 10:26:24 -0800572 }
573 long ident = Binder.clearCallingIdentity();
574 try {
575 synchronized (mGlobalLock) {
576 int effects = 0;
Robert Carre10ee3d2019-11-11 15:03:15 -0800577
578 /**
579 * If organizer is non-null we are looking to synchronize this transaction
580 * by collecting all the results in to a SurfaceFlinger transaction and
581 * then delivering that to the given organizers transaction ready callback.
582 * See {@link BLASTSyncEngine} for the details of the operation. But at
583 * a high level we create a sync operation with a given ID and an associated
584 * organizer. Then we notify each WindowContainer in this WindowContainer
585 * transaction that it is participating in a sync operation with that
586 * ID. Once everything is notified we tell the BLASTSyncEngine
587 * "setSyncReady" which means that we have added everything
588 * to the set. At any point after this, all the WindowContainers
589 * will eventually finish applying their changes and notify the
590 * BLASTSyncEngine which will deliver the Transaction to the organizer.
591 */
592 if (organizer != null) {
593 syncId = startSyncWithOrganizer(organizer);
594 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800595 mService.deferWindowLayout();
596 try {
597 ArraySet<WindowContainer> haveConfigChanges = new ArraySet<>();
598 Iterator<Map.Entry<IBinder, WindowContainerTransaction.Change>> entries =
599 t.getChanges().entrySet().iterator();
600 while (entries.hasNext()) {
601 final Map.Entry<IBinder, WindowContainerTransaction.Change> entry =
602 entries.next();
Evan Roskya8fde152020-01-07 19:09:13 -0800603 final WindowContainer wc = WindowContainer.fromBinder(entry.getKey());
Evan Rosky0037e5f2019-11-05 10:26:24 -0800604 int containerEffect = applyWindowContainerChange(wc, entry.getValue());
605 effects |= containerEffect;
Robert Carre10ee3d2019-11-11 15:03:15 -0800606
Evan Rosky0037e5f2019-11-05 10:26:24 -0800607 // Lifecycle changes will trigger ensureConfig for everything.
608 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) == 0
609 && (containerEffect & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
610 haveConfigChanges.add(wc);
611 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800612 if (syncId >= 0) {
613 mBLASTSyncEngine.addToSyncSet(syncId, wc);
614 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800615 }
Evan Roskya8fde152020-01-07 19:09:13 -0800616 // Hierarchy changes
617 final List<WindowContainerTransaction.HierarchyOp> hops = t.getHierarchyOps();
618 for (int i = 0, n = hops.size(); i < n; ++i) {
619 final WindowContainerTransaction.HierarchyOp hop = hops.get(i);
620 final WindowContainer wc = WindowContainer.fromBinder(hop.getContainer());
621 effects |= sanitizeAndApplyHierarchyOp(wc, hop);
622 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800623 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) != 0) {
624 // Already calls ensureActivityConfig
625 mService.mRootWindowContainer.ensureActivitiesVisible(
626 null, 0, PRESERVE_WINDOWS);
627 } else if ((effects & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
628 final PooledConsumer f = PooledLambda.obtainConsumer(
629 ActivityRecord::ensureActivityConfiguration,
630 PooledLambda.__(ActivityRecord.class), 0,
631 false /* preserveWindow */);
632 try {
633 for (int i = haveConfigChanges.size() - 1; i >= 0; --i) {
634 haveConfigChanges.valueAt(i).forAllActivities(f);
635 }
636 } finally {
637 f.recycle();
638 }
639 }
640 } finally {
641 mService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800642 if (syncId >= 0) {
643 setSyncReady(syncId);
644 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800645 }
646 }
647 } finally {
648 Binder.restoreCallingIdentity(ident);
649 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800650 return syncId;
651 }
652
653 @Override
654 public void transactionReady(int id, SurfaceControl.Transaction sc) {
655 final ITaskOrganizer organizer = mTaskOrganizersByPendingSyncId.get(id);
656 if (organizer == null) {
657 Slog.e(TAG, "Got transaction complete for unexpected ID");
658 }
659 try {
660 organizer.transactionReady(id, sc);
661 } catch (RemoteException e) {
662 }
663
664 mTaskOrganizersByPendingSyncId.remove(id);
665 }
666
667 int startSyncWithOrganizer(ITaskOrganizer organizer) {
668 int id = mBLASTSyncEngine.startSyncSet(this);
669 mTaskOrganizersByPendingSyncId.put(id, organizer);
670 return id;
671 }
672
673 void setSyncReady(int id) {
674 mBLASTSyncEngine.setReady(id);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800675 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800676}