Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeController.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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
17 package com.android.wm.shell.desktopmode;
18
19 import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
20 import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
21 import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
22 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
23 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
24 import static android.view.WindowManager.TRANSIT_CHANGE;
25 import static android.view.WindowManager.TRANSIT_OPEN;
26 import static android.view.WindowManager.TRANSIT_TO_FRONT;
27
28 import static com.android.wm.shell.common.ExecutorUtils.executeRemoteCallWithTaskPermission;
29 import static com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE;
30 import static com.android.wm.shell.sysui.ShellSharedConstants.KEY_EXTRA_SHELL_DESKTOP_MODE;
31
32 import android.app.ActivityManager.RunningTaskInfo;
33 import android.app.WindowConfiguration;
34 import android.content.Context;
35 import android.database.ContentObserver;
36 import android.net.Uri;
37 import android.os.Handler;
38 import android.os.IBinder;
39 import android.os.UserHandle;
40 import android.provider.Settings;
41 import android.util.ArraySet;
42 import android.view.SurfaceControl;
43 import android.view.WindowManager;
44 import android.window.DisplayAreaInfo;
45 import android.window.TransitionInfo;
46 import android.window.TransitionRequestInfo;
47 import android.window.WindowContainerTransaction;
48
49 import androidx.annotation.BinderThread;
50 import androidx.annotation.NonNull;
51 import androidx.annotation.Nullable;
52
53 import com.android.internal.annotations.VisibleForTesting;
54 import com.android.internal.protolog.common.ProtoLog;
55 import com.android.wm.shell.RootTaskDisplayAreaOrganizer;
56 import com.android.wm.shell.ShellTaskOrganizer;
57 import com.android.wm.shell.common.ExternalInterfaceBinder;
58 import com.android.wm.shell.common.RemoteCallable;
59 import com.android.wm.shell.common.ShellExecutor;
60 import com.android.wm.shell.common.annotations.ExternalThread;
61 import com.android.wm.shell.common.annotations.ShellMainThread;
62 import com.android.wm.shell.sysui.ShellController;
63 import com.android.wm.shell.sysui.ShellInit;
64 import com.android.wm.shell.transition.Transitions;
65
66 import java.util.ArrayList;
67 import java.util.Comparator;
68 import java.util.List;
69 import java.util.concurrent.Executor;
70
71 /**
72 * Handles windowing changes when desktop mode system setting changes
73 */
74 public class DesktopModeController implements RemoteCallable<DesktopModeController>,
75 Transitions.TransitionHandler {
76
77 private final Context mContext;
78 private final ShellController mShellController;
79 private final ShellTaskOrganizer mShellTaskOrganizer;
80 private final RootTaskDisplayAreaOrganizer mRootTaskDisplayAreaOrganizer;
81 private final Transitions mTransitions;
82 private final DesktopModeTaskRepository mDesktopModeTaskRepository;
83 private final ShellExecutor mMainExecutor;
84 private final DesktopModeImpl mDesktopModeImpl = new DesktopModeImpl();
85 private final SettingsObserver mSettingsObserver;
86
87 public DesktopModeController(Context context,
88 ShellInit shellInit,
89 ShellController shellController,
90 ShellTaskOrganizer shellTaskOrganizer,
91 RootTaskDisplayAreaOrganizer rootTaskDisplayAreaOrganizer,
92 Transitions transitions,
93 DesktopModeTaskRepository desktopModeTaskRepository,
94 @ShellMainThread Handler mainHandler,
95 @ShellMainThread ShellExecutor mainExecutor) {
96 mContext = context;
97 mShellController = shellController;
98 mShellTaskOrganizer = shellTaskOrganizer;
99 mRootTaskDisplayAreaOrganizer = rootTaskDisplayAreaOrganizer;
100 mTransitions = transitions;
101 mDesktopModeTaskRepository = desktopModeTaskRepository;
102 mMainExecutor = mainExecutor;
103 mSettingsObserver = new SettingsObserver(mContext, mainHandler);
104 if (DesktopModeStatus.isProto1Enabled()) {
105 shellInit.addInitCallback(this::onInit, this);
106 }
107 }
108
109 private void onInit() {
110 ProtoLog.d(WM_SHELL_DESKTOP_MODE, "Initialize DesktopModeController");
111 mShellController.addExternalInterface(KEY_EXTRA_SHELL_DESKTOP_MODE,
112 this::createExternalInterface, this);
113 mSettingsObserver.observe();
114 if (DesktopModeStatus.isActive(mContext)) {
115 updateDesktopModeActive(true);
116 }
117 mTransitions.addHandler(this);
118 }
119
120 @Override
121 public Context getContext() {
122 return mContext;
123 }
124
125 @Override
126 public ShellExecutor getRemoteCallExecutor() {
127 return mMainExecutor;
128 }
129
130 /**
131 * Get connection interface between sysui and shell
132 */
133 public DesktopMode asDesktopMode() {
134 return mDesktopModeImpl;
135 }
136
137 /**
138 * Creates a new instance of the external interface to pass to another process.
139 */
140 private ExternalInterfaceBinder createExternalInterface() {
141 return new IDesktopModeImpl(this);
142 }
143
144 /**
145 * Adds a listener to find out about changes in the visibility of freeform tasks.
146 *
147 * @param listener the listener to add.
148 * @param callbackExecutor the executor to call the listener on.
149 */
150 public void addListener(DesktopModeTaskRepository.VisibleTasksListener listener,
151 Executor callbackExecutor) {
152 mDesktopModeTaskRepository.addVisibleTasksListener(listener, callbackExecutor);
153 }
154
155 @VisibleForTesting
156 void updateDesktopModeActive(boolean active) {
157 ProtoLog.d(WM_SHELL_DESKTOP_MODE, "updateDesktopModeActive: active=%s", active);
158
159 int displayId = mContext.getDisplayId();
160
161 ArrayList<RunningTaskInfo> runningTasks = mShellTaskOrganizer.getRunningTasks(displayId);
162
163 WindowContainerTransaction wct = new WindowContainerTransaction();
164 // Reset freeform windowing mode that is set per task level so tasks inherit it
165 clearFreeformForStandardTasks(runningTasks, wct);
166 if (active) {
167 moveHomeBehindVisibleTasks(runningTasks, wct);
168 setDisplayAreaWindowingMode(displayId, WINDOWING_MODE_FREEFORM, wct);
169 } else {
170 clearBoundsForStandardTasks(runningTasks, wct);
171 setDisplayAreaWindowingMode(displayId, WINDOWING_MODE_FULLSCREEN, wct);
172 }
173 if (Transitions.ENABLE_SHELL_TRANSITIONS) {
174 mTransitions.startTransition(TRANSIT_CHANGE, wct, null);
175 } else {
176 mRootTaskDisplayAreaOrganizer.applyTransaction(wct);
177 }
178 }
179
180 private WindowContainerTransaction clearBoundsForStandardTasks(
181 ArrayList<RunningTaskInfo> runningTasks, WindowContainerTransaction wct) {
182 ProtoLog.v(WM_SHELL_DESKTOP_MODE, "prepareClearBoundsForTasks");
183 for (RunningTaskInfo taskInfo : runningTasks) {
184 if (taskInfo.getActivityType() == ACTIVITY_TYPE_STANDARD) {
185 ProtoLog.v(WM_SHELL_DESKTOP_MODE, "clearing bounds for token=%s taskInfo=%s",
186 taskInfo.token, taskInfo);
187 wct.setBounds(taskInfo.token, null);
188 }
189 }
190 return wct;
191 }
192
193 private void clearFreeformForStandardTasks(ArrayList<RunningTaskInfo> runningTasks,
194 WindowContainerTransaction wct) {
195 ProtoLog.v(WM_SHELL_DESKTOP_MODE, "prepareClearFreeformForTasks");
196 for (RunningTaskInfo taskInfo : runningTasks) {
197 if (taskInfo.getWindowingMode() == WINDOWING_MODE_FREEFORM
198 && taskInfo.getActivityType() == ACTIVITY_TYPE_STANDARD) {
199 ProtoLog.v(WM_SHELL_DESKTOP_MODE,
200 "clearing windowing mode for token=%s taskInfo=%s", taskInfo.token,
201 taskInfo);
202 wct.setWindowingMode(taskInfo.token, WINDOWING_MODE_UNDEFINED);
203 }
204 }
205 }
206
207 private void moveHomeBehindVisibleTasks(ArrayList<RunningTaskInfo> runningTasks,
208 WindowContainerTransaction wct) {
209 ProtoLog.v(WM_SHELL_DESKTOP_MODE, "moveHomeBehindVisibleTasks");
210 RunningTaskInfo homeTask = null;
211 ArrayList<RunningTaskInfo> visibleTasks = new ArrayList<>();
212 for (RunningTaskInfo taskInfo : runningTasks) {
213 if (taskInfo.getActivityType() == ACTIVITY_TYPE_HOME) {
214 homeTask = taskInfo;
215 } else if (taskInfo.getActivityType() == ACTIVITY_TYPE_STANDARD
216 && taskInfo.isVisible()) {
217 visibleTasks.add(taskInfo);
218 }
219 }
220 if (homeTask == null) {
221 ProtoLog.w(WM_SHELL_DESKTOP_MODE, "moveHomeBehindVisibleTasks: home task not found");
222 } else {
223 ProtoLog.v(WM_SHELL_DESKTOP_MODE, "moveHomeBehindVisibleTasks: visible tasks %d",
224 visibleTasks.size());
225 wct.reorder(homeTask.getToken(), true /* onTop */);
226 for (RunningTaskInfo task : visibleTasks) {
227 wct.reorder(task.getToken(), true /* onTop */);
228 }
229 }
230 }
231
232 private void setDisplayAreaWindowingMode(int displayId,
233 @WindowConfiguration.WindowingMode int windowingMode, WindowContainerTransaction wct) {
234 DisplayAreaInfo displayAreaInfo = mRootTaskDisplayAreaOrganizer.getDisplayAreaInfo(
235 displayId);
236 if (displayAreaInfo == null) {
237 ProtoLog.e(WM_SHELL_DESKTOP_MODE,
238 "unable to update windowing mode for display %d display not found", displayId);
239 return;
240 }
241
242 ProtoLog.v(WM_SHELL_DESKTOP_MODE,
243 "setWindowingMode: displayId=%d current wmMode=%d new wmMode=%d", displayId,
244 displayAreaInfo.configuration.windowConfiguration.getWindowingMode(),
245 windowingMode);
246
247 wct.setWindowingMode(displayAreaInfo.token, windowingMode);
248 }
249
250 /**
251 * Show apps on desktop
252 */
253 void showDesktopApps() {
254 WindowContainerTransaction wct = bringDesktopAppsToFront();
255
256 if (Transitions.ENABLE_SHELL_TRANSITIONS) {
257 mTransitions.startTransition(TRANSIT_TO_FRONT, wct, null /* handler */);
258 } else {
259 mShellTaskOrganizer.applyTransaction(wct);
260 }
261 }
262
263 @NonNull
264 private WindowContainerTransaction bringDesktopAppsToFront() {
265 final WindowContainerTransaction wct = new WindowContainerTransaction();
266 final ArraySet<Integer> activeTasks = mDesktopModeTaskRepository.getActiveTasks();
267 ProtoLog.d(WM_SHELL_DESKTOP_MODE, "bringDesktopAppsToFront: tasks=%s", activeTasks.size());
268
269 final List<RunningTaskInfo> taskInfos = new ArrayList<>();
270 for (Integer taskId : activeTasks) {
271 RunningTaskInfo taskInfo = mShellTaskOrganizer.getRunningTaskInfo(taskId);
272 if (taskInfo != null) {
273 taskInfos.add(taskInfo);
274 }
275 }
276
277 if (taskInfos.isEmpty()) {
278 return wct;
279 }
280
281 final boolean allActiveTasksAreVisible = taskInfos.stream()
282 .allMatch(info -> mDesktopModeTaskRepository.isVisibleTask(info.taskId));
283 if (allActiveTasksAreVisible) {
284 ProtoLog.d(WM_SHELL_DESKTOP_MODE,
285 "bringDesktopAppsToFront: active tasks are already in front, skipping.");
286 return wct;
287 }
288 ProtoLog.d(WM_SHELL_DESKTOP_MODE,
289 "bringDesktopAppsToFront: reordering all active tasks to the front");
290 final List<Integer> allTasksInZOrder =
291 mDesktopModeTaskRepository.getFreeformTasksInZOrder();
292 // Sort by z-order, bottom to top, so that the top-most task is reordered to the top last
293 // in the WCT.
294 taskInfos.sort(Comparator.comparingInt(task -> -allTasksInZOrder.indexOf(task.taskId)));
295 for (RunningTaskInfo task : taskInfos) {
296 wct.reorder(task.token, true);
297 }
298 return wct;
299 }
300
301 /**
302 * Turn desktop mode on or off
303 * @param active the desired state for desktop mode setting
304 */
305 public void setDesktopModeActive(boolean active) {
306 int value = active ? 1 : 0;
307 Settings.System.putInt(mContext.getContentResolver(), Settings.System.DESKTOP_MODE, value);
308 }
309
310 /**
311 * Returns the windowing mode of the display area with the specified displayId.
312 * @param displayId
313 * @return
314 */
315 public int getDisplayAreaWindowingMode(int displayId) {
316 return mRootTaskDisplayAreaOrganizer.getDisplayAreaInfo(displayId)
317 .configuration.windowConfiguration.getWindowingMode();
318 }
319
320 @Override
321 public boolean startAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
322 @NonNull SurfaceControl.Transaction startTransaction,
323 @NonNull SurfaceControl.Transaction finishTransaction,
324 @NonNull Transitions.TransitionFinishCallback finishCallback) {
325 // This handler should never be the sole handler, so should not animate anything.
326 return false;
327 }
328
329 @Nullable
330 @Override
331 public WindowContainerTransaction handleRequest(@NonNull IBinder transition,
332 @NonNull TransitionRequestInfo request) {
333 // Only do anything if we are in desktop mode and opening/moving-to-front a task/app in
334 // freeform
335 if (!DesktopModeStatus.isActive(mContext)) {
336 ProtoLog.d(WM_SHELL_DESKTOP_MODE,
337 "skip shell transition request: desktop mode not active");
338 return null;
339 }
340 if (request.getType() != TRANSIT_OPEN && request.getType() != TRANSIT_TO_FRONT) {
341 ProtoLog.d(WM_SHELL_DESKTOP_MODE,
342 "skip shell transition request: unsupported type %s",
343 WindowManager.transitTypeToString(request.getType()));
344 return null;
345 }
346 if (request.getTriggerTask() == null
347 || request.getTriggerTask().getWindowingMode() != WINDOWING_MODE_FREEFORM) {
348 ProtoLog.d(WM_SHELL_DESKTOP_MODE, "skip shell transition request: not freeform task");
349 return null;
350 }
351 ProtoLog.d(WM_SHELL_DESKTOP_MODE, "handle shell transition request: %s", request);
352
353 WindowContainerTransaction wct = mTransitions.dispatchRequest(transition, request, this);
354 if (wct == null) {
355 wct = new WindowContainerTransaction();
356 }
357 wct.merge(bringDesktopAppsToFront(), true /* transfer */);
358 wct.reorder(request.getTriggerTask().token, true /* onTop */);
359
360 return wct;
361 }
362
363 /**
364 * A {@link ContentObserver} for listening to changes to {@link Settings.System#DESKTOP_MODE}
365 */
366 private final class SettingsObserver extends ContentObserver {
367
368 private final Uri mDesktopModeSetting = Settings.System.getUriFor(
369 Settings.System.DESKTOP_MODE);
370
371 private final Context mContext;
372
373 SettingsObserver(Context context, Handler handler) {
374 super(handler);
375 mContext = context;
376 }
377
378 public void observe() {
379 // TODO(b/242867463): listen for setting change for all users
380 mContext.getContentResolver().registerContentObserver(mDesktopModeSetting,
381 false /* notifyForDescendants */, this /* observer */, UserHandle.USER_CURRENT);
382 }
383
384 @Override
385 public void onChange(boolean selfChange, @Nullable Uri uri) {
386 if (mDesktopModeSetting.equals(uri)) {
387 ProtoLog.d(WM_SHELL_DESKTOP_MODE, "Received update for desktop mode setting");
388 desktopModeSettingChanged();
389 }
390 }
391
392 private void desktopModeSettingChanged() {
393 boolean enabled = DesktopModeStatus.isActive(mContext);
394 updateDesktopModeActive(enabled);
395 }
396 }
397
398 /**
399 * The interface for calls from outside the shell, within the host process.
400 */
401 @ExternalThread
402 private final class DesktopModeImpl implements DesktopMode {
403
404 @Override
405 public void addListener(DesktopModeTaskRepository.VisibleTasksListener listener,
406 Executor callbackExecutor) {
407 mMainExecutor.execute(() -> {
408 DesktopModeController.this.addListener(listener, callbackExecutor);
409 });
410 }
411 }
412
413 /**
414 * The interface for calls from outside the host process.
415 */
416 @BinderThread
417 private static class IDesktopModeImpl extends IDesktopMode.Stub
418 implements ExternalInterfaceBinder {
419
420 private DesktopModeController mController;
421
422 IDesktopModeImpl(DesktopModeController controller) {
423 mController = controller;
424 }
425
426 /**
427 * Invalidates this instance, preventing future calls from updating the controller.
428 */
429 @Override
430 public void invalidate() {
431 mController = null;
432 }
433
434 public void showDesktopApps() {
435 executeRemoteCallWithTaskPermission(mController, "showDesktopApps",
436 DesktopModeController::showDesktopApps);
437 }
438 }
439 }