Skip to content

Commit

Permalink
Allow passing extra creation parameters for embedded Android views. (f…
Browse files Browse the repository at this point in the history
…lutter#6081)

This allows plugins to pass extra parameters from the Dart side to the
platform view constructor.
  • Loading branch information
amirh authored Aug 24, 2018
1 parent 78e74d4 commit 0914926
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
public abstract PlatformView create(Context context, int viewId, Object args);

/**
* Returns the codec to be used for decoding the args parameter of {@link #create}.
*/
PlatformView create(Context context, int viewId);
public final MessageCodec<Object> getCreateArgsCodec() {
return mCreateArgsCodec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -140,14 +141,20 @@ private void createPlatformView(MethodCall call, MethodChannel.Result result) {
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(),
viewFactory,
textureEntry.surfaceTexture(),
toPhysicalPixels(logicalWidth),
toPhysicalPixels(logicalHeight),
id
id,
createParams
);

if (vdController == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ static class PresentationState {
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;
Expand All @@ -74,10 +78,16 @@ static class PresentationState {
* 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,
Expand Down Expand Up @@ -117,7 +127,7 @@ protected void onCreate(Bundle savedInstanceState) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static VirtualDisplayController create(
SurfaceTexture surfaceTexture,
int width,
int height,
int viewId
int viewId,
Object createParams
) {
surfaceTexture.setDefaultBufferSize(width, height);
Surface surface = new Surface(surfaceTexture);
Expand All @@ -43,7 +44,8 @@ public static VirtualDisplayController create(
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;
Expand All @@ -60,14 +62,16 @@ private 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();
}

Expand Down

0 comments on commit 0914926

Please sign in to comment.