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.
This commit is contained in:
@@ -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<Object> mCreateArgsCodec;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param createArgsCodec the codec used to decode the args parameter of {@link #create}.
|
||||
*/
|
||||
public PlatformViewFactory(MessageCodec<Object> 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<Object> getCreateArgsCodec() {
|
||||
return mCreateArgsCodec;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user