From 950ec4aed632f9d372c36986e198d19b9da17f1c Mon Sep 17 00:00:00 2001 From: YaTang <33898039+designDo@users.noreply.github.com> Date: Tue, 21 Nov 2023 11:16:02 +0800 Subject: [PATCH] [PlatformView][Android]VirtualDisplay resize on Android31 and above (flutter/engine#47946) Resolves: https://github.com/flutter/flutter/issues/128920 --- .../platform/VirtualDisplayController.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java index 1b90ca5c3a..1cb44373b1 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java @@ -10,6 +10,7 @@ import android.annotation.TargetApi; import android.content.Context; import android.hardware.display.DisplayManager; import android.hardware.display.VirtualDisplay; +import android.os.Build; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; @@ -128,6 +129,16 @@ class VirtualDisplayController { } public void resize(final int width, final int height, final Runnable onNewSizeFrameAvailable) { + // When 'hot reload', although the resize method is triggered, the size of the native View has + // not changed. + if (width == getRenderTargetWidth() && height == getRenderTargetHeight()) { + getView().postDelayed(onNewSizeFrameAvailable, 0); + return; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + resize31(getView(), width, height, onNewSizeFrameAvailable); + return; + } boolean isFocused = getView().isFocused(); final SingleViewPresentation.PresentationState presentationState = presentation.detachState(); // We detach the surface to prevent it being destroyed when releasing the vd. @@ -199,6 +210,15 @@ class VirtualDisplayController { renderTarget.release(); } + @TargetApi(31) + private void resize31( + View embeddedView, int width, int height, final Runnable onNewSizeFrameAvailable) { + renderTarget.resize(width, height); + // On Android versions 31+ resizing of a Virtual Display's Presentation is natively supported. + virtualDisplay.resize(width, height, densityDpi); + embeddedView.postDelayed(onNewSizeFrameAvailable, 0); + } + /** See {@link PlatformView#onFlutterViewAttached(View)} */ /*package*/ void onFlutterViewAttached(@NonNull View flutterView) { if (presentation == null || presentation.getView() == null) {