diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index 049c6d2c0d..817c1c2925 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -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", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterPluginRegistry.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterPluginRegistry.java index f29da3faf9..ebf9b5f3ed 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterPluginRegistry.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterPluginRegistry.java @@ -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 mPluginMap = new LinkedHashMap<>(0); private final List mRequestPermissionsResultListeners = new ArrayList<>(0); private final List 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(); + } } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java index 129274de1a..4fd8a4def4 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/PluginRegistry.java @@ -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}. diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java new file mode 100644 index 0000000000..9b60d1fdbf --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java @@ -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(); +} 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 new file mode 100644 index 0000000000..ac70b1776d --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewFactory.java @@ -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); +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRegistry.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRegistry.java new file mode 100644 index 0000000000..0a45ab7a65 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRegistry.java @@ -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. + *

+ * 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); +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRegistryImpl.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRegistryImpl.java new file mode 100644 index 0000000000..ea1b02c478 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRegistryImpl.java @@ -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 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); + } +} 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 new file mode 100644 index 0000000000..2d10a45d81 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -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. + *

+ * 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 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 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. + } + +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java index c9899ee261..46d8f5cb0d 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java @@ -43,6 +43,7 @@ public class FlutterNativeView implements BinaryMessenger { } public void destroy() { + mPluginRegistry.destroy(); mFlutterView = null; nativeDestroy(mNativePlatformView); mNativePlatformView = 0;