Skip to content

Commit 7f681c7

Browse files
committed
add support for svg
1 parent 4c88f24 commit 7f681c7

4 files changed

Lines changed: 53 additions & 27 deletions

File tree

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ dependencies {
8181
implementation("io.coil-kt:coil:2.7.0")
8282
implementation("io.coil-kt:coil-gif:2.7.0")
8383
implementation("io.coil-kt:coil-video:2.7.0")
84+
implementation("io.coil-kt:coil-svg:2.7.0")
8485
implementation("androidx.gridlayout:gridlayout:1.0.0")
8586
implementation("io.noties.markwon:core:4.6.2")
8687
androidTestImplementation("junit:junit:4.13.2")

app/src/main/java/com/fredhappyface/ewesticker/ImageKeyboard.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import coil.Coil
2424
import coil.ImageLoader
2525
import coil.decode.GifDecoder
2626
import coil.decode.ImageDecoderDecoder
27+
import coil.decode.SvgDecoder
2728
import coil.decode.VideoFrameDecoder
2829
import coil.imageLoader
2930
import coil.load
@@ -104,6 +105,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
104105
add(GifDecoder.Factory())
105106
}
106107
add(VideoFrameDecoder.Factory())
108+
add(SvgDecoder.Factory())
107109
}
108110
.build()
109111
Coil.setImageLoader(imageLoader)

app/src/main/java/com/fredhappyface/ewesticker/utilities/StickerSender.kt

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ class StickerSender(
4747
private val imageLoader: ImageLoader,
4848
) {
4949

50-
private val supportedMimes: List<String> by lazy {
51-
Utils.getSupportedMimes()
52-
.filter { isCommitContentSupported(this.currentInputEditorInfo, it) }
53-
}
50+
private val supportedMimes = this.currentInputEditorInfo?.contentMimeTypes ?: emptyArray()
51+
private val packageName = this.currentInputEditorInfo?.packageName
5452

5553
private fun showToast(message: String) {
5654
CoroutineScope(Dispatchers.Main).launch {
@@ -98,14 +96,45 @@ class StickerSender(
9896
}
9997

10098
fun sendSticker(file: File) {
101-
val stickerType = Utils.getMimeType(file)
102-
if (stickerType == null || stickerType !in supportedMimes) {
99+
var stickerType = Utils.getMimeType(file) ?: "__unknown__"
100+
101+
102+
103+
104+
105+
// Here we 'mock' the mime for misbehaving applications such as whatsapp
106+
// if (this.packageName == "com.whatsapp"){
107+
// stickerType = when (stickerType) {
108+
// null -> null
109+
// "image/webp" -> "image/webp.wasticker"
110+
// "video/mp4" -> "video/x.looping_mp4"
111+
// else -> stickerType
112+
// }
113+
// }
114+
115+
if ((stickerType in supportedMimes
116+
|| "image/*" in supportedMimes && stickerType.startsWith("image/")
117+
|| "video/*" in supportedMimes && stickerType.startsWith("video/"))
118+
) {
119+
// Deal with any exceptions here such as telegram messenger
120+
if (this.packageName == "org.telegram.messenger" && stickerType == "image/svg+xml"
121+
|| this.packageName == "com.discord" && stickerType == "image/svg+xml"
122+
|| this.packageName == "im.vector.app" && stickerType == "image/svg+xml"
123+
|| this.packageName == "com.google.android.keep" && stickerType == "image/svg+xml") {
124+
CoroutineScope(Dispatchers.Main).launch {
125+
doFallbackCommitContent(file)
126+
}
127+
} else {
128+
// Commit content normally
129+
doCommitContent(stickerType, file)
130+
}
131+
} else {
132+
// Use fallback for unsupported types
103133
CoroutineScope(Dispatchers.Main).launch {
104134
doFallbackCommitContent(file)
105135
}
106-
} else {
107-
doCommitContent(stickerType, file)
108136
}
137+
109138
}
110139

111140
private fun openShareSheet(file: File) {
@@ -129,14 +158,18 @@ class StickerSender(
129158
}
130159

131160
private suspend fun doFallbackCommitContent(file: File) {
132-
if ("image/png" !in supportedMimes) {
133-
openShareSheet(file)
134-
return
135-
}
136-
val compatSticker = createCompatSticker(file)
137-
if (compatSticker != null) {
138-
doCommitContent("image/png", compatSticker)
161+
162+
if ("image/png" in supportedMimes || "image/*" in supportedMimes) {
163+
val compatSticker = createCompatSticker(file)
164+
if (compatSticker != null) {
165+
doCommitContent("image/png", compatSticker)
166+
return
167+
}
139168
}
169+
openShareSheet(file)
170+
171+
172+
140173
}
141174

142175
/**
@@ -157,6 +190,7 @@ class StickerSender(
157190
)
158191

159192
if (currentInputConnection != null && currentInputEditorInfo != null) {
193+
160194
InputConnectionCompat.commitContent(
161195
currentInputConnection,
162196
currentInputEditorInfo,
@@ -167,16 +201,4 @@ class StickerSender(
167201
}
168202
}
169203

170-
/**
171-
* Check if the sticker is supported by the receiver
172-
*
173-
* @param editorInfo: EditorInfo - the editor/ receiver
174-
* @param mimeType: String - the image mimetype
175-
* @return boolean - is the mimetype supported?
176-
*/
177-
private fun isCommitContentSupported(editorInfo: EditorInfo?, mimeType: String?): Boolean {
178-
return editorInfo?.packageName != null && mimeType != null && currentInputConnection != null &&
179-
EditorInfoCompat.getContentMimeTypes(editorInfo)
180-
.any { ClipDescription.compareMimeTypes(mimeType, it) }
181-
}
182204
}

app/src/main/java/com/fredhappyface/ewesticker/utilities/Utils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object Utils {
2222
*/
2323
fun getSupportedMimes(): MutableList<String> {
2424
return mutableListOf(
25+
"image/svg+xml",
2526
"image/gif",
2627
"image/png",
2728
"image/webp",

0 commit comments

Comments
 (0)