Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/compatui/CompatUIWindowManagerAbstract.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.compatui;
18
19 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
20 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
21 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
22 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY;
23 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
24
25 import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
26 import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
27 import static com.android.internal.annotations.VisibleForTesting.Visibility.PROTECTED;
28
29 import android.annotation.Nullable;
30 import android.app.TaskInfo;
31 import android.content.Context;
32 import android.content.res.Configuration;
33 import android.graphics.PixelFormat;
34 import android.graphics.Rect;
35 import android.os.Binder;
36 import android.util.Log;
37 import android.view.IWindow;
38 import android.view.SurfaceControl;
39 import android.view.SurfaceControlViewHost;
40 import android.view.SurfaceSession;
41 import android.view.View;
42 import android.view.WindowManager;
43 import android.view.WindowlessWindowManager;
44
45 import com.android.internal.annotations.VisibleForTesting;
46 import com.android.wm.shell.ShellTaskOrganizer;
47 import com.android.wm.shell.common.DisplayLayout;
48 import com.android.wm.shell.common.SyncTransactionQueue;
49
50 /**
51 * A superclass for all Compat UI {@link WindowlessWindowManager}s that holds shared logic and
52 * exposes general API for {@link CompatUIController}.
53 *
54 * <p>Holds view hierarchy of a root surface and helps to inflate and manage layout.
55 */
56 public abstract class CompatUIWindowManagerAbstract extends WindowlessWindowManager {
57
58 protected final int mTaskId;
59 protected Context mContext;
60
61 private final SyncTransactionQueue mSyncQueue;
62 private final int mDisplayId;
63 private Configuration mTaskConfig;
64 private ShellTaskOrganizer.TaskListener mTaskListener;
65 private DisplayLayout mDisplayLayout;
66 private final Rect mStableBounds;
67
68 /**
69 * Utility class for adding and releasing a View hierarchy for this {@link
70 * WindowlessWindowManager} to {@code mLeash}.
71 */
72 @Nullable
73 protected SurfaceControlViewHost mViewHost;
74
75 /**
76 * A surface leash to position the layout relative to the task, since we can't set position for
77 * the {@code mViewHost} directly.
78 */
79 @Nullable
80 protected SurfaceControl mLeash;
81
82 protected CompatUIWindowManagerAbstract(Context context, TaskInfo taskInfo,
83 SyncTransactionQueue syncQueue, ShellTaskOrganizer.TaskListener taskListener,
84 DisplayLayout displayLayout) {
85 super(taskInfo.configuration, null /* rootSurface */, null /* hostInputToken */);
86 mContext = context;
87 mSyncQueue = syncQueue;
88 mTaskConfig = taskInfo.configuration;
89 mDisplayId = mContext.getDisplayId();
90 mTaskId = taskInfo.taskId;
91 mTaskListener = taskListener;
92 mDisplayLayout = displayLayout;
93 mStableBounds = new Rect();
94 mDisplayLayout.getStableBounds(mStableBounds);
95 }
96
97 /**
98 * Returns the z-order of this window which will be passed to the {@link SurfaceControl} once
99 * {@link #attachToParentSurface} is called.
100 *
101 * <p>See {@link SurfaceControl.Transaction#setLayer}.
102 */
103 protected abstract int getZOrder();
104
105 /** Returns the layout of this window manager. */
106 protected abstract @Nullable View getLayout();
107
108 /**
109 * Inflates and inits the layout of this window manager on to the root surface if both {@code
110 * canShow} and {@link #eligibleToShowLayout} are true.
111 *
112 * <p>Doesn't do anything if layout is not eligible to be shown.
113 *
114 * @param canShow whether the layout is allowed to be shown by the parent controller.
115 * @return whether the layout is eligible to be shown.
116 */
117 @VisibleForTesting(visibility = PROTECTED)
118 public boolean createLayout(boolean canShow) {
119 if (!eligibleToShowLayout()) {
120 return false;
121 }
122 if (!canShow || getLayout() != null) {
123 // Wait until layout should be visible, or layout was already created.
124 return true;
125 }
126
127 if (mViewHost != null) {
128 throw new IllegalStateException(
129 "A UI has already been created with this window manager.");
130 }
131
132 // Construction extracted into separate methods to allow injection for tests.
133 mViewHost = createSurfaceViewHost();
134 mViewHost.setView(createLayout(), getWindowLayoutParams());
135
136 updateSurfacePosition();
137
138 return true;
139 }
140
141 /** Inflates and inits the layout of this window manager. */
142 protected abstract View createLayout();
143
144 protected abstract void removeLayout();
145
146 /**
147 * Whether the layout is eligible to be shown according to the internal state of the subclass.
148 */
149 protected abstract boolean eligibleToShowLayout();
150
151 @Override
152 public void setConfiguration(Configuration configuration) {
153 super.setConfiguration(configuration);
154 mContext = mContext.createConfigurationContext(configuration);
155 }
156
157 @Override
158 protected void attachToParentSurface(IWindow window, SurfaceControl.Builder b) {
159 String className = getClass().getSimpleName();
160 final SurfaceControl.Builder builder = new SurfaceControl.Builder(new SurfaceSession())
161 .setContainerLayer()
162 .setName(className + "Leash")
163 .setHidden(false)
164 .setCallsite(className + "#attachToParentSurface");
165 attachToParentSurface(builder);
166 mLeash = builder.build();
167 b.setParent(mLeash);
168
169 initSurface(mLeash);
170 }
171
172 /** Inits the z-order of the surface. */
173 private void initSurface(SurfaceControl leash) {
174 final int z = getZOrder();
175 mSyncQueue.runInSync(t -> {
176 if (leash == null || !leash.isValid()) {
177 Log.w(getTag(), "The leash has been released.");
178 return;
179 }
180 t.setLayer(leash, z);
181 });
182 }
183
184 /**
185 * Called when compat info changed.
186 *
187 * <p>The window manager is released if the layout is no longer eligible to be shown.
188 *
189 * @param canShow whether the layout is allowed to be shown by the parent controller.
190 * @return whether the layout is eligible to be shown.
191 */
192 @VisibleForTesting(visibility = PROTECTED)
193 public boolean updateCompatInfo(TaskInfo taskInfo,
194 ShellTaskOrganizer.TaskListener taskListener, boolean canShow) {
195 final Configuration prevTaskConfig = mTaskConfig;
196 final ShellTaskOrganizer.TaskListener prevTaskListener = mTaskListener;
197 mTaskConfig = taskInfo.configuration;
198 mTaskListener = taskListener;
199
200 // Update configuration.
201 setConfiguration(mTaskConfig);
202
203 if (!eligibleToShowLayout()) {
204 release();
205 return false;
206 }
207
208 View layout = getLayout();
209 if (layout == null || prevTaskListener != taskListener) {
210 // Layout wasn't created yet or TaskListener changed, recreate the layout for new
211 // surface parent.
212 release();
213 return createLayout(canShow);
214 }
215
216 boolean boundsUpdated = !mTaskConfig.windowConfiguration.getBounds().equals(
217 prevTaskConfig.windowConfiguration.getBounds());
218 boolean layoutDirectionUpdated =
219 mTaskConfig.getLayoutDirection() != prevTaskConfig.getLayoutDirection();
220 if (boundsUpdated || layoutDirectionUpdated) {
221 onParentBoundsChanged();
222 }
223
224 if (layout != null && layoutDirectionUpdated) {
225 // Update layout for RTL.
226 layout.setLayoutDirection(mTaskConfig.getLayoutDirection());
227 }
228
229 return true;
230 }
231
232 /**
233 * Updates the visibility of the layout.
234 *
235 * @param canShow whether the layout is allowed to be shown by the parent controller.
236 */
237 @VisibleForTesting(visibility = PACKAGE)
238 public void updateVisibility(boolean canShow) {
239 View layout = getLayout();
240 if (layout == null) {
241 // Layout may not have been created because it was hidden previously.
242 createLayout(canShow);
243 return;
244 }
245
246 final int newVisibility = canShow && eligibleToShowLayout() ? View.VISIBLE : View.GONE;
247 if (layout.getVisibility() != newVisibility) {
248 layout.setVisibility(newVisibility);
249 }
250 }
251
252 /** Called when display layout changed. */
253 @VisibleForTesting(visibility = PACKAGE)
254 public void updateDisplayLayout(DisplayLayout displayLayout) {
255 final Rect prevStableBounds = mStableBounds;
256 final Rect curStableBounds = new Rect();
257 displayLayout.getStableBounds(curStableBounds);
258 mDisplayLayout = displayLayout;
259 if (!prevStableBounds.equals(curStableBounds)) {
260 // mStableBounds should be updated before we call onParentBoundsChanged.
261 mStableBounds.set(curStableBounds);
262 onParentBoundsChanged();
263 }
264 }
265
266 /** Called when the surface is ready to be placed under the task surface. */
267 @VisibleForTesting(visibility = PRIVATE)
268 void attachToParentSurface(SurfaceControl.Builder b) {
269 mTaskListener.attachChildSurfaceToTask(mTaskId, b);
270 }
271
272 public int getDisplayId() {
273 return mDisplayId;
274 }
275
276 public int getTaskId() {
277 return mTaskId;
278 }
279
280 /** Releases the surface control and tears down the view hierarchy. */
281 public void release() {
282 // Hiding before releasing to avoid flickering when transitioning to the Home screen.
283 View layout = getLayout();
284 if (layout != null) {
285 layout.setVisibility(View.GONE);
286 }
287 removeLayout();
288
289 if (mViewHost != null) {
290 mViewHost.release();
291 mViewHost = null;
292 }
293
294 if (mLeash != null) {
295 final SurfaceControl leash = mLeash;
296 mSyncQueue.runInSync(t -> t.remove(leash));
297 mLeash = null;
298 }
299 }
300
301 /** Re-layouts the view host and updates the surface position. */
302 void relayout() {
303 relayout(getWindowLayoutParams());
304 }
305
306 protected void relayout(WindowManager.LayoutParams windowLayoutParams) {
307 if (mViewHost == null) {
308 return;
309 }
310 mViewHost.relayout(windowLayoutParams);
311 updateSurfacePosition();
312 }
313
314 /**
315 * Called following a change in the task bounds, display layout stable bounds, or the layout
316 * direction.
317 */
318 protected void onParentBoundsChanged() {
319 updateSurfacePosition();
320 }
321
322 /**
323 * Updates the position of the surface with respect to the parent bounds.
324 */
325 protected abstract void updateSurfacePosition();
326
327 /**
328 * Updates the position of the surface with respect to the given {@code positionX} and {@code
329 * positionY}.
330 */
331 protected void updateSurfacePosition(int positionX, int positionY) {
332 if (mLeash == null) {
333 return;
334 }
335 mSyncQueue.runInSync(t -> {
336 if (mLeash == null || !mLeash.isValid()) {
337 Log.w(getTag(), "The leash has been released.");
338 return;
339 }
340 t.setPosition(mLeash, positionX, positionY);
341 });
342 }
343
344 protected int getLayoutDirection() {
345 return mContext.getResources().getConfiguration().getLayoutDirection();
346 }
347
348 protected Rect getTaskBounds() {
349 return mTaskConfig.windowConfiguration.getBounds();
350 }
351
352 /** Returns the intersection between the task bounds and the display layout stable bounds. */
353 protected Rect getTaskStableBounds() {
354 final Rect result = new Rect(mStableBounds);
355 result.intersect(getTaskBounds());
356 return result;
357 }
358
359 /** Creates a {@link SurfaceControlViewHost} for this window manager. */
360 @VisibleForTesting(visibility = PRIVATE)
361 public SurfaceControlViewHost createSurfaceViewHost() {
362 return new SurfaceControlViewHost(mContext, mContext.getDisplay(), this);
363 }
364
365 /** Gets the layout params. */
366 protected WindowManager.LayoutParams getWindowLayoutParams() {
367 View layout = getLayout();
368 if (layout == null) {
369 return new WindowManager.LayoutParams();
370 }
371 // Measure how big the hint is since its size depends on the text size.
372 layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
373 return getWindowLayoutParams(layout.getMeasuredWidth(), layout.getMeasuredHeight());
374 }
375
376 /** Gets the layout params given the width and height of the layout. */
377 protected WindowManager.LayoutParams getWindowLayoutParams(int width, int height) {
378 final WindowManager.LayoutParams winParams = new WindowManager.LayoutParams(
379 // Cannot be wrap_content as this determines the actual window size
380 width, height,
381 TYPE_APPLICATION_OVERLAY,
382 FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL,
383 PixelFormat.TRANSLUCENT);
384 winParams.token = new Binder();
385 winParams.setTitle(getClass().getSimpleName() + mTaskId);
386 winParams.privateFlags |= PRIVATE_FLAG_NO_MOVE_ANIMATION | PRIVATE_FLAG_TRUSTED_OVERLAY;
387 return winParams;
388 }
389
390 protected final String getTag() {
391 return getClass().getSimpleName();
392 }
393 }