Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

ProWebView

Victor Campos edited this page Dec 30, 2017 · 4 revisions

ProWebView

Class information

public class ProWebView extends WebView implements DownloadListener, NestedScrollingChild

java.lang.Object

↪ android.view.View

  ↪ android.view.ViewGroup

    ↪ android.widget.AbsoluteLayout

      ↪ android.webkit.WebView

        ↪ vcm.github.webkit.proview.ProWebView

ProWebView is another WebView for android with more functionality and without the hell of the WebClient and the WebChomeClient. With ProWebView you don't have to worry about asking permissions, file download, file upload, JS alerts and dialogs, links without protocols, cache managing and/or cookies managing. ProWebView also supports XML attributes, private mode, block ads and nested scrolling.

Permissions

ProWebView requires the following permissions:

  • android.permission.INTERNET: required by the WebView.
  • android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE: required fo the download manager.
  • android.permission.ACCESS_FINE_LOCATION: required if the location is enabled and the location mode is setted to modeFine or modeBoth.
  • android.permission.ACCESS_COARSE_LOCATION: required if the location is enabled and the location mode is setted to modeCoarse or modeBoth.
  • android.permission.CAMERA: optional. Required for JS camera API.
  • android.permission.RECORD_AUDIO: optional. Required for JS microphone API
  • android.permission.ACCESS_NETWORK_STATE and android.network.ACCESS_WIFI_STATE: optional. Used to get the current connection mode.

Summary

XML Attributes

Attribute Format Values Function
nestedScrollingEnabled boolean - nestedScrollingEnabled(boolean)
locationEnabled boolean - setGeolocationEnabled(boolean)
blankHtml string - setAboutBlankHtml(String)
home string - setHomeUrl(String) or setHomeUrl(Uri)
defaultProtocol string - setDefaultProtocol(String)
targetView reference View ID (removed on version 2.0) setTargetView(ViewGroup)
privateMode boolean - privateModeActive(boolean)
locationMode enum modeBoth - modeFine - modeCoarse setLocationMode(LocationMode)
cookiesEnabled boolean - setCookiesEnabled(boolean) and setThirdPartyCookiesEnabled(boolean)
cacheMode enum cacheElseNetwork - cacheDefault - cacheOnly - noCache setCacheMode(int)
blockAds boolean - setBlockAds(boolean)

Note: The attribute locationMode requires android.permission.ACCESS_FINE_LOCATION for modeFine, android.permission.ACCESS_COARSE_LOCATION for modeCoarse and both permissions for modeBoth. This attribute and its related method exist for a better permission system.

Classes

Nested classes Name
static class ProClient Provides a easy handling for WebViewClient and WebChromeClient events
interface SourceCodeCallback Callback for source code request
interface JsConsoleMessageListener Listener for console messages
interface SchemeRequest Event handler for HTTP schemes
interface PermissionCallback Callback for permission request
interface UrlRequest Listener for blacklisted host and SSL errors
interface PrintListener Listener for print jobs
abstract class RequestCallback<F> Callback for requests like geolocation and permissions

Enums

Enums
LocationMode Used to ask for android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_COARSE_LOCATION permissions
ConnectionMode Used to detect the current connection mode

Public methods

Methods
void addHostToBlacklist(String host) Add host to the black list
void addSpecialScheme(String scheme, SchemeRequest request) Add a event handler for a HTTP scheme
boolean areAdsBlocked() Are the ad blocked enabled?
boolean areCookiesEnabled() Are the cookies enabled?
boolean areThirdPartyCookiesEnabled() For API 21. Are the third party cookies enabled?
void blockAds(boolean enabled) Enable or disable the ad blocker
void clearCache() Clear the browsing cache
void clearCookies() Clear the cookies
void clearDatabase() Clear the web data base
void clearStorage() Clear the web storage
void clearStorage(String origin) Clear the web storage from a origin
boolean cancelCodeRequest() Calcel the current active cource code request
void deleteData() Delete all the navigation data. This method does not delete the application cache
String getAboutBlankHtml Get the current HTML for about:blank
String[] getBackStackArray() Get the back stack. If the private mode is enabled this function will return null
List<String> getBackStack() Get the back stack. If the private mode is enabled this function will return null Deprecated
String[] getBlacklistedHostsArray() Get the list of the blocked hosts
List<String> getBlacklistedHosts() Get the list of the blocked hosts Deprecated
ConnectionMode getConnectionMode() Get the current connection mode
ConsoleMessage[] getConsoleMessagesArray() Get the list of the blocked hosts
List<ConsoleMessage> getConsoleMessages() Get a list of all the console messages Deprecated
String getDefaultProtocol() Get the current default protocol
String[] getForwardStackArray() Get the forward stack. If the private mode is enabled this function will return null
List<String> getForwardStack() Get the forward stack. If the private mode is enabled this function will return null Deprecated
void goHome() Load the current home page. If the home page is null it will load about:blank
String getHomeUrl() Get the current home page
LocationMode getLocationMode() Get the current location mode
Bitmap getPreview(int width, int height) Builds a preview of the web view
void getSourceCode(@NonNull SourceCodeCallback callback) Get the source code of the current web site
boolean goTo(int steps) A better implementation of canGoBackOrForward(int steps) and goBackOrForward(int steps)
boolean isBusy() Is the web view loading a web site?
boolean isGeolocationEnabled() Is geolocation enabled?
boolean isNetworkAvailable() Is the connection available?
void loadHtml(String html) Loads the HTML code
void loadHtml(String html, String baseUrl) Loads the HTML code
void loadHtml(String html, String baseUrl, String historyUrl) Loads the HTML code
void loadHtml(String html, String baseUrl, String historyUrl, String encoding) Loads the HTML code
void loadMarkdown(String markdown) Load a markdown string
void printWebView(@Nullable PrintListener listener) For API 19. Prints the web view content
void removeSpecialScheme(String scheme) Remove the handler for the HTTP scheme
void setAboutBlankHtml(String html) Set a custom HTML for about:blank
void setActivity(Activity activity) Required for the permission system. Call only if you are using the ProWebView inside an Activity
void setCookiesEnabled(boolean enabled) Enable or disable the cookies
void setDefaultProtocol(String protocol) Set the default protocol
void setDesktopMode(boolean enabled) Enable or disabled the desktop mode
void setFragment(Fragment fragment) Required for the permission system. Call only if you are using the ProWebView inside a Fragment
void setGeolocationEnabled(boolean enabled) Enable or disable Geolocation. Use this method instead of getSettings().setGeolocationEnabled(boolean enabled).
void setHomeUrl(String url) Set a home page
void setLocationMode(LocationMode mode) Set the location mode
void setPermissionCallback(@Nullable PermissionCallback callback) Set a permission callback
void setPrivateBrowsingEnabled(boolean active) Enable or disable the private browsing
void setProClient(ProClient proClient) Set the listener to the web view
void setTargetView(ViewGroup target) Get the target view for the custom view. See WebChromeClient.(View view, WebChromeClient.CustomViewCallback callback)
void setThirdPartyCookiesEnabled(boolean enabled) For API 21. Enable or disable the third party cookies
void setUrlRequestListener(UrlRequest request) Set a listener for URL request
void showBlank() Loads the HTML code for about:blank else it will show a blank page
boolean tryGoBack() A better implementation of canGoBack() and goBack()
boolean tryGoForward() A better implementation of canGoForward() and goForward()

This methods must be called

Note: This methods must be called in their homonyms in your Activity or Fragment.

Method
void onActivityResult(int requestCode, int resultCode, Intent data) Important Call this method on your Activity or in your Fragment
void onRestoreInstanceState(Bundle inState) Important Call this method on your Activity or in your Fragment
void onRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) Important Call this method on your Activity or in your Fragment
void onSavedInstanceState(Bundle outState) Important Call this method on your Activity or in your Fragment

Overriden methods from android.webkit.WebView

Method Reason
clearHistory() Clear the history and the back and forward stacks
goBack() Manage the back and forward stacks
goForward() Manage the back and forward stacks
isPrivateBrowsingEna bled() Is the private mode active?
loadUrl(String url) Load the links without protocol (Example: google.com) and handle the about:blank
loadUrl(String url, Map<String, String> additionalHttpHeaders) Load the links without protocol (Example: google.com) and handle the about:blank
onPause() To combine WebView.onPause() and WebView.pauseTimers()
onResume() To combine WebView.onResume() and WebView.resumeTimers()

Note: both setWebChromeClient(WebChromeClient webChromeClient) and setWebViewClient(WebViewClient client) are marked as deprecated and they will throw a UnsupportedOperationException if you try to call them.

Public methods

All the methods from android.webkit.WebView.

Adds a host to the black list. Any url from this host is not loaded. See setUrlRequestListener(UrlRequest request).

Parameters
  • host: Name os the host.

Set a custom hander for a HTTP sheme like file and what to do with that url.

Parameters
  • scheme: custom scheme.
  • request: a ShemeRequest. An action to do when an url with that scheme is loaded.

Check if the Ad Block is active.

Returns
  • true if the Ad Blocker is active

Check if the cookies are enabled

Returns
  • true if the cookies are enabled

Api >= 21

Check if the third party cookies are enabled

Returns
  • true if enabled

Enable or disable the Ad Blocker

Parameters
  • enabled: to enable or disable the Ad Blocker

This method clears the cache keeping the disk files. The same as calling WebView.clearCache(false)

Clear the cookies.

Clear the WebView's web data base.

Clear the WebView's storgare.

Clear the WebView's storgare from a specific origin.

Stops the current code request if is active.

Clears all the browsing data.

See

ProWebView supports custom HTML code for about:blank. If you set any HTML string this method will return that code.

Returns
  • a string with the HTML code for about:blank.

ProWebView manages its own back stack. This method gets all the previously visited web sites

Returns
  • an array with the previously visited web sites

Deprecated ProWebView manages its own back stack. This method gets all the previously visited web sites

Returns
  • a read-only list with the previously visited web sites

ProWebView offers you the possibility to block any host. You can get a list of all the blocked hosts.

Returns
  • an array with all the hosts that are blocked

Deprecated ProWebView offers you the possibility to block any host. You can get a list of all the blocked hosts.

Returns
  • a list with all the hosts that are blocked

Get the ProWebView's cache mode

See
Returns

Any of:

  • android.webkit.WebSettings.LOAD_CACHE_ELSE_NETWORK
  • android.webkit.WebSettings.LOAD_CACHE_ONLY
  • android.webkit.WebSettings.LOAD_DEFAULT
  • android.webkit.WebSettings.LOAD_NO_CACHE

Get the current connection mode. If you don't set android.permission.ACCESS_NETWORK_STATE and android.network.ACCESS_WIFI_STATE permissions this will return ConnectionMode.CONNECTION_NONE.

Returns

Any of:

  • ConnectionMode.CONNECTION_4G: if connected to mobile data.
  • ConnectionMode.CONNECTION_WIFI: if connected to a WiFi network.
  • ConnectionMode.CONNECTION_NONE: if there is not connected or the permissions are missing.

Get a list of all the messages sended by JavaScript from the current web site.

Returns

Deprecated Get a list of all the messages sended by JavaScript from the current web site.

Returns

ProWebView supports urls without a protocol. The default HTTP protocol is http.

Returns
  • the current HTTP protocol

ProWebView manages its own forward stack.

Returns
  • a read-only list of all web sites visited after the current

Loads the "home" url.

See

Get the current "home" url.

Returns
  • a string with the "home" url
See

Get the current location mode.

Returns

Any of:

  • LocationMode.MODE_FINE
  • LocationMode.MODE_COARSE
  • LocationMode.MODE_BOTH

Creates a Bitmap from the content of the current web site. Is like a substitute of WebView#capturePicture().

Note: sometimes the bitmap may be white.

Parameters
  • width: the preview width
  • height: the height of the preview
Returns
  • a preview of the web site

Get the source code of the current web site in an AsyncTask

A better implementation of WebView#canGoBackOrForward(int steps) and WebView#goBackOrForward(int steps).

Parameters
  • steps: the coutnof steps back or forward
Returns

Check if the view is loading something.

Returns
  • true if is loading something

Check if the location is enabled.

Returns
  • true if enabled

Chec if the network is enabled. Only works if you put android.permission.ACCESS_NETWORK_STATE and android.network.ACCESS_WIFI_STATE permissions in the manifest.

Returns
  • true if the connection is enabled and getConnectionMode() is not ConnectionMode.CONNECTION_NONE

Loads and displays the provided HTML source text

Parameters
  • html: the HTML source text to load

Loads and displays the provided HTML source text

Parameters
  • html: the HTML source text to load
  • baseUrl: the URL to use as the page's base URL

Loads and displays the provided HTML source text

Parameters
  • html: the HTML source text to load
  • baseUrl: the URL to use as the page's base URL
  • historyUrl: the URL to use for the page's history entry

Loads and displays the provided HTML source text

Parameters
  • html: the HTML source text to load
  • baseUrl: the URL to use as the page's base URL
  • historyUrl: the URL to use for the page's history entry
  • encoding: the encoding or charset of the HTML source text

Loads and displays the provided Markdown text

Api >= 19

This method is a wraper for WebView#createPrintDocumentAdapter().

Parameters
  • listener: a listener for the printer events

Removes a HTTP scheme from the handlers.

Parameters
  • scheme: to remove

Set a HTML code to load in the about:blank page.

Parameters
  • html: code to set

Set the activity that will handle the permissions and the view's lifecycle.

Parameters
  • activity: parent

Enable or disable the cookies

Parameters
  • enabled: set the cookies enabled or disabled

Set the default protocol for urls without it.

Parameters
  • protocol: wich protocol to use

Force to almost all the web sites to show ther desktop version. May not work if the web site is using media queries in their CSS files.

Parameters
  • enabled: if true loads the desktop version

Same as setActivity(Activity activity)

Set the fragment that will handle the permissions and the view's lifecycle.

Parameters
  • fragment: parent

Enable or disable Geolocation.

Use this method instead of WebView#getSettings().setGeolocationEnabled(boolean enabled).

Parameters
  • enabled: true to enable geolocation

Set the "home" url

Parameters
  • url: the "home" url
See

Set the location mode. This is used to ask the correct permissions to the system.

Parameters
  • mode: the location mode

Set a callback for the permissions manager. This is called when a web site asks for using the camera, the microphone or want access to the GPS.

Parameters
  • callback: a PermissionCallback for the permission system
See

ProWebView can manage the private browsing by itself. This also revokes the @Deprecated annotation added on the Api 17 in WebView (Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) constructor which is active in ProWebView.

Parameters
  • active: enable or disable the private browsing

This is basicaly an implementation of the WebViewClient and the WebChromeClient. This reduces the events from 10 methods in the WebViewClient and 17 methods in the WebChromeClient (including 2 @Deprecated methods) to 7 methods in the WebListener interface.

Parameters
  • proClient: a ProClient that will handle the web events

Set a target view used for the full screen video in the WebChromeClient.(View view, WebChromeClient.CustomViewCallback callback) method.

Parameters

Api >= 21

Enable or disable the third party cookies

Parameters
  • enabled
See

This events are called when the user tries to load a host in black list or when the view handless a SSL error.

Parameters
  • listener: a callback

Loads a blank page. If you set a blank HTML code for about:blank then that code will be loaded.

A better implementation of WebView#canGoBack() and WebView#goBack()

Returns

A better implementation of WebView#canGoForward() and WebView#goForward()

Final notes

  1. Some of the methods used in ProWebView were extracted from AdvancedWebView like the loadHtml methods and the setThirdPartyCookiesEnabled.
  2. ProWebView use Strapdown.js to load the markdown documents.
Clone this wiki locally