[Android] Make PVC1 and PVC2 share a platform view registry. (#162857)

This avoids the insanity of having to register everything twice. Since
the object is shared a registration on pvc1 will automatically apply to
pvc2.
This commit is contained in:
Jonah Williams 2025-02-07 17:05:00 -08:00 committed by GitHub
parent 6cca066620
commit 53bcdf2cda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 13 deletions

View File

@ -32,17 +32,6 @@ class MainActivity : FlutterActivity() {
add(NativeDriverSupportPlugin()) add(NativeDriverSupportPlugin())
} }
// TODO(jonahwilliams): make platform view controllers share platform view registry.
flutterEngine
.platformViewsController2
.registry
.apply {
registerViewFactory("blue_orange_gradient_platform_view", BlueOrangeGradientPlatformViewFactory())
registerViewFactory("blue_orange_gradient_surface_view_platform_view", BlueOrangeGradientSurfaceViewPlatformViewFactory())
registerViewFactory("changing_color_button_platform_view", ChangingColorButtonPlatformViewFactory())
registerViewFactory("box_platform_view", BoxPlatformViewFactory())
}
flutterEngine flutterEngine
.platformViewsController .platformViewsController
.registry .registry

View File

@ -364,6 +364,7 @@ public class FlutterEngine implements ViewUtils.DisplayUpdater {
} }
PlatformViewsController2 platformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 platformViewsController2 = new PlatformViewsController2();
platformViewsController2.setRegistry(platformViewsController.getRegistry());
flutterJNI.addEngineLifecycleListener(engineLifecycleListener); flutterJNI.addEngineLifecycleListener(engineLifecycleListener);
flutterJNI.setPlatformViewsController(platformViewsController); flutterJNI.setPlatformViewsController(platformViewsController);

View File

@ -46,7 +46,7 @@ import java.util.List;
public class PlatformViewsController2 implements PlatformViewsAccessibilityDelegate { public class PlatformViewsController2 implements PlatformViewsAccessibilityDelegate {
private static final String TAG = "PlatformViewsController2"; private static final String TAG = "PlatformViewsController2";
private final PlatformViewRegistryImpl registry; private PlatformViewRegistryImpl registry;
private AndroidTouchProcessor androidTouchProcessor; private AndroidTouchProcessor androidTouchProcessor;
private Context context; private Context context;
private FlutterView flutterView; private FlutterView flutterView;
@ -65,7 +65,6 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
private Surface overlayerSurface = null; private Surface overlayerSurface = null;
public PlatformViewsController2() { public PlatformViewsController2() {
registry = new PlatformViewRegistryImpl();
accessibilityEventsDelegate = new AccessibilityEventsDelegate(); accessibilityEventsDelegate = new AccessibilityEventsDelegate();
platformViews = new SparseArray<>(); platformViews = new SparseArray<>();
platformViewParent = new SparseArray<>(); platformViewParent = new SparseArray<>();
@ -74,6 +73,10 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
motionEventTracker = MotionEventTracker.getInstance(); motionEventTracker = MotionEventTracker.getInstance();
} }
public void setRegistry(@NonNull PlatformViewRegistry registry) {
this.registry = (PlatformViewRegistryImpl) registry;
}
@Override @Override
public boolean usesVirtualDisplay(int id) { public boolean usesVirtualDisplay(int id) {
return false; return false;

View File

@ -95,7 +95,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void itRemovesPlatformViewBeforeDiposeIsCalled() { public void itRemovesPlatformViewBeforeDiposeIsCalled() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
FlutterJNI jni = new FlutterJNI(); FlutterJNI jni = new FlutterJNI();
attach(jni, PlatformViewsController2); attach(jni, PlatformViewsController2);
// Get the platform view registry. // Get the platform view registry.
@ -127,7 +129,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void itNotifiesPlatformViewsOfEngineAttachmentAndDetachment() { public void itNotifiesPlatformViewsOfEngineAttachmentAndDetachment() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
FlutterJNI jni = new FlutterJNI(); FlutterJNI jni = new FlutterJNI();
attach(jni, PlatformViewsController2); attach(jni, PlatformViewsController2);
// Get the platform view registry. // Get the platform view registry.
@ -165,7 +169,9 @@ public class PlatformViewsController2Test {
@Test @Test
public void itUsesActionEventTypeFromFrameworkEventAsActionChanged() { public void itUsesActionEventTypeFromFrameworkEventAsActionChanged() {
MotionEventTracker motionEventTracker = MotionEventTracker.getInstance(); MotionEventTracker motionEventTracker = MotionEventTracker.getInstance();
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
MotionEvent original = MotionEvent original =
MotionEvent.obtain( MotionEvent.obtain(
@ -257,7 +263,9 @@ public class PlatformViewsController2Test {
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void getPlatformViewById() { public void getPlatformViewById() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -285,7 +293,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_initializesAndroidView() { public void createPlatformViewMessage_initializesAndroidView() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -307,7 +317,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_setsAndroidViewLayoutDirection() { public void createPlatformViewMessage_setsAndroidViewLayoutDirection() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -331,7 +343,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_throwsIfViewIsNull() { public void createPlatformViewMessage_throwsIfViewIsNull() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -355,7 +369,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createHybridPlatformViewMessage_throwsIfViewIsNull() { public void createHybridPlatformViewMessage_throwsIfViewIsNull() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -379,7 +395,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void setPlatformViewDirection_throwIfPlatformViewNotFound() { public void setPlatformViewDirection_throwIfPlatformViewNotFound() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -413,7 +431,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void disposeAndroidView() { public void disposeAndroidView() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));
@ -454,7 +474,9 @@ public class PlatformViewsController2Test {
@Test @Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class}) @Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void disposeNullAndroidView() { public void disposeNullAndroidView() {
PlatformViewRegistryImpl registryImpl = new PlatformViewRegistryImpl();
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2(); PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();
PlatformViewsController2.setRegistry(registryImpl);
int platformViewId = 0; int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId)); assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));

View File

@ -1665,6 +1665,7 @@ public class PlatformViewsControllerTest {
platformViewsController.attach(context, registry, executor); platformViewsController.attach(context, registry, executor);
PlatformViewsController2 secondController = new PlatformViewsController2(); PlatformViewsController2 secondController = new PlatformViewsController2();
secondController.setRegistry(new PlatformViewRegistryImpl());
final FlutterEngine engine = mock(FlutterEngine.class); final FlutterEngine engine = mock(FlutterEngine.class);
when(engine.getRenderer()).thenReturn(new FlutterRenderer(jni)); when(engine.getRenderer()).thenReturn(new FlutterRenderer(jni));