@@ -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}
0 commit comments