Fix WebView crash
This commit is contained in:
@@ -1,11 +1,15 @@
|
|||||||
package org.koitharu.kotatsu.browser
|
package org.koitharu.kotatsu.browser
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
|
import android.os.Looper
|
||||||
import android.webkit.WebResourceRequest
|
import android.webkit.WebResourceRequest
|
||||||
import android.webkit.WebResourceResponse
|
import android.webkit.WebResourceResponse
|
||||||
import android.webkit.WebView
|
import android.webkit.WebView
|
||||||
import android.webkit.WebViewClient
|
import android.webkit.WebViewClient
|
||||||
|
import androidx.annotation.AnyThread
|
||||||
import androidx.annotation.WorkerThread
|
import androidx.annotation.WorkerThread
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.koitharu.kotatsu.core.network.webview.adblock.AdBlock
|
import org.koitharu.kotatsu.core.network.webview.adblock.AdBlock
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
|
|
||||||
@@ -43,7 +47,7 @@ open class BrowserClient(
|
|||||||
override fun shouldInterceptRequest(
|
override fun shouldInterceptRequest(
|
||||||
view: WebView?,
|
view: WebView?,
|
||||||
url: String?
|
url: String?
|
||||||
): WebResourceResponse? = if (url.isNullOrEmpty() || adBlock.shouldLoadUrl(url, view?.url)) {
|
): WebResourceResponse? = if (url.isNullOrEmpty() || adBlock.shouldLoadUrl(url, view?.getUrlSafe())) {
|
||||||
super.shouldInterceptRequest(view, url)
|
super.shouldInterceptRequest(view, url)
|
||||||
} else {
|
} else {
|
||||||
emptyResponse()
|
emptyResponse()
|
||||||
@@ -53,8 +57,7 @@ open class BrowserClient(
|
|||||||
override fun shouldInterceptRequest(
|
override fun shouldInterceptRequest(
|
||||||
view: WebView?,
|
view: WebView?,
|
||||||
request: WebResourceRequest?
|
request: WebResourceRequest?
|
||||||
): WebResourceResponse? = if (request == null || adBlock.shouldLoadUrl(request.url.toString(), view?.url)) {
|
): WebResourceResponse? = if (request == null || adBlock.shouldLoadUrl(request.url.toString(), view?.getUrlSafe())) {
|
||||||
view?.originalUrl
|
|
||||||
super.shouldInterceptRequest(view, request)
|
super.shouldInterceptRequest(view, request)
|
||||||
} else {
|
} else {
|
||||||
emptyResponse()
|
emptyResponse()
|
||||||
@@ -62,4 +65,13 @@ open class BrowserClient(
|
|||||||
|
|
||||||
private fun emptyResponse(): WebResourceResponse =
|
private fun emptyResponse(): WebResourceResponse =
|
||||||
WebResourceResponse("text/plain", "utf-8", ByteArrayInputStream(byteArrayOf()))
|
WebResourceResponse("text/plain", "utf-8", ByteArrayInputStream(byteArrayOf()))
|
||||||
|
|
||||||
|
@AnyThread
|
||||||
|
private fun WebView.getUrlSafe(): String? = if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||||
|
url
|
||||||
|
} else {
|
||||||
|
runBlocking(Dispatchers.Main.immediate) {
|
||||||
|
url
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ material = "1.14.0-alpha01"
|
|||||||
moshi = "1.15.2"
|
moshi = "1.15.2"
|
||||||
okhttp = "4.12.0"
|
okhttp = "4.12.0"
|
||||||
okio = "3.11.0"
|
okio = "3.11.0"
|
||||||
parsers = "7b1a0b8d0d"
|
parsers = "9558a34b00"
|
||||||
preference = "1.2.1"
|
preference = "1.2.1"
|
||||||
recyclerview = "1.4.0"
|
recyclerview = "1.4.0"
|
||||||
room = "2.7.1"
|
room = "2.7.1"
|
||||||
|
|||||||
Reference in New Issue
Block a user