From f515674dff5c96f6172bc8c87c379a71d95f3f5a Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 7 Feb 2020 22:49:33 -0500 Subject: [PATCH] Show message if WebView version is too low --- .../network/CloudflareInterceptor.kt | 15 +++++--- .../tachiyomi/ui/webview/WebViewActivity.kt | 15 +++++--- .../tachiyomi/util/system/WebviewUtil.kt | 37 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/system/WebviewUtil.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt index 024253064c..a8fd4a2e18 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt @@ -8,6 +8,7 @@ import android.os.Looper import android.webkit.WebSettings import android.webkit.WebView import eu.kanade.tachiyomi.util.system.WebViewClientCompat +import eu.kanade.tachiyomi.util.system.checkVersion import okhttp3.Cookie import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Interceptor @@ -78,12 +79,15 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { val headers = request.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } handler.post { - val view = WebView(context) - webView = view - view.settings.javaScriptEnabled = true - view.settings.userAgentString = request.header("User-Agent") - view.webViewClient = object : WebViewClientCompat() { + val webview = WebView(context) + webView = webview + webview.checkVersion() + + webview.settings.javaScriptEnabled = true + webview.settings.userAgentString = request.header("User-Agent") + + webview.webViewClient = object : WebViewClientCompat() { override fun onPageFinished(view: WebView, url: String) { fun isCloudFlareBypassed(): Boolean { return networkHelper.cookieManager.get(origRequestUrl.toHttpUrl()) @@ -122,6 +126,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { } } } + webView?.loadUrl(origRequestUrl, headers) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt index 2658896c90..c63e079bd8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.ui.webview +import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.graphics.Bitmap @@ -14,10 +15,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.activity.BaseActivity -import eu.kanade.tachiyomi.util.system.WebViewClientCompat -import eu.kanade.tachiyomi.util.system.getResourceColor -import eu.kanade.tachiyomi.util.system.openInBrowser -import eu.kanade.tachiyomi.util.system.toast +import eu.kanade.tachiyomi.util.system.* import eu.kanade.tachiyomi.util.view.invisible import eu.kanade.tachiyomi.util.view.visible import kotlinx.android.synthetic.main.webview_activity.* @@ -29,6 +27,7 @@ class WebViewActivity : BaseActivity() { private var bundle: Bundle? = null + @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.webview_activity) @@ -54,6 +53,11 @@ class WebViewActivity : BaseActivity() { val url = intent.extras!!.getString(URL_KEY) ?: return val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } + webview.checkVersion() + + webview.settings.javaScriptEnabled = true + webview.settings.userAgentString = source.headers["User-Agent"] + webview.webChromeClient = object : WebChromeClient() { override fun onProgressChanged(view: WebView?, newProgress: Int) { progress_bar.visible() @@ -91,8 +95,7 @@ class WebViewActivity : BaseActivity() { nested_view.scrollTo(0, 0) } } - webview.settings.javaScriptEnabled = true - webview.settings.userAgentString = source.headers["User-Agent"] + webview.loadUrl(url, headers) } else { webview.restoreState(bundle) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/WebviewUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/WebviewUtil.kt new file mode 100644 index 0000000000..8590526b54 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/WebviewUtil.kt @@ -0,0 +1,37 @@ +package eu.kanade.tachiyomi.util.system + +import android.webkit.WebView +import android.widget.Toast +import eu.kanade.tachiyomi.R + +private val WEBVIEW_UA_VERSION_REGEX by lazy { + Regex(""".*Chrome/(\d+)\..*""") +} + +private const val MINIMUM_WEBVIEW_VERSION = 78 + +fun WebView.checkVersion() { + if (getWebviewMajorVersion(this) < MINIMUM_WEBVIEW_VERSION) { + this.context.toast(R.string.information_webview_outdated, Toast.LENGTH_LONG) + } +} + +// Based on https://stackoverflow.com/a/29218966 +private fun getWebviewMajorVersion(webview: WebView): Int { + val originalUA: String = webview.settings.userAgentString + + // Next call to getUserAgentString() will get us the default + webview.settings.userAgentString = null + + val uaRegexMatch = WEBVIEW_UA_VERSION_REGEX.matchEntire(webview.settings.userAgentString) + val webViewVersion: Int = if (uaRegexMatch != null && uaRegexMatch.groupValues.size > 1) { + uaRegexMatch.groupValues[1].toInt() + } else { + 0 + } + + // Revert to original UA string + webview.settings.userAgentString = originalUA + + return webViewVersion +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f76c5cf3f5..edea4c0250 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -522,6 +522,7 @@ No recently read manga Your library is empty, add series to your library from the catalogues. You have no categories. Hit the plus button to create one for organizing your library. + Please update the WebView app for better compatibility Downloader