From 16ee30686456ac5eb8447e89aef62cfedd12bb9d Mon Sep 17 00:00:00 2001 From: Kieran W <41634689+FredHappyface@users.noreply.github.com> Date: Sat, 8 Feb 2025 21:24:15 +0000 Subject: [PATCH] code tidy up for sticker sender and always encode svgs to png --- .../fredhappyface/ewesticker/ImageKeyboard.kt | 8 ++-- .../fredhappyface/ewesticker/MainActivity.kt | 3 +- .../ewesticker/utilities/StickerSender.kt | 44 ++++++------------- 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/fredhappyface/ewesticker/ImageKeyboard.kt b/app/src/main/java/com/fredhappyface/ewesticker/ImageKeyboard.kt index fef6a7b..641133b 100644 --- a/app/src/main/java/com/fredhappyface/ewesticker/ImageKeyboard.kt +++ b/app/src/main/java/com/fredhappyface/ewesticker/ImageKeyboard.kt @@ -111,7 +111,8 @@ class ImageKeyboard : InputMethodService(), StickerClickListener { Coil.setImageLoader(imageLoader) // Shared Preferences this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext) - this.backupSharedPreferences = this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE) + this.backupSharedPreferences = + this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE) this.restoreOnClose = this.backupSharedPreferences.getBoolean("restoreOnClose", false) this.vertical = this.backupSharedPreferences.getBoolean("vertical", false) this.scroll = this.backupSharedPreferences.getBoolean("scroll", false) @@ -261,7 +262,8 @@ class ImageKeyboard : InputMethodService(), StickerClickListener { stickers, this, gestureDetector, - this.vibrate) + this.vibrate + ) val layoutManager = GridLayoutManager( this, iconsPerX, @@ -297,7 +299,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener { packContent.layoutParams.height - ( resources.getDimension(R.dimen.qwerty_row_height) + - resources.getDimension(R.dimen.qwerty_row_height) * 4 + resources.getDimension(R.dimen.qwerty_row_height) * 4 ) searchResults.layoutParams.height = searchResultsHeight.toInt() diff --git a/app/src/main/java/com/fredhappyface/ewesticker/MainActivity.kt b/app/src/main/java/com/fredhappyface/ewesticker/MainActivity.kt index 0749af4..4d54253 100644 --- a/app/src/main/java/com/fredhappyface/ewesticker/MainActivity.kt +++ b/app/src/main/java/com/fredhappyface/ewesticker/MainActivity.kt @@ -52,7 +52,8 @@ class MainActivity : AppCompatActivity() { // Set late-init attrs this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) - this.backupSharedPreferences = this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE) + this.backupSharedPreferences = + this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE) this.contextView = findViewById(R.id.activityMainRoot) this.toaster = Toaster(baseContext) refreshStickerDirPath() diff --git a/app/src/main/java/com/fredhappyface/ewesticker/utilities/StickerSender.kt b/app/src/main/java/com/fredhappyface/ewesticker/utilities/StickerSender.kt index 7cdd693..ce58f59 100644 --- a/app/src/main/java/com/fredhappyface/ewesticker/utilities/StickerSender.kt +++ b/app/src/main/java/com/fredhappyface/ewesticker/utilities/StickerSender.kt @@ -4,11 +4,11 @@ import android.content.ClipDescription import android.content.Context import android.content.Intent import android.graphics.Bitmap +import android.util.Log import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputConnection import androidx.core.content.FileProvider import androidx.core.graphics.drawable.toBitmap -import androidx.core.view.inputmethod.EditorInfoCompat import androidx.core.view.inputmethod.InputConnectionCompat import androidx.core.view.inputmethod.InputContentInfoCompat import coil.ImageLoader @@ -96,40 +96,22 @@ class StickerSender( } fun sendSticker(file: File) { - var stickerType = Utils.getMimeType(file) ?: "__unknown__" - - - - - - // 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 -// } -// } + val stickerType = Utils.getMimeType(file) ?: "__unknown__" + // Try and only send as is if the app explicitly supports it + // Note: Many apps do not support svg, so send as png regardless! if ((stickerType in supportedMimes || "image/*" in supportedMimes && stickerType.startsWith("image/") || "video/*" in supportedMimes && stickerType.startsWith("video/")) + && stickerType != "image/svg+xml" ) { - // 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") { + + if (!doCommitContent(stickerType, file)) { CoroutineScope(Dispatchers.Main).launch { doFallbackCommitContent(file) } - } else { - // Commit content normally - doCommitContent(stickerType, file) } } else { - // Use fallback for unsupported types CoroutineScope(Dispatchers.Main).launch { doFallbackCommitContent(file) } @@ -162,14 +144,14 @@ class StickerSender( if ("image/png" in supportedMimes || "image/*" in supportedMimes) { val compatSticker = createCompatSticker(file) if (compatSticker != null) { - doCommitContent("image/png", compatSticker) + if (!doCommitContent("image/png", compatSticker)) { + openShareSheet(file) + } return } } openShareSheet(file) - - } /** @@ -178,7 +160,8 @@ class StickerSender( * @param mimeType String * @param file File */ - private fun doCommitContent(mimeType: String, file: File) { + private fun doCommitContent(mimeType: String, file: File): Boolean { +// Log.d("QWERTY", "Sending ${file.name} ($mimeType) to ${this.packageName}") val inputContentInfoCompat = InputContentInfoCompat( FileProvider.getUriForFile( context, @@ -191,7 +174,7 @@ class StickerSender( if (currentInputConnection != null && currentInputEditorInfo != null) { - InputConnectionCompat.commitContent( + return InputConnectionCompat.commitContent( currentInputConnection, currentInputEditorInfo, inputContentInfoCompat, @@ -199,6 +182,7 @@ class StickerSender( null, ) } + return false } }