diff --git a/quickstep/src/com/android/quickstep/util/SystemWindowManagerProxy.java b/quickstep/src/com/android/quickstep/util/SystemWindowManagerProxy.java index 348e4dc7bd..92685115a9 100644 --- a/quickstep/src/com/android/quickstep/util/SystemWindowManagerProxy.java +++ b/quickstep/src/com/android/quickstep/util/SystemWindowManagerProxy.java @@ -20,6 +20,7 @@ import static android.view.Display.DEFAULT_DISPLAY; import android.content.Context; import android.graphics.Rect; import android.util.ArrayMap; +import android.view.DisplayCutout; import android.view.Surface; import android.view.WindowManager; import android.view.WindowMetrics; @@ -74,4 +75,10 @@ public class SystemWindowManagerProxy extends WindowManagerProxy { } return result; } + + @Override + protected DisplayCutout rotateCutout(DisplayCutout original, int startWidth, int startHeight, + int fromRotation, int toRotation) { + return original.getRotated(startWidth, startHeight, fromRotation, toRotation); + } } diff --git a/quickstep/tests/src/com/android/quickstep/util/TaskViewSimulatorTest.java b/quickstep/tests/src/com/android/quickstep/util/TaskViewSimulatorTest.java index b365173663..9fa4b793ae 100644 --- a/quickstep/tests/src/com/android/quickstep/util/TaskViewSimulatorTest.java +++ b/quickstep/tests/src/com/android/quickstep/util/TaskViewSimulatorTest.java @@ -163,8 +163,7 @@ public class TaskViewSimulatorTest { helper.sandboxContext.allow(SystemUiProxy.INSTANCE); int rotation = mDisplaySize.x > mDisplaySize.y ? Surface.ROTATION_90 : Surface.ROTATION_0; - CachedDisplayInfo cdi = - new CachedDisplayInfo(mDisplaySize, rotation, new Rect()); + CachedDisplayInfo cdi = new CachedDisplayInfo(mDisplaySize, rotation); WindowBounds wm = new WindowBounds( new Rect(0, 0, mDisplaySize.x, mDisplaySize.y), mDisplayInsets); @@ -186,7 +185,7 @@ public class TaskViewSimulatorTest { ArrayMap> perDisplayBoundsCache = new ArrayMap<>(); - perDisplayBoundsCache.put(cdi.normalize(), allBounds); + perDisplayBoundsCache.put(cdi.normalize(wmProxy), allBounds); Configuration configuration = new Configuration(); configuration.densityDpi = mDensityDpi; diff --git a/src/com/android/launcher3/util/DisplayController.java b/src/com/android/launcher3/util/DisplayController.java index 1419dc4a6b..ff9521282d 100644 --- a/src/com/android/launcher3/util/DisplayController.java +++ b/src/com/android/launcher3/util/DisplayController.java @@ -362,10 +362,10 @@ public class DisplayController implements ComponentCallbacks, SafeCloseable { WindowManagerProxy wmProxy, Map> perDisplayBoundsCache) { CachedDisplayInfo displayInfo = wmProxy.getDisplayInfo(displayInfoContext); - normalizedDisplayInfo = displayInfo.normalize(); + normalizedDisplayInfo = displayInfo.normalize(wmProxy); rotation = displayInfo.rotation; currentSize = displayInfo.size; - cutout = displayInfo.cutout; + cutout = WindowManagerProxy.getSafeInsets(displayInfo.cutout); Configuration config = displayInfoContext.getResources().getConfiguration(); fontScale = config.fontScale; diff --git a/src/com/android/launcher3/util/window/CachedDisplayInfo.java b/src/com/android/launcher3/util/window/CachedDisplayInfo.java index 23f37aa2b2..c5084ad7eb 100644 --- a/src/com/android/launcher3/util/window/CachedDisplayInfo.java +++ b/src/com/android/launcher3/util/window/CachedDisplayInfo.java @@ -16,13 +16,16 @@ package com.android.launcher3.util.window; import static com.android.launcher3.util.RotationUtils.deltaRotation; -import static com.android.launcher3.util.RotationUtils.rotateRect; import static com.android.launcher3.util.RotationUtils.rotateSize; +import android.graphics.Insets; import android.graphics.Point; -import android.graphics.Rect; +import android.view.DisplayCutout; import android.view.Surface; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import java.util.Objects; /** @@ -30,36 +33,40 @@ import java.util.Objects; */ public class CachedDisplayInfo { + private static final DisplayCutout NO_CUTOUT = + new DisplayCutout(Insets.NONE, null, null, null, null); + public final Point size; public final int rotation; - public final Rect cutout; + @NonNull + public final DisplayCutout cutout; public CachedDisplayInfo() { this(new Point(0, 0), 0); } public CachedDisplayInfo(Point size, int rotation) { - this(size, rotation, new Rect()); + this(size, rotation, NO_CUTOUT); } - public CachedDisplayInfo(Point size, int rotation, Rect cutout) { + public CachedDisplayInfo(Point size, int rotation, @Nullable DisplayCutout cutout) { this.size = size; this.rotation = rotation; - this.cutout = cutout; + this.cutout = cutout == null ? NO_CUTOUT : cutout; } /** * Returns a CachedDisplayInfo where the properties are normalized to {@link Surface#ROTATION_0} */ - public CachedDisplayInfo normalize() { + public CachedDisplayInfo normalize(WindowManagerProxy windowManagerProxy) { if (rotation == Surface.ROTATION_0) { return this; } Point newSize = new Point(size); rotateSize(newSize, deltaRotation(rotation, Surface.ROTATION_0)); - Rect newCutout = new Rect(cutout); - rotateRect(newCutout, deltaRotation(rotation, Surface.ROTATION_0)); + DisplayCutout newCutout = windowManagerProxy.rotateCutout( + cutout, size.x, size.y, rotation, Surface.ROTATION_0); return new CachedDisplayInfo(newSize, Surface.ROTATION_0, newCutout); } @@ -79,11 +86,16 @@ public class CachedDisplayInfo { CachedDisplayInfo that = (CachedDisplayInfo) o; return rotation == that.rotation && Objects.equals(size, that.size) - && Objects.equals(cutout, that.cutout); + && cutout.getSafeInsetLeft() == that.cutout.getSafeInsetLeft() + && cutout.getSafeInsetTop() == that.cutout.getSafeInsetTop() + && cutout.getSafeInsetRight() == that.cutout.getSafeInsetRight() + && cutout.getSafeInsetBottom() == that.cutout.getSafeInsetBottom(); } @Override public int hashCode() { - return Objects.hash(size, rotation, cutout); + return Objects.hash(size, rotation, + cutout.getSafeInsetLeft(), cutout.getSafeInsetTop(), + cutout.getSafeInsetRight(), cutout.getSafeInsetBottom()); } } diff --git a/src/com/android/launcher3/util/window/WindowManagerProxy.java b/src/com/android/launcher3/util/window/WindowManagerProxy.java index 209a3dc779..32f17367e7 100644 --- a/src/com/android/launcher3/util/window/WindowManagerProxy.java +++ b/src/com/android/launcher3/util/window/WindowManagerProxy.java @@ -95,7 +95,7 @@ public class WindowManagerProxy implements ResourceBasedOverride { */ public ArrayMap> estimateInternalDisplayBounds( Context displayInfoContext) { - CachedDisplayInfo info = getDisplayInfo(displayInfoContext).normalize(); + CachedDisplayInfo info = getDisplayInfo(displayInfoContext).normalize(this); List bounds = estimateWindowBounds(displayInfoContext, info); ArrayMap> result = new ArrayMap<>(); result.put(info, bounds); @@ -186,10 +186,9 @@ public class WindowManagerProxy implements ResourceBasedOverride { * Returns a list of possible WindowBounds for the display keyed on the 4 surface rotations */ protected List estimateWindowBounds(Context context, - CachedDisplayInfo displayInfo) { + final CachedDisplayInfo displayInfo) { int densityDpi = context.getResources().getConfiguration().densityDpi; - int rotation = displayInfo.rotation; - Rect safeCutout = displayInfo.cutout; + final int rotation = displayInfo.rotation; int minSize = Math.min(displayInfo.size.x, displayInfo.size.y); int swDp = (int) dpiFromPx(minSize, densityDpi); @@ -247,8 +246,9 @@ public class WindowManagerProxy implements ResourceBasedOverride { statusBarHeight = statusBarHeightLandscape; } - Rect insets = new Rect(safeCutout); - rotateRect(insets, rotationChange); + DisplayCutout rotatedCutout = rotateCutout( + displayInfo.cutout, displayInfo.size.x, displayInfo.size.y, rotation, i); + Rect insets = getSafeInsets(rotatedCutout); insets.top = Math.max(insets.top, statusBarHeight); insets.bottom = Math.max(insets.bottom, navBarHeight); @@ -298,8 +298,7 @@ public class WindowManagerProxy implements ResourceBasedOverride { Point size = new Point(); Display display = getDisplay(displayInfoContext); display.getRealSize(size); - Rect cutoutRect = new Rect(); - return new CachedDisplayInfo(size, rotation, cutoutRect); + return new CachedDisplayInfo(size, rotation); } } @@ -309,13 +308,8 @@ public class WindowManagerProxy implements ResourceBasedOverride { @TargetApi(Build.VERSION_CODES.S) protected CachedDisplayInfo getDisplayInfo(WindowMetrics windowMetrics, int rotation) { Point size = new Point(windowMetrics.getBounds().right, windowMetrics.getBounds().bottom); - Rect cutoutRect = new Rect(); - DisplayCutout cutout = windowMetrics.getWindowInsets().getDisplayCutout(); - if (cutout != null) { - cutoutRect.set(cutout.getSafeInsetLeft(), cutout.getSafeInsetTop(), - cutout.getSafeInsetRight(), cutout.getSafeInsetBottom()); - } - return new CachedDisplayInfo(size, rotation, cutoutRect); + return new CachedDisplayInfo(size, rotation, + windowMetrics.getWindowInsets().getDisplayCutout()); } /** @@ -354,6 +348,16 @@ public class WindowManagerProxy implements ResourceBasedOverride { DEFAULT_DISPLAY); } + /** + * Returns a DisplayCutout which represents a rotated version of the original + */ + protected DisplayCutout rotateCutout(DisplayCutout original, int startWidth, int startHeight, + int fromRotation, int toRotation) { + Rect safeCutout = getSafeInsets(original); + rotateRect(safeCutout, deltaRotation(fromRotation, toRotation)); + return new DisplayCutout(Insets.of(safeCutout), null, null, null, null); + } + /** * Returns the current navigation mode from resource. */ @@ -373,4 +377,12 @@ public class WindowManagerProxy implements ResourceBasedOverride { return Utilities.ATLEAST_S ? NavigationMode.NO_BUTTON : NavigationMode.THREE_BUTTONS; } + + /** + * @see DisplayCutout#getSafeInsets + */ + public static Rect getSafeInsets(DisplayCutout cutout) { + return new Rect(cutout.getSafeInsetLeft(), cutout.getSafeInsetTop(), + cutout.getSafeInsetRight(), cutout.getSafeInsetBottom()); + } } diff --git a/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt b/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt index d44ccf57d5..dbafe79ca7 100644 --- a/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt +++ b/tests/src/com/android/launcher3/AbstractDeviceProfileTest.kt @@ -60,8 +60,8 @@ abstract class AbstractDeviceProfileTest { private val windowManagerProxy: WindowManagerProxy = mock() private val launcherPrefs: LauncherPrefs = mock() private val allowLeftRightSplitInPortrait: Boolean = initAllowLeftRightSplitInPortrait() - fun initAllowLeftRightSplitInPortrait() : Boolean { - val res = Resources.getSystem(); + fun initAllowLeftRightSplitInPortrait(): Boolean { + val res = Resources.getSystem() val resId = res.getIdentifier("config_leftRightSplitInPortrait", "bool", "android") return Flags.enableLeftRightSplitInPortrait() && resId > 0 && res.getBoolean(resId) } @@ -124,8 +124,7 @@ abstract class AbstractDeviceProfileTest { ) { val (naturalX, naturalY) = deviceSpec.naturalSize val windowsBounds = phoneWindowsBounds(deviceSpec, isGestureMode, naturalX, naturalY) - val displayInfo = - CachedDisplayInfo(Point(naturalX, naturalY), Surface.ROTATION_0, Rect(0, 0, 0, 0)) + val displayInfo = CachedDisplayInfo(Point(naturalX, naturalY), Surface.ROTATION_0) val perDisplayBoundsCache = mapOf(displayInfo to windowsBounds) initializeCommonVars( @@ -144,8 +143,7 @@ abstract class AbstractDeviceProfileTest { ) { val (naturalX, naturalY) = deviceSpec.naturalSize val windowsBounds = tabletWindowsBounds(deviceSpec, naturalX, naturalY) - val displayInfo = - CachedDisplayInfo(Point(naturalX, naturalY), Surface.ROTATION_0, Rect(0, 0, 0, 0)) + val displayInfo = CachedDisplayInfo(Point(naturalX, naturalY), Surface.ROTATION_0) val perDisplayBoundsCache = mapOf(displayInfo to windowsBounds) initializeCommonVars( @@ -168,21 +166,13 @@ abstract class AbstractDeviceProfileTest { val unfoldedWindowsBounds = tabletWindowsBounds(deviceSpecUnfolded, unfoldedNaturalX, unfoldedNaturalY) val unfoldedDisplayInfo = - CachedDisplayInfo( - Point(unfoldedNaturalX, unfoldedNaturalY), - Surface.ROTATION_0, - Rect(0, 0, 0, 0) - ) + CachedDisplayInfo(Point(unfoldedNaturalX, unfoldedNaturalY), Surface.ROTATION_0) val (foldedNaturalX, foldedNaturalY) = deviceSpecFolded.naturalSize val foldedWindowsBounds = phoneWindowsBounds(deviceSpecFolded, isGestureMode, foldedNaturalX, foldedNaturalY) val foldedDisplayInfo = - CachedDisplayInfo( - Point(foldedNaturalX, foldedNaturalY), - Surface.ROTATION_0, - Rect(0, 0, 0, 0) - ) + CachedDisplayInfo(Point(foldedNaturalX, foldedNaturalY), Surface.ROTATION_0) val perDisplayBoundsCache = mapOf( @@ -325,12 +315,16 @@ abstract class AbstractDeviceProfileTest { // TODO(b/315230497): We don't currently have device-specific device profile dumps, so just // update the result before we do the comparison if (allowLeftRightSplitInPortrait) { - val isLeftRightSplitInPortrait = when { - allowLeftRightSplitInPortrait && dp.isTablet -> !dp.isLandscape - else -> dp.isLandscape - } - expected = expected.replace(Regex("isLeftRightSplit:\\w+"), - "isLeftRightSplit:$isLeftRightSplitInPortrait") + val isLeftRightSplitInPortrait = + when { + allowLeftRightSplitInPortrait && dp.isTablet -> !dp.isLandscape + else -> dp.isLandscape + } + expected = + expected.replace( + Regex("isLeftRightSplit:\\w+"), + "isLeftRightSplit:$isLeftRightSplitInPortrait" + ) } Truth.assertThat(dump).isEqualTo(expected) diff --git a/tests/src/com/android/launcher3/util/DisplayControllerTest.kt b/tests/src/com/android/launcher3/util/DisplayControllerTest.kt index 8670d40f4e..706ab27755 100644 --- a/tests/src/com/android/launcher3/util/DisplayControllerTest.kt +++ b/tests/src/com/android/launcher3/util/DisplayControllerTest.kt @@ -95,8 +95,7 @@ class DisplayControllerTest { whenever(launcherPrefs.get(TASKBAR_PINNING)).thenReturn(false) // Mock WindowManagerProxy - val displayInfo = - CachedDisplayInfo(Point(width, height), Surface.ROTATION_0, Rect(0, 0, 0, 0)) + val displayInfo = CachedDisplayInfo(Point(width, height), Surface.ROTATION_0) whenever(windowManagerProxy.getDisplayInfo(any())).thenReturn(displayInfo) whenever(windowManagerProxy.estimateInternalDisplayBounds(any())) .thenAnswer( @@ -135,8 +134,7 @@ class DisplayControllerTest { @Test @UiThreadTest fun testRotation() { - val displayInfo = - CachedDisplayInfo(Point(height, width), Surface.ROTATION_90, Rect(0, 0, 0, 0)) + val displayInfo = CachedDisplayInfo(Point(height, width), Surface.ROTATION_90) whenever(windowManagerProxy.getDisplayInfo(any())).thenReturn(displayInfo) whenever(display.rotation).thenReturn(displayInfo.rotation) val configuration =