Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[PlatformView][Android]VirtualDisplay resize on Android31 and above #47946

Merged
merged 16 commits into from
Nov 21, 2023
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,6 +129,16 @@ public int getRenderTargetHeight() {
}

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the contract around onNewSizeFrameAvailable? In the case that the size doesn't change do we need to run 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.

yes, postDelayed can be removed

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is true, it must be called.

return;
Copy link
Contributor

Choose a reason for hiding this comment

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

After reading the calling code, we do need to run the onNewSizeFrameAvailable in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meaningless comments have been removed.
In this case, added onNewSizeFrameAvailable callback.
Thank you for your guidance and help!

}
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.
Expand Down Expand Up @@ -199,6 +210,15 @@ public void dispose() {
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) {
Expand Down