code tidy up for sticker sender and always encode svgs to png

This commit is contained in:
Kieran W 2025-02-08 21:24:15 +00:00
parent 7f681c7c6c
commit 16ee306864
3 changed files with 21 additions and 34 deletions

View File

@ -111,7 +111,8 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
Coil.setImageLoader(imageLoader) Coil.setImageLoader(imageLoader)
// Shared Preferences // Shared Preferences
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext) 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.restoreOnClose = this.backupSharedPreferences.getBoolean("restoreOnClose", false)
this.vertical = this.backupSharedPreferences.getBoolean("vertical", false) this.vertical = this.backupSharedPreferences.getBoolean("vertical", false)
this.scroll = this.backupSharedPreferences.getBoolean("scroll", false) this.scroll = this.backupSharedPreferences.getBoolean("scroll", false)
@ -261,7 +262,8 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
stickers, stickers,
this, this,
gestureDetector, gestureDetector,
this.vibrate) this.vibrate
)
val layoutManager = GridLayoutManager( val layoutManager = GridLayoutManager(
this, this,
iconsPerX, iconsPerX,

View File

@ -52,7 +52,8 @@ class MainActivity : AppCompatActivity() {
// Set late-init attrs // Set late-init attrs
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) 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.contextView = findViewById(R.id.activityMainRoot)
this.toaster = Toaster(baseContext) this.toaster = Toaster(baseContext)
refreshStickerDirPath() refreshStickerDirPath()

View File

@ -4,11 +4,11 @@ import android.content.ClipDescription
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.util.Log
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection import android.view.inputmethod.InputConnection
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.inputmethod.EditorInfoCompat
import androidx.core.view.inputmethod.InputConnectionCompat import androidx.core.view.inputmethod.InputConnectionCompat
import androidx.core.view.inputmethod.InputContentInfoCompat import androidx.core.view.inputmethod.InputContentInfoCompat
import coil.ImageLoader import coil.ImageLoader
@ -96,40 +96,22 @@ class StickerSender(
} }
fun sendSticker(file: File) { fun sendSticker(file: File) {
var stickerType = Utils.getMimeType(file) ?: "__unknown__" val 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
// }
// }
// 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 if ((stickerType in supportedMimes
|| "image/*" in supportedMimes && stickerType.startsWith("image/") || "image/*" in supportedMimes && stickerType.startsWith("image/")
|| "video/*" in supportedMimes && stickerType.startsWith("video/")) || "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" if (!doCommitContent(stickerType, file)) {
|| 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 { CoroutineScope(Dispatchers.Main).launch {
doFallbackCommitContent(file) doFallbackCommitContent(file)
} }
} else {
// Commit content normally
doCommitContent(stickerType, file)
} }
} else { } else {
// Use fallback for unsupported types
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
doFallbackCommitContent(file) doFallbackCommitContent(file)
} }
@ -162,14 +144,14 @@ class StickerSender(
if ("image/png" in supportedMimes || "image/*" in supportedMimes) { if ("image/png" in supportedMimes || "image/*" in supportedMimes) {
val compatSticker = createCompatSticker(file) val compatSticker = createCompatSticker(file)
if (compatSticker != null) { if (compatSticker != null) {
doCommitContent("image/png", compatSticker) if (!doCommitContent("image/png", compatSticker)) {
openShareSheet(file)
}
return return
} }
} }
openShareSheet(file) openShareSheet(file)
} }
/** /**
@ -178,7 +160,8 @@ class StickerSender(
* @param mimeType String * @param mimeType String
* @param file File * @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( val inputContentInfoCompat = InputContentInfoCompat(
FileProvider.getUriForFile( FileProvider.getUriForFile(
context, context,
@ -191,7 +174,7 @@ class StickerSender(
if (currentInputConnection != null && currentInputEditorInfo != null) { if (currentInputConnection != null && currentInputEditorInfo != null) {
InputConnectionCompat.commitContent( return InputConnectionCompat.commitContent(
currentInputConnection, currentInputConnection,
currentInputEditorInfo, currentInputEditorInfo,
inputContentInfoCompat, inputContentInfoCompat,
@ -199,6 +182,7 @@ class StickerSender(
null, null,
) )
} }
return false
} }
} }