Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
优化 CompatWebView 中链接拦截的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jan 17, 2022
1 parent 3cc2000 commit f0e3540
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
12 changes: 2 additions & 10 deletions web/src/main/kotlin/io/goooler/demoapp/web/CompatWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.net.http.SslError
import android.util.AttributeSet
import android.view.ViewGroup
import android.webkit.SslErrorHandler
import android.webkit.URLUtil
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebSettings
Expand Down Expand Up @@ -70,14 +69,7 @@ open class CompatWebView(context: Context, attrs: AttributeSet? = null) : WebVie
}

override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return if (URLUtil.isValidUrl(url)) {
if (URLUtil.isNetworkUrl(url)) {
false
} else {
onEventListener?.onInterceptUri(Uri.parse(url))
true
}
} else false
return onEventListener?.onInterceptUrl(url) ?: false
}

override fun onPageFinished(view: WebView, url: String) {
Expand Down Expand Up @@ -119,7 +111,7 @@ open class CompatWebView(context: Context, attrs: AttributeSet? = null) : WebVie
}

interface OnEventListener {
fun onInterceptUri(uri: Uri)
fun onInterceptUrl(url: String): Boolean

fun onReceivedTitle(title: String)

Expand Down
9 changes: 7 additions & 2 deletions web/src/main/kotlin/io/goooler/demoapp/web/WebFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.webkit.WebChromeClient
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.IntRange
import androidx.core.net.toUri
import io.goooler.demoapp.base.util.isNetworkUrl
import io.goooler.demoapp.base.util.putArguments
import io.goooler.demoapp.base.util.toMimeType
import io.goooler.demoapp.common.base.binding.BaseBindingFragment
Expand Down Expand Up @@ -51,8 +53,11 @@ class WebFragment : BaseBindingFragment<WebFragmentBinding>() {
}

private val listener = object : CompatWebView.OnEventListener, JsInterface {
override fun onInterceptUri(uri: Uri) {
startActivity(Intent(Intent.ACTION_VIEW, uri))
override fun onInterceptUrl(url: String): Boolean {
return if (url.isNetworkUrl()) false else {
startActivity(Intent(Intent.ACTION_VIEW, url.toUri()))
true
}
}

override fun onReceivedTitle(title: String) {
Expand Down

0 comments on commit f0e3540

Please sign in to comment.