Debian Codegraph @

core/java/android/view/WindowlessWindowManager.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 android.view;
18
19 import static android.view.WindowCallbacks.RESIZE_MODE_INVALID;
20
21 import android.annotation.Nullable;
22 import android.content.res.Configuration;
23 import android.graphics.PixelFormat;
24 import android.graphics.Rect;
25 import android.graphics.Region;
26 import android.os.Binder;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.os.RemoteCallback;
30 import android.os.RemoteException;
31 import android.util.Log;
32 import android.util.MergedConfiguration;
33 import android.window.ClientWindowFrames;
34 import android.window.OnBackInvokedCallbackInfo;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Objects;
39
40 /**
41 * A simplistic implementation of IWindowSession. Rather than managing Surfaces
42 * as children of the display, it manages Surfaces as children of a given root.
43 *
44 * By parcelling the root surface, the app can offer another app content for embedding.
45 * @hide
46 */
47 public class WindowlessWindowManager implements IWindowSession {
48 private final static String TAG = "WindowlessWindowManager";
49
50 private class State {
51 SurfaceControl mSurfaceControl;
52 WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
53 int mDisplayId;
54 IBinder mInputChannelToken;
55 Region mInputRegion;
56 IWindow mClient;
57 State(SurfaceControl sc, WindowManager.LayoutParams p, int displayId,
58 IBinder inputChannelToken, IWindow client) {
59 mSurfaceControl = sc;
60 mParams.copyFrom(p);
61 mDisplayId = displayId;
62 mInputChannelToken = inputChannelToken;
63 mClient = client;
64 }
65 };
66
67 /**
68 * Used to store SurfaceControl we've built for clients to
69 * reconfigure them if relayout is called.
70 */
71 final HashMap<IBinder, State> mStateForWindow = new HashMap<IBinder, State>();
72
73 public interface ResizeCompleteCallback {
74 public void finished(SurfaceControl.Transaction completion);
75 }
76
77 final HashMap<IBinder, ResizeCompleteCallback> mResizeCompletionForWindow =
78 new HashMap<IBinder, ResizeCompleteCallback>();
79
80 private final SurfaceSession mSurfaceSession = new SurfaceSession();
81 protected final SurfaceControl mRootSurface;
82 private final Configuration mConfiguration;
83 private final IWindowSession mRealWm;
84 private final IBinder mHostInputToken;
85 private final IBinder mFocusGrantToken = new Binder();
86 private InsetsState mInsetsState;
87 private final ClientWindowFrames mTmpFrames = new ClientWindowFrames();
88 private final MergedConfiguration mTmpConfig = new MergedConfiguration();
89
90 public WindowlessWindowManager(Configuration c, SurfaceControl rootSurface,
91 IBinder hostInputToken) {
92 mRootSurface = rootSurface;
93 mConfiguration = new Configuration(c);
94 mRealWm = WindowManagerGlobal.getWindowSession();
95 mHostInputToken = hostInputToken;
96 }
97
98 public void setConfiguration(Configuration configuration) {
99 mConfiguration.setTo(configuration);
100 }
101
102 IBinder getFocusGrantToken() {
103 return mFocusGrantToken;
104 }
105
106 /**
107 * Utility API.
108 */
109 void setCompletionCallback(IBinder window, ResizeCompleteCallback callback) {
110 if (mResizeCompletionForWindow.get(window) != null) {
111 Log.w(TAG, "Unsupported overlapping resizes");
112 }
113 mResizeCompletionForWindow.put(window, callback);
114 }
115
116 protected void setTouchRegion(IBinder window, @Nullable Region region) {
117 State state;
118 synchronized (this) {
119 // Do everything while locked so that we synchronize with relayout. This should be a
120 // very infrequent operation.
121 state = mStateForWindow.get(window);
122 if (state == null) {
123 return;
124 }
125 if (Objects.equals(region, state.mInputRegion)) {
126 return;
127 }
128 state.mInputRegion = region != null ? new Region(region) : null;
129 if (state.mInputChannelToken != null) {
130 try {
131 mRealWm.updateInputChannel(state.mInputChannelToken, state.mDisplayId,
132 state.mSurfaceControl, state.mParams.flags, state.mParams.privateFlags,
133 state.mInputRegion);
134 } catch (RemoteException e) {
135 Log.e(TAG, "Failed to update surface input channel: ", e);
136 }
137 }
138 }
139 }
140
141 protected void attachToParentSurface(IWindow window, SurfaceControl.Builder b) {
142 b.setParent(mRootSurface);
143 }
144
145 /**
146 * IWindowSession implementation.
147 */
148 @Override
149 public int addToDisplay(IWindow window, WindowManager.LayoutParams attrs,
150 int viewVisibility, int displayId, InsetsVisibilities requestedVisibilities,
151 InputChannel outInputChannel, InsetsState outInsetsState,
152 InsetsSourceControl[] outActiveControls, Rect outAttachedFrame,
153 float[] outSizeCompatScale) {
154 final SurfaceControl.Builder b = new SurfaceControl.Builder(mSurfaceSession)
155 .setFormat(attrs.format)
156 .setBLASTLayer()
157 .setName(attrs.getTitle().toString())
158 .setCallsite("WindowlessWindowManager.addToDisplay");
159 attachToParentSurface(window, b);
160 final SurfaceControl sc = b.build();
161
162 if (((attrs.inputFeatures &
163 WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0)) {
164 try {
165 if (mRealWm instanceof IWindowSession.Stub) {
166 mRealWm.grantInputChannel(displayId,
167 new SurfaceControl(sc, "WindowlessWindowManager.addToDisplay"),
168 window, mHostInputToken, attrs.flags, attrs.privateFlags, attrs.type,
169 mFocusGrantToken, attrs.getTitle().toString(), outInputChannel);
170 } else {
171 mRealWm.grantInputChannel(displayId, sc, window, mHostInputToken, attrs.flags,
172 attrs.privateFlags, attrs.type, mFocusGrantToken,
173 attrs.getTitle().toString(), outInputChannel);
174 }
175 } catch (RemoteException e) {
176 Log.e(TAG, "Failed to grant input to surface: ", e);
177 }
178 }
179
180 final State state = new State(sc, attrs, displayId,
181 outInputChannel != null ? outInputChannel.getToken() : null, window);
182 synchronized (this) {
183 mStateForWindow.put(window.asBinder(), state);
184 }
185 outAttachedFrame.set(0, 0, -1, -1);
186 outSizeCompatScale[0] = 1f;
187
188 final int res = WindowManagerGlobal.ADD_OKAY | WindowManagerGlobal.ADD_FLAG_APP_VISIBLE |
189 WindowManagerGlobal.ADD_FLAG_USE_BLAST;
190
191 // Include whether the window is in touch mode.
192 return isInTouchMode() ? res | WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE : res;
193 }
194
195 /**
196 * IWindowSession implementation. Currently this class doesn't need to support for multi-user.
197 */
198 @Override
199 public int addToDisplayAsUser(IWindow window, WindowManager.LayoutParams attrs,
200 int viewVisibility, int displayId, int userId, InsetsVisibilities requestedVisibilities,
201 InputChannel outInputChannel, InsetsState outInsetsState,
202 InsetsSourceControl[] outActiveControls, Rect outAttachedFrame,
203 float[] outSizeCompatScale) {
204 return addToDisplay(window, attrs, viewVisibility, displayId, requestedVisibilities,
205 outInputChannel, outInsetsState, outActiveControls, outAttachedFrame,
206 outSizeCompatScale);
207 }
208
209 @Override
210 public int addToDisplayWithoutInputChannel(android.view.IWindow window,
211 android.view.WindowManager.LayoutParams attrs, int viewVisibility, int layerStackId,
212 android.view.InsetsState insetsState, Rect outAttachedFrame,
213 float[] outSizeCompatScale) {
214 return 0;
215 }
216
217 @Override
218 public void remove(android.view.IWindow window) throws RemoteException {
219 mRealWm.remove(window);
220 State state;
221 synchronized (this) {
222 state = mStateForWindow.remove(window.asBinder());
223 }
224 if (state == null) {
225 throw new IllegalArgumentException(
226 "Invalid window token (never added or removed already)");
227 }
228 removeSurface(state.mSurfaceControl);
229 }
230
231 /** Separate from {@link #remove} so that subclasses can put removal on a sync transaction. */
232 protected void removeSurface(SurfaceControl sc) {
233 try (SurfaceControl.Transaction t = new SurfaceControl.Transaction()) {
234 t.remove(sc).apply();
235 }
236 }
237
238 private boolean isOpaque(WindowManager.LayoutParams attrs) {
239 if (attrs.surfaceInsets != null && attrs.surfaceInsets.left != 0 ||
240 attrs.surfaceInsets.top != 0 || attrs.surfaceInsets.right != 0 ||
241 attrs.surfaceInsets.bottom != 0) {
242 return false;
243 }
244 return !PixelFormat.formatHasAlpha(attrs.format);
245 }
246
247 private boolean isInTouchMode() {
248 try {
249 return WindowManagerGlobal.getWindowSession().getInTouchMode();
250 } catch (RemoteException e) {
251 Log.e(TAG, "Unable to check if the window is in touch mode", e);
252 }
253 return false;
254 }
255
256 /** Access to package members for SystemWindow leashing
257 * @hide
258 */
259 protected IBinder getWindowBinder(View rootView) {
260 final ViewRootImpl root = rootView.getViewRootImpl();
261 if (root == null) {
262 return null;
263 }
264 return root.mWindow.asBinder();
265 }
266
267 /** @hide */
268 @Nullable
269 protected SurfaceControl getSurfaceControl(View rootView) {
270 final ViewRootImpl root = rootView.getViewRootImpl();
271 if (root == null) {
272 return null;
273 }
274 return getSurfaceControl(root.mWindow);
275 }
276
277 /** @hide */
278 @Nullable
279 protected SurfaceControl getSurfaceControl(IWindow window) {
280 final State s = mStateForWindow.get(window.asBinder());
281 if (s == null) {
282 return null;
283 }
284 return s.mSurfaceControl;
285 }
286
287 @Override
288 public int relayout(IWindow window, WindowManager.LayoutParams inAttrs,
289 int requestedWidth, int requestedHeight, int viewFlags, int flags, int seq,
290 int lastSyncSeqId, ClientWindowFrames outFrames,
291 MergedConfiguration outMergedConfiguration, SurfaceControl outSurfaceControl,
292 InsetsState outInsetsState, InsetsSourceControl[] outActiveControls,
293 Bundle outSyncSeqIdBundle) {
294 final State state;
295 synchronized (this) {
296 state = mStateForWindow.get(window.asBinder());
297 }
298 if (state == null) {
299 throw new IllegalArgumentException(
300 "Invalid window token (never added or removed already)");
301 }
302 SurfaceControl sc = state.mSurfaceControl;
303 SurfaceControl.Transaction t = new SurfaceControl.Transaction();
304
305 int attrChanges = 0;
306 if (inAttrs != null) {
307 attrChanges = state.mParams.copyFrom(inAttrs);
308 }
309 WindowManager.LayoutParams attrs = state.mParams;
310
311 if (viewFlags == View.VISIBLE) {
312 t.setOpaque(sc, isOpaque(attrs)).show(sc).apply();
313 if (outSurfaceControl != null) {
314 outSurfaceControl.copyFrom(sc, "WindowlessWindowManager.relayout");
315 }
316 } else {
317 t.hide(sc).apply();
318 if (outSurfaceControl != null) {
319 outSurfaceControl.release();
320 }
321 }
322 if (outFrames != null) {
323 outFrames.frame.set(0, 0, attrs.width, attrs.height);
324 outFrames.displayFrame.set(outFrames.frame);
325 }
326
327 if (outMergedConfiguration != null) {
328 outMergedConfiguration.setConfiguration(mConfiguration, mConfiguration);
329 }
330
331 if ((attrChanges & WindowManager.LayoutParams.FLAGS_CHANGED) != 0
332 && state.mInputChannelToken != null) {
333 try {
334 if(mRealWm instanceof IWindowSession.Stub) {
335 mRealWm.updateInputChannel(state.mInputChannelToken, state.mDisplayId,
336 new SurfaceControl(sc, "WindowlessWindowManager.relayout"),
337 attrs.flags, attrs.privateFlags, state.mInputRegion);
338 } else {
339 mRealWm.updateInputChannel(state.mInputChannelToken, state.mDisplayId, sc,
340 attrs.flags, attrs.privateFlags, state.mInputRegion);
341 }
342 } catch (RemoteException e) {
343 Log.e(TAG, "Failed to update surface input channel: ", e);
344 }
345 }
346
347 if (outInsetsState != null && mInsetsState != null) {
348 outInsetsState.set(mInsetsState);
349 }
350
351 return 0;
352 }
353
354 @Override
355 public void relayoutAsync(IWindow window, WindowManager.LayoutParams inAttrs,
356 int requestedWidth, int requestedHeight, int viewFlags, int flags, int seq,
357 int lastSyncSeqId) {
358 relayout(window, inAttrs, requestedWidth, requestedHeight, viewFlags, flags, seq,
359 lastSyncSeqId, null /* outFrames */, null /* outMergedConfiguration */,
360 null /* outSurfaceControl */, null /* outInsetsState */,
361 null /* outActiveControls */, null /* outSyncSeqIdBundle */);
362 }
363
364 @Override
365 public void prepareToReplaceWindows(android.os.IBinder appToken, boolean childrenOnly) {
366 }
367
368 @Override
369 public boolean outOfMemory(android.view.IWindow window) {
370 return false;
371 }
372
373 @Override
374 public void setInsets(android.view.IWindow window, int touchableInsets,
375 android.graphics.Rect contentInsets, android.graphics.Rect visibleInsets,
376 android.graphics.Region touchableRegion) {
377 setTouchRegion(window.asBinder(), touchableRegion);
378 }
379
380 @Override
381 public void clearTouchableRegion(android.view.IWindow window) {
382 setTouchRegion(window.asBinder(), null);
383 }
384
385 @Override
386 public void finishDrawing(android.view.IWindow window,
387 android.view.SurfaceControl.Transaction postDrawTransaction, int seqId) {
388 synchronized (this) {
389 final ResizeCompleteCallback c =
390 mResizeCompletionForWindow.get(window.asBinder());
391 if (c == null) {
392 // No one wanted the callback, but it wasn't necessarily unexpected.
393 postDrawTransaction.apply();
394 return;
395 }
396 c.finished(postDrawTransaction);
397 mResizeCompletionForWindow.remove(window.asBinder());
398 }
399 }
400
401 @Override
402 public void setInTouchMode(boolean showFocus) {
403 }
404
405 @Override
406 public boolean getInTouchMode() {
407 return false;
408 }
409
410 @Override
411 public boolean performHapticFeedback(int effectId, boolean always) {
412 return false;
413 }
414
415 @Override
416 public android.os.IBinder performDrag(android.view.IWindow window, int flags,
417 android.view.SurfaceControl surface, int touchSource, float touchX, float touchY,
418 float thumbCenterX, float thumbCenterY, android.content.ClipData data) {
419 return null;
420 }
421
422 @Override
423 public void reportDropResult(android.view.IWindow window, boolean consumed) {
424 }
425
426 @Override
427 public void cancelDragAndDrop(android.os.IBinder dragToken, boolean skipAnimation) {
428 }
429
430 @Override
431 public void dragRecipientEntered(android.view.IWindow window) {
432 }
433
434 @Override
435 public void dragRecipientExited(android.view.IWindow window) {
436 }
437
438 @Override
439 public void setWallpaperPosition(android.os.IBinder windowToken, float x, float y,
440 float xstep, float ystep) {
441 }
442
443 @Override
444 public void setWallpaperZoomOut(android.os.IBinder windowToken, float zoom) {
445 }
446
447 @Override
448 public void setShouldZoomOutWallpaper(android.os.IBinder windowToken, boolean shouldZoom) {
449 }
450
451 @Override
452 public void wallpaperOffsetsComplete(android.os.IBinder window) {
453 }
454
455 @Override
456 public void setWallpaperDisplayOffset(android.os.IBinder windowToken, int x, int y) {
457 }
458
459 @Override
460 public android.os.Bundle sendWallpaperCommand(android.os.IBinder window,
461 java.lang.String action, int x, int y, int z, android.os.Bundle extras, boolean sync) {
462 return null;
463 }
464
465 @Override
466 public void wallpaperCommandComplete(android.os.IBinder window, android.os.Bundle result) {
467 }
468
469 @Override
470 public void onRectangleOnScreenRequested(android.os.IBinder token,
471 android.graphics.Rect rectangle) {
472 }
473
474 @Override
475 public android.view.IWindowId getWindowId(android.os.IBinder window) {
476 return null;
477 }
478
479 @Override
480 public void pokeDrawLock(android.os.IBinder window) {
481 }
482
483 @Override
484 public boolean startMovingTask(android.view.IWindow window, float startX, float startY) {
485 return false;
486 }
487
488 @Override
489 public void finishMovingTask(android.view.IWindow window) {
490 }
491
492 @Override
493 public void updatePointerIcon(android.view.IWindow window) {
494 }
495
496 @Override
497 public void updateTapExcludeRegion(android.view.IWindow window,
498 android.graphics.Region region) {
499 }
500
501 @Override
502 public void updateRequestedVisibilities(IWindow window, InsetsVisibilities visibilities) {
503 }
504
505 @Override
506 public void reportSystemGestureExclusionChanged(android.view.IWindow window,
507 List<Rect> exclusionRects) {
508 }
509
510 @Override
511 public void reportKeepClearAreasChanged(android.view.IWindow window, List<Rect> restrictedRects,
512 List<Rect> unrestrictedRects) {
513 }
514
515 @Override
516 public void grantInputChannel(int displayId, SurfaceControl surface, IWindow window,
517 IBinder hostInputToken, int flags, int privateFlags, int type, IBinder focusGrantToken,
518 String inputHandleName, InputChannel outInputChannel) {
519 }
520
521 @Override
522 public void updateInputChannel(IBinder channelToken, int displayId, SurfaceControl surface,
523 int flags, int privateFlags, Region region) {
524 }
525
526 @Override
527 public android.os.IBinder asBinder() {
528 return null;
529 }
530
531 @Override
532 public void grantEmbeddedWindowFocus(IWindow callingWindow, IBinder targetInputToken,
533 boolean grantFocus) {
534 }
535
536 @Override
537 public void generateDisplayHash(IWindow window, Rect boundsInWindow, String hashAlgorithm,
538 RemoteCallback callback) {
539 }
540
541 @Override
542 public void setOnBackInvokedCallbackInfo(IWindow iWindow,
543 OnBackInvokedCallbackInfo callbackInfo) throws RemoteException { }
544
545 @Override
546 public boolean dropForAccessibility(IWindow window, int x, int y) {
547 return false;
548 }
549
550 public void setInsetsState(InsetsState state) {
551 mInsetsState = state;
552 for (State s : mStateForWindow.values()) {
553 try {
554 mTmpFrames.frame.set(0, 0, s.mParams.width, s.mParams.height);
555 mTmpFrames.displayFrame.set(mTmpFrames.frame);
556 mTmpConfig.setConfiguration(mConfiguration, mConfiguration);
557 s.mClient.resized(mTmpFrames, false /* reportDraw */, mTmpConfig, state,
558 false /* forceLayout */, false /* alwaysConsumeSystemBars */, s.mDisplayId,
559 Integer.MAX_VALUE, RESIZE_MODE_INVALID);
560 } catch (RemoteException e) {
561 // Too bad
562 }
563 }
564 }
565
566 @Override
567 public boolean cancelDraw(IWindow window) {
568 return false;
569 }
570 }