diff --git a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md index 910d20dda6d1..b37ada2225f0 100644 --- a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.10.2 + +* Adds a getter to expose the Java InstanceManager. + ## 2.10.1 * Adds a method to the `WebView` wrapper to retrieve the X and Y positions simultaneously. diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InstanceManager.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InstanceManager.java index 306dc20be47d..fefd577ee9b5 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InstanceManager.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InstanceManager.java @@ -15,6 +15,9 @@ /** * Maintains instances used to communicate with the corresponding objects in Dart. * + *
Objects stored in this container are represented by an object in Dart that is also stored in + * an InstanceManager with the same identifier. + * *
When an instance is added with an identifier, either can be used to retrieve the other. * *
Added instances are added as a weak reference and a strong reference. When the strong diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterPlugin.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterPlugin.java index 8db976a26937..fe7615c664a4 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterPlugin.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterPlugin.java @@ -8,6 +8,7 @@ import android.os.Handler; import android.view.View; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.embedding.engine.plugins.activity.ActivityAware; import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; @@ -166,4 +167,10 @@ private void updateContext(Context context) { webViewHostApi.setContext(context); javaScriptChannelHostApi.setPlatformThreadHandler(new Handler(context.getMainLooper())); } + + /** Maintains instances used to communicate with the corresponding objects in Dart. */ + @Nullable + public InstanceManager getInstanceManager() { + return instanceManager; + } } diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java index f257ace71b0d..778ad611d05f 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java @@ -511,4 +511,9 @@ public void setBackgroundColor(Long instanceId, Long color) { final WebView webView = (WebView) instanceManager.getInstance(instanceId); webView.setBackgroundColor(color.intValue()); } + + /** Maintains instances used to communicate with the corresponding WebView Dart object. */ + public InstanceManager getInstanceManager() { + return instanceManager; + } } diff --git a/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterPluginTest.java b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterPluginTest.java new file mode 100644 index 000000000000..16dc6cf5de2b --- /dev/null +++ b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterPluginTest.java @@ -0,0 +1,45 @@ +// Copyright 2013 The Flutter 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.plugins.webviewflutter; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.when; + +import android.content.Context; +import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.plugin.common.BinaryMessenger; +import io.flutter.plugin.platform.PlatformViewRegistry; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +public class WebViewFlutterPluginTest { + @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); + + @Mock Context mockContext; + + @Mock BinaryMessenger mockBinaryMessenger; + + @Mock PlatformViewRegistry mockViewRegistry; + + @Mock FlutterPlugin.FlutterPluginBinding mockPluginBinding; + + @Test + public void getInstanceManagerAfterOnAttachedToEngine() { + final WebViewFlutterPlugin webViewFlutterPlugin = new WebViewFlutterPlugin(); + + when(mockPluginBinding.getApplicationContext()).thenReturn(mockContext); + when(mockPluginBinding.getPlatformViewRegistry()).thenReturn(mockViewRegistry); + when(mockPluginBinding.getBinaryMessenger()).thenReturn(mockBinaryMessenger); + + webViewFlutterPlugin.onAttachedToEngine(mockPluginBinding); + + assertNotNull(webViewFlutterPlugin.getInstanceManager()); + + webViewFlutterPlugin.onDetachedFromEngine(mockPluginBinding); + } +} diff --git a/packages/webview_flutter/webview_flutter_android/pubspec.yaml b/packages/webview_flutter/webview_flutter_android/pubspec.yaml index 62f4cc97aa06..f5f2e3c34e69 100644 --- a/packages/webview_flutter/webview_flutter_android/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter_android/pubspec.yaml @@ -2,7 +2,7 @@ name: webview_flutter_android description: A Flutter plugin that provides a WebView widget on Android. repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_android issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22 -version: 2.10.1 +version: 2.10.2 environment: sdk: ">=2.14.0 <3.0.0"