From abb2e231f681d1978ef59ac230649a1748d04f62 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 15 Feb 2020 17:13:17 -0500 Subject: [PATCH] Switch to AndroidX Webkit library --- app/build.gradle | 1 + .../network/CloudflareInterceptor.kt | 42 +++++++--- .../tachiyomi/ui/webview/WebViewActivity.kt | 9 +- .../util/system/WebViewClientCompat.kt | 82 ------------------- 4 files changed, 37 insertions(+), 97 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewClientCompat.kt diff --git a/app/build.gradle b/app/build.gradle index f33a8bdbcc..e5af4dad00 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -116,6 +116,7 @@ dependencies { implementation 'androidx.annotation:annotation:1.1.0' implementation 'androidx.browser:browser:1.2.0' implementation 'androidx.multidex:multidex:2.0.1' + implementation 'androidx.webkit:webkit:1.1.0' // UI library implementation 'com.google.android.material:material:1.1.0' 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 59d77cf2ae..f92e5b4bb6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt @@ -5,11 +5,15 @@ import android.content.Context import android.os.Build import android.os.Handler import android.os.Looper +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse import android.webkit.WebSettings import android.webkit.WebView import android.widget.Toast +import androidx.webkit.WebResourceErrorCompat +import androidx.webkit.WebViewClientCompat +import androidx.webkit.WebViewFeature import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.isOutdated import eu.kanade.tachiyomi.util.system.toast import okhttp3.Cookie @@ -97,8 +101,9 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { cloudflareBypassed = true latch.countDown() } - // Http error codes are only received since M - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && + + // HTTP error codes are only received since M + if (WebViewFeature.isFeatureSupported(WebViewFeature.RECEIVE_WEB_RESOURCE_ERROR) && url == origRequestUrl && !challengeFound ) { // The first request didn't return the challenge, abort. @@ -106,16 +111,31 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { } } - override fun onReceivedErrorCompat( + override fun onReceivedHttpError( view: WebView, - errorCode: Int, - description: String?, - failingUrl: String, - isMainFrame: Boolean + request: WebResourceRequest, + errorResponse: WebResourceResponse ) { - if (isMainFrame) { - if (errorCode == 503) { - // Found the cloudflare challenge page. + if (request.isForMainFrame) { + if (errorResponse.statusCode == 503) { + // Found the Cloudflare challenge page. + challengeFound = true + } else { + // Unlock thread, the challenge wasn't found. + latch.countDown() + } + } + } + + @SuppressLint("RequiresFeature") + override fun onReceivedError( + view: WebView, + request: WebResourceRequest, + error: WebResourceErrorCompat + ) { + if (request.isForMainFrame) { + if (error.errorCode == 503) { + // Found the Cloudflare challenge page. challengeFound = true } else { // Unlock thread, the challenge wasn't found. 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 898a1260e1..02964edcbe 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 @@ -9,13 +9,14 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.webkit.WebChromeClient +import android.webkit.WebResourceRequest import android.webkit.WebView import androidx.core.graphics.ColorUtils +import androidx.webkit.WebViewClientCompat 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 @@ -71,8 +72,8 @@ class WebViewActivity : BaseActivity() { } webview.webViewClient = object : WebViewClientCompat() { - override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { - view.loadUrl(url) + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { + view.loadUrl(request.url.toString()) return true } @@ -89,7 +90,7 @@ class WebViewActivity : BaseActivity() { invalidateOptionsMenu() } - override fun onPageCommitVisible(view: WebView?, url: String?) { + override fun onPageCommitVisible(view: WebView, url: String) { super.onPageCommitVisible(view, url) // Reset to top when page refreshes diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewClientCompat.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewClientCompat.kt deleted file mode 100644 index 7e1f840cdc..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewClientCompat.kt +++ /dev/null @@ -1,82 +0,0 @@ -package eu.kanade.tachiyomi.util.system - -import android.annotation.TargetApi -import android.os.Build -import android.webkit.* - -@Suppress("OverridingDeprecatedMember") -abstract class WebViewClientCompat : WebViewClient() { - - open fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { - return false - } - - open fun shouldInterceptRequestCompat(view: WebView, url: String): WebResourceResponse? { - return null - } - - open fun onReceivedErrorCompat( - view: WebView, - errorCode: Int, - description: String?, - failingUrl: String, - isMainFrame: Boolean) { - - } - - @TargetApi(Build.VERSION_CODES.N) - final override fun shouldOverrideUrlLoading( - view: WebView, - request: WebResourceRequest - ): Boolean { - return shouldOverrideUrlCompat(view, request.url.toString()) - } - - final override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { - return shouldOverrideUrlCompat(view, url) - } - - final override fun shouldInterceptRequest( - view: WebView, - request: WebResourceRequest - ): WebResourceResponse? { - return shouldInterceptRequestCompat(view, request.url.toString()) - } - - final override fun shouldInterceptRequest( - view: WebView, - url: String - ): WebResourceResponse? { - return shouldInterceptRequestCompat(view, url) - } - - @TargetApi(Build.VERSION_CODES.M) - final override fun onReceivedError( - view: WebView, - request: WebResourceRequest, - error: WebResourceError - ) { - onReceivedErrorCompat(view, error.errorCode, error.description?.toString(), - request.url.toString(), request.isForMainFrame) - } - - final override fun onReceivedError( - view: WebView, - errorCode: Int, - description: String?, - failingUrl: String - ) { - onReceivedErrorCompat(view, errorCode, description, failingUrl, failingUrl == view.url) - } - - @TargetApi(Build.VERSION_CODES.M) - final override fun onReceivedHttpError( - view: WebView, - request: WebResourceRequest, - error: WebResourceResponse - ) { - onReceivedErrorCompat(view, error.statusCode, error.reasonPhrase, request.url - .toString(), request.isForMainFrame) - } - -}