diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java index cd22f732d8..71a93c0a04 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java @@ -103,6 +103,10 @@ public class FlutterEngine { platformViewsController.onPreEngineRestart(); } + + public void onDisplayPlatformView(int viewId, int x, int y, int width, int height) { + platformViewsController.onDisplayPlatformView(viewId, x, y, width, height); + } }; /** @@ -207,6 +211,7 @@ public class FlutterEngine { flutterLoader.ensureInitializationComplete(context, dartVmArgs); flutterJNI.addEngineLifecycleListener(engineLifecycleListener); + flutterJNI.setPlatformViewsController(platformViewsController); attachToJni(); this.dartExecutor = new DartExecutor(flutterJNI, context.getAssets()); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index b0d5ad9ba1..478db5b909 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -22,6 +22,7 @@ import io.flutter.embedding.engine.dart.PlatformMessageHandler; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; import io.flutter.embedding.engine.renderer.RenderSurface; import io.flutter.plugin.common.StandardMessageCodec; +import io.flutter.plugin.platform.PlatformViewsController; import io.flutter.view.AccessibilityBridge; import io.flutter.view.FlutterCallbackInformation; import java.nio.ByteBuffer; @@ -168,6 +169,7 @@ public class FlutterJNI { @Nullable private Long nativePlatformViewId; @Nullable private AccessibilityDelegate accessibilityDelegate; @Nullable private PlatformMessageHandler platformMessageHandler; + @Nullable private PlatformViewsController platformViewsController; @NonNull private final Set engineLifecycleListeners = new CopyOnWriteArraySet<>(); @@ -415,6 +417,12 @@ public class FlutterJNI { long nativePlatformViewId, @NonNull ByteBuffer buffer, int position); // ------ End Touch Interaction Support --- + @UiThread + public void setPlatformViewsController(@NonNull PlatformViewsController platformViewsController) { + ensureRunningOnMainThread(); + this.platformViewsController = platformViewsController; + } + // ------ Start Accessibility Support ----- /** * Sets the {@link AccessibilityDelegate} for the attached Flutter context. @@ -782,6 +790,17 @@ public class FlutterJNI { } // ----- End Engine Lifecycle Support ---- + // @SuppressWarnings("unused") + @UiThread + public void onDisplayPlatformView(int viewId, int x, int y, int width, int height) { + ensureRunningOnMainThread(); + if (platformViewsController == null) { + throw new RuntimeException( + "platformViewsController must be set before attempting to position a platform view"); + } + platformViewsController.onDisplayPlatformView(viewId, x, y, width, height); + } + // TODO(mattcarroll): determine if this is nonull or nullable @UiThread public Bitmap getBitmap() { diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index bc36210e4c..fad763e339 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -533,4 +533,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega } vdControllers.clear(); } + + public void onDisplayPlatformView(int viewId, int x, int y, int width, int height) { + // TODO: Implement this method. https://github.com/flutter/flutter/issues/58288 + } } diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc index 4f6f9258b3..81a6caad1a 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc @@ -118,6 +118,19 @@ void FlutterViewOnPreEngineRestart(JNIEnv* env, jobject obj) { FML_CHECK(CheckException(env)); } +static jmethodID g_on_display_platform_view_method = nullptr; +void FlutterViewOnDisplayPlatformView(JNIEnv* env, + jobject obj, + jint view_id, + jint x, + jint y, + jint width, + jint height) { + env->CallVoidMethod(obj, g_on_display_platform_view_method, view_id, x, y, + width, height); + FML_CHECK(CheckException(env)); +} + static jmethodID g_attach_to_gl_context_method = nullptr; void SurfaceTextureAttachToGLContext(JNIEnv* env, jobject obj, jint textureId) { env->CallVoidMethod(obj, g_attach_to_gl_context_method, textureId); @@ -750,6 +763,14 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { return false; } + g_on_display_platform_view_method = env->GetMethodID( + g_flutter_jni_class->obj(), "onDisplayPlatformView", "(IIIII)V"); + + if (g_on_display_platform_view_method == nullptr) { + FML_LOG(ERROR) << "Could not locate onDisplayPlatformView method"; + return false; + } + g_surface_texture_class = new fml::jni::ScopedJavaGlobalRef( env, env->FindClass("android/graphics/SurfaceTexture")); if (g_surface_texture_class->is_null()) { diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni.h b/engine/src/flutter/shell/platform/android/platform_view_android_jni.h index 07c70b2a45..199af74bd6 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni.h +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni.h @@ -36,6 +36,14 @@ void FlutterViewOnFirstFrame(JNIEnv* env, jobject obj); void FlutterViewOnPreEngineRestart(JNIEnv* env, jobject obj); +void FlutterViewOnDisplayPlatformView(JNIEnv* env, + jobject obj, + jint view_id, + jint x, + jint y, + jint width, + jint height); + void SurfaceTextureAttachToGLContext(JNIEnv* env, jobject obj, jint textureId); void SurfaceTextureUpdateTexImage(JNIEnv* env, jobject obj); diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java index 98b8660683..03f5dc38db 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java @@ -1,8 +1,12 @@ package io.flutter.embedding.engine; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; +import io.flutter.plugin.platform.PlatformViewsController; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.junit.runner.RunWith; @@ -44,4 +48,21 @@ public class FlutterJNITest { // --- Verify Results --- assertEquals(1, callbackInvocationCount.get()); } + + @Test + public void onDisplayPlatformView__callsPlatformViewsController() { + PlatformViewsController platformViewsController = mock(PlatformViewsController.class); + + FlutterJNI flutterJNI = new FlutterJNI(); + flutterJNI.setPlatformViewsController(platformViewsController); + + // --- Execute Test --- + flutterJNI.onDisplayPlatformView( + /*viewId=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200); + + // --- Verify Results --- + verify(platformViewsController, times(1)) + .onDisplayPlatformView( + /*viewId=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200); + } }