From 20aab0ea62c9ade648152b60cdee5f7ffd3bf9a1 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 12 Sep 2023 00:17:41 +0200 Subject: [PATCH] Improve image handling --- .../android/fragments/ArticleFragment.kt | 82 +++++++++---------- .../utils/ModelConverters.kt | 15 +--- .../apps/readerforselfossv2/utils/Enums.kt | 16 ++++ 3 files changed, 60 insertions(+), 53 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index e497f9c..5ba8e9d 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -329,11 +329,19 @@ class ArticleFragment : Fragment(), DIAware { private fun handleImageLoading() { binding.webcontent.webViewClient = object : WebViewClient() { - @Deprecated("Deprecated in Java") - override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean { - return if (context != null && url.isUrlValid() && binding.webcontent.hitTestResult.type != WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { + override fun shouldOverrideUrlLoading( + view: WebView?, + 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 { - requireContext().startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) + requireContext().startActivity( + Intent( + Intent.ACTION_VIEW, + Uri.parse(url) + ) + ) } catch (e: ActivityNotFoundException) { e.sendSilentlyWithAcraWithName("activityNotFound > $url") } @@ -343,50 +351,42 @@ class ArticleFragment : Fragment(), DIAware { } } - @Deprecated("Deprecated in Java") - override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse? { + override fun shouldInterceptRequest( + view: WebView, + request: WebResourceRequest + ): WebResourceResponse? { + val url = request.url.toString() 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 { - val image = - Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() - return WebResourceResponse( - IMAGE_JPG, - "UTF-8", - getBitmapInputStream(image, Bitmap.CompressFormat.JPEG) - ) - } catch (e: ExecutionException) { - // Do nothing - } - } else if (url.lowercase(Locale.US).contains(".png")) { - try { - val image = - 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) - ) + val image = Glide.with(view) + .asBitmap() + .apply(glideOptions) + .load(url) + .submit() + .get() + + val mimeType = ImageMimeType.findMimeTypeByExtension(it) + if (mimeType != null) { + return WebResourceResponse( + mimeType, + "UTF-8", + getBitmapInputStream(image, Bitmap.CompressFormat.WEBP) + ) + } } catch (e: ExecutionException) { // Do nothing } } - return super.shouldInterceptRequest(view, url) + return super.shouldInterceptRequest(view, request) } } } diff --git a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index 8f9bf88..56f27e7 100644 --- a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -25,19 +25,10 @@ actual fun SelfossModel.Item.getThumbnail(baseUrl: String): String { } actual fun SelfossModel.Item.getImages(): ArrayList { - val allImages = ArrayList() + val doc = Jsoup.parse(content) + val images = doc.getElementsByTag("img") - for ( image in Jsoup.parse(content).getElementsByTag("img")) { - 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 + return ArrayList(images.map { it.attr("src") }) } actual fun SelfossModel.Source.getIcon(baseUrl: String): String { diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt index fd610d2..77e7c59 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt @@ -8,4 +8,20 @@ enum class ItemType(val position: Int, val type: String) { companion object { 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 + } } \ No newline at end of file