Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/kidsmode/KidsModeTaskOrganizer.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.kidsmode;
18
19 import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
20 import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
21 import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
22 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
23 import static android.view.Display.DEFAULT_DISPLAY;
24
25 import android.app.ActivityManager;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.content.res.Configuration;
31 import android.graphics.Rect;
32 import android.os.Binder;
33 import android.os.Handler;
34 import android.os.IBinder;
35 import android.view.InsetsSource;
36 import android.view.InsetsState;
37 import android.view.SurfaceControl;
38 import android.window.ITaskOrganizerController;
39 import android.window.TaskAppearedInfo;
40 import android.window.WindowContainerToken;
41 import android.window.WindowContainerTransaction;
42
43 import androidx.annotation.NonNull;
44
45 import com.android.internal.annotations.VisibleForTesting;
46 import com.android.wm.shell.ShellTaskOrganizer;
47 import com.android.wm.shell.common.DisplayController;
48 import com.android.wm.shell.common.DisplayInsetsController;
49 import com.android.wm.shell.common.DisplayLayout;
50 import com.android.wm.shell.common.ShellExecutor;
51 import com.android.wm.shell.common.SyncTransactionQueue;
52 import com.android.wm.shell.recents.RecentTasksController;
53 import com.android.wm.shell.sysui.ShellCommandHandler;
54 import com.android.wm.shell.sysui.ShellInit;
55 import com.android.wm.shell.unfold.UnfoldAnimationController;
56
57 import java.io.PrintWriter;
58 import java.util.List;
59 import java.util.Objects;
60 import java.util.Optional;
61
62 /**
63 * A dedicated task organizer when kids mode is enabled.
64 * - Creates a root task with bounds that exclude the navigation bar area
65 * - Launch all task into the root task except for Launcher
66 */
67 public class KidsModeTaskOrganizer extends ShellTaskOrganizer {
68 private static final String TAG = "KidsModeTaskOrganizer";
69
70 private static final int[] CONTROLLED_ACTIVITY_TYPES =
71 {ACTIVITY_TYPE_UNDEFINED, ACTIVITY_TYPE_STANDARD};
72 private static final int[] CONTROLLED_WINDOWING_MODES =
73 {WINDOWING_MODE_FULLSCREEN, WINDOWING_MODE_UNDEFINED};
74
75 private final Handler mMainHandler;
76 private final Context mContext;
77 private final ShellCommandHandler mShellCommandHandler;
78 private final SyncTransactionQueue mSyncQueue;
79 private final DisplayController mDisplayController;
80 private final DisplayInsetsController mDisplayInsetsController;
81
82 @VisibleForTesting
83 ActivityManager.RunningTaskInfo mLaunchRootTask;
84 @VisibleForTesting
85 SurfaceControl mLaunchRootLeash;
86 @VisibleForTesting
87 final IBinder mCookie = new Binder();
88
89 private final InsetsState mInsetsState = new InsetsState();
90 private int mDisplayWidth;
91 private int mDisplayHeight;
92
93 private KidsModeSettingsObserver mKidsModeSettingsObserver;
94 private boolean mEnabled;
95
96 private final BroadcastReceiver mUserSwitchIntentReceiver = new BroadcastReceiver() {
97 @Override
98 public void onReceive(Context context, Intent intent) {
99 updateKidsModeState();
100 }
101 };
102
103 DisplayController.OnDisplaysChangedListener mOnDisplaysChangedListener =
104 new DisplayController.OnDisplaysChangedListener() {
105 @Override
106 public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) {
107 if (displayId != DEFAULT_DISPLAY) {
108 return;
109 }
110 final DisplayLayout displayLayout =
111 mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
112 if (displayLayout == null) {
113 return;
114 }
115 final int displayWidth = displayLayout.width();
116 final int displayHeight = displayLayout.height();
117 if (displayWidth == mDisplayWidth || displayHeight == mDisplayHeight) {
118 return;
119 }
120 mDisplayWidth = displayWidth;
121 mDisplayHeight = displayHeight;
122 updateBounds();
123 }
124 };
125
126 DisplayInsetsController.OnInsetsChangedListener mOnInsetsChangedListener =
127 new DisplayInsetsController.OnInsetsChangedListener() {
128 @Override
129 public void insetsChanged(InsetsState insetsState) {
130 // Update bounds only when the insets of navigation bar or task bar is changed.
131 if (Objects.equals(insetsState.peekSource(InsetsState.ITYPE_NAVIGATION_BAR),
132 mInsetsState.peekSource(InsetsState.ITYPE_NAVIGATION_BAR))
133 && Objects.equals(insetsState.peekSource(
134 InsetsState.ITYPE_EXTRA_NAVIGATION_BAR),
135 mInsetsState.peekSource(InsetsState.ITYPE_EXTRA_NAVIGATION_BAR))) {
136 return;
137 }
138 mInsetsState.set(insetsState);
139 updateBounds();
140 }
141 };
142
143 @VisibleForTesting
144 KidsModeTaskOrganizer(
145 Context context,
146 ShellInit shellInit,
147 ShellCommandHandler shellCommandHandler,
148 ITaskOrganizerController taskOrganizerController,
149 SyncTransactionQueue syncTransactionQueue,
150 DisplayController displayController,
151 DisplayInsetsController displayInsetsController,
152 Optional<UnfoldAnimationController> unfoldAnimationController,
153 Optional<RecentTasksController> recentTasks,
154 KidsModeSettingsObserver kidsModeSettingsObserver,
155 ShellExecutor mainExecutor,
156 Handler mainHandler) {
157 // Note: we don't call super with the shell init because we will be initializing manually
158 super(/* shellInit= */ null, /* shellCommandHandler= */ null, taskOrganizerController,
159 /* compatUI= */ null, unfoldAnimationController, recentTasks, mainExecutor);
160 mContext = context;
161 mShellCommandHandler = shellCommandHandler;
162 mMainHandler = mainHandler;
163 mSyncQueue = syncTransactionQueue;
164 mDisplayController = displayController;
165 mDisplayInsetsController = displayInsetsController;
166 mKidsModeSettingsObserver = kidsModeSettingsObserver;
167 shellInit.addInitCallback(this::onInit, this);
168 }
169
170 public KidsModeTaskOrganizer(
171 Context context,
172 ShellInit shellInit,
173 ShellCommandHandler shellCommandHandler,
174 SyncTransactionQueue syncTransactionQueue,
175 DisplayController displayController,
176 DisplayInsetsController displayInsetsController,
177 Optional<UnfoldAnimationController> unfoldAnimationController,
178 Optional<RecentTasksController> recentTasks,
179 ShellExecutor mainExecutor,
180 Handler mainHandler) {
181 // Note: we don't call super with the shell init because we will be initializing manually
182 super(/* shellInit= */ null, /* taskOrganizerController= */ null, /* compatUI= */ null,
183 unfoldAnimationController, recentTasks, mainExecutor);
184 mContext = context;
185 mShellCommandHandler = shellCommandHandler;
186 mMainHandler = mainHandler;
187 mSyncQueue = syncTransactionQueue;
188 mDisplayController = displayController;
189 mDisplayInsetsController = displayInsetsController;
190 shellInit.addInitCallback(this::onInit, this);
191 }
192
193 /**
194 * Initializes kids mode status.
195 */
196 public void onInit() {
197 if (mShellCommandHandler != null) {
198 mShellCommandHandler.addDumpCallback(this::dump, this);
199 }
200 if (mKidsModeSettingsObserver == null) {
201 mKidsModeSettingsObserver = new KidsModeSettingsObserver(mMainHandler, mContext);
202 }
203 mKidsModeSettingsObserver.setOnChangeRunnable(() -> updateKidsModeState());
204 updateKidsModeState();
205 mKidsModeSettingsObserver.register();
206
207 final IntentFilter filter = new IntentFilter();
208 filter.addAction(Intent.ACTION_USER_SWITCHED);
209 mContext.registerReceiverForAllUsers(mUserSwitchIntentReceiver, filter, null, mMainHandler);
210 }
211
212 @Override
213 public void onTaskAppeared(ActivityManager.RunningTaskInfo taskInfo, SurfaceControl leash) {
214 if (mEnabled && mLaunchRootTask == null && taskInfo.launchCookies != null
215 && taskInfo.launchCookies.contains(mCookie)) {
216 mLaunchRootTask = taskInfo;
217 mLaunchRootLeash = leash;
218 updateTask();
219 }
220 super.onTaskAppeared(taskInfo, leash);
221
222 mSyncQueue.runInSync(t -> {
223 // Reset several properties back to fullscreen (PiP, for example, leaves all these
224 // properties in a bad state).
225 t.setCrop(leash, null);
226 t.setPosition(leash, 0, 0);
227 t.setAlpha(leash, 1f);
228 t.setMatrix(leash, 1, 0, 0, 1);
229 t.show(leash);
230 });
231 }
232
233 @Override
234 public void onTaskInfoChanged(ActivityManager.RunningTaskInfo taskInfo) {
235 if (mLaunchRootTask != null && mLaunchRootTask.taskId == taskInfo.taskId
236 && !taskInfo.equals(mLaunchRootTask)) {
237 mLaunchRootTask = taskInfo;
238 }
239
240 super.onTaskInfoChanged(taskInfo);
241 }
242
243 @VisibleForTesting
244 void updateKidsModeState() {
245 final boolean enabled = mKidsModeSettingsObserver.isEnabled();
246 if (mEnabled == enabled) {
247 return;
248 }
249 mEnabled = enabled;
250 if (mEnabled) {
251 enable();
252 } else {
253 disable();
254 }
255 }
256
257 @VisibleForTesting
258 void enable() {
259 // Needed since many Kids apps aren't optimised to support both orientations and it will be
260 // hard for kids to understand the app compat mode.
261 // TODO(229961548): Remove ignoreOrientationRequest exception for Kids Mode once possible.
262 setIsIgnoreOrientationRequestDisabled(true);
263 final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
264 if (displayLayout != null) {
265 mDisplayWidth = displayLayout.width();
266 mDisplayHeight = displayLayout.height();
267 }
268 mInsetsState.set(mDisplayController.getInsetsState(DEFAULT_DISPLAY));
269 mDisplayInsetsController.addInsetsChangedListener(DEFAULT_DISPLAY,
270 mOnInsetsChangedListener);
271 mDisplayController.addDisplayWindowListener(mOnDisplaysChangedListener);
272 List<TaskAppearedInfo> taskAppearedInfos = registerOrganizer();
273 for (int i = 0; i < taskAppearedInfos.size(); i++) {
274 final TaskAppearedInfo info = taskAppearedInfos.get(i);
275 onTaskAppeared(info.getTaskInfo(), info.getLeash());
276 }
277 createRootTask(DEFAULT_DISPLAY, WINDOWING_MODE_FULLSCREEN, mCookie);
278 updateTask();
279 }
280
281 @VisibleForTesting
282 void disable() {
283 setIsIgnoreOrientationRequestDisabled(false);
284 mDisplayInsetsController.removeInsetsChangedListener(DEFAULT_DISPLAY,
285 mOnInsetsChangedListener);
286 mDisplayController.removeDisplayWindowListener(mOnDisplaysChangedListener);
287 updateTask();
288 final WindowContainerToken token = mLaunchRootTask.token;
289 if (token != null) {
290 deleteRootTask(token);
291 }
292 mLaunchRootTask = null;
293 mLaunchRootLeash = null;
294 unregisterOrganizer();
295 }
296
297 private void updateTask() {
298 updateTask(getWindowContainerTransaction());
299 }
300
301 private void updateTask(WindowContainerTransaction wct) {
302 if (mLaunchRootTask == null || mLaunchRootLeash == null) {
303 return;
304 }
305 final Rect taskBounds = calculateBounds();
306 final WindowContainerToken rootToken = mLaunchRootTask.token;
307 wct.setBounds(rootToken, mEnabled ? taskBounds : null);
308 wct.setLaunchRoot(rootToken,
309 mEnabled ? CONTROLLED_WINDOWING_MODES : null,
310 mEnabled ? CONTROLLED_ACTIVITY_TYPES : null);
311 wct.reparentTasks(
312 mEnabled ? null : rootToken /* currentParent */,
313 mEnabled ? rootToken : null /* newParent */,
314 CONTROLLED_WINDOWING_MODES,
315 CONTROLLED_ACTIVITY_TYPES,
316 true /* onTop */);
317 wct.reorder(rootToken, mEnabled /* onTop */);
318 mSyncQueue.queue(wct);
319 if (mEnabled) {
320 final SurfaceControl rootLeash = mLaunchRootLeash;
321 mSyncQueue.runInSync(t -> {
322 t.setPosition(rootLeash, taskBounds.left, taskBounds.top);
323 t.setWindowCrop(rootLeash, taskBounds.width(), taskBounds.height());
324 });
325 }
326 }
327
328 private Rect calculateBounds() {
329 final Rect bounds = new Rect(0, 0, mDisplayWidth, mDisplayHeight);
330 final InsetsSource navBarSource = mInsetsState.peekSource(InsetsState.ITYPE_NAVIGATION_BAR);
331 final InsetsSource taskBarSource = mInsetsState.peekSource(
332 InsetsState.ITYPE_EXTRA_NAVIGATION_BAR);
333 if (navBarSource != null && !navBarSource.getFrame().isEmpty()) {
334 bounds.inset(navBarSource.calculateInsets(bounds, false /* ignoreVisibility */));
335 } else if (taskBarSource != null && !taskBarSource.getFrame().isEmpty()) {
336 bounds.inset(taskBarSource.calculateInsets(bounds, false /* ignoreVisibility */));
337 } else {
338 bounds.setEmpty();
339 }
340 return bounds;
341 }
342
343 private void updateBounds() {
344 if (mLaunchRootTask == null) {
345 return;
346 }
347 final WindowContainerTransaction wct = getWindowContainerTransaction();
348 final Rect taskBounds = calculateBounds();
349 wct.setBounds(mLaunchRootTask.token, taskBounds);
350 mSyncQueue.queue(wct);
351 final SurfaceControl finalLeash = mLaunchRootLeash;
352 mSyncQueue.runInSync(t -> {
353 t.setPosition(finalLeash, taskBounds.left, taskBounds.top);
354 t.setWindowCrop(finalLeash, taskBounds.width(), taskBounds.height());
355 });
356 }
357
358 @VisibleForTesting
359 WindowContainerTransaction getWindowContainerTransaction() {
360 return new WindowContainerTransaction();
361 }
362
363 @Override
364 public void dump(@NonNull PrintWriter pw, String prefix) {
365 final String innerPrefix = prefix + " ";
366 pw.println(prefix + TAG);
367 pw.println(innerPrefix + " mEnabled=" + mEnabled);
368 pw.println(innerPrefix + " mLaunchRootTask=" + mLaunchRootTask);
369 pw.println(innerPrefix + " mLaunchRootLeash=" + mLaunchRootLeash);
370 pw.println(innerPrefix + " mDisplayWidth=" + mDisplayWidth);
371 pw.println(innerPrefix + " mDisplayHeight=" + mDisplayHeight);
372 pw.println(innerPrefix + " mInsetsState=" + mInsetsState);
373 super.dump(pw, innerPrefix);
374 }
375 }