Improve image handling
This commit is contained in:
parent
dcb9b1cb1f
commit
20aab0ea62
@ -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 }
|
||||||
) {
|
|
||||||
try {
|
val matchingExtension = supportedExtensions.find {
|
||||||
val image =
|
url.lowercase(Locale.US).contains(it)
|
||||||
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")) {
|
|
||||||
|
matchingExtension?.let {
|
||||||
try {
|
try {
|
||||||
val image =
|
val image = Glide.with(view)
|
||||||
Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get()
|
.asBitmap()
|
||||||
|
.apply(glideOptions)
|
||||||
|
.load(url)
|
||||||
|
.submit()
|
||||||
|
.get()
|
||||||
|
|
||||||
|
val mimeType = ImageMimeType.findMimeTypeByExtension(it)
|
||||||
|
if (mimeType != null) {
|
||||||
return WebResourceResponse(
|
return WebResourceResponse(
|
||||||
IMAGE_JPG,
|
mimeType,
|
||||||
"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",
|
"UTF-8",
|
||||||
getBitmapInputStream(image, Bitmap.CompressFormat.WEBP)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -9,3 +9,19 @@ enum class ItemType(val position: Int, val type: String) {
|
|||||||
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user