Skip to content

Commit 16ee306

Browse files
committed
code tidy up for sticker sender and always encode svgs to png
1 parent 7f681c7 commit 16ee306

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
111111
Coil.setImageLoader(imageLoader)
112112
// Shared Preferences
113113
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext)
114-
this.backupSharedPreferences = this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE)
114+
this.backupSharedPreferences =
115+
this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE)
115116
this.restoreOnClose = this.backupSharedPreferences.getBoolean("restoreOnClose", false)
116117
this.vertical = this.backupSharedPreferences.getBoolean("vertical", false)
117118
this.scroll = this.backupSharedPreferences.getBoolean("scroll", false)
@@ -261,7 +262,8 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
261262
stickers,
262263
this,
263264
gestureDetector,
264-
this.vibrate)
265+
this.vibrate
266+
)
265267
val layoutManager = GridLayoutManager(
266268
this,
267269
iconsPerX,
@@ -297,7 +299,7 @@ class ImageKeyboard : InputMethodService(), StickerClickListener {
297299
packContent.layoutParams.height -
298300
(
299301
resources.getDimension(R.dimen.qwerty_row_height) +
300-
resources.getDimension(R.dimen.qwerty_row_height) * 4
302+
resources.getDimension(R.dimen.qwerty_row_height) * 4
301303
)
302304

303305
searchResults.layoutParams.height = searchResultsHeight.toInt()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class MainActivity : AppCompatActivity() {
5252

5353
// Set late-init attrs
5454
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
55-
this.backupSharedPreferences = this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE)
55+
this.backupSharedPreferences =
56+
this.getSharedPreferences("backup_prefs", Context.MODE_PRIVATE)
5657
this.contextView = findViewById(R.id.activityMainRoot)
5758
this.toaster = Toaster(baseContext)
5859
refreshStickerDirPath()

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

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import android.content.ClipDescription
44
import android.content.Context
55
import android.content.Intent
66
import android.graphics.Bitmap
7+
import android.util.Log
78
import android.view.inputmethod.EditorInfo
89
import android.view.inputmethod.InputConnection
910
import androidx.core.content.FileProvider
1011
import androidx.core.graphics.drawable.toBitmap
11-
import androidx.core.view.inputmethod.EditorInfoCompat
1212
import androidx.core.view.inputmethod.InputConnectionCompat
1313
import androidx.core.view.inputmethod.InputContentInfoCompat
1414
import coil.ImageLoader
@@ -96,40 +96,22 @@ class StickerSender(
9696
}
9797

9898
fun sendSticker(file: File) {
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-
// }
99+
val stickerType = Utils.getMimeType(file) ?: "__unknown__"
114100

101+
// Try and only send as is if the app explicitly supports it
102+
// Note: Many apps do not support svg, so send as png regardless!
115103
if ((stickerType in supportedMimes
116104
|| "image/*" in supportedMimes && stickerType.startsWith("image/")
117105
|| "video/*" in supportedMimes && stickerType.startsWith("video/"))
106+
&& stickerType != "image/svg+xml"
118107
) {
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") {
108+
109+
if (!doCommitContent(stickerType, file)) {
124110
CoroutineScope(Dispatchers.Main).launch {
125111
doFallbackCommitContent(file)
126112
}
127-
} else {
128-
// Commit content normally
129-
doCommitContent(stickerType, file)
130113
}
131114
} else {
132-
// Use fallback for unsupported types
133115
CoroutineScope(Dispatchers.Main).launch {
134116
doFallbackCommitContent(file)
135117
}
@@ -162,14 +144,14 @@ class StickerSender(
162144
if ("image/png" in supportedMimes || "image/*" in supportedMimes) {
163145
val compatSticker = createCompatSticker(file)
164146
if (compatSticker != null) {
165-
doCommitContent("image/png", compatSticker)
147+
if (!doCommitContent("image/png", compatSticker)) {
148+
openShareSheet(file)
149+
}
166150
return
167151
}
168152
}
169153
openShareSheet(file)
170154

171-
172-
173155
}
174156

175157
/**
@@ -178,7 +160,8 @@ class StickerSender(
178160
* @param mimeType String
179161
* @param file File
180162
*/
181-
private fun doCommitContent(mimeType: String, file: File) {
163+
private fun doCommitContent(mimeType: String, file: File): Boolean {
164+
// Log.d("QWERTY", "Sending ${file.name} ($mimeType) to ${this.packageName}")
182165
val inputContentInfoCompat = InputContentInfoCompat(
183166
FileProvider.getUriForFile(
184167
context,
@@ -191,14 +174,15 @@ class StickerSender(
191174

192175
if (currentInputConnection != null && currentInputEditorInfo != null) {
193176

194-
InputConnectionCompat.commitContent(
177+
return InputConnectionCompat.commitContent(
195178
currentInputConnection,
196179
currentInputEditorInfo,
197180
inputContentInfoCompat,
198181
InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION,
199182
null,
200183
)
201184
}
185+
return false
202186
}
203187

204188
}

0 commit comments

Comments
 (0)