From 00c385f497e34a05751fff5cb383e50e56115ec2 Mon Sep 17 00:00:00 2001 From: amirh Date: Fri, 24 Aug 2018 11:15:14 -0700 Subject: [PATCH] Allow passing extra creation parameters for embedded Android views. (flutter/engine#6081) This allows plugins to pass extra parameters from the Dart side to the platform view constructor. --- .../plugin/platform/PlatformViewFactory.java | 25 +++++++++++++++++-- .../platform/PlatformViewsController.java | 9 ++++++- .../platform/SingleViewPresentation.java | 16 +++++++++--- .../platform/VirtualDisplayController.java | 12 ++++++--- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewFactory.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewFactory.java index 9cb7731e1c..f6fcd35b9c 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewFactory.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewFactory.java @@ -5,13 +5,34 @@ package io.flutter.plugin.platform; import android.content.Context; +import io.flutter.plugin.common.MessageCodec; + +public abstract class PlatformViewFactory { + private final MessageCodec mCreateArgsCodec; + + /** + * + * @param createArgsCodec the codec used to decode the args parameter of {@link #create}. + */ + public PlatformViewFactory(MessageCodec createArgsCodec) { + mCreateArgsCodec = createArgsCodec; + } -public interface PlatformViewFactory { /** * Creates a new Android view to be embedded in the Flutter hierarchy. * * @param context the context to be used when creating the view, this is different than FlutterView's context. * @param viewId unique identifier for the created instance, this value is known on the Dart side. + * @param args arguments sent from the Flutter app. The bytes for this value are decoded using the createArgsCodec + * argument passed to the constructor. This is null if createArgsCodec was null, or no arguments were + * sent from the Flutter app. */ - PlatformView create(Context context, int viewId); + public abstract PlatformView create(Context context, int viewId, Object args); + + /** + * Returns the codec to be used for decoding the args parameter of {@link #create}. + */ + public final MessageCodec getCreateArgsCodec() { + return mCreateArgsCodec; + } } 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 caf04ccdd0..5b5ed193d0 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 @@ -15,6 +15,7 @@ import io.flutter.plugin.common.StandardMethodCodec; import io.flutter.view.FlutterView; import io.flutter.view.TextureRegistry; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -140,6 +141,11 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler return; } + Object createParams = null; + if (args.containsKey("params")) { + createParams = viewFactory.getCreateArgsCodec().decodeMessage(ByteBuffer.wrap((byte[]) args.get("params"))); + } + TextureRegistry.SurfaceTextureEntry textureEntry = mFlutterView.createSurfaceTexture(); VirtualDisplayController vdController = VirtualDisplayController.create( mFlutterView.getContext(), @@ -147,7 +153,8 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler textureEntry.surfaceTexture(), toPhysicalPixels(logicalWidth), toPhysicalPixels(logicalHeight), - id + id, + createParams ); if (vdController == null) { diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java index 04a9114319..733f38819e 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java @@ -58,9 +58,13 @@ class SingleViewPresentation extends Presentation { private final PlatformViewFactory mViewFactory; // This is the view id assigned by the Flutter framework to the embedded view, we keep it here - // so when we create the platform we can tell it its view id. + // so when we create the platform view we can tell it its view id. private int mViewId; + // This is the creation parameters for the platform view, we keep it here + // so when we create the platform view we can tell it its view id. + private Object mCreateParams; + // The root view for the presentation, it has 2 childs: mContainer which contains the embedded view, and // mFakeWindowRootView which contains views that were added directly to the presentation's window manager. private FrameLayout mRootView; @@ -74,10 +78,16 @@ class SingleViewPresentation extends Presentation { * Creates a presentation that will use the view factory to create a new * platform view in the presentation's onCreate, and attach it. */ - public SingleViewPresentation(Context outerContext, Display display, PlatformViewFactory viewFactory, int viewId) { + public SingleViewPresentation( + Context outerContext, + Display display, + PlatformViewFactory viewFactory, + int viewId, + Object createParams) { super(outerContext, display); mViewFactory = viewFactory; mViewId = viewId; + mCreateParams = createParams; mState = new PresentationState(); getWindow().setFlags( WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, @@ -117,7 +127,7 @@ class SingleViewPresentation extends Presentation { PresentationContext context = new PresentationContext(getContext(), mState.mWindowManagerHandler); if (mState.mView == null) { - mState.mView = mViewFactory.create(context, mViewId); + mState.mView = mViewFactory.create(context, mViewId, mCreateParams); } mContainer.addView(mState.mView.getView()); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java index 656246cf5d..4495b7388b 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java @@ -23,7 +23,8 @@ class VirtualDisplayController { SurfaceTexture surfaceTexture, int width, int height, - int viewId + int viewId, + Object createParams ) { surfaceTexture.setDefaultBufferSize(width, height); Surface surface = new Surface(surfaceTexture); @@ -43,7 +44,8 @@ class VirtualDisplayController { return null; } - return new VirtualDisplayController(context, virtualDisplay, viewFactory, surface, surfaceTexture, viewId); + return new VirtualDisplayController( + context, virtualDisplay, viewFactory, surface, surfaceTexture, viewId, createParams); } private final Context mContext; @@ -60,14 +62,16 @@ class VirtualDisplayController { PlatformViewFactory viewFactory, Surface surface, SurfaceTexture surfaceTexture, - int viewId + int viewId, + Object createParams ) { mSurfaceTexture = surfaceTexture; mSurface = surface; mContext = context; mVirtualDisplay = virtualDisplay; mDensityDpi = context.getResources().getDisplayMetrics().densityDpi; - mPresentation = new SingleViewPresentation(context, mVirtualDisplay.getDisplay(), viewFactory, viewId); + mPresentation = new SingleViewPresentation( + context, mVirtualDisplay.getDisplay(), viewFactory, viewId, createParams); mPresentation.show(); }