Skip to content

Commit

Permalink
support image url preview
Browse files Browse the repository at this point in the history
  • Loading branch information
SolinariWu committed May 5, 2021
1 parent f6a5d32 commit a89a0f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
6 changes: 3 additions & 3 deletions BahaUrlPreview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3'
implementation 'org.jsoup:jsoup:1.13.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
Expand Down Expand Up @@ -82,7 +82,7 @@ afterEvaluate {
release(MavenPublication) {
groupId group
artifactId 'Baha-UrlPreview'
version '1.0.0'
version '1.0.1'

if (project.plugins.findPlugin("com.android.library")) {
from components.release
Expand All @@ -105,7 +105,7 @@ afterEvaluate {
}
developers {
developer {
name = 'Oneup'
name = 'Solinari'
email = 'solinari@gamer.com.tw'
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.baha.url.preview

import android.net.Uri
import kotlinx.coroutines.*
import java.util.*

class BahaUrlPreview(val url: String, var callback: IUrlPreviewCallback?) {
val scope = CoroutineScope(Job() + Dispatchers.Main)
private val imageExtensionArray = arrayOf(".gif", ".png", ".jpg", ".jpeg", ".bmp", ".webp")

fun fetchUrlPreview(timeOut: Int = 30000) {
val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
Expand All @@ -15,12 +18,29 @@ class BahaUrlPreview(val url: String, var callback: IUrlPreviewCallback?) {
}

private suspend fun fetch(timeOut: Int = 30000) {
val document = get(url, timeOut)
val urlInfoItem = parseHtml(document)
urlInfoItem.url = url
lateinit var urlInfoItem: UrlInfoItem
if (checkIsImageUrl()) {
urlInfoItem = UrlInfoItem(url = url, image = url)
} else {
val document = getDocument(url, timeOut)
urlInfoItem = parseHtml(document)
urlInfoItem.url = url
}
callback?.onComplete(urlInfoItem)
}

private fun checkIsImageUrl(): Boolean {
val uri = Uri.parse(url)
var isImage = false
for (imageExtension in imageExtensionArray) {
if (uri.path != null && uri.path!!.toLowerCase(Locale.getDefault()).endsWith(imageExtension)) {
isImage = true
break
}
}
return isImage
}

fun cleanUp() {
scope.cancel()
callback = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private val META_ITEMPROP_IMAGE = arrayOf("image", "\"image\"", "'image'")

private const val CONTENT = "content"

suspend fun get(url: String, timeOut: Int = 30000): Document =
suspend fun getDocument(url: String, timeOut: Int = 30000): Document =
withContext(Dispatchers.IO) {
return@withContext Jsoup.connect(url)
.timeout(timeOut)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class MainActivity : AppCompatActivity() {
"url preview failed!",
Toast.LENGTH_SHORT
).show()
binding.progressBar.visibility = View.GONE
}
})

Expand Down

0 comments on commit a89a0f0

Please sign in to comment.