Reland: Create PlatformView instance right after method channel call from Dart (flutter/engine#20568)
This commit is contained in:
@@ -79,9 +79,20 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
// it is associated with(e.g if a platform view creates other views in the same virtual display.
|
||||
private final HashMap<Context, View> contextToPlatformView;
|
||||
|
||||
private final SparseArray<PlatformViewsChannel.PlatformViewCreationRequest> platformViewRequests;
|
||||
// The views returned by `PlatformView#getView()`.
|
||||
//
|
||||
// This only applies to hybrid composition.
|
||||
private final SparseArray<View> platformViews;
|
||||
private final SparseArray<FlutterMutatorView> mutatorViews;
|
||||
|
||||
// The platform view parents that are appended to `FlutterView`.
|
||||
// If an entry in `platformViews` doesn't have an entry in this array, the platform view isn't
|
||||
// in the view hierarchy.
|
||||
//
|
||||
// This view provides a wrapper that applies scene builder operations to the platform view.
|
||||
// For example, a transform matrix, or setting opacity on the platform view layer.
|
||||
//
|
||||
// This is only applies to hybrid composition.
|
||||
private final SparseArray<FlutterMutatorView> platformViewParent;
|
||||
|
||||
// Map of unique IDs to views that render overlay layers.
|
||||
private final SparseArray<FlutterImageView> overlayLayerViews;
|
||||
@@ -107,25 +118,57 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
@Override
|
||||
public void createAndroidViewForPlatformView(
|
||||
@NonNull PlatformViewsChannel.PlatformViewCreationRequest request) {
|
||||
// API level 19 is required for android.graphics.ImageReader.
|
||||
// API level 19 is required for `android.graphics.ImageReader`.
|
||||
ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT);
|
||||
platformViewRequests.put(request.viewId, request);
|
||||
|
||||
if (!validateDirection(request.direction)) {
|
||||
throw new IllegalStateException(
|
||||
"Trying to create a view with unknown direction value: "
|
||||
+ request.direction
|
||||
+ "(view id: "
|
||||
+ request.viewId
|
||||
+ ")");
|
||||
}
|
||||
|
||||
final PlatformViewFactory factory = registry.getFactory(request.viewType);
|
||||
if (factory == null) {
|
||||
throw new IllegalStateException(
|
||||
"Trying to create a platform view of unregistered type: " + request.viewType);
|
||||
}
|
||||
|
||||
Object createParams = null;
|
||||
if (request.params != null) {
|
||||
createParams = factory.getCreateArgsCodec().decodeMessage(request.params);
|
||||
}
|
||||
|
||||
final PlatformView platformView = factory.create(context, request.viewId, createParams);
|
||||
final View view = platformView.getView();
|
||||
if (view == null) {
|
||||
throw new IllegalStateException(
|
||||
"PlatformView#getView() returned null, but an Android view reference was expected.");
|
||||
}
|
||||
if (view.getParent() != null) {
|
||||
throw new IllegalStateException(
|
||||
"The Android view returned from PlatformView#getView() was already added to a parent view.");
|
||||
}
|
||||
platformViews.put(request.viewId, view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disposeAndroidViewForPlatformView(int viewId) {
|
||||
// Hybrid view.
|
||||
if (platformViewRequests.get(viewId) != null) {
|
||||
platformViewRequests.remove(viewId);
|
||||
final View platformView = platformViews.get(viewId);
|
||||
final FlutterMutatorView parentView = platformViewParent.get(viewId);
|
||||
if (platformView != null) {
|
||||
if (parentView != null) {
|
||||
parentView.removeView(platformView);
|
||||
}
|
||||
platformViews.remove(viewId);
|
||||
}
|
||||
|
||||
final View platformView = platformViews.get(viewId);
|
||||
if (platformView != null) {
|
||||
final FlutterMutatorView mutatorView = mutatorViews.get(viewId);
|
||||
mutatorView.removeView(platformView);
|
||||
((FlutterView) flutterView).removeView(mutatorView);
|
||||
platformViews.remove(viewId);
|
||||
mutatorViews.remove(viewId);
|
||||
if (parentView != null) {
|
||||
((FlutterView) flutterView).removeView(parentView);
|
||||
platformViewParent.remove(viewId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,9 +421,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
currentFrameUsedOverlayLayerIds = new HashSet<>();
|
||||
currentFrameUsedPlatformViewIds = new HashSet<>();
|
||||
|
||||
platformViewRequests = new SparseArray<>();
|
||||
platformViews = new SparseArray<>();
|
||||
mutatorViews = new SparseArray<>();
|
||||
platformViewParent = new SparseArray<>();
|
||||
|
||||
motionEventTracker = MotionEventTracker.getInstance();
|
||||
}
|
||||
@@ -651,55 +693,20 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
|
||||
@VisibleForTesting
|
||||
void initializePlatformViewIfNeeded(int viewId) {
|
||||
if (platformViews.get(viewId) != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
PlatformViewsChannel.PlatformViewCreationRequest request = platformViewRequests.get(viewId);
|
||||
if (request == null) {
|
||||
final View view = platformViews.get(viewId);
|
||||
if (view == null) {
|
||||
throw new IllegalStateException(
|
||||
"Platform view hasn't been initialized from the platform view channel.");
|
||||
}
|
||||
|
||||
if (!validateDirection(request.direction)) {
|
||||
throw new IllegalStateException(
|
||||
"Trying to create a view with unknown direction value: "
|
||||
+ request.direction
|
||||
+ "(view id: "
|
||||
+ viewId
|
||||
+ ")");
|
||||
if (platformViewParent.get(viewId) != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
PlatformViewFactory factory = registry.getFactory(request.viewType);
|
||||
if (factory == null) {
|
||||
throw new IllegalStateException(
|
||||
"Trying to create a platform view of unregistered type: " + request.viewType);
|
||||
}
|
||||
|
||||
Object createParams = null;
|
||||
if (request.params != null) {
|
||||
createParams = factory.getCreateArgsCodec().decodeMessage(request.params);
|
||||
}
|
||||
|
||||
PlatformView platformView = factory.create(context, viewId, createParams);
|
||||
View view = platformView.getView();
|
||||
|
||||
if (view == null) {
|
||||
throw new IllegalStateException(
|
||||
"PlatformView#getView() returned null, but an Android view reference was expected.");
|
||||
}
|
||||
if (view.getParent() != null) {
|
||||
throw new IllegalStateException(
|
||||
"The Android view returned from PlatformView#getView() was already added to a parent view.");
|
||||
}
|
||||
platformViews.put(viewId, view);
|
||||
|
||||
FlutterMutatorView mutatorView =
|
||||
final FlutterMutatorView parentView =
|
||||
new FlutterMutatorView(
|
||||
context, context.getResources().getDisplayMetrics().density, androidTouchProcessor);
|
||||
mutatorViews.put(viewId, mutatorView);
|
||||
mutatorView.addView(view);
|
||||
((FlutterView) flutterView).addView(mutatorView);
|
||||
platformViewParent.put(viewId, parentView);
|
||||
parentView.addView(view);
|
||||
((FlutterView) flutterView).addView(parentView);
|
||||
}
|
||||
|
||||
public void attachToFlutterRenderer(FlutterRenderer flutterRenderer) {
|
||||
@@ -718,13 +725,14 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
initializeRootImageViewIfNeeded();
|
||||
initializePlatformViewIfNeeded(viewId);
|
||||
|
||||
FlutterMutatorView mutatorView = mutatorViews.get(viewId);
|
||||
mutatorView.readyToDisplay(mutatorsStack, x, y, width, height);
|
||||
mutatorView.setVisibility(View.VISIBLE);
|
||||
mutatorView.bringToFront();
|
||||
final FlutterMutatorView parentView = platformViewParent.get(viewId);
|
||||
parentView.readyToDisplay(mutatorsStack, x, y, width, height);
|
||||
parentView.setVisibility(View.VISIBLE);
|
||||
parentView.bringToFront();
|
||||
|
||||
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(viewWidth, viewHeight);
|
||||
View platformView = platformViews.get(viewId);
|
||||
final FrameLayout.LayoutParams layoutParams =
|
||||
new FrameLayout.LayoutParams(viewWidth, viewHeight);
|
||||
final View platformView = platformViews.get(viewId);
|
||||
platformView.setLayoutParams(layoutParams);
|
||||
platformView.bringToFront();
|
||||
currentFrameUsedPlatformViewIds.add(viewId);
|
||||
@@ -733,7 +741,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
public void onDisplayOverlaySurface(int id, int x, int y, int width, int height) {
|
||||
initializeRootImageViewIfNeeded();
|
||||
|
||||
FlutterImageView overlayView = overlayLayerViews.get(id);
|
||||
final FlutterImageView overlayView = overlayLayerViews.get(id);
|
||||
if (overlayView.getParent() == null) {
|
||||
((FlutterView) flutterView).addView(overlayView);
|
||||
}
|
||||
@@ -776,19 +784,19 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
// If one of the surfaces doesn't have an image, the frame may be incomplete and must be
|
||||
// dropped.
|
||||
// For example, a toolbar widget painted by Flutter may not be rendered.
|
||||
boolean isFrameRenderedUsingImageReaders =
|
||||
final boolean isFrameRenderedUsingImageReaders =
|
||||
flutterViewConvertedToImageView && view.acquireLatestImageViewFrame();
|
||||
finishFrame(isFrameRenderedUsingImageReaders);
|
||||
}
|
||||
|
||||
private void finishFrame(boolean isFrameRenderedUsingImageReaders) {
|
||||
for (int i = 0; i < overlayLayerViews.size(); i++) {
|
||||
int overlayId = overlayLayerViews.keyAt(i);
|
||||
FlutterImageView overlayView = overlayLayerViews.valueAt(i);
|
||||
final int overlayId = overlayLayerViews.keyAt(i);
|
||||
final FlutterImageView overlayView = overlayLayerViews.valueAt(i);
|
||||
|
||||
if (currentFrameUsedOverlayLayerIds.contains(overlayId)) {
|
||||
((FlutterView) flutterView).attachOverlaySurfaceToRender(overlayView);
|
||||
boolean didAcquireOverlaySurfaceImage = overlayView.acquireLatestImage();
|
||||
final boolean didAcquireOverlaySurfaceImage = overlayView.acquireLatestImage();
|
||||
isFrameRenderedUsingImageReaders &= didAcquireOverlaySurfaceImage;
|
||||
} else {
|
||||
// If the background surface isn't rendered by the image view, then the
|
||||
@@ -802,22 +810,20 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < platformViews.size(); i++) {
|
||||
int viewId = platformViews.keyAt(i);
|
||||
View platformView = platformViews.get(viewId);
|
||||
View mutatorView = mutatorViews.get(viewId);
|
||||
for (int i = 0; i < platformViewParent.size(); i++) {
|
||||
final int viewId = platformViewParent.keyAt(i);
|
||||
final View parentView = platformViewParent.get(viewId);
|
||||
|
||||
// Show platform views only if the surfaces have images available in this frame,
|
||||
// and if the platform view is rendered in this frame.
|
||||
// The platform view is appended to a mutator view.
|
||||
//
|
||||
// Otherwise, hide the platform view, but don't remove it from the view hierarchy yet as
|
||||
// they are removed when the framework diposes the platform view widget.
|
||||
if (isFrameRenderedUsingImageReaders && currentFrameUsedPlatformViewIds.contains(viewId)) {
|
||||
platformView.setVisibility(View.VISIBLE);
|
||||
mutatorView.setVisibility(View.VISIBLE);
|
||||
parentView.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
platformView.setVisibility(View.GONE);
|
||||
mutatorView.setVisibility(View.GONE);
|
||||
parentView.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import static org.mockito.Mockito.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.SparseArray;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.Surface;
|
||||
import android.view.SurfaceHolder;
|
||||
@@ -28,7 +29,9 @@ import io.flutter.embedding.engine.systemchannels.KeyEventChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.SettingsChannel;
|
||||
import io.flutter.embedding.engine.systemchannels.TextInputChannel;
|
||||
import io.flutter.plugin.common.FlutterException;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.StandardMessageCodec;
|
||||
import io.flutter.plugin.common.StandardMethodCodec;
|
||||
import io.flutter.plugin.localization.LocalizationPlugin;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -242,7 +245,29 @@ public class PlatformViewsControllerTest {
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterJNI.class})
|
||||
public void initializePlatformViewIfNeeded__throwsIfViewIsNull() {
|
||||
public void createPlatformViewMessage__initializesAndroidView() {
|
||||
PlatformViewsController platformViewsController = new PlatformViewsController();
|
||||
|
||||
int platformViewId = 0;
|
||||
assertNull(platformViewsController.getPlatformViewById(platformViewId));
|
||||
|
||||
PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
|
||||
PlatformView platformView = mock(PlatformView.class);
|
||||
when(platformView.getView()).thenReturn(mock(View.class));
|
||||
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
|
||||
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
|
||||
|
||||
FlutterJNI jni = new FlutterJNI();
|
||||
attach(jni, platformViewsController);
|
||||
|
||||
// Simulate create call from the framework.
|
||||
createPlatformView(jni, platformViewsController, platformViewId, "testType");
|
||||
verify(platformView, times(1)).getView();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterJNI.class})
|
||||
public void createPlatformViewMessage__throwsIfViewIsNull() {
|
||||
PlatformViewsController platformViewsController = new PlatformViewsController();
|
||||
|
||||
int platformViewId = 0;
|
||||
@@ -259,22 +284,28 @@ public class PlatformViewsControllerTest {
|
||||
|
||||
// Simulate create call from the framework.
|
||||
createPlatformView(jni, platformViewsController, platformViewId, "testType");
|
||||
assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
|
||||
|
||||
final ByteBuffer responseBuffer = ShadowFlutterJNI.getResponses().get(0);
|
||||
responseBuffer.rewind();
|
||||
|
||||
StandardMethodCodec methodCodec = new StandardMethodCodec(new StandardMessageCodec());
|
||||
try {
|
||||
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
|
||||
} catch (Exception exception) {
|
||||
assertTrue(exception instanceof IllegalStateException);
|
||||
assertEquals(
|
||||
exception.getMessage(),
|
||||
"PlatformView#getView() returned null, but an Android view reference was expected.");
|
||||
methodCodec.decodeEnvelope(responseBuffer);
|
||||
} catch (FlutterException exception) {
|
||||
assertTrue(
|
||||
exception
|
||||
.getMessage()
|
||||
.contains(
|
||||
"PlatformView#getView() returned null, but an Android view reference was expected."));
|
||||
return;
|
||||
}
|
||||
assertTrue(false);
|
||||
assertFalse(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterJNI.class})
|
||||
public void initializePlatformViewIfNeeded__throwsIfViewHasParent() {
|
||||
public void createPlatformViewMessage__throwsIfViewHasParent() {
|
||||
PlatformViewsController platformViewsController = new PlatformViewsController();
|
||||
|
||||
int platformViewId = 0;
|
||||
@@ -293,16 +324,23 @@ public class PlatformViewsControllerTest {
|
||||
|
||||
// Simulate create call from the framework.
|
||||
createPlatformView(jni, platformViewsController, platformViewId, "testType");
|
||||
assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
|
||||
|
||||
final ByteBuffer responseBuffer = ShadowFlutterJNI.getResponses().get(0);
|
||||
responseBuffer.rewind();
|
||||
|
||||
StandardMethodCodec methodCodec = new StandardMethodCodec(new StandardMessageCodec());
|
||||
try {
|
||||
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
|
||||
} catch (Exception exception) {
|
||||
assertTrue(exception instanceof IllegalStateException);
|
||||
assertEquals(
|
||||
exception.getMessage(),
|
||||
"The Android view returned from PlatformView#getView() was already added to a parent view.");
|
||||
methodCodec.decodeEnvelope(responseBuffer);
|
||||
} catch (FlutterException exception) {
|
||||
assertTrue(
|
||||
exception
|
||||
.getMessage()
|
||||
.contains(
|
||||
"The Android view returned from PlatformView#getView() was already added to a parent view."));
|
||||
return;
|
||||
}
|
||||
assertTrue(false);
|
||||
assertFalse(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -407,6 +445,80 @@ public class PlatformViewsControllerTest {
|
||||
verify(overlayImageView, times(1)).detachFromRenderer();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterSurfaceView.class, ShadowFlutterJNI.class})
|
||||
public void onEndFrame__removesPlatformView() {
|
||||
final PlatformViewsController platformViewsController = new PlatformViewsController();
|
||||
|
||||
final int platformViewId = 0;
|
||||
assertNull(platformViewsController.getPlatformViewById(platformViewId));
|
||||
|
||||
final PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
|
||||
final PlatformView platformView = mock(PlatformView.class);
|
||||
final View androidView = mock(View.class);
|
||||
when(platformView.getView()).thenReturn(androidView);
|
||||
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
|
||||
|
||||
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
|
||||
|
||||
final FlutterJNI jni = new FlutterJNI();
|
||||
jni.attachToNative(false);
|
||||
attach(jni, platformViewsController);
|
||||
|
||||
jni.onFirstFrame();
|
||||
|
||||
// Simulate create call from the framework.
|
||||
createPlatformView(jni, platformViewsController, platformViewId, "testType");
|
||||
|
||||
// Simulate first frame from the framework.
|
||||
jni.onFirstFrame();
|
||||
platformViewsController.onBeginFrame();
|
||||
|
||||
platformViewsController.onEndFrame();
|
||||
verify(androidView, never()).setVisibility(View.GONE);
|
||||
|
||||
final ViewParent parentView = mock(ViewParent.class);
|
||||
when(androidView.getParent()).thenReturn(parentView);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterSurfaceView.class, ShadowFlutterJNI.class})
|
||||
public void onEndFrame__removesPlatformViewParent() {
|
||||
final PlatformViewsController platformViewsController = new PlatformViewsController();
|
||||
|
||||
final int platformViewId = 0;
|
||||
assertNull(platformViewsController.getPlatformViewById(platformViewId));
|
||||
|
||||
final PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
|
||||
final PlatformView platformView = mock(PlatformView.class);
|
||||
final View androidView = mock(View.class);
|
||||
when(platformView.getView()).thenReturn(androidView);
|
||||
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
|
||||
|
||||
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
|
||||
|
||||
final FlutterJNI jni = new FlutterJNI();
|
||||
jni.attachToNative(false);
|
||||
|
||||
final FlutterView flutterView = attach(jni, platformViewsController);
|
||||
|
||||
jni.onFirstFrame();
|
||||
|
||||
// Simulate create call from the framework.
|
||||
createPlatformView(jni, platformViewsController, platformViewId, "testType");
|
||||
platformViewsController.initializePlatformViewIfNeeded(platformViewId);
|
||||
assertEquals(flutterView.getChildCount(), 2);
|
||||
|
||||
// Simulate first frame from the framework.
|
||||
jni.onFirstFrame();
|
||||
platformViewsController.onBeginFrame();
|
||||
platformViewsController.onEndFrame();
|
||||
|
||||
// Simulate dispose call from the framework.
|
||||
disposePlatformView(jni, platformViewsController, platformViewId);
|
||||
assertEquals(flutterView.getChildCount(), 1);
|
||||
}
|
||||
|
||||
private static byte[] encodeMethodCall(MethodCall call) {
|
||||
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
|
||||
buffer.rewind();
|
||||
@@ -446,7 +558,8 @@ public class PlatformViewsControllerTest {
|
||||
"flutter/platform_views", encodeMethodCall(platformDisposeMethodCall), /*replyId=*/ 0);
|
||||
}
|
||||
|
||||
private static void attach(FlutterJNI jni, PlatformViewsController platformViewsController) {
|
||||
private static FlutterView attach(
|
||||
FlutterJNI jni, PlatformViewsController platformViewsController) {
|
||||
final DartExecutor executor = new DartExecutor(jni, mock(AssetManager.class));
|
||||
executor.onAttachedToJNI();
|
||||
|
||||
@@ -477,10 +590,12 @@ public class PlatformViewsControllerTest {
|
||||
|
||||
view.attachToFlutterEngine(engine);
|
||||
platformViewsController.attachToView(view);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Implements(FlutterJNI.class)
|
||||
public static class ShadowFlutterJNI {
|
||||
private static SparseArray<ByteBuffer> replies = new SparseArray<>();
|
||||
|
||||
public ShadowFlutterJNI() {}
|
||||
|
||||
@@ -527,7 +642,13 @@ public class PlatformViewsControllerTest {
|
||||
|
||||
@Implementation
|
||||
public void invokePlatformMessageResponseCallback(
|
||||
int responseId, ByteBuffer message, int position) {}
|
||||
int responseId, ByteBuffer message, int position) {
|
||||
replies.put(responseId, message);
|
||||
}
|
||||
|
||||
public static SparseArray<ByteBuffer> getResponses() {
|
||||
return replies;
|
||||
}
|
||||
}
|
||||
|
||||
@Implements(SurfaceView.class)
|
||||
|
||||
Reference in New Issue
Block a user