Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/common/SystemWindows.java

raw symbols SCIP
1 /*
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
17 package com.android.wm.shell.common;
18
19 import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
20
21 import android.annotation.NonNull;
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.graphics.Region;
25 import android.os.Bundle;
26 import android.os.IBinder;
27 import android.os.ParcelFileDescriptor;
28 import android.os.RemoteException;
29 import android.util.MergedConfiguration;
30 import android.util.Slog;
31 import android.util.SparseArray;
32 import android.view.Display;
33 import android.view.DragEvent;
34 import android.view.IScrollCaptureResponseListener;
35 import android.view.IWindow;
36 import android.view.IWindowManager;
37 import android.view.IWindowSession;
38 import android.view.IWindowSessionCallback;
39 import android.view.InsetsSourceControl;
40 import android.view.InsetsState;
41 import android.view.ScrollCaptureResponse;
42 import android.view.SurfaceControl;
43 import android.view.SurfaceControlViewHost;
44 import android.view.SurfaceSession;
45 import android.view.View;
46 import android.view.ViewGroup;
47 import android.view.WindowManager;
48 import android.view.WindowlessWindowManager;
49 import android.window.ClientWindowFrames;
50
51 import com.android.internal.os.IResultReceiver;
52
53 import java.util.HashMap;
54
55 /**
56 * Represents the "windowing" layer of the WM Shell. This layer allows shell components to place and
57 * manipulate windows without talking to WindowManager.
58 */
59 public class SystemWindows {
60 private static final String TAG = "SystemWindows";
61
62 private final SparseArray<PerDisplay> mPerDisplay = new SparseArray<>();
63 private final HashMap<View, SurfaceControlViewHost> mViewRoots = new HashMap<>();
64 private final DisplayController mDisplayController;
65 private final IWindowManager mWmService;
66 private IWindowSession mSession;
67
68 private final DisplayController.OnDisplaysChangedListener mDisplayListener =
69 new DisplayController.OnDisplaysChangedListener() {
70 @Override
71 public void onDisplayAdded(int displayId) { }
72
73 @Override
74 public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) {
75 PerDisplay pd = mPerDisplay.get(displayId);
76 if (pd == null) {
77 return;
78 }
79 pd.updateConfiguration(newConfig);
80 }
81
82 @Override
83 public void onDisplayRemoved(int displayId) { }
84 };
85
86 public SystemWindows(DisplayController displayController, IWindowManager wmService) {
87 mWmService = wmService;
88 mDisplayController = displayController;
89 mDisplayController.addDisplayWindowListener(mDisplayListener);
90 try {
91 mSession = wmService.openSession(
92 new IWindowSessionCallback.Stub() {
93 @Override
94 public void onAnimatorScaleChanged(float scale) {}
95 });
96 } catch (RemoteException e) {
97 Slog.e(TAG, "Unable to create layer", e);
98 }
99 }
100
101 /**
102 * Adds a view to system-ui window management.
103 */
104 public void addView(View view, WindowManager.LayoutParams attrs, int displayId,
105 @WindowManager.ShellRootLayer int shellRootLayer) {
106 PerDisplay pd = mPerDisplay.get(displayId);
107 if (pd == null) {
108 pd = new PerDisplay(displayId);
109 mPerDisplay.put(displayId, pd);
110 }
111 pd.addView(view, attrs, shellRootLayer);
112 }
113
114 /**
115 * Removes a view from system-ui window management.
116 * @param view
117 */
118 public void removeView(View view) {
119 SurfaceControlViewHost root = mViewRoots.remove(view);
120 root.release();
121 }
122
123 /**
124 * Updates the layout params of a view.
125 */
126 public void updateViewLayout(@NonNull View view, ViewGroup.LayoutParams params) {
127 SurfaceControlViewHost root = mViewRoots.get(view);
128 if (root == null || !(params instanceof WindowManager.LayoutParams)) {
129 return;
130 }
131 view.setLayoutParams(params);
132 root.relayout((WindowManager.LayoutParams) params);
133 }
134
135 /**
136 * Sets the accessibility window for the given {@param shellRootLayer}.
137 */
138 public void setShellRootAccessibilityWindow(int displayId,
139 @WindowManager.ShellRootLayer int shellRootLayer, View view) {
140 PerDisplay pd = mPerDisplay.get(displayId);
141 if (pd == null) {
142 return;
143 }
144 pd.setShellRootAccessibilityWindow(shellRootLayer, view);
145 }
146
147 /**
148 * Sets the touchable region of a view's window. This will be cropped to the window size.
149 * @param view
150 * @param region
151 */
152 public void setTouchableRegion(@NonNull View view, Region region) {
153 SurfaceControlViewHost root = mViewRoots.get(view);
154 if (root == null) {
155 return;
156 }
157 WindowlessWindowManager wwm = root.getWindowlessWM();
158 if (!(wwm instanceof SysUiWindowManager)) {
159 return;
160 }
161 ((SysUiWindowManager) wwm).setTouchableRegionForWindow(view, region);
162 }
163
164 /**
165 * Get the IWindow token for a specific root.
166 *
167 * @param windowType A window type from {@link WindowManager}.
168 */
169 IWindow getWindow(int displayId, int windowType) {
170 PerDisplay pd = mPerDisplay.get(displayId);
171 if (pd == null) {
172 return null;
173 }
174 return pd.getWindow(windowType);
175 }
176
177 /**
178 * Gets the SurfaceControl associated with a root view. This is the same surface that backs the
179 * ViewRootImpl.
180 */
181 public SurfaceControl getViewSurface(View rootView) {
182 for (int i = 0; i < mPerDisplay.size(); ++i) {
183 for (int iWm = 0; iWm < mPerDisplay.valueAt(i).mWwms.size(); ++iWm) {
184 SurfaceControl out = mPerDisplay.valueAt(i).mWwms.valueAt(iWm)
185 .getSurfaceControlForWindow(rootView);
186 if (out != null) {
187 return out;
188 }
189 }
190 }
191 return null;
192 }
193
194 /**
195 * Gets a token associated with the view that can be used to grant the view focus.
196 */
197 public IBinder getFocusGrantToken(View view) {
198 SurfaceControlViewHost root = mViewRoots.get(view);
199 if (root == null) {
200 Slog.e(TAG, "Couldn't get focus grant token since view does not exist in "
201 + "SystemWindow:" + view);
202 return null;
203 }
204 return root.getFocusGrantToken();
205 }
206
207 private class PerDisplay {
208 final int mDisplayId;
209 private final SparseArray<SysUiWindowManager> mWwms = new SparseArray<>();
210
211 PerDisplay(int displayId) {
212 mDisplayId = displayId;
213 }
214
215 public void addView(View view, WindowManager.LayoutParams attrs,
216 @WindowManager.ShellRootLayer int shellRootLayer) {
217 SysUiWindowManager wwm = addRoot(shellRootLayer);
218 if (wwm == null) {
219 Slog.e(TAG, "Unable to create systemui root");
220 return;
221 }
222 final Display display = mDisplayController.getDisplay(mDisplayId);
223 SurfaceControlViewHost viewRoot =
224 new SurfaceControlViewHost(view.getContext(), display, wwm);
225 attrs.flags |= FLAG_HARDWARE_ACCELERATED;
226 viewRoot.setView(view, attrs);
227 mViewRoots.put(view, viewRoot);
228 setShellRootAccessibilityWindow(shellRootLayer, view);
229 }
230
231 SysUiWindowManager addRoot(@WindowManager.ShellRootLayer int shellRootLayer) {
232 SysUiWindowManager wwm = mWwms.get(shellRootLayer);
233 if (wwm != null) {
234 return wwm;
235 }
236 SurfaceControl rootSurface = null;
237 ContainerWindow win = new ContainerWindow();
238 try {
239 rootSurface = mWmService.addShellRoot(mDisplayId, win, shellRootLayer);
240 } catch (RemoteException e) {
241 }
242 if (rootSurface == null) {
243 Slog.e(TAG, "Unable to get root surfacecontrol for systemui");
244 return null;
245 }
246 Context displayContext = mDisplayController.getDisplayContext(mDisplayId);
247 wwm = new SysUiWindowManager(mDisplayId, displayContext, rootSurface, win);
248 mWwms.put(shellRootLayer, wwm);
249 return wwm;
250 }
251
252 IWindow getWindow(int windowType) {
253 SysUiWindowManager wwm = mWwms.get(windowType);
254 if (wwm == null) {
255 return null;
256 }
257 return wwm.mContainerWindow;
258 }
259
260 void setShellRootAccessibilityWindow(@WindowManager.ShellRootLayer int shellRootLayer,
261 View view) {
262 SysUiWindowManager wwm = mWwms.get(shellRootLayer);
263 if (wwm == null) {
264 return;
265 }
266 try {
267 mWmService.setShellRootAccessibilityWindow(mDisplayId, shellRootLayer,
268 view != null ? mViewRoots.get(view).getWindowToken() : null);
269 } catch (RemoteException e) {
270 Slog.e(TAG, "Error setting accessibility window for " + mDisplayId + ":"
271 + shellRootLayer, e);
272 }
273 }
274
275 void updateConfiguration(Configuration configuration) {
276 for (int i = 0; i < mWwms.size(); ++i) {
277 mWwms.valueAt(i).updateConfiguration(configuration);
278 }
279 }
280 }
281
282 /**
283 * A subclass of WindowlessWindowManager that provides insets to its viewroots.
284 */
285 public class SysUiWindowManager extends WindowlessWindowManager {
286 final int mDisplayId;
287 ContainerWindow mContainerWindow;
288 final HashMap<IBinder, SurfaceControl> mLeashForWindow =
289 new HashMap<IBinder, SurfaceControl>();
290 public SysUiWindowManager(int displayId, Context ctx, SurfaceControl rootSurface,
291 ContainerWindow container) {
292 super(ctx.getResources().getConfiguration(), rootSurface, null /* hostInputToken */);
293 mContainerWindow = container;
294 mDisplayId = displayId;
295 }
296
297 void updateConfiguration(Configuration configuration) {
298 setConfiguration(configuration);
299 }
300
301 SurfaceControl getSurfaceControlForWindow(View rootView) {
302 synchronized (this) {
303 return mLeashForWindow.get(getWindowBinder(rootView));
304 }
305 }
306
307 protected void attachToParentSurface(IWindow window, SurfaceControl.Builder b) {
308 SurfaceControl leash = new SurfaceControl.Builder(new SurfaceSession())
309 .setContainerLayer()
310 .setName("SystemWindowLeash")
311 .setHidden(false)
312 .setParent(mRootSurface)
313 .setCallsite("SysUiWIndowManager#attachToParentSurface").build();
314 synchronized (this) {
315 mLeashForWindow.put(window.asBinder(), leash);
316 }
317 b.setParent(leash);
318 }
319
320 @Override
321 public void remove(android.view.IWindow window) throws RemoteException {
322 super.remove(window);
323 synchronized(this) {
324 IBinder token = window.asBinder();
325 new SurfaceControl.Transaction().remove(mLeashForWindow.get(token))
326 .apply();
327 mLeashForWindow.remove(token);
328 }
329 }
330
331 void setTouchableRegionForWindow(View rootView, Region region) {
332 IBinder token = rootView.getWindowToken();
333 if (token == null) {
334 return;
335 }
336 setTouchRegion(token, region);
337 }
338 }
339
340 static class ContainerWindow extends IWindow.Stub {
341 ContainerWindow() {}
342
343 @Override
344 public void resized(ClientWindowFrames frames, boolean reportDraw,
345 MergedConfiguration newMergedConfiguration, InsetsState insetsState,
346 boolean forceLayout, boolean alwaysConsumeSystemBars, int displayId, int syncSeqId,
347 int resizeMode) {}
348
349 @Override
350 public void insetsControlChanged(InsetsState insetsState,
351 InsetsSourceControl[] activeControls) {}
352
353 @Override
354 public void showInsets(int types, boolean fromIme) {}
355
356 @Override
357 public void hideInsets(int types, boolean fromIme) {}
358
359 @Override
360 public void moved(int newX, int newY) {}
361
362 @Override
363 public void dispatchAppVisibility(boolean visible) {}
364
365 @Override
366 public void dispatchGetNewSurface() {}
367
368 @Override
369 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {}
370
371 @Override
372 public void closeSystemDialogs(String reason) {}
373
374 @Override
375 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
376 float zoom, boolean sync) {}
377
378 @Override
379 public void dispatchWallpaperCommand(String action, int x, int y,
380 int z, Bundle extras, boolean sync) {}
381
382 /* Drag/drop */
383 @Override
384 public void dispatchDragEvent(DragEvent event) {}
385
386 @Override
387 public void updatePointerIcon(float x, float y) {}
388
389 @Override
390 public void dispatchWindowShown() {}
391
392 @Override
393 public void requestAppKeyboardShortcuts(IResultReceiver receiver, int deviceId) {}
394
395 @Override
396 public void requestScrollCapture(IScrollCaptureResponseListener listener) {
397 try {
398 listener.onScrollCaptureResponse(
399 new ScrollCaptureResponse.Builder()
400 .setDescription("Not Implemented")
401 .build());
402
403 } catch (RemoteException ex) {
404 // ignore
405 }
406 }
407 }
408 }