Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address comments, formatting
  • Loading branch information
GaryQian committed Sep 2, 2020
commit 9504f64bd7e1f9ef1e9283dedf308498cbd72640
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ private int guessBottomKeyboardInset(WindowInsets insets) {
public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) {
WindowInsets newInsets = super.onApplyWindowInsets(insets);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// getSystemGestureInsets() was introduced in API 29 and immediately deprecated in 30.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
Insets systemGestureInsets = insets.getSystemGestureInsets();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to do this now, but this got deprecated after 1 Android version 🤣. We could add an R check for the new API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're already doing getInsets below. If that works in Q, we should just merge it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, getInsets is API 30 only, so to support this for Q, we still need to do this. For R and above though, we can move to getInsets

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, SG

viewportMetrics.systemGestureInsetTop = systemGestureInsets.top;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super tangential, just realized we added this to mediaquery. We should move the iOS home bar to use this too :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry disregard. After reading this from media query and android, I realized it doesn't mean the same thing

viewportMetrics.systemGestureInsetRight = systemGestureInsets.right;
Expand All @@ -522,37 +523,54 @@ public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) {
(SYSTEM_UI_FLAG_HIDE_NAVIGATION & getWindowSystemUiVisibility()) == 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These things are kinda big now. Feel free to split them into populatePreQ, populateQ, populateR if needed.


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
int mask = android.view.WindowInsets.Type.ime();
int mask = 0;
if (navigationBarVisible) {
mask = mask | android.view.WindowInsets.Type.navigationBars();
}
if (statusBarVisible) {
mask = mask | android.view.WindowInsets.Type.statusBars();
}
Insets uiInsets = insets.getInsets(mask);

viewportMetrics.paddingTop = uiInsets.top;
viewportMetrics.paddingRight = uiInsets.right;
viewportMetrics.paddingBottom = 0;
viewportMetrics.paddingBottom = uiInsets.bottom;
viewportMetrics.paddingLeft = uiInsets.left;

viewportMetrics.viewInsetTop = 0;
viewportMetrics.viewInsetRight = 0;
viewportMetrics.viewInsetBottom = uiInsets.bottom;
viewportMetrics.viewInsetLeft = 0;
Insets imeInsets = insets.getInsets(android.view.WindowInsets.Type.ime());
viewportMetrics.viewInsetTop = imeInsets.top;
viewportMetrics.viewInsetRight = imeInsets.right;
viewportMetrics.viewInsetBottom = imeInsets.bottom; // Typically, only bottom is non-zero
viewportMetrics.viewInsetLeft = imeInsets.left;

Insets systemGestureInsets = insets.getInsets(android.view.WindowInsets.Type.systemGestures());
viewportMetrics.systemGestureInsetTop = systemGestureInsets.top;
viewportMetrics.systemGestureInsetRight = systemGestureInsets.right;
viewportMetrics.systemGestureInsetBottom = systemGestureInsets.bottom;
viewportMetrics.systemGestureInsetLeft = systemGestureInsets.left;

// TODO(garyq): Expose the full rects of the display cutout.

// Take the max of the display cutout insets and existing insets to merge them
DisplayCutout cutout = insets.getCutout();
// Take the max of the display cutout insets and existing padding to merge them
DisplayCutout cutout = insets.getDisplayCutout();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, neat that they have the insets apis separate from the rect apis. I suppose there's no regression here since on iOS, you already can't tell apart the notch rect from the insets to "utilize" the remaining area.

Let's do the rect separately, sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flutter/flutter#65088 Tracks the rect work

if (cutout != null) {
Insets waterfallInsets = cutout.getWaterfallInsets();
viewportMetrics.systemGestureInsetTop = Math.max(Math.max(viewportMetrics.systemGestureInsetTop, waterfallInsets.top), cutout.getSafeInsetTop());
viewportMetrics.systemGestureInsetRight = Math.max(Math.max(viewportMetrics.systemGestureInsetRight, waterfallInsets.right), cutout.getSafeInsetRight());
viewportMetrics.systemGestureInsetBottom = Math.max(Math.max(viewportMetrics.systemGestureInsetBottom, waterfallInsets.bottom), cutout.getSafeInsetBottom());
viewportMetrics.systemGestureInsetLeft = Math.max(Math.max(viewportMetrics.systemGestureInsetLeft, waterfallInsets.left), cutout.getSafeInsetLeft());
viewportMetrics.paddingTop =
Math.max(
Math.max(viewportMetrics.paddingTop, waterfallInsets.top),
cutout.getSafeInsetTop());
viewportMetrics.paddingRight =
Math.max(
Math.max(viewportMetrics.paddingRight, waterfallInsets.right),
cutout.getSafeInsetRight());
viewportMetrics.paddingBottom =
Math.max(
Math.max(viewportMetrics.paddingBottom, waterfallInsets.bottom),
cutout.getSafeInsetBottom());
viewportMetrics.paddingLeft =
Math.max(
Math.max(viewportMetrics.paddingLeft, waterfallInsets.left),
cutout.getSafeInsetLeft());
}

} else {
// We zero the left and/or right sides to prevent the padding the
// navigation bar would have caused.
Expand Down Expand Up @@ -584,7 +602,6 @@ public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) {
viewportMetrics.viewInsetLeft = 0;
}


Log.v(
TAG,
"Updating window insets (onApplyWindowInsets()):\n"
Expand Down
49 changes: 33 additions & 16 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ private int guessBottomKeyboardInset(WindowInsets insets) {
@RequiresApi(20)
@SuppressLint({"InlinedApi", "NewApi"})
public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// getSystemGestureInsets() was introduced in API 29 and immediately deprecated in 30.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
Insets systemGestureInsets = insets.getSystemGestureInsets();
mMetrics.systemGestureInsetTop = systemGestureInsets.top;
mMetrics.systemGestureInsetRight = systemGestureInsets.right;
Expand All @@ -601,39 +602,55 @@ public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
boolean navigationBarVisible =
(SYSTEM_UI_FLAG_HIDE_NAVIGATION & getWindowSystemUiVisibility()) == 0;


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
int mask = android.view.WindowInsets.Type.ime();
int mask = 0;
if (navigationBarVisible) {
mask = mask | android.view.WindowInsets.Type.navigationBars();
}
if (statusBarVisible) {
mask = mask | android.view.WindowInsets.Type.statusBars();
}
Insets uiInsets = insets.getInsets(mask);

mMetrics.physicalPaddingTop = uiInsets.top;
mMetrics.physicalPaddingRight = uiInsets.right;
mMetrics.physicalPaddingBottom = 0;
mMetrics.physicalPaddingBottom = uiInsets.bottom;
mMetrics.physicalPaddingLeft = uiInsets.left;

mMetrics.physicalViewInsetTop = 0;
mMetrics.physicalViewInsetRight = 0;
mMetrics.physicalViewInsetBottom = uiInsets.bottom;
mMetrics.physicalViewInsetLeft = 0;
Insets imeInsets = insets.getInsets(android.view.WindowInsets.Type.ime());
mMetrics.physicalViewInsetTop = imeInsets.top;
mMetrics.physicalViewInsetRight = imeInsets.right;
mMetrics.physicalViewInsetBottom = imeInsets.bottom; // Typically, only bottom is non-zero
mMetrics.physicalViewInsetLeft = imeInsets.left;

Insets systemGestureInsets = insets.getInsets(android.view.WindowInsets.Type.systemGestures());
mMetrics.systemGestureInsetTop = systemGestureInsets.top;
mMetrics.systemGestureInsetRight = systemGestureInsets.right;
mMetrics.systemGestureInsetBottom = systemGestureInsets.bottom;
mMetrics.systemGestureInsetLeft = systemGestureInsets.left;

// TODO(garyq): Expose the full rects of the display cutout.

// Take the max of the display cutout insets and existing insets to merge them
DisplayCutout cutout = insets.getCutout();
// Take the max of the display cutout insets and existing padding to merge them
DisplayCutout cutout = insets.getDisplayCutout();
if (cutout != null) {
Insets waterfallInsets = cutout.getWaterfallInsets();
mMetrics.systemGestureInsetTop = Math.max(Math.max(mMetrics.systemGestureInsetTop, waterfallInsets.top), cutout.getSafeInsetTop());
mMetrics.systemGestureInsetRight = Math.max(Math.max(mMetrics.systemGestureInsetRight, waterfallInsets.right), cutout.getSafeInsetRight());
mMetrics.systemGestureInsetBottom = Math.max(Math.max(mMetrics.systemGestureInsetBottom, waterfallInsets.bottom), cutout.getSafeInsetBottom());
mMetrics.systemGestureInsetLeft = Math.max(Math.max(mMetrics.systemGestureInsetLeft, waterfallInsets.left), cutout.getSafeInsetLeft());
mMetrics.physicalPaddingTop =
Math.max(
Math.max(mMetrics.physicalPaddingTop, waterfallInsets.top),
cutout.getSafeInsetTop());
mMetrics.physicalPaddingRight =
Math.max(
Math.max(mMetrics.physicalPaddingRight, waterfallInsets.right),
cutout.getSafeInsetRight());
mMetrics.physicalPaddingBottom =
Math.max(
Math.max(mMetrics.physicalPaddingBottom, waterfallInsets.bottom),
cutout.getSafeInsetBottom());
mMetrics.physicalPaddingLeft =
Math.max(
Math.max(mMetrics.physicalPaddingLeft, waterfallInsets.left),
cutout.getSafeInsetLeft());
}

} else {
// We zero the left and/or right sides to prevent the padding the
// navigation bar would have caused.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ public void systemInsetDisplayCutoutSimple() {
((WindowManager)
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay());
// display.setRotation(0);
assertEquals(0, flutterView.getSystemUiVisibility());
when(flutterView.getWindowSystemUiVisibility()).thenReturn(0);
when(flutterView.getContext()).thenReturn(RuntimeEnvironment.systemContext);
Expand Down Expand Up @@ -529,20 +528,20 @@ public void systemInsetDisplayCutoutSimple() {

Insets waterfallInsets = Insets.of(200, 0, 200, 0);
when(displayCutout.getWaterfallInsets()).thenReturn(waterfallInsets);
when(displatCutout.getSafeInsetTop()).thenReturn(150);
when(displatCutout.getSafeInsetBottom()).thenReturn(150);
when(displatCutout.getSafeInsetLeft()).thenReturn(150);
when(displatCutout.getSafeInsetRight()).thenReturn(150);
when(displayCutout.getSafeInsetTop()).thenReturn(150);
when(displayCutout.getSafeInsetBottom()).thenReturn(150);
when(displayCutout.getSafeInsetLeft()).thenReturn(150);
when(displayCutout.getSafeInsetRight()).thenReturn(150);

flutterView.onApplyWindowInsets(windowInsets);

verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
assertEquals(150, viewportMetricsCaptor.getValue().systemGestureInsetTop);
assertEquals(150, viewportMetricsCaptor.getValue().systemGestureInsetBottom);
assertEquals(200, viewportMetricsCaptor.getValue().systemGestureInsetLeft);
assertEquals(200, viewportMetricsCaptor.getValue().systemGestureInsetRight);
assertEquals(150, viewportMetricsCaptor.getValue().paddingTop);
assertEquals(150, viewportMetricsCaptor.getValue().paddingBottom);
assertEquals(200, viewportMetricsCaptor.getValue().paddingLeft);
assertEquals(200, viewportMetricsCaptor.getValue().paddingRight);

assertEquals(100, viewportMetricsCaptor.getValue().paddingTop);
assertEquals(100, viewportMetricsCaptor.getValue().viewInsetTop);
}

@Test
Expand Down