added android willSuppressErrorPage and algorithmicDarkeningAllowed settings

This commit is contained in:
Lorenzo Pichilli 2022-10-07 01:45:15 +02:00
parent 80b8bde10a
commit 22ae9deac6
6 changed files with 94 additions and 2 deletions

View File

@ -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

View File

@ -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<String, Map<String, Object>> 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<String, Object> 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;
}

View File

@ -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<InAppWebViewInterface> {
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<InAppWebViewInterface> {
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<InAppWebViewInterface> {
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<String, Object> getRealSettings(@NonNull InAppWebViewInterface inAppWebView) {
@ -546,6 +558,12 @@ public class InAppWebViewSettings implements ISettings<InAppWebViewInterface> {
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;
}

View File

@ -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`.

View File

@ -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<WebViewFeature> 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<AndroidWebViewFeature> 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.

View File

@ -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 WebViews 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) {