Use getBoundingRects to add support inset MediaQuery/SafeArea when in freeform mode controls are shown. (flutter/engine#54294)

Original Title: Add FlutterViewDelegate and BoundingRect methods

Check the bounding rect for caption bar when sending viewport metrics to
Flutter to account for freeform mode. Use the more recent
`getBoundingRects` over `getInsets`.

Tests will need to be kept separate until Robolectric publishes a
version that supports API level 35.
Roboletric tests for this pr will be part of google testing until then. 
Pr for tests here https://critique.corp.google.com/cl/657302386. 

https://github.com/flutter/flutter/issues/146658

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] I signed the [CLA].
- [x] All existing and new tests are passing.
This commit is contained in:
yaakovschectman
2024-08-20 13:09:00 -04:00
committed by GitHub
parent 149b1974f5
commit 2f6952657d
5 changed files with 103 additions and 0 deletions

View File

@@ -44060,6 +44060,7 @@ ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/Flu
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterSurfaceView.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterTextureView.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterViewDelegate.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/KeyChannelResponder.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/KeyData.java + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/android/io/flutter/embedding/android/KeyEmbedderResponder.java + ../../../flutter/LICENSE
@@ -46946,6 +46947,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/Flutt
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterSurfaceView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterTextureView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/FlutterViewDelegate.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/KeyChannelResponder.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/KeyData.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/android/KeyEmbedderResponder.java

View File

@@ -228,6 +228,7 @@ android_java_sources = [
"io/flutter/embedding/android/FlutterSurfaceView.java",
"io/flutter/embedding/android/FlutterTextureView.java",
"io/flutter/embedding/android/FlutterView.java",
"io/flutter/embedding/android/FlutterViewDelegate.java",
"io/flutter/embedding/android/KeyChannelResponder.java",
"io/flutter/embedding/android/KeyData.java",
"io/flutter/embedding/android/KeyEmbedderResponder.java",

View File

@@ -772,6 +772,15 @@ public class FlutterView extends FrameLayout
viewportMetrics.viewInsetLeft = 0;
}
// The caption bar inset is a new addition, and the APIs called to query it utilize a list of
// bounding Rects instead of an Insets object, which is a newer API method, as compared to the
// existing Insets-based method calls above.
if (Build.VERSION.SDK_INT >= API_LEVELS.API_35) {
delegate.growViewportMetricsToCaptionBar(getContext(), viewportMetrics);
} else {
Log.w(TAG, "API level " + Build.VERSION.SDK_INT + " is too low to query bounding rects.");
}
Log.v(
TAG,
"Updating window insets (onApplyWindowInsets()):\n"
@@ -1449,6 +1458,13 @@ public class FlutterView extends FrameLayout
.send();
}
private FlutterViewDelegate delegate = new FlutterViewDelegate();
@VisibleForTesting
public void setDelegate(@NonNull FlutterViewDelegate delegate) {
this.delegate = delegate;
}
private void sendViewportMetricsToFlutter() {
if (!isAttachedToFlutterEngine()) {
Log.w(
@@ -1460,6 +1476,7 @@ public class FlutterView extends FrameLayout
viewportMetrics.devicePixelRatio = getResources().getDisplayMetrics().density;
viewportMetrics.physicalTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
flutterEngine.getRenderer().setViewportMetrics(viewportMetrics);
}
@@ -1485,6 +1502,15 @@ public class FlutterView extends FrameLayout
}
}
/**
* Allow access to the viewport metrics so that tests can set them to be valid with nonzero
* dimensions.
*/
@VisibleForTesting
public FlutterRenderer.ViewportMetrics getViewportMetrics() {
return viewportMetrics;
}
/**
* Listener that is notified when a {@link io.flutter.embedding.engine.FlutterEngine} is attached
* to/detached from a given {@code FlutterView}.

View File

@@ -0,0 +1,71 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.embedding.android;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.Window;
import android.view.WindowInsets;
import androidx.annotation.RequiresApi;
import androidx.annotation.VisibleForTesting;
import io.flutter.Build;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.util.ViewUtils;
import java.util.Collections;
import java.util.List;
/**
* A delegate class that performs the task of retrieving the bounding rect values. Logic that is
* independent of the engine, or that tests must access in the absence of an engine, shall reside
* within this class.
*/
public class FlutterViewDelegate {
/**
* Return the WindowInsets object for the provided Context. A Context will only have a window if
* it is an instance of Activity. If context does not have a window, or it is not an activity,
* this method will return null. Otherwise, this method will return the WindowInsets for the
* provided activity's window.
*/
@RequiresApi(api = Build.API_LEVELS.API_23)
@VisibleForTesting
public WindowInsets getWindowInsets(Context context) {
Activity activity = ViewUtils.getActivity(context);
if (activity == null) {
return null;
}
Window window = activity.getWindow();
if (window == null) {
return null;
}
return window.getDecorView().getRootWindowInsets();
}
@RequiresApi(api = Build.API_LEVELS.API_35)
public List<Rect> getCaptionBarInsets(Context context) {
WindowInsets insets = getWindowInsets(context);
if (insets == null) {
return Collections.emptyList();
}
return insets.getBoundingRects(WindowInsets.Type.captionBar());
}
@RequiresApi(api = Build.API_LEVELS.API_35)
public void growViewportMetricsToCaptionBar(
Context context, FlutterRenderer.ViewportMetrics viewportMetrics) {
List<Rect> boundingRects = getCaptionBarInsets(context);
int viewPaddingTop = viewportMetrics.viewPaddingTop;
for (Rect rect : boundingRects) {
viewPaddingTop = Math.max(viewPaddingTop, rect.bottom);
}
// The value getCaptionBarInset returns is only the bounding rects of the caption bar.
// When assigning the new value of viewPaddingTop, the maximum is taken with its old value
// to ensure that any previous top padding that is greater than that from the caption bar
// is not destroyed by this operation.
// Any potential update that will allow the caption bar to be positioned somewhere other than
// the top of the app window will require that this method be rewritten.
viewportMetrics.viewPaddingTop = viewPaddingTop;
}
}

View File

@@ -1271,6 +1271,9 @@ public class FlutterRenderer implements TextureRegistry {
public float devicePixelRatio = 1.0f;
public int width = 0;
public int height = 0;
// The fields prefixed with viewPadding and viewInset are used to calculate the padding,
// viewPadding, and viewInsets of ViewConfiguration in Dart. This calculation is performed at
// https://github.com/flutter/engine/blob/main/lib/ui/hooks.dart#L139-L155.
public int viewPaddingTop = 0;
public int viewPaddingRight = 0;
public int viewPaddingBottom = 0;