[PlatformView][Android]VirtualDisplay resize on Android31 and above (#47946)
Resolves: https://github.com/flutter/flutter/issues/128920
diff --git a/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java b/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java
index 1b90ca5..1cb4437 100644
--- a/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java
+++ b/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java
@@ -10,6 +10,7 @@
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 @@
}
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 @@
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) {