Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/common/DisplayLayout.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.content.res.Configuration.ORIENTATION_LANDSCAPE;
20 import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
21 import static android.content.res.Configuration.UI_MODE_TYPE_CAR;
22 import static android.content.res.Configuration.UI_MODE_TYPE_MASK;
23 import static android.os.Process.SYSTEM_UID;
24 import static android.provider.Settings.Global.DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS;
25 import static android.util.RotationUtils.rotateBounds;
26 import static android.util.RotationUtils.rotateInsets;
27 import static android.view.Display.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
28 import static android.view.InsetsState.ITYPE_EXTRA_NAVIGATION_BAR;
29 import static android.view.Surface.ROTATION_0;
30 import static android.view.Surface.ROTATION_270;
31 import static android.view.Surface.ROTATION_90;
32
33 import android.annotation.IntDef;
34 import android.annotation.NonNull;
35 import android.annotation.Nullable;
36 import android.content.ContentResolver;
37 import android.content.Context;
38 import android.content.res.Resources;
39 import android.graphics.Insets;
40 import android.graphics.Rect;
41 import android.os.SystemProperties;
42 import android.provider.Settings;
43 import android.util.DisplayMetrics;
44 import android.util.Size;
45 import android.view.Display;
46 import android.view.DisplayCutout;
47 import android.view.DisplayInfo;
48 import android.view.Gravity;
49 import android.view.InsetsSource;
50 import android.view.InsetsState;
51 import android.view.Surface;
52
53 import androidx.annotation.VisibleForTesting;
54
55 import com.android.internal.R;
56 import com.android.internal.policy.SystemBarUtils;
57
58 import java.lang.annotation.Retention;
59 import java.lang.annotation.RetentionPolicy;
60 import java.util.Objects;
61
62 /**
63 * Contains information about the layout-properties of a display. This refers to internal layout
64 * like insets/cutout/rotation. In general, this can be thought of as the shell analog to
65 * DisplayPolicy.
66 */
67 public class DisplayLayout {
68 @IntDef(prefix = { "NAV_BAR_" }, value = {
69 NAV_BAR_LEFT,
70 NAV_BAR_RIGHT,
71 NAV_BAR_BOTTOM,
72 })
73 @Retention(RetentionPolicy.SOURCE)
74 public @interface NavBarPosition {}
75
76 // Navigation bar position values
77 public static final int NAV_BAR_LEFT = 1 << 0;
78 public static final int NAV_BAR_RIGHT = 1 << 1;
79 public static final int NAV_BAR_BOTTOM = 1 << 2;
80
81 private int mUiMode;
82 private int mWidth;
83 private int mHeight;
84 private DisplayCutout mCutout;
85 private int mRotation;
86 private int mDensityDpi;
87 private final Rect mNonDecorInsets = new Rect();
88 private final Rect mStableInsets = new Rect();
89 private boolean mHasNavigationBar = false;
90 private boolean mHasStatusBar = false;
91 private int mNavBarFrameHeight = 0;
92 private boolean mAllowSeamlessRotationDespiteNavBarMoving = false;
93 private boolean mNavigationBarCanMove = false;
94 private boolean mReverseDefaultRotation = false;
95 private InsetsState mInsetsState = new InsetsState();
96
97 /**
98 * Different from {@link #equals(Object)}, this method compares the basic geometry properties
99 * of two {@link DisplayLayout} objects including width, height, rotation, density, cutout and
100 * insets.
101 * @return {@code true} if the given {@link DisplayLayout} is identical geometry wise.
102 */
103 public boolean isSameGeometry(@NonNull DisplayLayout other) {
104 return mWidth == other.mWidth
105 && mHeight == other.mHeight
106 && mRotation == other.mRotation
107 && mDensityDpi == other.mDensityDpi
108 && Objects.equals(mCutout, other.mCutout)
109 && Objects.equals(mStableInsets, other.mStableInsets);
110 }
111
112 @Override
113 public boolean equals(Object o) {
114 if (this == o) return true;
115 if (!(o instanceof DisplayLayout)) return false;
116 final DisplayLayout other = (DisplayLayout) o;
117 return mUiMode == other.mUiMode
118 && mWidth == other.mWidth
119 && mHeight == other.mHeight
120 && Objects.equals(mCutout, other.mCutout)
121 && mRotation == other.mRotation
122 && mDensityDpi == other.mDensityDpi
123 && Objects.equals(mNonDecorInsets, other.mNonDecorInsets)
124 && Objects.equals(mStableInsets, other.mStableInsets)
125 && mHasNavigationBar == other.mHasNavigationBar
126 && mHasStatusBar == other.mHasStatusBar
127 && mAllowSeamlessRotationDespiteNavBarMoving
128 == other.mAllowSeamlessRotationDespiteNavBarMoving
129 && mNavigationBarCanMove == other.mNavigationBarCanMove
130 && mReverseDefaultRotation == other.mReverseDefaultRotation
131 && mNavBarFrameHeight == other.mNavBarFrameHeight
132 && Objects.equals(mInsetsState, other.mInsetsState);
133 }
134
135 @Override
136 public int hashCode() {
137 return Objects.hash(mUiMode, mWidth, mHeight, mCutout, mRotation, mDensityDpi,
138 mNonDecorInsets, mStableInsets, mHasNavigationBar, mHasStatusBar,
139 mNavBarFrameHeight, mAllowSeamlessRotationDespiteNavBarMoving,
140 mNavigationBarCanMove, mReverseDefaultRotation, mInsetsState);
141 }
142
143 /**
144 * Create empty layout.
145 */
146 public DisplayLayout() {
147 }
148
149 /**
150 * Construct a custom display layout using a DisplayInfo.
151 * @param info
152 * @param res
153 */
154 public DisplayLayout(DisplayInfo info, Resources res, boolean hasNavigationBar,
155 boolean hasStatusBar) {
156 init(info, res, hasNavigationBar, hasStatusBar);
157 }
158
159 /**
160 * Construct a display layout based on a live display.
161 * @param context Used for resources.
162 */
163 public DisplayLayout(@NonNull Context context, @NonNull Display rawDisplay) {
164 final int displayId = rawDisplay.getDisplayId();
165 DisplayInfo info = new DisplayInfo();
166 rawDisplay.getDisplayInfo(info);
167 init(info, context.getResources(), hasNavigationBar(info, context, displayId),
168 hasStatusBar(displayId));
169 }
170
171 public DisplayLayout(DisplayLayout dl) {
172 set(dl);
173 }
174
175 /** sets this DisplayLayout to a copy of another on. */
176 public void set(DisplayLayout dl) {
177 mUiMode = dl.mUiMode;
178 mWidth = dl.mWidth;
179 mHeight = dl.mHeight;
180 mCutout = dl.mCutout;
181 mRotation = dl.mRotation;
182 mDensityDpi = dl.mDensityDpi;
183 mHasNavigationBar = dl.mHasNavigationBar;
184 mHasStatusBar = dl.mHasStatusBar;
185 mAllowSeamlessRotationDespiteNavBarMoving = dl.mAllowSeamlessRotationDespiteNavBarMoving;
186 mNavigationBarCanMove = dl.mNavigationBarCanMove;
187 mReverseDefaultRotation = dl.mReverseDefaultRotation;
188 mNavBarFrameHeight = dl.mNavBarFrameHeight;
189 mNonDecorInsets.set(dl.mNonDecorInsets);
190 mStableInsets.set(dl.mStableInsets);
191 mInsetsState.set(dl.mInsetsState, true /* copySources */);
192 }
193
194 private void init(DisplayInfo info, Resources res, boolean hasNavigationBar,
195 boolean hasStatusBar) {
196 mUiMode = res.getConfiguration().uiMode;
197 mWidth = info.logicalWidth;
198 mHeight = info.logicalHeight;
199 mRotation = info.rotation;
200 mCutout = info.displayCutout;
201 mDensityDpi = info.logicalDensityDpi;
202 mHasNavigationBar = hasNavigationBar;
203 mHasStatusBar = hasStatusBar;
204 mAllowSeamlessRotationDespiteNavBarMoving = res.getBoolean(
205 R.bool.config_allowSeamlessRotationDespiteNavBarMoving);
206 mNavigationBarCanMove = res.getBoolean(R.bool.config_navBarCanMove);
207 mReverseDefaultRotation = res.getBoolean(R.bool.config_reverseDefaultRotation);
208 recalcInsets(res);
209 }
210
211 /**
212 * Updates the current insets.
213 */
214 public void setInsets(Resources res, InsetsState state) {
215 mInsetsState = state;
216 recalcInsets(res);
217 }
218
219 @VisibleForTesting
220 void recalcInsets(Resources res) {
221 computeNonDecorInsets(res, mRotation, mWidth, mHeight, mCutout, mInsetsState, mUiMode,
222 mNonDecorInsets, mHasNavigationBar);
223 mStableInsets.set(mNonDecorInsets);
224 if (mHasStatusBar) {
225 convertNonDecorInsetsToStableInsets(res, mStableInsets, mCutout, mHasStatusBar);
226 }
227 mNavBarFrameHeight = getNavigationBarFrameHeight(res, mWidth > mHeight);
228 }
229
230 /**
231 * Apply a rotation to this layout and its parameters.
232 * @param res
233 * @param targetRotation
234 */
235 public void rotateTo(Resources res, @Surface.Rotation int targetRotation) {
236 final int rotationDelta = (targetRotation - mRotation + 4) % 4;
237 final boolean changeOrient = (rotationDelta % 2) != 0;
238
239 final int origWidth = mWidth;
240 final int origHeight = mHeight;
241
242 mRotation = targetRotation;
243 if (changeOrient) {
244 mWidth = origHeight;
245 mHeight = origWidth;
246 }
247
248 if (mCutout != null && !mCutout.isEmpty()) {
249 mCutout = calculateDisplayCutoutForRotation(mCutout, rotationDelta, origWidth,
250 origHeight);
251 }
252
253 recalcInsets(res);
254 }
255
256 /** Get this layout's non-decor insets. */
257 public Rect nonDecorInsets() {
258 return mNonDecorInsets;
259 }
260
261 /** Get this layout's stable insets. */
262 public Rect stableInsets() {
263 return mStableInsets;
264 }
265
266 /** Get this layout's width. */
267 public int width() {
268 return mWidth;
269 }
270
271 /** Get this layout's height. */
272 public int height() {
273 return mHeight;
274 }
275
276 /** Get this layout's display rotation. */
277 public int rotation() {
278 return mRotation;
279 }
280
281 /** Get this layout's display density. */
282 public int densityDpi() {
283 return mDensityDpi;
284 }
285
286 /** Get the density scale for the display. */
287 public float density() {
288 return mDensityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
289 }
290
291 /** Get whether this layout is landscape. */
292 public boolean isLandscape() {
293 return mWidth > mHeight;
294 }
295
296 /** Get the navbar frame (or window) height (used by ime). */
297 public int navBarFrameHeight() {
298 return mNavBarFrameHeight;
299 }
300
301 /** @return whether we can seamlessly rotate even if nav-bar can change sides. */
302 public boolean allowSeamlessRotationDespiteNavBarMoving() {
303 return mAllowSeamlessRotationDespiteNavBarMoving;
304 }
305
306 /** @return whether the navigation bar will change sides during rotation. */
307 public boolean navigationBarCanMove() {
308 return mNavigationBarCanMove;
309 }
310
311 /** @return the rotation that would make the physical display "upside down". */
312 public int getUpsideDownRotation() {
313 boolean displayHardwareIsLandscape = mWidth > mHeight;
314 if ((mRotation % 2) != 0) {
315 displayHardwareIsLandscape = !displayHardwareIsLandscape;
316 }
317 if (displayHardwareIsLandscape) {
318 return mReverseDefaultRotation ? Surface.ROTATION_270 : Surface.ROTATION_90;
319 }
320 return Surface.ROTATION_180;
321 }
322
323 /** Gets the orientation of this layout */
324 public int getOrientation() {
325 return (mWidth > mHeight) ? ORIENTATION_LANDSCAPE : ORIENTATION_PORTRAIT;
326 }
327
328 /** Gets the calculated stable-bounds for this layout */
329 public void getStableBounds(Rect outBounds) {
330 outBounds.set(0, 0, mWidth, mHeight);
331 outBounds.inset(mStableInsets);
332 }
333
334 /**
335 * Gets navigation bar position for this layout
336 * @return Navigation bar position for this layout.
337 */
338 public @NavBarPosition int getNavigationBarPosition(Resources res) {
339 return navigationBarPosition(res, mWidth, mHeight, mRotation);
340 }
341
342 /** @return {@link DisplayCutout} instance. */
343 @Nullable
344 public DisplayCutout getDisplayCutout() {
345 return mCutout;
346 }
347
348 /**
349 * Calculates the stable insets if we already have the non-decor insets.
350 */
351 private void convertNonDecorInsetsToStableInsets(Resources res, Rect inOutInsets,
352 DisplayCutout cutout, boolean hasStatusBar) {
353 if (!hasStatusBar) {
354 return;
355 }
356 int statusBarHeight = SystemBarUtils.getStatusBarHeight(res, cutout);
357 inOutInsets.top = Math.max(inOutInsets.top, statusBarHeight);
358 }
359
360 /**
361 * Calculates the insets for the areas that could never be removed in Honeycomb, i.e. system
362 * bar or button bar.
363 *
364 * @param displayRotation the current display rotation
365 * @param displayWidth the current display width
366 * @param displayHeight the current display height
367 * @param displayCutout the current display cutout
368 * @param outInsets the insets to return
369 */
370 static void computeNonDecorInsets(Resources res, int displayRotation, int displayWidth,
371 int displayHeight, DisplayCutout displayCutout, InsetsState insetsState, int uiMode,
372 Rect outInsets, boolean hasNavigationBar) {
373 outInsets.setEmpty();
374
375 // Only navigation bar
376 if (hasNavigationBar) {
377 final InsetsSource extraNavBar = insetsState.getSource(ITYPE_EXTRA_NAVIGATION_BAR);
378 final boolean hasExtraNav = extraNavBar != null && extraNavBar.isVisible();
379 int position = navigationBarPosition(res, displayWidth, displayHeight, displayRotation);
380 int navBarSize =
381 getNavigationBarSize(res, position, displayWidth > displayHeight, uiMode);
382 if (position == NAV_BAR_BOTTOM) {
383 outInsets.bottom = hasExtraNav
384 ? Math.max(navBarSize, extraNavBar.getFrame().height())
385 : navBarSize;
386 } else if (position == NAV_BAR_RIGHT) {
387 outInsets.right = hasExtraNav
388 ? Math.max(navBarSize, extraNavBar.getFrame().width())
389 : navBarSize;
390 } else if (position == NAV_BAR_LEFT) {
391 outInsets.left = hasExtraNav
392 ? Math.max(navBarSize, extraNavBar.getFrame().width())
393 : navBarSize;
394 }
395 }
396
397 if (displayCutout != null) {
398 outInsets.left += displayCutout.getSafeInsetLeft();
399 outInsets.top += displayCutout.getSafeInsetTop();
400 outInsets.right += displayCutout.getSafeInsetRight();
401 outInsets.bottom += displayCutout.getSafeInsetBottom();
402 }
403 }
404
405 /** Calculate the DisplayCutout for a particular display size/rotation. */
406 public static DisplayCutout calculateDisplayCutoutForRotation(
407 DisplayCutout cutout, int rotation, int displayWidth, int displayHeight) {
408 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
409 return null;
410 }
411 if (rotation == ROTATION_0) {
412 return computeSafeInsets(cutout, displayWidth, displayHeight);
413 }
414 final Insets waterfallInsets = rotateInsets(cutout.getWaterfallInsets(), rotation);
415 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
416 Rect[] cutoutRects = cutout.getBoundingRectsAll();
417 final Rect[] newBounds = new Rect[cutoutRects.length];
418 final Rect displayBounds = new Rect(0, 0, displayWidth, displayHeight);
419 for (int i = 0; i < cutoutRects.length; ++i) {
420 final Rect rect = new Rect(cutoutRects[i]);
421 if (!rect.isEmpty()) {
422 rotateBounds(rect, displayBounds, rotation);
423 }
424 newBounds[getBoundIndexFromRotation(i, rotation)] = rect;
425 }
426 final DisplayCutout.CutoutPathParserInfo info = cutout.getCutoutPathParserInfo();
427 final DisplayCutout.CutoutPathParserInfo newInfo = new DisplayCutout.CutoutPathParserInfo(
428 info.getDisplayWidth(), info.getDisplayHeight(), info.getPhysicalDisplayWidth(),
429 info.getPhysicalDisplayHeight(), info.getDensity(), info.getCutoutSpec(), rotation,
430 info.getScale(), info.getPhysicalPixelDisplaySizeRatio());
431 return computeSafeInsets(
432 DisplayCutout.constructDisplayCutout(newBounds, waterfallInsets, newInfo),
433 rotated ? displayHeight : displayWidth,
434 rotated ? displayWidth : displayHeight);
435 }
436
437 private static int getBoundIndexFromRotation(int index, int rotation) {
438 return (index - rotation) < 0
439 ? index - rotation + DisplayCutout.BOUNDS_POSITION_LENGTH
440 : index - rotation;
441 }
442
443 /** Calculate safe insets. */
444 public static DisplayCutout computeSafeInsets(DisplayCutout inner,
445 int displayWidth, int displayHeight) {
446 if (inner == DisplayCutout.NO_CUTOUT) {
447 return null;
448 }
449
450 final Size displaySize = new Size(displayWidth, displayHeight);
451 final Rect safeInsets = computeSafeInsets(displaySize, inner);
452 return inner.replaceSafeInsets(safeInsets);
453 }
454
455 private static Rect computeSafeInsets(
456 Size displaySize, DisplayCutout cutout) {
457 if (displaySize.getWidth() == displaySize.getHeight()) {
458 throw new UnsupportedOperationException("not implemented: display=" + displaySize
459 + " cutout=" + cutout);
460 }
461
462 int leftInset = Math.max(cutout.getWaterfallInsets().left,
463 findCutoutInsetForSide(displaySize, cutout.getBoundingRectLeft(), Gravity.LEFT));
464 int topInset = Math.max(cutout.getWaterfallInsets().top,
465 findCutoutInsetForSide(displaySize, cutout.getBoundingRectTop(), Gravity.TOP));
466 int rightInset = Math.max(cutout.getWaterfallInsets().right,
467 findCutoutInsetForSide(displaySize, cutout.getBoundingRectRight(), Gravity.RIGHT));
468 int bottomInset = Math.max(cutout.getWaterfallInsets().bottom,
469 findCutoutInsetForSide(displaySize, cutout.getBoundingRectBottom(),
470 Gravity.BOTTOM));
471
472 return new Rect(leftInset, topInset, rightInset, bottomInset);
473 }
474
475 private static int findCutoutInsetForSide(Size display, Rect boundingRect, int gravity) {
476 if (boundingRect.isEmpty()) {
477 return 0;
478 }
479
480 int inset = 0;
481 switch (gravity) {
482 case Gravity.TOP:
483 return Math.max(inset, boundingRect.bottom);
484 case Gravity.BOTTOM:
485 return Math.max(inset, display.getHeight() - boundingRect.top);
486 case Gravity.LEFT:
487 return Math.max(inset, boundingRect.right);
488 case Gravity.RIGHT:
489 return Math.max(inset, display.getWidth() - boundingRect.left);
490 default:
491 throw new IllegalArgumentException("unknown gravity: " + gravity);
492 }
493 }
494
495 static boolean hasNavigationBar(DisplayInfo info, Context context, int displayId) {
496 if (displayId == Display.DEFAULT_DISPLAY) {
497 // Allow a system property to override this. Used by the emulator.
498 final String navBarOverride = SystemProperties.get("qemu.hw.mainkeys");
499 if ("1".equals(navBarOverride)) {
500 return false;
501 } else if ("0".equals(navBarOverride)) {
502 return true;
503 }
504 return context.getResources().getBoolean(R.bool.config_showNavigationBar);
505 } else {
506 boolean isUntrustedVirtualDisplay = info.type == Display.TYPE_VIRTUAL
507 && info.ownerUid != SYSTEM_UID;
508 final ContentResolver resolver = context.getContentResolver();
509 boolean forceDesktopOnExternal = Settings.Global.getInt(resolver,
510 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, 0) != 0;
511
512 return ((info.flags & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
513 || (forceDesktopOnExternal && !isUntrustedVirtualDisplay));
514 // TODO(b/142569966): make sure VR2D and DisplayWindowSettings are moved here somehow.
515 }
516 }
517
518 static boolean hasStatusBar(int displayId) {
519 return displayId == Display.DEFAULT_DISPLAY;
520 }
521
522 /** Retrieve navigation bar position from resources based on rotation and size. */
523 public static @NavBarPosition int navigationBarPosition(Resources res, int displayWidth,
524 int displayHeight, int rotation) {
525 boolean navBarCanMove = displayWidth != displayHeight && res.getBoolean(
526 com.android.internal.R.bool.config_navBarCanMove);
527 if (navBarCanMove && displayWidth > displayHeight) {
528 if (rotation == Surface.ROTATION_90) {
529 return NAV_BAR_RIGHT;
530 } else {
531 return NAV_BAR_LEFT;
532 }
533 }
534 return NAV_BAR_BOTTOM;
535 }
536
537 /** Retrieve navigation bar size from resources based on side/orientation/ui-mode */
538 public static int getNavigationBarSize(Resources res, int navBarSide, boolean landscape,
539 int uiMode) {
540 final boolean carMode = (uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_CAR;
541 if (carMode) {
542 if (navBarSide == NAV_BAR_BOTTOM) {
543 return res.getDimensionPixelSize(landscape
544 ? R.dimen.navigation_bar_height_landscape_car_mode
545 : R.dimen.navigation_bar_height_car_mode);
546 } else {
547 return res.getDimensionPixelSize(R.dimen.navigation_bar_width_car_mode);
548 }
549
550 } else {
551 if (navBarSide == NAV_BAR_BOTTOM) {
552 return res.getDimensionPixelSize(landscape
553 ? R.dimen.navigation_bar_height_landscape
554 : R.dimen.navigation_bar_height);
555 } else {
556 return res.getDimensionPixelSize(R.dimen.navigation_bar_width);
557 }
558 }
559 }
560
561 /** @see com.android.server.wm.DisplayPolicy#getNavigationBarFrameHeight */
562 public static int getNavigationBarFrameHeight(Resources res, boolean landscape) {
563 return res.getDimensionPixelSize(landscape
564 ? R.dimen.navigation_bar_frame_height_landscape
565 : R.dimen.navigation_bar_frame_height);
566 }
567 }