add support for svg
This commit is contained in:
parent
4c88f24af3
commit
7f681c7c6c
@ -81,6 +81,7 @@ dependencies {
|
|||||||
implementation("io.coil-kt:coil:2.7.0")
|
implementation("io.coil-kt:coil:2.7.0")
|
||||||
implementation("io.coil-kt:coil-gif:2.7.0")
|
implementation("io.coil-kt:coil-gif:2.7.0")
|
||||||
implementation("io.coil-kt:coil-video:2.7.0")
|
implementation("io.coil-kt:coil-video:2.7.0")
|
||||||
|
implementation("io.coil-kt:coil-svg:2.7.0")
|
||||||
implementation("androidx.gridlayout:gridlayout:1.0.0")
|
implementation("androidx.gridlayout:gridlayout:1.0.0")
|
||||||
implementation("io.noties.markwon:core:4.6.2")
|
implementation("io.noties.markwon:core:4.6.2")
|
||||||
androidTestImplementation("junit:junit:4.13.2")
|
androidTestImplementation("junit:junit:4.13.2")
|
||||||
|
@ -24,6 +24,7 @@ import coil.Coil
|
|||||||
import coil.ImageLoader
|
import coil.ImageLoader
|
||||||
import coil.decode.GifDecoder
|
import coil.decode.GifDecoder
|
||||||
import coil.decode.ImageDecoderDecoder
|
import coil.decode.ImageDecoderDecoder
|
||||||
|
import coil.decode.SvgDecoder
|
||||||
import coil.decode.VideoFrameDecoder
|
import coil.decode.VideoFrameDecoder
|
||||||
import coil.imageLoader
|
import coil.imageLoader
|
||||||
import coil.load
|
import coil.load
|
||||||
@ -104,6 +105,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
|
|||||||
add(GifDecoder.Factory())
|
add(GifDecoder.Factory())
|
||||||
}
|
}
|
||||||
add(VideoFrameDecoder.Factory())
|
add(VideoFrameDecoder.Factory())
|
||||||
|
add(SvgDecoder.Factory())
|
||||||
}
|
}
|
||||||
.build()
|
.build()
|
||||||
Coil.setImageLoader(imageLoader)
|
Coil.setImageLoader(imageLoader)
|
||||||
|
@ -47,10 +47,8 @@ class StickerSender(
|
|||||||
private val imageLoader: ImageLoader,
|
private val imageLoader: ImageLoader,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val supportedMimes: List<String> by lazy {
|
private val supportedMimes = this.currentInputEditorInfo?.contentMimeTypes ?: emptyArray()
|
||||||
Utils.getSupportedMimes()
|
private val packageName = this.currentInputEditorInfo?.packageName
|
||||||
.filter { isCommitContentSupported(this.currentInputEditorInfo, it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showToast(message: String) {
|
private fun showToast(message: String) {
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
@ -98,14 +96,45 @@ class StickerSender(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun sendSticker(file: File) {
|
fun sendSticker(file: File) {
|
||||||
val stickerType = Utils.getMimeType(file)
|
var stickerType = Utils.getMimeType(file) ?: "__unknown__"
|
||||||
if (stickerType == null || stickerType !in supportedMimes) {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Here we 'mock' the mime for misbehaving applications such as whatsapp
|
||||||
|
// if (this.packageName == "com.whatsapp"){
|
||||||
|
// stickerType = when (stickerType) {
|
||||||
|
// null -> null
|
||||||
|
// "image/webp" -> "image/webp.wasticker"
|
||||||
|
// "video/mp4" -> "video/x.looping_mp4"
|
||||||
|
// else -> stickerType
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
if ((stickerType in supportedMimes
|
||||||
|
|| "image/*" in supportedMimes && stickerType.startsWith("image/")
|
||||||
|
|| "video/*" in supportedMimes && stickerType.startsWith("video/"))
|
||||||
|
) {
|
||||||
|
// Deal with any exceptions here such as telegram messenger
|
||||||
|
if (this.packageName == "org.telegram.messenger" && stickerType == "image/svg+xml"
|
||||||
|
|| this.packageName == "com.discord" && stickerType == "image/svg+xml"
|
||||||
|
|| this.packageName == "im.vector.app" && stickerType == "image/svg+xml"
|
||||||
|
|| this.packageName == "com.google.android.keep" && stickerType == "image/svg+xml") {
|
||||||
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
|
doFallbackCommitContent(file)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Commit content normally
|
||||||
|
doCommitContent(stickerType, file)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Use fallback for unsupported types
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
doFallbackCommitContent(file)
|
doFallbackCommitContent(file)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
doCommitContent(stickerType, file)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openShareSheet(file: File) {
|
private fun openShareSheet(file: File) {
|
||||||
@ -129,14 +158,18 @@ class StickerSender(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun doFallbackCommitContent(file: File) {
|
private suspend fun doFallbackCommitContent(file: File) {
|
||||||
if ("image/png" !in supportedMimes) {
|
|
||||||
openShareSheet(file)
|
if ("image/png" in supportedMimes || "image/*" in supportedMimes) {
|
||||||
return
|
val compatSticker = createCompatSticker(file)
|
||||||
}
|
if (compatSticker != null) {
|
||||||
val compatSticker = createCompatSticker(file)
|
doCommitContent("image/png", compatSticker)
|
||||||
if (compatSticker != null) {
|
return
|
||||||
doCommitContent("image/png", compatSticker)
|
}
|
||||||
}
|
}
|
||||||
|
openShareSheet(file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -157,6 +190,7 @@ class StickerSender(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (currentInputConnection != null && currentInputEditorInfo != null) {
|
if (currentInputConnection != null && currentInputEditorInfo != null) {
|
||||||
|
|
||||||
InputConnectionCompat.commitContent(
|
InputConnectionCompat.commitContent(
|
||||||
currentInputConnection,
|
currentInputConnection,
|
||||||
currentInputEditorInfo,
|
currentInputEditorInfo,
|
||||||
@ -167,16 +201,4 @@ class StickerSender(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the sticker is supported by the receiver
|
|
||||||
*
|
|
||||||
* @param editorInfo: EditorInfo - the editor/ receiver
|
|
||||||
* @param mimeType: String - the image mimetype
|
|
||||||
* @return boolean - is the mimetype supported?
|
|
||||||
*/
|
|
||||||
private fun isCommitContentSupported(editorInfo: EditorInfo?, mimeType: String?): Boolean {
|
|
||||||
return editorInfo?.packageName != null && mimeType != null && currentInputConnection != null &&
|
|
||||||
EditorInfoCompat.getContentMimeTypes(editorInfo)
|
|
||||||
.any { ClipDescription.compareMimeTypes(mimeType, it) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ object Utils {
|
|||||||
*/
|
*/
|
||||||
fun getSupportedMimes(): MutableList<String> {
|
fun getSupportedMimes(): MutableList<String> {
|
||||||
return mutableListOf(
|
return mutableListOf(
|
||||||
|
"image/svg+xml",
|
||||||
"image/gif",
|
"image/gif",
|
||||||
"image/png",
|
"image/png",
|
||||||
"image/webp",
|
"image/webp",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user