WebViewCompat
public
class
WebViewCompat
extends Object
| java.lang.Object | |
| ↳ | androidx.webkit.WebViewCompat |
Compatibility version of WebView
Summary
Nested classes | ||
|---|---|---|
interface |
WebViewCompat.VisualStateCallback
Callback interface supplied to |
|
Public methods | |
|---|---|
static
WebMessagePortCompat[]
|
createWebMessageChannel(WebView webview)
Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel. |
static
PackageInfo
|
getCurrentWebViewPackage(Context context)
If WebView has already been loaded into the current process this method will return the package that was used to load it. |
static
Uri
|
getSafeBrowsingPrivacyPolicyUrl()
Returns a URL pointing to the privacy policy for Safe Browsing reporting. |
static
void
|
postVisualStateCallback(WebView webview, long requestId, WebViewCompat.VisualStateCallback callback)
Posts a |
static
void
|
postWebMessage(WebView webview, WebMessageCompat message, Uri targetOrigin)
Post a message to main frame. |
static
void
|
setSafeBrowsingWhitelist(List<String> hosts, ValueCallback<Boolean> callback)
Sets the list of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks. |
static
void
|
startSafeBrowsing(Context context, ValueCallback<Boolean> callback)
Starts Safe Browsing initialization. |
Inherited methods | |
|---|---|
Public methods
createWebMessageChannel
public static WebMessagePortCompat[] createWebMessageChannel (WebView webview)
Creates a message channel to communicate with JS and returns the message ports that represent the endpoints of this message channel. The HTML5 message channel functionality is described here
The returned message channels are entangled and already in started state.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.CREATE_WEB_MESSAGE_CHANNEL.
| Parameters | |
|---|---|
webview |
WebView |
| Returns | |
|---|---|
WebMessagePortCompat[] |
an array of size two, containing the two message ports that form the message channel. |
getCurrentWebViewPackage
public static PackageInfo getCurrentWebViewPackage (Context context)
If WebView has already been loaded into the current process this method will return the package that was used to load it. Otherwise, the package that would be used if the WebView was loaded right now will be returned; this does not cause WebView to be loaded, so this information may become outdated at any time. The WebView package changes either when the current WebView package is updated, disabled, or uninstalled. It can also be changed through a Developer Setting. If the WebView package changes, any app process that has loaded WebView will be killed. The next time the app starts and loads WebView it will use the new WebView package instead.
| Parameters | |
|---|---|
context |
Context |
| Returns | |
|---|---|
PackageInfo |
the current WebView package, or null if there is none.
|
getSafeBrowsingPrivacyPolicyUrl
public static Uri getSafeBrowsingPrivacyPolicyUrl ()
Returns a URL pointing to the privacy policy for Safe Browsing reporting.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.SAFE_BROWSING_PRIVACY_POLICY_URL.
| Returns | |
|---|---|
Uri |
the url pointing to a privacy policy document which can be displayed to users. |
postVisualStateCallback
public static void postVisualStateCallback (WebView webview, long requestId, WebViewCompat.VisualStateCallback callback)
Posts a WebViewCompat.VisualStateCallback, which will be called when
the current state of the WebView is ready to be drawn.
Because updates to the DOM are processed asynchronously, updates to the DOM may not
immediately be reflected visually by subsequent WebView.onDraw(Canvas) invocations. The
WebViewCompat.VisualStateCallback provides a mechanism to notify the caller when the contents
of the DOM at the current time are ready to be drawn the next time the WebView draws.
The next draw after the callback completes is guaranteed to reflect all the updates to the
DOM up to the point at which the WebViewCompat.VisualStateCallback was posted, but it may
also contain updates applied after the callback was posted.
The state of the DOM covered by this API includes the following:
- primitive HTML elements (div, img, span, etc..)
- images
- CSS animations
- WebGL
- canvas
- the video tag
To guarantee that the WebView will successfully render the first frame
after the WebViewCompat.VisualStateCallback.onComplete(long) method has been called a set of
conditions must be met:
- If the
WebView's visibility is set toVISIBLEthen * theWebViewmust be attached to the view hierarchy. - If the
WebView's visibility is set toINVISIBLEthen theWebViewmust be attached to the view hierarchy and must be madeVISIBLEfrom theWebViewCompat.VisualStateCallback.onComplete(long)method. - If the
WebView's visibility is set toGONEthen theWebViewmust be attached to the view hierarchy and itsLayoutParams's width and height need to be set to fixed values and must be madeVISIBLEfrom theWebViewCompat.VisualStateCallback.onComplete(long)method.
When using this API it is also recommended to enable pre-rasterization if the WebView is off screen to avoid flickering. See
WebSettings.setOffscreenPreRaster(boolean) for more details and do consider its
caveats.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.VISUAL_STATE_CALLBACK.
| Parameters | |
|---|---|
webview |
WebView |
requestId |
long: An id that will be returned in the callback to allow callers to match
requests with callbacks. |
callback |
WebViewCompat.VisualStateCallback: The callback to be invoked.
|
postWebMessage
public static void postWebMessage (WebView webview, WebMessageCompat message, Uri targetOrigin)
Post a message to main frame. The embedded application can restrict the messages to a certain target origin. See HTML5 spec for how target origin can be used.
A target origin can be set as a wildcard ("*"). However this is not recommended. See the page above for security issues.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.POST_WEB_MESSAGE.
| Parameters | |
|---|---|
webview |
WebView |
message |
WebMessageCompat: the WebMessage |
targetOrigin |
Uri: the target origin.
|
setSafeBrowsingWhitelist
public static void setSafeBrowsingWhitelist (List<String> hosts,
ValueCallback<Boolean> callback)
Sets the list of hosts (domain names/IP addresses) that are exempt from SafeBrowsing checks. The list is global for all the WebViews.
Each rule should take one of these:
| Rule | Example | Matches Subdomain |
|---|---|---|
| HOSTNAME | example.com | Yes |
| .HOSTNAME | .example.com | No |
| IPV4_LITERAL | 192.168.1.1 | No |
| IPV6_LITERAL_WITH_BRACKETS | [10:20:30:40:50:60:70:80] | No |
All other rules, including wildcards, are invalid.
The correct syntax for hosts is defined by RFC 3986.
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.SAFE_BROWSING_WHITELIST.
| Parameters | |
|---|---|
hosts |
List: the list of hosts |
callback |
ValueCallback: will be called with true if hosts are successfully added to the
whitelist. It will be called with false if any hosts are malformed. The callback
will be run on the UI thread
|
startSafeBrowsing
public static void startSafeBrowsing (Context context, ValueCallback<Boolean> callback)
Starts Safe Browsing initialization.
URL loads are not guaranteed to be protected by Safe Browsing until after callback is
invoked with true. Safe Browsing is not fully supported on all devices. For those
devices callback will receive false.
This should not be called if Safe Browsing has been disabled by manifest tag or WebSettings.setSafeBrowsingEnabled(boolean). This prepares resources used for Safe
Browsing.
This should be called with the Application Context (and will always use the Application context to do its work regardless).
This method should only be called if
WebViewFeature.isFeatureSupported(String)
returns true for WebViewFeature.START_SAFE_BROWSING.
| Parameters | |
|---|---|
context |
Context: Application Context. |
callback |
ValueCallback: will be called on the UI thread with true if initialization is
successful, false otherwise.
|
Interfaces
Classes
- SafeBrowsingResponseCompat
- ServiceWorkerClientCompat
- ServiceWorkerControllerCompat
- ServiceWorkerWebSettingsCompat
- WebMessageCompat
- WebMessagePortCompat
- WebMessagePortCompat.WebMessageCallbackCompat
- WebResourceErrorCompat
- WebResourceRequestCompat
- WebSettingsCompat
- WebViewClientCompat
- WebViewCompat
- WebViewFeature
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.