blob: cc52cb445a1aa8a220e963092761cd1c5b5156fb [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;
Robert Carrf6878a42019-12-18 02:13:12 -080026import static com.android.server.wm.Task.FLAG_FORCE_HIDDEN_FOR_TASK_ORG;
Evan Roskya8fde152020-01-07 19:09:13 -080027import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
28import static com.android.server.wm.WindowContainer.POSITION_TOP;
Evan Rosky0037e5f2019-11-05 10:26:24 -080029
30import android.annotation.Nullable;
31import android.app.ActivityManager.RunningTaskInfo;
32import android.app.ITaskOrganizerController;
33import android.app.WindowConfiguration;
34import android.content.pm.ActivityInfo;
35import android.content.res.Configuration;
36import android.graphics.Rect;
37import android.os.Binder;
Robert Carr8a2f9132019-11-11 15:03:15 -080038import android.os.IBinder;
39import android.os.RemoteException;
Evan Rosky0037e5f2019-11-05 10:26:24 -080040import android.util.ArraySet;
Robert Carr8a2f9132019-11-11 15:03:15 -080041import android.util.Slog;
42import android.view.ITaskOrganizer;
Evan Rosky0037e5f2019-11-05 10:26:24 -080043import android.view.IWindowContainer;
Robert Carre10ee3d2019-11-11 15:03:15 -080044import android.view.SurfaceControl;
Evan Rosky0037e5f2019-11-05 10:26:24 -080045import android.view.WindowContainerTransaction;
46
Evan Rosky29d4a0a2020-02-04 16:40:44 -080047import com.android.internal.util.ArrayUtils;
Evan Rosky0037e5f2019-11-05 10:26:24 -080048import com.android.internal.util.function.pooled.PooledConsumer;
49import com.android.internal.util.function.pooled.PooledLambda;
Robert Carr8a2f9132019-11-11 15:03:15 -080050
51import java.util.ArrayList;
52import java.util.HashMap;
Evan Rosky0037e5f2019-11-05 10:26:24 -080053import java.util.Iterator;
Evan Roskya8fde152020-01-07 19:09:13 -080054import java.util.List;
Evan Rosky0037e5f2019-11-05 10:26:24 -080055import java.util.Map;
56import java.util.WeakHashMap;
Robert Carr8a2f9132019-11-11 15:03:15 -080057
58/**
59 * Stores the TaskOrganizers associated with a given windowing mode and
60 * their associated state.
61 */
Robert Carre10ee3d2019-11-11 15:03:15 -080062class TaskOrganizerController extends ITaskOrganizerController.Stub
63 implements BLASTSyncEngine.TransactionReadyListener {
Robert Carr8a2f9132019-11-11 15:03:15 -080064 private static final String TAG = "TaskOrganizerController";
65
Evan Rosky0037e5f2019-11-05 10:26:24 -080066 /** Flag indicating that an applied transaction may have effected lifecycle */
67 private static final int TRANSACT_EFFECTS_CLIENT_CONFIG = 1;
68 private static final int TRANSACT_EFFECTS_LIFECYCLE = 1 << 1;
69
70 private final WindowManagerGlobalLock mGlobalLock;
Robert Carr8a2f9132019-11-11 15:03:15 -080071
72 private class DeathRecipient implements IBinder.DeathRecipient {
73 int mWindowingMode;
74 ITaskOrganizer mTaskOrganizer;
75
76 DeathRecipient(ITaskOrganizer organizer, int windowingMode) {
77 mTaskOrganizer = organizer;
78 mWindowingMode = windowingMode;
79 }
80
81 @Override
82 public void binderDied() {
83 synchronized (mGlobalLock) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -080084 final TaskOrganizerState state =
85 mTaskOrganizerStates.get(mTaskOrganizer.asBinder());
86 state.releaseTasks();
87 mTaskOrganizerStates.remove(mTaskOrganizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -080088 if (mTaskOrganizersForWindowingMode.get(mWindowingMode) == mTaskOrganizer) {
89 mTaskOrganizersForWindowingMode.remove(mWindowingMode);
90 }
91 }
92 }
93 };
94
95 class TaskOrganizerState {
96 ITaskOrganizer mOrganizer;
97 DeathRecipient mDeathRecipient;
Robert Carr7d7c8ab2020-01-28 15:57:23 -080098 int mWindowingMode;
Robert Carr8a2f9132019-11-11 15:03:15 -080099
100 ArrayList<Task> mOrganizedTasks = new ArrayList<>();
101
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800102 // Save the TaskOrganizer which we replaced registration for
103 // so it can be re-registered if we unregister.
104 TaskOrganizerState mReplacementFor;
105 boolean mDisposed = false;
106
107
108 TaskOrganizerState(ITaskOrganizer organizer, int windowingMode,
Winson Chung77338ab2020-03-09 16:32:34 -0700109 @Nullable TaskOrganizerState replacing) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800110 mOrganizer = organizer;
111 mDeathRecipient = new DeathRecipient(organizer, windowingMode);
112 try {
113 organizer.asBinder().linkToDeath(mDeathRecipient, 0);
114 } catch (RemoteException e) {
115 Slog.e(TAG, "TaskOrganizer failed to register death recipient");
116 }
117 mWindowingMode = windowingMode;
118 mReplacementFor = replacing;
119 }
120
Robert Carr8a2f9132019-11-11 15:03:15 -0800121 void addTask(Task t) {
122 mOrganizedTasks.add(t);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800123 try {
124 mOrganizer.taskAppeared(t.getTaskInfo());
125 } catch (Exception e) {
126 Slog.e(TAG, "Exception sending taskAppeared callback" + e);
127 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800128 }
129
130 void removeTask(Task t) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800131 try {
Rob Carrab179782020-03-10 12:50:30 -0700132 mOrganizer.taskVanished(t.getTaskInfo());
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800133 } catch (Exception e) {
134 Slog.e(TAG, "Exception sending taskVanished callback" + e);
135 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800136 mOrganizedTasks.remove(t);
137 }
138
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800139 void dispose() {
140 mDisposed = true;
141 releaseTasks();
142 handleReplacement();
143 }
144
145 void releaseTasks() {
146 for (int i = mOrganizedTasks.size() - 1; i >= 0; i--) {
147 final Task t = mOrganizedTasks.get(i);
148 t.taskOrganizerDied();
149 removeTask(t);
150 }
151 }
152
153 void handleReplacement() {
154 if (mReplacementFor != null && !mReplacementFor.mDisposed) {
155 mTaskOrganizersForWindowingMode.put(mWindowingMode, mReplacementFor);
156 }
157 }
158
159 void unlinkDeath() {
160 mDisposed = true;
161 mOrganizer.asBinder().unlinkToDeath(mDeathRecipient, 0);
Robert Carr8a2f9132019-11-11 15:03:15 -0800162 }
163 };
164
165
166 final HashMap<Integer, TaskOrganizerState> mTaskOrganizersForWindowingMode = new HashMap();
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800167 final HashMap<IBinder, TaskOrganizerState> mTaskOrganizerStates = new HashMap();
Robert Carr8a2f9132019-11-11 15:03:15 -0800168
169 final HashMap<Integer, ITaskOrganizer> mTaskOrganizersByPendingSyncId = new HashMap();
170
Evan Rosky0037e5f2019-11-05 10:26:24 -0800171 private final WeakHashMap<Task, RunningTaskInfo> mLastSentTaskInfos = new WeakHashMap<>();
172 private final ArrayList<Task> mPendingTaskInfoChanges = new ArrayList<>();
173
Robert Carre10ee3d2019-11-11 15:03:15 -0800174 private final BLASTSyncEngine mBLASTSyncEngine = new BLASTSyncEngine();
175
Robert Carr8a2f9132019-11-11 15:03:15 -0800176 final ActivityTaskManagerService mService;
177
Evan Rosky0037e5f2019-11-05 10:26:24 -0800178 RunningTaskInfo mTmpTaskInfo;
179
180 TaskOrganizerController(ActivityTaskManagerService atm) {
Robert Carr8a2f9132019-11-11 15:03:15 -0800181 mService = atm;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800182 mGlobalLock = atm.mGlobalLock;
183 }
184
185 private void enforceStackPermission(String func) {
186 mService.mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, func);
Robert Carr8a2f9132019-11-11 15:03:15 -0800187 }
188
Robert Carr8a2f9132019-11-11 15:03:15 -0800189 /**
190 * Register a TaskOrganizer to manage tasks as they enter the given windowing mode.
191 * If there was already a TaskOrganizer for this windowing mode it will be evicted
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800192 * but will continue to organize it's existing tasks.
Robert Carr8a2f9132019-11-11 15:03:15 -0800193 */
Evan Rosky0037e5f2019-11-05 10:26:24 -0800194 @Override
195 public void registerTaskOrganizer(ITaskOrganizer organizer, int windowingMode) {
196 if (windowingMode != WINDOWING_MODE_PINNED
197 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
198 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
199 && windowingMode != WINDOWING_MODE_MULTI_WINDOW) {
200 throw new UnsupportedOperationException("As of now only Pinned/Split/Multiwindow"
201 + " windowing modes are supported for registerTaskOrganizer");
Robert Carr8a2f9132019-11-11 15:03:15 -0800202 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800203 enforceStackPermission("registerTaskOrganizer()");
204 final long origId = Binder.clearCallingIdentity();
Robert Carr8a2f9132019-11-11 15:03:15 -0800205 try {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800206 synchronized (mGlobalLock) {
Winson Chung77338ab2020-03-09 16:32:34 -0700207 if (getTaskOrganizer(windowingMode) != null) {
208 Slog.w(TAG, "Task organizer already exists for windowing mode: "
209 + windowingMode);
210 }
211 final TaskOrganizerState previousState =
212 mTaskOrganizersForWindowingMode.get(windowingMode);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800213 final TaskOrganizerState state = new TaskOrganizerState(organizer, windowingMode,
Winson Chung77338ab2020-03-09 16:32:34 -0700214 previousState);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800215 mTaskOrganizersForWindowingMode.put(windowingMode, state);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800216 mTaskOrganizerStates.put(organizer.asBinder(), state);
Winson Chung77338ab2020-03-09 16:32:34 -0700217
218 if (previousState == null) {
219 // Only in the case where this is the root task organizer for the given
220 // windowing mode, we add report all existing tasks in that mode to the new
221 // task organizer.
222 mService.mRootWindowContainer.forAllTasks((task) -> {
223 if (task.getWindowingMode() == windowingMode) {
224 task.updateTaskOrganizerState(true /* forceUpdate */);
225 }
226 });
227 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800228 }
229 } finally {
230 Binder.restoreCallingIdentity(origId);
Robert Carr8a2f9132019-11-11 15:03:15 -0800231 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800232 }
233
Winson Chung8a168902020-03-12 22:39:22 -0700234 @Override
235 public void unregisterTaskOrganizer(ITaskOrganizer organizer) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800236 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
237 state.unlinkDeath();
238 if (mTaskOrganizersForWindowingMode.get(state.mWindowingMode) == state) {
239 mTaskOrganizersForWindowingMode.remove(state.mWindowingMode);
240 }
241 state.dispose();
242 }
243
Robert Carr8a2f9132019-11-11 15:03:15 -0800244 ITaskOrganizer getTaskOrganizer(int windowingMode) {
245 final TaskOrganizerState state = mTaskOrganizersForWindowingMode.get(windowingMode);
246 if (state == null) {
247 return null;
248 }
249 return state.mOrganizer;
250 }
251
Robert Carr8a2f9132019-11-11 15:03:15 -0800252 void onTaskAppeared(ITaskOrganizer organizer, Task task) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800253 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800254 state.addTask(task);
Robert Carr8a2f9132019-11-11 15:03:15 -0800255 }
256
257 void onTaskVanished(ITaskOrganizer organizer, Task task) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800258 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800259 state.removeTask(task);
260 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800261
262 @Override
263 public RunningTaskInfo createRootTask(int displayId, int windowingMode) {
264 enforceStackPermission("createRootTask()");
265 final long origId = Binder.clearCallingIdentity();
266 try {
267 synchronized (mGlobalLock) {
268 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
269 if (display == null) {
270 return null;
271 }
272 final int nextId = display.getNextStackId();
273 TaskTile tile = new TaskTile(mService, nextId, windowingMode);
274 display.addTile(tile);
Winson Chung66b08f02020-03-03 14:32:35 -0800275 RunningTaskInfo out = tile.getTaskInfo();
Evan Rosky0037e5f2019-11-05 10:26:24 -0800276 mLastSentTaskInfos.put(tile, out);
277 return out;
278 }
279 } finally {
280 Binder.restoreCallingIdentity(origId);
281 }
282 }
283
284 @Override
285 public boolean deleteRootTask(IWindowContainer token) {
286 enforceStackPermission("deleteRootTask()");
287 final long origId = Binder.clearCallingIdentity();
288 try {
289 synchronized (mGlobalLock) {
290 TaskTile tile = TaskTile.forToken(token.asBinder());
291 if (tile == null) {
292 return false;
293 }
294 tile.removeImmediately();
295 return true;
296 }
297 } finally {
298 Binder.restoreCallingIdentity(origId);
299 }
300 }
301
302 void dispatchPendingTaskInfoChanges() {
303 if (mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
304 return;
305 }
306 for (int i = 0, n = mPendingTaskInfoChanges.size(); i < n; ++i) {
307 dispatchTaskInfoChanged(mPendingTaskInfoChanges.get(i), false /* force */);
308 }
309 mPendingTaskInfoChanges.clear();
310 }
311
312 void dispatchTaskInfoChanged(Task task, boolean force) {
313 if (!force && mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
314 // Defer task info reporting while layout is deferred. This is because layout defer
315 // blocks tend to do lots of re-ordering which can mess up animations in receivers.
316 mPendingTaskInfoChanges.remove(task);
317 mPendingTaskInfoChanges.add(task);
318 return;
319 }
320 RunningTaskInfo lastInfo = mLastSentTaskInfos.get(task);
321 if (mTmpTaskInfo == null) {
322 mTmpTaskInfo = new RunningTaskInfo();
323 }
324 task.fillTaskInfo(mTmpTaskInfo);
325 boolean changed = lastInfo == null
326 || mTmpTaskInfo.topActivityType != lastInfo.topActivityType
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800327 || mTmpTaskInfo.isResizable() != lastInfo.isResizable()
328 || mTmpTaskInfo.pictureInPictureParams != lastInfo.pictureInPictureParams;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800329 if (!(changed || force)) {
330 return;
331 }
332 final RunningTaskInfo newInfo = mTmpTaskInfo;
333 mLastSentTaskInfos.put(task, mTmpTaskInfo);
334 // Since we've stored this, clean up the reference so a new one will be created next time.
335 // Transferring it this way means we only have to construct new RunningTaskInfos when they
336 // change.
337 mTmpTaskInfo = null;
338
339 if (task.mTaskOrganizer != null) {
340 try {
341 task.mTaskOrganizer.onTaskInfoChanged(newInfo);
342 } catch (RemoteException e) {
343 }
344 }
345 }
346
347 @Override
348 public IWindowContainer getImeTarget(int displayId) {
349 enforceStackPermission("getImeTarget()");
350 final long origId = Binder.clearCallingIdentity();
351 try {
352 synchronized (mGlobalLock) {
353 DisplayContent dc = mService.mWindowManager.mRoot
354 .getDisplayContent(displayId);
355 if (dc == null || dc.mInputMethodTarget == null) {
356 return null;
357 }
358 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
359 final Task task = dc.mInputMethodTarget.getTask();
360 if (task == null) {
361 return null;
362 }
363 ActivityStack rootTask = (ActivityStack) task.getRootTask();
364 final TaskTile tile = rootTask.getTile();
365 if (tile != null) {
366 rootTask = tile;
367 }
368 return rootTask.mRemoteToken;
369 }
370 } finally {
371 Binder.restoreCallingIdentity(origId);
372 }
373 }
374
375 @Override
376 public void setLaunchRoot(int displayId, @Nullable IWindowContainer tile) {
377 enforceStackPermission("setLaunchRoot()");
378 final long origId = Binder.clearCallingIdentity();
379 try {
380 synchronized (mGlobalLock) {
381 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
382 if (display == null) {
383 return;
384 }
385 TaskTile taskTile = tile == null ? null : TaskTile.forToken(tile.asBinder());
386 if (taskTile == null) {
387 display.mLaunchTile = null;
388 return;
389 }
390 if (taskTile.getDisplay() != display) {
391 throw new RuntimeException("Can't set launch root for display " + displayId
392 + " to task on display " + taskTile.getDisplay().getDisplayId());
393 }
394 display.mLaunchTile = taskTile;
395 }
396 } finally {
397 Binder.restoreCallingIdentity(origId);
398 }
399 }
400
Evan Roskya8fde152020-01-07 19:09:13 -0800401 @Override
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800402 public List<RunningTaskInfo> getChildTasks(IWindowContainer parent,
403 @Nullable int[] activityTypes) {
Evan Roskya8fde152020-01-07 19:09:13 -0800404 enforceStackPermission("getChildTasks()");
405 final long ident = Binder.clearCallingIdentity();
406 try {
407 synchronized (mGlobalLock) {
408 if (parent == null) {
409 throw new IllegalArgumentException("Can't get children of null parent");
410 }
411 final WindowContainer container = WindowContainer.fromBinder(parent.asBinder());
412 if (container == null) {
413 Slog.e(TAG, "Can't get children of " + parent + " because it is not valid.");
414 return null;
415 }
416 // For now, only support returning children of persistent root tasks (of which the
417 // only current implementation is TaskTile).
418 if (!(container instanceof TaskTile)) {
419 Slog.w(TAG, "Can only get children of root tasks created via createRootTask");
420 return null;
421 }
422 ArrayList<RunningTaskInfo> out = new ArrayList<>();
423 // Tiles aren't real parents, so we need to go through stacks on the display to
424 // ensure correct ordering.
425 final DisplayContent dc = container.getDisplayContent();
426 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
427 final ActivityStack as = dc.getStackAt(i);
428 if (as.getTile() == container) {
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800429 if (activityTypes != null
430 && !ArrayUtils.contains(activityTypes, as.getActivityType())) {
431 continue;
432 }
Winson Chung66b08f02020-03-03 14:32:35 -0800433 out.add(as.getTaskInfo());
Evan Roskya8fde152020-01-07 19:09:13 -0800434 }
435 }
436 return out;
437 }
438 } finally {
439 Binder.restoreCallingIdentity(ident);
440 }
441 }
442
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800443 @Override
444 public List<RunningTaskInfo> getRootTasks(int displayId, @Nullable int[] activityTypes) {
445 enforceStackPermission("getRootTasks()");
446 final long ident = Binder.clearCallingIdentity();
447 try {
448 synchronized (mGlobalLock) {
449 final DisplayContent dc =
450 mService.mRootWindowContainer.getDisplayContent(displayId);
451 if (dc == null) {
452 throw new IllegalArgumentException("Display " + displayId + " doesn't exist");
453 }
454 ArrayList<RunningTaskInfo> out = new ArrayList<>();
455 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
456 final ActivityStack task = dc.getStackAt(i);
457 if (task.getTile() != null) {
458 // a tile is supposed to look like a parent, so don't include their
459 // "children" here. They can be accessed via getChildTasks()
460 continue;
461 }
462 if (activityTypes != null
463 && !ArrayUtils.contains(activityTypes, task.getActivityType())) {
464 continue;
465 }
Winson Chung66b08f02020-03-03 14:32:35 -0800466 out.add(task.getTaskInfo());
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800467 }
468 return out;
469 }
470 } finally {
471 Binder.restoreCallingIdentity(ident);
472 }
473 }
474
Evan Rosky0037e5f2019-11-05 10:26:24 -0800475 private int sanitizeAndApplyChange(WindowContainer container,
476 WindowContainerTransaction.Change change) {
477 if (!(container instanceof Task)) {
478 throw new RuntimeException("Invalid token in task transaction");
479 }
Robert Carrf6878a42019-12-18 02:13:12 -0800480 final Task task = (Task) container;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800481 // The "client"-facing API should prevent bad changes; however, just in case, sanitize
482 // masks here.
483 int configMask = change.getConfigSetMask();
484 int windowMask = change.getWindowSetMask();
485 configMask &= ActivityInfo.CONFIG_WINDOW_CONFIGURATION
Evan Rosky05ec8862020-02-28 19:37:04 -0800486 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_SCREEN_SIZE;
487 windowMask &= (WindowConfiguration.WINDOW_CONFIG_BOUNDS
488 | WindowConfiguration.WINDOW_CONFIG_APP_BOUNDS);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800489 int effects = 0;
490 if (configMask != 0) {
491 Configuration c = new Configuration(container.getRequestedOverrideConfiguration());
492 c.setTo(change.getConfiguration(), configMask, windowMask);
493 container.onRequestedOverrideConfigurationChanged(c);
494 // TODO(b/145675353): remove the following once we could apply new bounds to the
495 // pinned stack together with its children.
496 resizePinnedStackIfNeeded(container, configMask, windowMask, c);
497 effects |= TRANSACT_EFFECTS_CLIENT_CONFIG;
498 }
499 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_FOCUSABLE) != 0) {
500 if (container.setFocusable(change.getFocusable())) {
501 effects |= TRANSACT_EFFECTS_LIFECYCLE;
502 }
503 }
Robert Carrf6878a42019-12-18 02:13:12 -0800504 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_HIDDEN) != 0) {
505 if (task.setForceHidden(FLAG_FORCE_HIDDEN_FOR_TASK_ORG, change.getHidden())) {
506 effects |= TRANSACT_EFFECTS_LIFECYCLE;
507 }
508 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800509 return effects;
510 }
511
Evan Roskya8fde152020-01-07 19:09:13 -0800512 private int sanitizeAndApplyHierarchyOp(WindowContainer container,
513 WindowContainerTransaction.HierarchyOp hop) {
514 if (!(container instanceof Task)) {
515 throw new IllegalArgumentException("Invalid container in hierarchy op");
516 }
Evan Roskyaf9f27c2020-02-18 18:58:35 +0000517 if (container.getDisplayContent() == null) {
518 Slog.w(TAG, "Container is no longer attached: " + container);
519 return 0;
520 }
Evan Roskya8fde152020-01-07 19:09:13 -0800521 if (hop.isReparent()) {
522 // special case for tiles since they are "virtual" parents
523 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
524 ActivityStack as = (ActivityStack) container;
525 TaskTile newParent = hop.getNewParent() == null ? null
526 : (TaskTile) WindowContainer.fromBinder(hop.getNewParent());
527 if (as.getTile() != newParent) {
528 if (as.getTile() != null) {
529 as.getTile().removeChild(as);
530 }
531 if (newParent != null) {
532 if (!as.affectedBySplitScreenResize()) {
533 return 0;
534 }
535 newParent.addChild(as, POSITION_TOP);
536 }
537 }
538 if (hop.getToTop()) {
539 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
540 } else {
541 as.getDisplay().positionStackAtBottom(as);
542 }
543 } else if (container instanceof Task) {
544 throw new RuntimeException("Reparenting leaf Tasks is not supported now.");
545 }
546 } else {
547 // Ugh, of course ActivityStack has its own special reorder logic...
548 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
549 ActivityStack as = (ActivityStack) container;
550 if (hop.getToTop()) {
551 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
552 } else {
553 as.getDisplay().positionStackAtBottom(as);
554 }
555 } else {
556 container.getParent().positionChildAt(
557 hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
558 container, false /* includingParents */);
559 }
560 }
561 return TRANSACT_EFFECTS_LIFECYCLE;
562 }
563
Evan Rosky0037e5f2019-11-05 10:26:24 -0800564 private void resizePinnedStackIfNeeded(ConfigurationContainer container, int configMask,
565 int windowMask, Configuration config) {
566 if ((container instanceof ActivityStack)
567 && ((configMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0)
568 && ((windowMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0)) {
569 final ActivityStack stack = (ActivityStack) container;
570 if (stack.inPinnedWindowingMode()) {
571 stack.resize(config.windowConfiguration.getBounds(),
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -0800572 null /* configBounds */, PRESERVE_WINDOWS, true /* deferResume */);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800573 }
574 }
575 }
576
577 private int applyWindowContainerChange(WindowContainer wc,
578 WindowContainerTransaction.Change c) {
579 int effects = sanitizeAndApplyChange(wc, c);
580
Robert Carr2bed6212020-02-20 16:55:07 -0800581 final Task tr = wc.asTask();
582
Robert Carr711e7052020-02-19 11:14:33 -0800583 final SurfaceControl.Transaction t = c.getBoundsChangeTransaction();
584 if (t != null) {
Robert Carr711e7052020-02-19 11:14:33 -0800585 tr.setMainWindowSizeChangeTransaction(t);
586 }
587
Evan Rosky0037e5f2019-11-05 10:26:24 -0800588 Rect enterPipBounds = c.getEnterPipBounds();
589 if (enterPipBounds != null) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800590 mService.mStackSupervisor.updatePictureInPictureMode(tr,
591 enterPipBounds, true);
592 }
Robert Carr2bed6212020-02-20 16:55:07 -0800593
594 final int windowingMode = c.getWindowingMode();
595 if (windowingMode > -1) {
596 tr.setWindowingMode(windowingMode);
597 }
598 final int childWindowingMode = c.getActivityWindowingMode();
599 if (childWindowingMode > -1) {
600 tr.setActivityWindowingMode(childWindowingMode);
601 }
602
Evan Rosky0037e5f2019-11-05 10:26:24 -0800603 return effects;
604 }
605
606 @Override
Robert Carre10ee3d2019-11-11 15:03:15 -0800607 public int applyContainerTransaction(WindowContainerTransaction t, ITaskOrganizer organizer) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800608 enforceStackPermission("applyContainerTransaction()");
Robert Carre10ee3d2019-11-11 15:03:15 -0800609 int syncId = -1;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800610 if (t == null) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800611 throw new IllegalArgumentException(
612 "Null transaction passed to applyContainerTransaction");
Evan Rosky0037e5f2019-11-05 10:26:24 -0800613 }
614 long ident = Binder.clearCallingIdentity();
615 try {
616 synchronized (mGlobalLock) {
617 int effects = 0;
Robert Carre10ee3d2019-11-11 15:03:15 -0800618
619 /**
620 * If organizer is non-null we are looking to synchronize this transaction
621 * by collecting all the results in to a SurfaceFlinger transaction and
622 * then delivering that to the given organizers transaction ready callback.
623 * See {@link BLASTSyncEngine} for the details of the operation. But at
624 * a high level we create a sync operation with a given ID and an associated
625 * organizer. Then we notify each WindowContainer in this WindowContainer
626 * transaction that it is participating in a sync operation with that
627 * ID. Once everything is notified we tell the BLASTSyncEngine
628 * "setSyncReady" which means that we have added everything
629 * to the set. At any point after this, all the WindowContainers
630 * will eventually finish applying their changes and notify the
631 * BLASTSyncEngine which will deliver the Transaction to the organizer.
632 */
633 if (organizer != null) {
634 syncId = startSyncWithOrganizer(organizer);
635 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800636 mService.deferWindowLayout();
637 try {
638 ArraySet<WindowContainer> haveConfigChanges = new ArraySet<>();
639 Iterator<Map.Entry<IBinder, WindowContainerTransaction.Change>> entries =
640 t.getChanges().entrySet().iterator();
641 while (entries.hasNext()) {
642 final Map.Entry<IBinder, WindowContainerTransaction.Change> entry =
643 entries.next();
Evan Roskya8fde152020-01-07 19:09:13 -0800644 final WindowContainer wc = WindowContainer.fromBinder(entry.getKey());
Evan Rosky0037e5f2019-11-05 10:26:24 -0800645 int containerEffect = applyWindowContainerChange(wc, entry.getValue());
646 effects |= containerEffect;
Robert Carre10ee3d2019-11-11 15:03:15 -0800647
Evan Rosky0037e5f2019-11-05 10:26:24 -0800648 // Lifecycle changes will trigger ensureConfig for everything.
649 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) == 0
650 && (containerEffect & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
651 haveConfigChanges.add(wc);
652 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800653 if (syncId >= 0) {
654 mBLASTSyncEngine.addToSyncSet(syncId, wc);
655 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800656 }
Evan Roskya8fde152020-01-07 19:09:13 -0800657 // Hierarchy changes
658 final List<WindowContainerTransaction.HierarchyOp> hops = t.getHierarchyOps();
659 for (int i = 0, n = hops.size(); i < n; ++i) {
660 final WindowContainerTransaction.HierarchyOp hop = hops.get(i);
661 final WindowContainer wc = WindowContainer.fromBinder(hop.getContainer());
662 effects |= sanitizeAndApplyHierarchyOp(wc, hop);
663 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800664 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) != 0) {
665 // Already calls ensureActivityConfig
666 mService.mRootWindowContainer.ensureActivitiesVisible(
667 null, 0, PRESERVE_WINDOWS);
668 } else if ((effects & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
669 final PooledConsumer f = PooledLambda.obtainConsumer(
670 ActivityRecord::ensureActivityConfiguration,
671 PooledLambda.__(ActivityRecord.class), 0,
672 false /* preserveWindow */);
673 try {
674 for (int i = haveConfigChanges.size() - 1; i >= 0; --i) {
675 haveConfigChanges.valueAt(i).forAllActivities(f);
676 }
677 } finally {
678 f.recycle();
679 }
680 }
681 } finally {
682 mService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800683 if (syncId >= 0) {
684 setSyncReady(syncId);
685 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800686 }
687 }
688 } finally {
689 Binder.restoreCallingIdentity(ident);
690 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800691 return syncId;
692 }
693
694 @Override
695 public void transactionReady(int id, SurfaceControl.Transaction sc) {
696 final ITaskOrganizer organizer = mTaskOrganizersByPendingSyncId.get(id);
697 if (organizer == null) {
698 Slog.e(TAG, "Got transaction complete for unexpected ID");
699 }
700 try {
701 organizer.transactionReady(id, sc);
702 } catch (RemoteException e) {
703 }
704
705 mTaskOrganizersByPendingSyncId.remove(id);
706 }
707
708 int startSyncWithOrganizer(ITaskOrganizer organizer) {
709 int id = mBLASTSyncEngine.startSyncSet(this);
710 mTaskOrganizersByPendingSyncId.put(id, organizer);
711 return id;
712 }
713
714 void setSyncReady(int id) {
715 mBLASTSyncEngine.setReady(id);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800716 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800717}