Skip to content

Instantly share code, notes, and snippets.

@AndSky90
Created November 4, 2019 14:20
Show Gist options
  • Save AndSky90/ca1a35be0d453eeb41636a7b1781b5a9 to your computer and use it in GitHub Desktop.
Save AndSky90/ca1a35be0d453eeb41636a7b1781b5a9 to your computer and use it in GitHub Desktop.
WebView client handle clicks image/mail/link
const val header = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<body>"""
const val footer = """</body>
</html>"""
import android.webkit.WebView
import android.webkit.WebResourceRequest
import android.os.Build
import android.annotation.TargetApi
import android.content.ActivityNotFoundException
import android.content.Context
import android.webkit.WebViewClient
import android.content.Intent
import android.net.Uri
open class CustomWebViewClient(var context: Context) : WebViewClient() {
var handleImageClick: (url: String) -> Unit =
{ NavigationProvider.navigator.openPhotoFromFile(it) }
//для API>=24
@TargetApi(Build.VERSION_CODES.N)
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) =
resolveUrlLoading(request.url.toString())
//для API<24
@Suppress("DEPRECATION")
override fun shouldOverrideUrlLoading(view: WebView, url: String) =
resolveUrlLoading(url)
private fun resolveUrlLoading(url: String): Boolean {
if (url.endsWith(".png") || url.endsWith(".jpg")
|| url.endsWith(".jpeg") || url.endsWith(".bmp")
) {
handleImageClick(url)
} else if (url.startsWith("mailto:")) {
startEmailClient(url)
} else {
startWebClient(url)
}
return true
}
private fun startEmailClient(address: String) {
val intent = Intent(Intent.ACTION_SENDTO, Uri.parse(address))
if (intent.resolveActivity(context.packageManager) != null) {
context.startActivity(intent)
} else {
try {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=mail&c=apps"))
)
} catch (e: ActivityNotFoundException) {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/search?q=mail&c=apps"))
)
}
}
}
private fun startWebClient(address: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(address))
if (intent.resolveActivity(context.packageManager) != null) {
context.startActivity(intent)
} else {
try {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=browser&c=apps"))
)
} catch (e: ActivityNotFoundException) {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/search?q=browser&c=apps"))
)
}
}
}
}
webViewFrame.webViewClient = CustomWebViewClient(this)
webViewFrame.reload()
//если HTML неполный - дополняем
if (data.text?.contains("<!DOCTYPE html>") == false) {
data.text = header + data.text + footer
}
webViewFrame.loadDataWithBaseURL(null, data.text, "text/html", "UTF-8", null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment