From 35ad69239c5d8d682b42cde16a2eedc53ab184f1 Mon Sep 17 00:00:00 2001 From: Grigorii Date: Wed, 28 Dec 2022 14:27:05 +0400 Subject: [PATCH] Add custom condition to IgnoredErrorsHolder --- .../roboswag/webview/web_view/BaseWebView.kt | 12 +++++---- .../webview/web_view/BaseWebViewClient.kt | 6 ++--- .../redirection/IgnoredErrorsHolder.kt | 26 +++++++++++++++++++ .../web_view/redirection/IgnoredUrlsHolder.kt | 15 ----------- 4 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredErrorsHolder.kt delete mode 100644 webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredUrlsHolder.kt diff --git a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebView.kt b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebView.kt index 08c2708..fe93454 100644 --- a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebView.kt +++ b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebView.kt @@ -15,6 +15,7 @@ import ru.touchin.extensions.setOnRippleClickListener import ru.touchin.roboswag.views.widget.Switcher import ru.touchin.roboswag.webview.R import ru.touchin.roboswag.webview.databinding.BaseWebViewBinding +import ru.touchin.roboswag.webview.web_view.redirection.IgnoredErrorsHolder import ru.touchin.roboswag.webview.web_view.redirection.RedirectionController open class BaseWebView @JvmOverloads constructor( @@ -80,8 +81,11 @@ open class BaseWebView @JvmOverloads constructor( binding.linearProgressBar.progress = progress } - fun setBaseWebViewClient(callback: WebViewCallback = this) { - binding.webView.webViewClient = BaseWebViewClient(callback) + fun setBaseWebViewClient( + callback: WebViewCallback = this, + ignoredErrorsHolder: IgnoredErrorsHolder = IgnoredErrorsHolder() + ) { + binding.webView.webViewClient = BaseWebViewClient(callback, ignoredErrorsHolder) binding.webView.webChromeClient = BaseChromeWebViewClient(callback) } @@ -119,7 +123,7 @@ open class BaseWebView @JvmOverloads constructor( ) { val indexOfHead = htmlString .indexOf("", ignoreCase = true) - .takeIf { it != -1 } ?: 0 + .takeIf { it >= 0 } ?: 0 val styledHtml = StringBuilder(htmlString) .insert(indexOfHead, styleDeps) @@ -147,8 +151,6 @@ open class BaseWebView @JvmOverloads constructor( scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY setLayerType(View.LAYER_TYPE_HARDWARE, null) with(settings) { - allowContentAccess = true - allowFileAccess = true loadsImagesAutomatically = true javaScriptEnabled = true domStorageEnabled = true diff --git a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebViewClient.kt b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebViewClient.kt index a8b8da5..2cdb949 100644 --- a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebViewClient.kt +++ b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/BaseWebViewClient.kt @@ -11,11 +11,11 @@ import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient import androidx.core.os.postDelayed -import ru.touchin.roboswag.webview.web_view.redirection.IgnoredUrlsHolder +import ru.touchin.roboswag.webview.web_view.redirection.IgnoredErrorsHolder open class BaseWebViewClient( private val callback: WebViewCallback, - private val ignoredUrlsHolder: IgnoredUrlsHolder = IgnoredUrlsHolder(), + private val ignoredErrorsHolder: IgnoredErrorsHolder = IgnoredErrorsHolder(), private val isSslPinningEnable: Boolean = true ) : WebViewClient() { @@ -83,7 +83,7 @@ open class BaseWebViewClient( * onReceivedError isn't called when url is "about:blank" (url string isBlank) */ override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) { - if (ignoredUrlsHolder.shouldIgnoreError(request.url.toString())) return + if (ignoredErrorsHolder.shouldIgnoreError(request, error)) return isError = true } diff --git a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredErrorsHolder.kt b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredErrorsHolder.kt new file mode 100644 index 0000000..b1b2677 --- /dev/null +++ b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredErrorsHolder.kt @@ -0,0 +1,26 @@ +package ru.touchin.roboswag.webview.web_view.redirection + +import android.webkit.WebResourceError +import android.webkit.WebResourceRequest + +/** + * Define some urls here which errors should be ignored while redirection to prevent show error screen + * Additionally you can add custom error ignoring condition + */ +class IgnoredErrorsHolder( + private val ignoredUrlsList: List = listOf( + "https://stats.g.doubleclick.net/" + ), + private val ignoreCondition: ((WebResourceRequest, WebResourceError) -> Boolean)? = null +) { + + constructor(vararg urls: String) : this(urls.toList()) + + fun shouldIgnoreError(request: WebResourceRequest, error: WebResourceError): Boolean { + if (ignoreCondition != null) return ignoreCondition.invoke(request, error) + + val url = request.url.toString() + + return ignoredUrlsList.any { url in it } + } +} diff --git a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredUrlsHolder.kt b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredUrlsHolder.kt deleted file mode 100644 index 90a6237..0000000 --- a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/IgnoredUrlsHolder.kt +++ /dev/null @@ -1,15 +0,0 @@ -package ru.touchin.roboswag.webview.web_view.redirection - -/** - * Define some urls here which errors should be ignored while redirection to prevent show error screen - */ -class IgnoredUrlsHolder( - private val ignoredUrlsList: List = listOf( - "https://stats.g.doubleclick.net/" - ) -) { - - constructor(vararg urls: String) : this(urls.toList()) - - fun shouldIgnoreError(url: String) = ignoredUrlsList.any { url in it } -}