blob: 9bf4d63550711b61ffaeff3f862abc46af186928 [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;
Evan Rosky0037e5f2019-11-05 10:26:24 -080032import 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;
Robert Carre10ee3d2019-11-11 15:03:15 -080041import android.view.SurfaceControl;
Wale Ogunwale57946582020-03-21 14:29:07 -070042import android.window.ITaskOrganizerController;
43import android.window.ITaskOrganizer;
44import android.window.IWindowContainer;
45import android.window.WindowContainerTransaction;
Evan Rosky0037e5f2019-11-05 10:26:24 -080046
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
Winson Chung8a168902020-03-12 22:39:22 -0700249 @Override
250 public void unregisterTaskOrganizer(ITaskOrganizer organizer) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800251 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
252 state.unlinkDeath();
253 if (mTaskOrganizersForWindowingMode.get(state.mWindowingMode) == state) {
254 mTaskOrganizersForWindowingMode.remove(state.mWindowingMode);
255 }
256 state.dispose();
257 }
258
Robert Carr8a2f9132019-11-11 15:03:15 -0800259 ITaskOrganizer getTaskOrganizer(int windowingMode) {
260 final TaskOrganizerState state = mTaskOrganizersForWindowingMode.get(windowingMode);
261 if (state == null) {
262 return null;
263 }
264 return state.mOrganizer;
265 }
266
Robert Carr8a2f9132019-11-11 15:03:15 -0800267 void onTaskAppeared(ITaskOrganizer organizer, Task task) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800268 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800269 state.addTask(task);
Robert Carr8a2f9132019-11-11 15:03:15 -0800270 }
271
272 void onTaskVanished(ITaskOrganizer organizer, Task task) {
Robert Carr7d7c8ab2020-01-28 15:57:23 -0800273 final TaskOrganizerState state = mTaskOrganizerStates.get(organizer.asBinder());
Robert Carr8a2f9132019-11-11 15:03:15 -0800274 state.removeTask(task);
275 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800276
277 @Override
278 public RunningTaskInfo createRootTask(int displayId, int windowingMode) {
279 enforceStackPermission("createRootTask()");
280 final long origId = Binder.clearCallingIdentity();
281 try {
282 synchronized (mGlobalLock) {
283 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
284 if (display == null) {
285 return null;
286 }
287 final int nextId = display.getNextStackId();
288 TaskTile tile = new TaskTile(mService, nextId, windowingMode);
289 display.addTile(tile);
Winson Chung66b08f02020-03-03 14:32:35 -0800290 RunningTaskInfo out = tile.getTaskInfo();
Evan Rosky0037e5f2019-11-05 10:26:24 -0800291 mLastSentTaskInfos.put(tile, out);
292 return out;
293 }
294 } finally {
295 Binder.restoreCallingIdentity(origId);
296 }
297 }
298
299 @Override
300 public boolean deleteRootTask(IWindowContainer token) {
301 enforceStackPermission("deleteRootTask()");
302 final long origId = Binder.clearCallingIdentity();
303 try {
304 synchronized (mGlobalLock) {
305 TaskTile tile = TaskTile.forToken(token.asBinder());
306 if (tile == null) {
307 return false;
308 }
309 tile.removeImmediately();
310 return true;
311 }
312 } finally {
313 Binder.restoreCallingIdentity(origId);
314 }
315 }
316
317 void dispatchPendingTaskInfoChanges() {
318 if (mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
319 return;
320 }
321 for (int i = 0, n = mPendingTaskInfoChanges.size(); i < n; ++i) {
322 dispatchTaskInfoChanged(mPendingTaskInfoChanges.get(i), false /* force */);
323 }
324 mPendingTaskInfoChanges.clear();
325 }
326
327 void dispatchTaskInfoChanged(Task task, boolean force) {
328 if (!force && mService.mWindowManager.mWindowPlacerLocked.isLayoutDeferred()) {
329 // Defer task info reporting while layout is deferred. This is because layout defer
330 // blocks tend to do lots of re-ordering which can mess up animations in receivers.
331 mPendingTaskInfoChanges.remove(task);
332 mPendingTaskInfoChanges.add(task);
333 return;
334 }
335 RunningTaskInfo lastInfo = mLastSentTaskInfos.get(task);
336 if (mTmpTaskInfo == null) {
337 mTmpTaskInfo = new RunningTaskInfo();
338 }
Evan Roskyf64f5da2020-03-16 13:47:48 -0700339 mTmpTaskInfo.configuration.unset();
Evan Rosky0037e5f2019-11-05 10:26:24 -0800340 task.fillTaskInfo(mTmpTaskInfo);
341 boolean changed = lastInfo == null
342 || mTmpTaskInfo.topActivityType != lastInfo.topActivityType
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800343 || mTmpTaskInfo.isResizable() != lastInfo.isResizable()
344 || mTmpTaskInfo.pictureInPictureParams != lastInfo.pictureInPictureParams;
Evan Roskyf64f5da2020-03-16 13:47:48 -0700345 if (!changed) {
346 int cfgChanges = mTmpTaskInfo.configuration.diff(lastInfo.configuration);
347 final int winCfgChanges = (cfgChanges & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0
348 ? (int) mTmpTaskInfo.configuration.windowConfiguration.diff(
349 lastInfo.configuration.windowConfiguration,
350 true /* compareUndefined */) : 0;
351 if ((winCfgChanges & REPORT_WINDOW_CONFIGS) == 0) {
352 cfgChanges &= ~ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
353 }
354 changed = (cfgChanges & REPORT_CONFIGS) != 0;
355 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800356 if (!(changed || force)) {
357 return;
358 }
359 final RunningTaskInfo newInfo = mTmpTaskInfo;
360 mLastSentTaskInfos.put(task, mTmpTaskInfo);
361 // Since we've stored this, clean up the reference so a new one will be created next time.
362 // Transferring it this way means we only have to construct new RunningTaskInfos when they
363 // change.
364 mTmpTaskInfo = null;
365
366 if (task.mTaskOrganizer != null) {
367 try {
368 task.mTaskOrganizer.onTaskInfoChanged(newInfo);
369 } catch (RemoteException e) {
370 }
371 }
372 }
373
374 @Override
375 public IWindowContainer getImeTarget(int displayId) {
376 enforceStackPermission("getImeTarget()");
377 final long origId = Binder.clearCallingIdentity();
378 try {
379 synchronized (mGlobalLock) {
380 DisplayContent dc = mService.mWindowManager.mRoot
381 .getDisplayContent(displayId);
382 if (dc == null || dc.mInputMethodTarget == null) {
383 return null;
384 }
385 // Avoid WindowState#getRootTask() so we don't attribute system windows to a task.
386 final Task task = dc.mInputMethodTarget.getTask();
387 if (task == null) {
388 return null;
389 }
390 ActivityStack rootTask = (ActivityStack) task.getRootTask();
391 final TaskTile tile = rootTask.getTile();
392 if (tile != null) {
393 rootTask = tile;
394 }
395 return rootTask.mRemoteToken;
396 }
397 } finally {
398 Binder.restoreCallingIdentity(origId);
399 }
400 }
401
402 @Override
403 public void setLaunchRoot(int displayId, @Nullable IWindowContainer tile) {
404 enforceStackPermission("setLaunchRoot()");
405 final long origId = Binder.clearCallingIdentity();
406 try {
407 synchronized (mGlobalLock) {
408 DisplayContent display = mService.mRootWindowContainer.getDisplayContent(displayId);
409 if (display == null) {
410 return;
411 }
412 TaskTile taskTile = tile == null ? null : TaskTile.forToken(tile.asBinder());
413 if (taskTile == null) {
414 display.mLaunchTile = null;
415 return;
416 }
417 if (taskTile.getDisplay() != display) {
418 throw new RuntimeException("Can't set launch root for display " + displayId
419 + " to task on display " + taskTile.getDisplay().getDisplayId());
420 }
421 display.mLaunchTile = taskTile;
422 }
423 } finally {
424 Binder.restoreCallingIdentity(origId);
425 }
426 }
427
Evan Roskya8fde152020-01-07 19:09:13 -0800428 @Override
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800429 public List<RunningTaskInfo> getChildTasks(IWindowContainer parent,
430 @Nullable int[] activityTypes) {
Evan Roskya8fde152020-01-07 19:09:13 -0800431 enforceStackPermission("getChildTasks()");
432 final long ident = Binder.clearCallingIdentity();
433 try {
434 synchronized (mGlobalLock) {
435 if (parent == null) {
436 throw new IllegalArgumentException("Can't get children of null parent");
437 }
438 final WindowContainer container = WindowContainer.fromBinder(parent.asBinder());
439 if (container == null) {
440 Slog.e(TAG, "Can't get children of " + parent + " because it is not valid.");
441 return null;
442 }
443 // For now, only support returning children of persistent root tasks (of which the
444 // only current implementation is TaskTile).
445 if (!(container instanceof TaskTile)) {
446 Slog.w(TAG, "Can only get children of root tasks created via createRootTask");
447 return null;
448 }
449 ArrayList<RunningTaskInfo> out = new ArrayList<>();
450 // Tiles aren't real parents, so we need to go through stacks on the display to
451 // ensure correct ordering.
452 final DisplayContent dc = container.getDisplayContent();
453 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
454 final ActivityStack as = dc.getStackAt(i);
455 if (as.getTile() == container) {
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800456 if (activityTypes != null
457 && !ArrayUtils.contains(activityTypes, as.getActivityType())) {
458 continue;
459 }
Winson Chung66b08f02020-03-03 14:32:35 -0800460 out.add(as.getTaskInfo());
Evan Roskya8fde152020-01-07 19:09:13 -0800461 }
462 }
463 return out;
464 }
465 } finally {
466 Binder.restoreCallingIdentity(ident);
467 }
468 }
469
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800470 @Override
471 public List<RunningTaskInfo> getRootTasks(int displayId, @Nullable int[] activityTypes) {
472 enforceStackPermission("getRootTasks()");
473 final long ident = Binder.clearCallingIdentity();
474 try {
475 synchronized (mGlobalLock) {
476 final DisplayContent dc =
477 mService.mRootWindowContainer.getDisplayContent(displayId);
478 if (dc == null) {
479 throw new IllegalArgumentException("Display " + displayId + " doesn't exist");
480 }
481 ArrayList<RunningTaskInfo> out = new ArrayList<>();
482 for (int i = dc.getStackCount() - 1; i >= 0; --i) {
483 final ActivityStack task = dc.getStackAt(i);
484 if (task.getTile() != null) {
485 // a tile is supposed to look like a parent, so don't include their
486 // "children" here. They can be accessed via getChildTasks()
487 continue;
488 }
489 if (activityTypes != null
490 && !ArrayUtils.contains(activityTypes, task.getActivityType())) {
491 continue;
492 }
Winson Chung66b08f02020-03-03 14:32:35 -0800493 out.add(task.getTaskInfo());
Evan Rosky29d4a0a2020-02-04 16:40:44 -0800494 }
495 return out;
496 }
497 } finally {
498 Binder.restoreCallingIdentity(ident);
499 }
500 }
501
Evan Rosky0037e5f2019-11-05 10:26:24 -0800502 private int sanitizeAndApplyChange(WindowContainer container,
503 WindowContainerTransaction.Change change) {
504 if (!(container instanceof Task)) {
505 throw new RuntimeException("Invalid token in task transaction");
506 }
Robert Carrf6878a42019-12-18 02:13:12 -0800507 final Task task = (Task) container;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800508 // The "client"-facing API should prevent bad changes; however, just in case, sanitize
509 // masks here.
Evan Roskyf64f5da2020-03-16 13:47:48 -0700510 final int configMask = change.getConfigSetMask() & CONTROLLABLE_CONFIGS;
511 final int windowMask = change.getWindowSetMask() & CONTROLLABLE_WINDOW_CONFIGS;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800512 int effects = 0;
513 if (configMask != 0) {
514 Configuration c = new Configuration(container.getRequestedOverrideConfiguration());
515 c.setTo(change.getConfiguration(), configMask, windowMask);
516 container.onRequestedOverrideConfigurationChanged(c);
517 // TODO(b/145675353): remove the following once we could apply new bounds to the
518 // pinned stack together with its children.
519 resizePinnedStackIfNeeded(container, configMask, windowMask, c);
520 effects |= TRANSACT_EFFECTS_CLIENT_CONFIG;
521 }
522 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_FOCUSABLE) != 0) {
523 if (container.setFocusable(change.getFocusable())) {
524 effects |= TRANSACT_EFFECTS_LIFECYCLE;
525 }
526 }
Robert Carrf6878a42019-12-18 02:13:12 -0800527 if ((change.getChangeMask() & WindowContainerTransaction.Change.CHANGE_HIDDEN) != 0) {
528 if (task.setForceHidden(FLAG_FORCE_HIDDEN_FOR_TASK_ORG, change.getHidden())) {
529 effects |= TRANSACT_EFFECTS_LIFECYCLE;
530 }
531 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800532 return effects;
533 }
534
Evan Roskya8fde152020-01-07 19:09:13 -0800535 private int sanitizeAndApplyHierarchyOp(WindowContainer container,
536 WindowContainerTransaction.HierarchyOp hop) {
537 if (!(container instanceof Task)) {
538 throw new IllegalArgumentException("Invalid container in hierarchy op");
539 }
Evan Roskyaf9f27c2020-02-18 18:58:35 +0000540 if (container.getDisplayContent() == null) {
541 Slog.w(TAG, "Container is no longer attached: " + container);
542 return 0;
543 }
Evan Roskya8fde152020-01-07 19:09:13 -0800544 if (hop.isReparent()) {
545 // special case for tiles since they are "virtual" parents
546 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
547 ActivityStack as = (ActivityStack) container;
548 TaskTile newParent = hop.getNewParent() == null ? null
549 : (TaskTile) WindowContainer.fromBinder(hop.getNewParent());
550 if (as.getTile() != newParent) {
551 if (as.getTile() != null) {
552 as.getTile().removeChild(as);
553 }
554 if (newParent != null) {
555 if (!as.affectedBySplitScreenResize()) {
556 return 0;
557 }
558 newParent.addChild(as, POSITION_TOP);
559 }
560 }
561 if (hop.getToTop()) {
562 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
563 } else {
564 as.getDisplay().positionStackAtBottom(as);
565 }
566 } else if (container instanceof Task) {
567 throw new RuntimeException("Reparenting leaf Tasks is not supported now.");
568 }
569 } else {
570 // Ugh, of course ActivityStack has its own special reorder logic...
571 if (container instanceof ActivityStack && ((ActivityStack) container).isRootTask()) {
572 ActivityStack as = (ActivityStack) container;
573 if (hop.getToTop()) {
574 as.getDisplay().positionStackAtTop(as, false /* includingParents */);
575 } else {
576 as.getDisplay().positionStackAtBottom(as);
577 }
578 } else {
579 container.getParent().positionChildAt(
580 hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
581 container, false /* includingParents */);
582 }
583 }
584 return TRANSACT_EFFECTS_LIFECYCLE;
585 }
586
Evan Rosky0037e5f2019-11-05 10:26:24 -0800587 private void resizePinnedStackIfNeeded(ConfigurationContainer container, int configMask,
588 int windowMask, Configuration config) {
589 if ((container instanceof ActivityStack)
590 && ((configMask & ActivityInfo.CONFIG_WINDOW_CONFIGURATION) != 0)
591 && ((windowMask & WindowConfiguration.WINDOW_CONFIG_BOUNDS) != 0)) {
592 final ActivityStack stack = (ActivityStack) container;
593 if (stack.inPinnedWindowingMode()) {
594 stack.resize(config.windowConfiguration.getBounds(),
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -0800595 null /* configBounds */, PRESERVE_WINDOWS, true /* deferResume */);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800596 }
597 }
598 }
599
600 private int applyWindowContainerChange(WindowContainer wc,
601 WindowContainerTransaction.Change c) {
602 int effects = sanitizeAndApplyChange(wc, c);
603
Robert Carr2bed6212020-02-20 16:55:07 -0800604 final Task tr = wc.asTask();
605
Robert Carr711e7052020-02-19 11:14:33 -0800606 final SurfaceControl.Transaction t = c.getBoundsChangeTransaction();
607 if (t != null) {
Robert Carr711e7052020-02-19 11:14:33 -0800608 tr.setMainWindowSizeChangeTransaction(t);
609 }
610
Evan Rosky0037e5f2019-11-05 10:26:24 -0800611 Rect enterPipBounds = c.getEnterPipBounds();
612 if (enterPipBounds != null) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800613 mService.mStackSupervisor.updatePictureInPictureMode(tr,
614 enterPipBounds, true);
615 }
Robert Carr2bed6212020-02-20 16:55:07 -0800616
617 final int windowingMode = c.getWindowingMode();
618 if (windowingMode > -1) {
619 tr.setWindowingMode(windowingMode);
620 }
621 final int childWindowingMode = c.getActivityWindowingMode();
622 if (childWindowingMode > -1) {
623 tr.setActivityWindowingMode(childWindowingMode);
624 }
625
Evan Rosky0037e5f2019-11-05 10:26:24 -0800626 return effects;
627 }
628
629 @Override
Robert Carre10ee3d2019-11-11 15:03:15 -0800630 public int applyContainerTransaction(WindowContainerTransaction t, ITaskOrganizer organizer) {
Evan Rosky0037e5f2019-11-05 10:26:24 -0800631 enforceStackPermission("applyContainerTransaction()");
Robert Carre10ee3d2019-11-11 15:03:15 -0800632 int syncId = -1;
Evan Rosky0037e5f2019-11-05 10:26:24 -0800633 if (t == null) {
Robert Carre10ee3d2019-11-11 15:03:15 -0800634 throw new IllegalArgumentException(
635 "Null transaction passed to applyContainerTransaction");
Evan Rosky0037e5f2019-11-05 10:26:24 -0800636 }
637 long ident = Binder.clearCallingIdentity();
638 try {
639 synchronized (mGlobalLock) {
640 int effects = 0;
Robert Carre10ee3d2019-11-11 15:03:15 -0800641
642 /**
643 * If organizer is non-null we are looking to synchronize this transaction
644 * by collecting all the results in to a SurfaceFlinger transaction and
645 * then delivering that to the given organizers transaction ready callback.
646 * See {@link BLASTSyncEngine} for the details of the operation. But at
647 * a high level we create a sync operation with a given ID and an associated
648 * organizer. Then we notify each WindowContainer in this WindowContainer
649 * transaction that it is participating in a sync operation with that
650 * ID. Once everything is notified we tell the BLASTSyncEngine
651 * "setSyncReady" which means that we have added everything
652 * to the set. At any point after this, all the WindowContainers
653 * will eventually finish applying their changes and notify the
654 * BLASTSyncEngine which will deliver the Transaction to the organizer.
655 */
656 if (organizer != null) {
657 syncId = startSyncWithOrganizer(organizer);
658 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800659 mService.deferWindowLayout();
660 try {
661 ArraySet<WindowContainer> haveConfigChanges = new ArraySet<>();
662 Iterator<Map.Entry<IBinder, WindowContainerTransaction.Change>> entries =
663 t.getChanges().entrySet().iterator();
664 while (entries.hasNext()) {
665 final Map.Entry<IBinder, WindowContainerTransaction.Change> entry =
666 entries.next();
Evan Roskya8fde152020-01-07 19:09:13 -0800667 final WindowContainer wc = WindowContainer.fromBinder(entry.getKey());
Evan Rosky0037e5f2019-11-05 10:26:24 -0800668 int containerEffect = applyWindowContainerChange(wc, entry.getValue());
669 effects |= containerEffect;
Robert Carre10ee3d2019-11-11 15:03:15 -0800670
Evan Rosky0037e5f2019-11-05 10:26:24 -0800671 // Lifecycle changes will trigger ensureConfig for everything.
672 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) == 0
673 && (containerEffect & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
674 haveConfigChanges.add(wc);
675 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800676 if (syncId >= 0) {
677 mBLASTSyncEngine.addToSyncSet(syncId, wc);
678 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800679 }
Evan Roskya8fde152020-01-07 19:09:13 -0800680 // Hierarchy changes
681 final List<WindowContainerTransaction.HierarchyOp> hops = t.getHierarchyOps();
682 for (int i = 0, n = hops.size(); i < n; ++i) {
683 final WindowContainerTransaction.HierarchyOp hop = hops.get(i);
684 final WindowContainer wc = WindowContainer.fromBinder(hop.getContainer());
685 effects |= sanitizeAndApplyHierarchyOp(wc, hop);
686 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800687 if ((effects & TRANSACT_EFFECTS_LIFECYCLE) != 0) {
688 // Already calls ensureActivityConfig
689 mService.mRootWindowContainer.ensureActivitiesVisible(
690 null, 0, PRESERVE_WINDOWS);
691 } else if ((effects & TRANSACT_EFFECTS_CLIENT_CONFIG) != 0) {
692 final PooledConsumer f = PooledLambda.obtainConsumer(
693 ActivityRecord::ensureActivityConfiguration,
694 PooledLambda.__(ActivityRecord.class), 0,
695 false /* preserveWindow */);
696 try {
697 for (int i = haveConfigChanges.size() - 1; i >= 0; --i) {
Evan Roskyf64f5da2020-03-16 13:47:48 -0700698 final WindowContainer wc = haveConfigChanges.valueAt(i);
699 final Task task = wc.asTask();
700 final TaskTile tile = task != null ? task.asTile() : null;
701 if (tile != null) {
702 // Special case for tile. Can't override normal forAllActivities
703 // because it generates duplicate calls and messes up existing
704 // code-paths.
705 tile.forAllTileActivities(f);
706 } else {
707 wc.forAllActivities(f);
708 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800709 }
710 } finally {
711 f.recycle();
712 }
713 }
714 } finally {
715 mService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800716 if (syncId >= 0) {
717 setSyncReady(syncId);
718 }
Evan Rosky0037e5f2019-11-05 10:26:24 -0800719 }
720 }
721 } finally {
722 Binder.restoreCallingIdentity(ident);
723 }
Robert Carre10ee3d2019-11-11 15:03:15 -0800724 return syncId;
725 }
726
727 @Override
728 public void transactionReady(int id, SurfaceControl.Transaction sc) {
729 final ITaskOrganizer organizer = mTaskOrganizersByPendingSyncId.get(id);
730 if (organizer == null) {
731 Slog.e(TAG, "Got transaction complete for unexpected ID");
732 }
733 try {
734 organizer.transactionReady(id, sc);
735 } catch (RemoteException e) {
736 }
737
738 mTaskOrganizersByPendingSyncId.remove(id);
739 }
740
741 int startSyncWithOrganizer(ITaskOrganizer organizer) {
742 int id = mBLASTSyncEngine.startSyncSet(this);
743 mTaskOrganizersByPendingSyncId.put(id, organizer);
744 return id;
745 }
746
747 void setSyncReady(int id) {
748 mBLASTSyncEngine.setReady(id);
Evan Rosky0037e5f2019-11-05 10:26:24 -0800749 }
Robert Carr8a2f9132019-11-11 15:03:15 -0800750}