Skip to content

Commit 223e26b

Browse files
bparrishMinesAdam Harwood
authored andcommitted
[webview_flutter_android] Expose the Java InstanceManager (flutter#6421)
1 parent e737a36 commit 223e26b

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.10.2
2+
3+
* Adds a getter to expose the Java InstanceManager.
4+
15
## 2.10.1
26

37
* Adds a method to the `WebView` wrapper to retrieve the X and Y positions simultaneously.

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InstanceManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
/**
1616
* Maintains instances used to communicate with the corresponding objects in Dart.
1717
*
18+
* <p>Objects stored in this container are represented by an object in Dart that is also stored in
19+
* an InstanceManager with the same identifier.
20+
*
1821
* <p>When an instance is added with an identifier, either can be used to retrieve the other.
1922
*
2023
* <p>Added instances are added as a weak reference and a strong reference. When the strong

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterPlugin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.os.Handler;
99
import android.view.View;
1010
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
1112
import io.flutter.embedding.engine.plugins.FlutterPlugin;
1213
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
1314
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
@@ -166,4 +167,10 @@ private void updateContext(Context context) {
166167
webViewHostApi.setContext(context);
167168
javaScriptChannelHostApi.setPlatformThreadHandler(new Handler(context.getMainLooper()));
168169
}
170+
171+
/** Maintains instances used to communicate with the corresponding objects in Dart. */
172+
@Nullable
173+
public InstanceManager getInstanceManager() {
174+
return instanceManager;
175+
}
169176
}

packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewHostApiImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,9 @@ public void setBackgroundColor(Long instanceId, Long color) {
511511
final WebView webView = (WebView) instanceManager.getInstance(instanceId);
512512
webView.setBackgroundColor(color.intValue());
513513
}
514+
515+
/** Maintains instances used to communicate with the corresponding WebView Dart object. */
516+
public InstanceManager getInstanceManager() {
517+
return instanceManager;
518+
}
514519
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.webviewflutter;
6+
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.mockito.Mockito.when;
9+
10+
import android.content.Context;
11+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
12+
import io.flutter.plugin.common.BinaryMessenger;
13+
import io.flutter.plugin.platform.PlatformViewRegistry;
14+
import org.junit.Rule;
15+
import org.junit.Test;
16+
import org.mockito.Mock;
17+
import org.mockito.junit.MockitoJUnit;
18+
import org.mockito.junit.MockitoRule;
19+
20+
public class WebViewFlutterPluginTest {
21+
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
22+
23+
@Mock Context mockContext;
24+
25+
@Mock BinaryMessenger mockBinaryMessenger;
26+
27+
@Mock PlatformViewRegistry mockViewRegistry;
28+
29+
@Mock FlutterPlugin.FlutterPluginBinding mockPluginBinding;
30+
31+
@Test
32+
public void getInstanceManagerAfterOnAttachedToEngine() {
33+
final WebViewFlutterPlugin webViewFlutterPlugin = new WebViewFlutterPlugin();
34+
35+
when(mockPluginBinding.getApplicationContext()).thenReturn(mockContext);
36+
when(mockPluginBinding.getPlatformViewRegistry()).thenReturn(mockViewRegistry);
37+
when(mockPluginBinding.getBinaryMessenger()).thenReturn(mockBinaryMessenger);
38+
39+
webViewFlutterPlugin.onAttachedToEngine(mockPluginBinding);
40+
41+
assertNotNull(webViewFlutterPlugin.getInstanceManager());
42+
43+
webViewFlutterPlugin.onDetachedFromEngine(mockPluginBinding);
44+
}
45+
}

packages/webview_flutter/webview_flutter_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_android
22
description: A Flutter plugin that provides a WebView widget on Android.
33
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 2.10.1
5+
version: 2.10.2
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)