blob: b0dbc8275b025bff495de14b5433aa886770157e [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
Evan Roskyf64f5da2020-03-16 13:47:48 -070070 /**
71 * Masks specifying which configurations task-organizers can control. Incoming transactions
72 * will be filtered to only include these.
73 */
74 private static final int CONTROLLABLE_CONFIGS = ActivityInfo.CONFIG_WINDOW_CONFIGURATION
75 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE | ActivityInfo.CONFIG_SCREEN_SIZE;
76 private static final int CONTROLLABLE_WINDOW_CONFIGS = WindowConfiguration.WINDOW_CONFIG_BOUNDS
77 | WindowConfiguration.WINDOW_CONFIG_APP_BOUNDS;
78 /**
79 * Masks specifying which configurations are important to report back to an organizer when
80 * changed.
81 */
82 private static final int REPORT_CONFIGS = CONTROLLABLE_CONFIGS;
83 private static final int REPORT_WINDOW_CONFIGS = CONTROLLABLE_WINDOW_CONFIGS;
84
Evan Rosky0037e5f2019-11-05 10:26:24 -080085 private final WindowManagerGlobalLock mGlobalLock;
Robert Carr8a2f9132019-11-11 15:03:15 -080086
87 private class DeathRecipient implements IBinder.DeathRecipient {
88 int mWindowingMode;
89 ITaskOrganizer mTaskOrganizer;
90
91 DeathRecipient(ITaskOrganizer organizer, int windowingMode) {
92 mTaskOrganizer = organizer;
93 mWindowingMode = windowingMode;
94 }
95
96 @Override
97 public void binderDied() {
98 synchronized (mGlobalLock) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -080099 final TaskOrganizerState state =
100 mTaskOrganizerStates.get(mTaskOrganizer.asBinder());
101 state.releaseTasks();
102 mTaskOrganizerStates.remove(mTaskOrganizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800103 if (mTaskOrganizersForWindowingMode.get(mWindowingMode) == mTaskOrganizer) {
104 mTaskOrganizersForWindowingMode.remove(mWindowingMode);
105 }
106 }
107 }
108 };
109
110 class TaskOrganizerState {
111 ITaskOrganizer mOrganizer;
112 DeathRecipient mDeathRecipient;
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800113 int mWindowingMode;
Robert Carr8a2f9132019-11-11 15:03:15 -0800114
115 ArrayList<Task> mOrganizedTasks = new ArrayList<>();
116
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800117 // Save the TaskOrganizer which we replaced registration for
118 // so it can be re-registered if we unregister.
119 TaskOrganizerState mReplacementFor;
120 boolean mDisposed = false;
121
122
123 TaskOrganizerState(ITaskOrganizer organizer, int windowingMode,
Winson Chung77338ab2020-03-09 16:32:34 -0700124 @Nullable TaskOrganizerState replacing) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800125 mOrganizer = organizer;
126 mDeathRecipient = new DeathRecipient(organizer, windowingMode);
127 try {
128 organizer.asBinder().linkToDeath(mDeathRecipient, 0);
129 } catch (RemoteException e) {
130 Slog.e(TAG, "TaskOrganizer failed to register death recipient");
131 }
132 mWindowingMode = windowingMode;
133 mReplacementFor = replacing;
134 }
135
Robert Carr8a2f9132019-11-11 15:03:15 -0800136 void addTask(Task t) {
137 mOrganizedTasks.add(t);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800138 try {
139 mOrganizer.taskAppeared(t.getTaskInfo());
140 } catch (Exception e) {
141 Slog.e(TAG, "Exception sending taskAppeared callback" + e);
142 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800143 }
144
145 void removeTask(Task t) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800146 try {
Rob Carrab179782020-03-10 12:50:30 -0700147 mOrganizer.taskVanished(t.getTaskInfo());
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800148 } catch (Exception e) {
149 Slog.e(TAG, "Exception sending taskVanished callback" + e);
150 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800151 mOrganizedTasks.remove(t);
152 }
153
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800154 void dispose() {
155 mDisposed = true;
156 releaseTasks();
157 handleReplacement();
158 }
159
160 void releaseTasks() {
161 for (int i = mOrganizedTasks.size() - 1; i >= 0; i--) {
162 final Task t = mOrganizedTasks.get(i);
163 t.taskOrganizerDied();
164 removeTask(t);
165 }
166 }
167
168 void handleReplacement() {
169 if (mReplacementFor != null && !mReplacementFor.mDisposed) {
170 mTaskOrganizersForWindowingMode.put(mWindowingMode, mReplacementFor);
171 }
172 }
173
174 void unlinkDeath() {
175 mDisposed = true;
176 mOrganizer.asBinder().unlinkToDeath(mDeathRecipient, 0);
Robert Carr8a2f9132019-11-11 15:03:15 -0800177 }
178 };
179
180
181 final HashMap<Integer, TaskOrganizerState> mTaskOrganizersForWindowingMode = new HashMap();
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800182 final HashMap<IBinder, TaskOrganizerState> mTaskOrganizerStates = new HashMap();
Robert Carr8a2f9132019-11-11 15:03:15 -0800183
184 final HashMap<Integer, ITaskOrganizer> mTaskOrganizersByPendingSyncId = new HashMap();
185
Evan Rosky0037e5f2019-11-05 10:26:24 -0800186 private final WeakHashMap<Task, RunningTaskInfo> mLastSentTaskInfos = new WeakHashMap<>();
187 private final ArrayList<Task> mPendingTaskInfoChanges = new ArrayList<>();
188
Robert Carre10ee3d2019-11-11 15:03:15 -0800189 private final BLASTSyncEngine mBLASTSyncEngine = new BLASTSyncEngine();
190
Robert Carr8a2f9132019-11-11 15:03:15 -0800191 final ActivityTaskManagerService mService;
192
Evan Rosky0037e5f2019-11-05 10:26:24 -0800193 RunningTaskInfo mTmpTaskInfo;
194
195 TaskOrganizerController(ActivityTaskManagerService atm) {
Robert Carr8a2f9132019-11-11 15:03:15 -0800196 mService = atm;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800197 mGlobalLock = atm.mGlobalLock;
198 }
199
200 private void enforceStackPermission(String func) {
201 mService.mAmInternal.enforceCallingPermission(MANAGE_ACTIVITY_STACKS, func);
Robert Carr8a2f9132019-11-11 15:03:15 -0800202 }
203
Robert Carr8a2f9132019-11-11 15:03:15 -0800204 /**
205 * Register a TaskOrganizer to manage tasks as they enter the given windowing mode.
206 * If there was already a TaskOrganizer for this windowing mode it will be evicted
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800207 * but will continue to organize it's existing tasks.
Robert Carr8a2f9132019-11-11 15:03:15 -0800208 */
Evan Rosky0037e5f2019-11-05 10:26:24 -0800209 @Override
210 public void registerTaskOrganizer(ITaskOrganizer organizer, int windowingMode) {
211 if (windowingMode != WINDOWING_MODE_PINNED
212 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
213 && windowingMode != WINDOWING_MODE_SPLIT_SCREEN_SECONDARY
214 && windowingMode != WINDOWING_MODE_MULTI_WINDOW) {
215 throw new UnsupportedOperationException("As of now only Pinned/Split/Multiwindow"
216 + " windowing modes are supported for registerTaskOrganizer");
Robert Carr8a2f9132019-11-11 15:03:15 -0800217 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800218 enforceStackPermission("registerTaskOrganizer()");
219 final long origId = Binder.clearCallingIdentity();
Robert Carr8a2f9132019-11-11 15:03:15 -0800220 try {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800221 synchronized (mGlobalLock) {
Winson Chung77338ab2020-03-09 16:32:34 -0700222 if (getTaskOrganizer(windowingMode) != null) {
223 Slog.w(TAG, "Task organizer already exists for windowing mode: "
224 + windowingMode);
225 }
226 final TaskOrganizerState previousState =
227 mTaskOrganizersForWindowingMode.get(windowingMode);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800228 final TaskOrganizerState state = new TaskOrganizerState(organizer, windowingMode,
Winson Chung77338ab2020-03-09 16:32:34 -0700229 previousState);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800230 mTaskOrganizersForWindowingMode.put(windowingMode, state);
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800231 mTaskOrganizerStates.put(organizer.asBinder(), state);
Winson Chung77338ab2020-03-09 16:32:34 -0700232
233 if (previousState == null) {
234 // Only in the case where this is the root task organizer for the given
235 // windowing mode, we add report all existing tasks in that mode to the new
236 // task organizer.
237 mService.mRootWindowContainer.forAllTasks((task) -> {
238 if (task.getWindowingMode() == windowingMode) {
239 task.updateTaskOrganizerState(true /* forceUpdate */);
240 }
241 });
242 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800243 }
244 } finally {
245 Binder.restoreCallingIdentity(origId);
Robert Carr8a2f9132019-11-11 15:03:15 -0800246 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800247 }
248
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800249 void unregisterTaskOrganizer(ITaskOrganizer organizer) {
250 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
251 state.unlinkDeath();
252 if (mTaskOrganizersForWindowingMode.get(state.mWindowingMode) == state) {
253 mTaskOrganizersForWindowingMode.remove(state.mWindowingMode);
254 }
255 state.dispose();
256 }
257
Robert Carr8a2f9132019-11-11 15:03:15 -0800258 ITaskOrganizer getTaskOrganizer(int windowingMode) {
259 final TaskOrganizerState state = mTaskOrganizersForWindowingMode.get(windowingMode);
260 if (state == null) {
261 return null;
262 }
263 return state.mOrganizer;
264 }
265
Robert Carr8a2f9132019-11-11 15:03:15 -0800266 void onTaskAppeared(ITaskOrganizer organizer, Task task) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800267 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800268 state.addTask(task);
Robert Carr8a2f9132019-11-11 15:03:15 -0800269 }
270
271 void onTaskVanished(ITaskOrganizer organizer, Task task) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800272 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800273 state.removeTask(task);
274 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800275
276 @Override
277 public RunningTaskInfo createRootTask(int displayId, int windowingMode) {
278 enforceStackPermission("createRootTask()");
279 final long origId = Binder.clearCallingIdentity();
280 try {
281 synchronized (mGlobalLock) {
282 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
283 if (display == null) {
284 return null;
285 }
286 final int nextId = display.getNextStackId();
287 TaskTile tile = new TaskTile(mService, nextId, windowingMode);
288 display.addTile(tile);
Winson Chung66b08f02020-03-03 14:32:35 -0800289 RunningTaskInfo out = tile.getTaskInfo();
Evan Rosky0037e5f2019-11-05 10:26:24 -0800290 mLastSentTaskInfos.put(tile, out);
291 return out;
292 }
293 } finally {
294 Binder.restoreCallingIdentity(origId);
295 }
296 }
297
298 @Override
299 public boolean deleteRootTask(IWindowContainer token) {
300 enforceStackPermission("deleteRootTask()");
301 final long origId = Binder.clearCallingIdentity();
302 try {
303 synchronized (mGlobalLock) {
304 TaskTile tile = TaskTile.forToken(token.asBinder());
305 if (tile == null) {
306 return false;
307 }
308 tile.removeImmediately();
309 return true;
310 }
311 } finally {
312 Binder.restoreCallingIdentity(origId);
313 }
314 }
315
316 void dispatchPendingTaskInfoChanges() {
317 if (mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
318 return;
319 }
320 for (int i = 0, n = mPendingTaskInfoChanges.size(); i < n; ++i) {
321 dispatchTaskInfoChanged(mPendingTaskInfoChanges.get(i), false /* force */);
322 }
323 mPendingTaskInfoChanges.clear();
324 }
325
326 void dispatchTaskInfoChanged(Task task, boolean force) {
327 if (!force && mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
328 // Defer task info reporting while layout is deferred. This is because layout defer
329 // blocks tend to do lots of re-ordering which can mess up animations in receivers.
330 mPendingTaskInfoChanges.remove(task);
331 mPendingTaskInfoChanges.add(task);
332 return;
333 }
334 RunningTaskInfo lastInfo = mLastSentTaskInfos.get(task);
335 if (mTmpTaskInfo == null) {
336 mTmpTaskInfo = new RunningTaskInfo();
337 }
Evan Roskyf64f5da2020-03-16 13:47:48 -0700338 mTmpTaskInfo.configuration.unset();
Evan Rosky0037e5f2019-11-05 10:26:24 -0800339 task.fillTaskInfo(mTmpTaskInfo);
340 boolean changed = lastInfo == null
341 || mTmpTaskInfo.topActivityType != lastInfo.topActivityType
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800342 || mTmpTaskInfo.isResizable() != lastInfo.isResizable()
343 || mTmpTaskInfo.pictureInPictureParams != lastInfo.pictureInPictureParams;
Evan Roskyf64f5da2020-03-16 13:47:48 -0700344 if (!changed) {
345 int cfgChanges = mTmpTaskInfo.configuration.diff(lastInfo.configuration);
346 final int winCfgChanges = (cfgChanges & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0
347 ? (int) mTmpTaskInfo.configuration.windowConfiguration.diff(
348 lastInfo.configuration.windowConfiguration,
349 true /* compareUndefined */) : 0;
350 if ((winCfgChanges & REPORT_WINDOW_CONFIGS) == 0) {
351 cfgChanges &= ~ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
352 }
353 changed = (cfgChanges & REPORT_CONFIGS) != 0;
354 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800355 if (!(changed || force)) {
356 return;
357 }
358 final RunningTaskInfo newInfo = mTmpTaskInfo;
359 mLastSentTaskInfos.put(task, mTmpTaskInfo);
360 // Since we've stored this, clean up the reference so a new one will be created next time.
361 // Transferring it this way means we only have to construct new RunningTaskInfos when they
362 // change.
363 mTmpTaskInfo = null;
364
365 if (task.mTaskOrganizer != null) {
366 try {
367 task.mTaskOrganizer.onTaskInfoChanged(newInfo);
368 } catch (RemoteException e) {
369 }
370 }
371 }
372
373 @Override
374 public IWindowContainer getImeTarget(int displayId) {
375 enforceStackPermission("getImeTarget()");
376 final long origId = Binder.clearCallingIdentity();
377 try {
378 synchronized (mGlobalLock) {
379 DisplayContent dc = mService.mWindowManager.mRoot
380 .getDisplayContent(displayId);
381 if (dc == null || dc.mInputMethodTarget == null) {
382 return null;
383 }
384 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
385 final Task task = dc.mInputMethodTarget.getTask();
386 if (task == null) {
387 return null;
388 }
389 ActivityStack rootTask = (ActivityStack) task.getRootTask();
390 final TaskTile tile = rootTask.getTile();
391 if (tile != null) {
392 rootTask = tile;
393 }
394 return rootTask.mRemoteToken;
395 }
396 } finally {
397 Binder.restoreCallingIdentity(origId);
398 }
399 }
400
401 @Override
402 public void setLaunchRoot(int displayId, @Nullable IWindowContainer tile) {
403 enforceStackPermission("setLaunchRoot()");
404 final long origId = Binder.clearCallingIdentity();
405 try {
406 synchronized (mGlobalLock) {
407 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
408 if (display == null) {
409 return;
410 }
411 TaskTile taskTile = tile == null ? null : TaskTile.forToken(tile.asBinder());
412 if (taskTile == null) {
413 display.mLaunchTile = null;
414 return;
415 }
416 if (taskTile.getDisplay() != display) {
417 throw new RuntimeException("Can't set launch root for display " + displayId
418 + " to task on display " + taskTile.getDisplay().getDisplayId());
419 }
420 display.mLaunchTile = taskTile;
421 }
422 } finally {
423 Binder.restoreCallingIdentity(origId);
424 }
425 }
426
Evan Roskya8fde152020-01-07 19:09:13 -0800427 @Override
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800428 public List<RunningTaskInfo> getChildTasks(IWindowContainer parent,
429 @Nullable int[] activityTypes) {
Evan Roskya8fde152020-01-07 19:09:13 -0800430 enforceStackPermission("getChildTasks()");
431 final long ident = Binder.clearCallingIdentity();
432 try {
433 synchronized (mGlobalLock) {
434 if (parent == null) {
435 throw new IllegalArgumentException("Can't get children of null parent");
436 }
437 final WindowContainer container = WindowContainer.fromBinder(parent.asBinder());
438 if (container == null) {
439 Slog.e(TAG, "Can't get children of " + parent + " because it is not valid.");
440 return null;
441 }
442 // For now, only support returning children of persistent root tasks (of which the
443 // only current implementation is TaskTile).
444 if (!(container instanceof TaskTile)) {
445 Slog.w(TAG, "Can only get children of root tasks created via createRootTask");
446 return null;
447 }
448 ArrayList<RunningTaskInfo> out = new ArrayList<>();
449 // Tiles aren't real parents, so we need to go through stacks on the display to
450 // ensure correct ordering.
451 final DisplayContent dc = container.getDisplayContent();
452 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
453 final ActivityStack as = dc.getStackAt(i);
454 if (as.getTile() == container) {
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800455 if (activityTypes != null
456 && !ArrayUtils.contains(activityTypes, as.getActivityType())) {
457 continue;
458 }
Winson Chung66b08f02020-03-03 14:32:35 -0800459 out.add(as.getTaskInfo());
Evan Roskya8fde152020-01-07 19:09:13 -0800460 }
461 }
462 return out;
463 }
464 } finally {
465 Binder.restoreCallingIdentity(ident);
466 }
467 }
468
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800469 @Override
470 public List<RunningTaskInfo> getRootTasks(int displayId, @Nullable int[] activityTypes) {
471 enforceStackPermission("getRootTasks()");
472 final long ident = Binder.clearCallingIdentity();
473 try {
474 synchronized (mGlobalLock) {
475 final DisplayContent dc =
476 mService.mRootWindowContainer.getDisplayContent(displayId);
477 if (dc == null) {
478 throw new IllegalArgumentException("Display " + displayId + " doesn't exist");
479 }
480 ArrayList<RunningTaskInfo> out = new ArrayList<>();
481 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
482 final ActivityStack task = dc.getStackAt(i);
483 if (task.getTile() != null) {
484 // a tile is supposed to look like a parent, so don't include their
485 // "children" here. They can be accessed via getChildTasks()
486 continue;
487 }
488 if (activityTypes != null
489 && !ArrayUtils.contains(activityTypes, task.getActivityType())) {
490 continue;
491 }
Winson Chung66b08f02020-03-03 14:32:35 -0800492 out.add(task.getTaskInfo());
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800493 }
494 return out;
495 }
496 } finally {
497 Binder.restoreCallingIdentity(ident);
498 }
499 }
500
Evan Rosky0037e5f2019-11-05 10:26:24 -0800501 private int sanitizeAndApplyChange(WindowContainer container,
502 WindowContainerTransaction.Change change) {
503 if (!(container instanceof Task)) {
504 throw new RuntimeException("Invalid token in task transaction");
505 }
Robert Carrf6878a42019-12-18 02:13:12 -0800506 final Task task = (Task) container;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800507 // The "client"-facing API should prevent bad changes; however, just in case, sanitize
508 // masks here.
Evan Roskyf64f5da2020-03-16 13:47:48 -0700509 final int configMask = change.getConfigSetMask() & CONTROLLABLE_CONFIGS;
510 final int windowMask = change.getWindowSetMask() & CONTROLLABLE_WINDOW_CONFIGS;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800511 int effects = 0;
512 if (configMask != 0) {
513 Configuration c = new Configuration(container.getRequestedOverrideConfiguration());
514 c.setTo(change.getConfiguration(), configMask, windowMask);
515 container.onRequestedOverrideConfigurationChanged(c);
516 // TODO(b/145675353): remove the following once we could apply new bounds to the
517 // pinned stack together with its children.
518 resizePinnedStackIfNeeded(container, configMask, windowMask, c);
519 effects |= TRANSACT_EFFECTS_CLIENT_CONFIG;
520 }
521 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_FOCUSABLE) != 0) {
522 if (container.setFocusable(change.getFocusable())) {
523 effects |= TRANSACT_EFFECTS_LIFECYCLE;
524 }
525 }
Robert Carrf6878a42019-12-18 02:13:12 -0800526 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_HIDDEN) != 0) {
527 if (task.setForceHidden(FLAG_FORCE_HIDDEN_FOR_TASK_ORG, change.getHidden())) {
528 effects |= TRANSACT_EFFECTS_LIFECYCLE;
529 }
530 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800531 return effects;
532 }
533
Evan Roskya8fde152020-01-07 19:09:13 -0800534 private int sanitizeAndApplyHierarchyOp(WindowContainer container,
535 WindowContainerTransaction.HierarchyOp hop) {
536 if (!(container instanceof Task)) {
537 throw new IllegalArgumentException("Invalid container in hierarchy op");
538 }
Evan Roskyaf9f27c2020-02-18 18:58:35 +0000539 if (container.getDisplayContent() == null) {
540 Slog.w(TAG, "Container is no longer attached: " + container);
541 return 0;
542 }
Evan Roskya8fde152020-01-07 19:09:13 -0800543 if (hop.isReparent()) {
544 // special case for tiles since they are "virtual" parents
545 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
546 ActivityStack as = (ActivityStack) container;
547 TaskTile newParent = hop.getNewParent() == null ? null
548 : (TaskTile) WindowContainer.fromBinder(hop.getNewParent());
549 if (as.getTile() != newParent) {
550 if (as.getTile() != null) {
551 as.getTile().removeChild(as);
552 }
553 if (newParent != null) {
554 if (!as.affectedBySplitScreenResize()) {
555 return 0;
556 }
557 newParent.addChild(as, POSITION_TOP);
558 }
559 }
560 if (hop.getToTop()) {
561 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
562 } else {
563 as.getDisplay().positionStackAtBottom(as);
564 }
565 } else if (container instanceof Task) {
566 throw new RuntimeException("Reparenting leaf Tasks is not supported now.");
567 }
568 } else {
569 // Ugh, of course ActivityStack has its own special reorder logic...
570 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
571 ActivityStack as = (ActivityStack) container;
572 if (hop.getToTop()) {
573 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
574 } else {
575 as.getDisplay().positionStackAtBottom(as);
576 }
577 } else {
578 container.getParent().positionChildAt(
579 hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
580 container, false /* includingParents */);
581 }
582 }
583 return TRANSACT_EFFECTS_LIFECYCLE;
584 }
585
Evan Rosky0037e5f2019-11-05 10:26:24 -0800586 private void resizePinnedStackIfNeeded(ConfigurationContainer container, int configMask,
587 int windowMask, Configuration config) {
588 if ((container instanceof ActivityStack)
589 && ((configMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0)
590 && ((windowMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0)) {
591 final ActivityStack stack = (ActivityStack) container;
592 if (stack.inPinnedWindowingMode()) {
593 stack.resize(config.windowConfiguration.getBounds(),
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -0800594 null /* configBounds */, PRESERVE_WINDOWS, true /* deferResume */);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800595 }
596 }
597 }
598
599 private int applyWindowContainerChange(WindowContainer wc,
600 WindowContainerTransaction.Change c) {
601 int effects = sanitizeAndApplyChange(wc, c);
602
Robert Carr2bed6212020-02-20 16:55:07 -0800603 final Task tr = wc.asTask();
604
Robert Carr711e7052020-02-19 11:14:33 -0800605 final SurfaceControl.Transaction t = c.getBoundsChangeTransaction();
606 if (t != null) {
Robert Carr711e7052020-02-19 11:14:33 -0800607 tr.setMainWindowSizeChangeTransaction(t);
608 }
609
Evan Rosky0037e5f2019-11-05 10:26:24 -0800610 Rect enterPipBounds = c.getEnterPipBounds();
611 if (enterPipBounds != null) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800612 mService.mStackSupervisor.updatePictureInPictureMode(tr,
613 enterPipBounds, true);
614 }
Robert Carr2bed6212020-02-20 16:55:07 -0800615
616 final int windowingMode = c.getWindowingMode();
617 if (windowingMode > -1) {
618 tr.setWindowingMode(windowingMode);
619 }
620 final int childWindowingMode = c.getActivityWindowingMode();
621 if (childWindowingMode > -1) {
622 tr.setActivityWindowingMode(childWindowingMode);
623 }
624
Evan Rosky0037e5f2019-11-05 10:26:24 -0800625 return effects;
626 }
627
628 @Override
Robert Carre10ee3d2019-11-11 15:03:15 -0800629 public int applyContainerTransaction(WindowContainerTransaction t, ITaskOrganizer organizer) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800630 enforceStackPermission("applyContainerTransaction()");
Robert Carre10ee3d2019-11-11 15:03:15 -0800631 int syncId = -1;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800632 if (t == null) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800633 throw new IllegalArgumentException(
634 "Null transaction passed to applyContainerTransaction");
Evan Rosky0037e5f2019-11-05 10:26:24 -0800635 }
636 long ident = Binder.clearCallingIdentity();
637 try {
638 synchronized (mGlobalLock) {
639 int effects = 0;
Robert Carre10ee3d2019-11-11 15:03:15 -0800640
641 /**
642 * If organizer is non-null we are looking to synchronize this transaction
643 * by collecting all the results in to a SurfaceFlinger transaction and
644 * then delivering that to the given organizers transaction ready callback.
645 * See {@link BLASTSyncEngine} for the details of the operation. But at
646 * a high level we create a sync operation with a given ID and an associated
647 * organizer. Then we notify each WindowContainer in this WindowContainer
648 * transaction that it is participating in a sync operation with that
649 * ID. Once everything is notified we tell the BLASTSyncEngine
650 * "setSyncReady" which means that we have added everything
651 * to the set. At any point after this, all the WindowContainers
652 * will eventually finish applying their changes and notify the
653 * BLASTSyncEngine which will deliver the Transaction to the organizer.
654 */
655 if (organizer != null) {
656 syncId = startSyncWithOrganizer(organizer);
657 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800658 mService.deferWindowLayout();
659 try {
660 ArraySet<WindowContainer> haveConfigChanges = new ArraySet<>();
661 Iterator<Map.Entry<IBinder, WindowContainerTransaction.Change>> entries =
662 t.getChanges().entrySet().iterator();
663 while (entries.hasNext()) {
664 final Map.Entry<IBinder, WindowContainerTransaction.Change> entry =
665 entries.next();
Evan Roskya8fde152020-01-07 19:09:13 -0800666 final WindowContainer wc = WindowContainer.fromBinder(entry.getKey());
Evan Rosky0037e5f2019-11-05 10:26:24 -0800667 int containerEffect = applyWindowContainerChange(wc, entry.getValue());
668 effects |= containerEffect;
Robert Carre10ee3d2019-11-11 15:03:15 -0800669
Evan Rosky0037e5f2019-11-05 10:26:24 -0800670 // Lifecycle changes will trigger ensureConfig for everything.
671 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) == 0
672 && (containerEffect & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
673 haveConfigChanges.add(wc);
674 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800675 if (syncId >= 0) {
676 mBLASTSyncEngine.addToSyncSet(syncId, wc);
677 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800678 }
Evan Roskya8fde152020-01-07 19:09:13 -0800679 // Hierarchy changes
680 final List<WindowContainerTransaction.HierarchyOp> hops = t.getHierarchyOps();
681 for (int i = 0, n = hops.size(); i < n; ++i) {
682 final WindowContainerTransaction.HierarchyOp hop = hops.get(i);
683 final WindowContainer wc = WindowContainer.fromBinder(hop.getContainer());
684 effects |= sanitizeAndApplyHierarchyOp(wc, hop);
685 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800686 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) != 0) {
687 // Already calls ensureActivityConfig
688 mService.mRootWindowContainer.ensureActivitiesVisible(
689 null, 0, PRESERVE_WINDOWS);
690 } else if ((effects & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
691 final PooledConsumer f = PooledLambda.obtainConsumer(
692 ActivityRecord::ensureActivityConfiguration,
693 PooledLambda.__(ActivityRecord.class), 0,
694 false /* preserveWindow */);
695 try {
696 for (int i = haveConfigChanges.size() - 1; i >= 0; --i) {
Evan Roskyf64f5da2020-03-16 13:47:48 -0700697 final WindowContainer wc = haveConfigChanges.valueAt(i);
698 final Task task = wc.asTask();
699 final TaskTile tile = task != null ? task.asTile() : null;
700 if (tile != null) {
701 // Special case for tile. Can't override normal forAllActivities
702 // because it generates duplicate calls and messes up existing
703 // code-paths.
704 tile.forAllTileActivities(f);
705 } else {
706 wc.forAllActivities(f);
707 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800708 }
709 } finally {
710 f.recycle();
711 }
712 }
713 } finally {
714 mService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800715 if (syncId >= 0) {
716 setSyncReady(syncId);
717 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800718 }
719 }
720 } finally {
721 Binder.restoreCallingIdentity(ident);
722 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800723 return syncId;
724 }
725
726 @Override
727 public void transactionReady(int id, SurfaceControl.Transaction sc) {
728 final ITaskOrganizer organizer = mTaskOrganizersByPendingSyncId.get(id);
729 if (organizer == null) {
730 Slog.e(TAG, "Got transaction complete for unexpected ID");
731 }
732 try {
733 organizer.transactionReady(id, sc);
734 } catch (RemoteException e) {
735 }
736
737 mTaskOrganizersByPendingSyncId.remove(id);
738 }
739
740 int startSyncWithOrganizer(ITaskOrganizer organizer) {
741 int id = mBLASTSyncEngine.startSyncSet(this);
742 mTaskOrganizersByPendingSyncId.put(id, organizer);
743 return id;
744 }
745
746 void setSyncReady(int id) {
747 mBLASTSyncEngine.setReady(id);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800748 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800749}