From 22ae9deac651512a12cf9dfb42459b52c151e007 Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Fri, 7 Oct 2022 01:45:15 +0200 Subject: [PATCH] added android willSuppressErrorPage and algorithmicDarkeningAllowed settings --- CHANGELOG.md | 2 +- .../webview/in_app_webview/InAppWebView.java | 23 +++++++++++++ .../in_app_webview/InAppWebViewSettings.java | 18 ++++++++++ lib/src/android/webview_feature.dart | 10 +++++- lib/src/android/webview_feature.g.dart | 10 ++++++ .../in_app_webview_settings.dart | 33 +++++++++++++++++++ 6 files changed, 94 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e1aff56..6f4b7a56 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Added `PrintJobController` to manage print jobs - Added `WebAuthenticationSession` for iOS - Added `pauseAllMediaPlayback`, `setAllMediaPlaybackSuspended`, `closeAllMediaPresentations`, `requestMediaPlaybackState`, `isInFullscreen`, `getCameraCaptureState`, `setCameraCaptureState`, `getMicrophoneCaptureState`, `setMicrophoneCaptureState` WebView controller methods -- Added `underPageBackgroundColor`, `isTextInteractionEnabled`, `isSiteSpecificQuirksModeEnabled`, `upgradeKnownHostsToHTTPS`, `forceDarkStrategy` WebView settings +- Added `underPageBackgroundColor`, `isTextInteractionEnabled`, `isSiteSpecificQuirksModeEnabled`, `upgradeKnownHostsToHTTPS`, `forceDarkStrategy`, `willSuppressErrorPage`, `algorithmicDarkeningAllowed` WebView settings - Added `onCameraCaptureStateChanged`, `onMicrophoneCaptureStateChanged` WebView events - Added support for `onPermissionRequest` event on iOS 15.0+ - Added `debugLoggingSettings` static property for WebView and ChromeSafariBrowser diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java index 33846277..f33d73b2 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java @@ -59,6 +59,7 @@ import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin; import com.pichillilorenzo.flutter_inappwebview.print_job.PrintJobController; import com.pichillilorenzo.flutter_inappwebview.print_job.PrintJobManager; import com.pichillilorenzo.flutter_inappwebview.print_job.PrintJobSettings; +import com.pichillilorenzo.flutter_inappwebview.types.HitTestResult; import com.pichillilorenzo.flutter_inappwebview.webview.JavaScriptBridgeInterface; import com.pichillilorenzo.flutter_inappwebview.R; import com.pichillilorenzo.flutter_inappwebview.Util; @@ -196,6 +197,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie } } + @SuppressLint("RestrictedApi") public void prepare() { httpClient = new OkHttpClient().newBuilder().build(); @@ -379,6 +381,14 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie customSettings.rendererPriorityPolicy.put("waivedWhenNotVisible", getRendererPriorityWaivedWhenNotVisible()); } + if (WebViewFeature.isFeatureSupported(WebViewFeature.SUPPRESS_ERROR_PAGE)) { + WebSettingsCompat.setWillSuppressErrorPage(settings, customSettings.willSuppressErrorPage); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + settings.setAlgorithmicDarkeningAllowed(customSettings.algorithmicDarkeningAllowed); + } + contentBlockerHandler.getRuleList().clear(); for (Map> contentBlocker : customSettings.contentBlockers) { // compile ContentBlockerTrigger urlFilter @@ -690,6 +700,7 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie }); } + @SuppressLint("RestrictedApi") public void setSettings(InAppWebViewSettings newCustomSettings, HashMap newSettingsMap) { WebSettings settings = getSettings(); @@ -1013,6 +1024,18 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie setHorizontalScrollbarTrackDrawable(new ColorDrawable(Color.parseColor(newCustomSettings.horizontalScrollbarTrackColor))); } + if (newSettingsMap.get("willSuppressErrorPage") != null && + !Util.objEquals(customSettings.willSuppressErrorPage, newCustomSettings.willSuppressErrorPage) && + WebViewFeature.isFeatureSupported(WebViewFeature.SUPPRESS_ERROR_PAGE)) { + WebSettingsCompat.setWillSuppressErrorPage(settings, newCustomSettings.willSuppressErrorPage); + } + + if (newSettingsMap.get("algorithmicDarkeningAllowed") != null && + !Util.objEquals(customSettings.algorithmicDarkeningAllowed, newCustomSettings.algorithmicDarkeningAllowed) && + Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + settings.setAlgorithmicDarkeningAllowed(newCustomSettings.algorithmicDarkeningAllowed); + } + customSettings = newCustomSettings; } diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewSettings.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewSettings.java index e82394bc..88f510da 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewSettings.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewSettings.java @@ -1,5 +1,6 @@ package com.pichillilorenzo.flutter_inappwebview.webview.in_app_webview; +import android.annotation.SuppressLint; import android.os.Build; import android.view.View; import android.webkit.WebSettings; @@ -112,6 +113,8 @@ public class InAppWebViewSettings implements ISettings { public String horizontalScrollbarThumbColor; @Nullable public String horizontalScrollbarTrackColor; + public Boolean willSuppressErrorPage = false; + public Boolean algorithmicDarkeningAllowed = false; @NonNull @Override @@ -370,6 +373,12 @@ public class InAppWebViewSettings implements ISettings { case "horizontalScrollbarTrackColor": horizontalScrollbarTrackColor = (String) value; break; + case "willSuppressErrorPage": + willSuppressErrorPage = (Boolean) value; + break; + case "algorithmicDarkeningAllowed": + algorithmicDarkeningAllowed = (Boolean) value; + break; } } @@ -462,9 +471,12 @@ public class InAppWebViewSettings implements ISettings { settings.put("verticalScrollbarTrackColor", verticalScrollbarTrackColor); settings.put("horizontalScrollbarThumbColor", horizontalScrollbarThumbColor); settings.put("horizontalScrollbarTrackColor", horizontalScrollbarTrackColor); + settings.put("willSuppressErrorPage", willSuppressErrorPage); + settings.put("algorithmicDarkeningAllowed", algorithmicDarkeningAllowed); return settings; } + @SuppressLint("RestrictedApi") @NonNull @Override public Map getRealSettings(@NonNull InAppWebViewInterface inAppWebView) { @@ -546,6 +558,12 @@ public class InAppWebViewSettings implements ISettings { rendererPriorityPolicy.put("waivedWhenNotVisible", webView.getRendererPriorityWaivedWhenNotVisible()); realSettings.put("rendererPriorityPolicy", rendererPriorityPolicy); } + if (WebViewFeature.isFeatureSupported(WebViewFeature.SUPPRESS_ERROR_PAGE)) { + realSettings.put("willSuppressErrorPage", WebSettingsCompat.willSuppressErrorPage(settings)); + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + realSettings.put("algorithmicDarkeningAllowed", settings.isAlgorithmicDarkeningAllowed()); + } } return realSettings; } diff --git a/lib/src/android/webview_feature.dart b/lib/src/android/webview_feature.dart index 73a2cb01..423454fa 100644 --- a/lib/src/android/webview_feature.dart +++ b/lib/src/android/webview_feature.dart @@ -190,7 +190,11 @@ class WebViewFeature_ { ///This feature covers [UserScriptInjectionTime.AT_DOCUMENT_START]. static const DOCUMENT_START_SCRIPT = - const AndroidWebViewFeature_._internal("DOCUMENT_START_SCRIPT"); + const WebViewFeature_._internal("DOCUMENT_START_SCRIPT"); + + ///This feature covers [InAppWebViewSettings.willSuppressErrorPage]. + static const SUPPRESS_ERROR_PAGE = + const WebViewFeature_._internal("SUPPRESS_ERROR_PAGE"); ///Return whether a feature is supported at run-time. On devices running Android version `Build.VERSION_CODES.LOLLIPOP` and higher, ///this will check whether a feature is supported, depending on the combination of the desired feature, the Android version of device, @@ -395,6 +399,10 @@ class AndroidWebViewFeature_ { static const DOCUMENT_START_SCRIPT = const AndroidWebViewFeature_._internal("DOCUMENT_START_SCRIPT"); + ///This feature covers [InAppWebViewSettings.willSuppressErrorPage]. + static const SUPPRESS_ERROR_PAGE = + const AndroidWebViewFeature_._internal("SUPPRESS_ERROR_PAGE"); + ///Return whether a feature is supported at run-time. On devices running Android version `Build.VERSION_CODES.LOLLIPOP` and higher, ///this will check whether a feature is supported, depending on the combination of the desired feature, the Android version of device, ///and the WebView APK on the device. If running on a device with a lower API level, this will always return `false`. diff --git a/lib/src/android/webview_feature.g.dart b/lib/src/android/webview_feature.g.dart index 91520b07..d227a530 100644 --- a/lib/src/android/webview_feature.g.dart +++ b/lib/src/android/webview_feature.g.dart @@ -193,6 +193,10 @@ class WebViewFeature { static const DOCUMENT_START_SCRIPT = WebViewFeature._internal( 'DOCUMENT_START_SCRIPT', 'DOCUMENT_START_SCRIPT'); + ///This feature covers [InAppWebViewSettings.willSuppressErrorPage]. + static const SUPPRESS_ERROR_PAGE = + WebViewFeature._internal('SUPPRESS_ERROR_PAGE', 'SUPPRESS_ERROR_PAGE'); + ///Set of all values of [WebViewFeature]. static final Set values = [ WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL, @@ -237,6 +241,7 @@ class WebViewFeature { WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, WebViewFeature.WEB_VIEW_RENDERER_TERMINATE, WebViewFeature.DOCUMENT_START_SCRIPT, + WebViewFeature.SUPPRESS_ERROR_PAGE, ].toSet(); ///Gets a possible [WebViewFeature] instance from [String] value. @@ -488,6 +493,10 @@ class AndroidWebViewFeature { static const DOCUMENT_START_SCRIPT = AndroidWebViewFeature._internal( 'DOCUMENT_START_SCRIPT', 'DOCUMENT_START_SCRIPT'); + ///This feature covers [InAppWebViewSettings.willSuppressErrorPage]. + static const SUPPRESS_ERROR_PAGE = AndroidWebViewFeature._internal( + 'SUPPRESS_ERROR_PAGE', 'SUPPRESS_ERROR_PAGE'); + ///Set of all values of [AndroidWebViewFeature]. static final Set values = [ AndroidWebViewFeature.CREATE_WEB_MESSAGE_CHANNEL, @@ -532,6 +541,7 @@ class AndroidWebViewFeature { AndroidWebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE, AndroidWebViewFeature.WEB_VIEW_RENDERER_TERMINATE, AndroidWebViewFeature.DOCUMENT_START_SCRIPT, + AndroidWebViewFeature.SUPPRESS_ERROR_PAGE, ].toSet(); ///Gets a possible [AndroidWebViewFeature] instance from [String] value. diff --git a/lib/src/in_app_webview/in_app_webview_settings.dart b/lib/src/in_app_webview/in_app_webview_settings.dart index 8ccb6ba5..38c2d95e 100755 --- a/lib/src/in_app_webview/in_app_webview_settings.dart +++ b/lib/src/in_app_webview/in_app_webview_settings.dart @@ -9,6 +9,7 @@ import '../types/main.dart'; import '../util.dart'; import '../in_app_browser/in_app_browser_settings.dart'; import 'webview.dart'; +import '../android/webview_feature.dart'; ///This class represents all the WebView settings available. class InAppWebViewSettings { @@ -648,6 +649,32 @@ class InAppWebViewSettings { ///- Android native WebView Color? horizontalScrollbarTrackColor; + ///Sets whether the WebView’s internal error page should be suppressed or displayed for bad navigations. + ///`true` means suppressed (not shown), `false` means it will be displayed. The default value is `false`. + /// + ///**NOTE**: available on Android only if [WebViewFeature.SUPPRESS_ERROR_PAGE] feature is supported. + /// + ///**Supported Platforms/Implementations**: + ///- Android native WebView + bool willSuppressErrorPage; + + ///Control whether algorithmic darkening is allowed. + /// + ///WebView always sets the media query `prefers-color-scheme` according to the app's theme attribute `isLightTheme`, + ///i.e. `prefers-color-scheme` is light if `isLightTheme` is `true` or not specified, otherwise it is `dark`. + ///This means that the web content's light or dark style will be applied automatically to match the app's theme if the content supports it. + /// + ///Algorithmic darkening is disallowed by default. + /// + ///If the app's theme is dark and it allows algorithmic darkening, + ///WebView will attempt to darken web content using an algorithm, + ///if the content doesn't define its own dark styles and doesn't explicitly disable darkening. + /// + ///If Android is applying Force Dark to WebView then WebView will ignore the value of this setting and behave as if it were set to true. + /// + ///**NOTE**: available on Android 33+ + bool algorithmicDarkeningAllowed; + ///Set to `true` to disable the bouncing of the WebView when the scrolling has reached an edge of the content. The default value is `false`. /// ///**Supported Platforms/Implementations**: @@ -1122,6 +1149,8 @@ class InAppWebViewSettings { this.verticalScrollbarTrackColor, this.horizontalScrollbarThumbColor, this.horizontalScrollbarTrackColor, + this.willSuppressErrorPage = false, + this.algorithmicDarkeningAllowed = false, this.disallowOverScroll = false, this.enableViewportScale = false, this.suppressesIncrementalRendering = false, @@ -1268,6 +1297,8 @@ class InAppWebViewSettings { "verticalScrollbarTrackColor": verticalScrollbarTrackColor?.toHex(), "horizontalScrollbarThumbColor": horizontalScrollbarThumbColor?.toHex(), "horizontalScrollbarTrackColor": horizontalScrollbarTrackColor?.toHex(), + "willSuppressErrorPage": willSuppressErrorPage, + "algorithmicDarkeningAllowed": algorithmicDarkeningAllowed, "disallowOverScroll": disallowOverScroll, "enableViewportScale": enableViewportScale, "suppressesIncrementalRendering": suppressesIncrementalRendering, @@ -1451,6 +1482,8 @@ class InAppWebViewSettings { UtilColor.fromHex(map["horizontalScrollbarThumbColor"]); settings.horizontalScrollbarTrackColor = UtilColor.fromHex(map["horizontalScrollbarTrackColor"]); + settings.willSuppressErrorPage = map["willSuppressErrorPage"]; + settings.algorithmicDarkeningAllowed = map["algorithmicDarkeningAllowed"]; } else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {