blob: 084a642bf38282180339dd963be8b1ed9c935950 [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,
Winson Chung77338ab2020-03-09 16:32:34 -0700108 @Nullable TaskOrganizerState replacing) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800109 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 {
Rob Carrab179782020-03-10 12:50:30 -0700131 mOrganizer.taskVanished(t.getTaskInfo());
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800132 } 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) {
Winson Chung77338ab2020-03-09 16:32:34 -0700206 if (getTaskOrganizer(windowingMode) != null) {
207 Slog.w(TAG, "Task organizer already exists for windowing mode: "
208 + windowingMode);
209 }
210 final TaskOrganizerState previousState =
211 mTaskOrganizersForWindowingMode.get(windowingMode);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800212 final TaskOrganizerState state = new TaskOrganizerState(organizer, windowingMode,
Winson Chung77338ab2020-03-09 16:32:34 -0700213 previousState);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800214 mTaskOrganizersForWindowingMode.put(windowingMode, state);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800215 mTaskOrganizerStates.put(organizer.asBinder(), state);
Winson Chung77338ab2020-03-09 16:32:34 -0700216
217 if (previousState == null) {
218 // Only in the case where this is the root task organizer for the given
219 // windowing mode, we add report all existing tasks in that mode to the new
220 // task organizer.
221 mService.mRootWindowContainer.forAllTasks((task) -> {
222 if (task.getWindowingMode() == windowingMode) {
223 task.updateTaskOrganizerState(true /* forceUpdate */);
224 }
225 });
226 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800227 }
228 } finally {
229 Binder.restoreCallingIdentity(origId);
Robert Carr8a2f9132019-11-11 15:03:15 -0800230 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800231 }
232
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800233 void unregisterTaskOrganizer(ITaskOrganizer organizer) {
234 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
235 state.unlinkDeath();
236 if (mTaskOrganizersForWindowingMode.get(state.mWindowingMode) == state) {
237 mTaskOrganizersForWindowingMode.remove(state.mWindowingMode);
238 }
239 state.dispose();
240 }
241
Robert Carr8a2f9132019-11-11 15:03:15 -0800242 ITaskOrganizer getTaskOrganizer(int windowingMode) {
243 final TaskOrganizerState state = mTaskOrganizersForWindowingMode.get(windowingMode);
244 if (state == null) {
245 return null;
246 }
247 return state.mOrganizer;
248 }
249
Robert Carr8a2f9132019-11-11 15:03:15 -0800250 void onTaskAppeared(ITaskOrganizer organizer, Task task) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800251 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800252 state.addTask(task);
Robert Carr8a2f9132019-11-11 15:03:15 -0800253 }
254
255 void onTaskVanished(ITaskOrganizer organizer, Task task) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800256 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800257 state.removeTask(task);
258 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800259
260 @Override
261 public RunningTaskInfo createRootTask(int displayId, int windowingMode) {
262 enforceStackPermission("createRootTask()");
263 final long origId = Binder.clearCallingIdentity();
264 try {
265 synchronized (mGlobalLock) {
266 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
267 if (display == null) {
268 return null;
269 }
270 final int nextId = display.getNextStackId();
271 TaskTile tile = new TaskTile(mService, nextId, windowingMode);
272 display.addTile(tile);
Winson Chung66b08f02020-03-03 14:32:35 -0800273 RunningTaskInfo out = tile.getTaskInfo();
Evan Rosky0037e5f2019-11-05 10:26:24 -0800274 mLastSentTaskInfos.put(tile, out);
275 return out;
276 }
277 } finally {
278 Binder.restoreCallingIdentity(origId);
279 }
280 }
281
282 @Override
283 public boolean deleteRootTask(IWindowContainer token) {
284 enforceStackPermission("deleteRootTask()");
285 final long origId = Binder.clearCallingIdentity();
286 try {
287 synchronized (mGlobalLock) {
288 TaskTile tile = TaskTile.forToken(token.asBinder());
289 if (tile == null) {
290 return false;
291 }
292 tile.removeImmediately();
293 return true;
294 }
295 } finally {
296 Binder.restoreCallingIdentity(origId);
297 }
298 }
299
300 void dispatchPendingTaskInfoChanges() {
301 if (mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
302 return;
303 }
304 for (int i = 0, n = mPendingTaskInfoChanges.size(); i < n; ++i) {
305 dispatchTaskInfoChanged(mPendingTaskInfoChanges.get(i), false /* force */);
306 }
307 mPendingTaskInfoChanges.clear();
308 }
309
310 void dispatchTaskInfoChanged(Task task, boolean force) {
311 if (!force && mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
312 // Defer task info reporting while layout is deferred. This is because layout defer
313 // blocks tend to do lots of re-ordering which can mess up animations in receivers.
314 mPendingTaskInfoChanges.remove(task);
315 mPendingTaskInfoChanges.add(task);
316 return;
317 }
318 RunningTaskInfo lastInfo = mLastSentTaskInfos.get(task);
319 if (mTmpTaskInfo == null) {
320 mTmpTaskInfo = new RunningTaskInfo();
321 }
322 task.fillTaskInfo(mTmpTaskInfo);
323 boolean changed = lastInfo == null
324 || mTmpTaskInfo.topActivityType != lastInfo.topActivityType
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800325 || mTmpTaskInfo.isResizable() != lastInfo.isResizable()
326 || mTmpTaskInfo.pictureInPictureParams != lastInfo.pictureInPictureParams;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800327 if (!(changed || force)) {
328 return;
329 }
330 final RunningTaskInfo newInfo = mTmpTaskInfo;
331 mLastSentTaskInfos.put(task, mTmpTaskInfo);
332 // Since we've stored this, clean up the reference so a new one will be created next time.
333 // Transferring it this way means we only have to construct new RunningTaskInfos when they
334 // change.
335 mTmpTaskInfo = null;
336
337 if (task.mTaskOrganizer != null) {
338 try {
339 task.mTaskOrganizer.onTaskInfoChanged(newInfo);
340 } catch (RemoteException e) {
341 }
342 }
343 }
344
345 @Override
346 public IWindowContainer getImeTarget(int displayId) {
347 enforceStackPermission("getImeTarget()");
348 final long origId = Binder.clearCallingIdentity();
349 try {
350 synchronized (mGlobalLock) {
351 DisplayContent dc = mService.mWindowManager.mRoot
352 .getDisplayContent(displayId);
353 if (dc == null || dc.mInputMethodTarget == null) {
354 return null;
355 }
356 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
357 final Task task = dc.mInputMethodTarget.getTask();
358 if (task == null) {
359 return null;
360 }
361 ActivityStack rootTask = (ActivityStack) task.getRootTask();
362 final TaskTile tile = rootTask.getTile();
363 if (tile != null) {
364 rootTask = tile;
365 }
366 return rootTask.mRemoteToken;
367 }
368 } finally {
369 Binder.restoreCallingIdentity(origId);
370 }
371 }
372
373 @Override
374 public void setLaunchRoot(int displayId, @Nullable IWindowContainer tile) {
375 enforceStackPermission("setLaunchRoot()");
376 final long origId = Binder.clearCallingIdentity();
377 try {
378 synchronized (mGlobalLock) {
379 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
380 if (display == null) {
381 return;
382 }
383 TaskTile taskTile = tile == null ? null : TaskTile.forToken(tile.asBinder());
384 if (taskTile == null) {
385 display.mLaunchTile = null;
386 return;
387 }
388 if (taskTile.getDisplay() != display) {
389 throw new RuntimeException("Can't set launch root for display " + displayId
390 + " to task on display " + taskTile.getDisplay().getDisplayId());
391 }
392 display.mLaunchTile = taskTile;
393 }
394 } finally {
395 Binder.restoreCallingIdentity(origId);
396 }
397 }
398
Evan Roskya8fde152020-01-07 19:09:13 -0800399 @Override
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800400 public List<RunningTaskInfo> getChildTasks(IWindowContainer parent,
401 @Nullable int[] activityTypes) {
Evan Roskya8fde152020-01-07 19:09:13 -0800402 enforceStackPermission("getChildTasks()");
403 final long ident = Binder.clearCallingIdentity();
404 try {
405 synchronized (mGlobalLock) {
406 if (parent == null) {
407 throw new IllegalArgumentException("Can't get children of null parent");
408 }
409 final WindowContainer container = WindowContainer.fromBinder(parent.asBinder());
410 if (container == null) {
411 Slog.e(TAG, "Can't get children of " + parent + " because it is not valid.");
412 return null;
413 }
414 // For now, only support returning children of persistent root tasks (of which the
415 // only current implementation is TaskTile).
416 if (!(container instanceof TaskTile)) {
417 Slog.w(TAG, "Can only get children of root tasks created via createRootTask");
418 return null;
419 }
420 ArrayList<RunningTaskInfo> out = new ArrayList<>();
421 // Tiles aren't real parents, so we need to go through stacks on the display to
422 // ensure correct ordering.
423 final DisplayContent dc = container.getDisplayContent();
424 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
425 final ActivityStack as = dc.getStackAt(i);
426 if (as.getTile() == container) {
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800427 if (activityTypes != null
428 && !ArrayUtils.contains(activityTypes, as.getActivityType())) {
429 continue;
430 }
Winson Chung66b08f02020-03-03 14:32:35 -0800431 out.add(as.getTaskInfo());
Evan Roskya8fde152020-01-07 19:09:13 -0800432 }
433 }
434 return out;
435 }
436 } finally {
437 Binder.restoreCallingIdentity(ident);
438 }
439 }
440
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800441 @Override
442 public List<RunningTaskInfo> getRootTasks(int displayId, @Nullable int[] activityTypes) {
443 enforceStackPermission("getRootTasks()");
444 final long ident = Binder.clearCallingIdentity();
445 try {
446 synchronized (mGlobalLock) {
447 final DisplayContent dc =
448 mService.mRootWindowContainer.getDisplayContent(displayId);
449 if (dc == null) {
450 throw new IllegalArgumentException("Display " + displayId + " doesn't exist");
451 }
452 ArrayList<RunningTaskInfo> out = new ArrayList<>();
453 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
454 final ActivityStack task = dc.getStackAt(i);
455 if (task.getTile() != null) {
456 // a tile is supposed to look like a parent, so don't include their
457 // "children" here. They can be accessed via getChildTasks()
458 continue;
459 }
460 if (activityTypes != null
461 && !ArrayUtils.contains(activityTypes, task.getActivityType())) {
462 continue;
463 }
Winson Chung66b08f02020-03-03 14:32:35 -0800464 out.add(task.getTaskInfo());
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800465 }
466 return out;
467 }
468 } finally {
469 Binder.restoreCallingIdentity(ident);
470 }
471 }
472
Evan Rosky0037e5f2019-11-05 10:26:24 -0800473 private int sanitizeAndApplyChange(WindowContainer container,
474 WindowContainerTransaction.Change change) {
475 if (!(container instanceof Task)) {
476 throw new RuntimeException("Invalid token in task transaction");
477 }
478 // The "client"-facing API should prevent bad changes; however, just in case, sanitize
479 // masks here.
480 int configMask = change.getConfigSetMask();
481 int windowMask = change.getWindowSetMask();
482 configMask &= ActivityInfo.CONFIG_WINDOW_CONFIGURATION
Evan Rosky05ec8862020-02-28 19:37:04 -0800483 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_SCREEN_SIZE;
484 windowMask &= (WindowConfiguration.WINDOW_CONFIG_BOUNDS
485 | WindowConfiguration.WINDOW_CONFIG_APP_BOUNDS);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800486 int effects = 0;
487 if (configMask != 0) {
488 Configuration c = new Configuration(container.getRequestedOverrideConfiguration());
489 c.setTo(change.getConfiguration(), configMask, windowMask);
490 container.onRequestedOverrideConfigurationChanged(c);
491 // TODO(b/145675353): remove the following once we could apply new bounds to the
492 // pinned stack together with its children.
493 resizePinnedStackIfNeeded(container, configMask, windowMask, c);
494 effects |= TRANSACT_EFFECTS_CLIENT_CONFIG;
495 }
496 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_FOCUSABLE) != 0) {
497 if (container.setFocusable(change.getFocusable())) {
498 effects |= TRANSACT_EFFECTS_LIFECYCLE;
499 }
500 }
501 return effects;
502 }
503
Evan Roskya8fde152020-01-07 19:09:13 -0800504 private int sanitizeAndApplyHierarchyOp(WindowContainer container,
505 WindowContainerTransaction.HierarchyOp hop) {
506 if (!(container instanceof Task)) {
507 throw new IllegalArgumentException("Invalid container in hierarchy op");
508 }
Evan Roskyaf9f27c2020-02-18 18:58:35 +0000509 if (container.getDisplayContent() == null) {
510 Slog.w(TAG, "Container is no longer attached: " + container);
511 return 0;
512 }
Evan Roskya8fde152020-01-07 19:09:13 -0800513 if (hop.isReparent()) {
514 // special case for tiles since they are "virtual" parents
515 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
516 ActivityStack as = (ActivityStack) container;
517 TaskTile newParent = hop.getNewParent() == null ? null
518 : (TaskTile) WindowContainer.fromBinder(hop.getNewParent());
519 if (as.getTile() != newParent) {
520 if (as.getTile() != null) {
521 as.getTile().removeChild(as);
522 }
523 if (newParent != null) {
524 if (!as.affectedBySplitScreenResize()) {
525 return 0;
526 }
527 newParent.addChild(as, POSITION_TOP);
528 }
529 }
530 if (hop.getToTop()) {
531 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
532 } else {
533 as.getDisplay().positionStackAtBottom(as);
534 }
535 } else if (container instanceof Task) {
536 throw new RuntimeException("Reparenting leaf Tasks is not supported now.");
537 }
538 } else {
539 // Ugh, of course ActivityStack has its own special reorder logic...
540 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
541 ActivityStack as = (ActivityStack) container;
542 if (hop.getToTop()) {
543 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
544 } else {
545 as.getDisplay().positionStackAtBottom(as);
546 }
547 } else {
548 container.getParent().positionChildAt(
549 hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
550 container, false /* includingParents */);
551 }
552 }
553 return TRANSACT_EFFECTS_LIFECYCLE;
554 }
555
Evan Rosky0037e5f2019-11-05 10:26:24 -0800556 private void resizePinnedStackIfNeeded(ConfigurationContainer container, int configMask,
557 int windowMask, Configuration config) {
558 if ((container instanceof ActivityStack)
559 && ((configMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0)
560 && ((windowMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0)) {
561 final ActivityStack stack = (ActivityStack) container;
562 if (stack.inPinnedWindowingMode()) {
563 stack.resize(config.windowConfiguration.getBounds(),
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -0800564 null /* configBounds */, PRESERVE_WINDOWS, true /* deferResume */);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800565 }
566 }
567 }
568
569 private int applyWindowContainerChange(WindowContainer wc,
570 WindowContainerTransaction.Change c) {
571 int effects = sanitizeAndApplyChange(wc, c);
572
Robert Carr711e7052020-02-19 11:14:33 -0800573 final SurfaceControl.Transaction t = c.getBoundsChangeTransaction();
574 if (t != null) {
575 Task tr = (Task) wc;
576 tr.setMainWindowSizeChangeTransaction(t);
577 }
578
Evan Rosky0037e5f2019-11-05 10:26:24 -0800579 Rect enterPipBounds = c.getEnterPipBounds();
580 if (enterPipBounds != null) {
581 Task tr = (Task) wc;
582 mService.mStackSupervisor.updatePictureInPictureMode(tr,
583 enterPipBounds, true);
584 }
585 return effects;
586 }
587
588 @Override
Robert Carre10ee3d2019-11-11 15:03:15 -0800589 public int applyContainerTransaction(WindowContainerTransaction t, ITaskOrganizer organizer) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800590 enforceStackPermission("applyContainerTransaction()");
Robert Carre10ee3d2019-11-11 15:03:15 -0800591 int syncId = -1;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800592 if (t == null) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800593 throw new IllegalArgumentException(
594 "Null transaction passed to applyContainerTransaction");
Evan Rosky0037e5f2019-11-05 10:26:24 -0800595 }
596 long ident = Binder.clearCallingIdentity();
597 try {
598 synchronized (mGlobalLock) {
599 int effects = 0;
Robert Carre10ee3d2019-11-11 15:03:15 -0800600
601 /**
602 * If organizer is non-null we are looking to synchronize this transaction
603 * by collecting all the results in to a SurfaceFlinger transaction and
604 * then delivering that to the given organizers transaction ready callback.
605 * See {@link BLASTSyncEngine} for the details of the operation. But at
606 * a high level we create a sync operation with a given ID and an associated
607 * organizer. Then we notify each WindowContainer in this WindowContainer
608 * transaction that it is participating in a sync operation with that
609 * ID. Once everything is notified we tell the BLASTSyncEngine
610 * "setSyncReady" which means that we have added everything
611 * to the set. At any point after this, all the WindowContainers
612 * will eventually finish applying their changes and notify the
613 * BLASTSyncEngine which will deliver the Transaction to the organizer.
614 */
615 if (organizer != null) {
616 syncId = startSyncWithOrganizer(organizer);
617 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800618 mService.deferWindowLayout();
619 try {
620 ArraySet<WindowContainer> haveConfigChanges = new ArraySet<>();
621 Iterator<Map.Entry<IBinder, WindowContainerTransaction.Change>> entries =
622 t.getChanges().entrySet().iterator();
623 while (entries.hasNext()) {
624 final Map.Entry<IBinder, WindowContainerTransaction.Change> entry =
625 entries.next();
Evan Roskya8fde152020-01-07 19:09:13 -0800626 final WindowContainer wc = WindowContainer.fromBinder(entry.getKey());
Evan Rosky0037e5f2019-11-05 10:26:24 -0800627 int containerEffect = applyWindowContainerChange(wc, entry.getValue());
628 effects |= containerEffect;
Robert Carre10ee3d2019-11-11 15:03:15 -0800629
Evan Rosky0037e5f2019-11-05 10:26:24 -0800630 // Lifecycle changes will trigger ensureConfig for everything.
631 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) == 0
632 && (containerEffect & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
633 haveConfigChanges.add(wc);
634 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800635 if (syncId >= 0) {
636 mBLASTSyncEngine.addToSyncSet(syncId, wc);
637 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800638 }
Evan Roskya8fde152020-01-07 19:09:13 -0800639 // Hierarchy changes
640 final List<WindowContainerTransaction.HierarchyOp> hops = t.getHierarchyOps();
641 for (int i = 0, n = hops.size(); i < n; ++i) {
642 final WindowContainerTransaction.HierarchyOp hop = hops.get(i);
643 final WindowContainer wc = WindowContainer.fromBinder(hop.getContainer());
644 effects |= sanitizeAndApplyHierarchyOp(wc, hop);
645 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800646 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) != 0) {
647 // Already calls ensureActivityConfig
648 mService.mRootWindowContainer.ensureActivitiesVisible(
649 null, 0, PRESERVE_WINDOWS);
650 } else if ((effects & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
651 final PooledConsumer f = PooledLambda.obtainConsumer(
652 ActivityRecord::ensureActivityConfiguration,
653 PooledLambda.__(ActivityRecord.class), 0,
654 false /* preserveWindow */);
655 try {
656 for (int i = haveConfigChanges.size() - 1; i >= 0; --i) {
657 haveConfigChanges.valueAt(i).forAllActivities(f);
658 }
659 } finally {
660 f.recycle();
661 }
662 }
663 } finally {
664 mService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800665 if (syncId >= 0) {
666 setSyncReady(syncId);
667 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800668 }
669 }
670 } finally {
671 Binder.restoreCallingIdentity(ident);
672 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800673 return syncId;
674 }
675
676 @Override
677 public void transactionReady(int id, SurfaceControl.Transaction sc) {
678 final ITaskOrganizer organizer = mTaskOrganizersByPendingSyncId.get(id);
679 if (organizer == null) {
680 Slog.e(TAG, "Got transaction complete for unexpected ID");
681 }
682 try {
683 organizer.transactionReady(id, sc);
684 } catch (RemoteException e) {
685 }
686
687 mTaskOrganizersByPendingSyncId.remove(id);
688 }
689
690 int startSyncWithOrganizer(ITaskOrganizer organizer) {
691 int id = mBLASTSyncEngine.startSyncSet(this);
692 mTaskOrganizersByPendingSyncId.put(id, organizer);
693 return id;
694 }
695
696 void setSyncReady(int id) {
697 mBLASTSyncEngine.setReady(id);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800698 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800699}