Platform Views: Android and MethodChannel APIs, and implementation skeleton. (flutter/engine#5684)

To keep the scope of this CL as small of possible I'm leaving the actual
implementation of the platform view mechanics to a following CL.

This CL introduces:
  * A PlatformViewsController class which will be responsible for creating,
    resizing, and disposing platform views.
  * A PlatformViewRegistry which is exposed through the PluginRegistry
    and allows plugins to register factories for platform views.

Android plugin code will add support for a new platform view type by
implementing PlatformViewFactory, and registering a factory with the
registry, e.g:
```java
registrar.platformViewRegistry().registerViewFactory(
  'webview',
  new FlutterWebViewFactory()
);
```
On the Dart side, the framework will ask the engine to create new
platform views by sending a create message over the platformviews method
channel with the unique platform view type id, dimensions, and a unique
id allocated by the framework for the new platform view instance.
The platformviews method channel is also used for resizing and disposing
platform views.
This commit is contained in:
amirh
2018-07-11 14:11:15 -07:00
committed by GitHub
parent d22a5b3c16
commit e0203cb408
9 changed files with 210 additions and 1 deletions

View File

@@ -108,8 +108,8 @@ java_library("flutter_shell_java") {
"io/flutter/plugin/common/BasicMessageChannel.java",
"io/flutter/plugin/common/BinaryCodec.java",
"io/flutter/plugin/common/BinaryMessenger.java",
"io/flutter/plugin/common/FlutterException.java",
"io/flutter/plugin/common/EventChannel.java",
"io/flutter/plugin/common/FlutterException.java",
"io/flutter/plugin/common/JSONMessageCodec.java",
"io/flutter/plugin/common/JSONMethodCodec.java",
"io/flutter/plugin/common/JSONUtil.java",
@@ -124,6 +124,11 @@ java_library("flutter_shell_java") {
"io/flutter/plugin/editing/InputConnectionAdaptor.java",
"io/flutter/plugin/editing/TextInputPlugin.java",
"io/flutter/plugin/platform/PlatformPlugin.java",
"io/flutter/plugin/platform/PlatformView.java",
"io/flutter/plugin/platform/PlatformViewFactory.java",
"io/flutter/plugin/platform/PlatformViewRegistry.java",
"io/flutter/plugin/platform/PlatformViewRegistryImpl.java",
"io/flutter/plugin/platform/PlatformViewsController.java",
"io/flutter/util/PathUtils.java",
"io/flutter/util/Preconditions.java",
"io/flutter/view/AccessibilityBridge.java",

View File

@@ -9,6 +9,8 @@ import android.content.Context;
import android.content.Intent;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformViewRegistry;
import io.flutter.plugin.platform.PlatformViewsController;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
@@ -33,6 +35,7 @@ public class FlutterPluginRegistry
private FlutterNativeView mNativeView;
private FlutterView mFlutterView;
private final PlatformViewsController mPlatformViewsController;
private final Map<String, Object> mPluginMap = new LinkedHashMap<>(0);
private final List<RequestPermissionsResultListener> mRequestPermissionsResultListeners = new ArrayList<>(0);
private final List<ActivityResultListener> mActivityResultListeners = new ArrayList<>(0);
@@ -43,6 +46,7 @@ public class FlutterPluginRegistry
public FlutterPluginRegistry(FlutterNativeView nativeView, Context context) {
mNativeView = nativeView;
mAppContext = context;
mPlatformViewsController = new PlatformViewsController();
}
@Override
@@ -68,9 +72,12 @@ public class FlutterPluginRegistry
public void attach(FlutterView flutterView, Activity activity) {
mFlutterView = flutterView;
mActivity = activity;
mPlatformViewsController.attachFlutterView(flutterView);
}
public void detach() {
mPlatformViewsController.detachFlutterView();
mPlatformViewsController.onFlutterViewDestroyed();
mFlutterView = null;
mActivity = null;
}
@@ -107,6 +114,11 @@ public class FlutterPluginRegistry
return mFlutterView;
}
@Override
public PlatformViewRegistry platformViewRegistry() {
return mPlatformViewsController.getRegistry();
}
@Override
public FlutterView view() {
return mFlutterView;
@@ -225,4 +237,8 @@ public class FlutterPluginRegistry
}
return handled;
}
public void destroy() {
mPlatformViewsController.onFlutterViewDestroyed();
}
}

View File

@@ -7,6 +7,7 @@ package io.flutter.plugin.common;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import io.flutter.plugin.platform.PlatformViewRegistry;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
@@ -99,6 +100,13 @@ public interface PluginRegistry {
*/
TextureRegistry textures();
/**
* Returns the application's {@link PlatformViewRegistry}.
*
* Plugins can use the platform registry to register their view factories.
*/
PlatformViewRegistry platformViewRegistry();
/**
* Returns the {@link FlutterView} that's instantiated by this plugin's
* {@link #activity() activity}.

View File

@@ -0,0 +1,17 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugin.platform;
import android.view.View;
/**
* A handle to an Android view to be embedded in the Flutter hierarchy.
*/
public interface PlatformView {
/**
* Returns the Android view to be embedded in the Flutter hierarchy.
*/
View getView();
}

View File

@@ -0,0 +1,11 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugin.platform;
import android.content.Context;
public interface PlatformViewFactory {
PlatformView create(Context context);
}

View File

@@ -0,0 +1,23 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugin.platform;
/**
* Registry for platform view factories.
* <p>
* Plugins can register factories for specific view types.
*/
public interface PlatformViewRegistry {
/**
* Registers a factory for a platform view.
*
* @param viewTypeId unique identifier for the platform view's type.
* @param factory factory for creating platform views of the specified type.
* @return true if succeeded, false if a factory is already registered for viewTypeId.
*/
boolean registerViewFactory(String viewTypeId, PlatformViewFactory factory);
}

View File

@@ -0,0 +1,30 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugin.platform;
import java.util.HashMap;
import java.util.Map;
class PlatformViewRegistryImpl implements PlatformViewRegistry {
PlatformViewRegistryImpl() {
viewFactories = new HashMap<>();
}
// Maps a platform view type id to its factory.
private final Map<String, PlatformViewFactory> viewFactories;
@Override
public boolean registerViewFactory(String viewTypeId, PlatformViewFactory factory) {
if (viewFactories.containsKey(viewTypeId))
return false;
viewFactories.put(viewTypeId, factory);
return true;
}
PlatformViewFactory getFactory(String viewTypeId) {
return viewFactories.get(viewTypeId);
}
}

View File

@@ -0,0 +1,98 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugin.platform;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.view.FlutterView;
import java.util.HashMap;
import java.util.Map;
/**
* Manages platform views.
* <p>
* Each {@link io.flutter.app.FlutterPluginRegistry} has a single platform views controller.
* A platform views controller can be attached to at most one Flutter view.
*/
public class PlatformViewsController implements MethodChannel.MethodCallHandler {
private static final String CHANNEL_NAME = "flutter/platform_views";
private final PlatformViewRegistryImpl mRegistry;
private FlutterView mFlutterView;
public PlatformViewsController() {
mRegistry = new PlatformViewRegistryImpl();
}
public void attachFlutterView(FlutterView view) {
if (mFlutterView != null)
throw new AssertionError(
"A PlatformViewsController can only be attached to a single FlutterView.\n" +
"attachFlutterView was called while a FlutterView was already attached."
);
mFlutterView = view;
MethodChannel channel = new MethodChannel(view, CHANNEL_NAME, StandardMethodCodec.INSTANCE);
channel.setMethodCallHandler(this);
}
public void detachFlutterView() {
mFlutterView.setMessageHandler(CHANNEL_NAME, null);
mFlutterView = null;
}
public PlatformViewRegistry getRegistry() {
return mRegistry;
}
public void onFlutterViewDestroyed() {
// TODO(amirh): tear down all vd resources.
}
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
switch (call.method) {
case "create":
createPlatformView(call);
break;
case "dispose":
disposePlatformView(call);
break;
case "resize":
resizePlatformView(call);
break;
}
result.success(null);
}
private void createPlatformView(MethodCall call) {
Map<String, Object> args = call.arguments();
int id = (int) args.get("id");
String viewType = (String) args.get("viewType");
double width = (double) args.get("width");
double height = (double) args.get("height");
// TODO(amirh): implement this.
}
private void disposePlatformView(MethodCall call) {
int id = (int) call.arguments();
// TODO(amirh): implement this.
}
private void resizePlatformView(MethodCall call) {
Map<String, Object> args = call.arguments();
int id = (int) args.get("id");
double width = (double) args.get("width");
double height = (double) args.get("height");
// TODO(amirh): implement this.
}
}

View File

@@ -43,6 +43,7 @@ public class FlutterNativeView implements BinaryMessenger {
}
public void destroy() {
mPluginRegistry.destroy();
mFlutterView = null;
nativeDestroy(mNativePlatformView);
mNativePlatformView = 0;