diff --git a/packages/webview_flutter/CHANGELOG.md b/packages/webview_flutter/CHANGELOG.md index 6d2b4bb26815..68d621e4fedb 100644 --- a/packages/webview_flutter/CHANGELOG.md +++ b/packages/webview_flutter/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.3 + +* Add common support for Android WebView initializing(`FlutterWebView.setCommonIniter()`) [flutter/issues/70731](https://github.com/flutter/flutter/issues/70731). + ## 2.0.2 * Fixes bug where text fields are hidden behind the keyboard diff --git a/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java b/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java index 022f1c3597e7..080a3f618220 100644 --- a/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java +++ b/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebView.java @@ -29,6 +29,20 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames"; + private static FlutterWebViewIniter commonIniter; + + /** + * Set common initializing logic for all Flutter WebView's. When the WebView is created and the + * original initialization is done, {@link FlutterWebViewIniter#initWebView(WebView)} would be + * called. + * + *

Suggestion: you can call this method in {@link android.app.Application} to avoid memory + * leak. + */ + public static void setCommonIniter(FlutterWebViewIniter initer) { + FlutterWebView.commonIniter = initer; + } + private final WebView webView; private final MethodChannel methodChannel; private final FlutterWebViewClient flutterWebViewClient; @@ -128,6 +142,7 @@ public void onProgressChanged(WebView view, int progress) { String userAgent = (String) params.get("userAgent"); updateUserAgent(userAgent); } + if (commonIniter != null) commonIniter.initWebView(webView); if (params.containsKey("initialUrl")) { String url = (String) params.get("initialUrl"); webView.loadUrl(url); diff --git a/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebViewIniter.java b/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebViewIniter.java new file mode 100644 index 000000000000..e8c8c585b549 --- /dev/null +++ b/packages/webview_flutter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterWebViewIniter.java @@ -0,0 +1,23 @@ +// Copyright 2019 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.plugins.webviewflutter; + +import android.webkit.WebView; + +/** + * Interface to initialize Flutter WebView in native Android code. + * + *

Why? In most cases, an APP has custom COMMON WebView initialization logic, like {@link + * android.webkit.WebSettings#setBuiltInZoomControls(boolean)}, {@link + * android.webkit.WebSettings#setMixedContentMode(int)}, {@link + * android.webkit.WebSettings#setTextZoom(int)} etc. + * + *

Adding all these settings to Flutter WebView params is tedious, and unnecessary mostly, so we + * can put these initialization on our APP's self native code as you like. + */ +public interface FlutterWebViewIniter { + /** How to initialize WebView */ + void initWebView(WebView webView); +} diff --git a/packages/webview_flutter/example/android/app/src/main/AndroidManifest.xml b/packages/webview_flutter/example/android/app/src/main/AndroidManifest.xml index f895f92bd7a4..5fdab00fba4a 100644 --- a/packages/webview_flutter/example/android/app/src/main/AndroidManifest.xml +++ b/packages/webview_flutter/example/android/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ + android:name=".App"> diff --git a/packages/webview_flutter/example/android/app/src/main/java/io/flutter/plugins/webviewflutterexample/App.java b/packages/webview_flutter/example/android/app/src/main/java/io/flutter/plugins/webviewflutterexample/App.java new file mode 100644 index 000000000000..37190fe4eabc --- /dev/null +++ b/packages/webview_flutter/example/android/app/src/main/java/io/flutter/plugins/webviewflutterexample/App.java @@ -0,0 +1,28 @@ +package io.flutter.plugins.webviewflutterexample; + +import android.os.Build; +import android.webkit.WebSettings; +import android.webkit.WebView; +import io.flutter.app.FlutterApplication; +import io.flutter.plugins.webviewflutter.FlutterWebView; +import io.flutter.plugins.webviewflutter.FlutterWebViewIniter; + +public class App extends FlutterApplication implements FlutterWebViewIniter { + @Override + public void onCreate() { + super.onCreate(); + + FlutterWebView.setCommonIniter(this); + } + + @Override + public void initWebView(WebView webView) { + WebSettings settings = webView.getSettings(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); + } + settings.setSupportZoom(true); + settings.setBuiltInZoomControls(true); + settings.setDisplayZoomControls(false); + } +} diff --git a/packages/webview_flutter/pubspec.yaml b/packages/webview_flutter/pubspec.yaml index 6ee9e119bd3a..a89ded4e9014 100644 --- a/packages/webview_flutter/pubspec.yaml +++ b/packages/webview_flutter/pubspec.yaml @@ -1,7 +1,7 @@ name: webview_flutter description: A Flutter plugin that provides a WebView widget on Android and iOS. homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter -version: 2.0.2 +version: 2.0.3 environment: sdk: ">=2.12.0-259.9.beta <3.0.0"