Merge pull request #177 from TouchInstinct/web-view
Added ssl processing, redirect, cookies processing
This commit is contained in:
commit
8414e23934
|
|
@ -27,6 +27,7 @@ open class BaseWebView @JvmOverloads constructor(
|
|||
var onWebViewLoaded: (() -> Unit)? = null
|
||||
var onWebViewRepeatButtonClicked: (() -> Unit)? = null
|
||||
var onWebViewScrolled: ((WebView, Int, Int) -> Unit)? = null
|
||||
var onCookieLoaded: ((cookies: Map<String, String>) -> Unit)? = null
|
||||
|
||||
var onJsConfirm: (() -> Unit)? = null
|
||||
var onJsAlert: (() -> Unit)? = null
|
||||
|
|
@ -40,6 +41,8 @@ open class BaseWebView @JvmOverloads constructor(
|
|||
field = value
|
||||
}
|
||||
|
||||
var isRedirectEnable = false
|
||||
|
||||
init {
|
||||
binding.pullToRefresh.isEnabled = isPullToRefreshEnable
|
||||
binding.apply {
|
||||
|
|
@ -72,16 +75,13 @@ open class BaseWebView @JvmOverloads constructor(
|
|||
errorRepeatButton.setOnRippleClickListener {
|
||||
onWebViewRepeatButtonClicked?.invoke()
|
||||
}
|
||||
webView.onScrollChanged = { scrollX, scrollY, _, _ ->
|
||||
onWebViewScrolled?.invoke(binding.webView, scrollX, scrollY)
|
||||
}
|
||||
setWebViewPreferences()
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
binding.webView.onScrollChanged = { scrollX, scrollY, _, _ ->
|
||||
onWebViewScrolled?.invoke(binding.webView, scrollX, scrollY)
|
||||
}
|
||||
setWebViewPreferences()
|
||||
}
|
||||
|
||||
override fun onStateChanged(newState: WebViewLoadingState) {
|
||||
when {
|
||||
newState == WebViewLoadingState.LOADED -> {
|
||||
|
|
@ -99,8 +99,14 @@ open class BaseWebView @JvmOverloads constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun setBaseWebViewClient() {
|
||||
binding.webView.webViewClient = BaseWebViewClient(this)
|
||||
override fun onOverrideUrlLoading(url: String?): Boolean = isRedirectEnable
|
||||
|
||||
override fun onPageCookiesLoaded(cookies: Map<String, String>) {
|
||||
onCookieLoaded?.invoke(cookies)
|
||||
}
|
||||
|
||||
fun setBaseWebViewClient(isSSlPinningEnable: Boolean = false) {
|
||||
binding.webView.webViewClient = BaseWebViewClient(this, isSSlPinningEnable)
|
||||
binding.webView.webChromeClient = BaseChromeWebViewClient(onJsConfirm, onJsAlert, onJsPrompt, onJsError)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import android.graphics.Bitmap
|
|||
import android.net.http.SslError
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.SslErrorHandler
|
||||
import android.webkit.WebResourceError
|
||||
import android.webkit.WebResourceRequest
|
||||
|
|
@ -11,7 +12,7 @@ import android.webkit.WebView
|
|||
import android.webkit.WebViewClient
|
||||
import androidx.core.os.postDelayed
|
||||
|
||||
open class BaseWebViewClient(private val callback: WebViewCallback) : WebViewClient() {
|
||||
open class BaseWebViewClient(private val callback: WebViewCallback, private val isSslPinningEnable: Boolean) : WebViewClient() {
|
||||
|
||||
companion object {
|
||||
private const val WEB_VIEW_TIMEOUT_MS = 30 * 1000L // 30 sec
|
||||
|
|
@ -40,11 +41,22 @@ open class BaseWebViewClient(private val callback: WebViewCallback) : WebViewCli
|
|||
override fun onPageFinished(view: WebView, url: String) {
|
||||
super.onPageFinished(view, url)
|
||||
isTimeout = false
|
||||
callback.onPageCookiesLoaded(CookieManager.getInstance().getCookie(url).processCookies())
|
||||
pageFinished()
|
||||
}
|
||||
|
||||
override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
|
||||
return !callback.onOverrideUrlLoading(url) && view.originalUrl != null
|
||||
}
|
||||
|
||||
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
|
||||
handler.proceed()
|
||||
if (isSslPinningEnable) {
|
||||
super.onReceivedSslError(view, handler, error)
|
||||
isError = true
|
||||
callback.onStateChanged(WebViewLoadingState.ERROR)
|
||||
} else {
|
||||
handler.proceed()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
|
||||
|
|
@ -58,6 +70,16 @@ open class BaseWebViewClient(private val callback: WebViewCallback) : WebViewCli
|
|||
callback.onStateChanged(if (isError) WebViewLoadingState.ERROR else WebViewLoadingState.LOADED)
|
||||
}
|
||||
|
||||
private fun String.processCookies(): Map<String, String> {
|
||||
val cookiesMap = mutableMapOf<String, String>()
|
||||
this.split(";")
|
||||
.forEach { cookie ->
|
||||
val splittedCookie = cookie.trim().split("=")
|
||||
cookiesMap[splittedCookie.first()] = splittedCookie.last()
|
||||
}
|
||||
return cookiesMap
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum class WebViewLoadingState {
|
||||
|
|
|
|||
|
|
@ -4,4 +4,8 @@ interface WebViewCallback {
|
|||
|
||||
fun onStateChanged(newState: WebViewLoadingState)
|
||||
|
||||
fun onOverrideUrlLoading(url: String?): Boolean
|
||||
|
||||
fun onPageCookiesLoaded(cookies: Map<String, String>)
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue