Эх сурвалжийг харах

Show message if WebView version is too low

arkon 5 жил өмнө
parent
commit
f515674dff

+ 10 - 5
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)
         }
 

+ 9 - 6
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)

+ 37 - 0
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
+}

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -522,6 +522,7 @@
     <string name="information_no_recent_manga">No recently read manga</string>
     <string name="information_empty_library">Your library is empty, add series to your library from the catalogues.</string>
     <string name="information_empty_category">You have no categories. Hit the plus button to create one for organizing your library.</string>
+    <string name="information_webview_outdated">Please update the WebView app for better compatibility</string>
 
     <!-- Download Notification -->
     <string name="download_notifier_downloader_title">Downloader</string>