Improve image handling
Some checks failed
continuous-integration/drone/pr Build is failing

This commit is contained in:
davidoskky 2023-09-12 00:17:41 +02:00
parent dcb9b1cb1f
commit 20aab0ea62
3 changed files with 60 additions and 53 deletions

View File

@ -329,11 +329,19 @@ class ArticleFragment : Fragment(), DIAware {
private fun handleImageLoading() { private fun handleImageLoading() {
binding.webcontent.webViewClient = object : WebViewClient() { binding.webcontent.webViewClient = object : WebViewClient() {
@Deprecated("Deprecated in Java") override fun shouldOverrideUrlLoading(
override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean { view: WebView?,
return if (context != null && url.isUrlValid() && binding.webcontent.hitTestResult.type != WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { request: WebResourceRequest?
): Boolean {
val url = request?.url?.toString()
return if (context != null && url != null && url.isUrlValid() && binding.webcontent.hitTestResult.type != WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
try { try {
requireContext().startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) requireContext().startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(url)
)
)
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
e.sendSilentlyWithAcraWithName("activityNotFound > $url") e.sendSilentlyWithAcraWithName("activityNotFound > $url")
} }
@ -343,50 +351,42 @@ class ArticleFragment : Fragment(), DIAware {
} }
} }
@Deprecated("Deprecated in Java") override fun shouldInterceptRequest(
override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse? { view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
val url = request.url.toString()
val glideOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL) val glideOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL)
if (url.lowercase(Locale.US).contains(".jpg") || url.lowercase(Locale.US)
.contains(".jpeg") val supportedExtensions = ImageMimeType.values().map { it.extension }
) {
val matchingExtension = supportedExtensions.find {
url.lowercase(Locale.US).contains(it)
}
matchingExtension?.let {
try { try {
val image = val image = Glide.with(view)
Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() .asBitmap()
return WebResourceResponse( .apply(glideOptions)
IMAGE_JPG, .load(url)
"UTF-8", .submit()
getBitmapInputStream(image, Bitmap.CompressFormat.JPEG) .get()
)
} catch (e: ExecutionException) { val mimeType = ImageMimeType.findMimeTypeByExtension(it)
// Do nothing if (mimeType != null) {
} return WebResourceResponse(
} else if (url.lowercase(Locale.US).contains(".png")) { mimeType,
try { "UTF-8",
val image = getBitmapInputStream(image, Bitmap.CompressFormat.WEBP)
Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() )
return WebResourceResponse( }
IMAGE_JPG,
"UTF-8",
getBitmapInputStream(image, Bitmap.CompressFormat.PNG)
)
} catch (e: ExecutionException) {
// Do nothing
}
} else if (url.lowercase(Locale.US).contains(".webp")) {
try {
val image =
Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get()
return WebResourceResponse(
IMAGE_JPG,
"UTF-8",
getBitmapInputStream(image, Bitmap.CompressFormat.WEBP)
)
} catch (e: ExecutionException) { } catch (e: ExecutionException) {
// Do nothing // Do nothing
} }
} }
return super.shouldInterceptRequest(view, url) return super.shouldInterceptRequest(view, request)
} }
} }
} }

View File

@ -25,19 +25,10 @@ actual fun SelfossModel.Item.getThumbnail(baseUrl: String): String {
} }
actual fun SelfossModel.Item.getImages(): ArrayList<String> { actual fun SelfossModel.Item.getImages(): ArrayList<String> {
val allImages = ArrayList<String>() val doc = Jsoup.parse(content)
val images = doc.getElementsByTag("img")
for ( image in Jsoup.parse(content).getElementsByTag("img")) { return ArrayList(images.map { it.attr("src") })
val url = image.attr("src")
if (url.lowercase(Locale.US).contains(".jpg") ||
url.lowercase(Locale.US).contains(".jpeg") ||
url.lowercase(Locale.US).contains(".png") ||
url.lowercase(Locale.US).contains(".webp"))
{
allImages.add(url)
}
}
return allImages
} }
actual fun SelfossModel.Source.getIcon(baseUrl: String): String { actual fun SelfossModel.Source.getIcon(baseUrl: String): String {

View File

@ -8,4 +8,20 @@ enum class ItemType(val position: Int, val type: String) {
companion object { companion object {
fun fromInt(value: Int) = values().first { it.position == value } fun fromInt(value: Int) = values().first { it.position == value }
} }
}
enum class ImageMimeType(val extension: String, val mimeType: String) {
JPG(".jpg", "image/jpeg"),
JPEG(".jpeg", "image/jpeg"),
PNG(".png", "image/png"),
WEBP(".webp", "image/webp"),
GIF(".gif", "image/gif"),
SVG(".svg", "image/svg+xml"),
BMP(".bmp", "image/bmp"),
TIFF(".tiff", "image/tiff"),
TIF(".tif", "image/tiff");
companion object {
fun findMimeTypeByExtension(ext: String) = values().find { it.extension == ext }?.mimeType
}
} }