Merge pull request #192 from TouchInstinct/ban-105-106-107-webview-invalid-url-crashes

fix webview crashes
This commit is contained in:
Дэнис Марквальд 2021-05-27 13:21:49 +02:00 committed by GitHub
commit 70de19523d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 16 deletions

View File

@ -27,7 +27,7 @@ open class BaseWebView @JvmOverloads constructor(
var onWebViewLoaded: (() -> Unit)? = null
var onWebViewRepeatButtonClicked: (() -> Unit)? = null
var onWebViewScrolled: ((WebView, Int, Int) -> Unit)? = null
var onCookieLoaded: ((cookies: Map<String, String>) -> Unit)? = null
var onCookieLoaded: ((cookies: Map<String, String>?) -> Unit)? = null
var onJsConfirm: (() -> Unit)? = null
var onJsAlert: (() -> Unit)? = null
@ -101,7 +101,7 @@ open class BaseWebView @JvmOverloads constructor(
override fun onOverrideUrlLoading(url: String?): Boolean = isRedirectEnable
override fun onPageCookiesLoaded(cookies: Map<String, String>) {
override fun onPageCookiesLoaded(cookies: Map<String, String>?) {
onCookieLoaded?.invoke(cookies)
}
@ -112,8 +112,12 @@ open class BaseWebView @JvmOverloads constructor(
fun getWebView() = binding.webView
/**
* if url is null it changes to empty string
* to prevent infinite LOADING state
*/
fun loadUrl(url: String?) {
binding.webView.loadUrl(url)
binding.webView.loadUrl(url ?: "")
}
fun setState(newState: WebViewLoadingState) {
@ -124,16 +128,24 @@ open class BaseWebView @JvmOverloads constructor(
binding.webView.onWebViewDisplayedContent = action
}
/**
* loadWithOverviewMode loads the WebView completely zoomed out
* useWideViewPort sets page size to fit screen
* setInitialScale(1) prevents horizontal scrolling when
* page has horizontal paddings
*/
@SuppressLint("SetJavaScriptEnabled")
open fun setWebViewPreferences() {
binding.webView.apply {
scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
setLayerType(View.LAYER_TYPE_SOFTWARE, null)
setLayerType(View.LAYER_TYPE_HARDWARE, null)
with(settings) {
loadsImagesAutomatically = true
javaScriptEnabled = true
domStorageEnabled = true
loadWithOverviewMode = true
useWideViewPort = true
setInitialScale(1)
}
}
}

View File

@ -38,10 +38,19 @@ open class BaseWebViewClient(private val callback: WebViewCallback, private val
}
}
/**
* onPageFinished is always called after onReceivedError,
* except when there is a error page in the cache and onReceivedError is called first
*/
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
isTimeout = false
callback.onPageCookiesLoaded(CookieManager.getInstance().getCookie(url).processCookies())
if (url == "about:blank") {
isError = true
}
if (!isError) {
callback.onPageCookiesLoaded(CookieManager.getInstance().getCookie(url).processCookies())
}
pageFinished()
}
@ -59,24 +68,24 @@ open class BaseWebViewClient(private val callback: WebViewCallback, private val
}
}
/**
* onReceivedError isn't called when url is "about:blank" (url string isBlank)
*/
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
if (!(error.errorCode == -10 && "about:blank" == request.url.toString())) {
isError = true
}
pageFinished()
isError = true
}
private fun pageFinished() {
callback.onStateChanged(if (isError) WebViewLoadingState.ERROR else WebViewLoadingState.LOADED)
}
private fun String.processCookies(): Map<String, String> {
private fun String?.processCookies(): Map<String, String> {
val cookiesMap = mutableMapOf<String, String>()
this.split(";")
.forEach { cookie ->
val splittedCookie = cookie.trim().split("=")
cookiesMap[splittedCookie.first()] = splittedCookie.last()
}
this?.split(";")
?.forEach { cookie ->
val splittedCookie = cookie.trim().split("=")
cookiesMap[splittedCookie.first()] = splittedCookie.last()
}
return cookiesMap
}

View File

@ -6,6 +6,6 @@ interface WebViewCallback {
fun onOverrideUrlLoading(url: String?): Boolean
fun onPageCookiesLoaded(cookies: Map<String, String>)
fun onPageCookiesLoaded(cookies: Map<String, String>?)
}