[Android] Prevent IllegalArgumentException when text contains emoji on Samsung devices

Nicolas Pomepuy git at videolan.org
Tue Apr 5 12:24:47 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Apr  4 14:13:05 2022 +0200| [52ee2f2ed4ceaacb0f1a84a0354437ffa8eaeab6] | committer: Duncan McNamara

Prevent IllegalArgumentException when text contains emoji on Samsung devices

> https://code.videolan.org/videolan/vlc-android/commit/52ee2f2ed4ceaacb0f1a84a0354437ffa8eaeab6
---

 .../src/org/videolan/vlc/util/Kextensions.kt          | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 02fb5f44d..7dbd75c01 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -21,6 +21,7 @@ import androidx.appcompat.widget.AppCompatTextView
 import androidx.core.content.FileProvider
 import androidx.core.net.toUri
 import androidx.core.text.PrecomputedTextCompat
+import androidx.core.text.toSpannable
 import androidx.core.widget.TextViewCompat
 import androidx.databinding.BindingAdapter
 import androidx.fragment.app.Fragment
@@ -48,6 +49,7 @@ import org.videolan.tools.AppScope
 import org.videolan.tools.isStarted
 import org.videolan.vlc.R
 import java.io.File
+import java.lang.ref.WeakReference
 import java.net.URI
 import java.net.URISyntaxException
 import java.security.SecureRandom
@@ -164,7 +166,7 @@ fun asyncText(view: TextView, text: CharSequence?) {
     }
     view.visibility = View.VISIBLE
     val params = TextViewCompat.getTextMetricsParams(view)
-    (view as AppCompatTextView).setTextFuture(PrecomputedTextCompat.getTextFuture(text, params, null))
+    setTextAsync(view, text, params)
 }
 
 @BindingAdapter("app:asyncText", requireAll = false)
@@ -185,7 +187,20 @@ fun asyncTextItem(view: TextView, item: MediaLibraryItem?) {
     }
     view.visibility = View.VISIBLE
     val params = TextViewCompat.getTextMetricsParams(view)
-    (view as AppCompatTextView).setTextFuture(PrecomputedTextCompat.getTextFuture(text, params, null))
+    setTextAsync(view, text, params)
+}
+
+private fun setTextAsync(view: TextView, text: CharSequence, params: PrecomputedTextCompat.Params) {
+    val ref = WeakReference(view)
+    AppScope.launch(Dispatchers.Default) {
+        val pText = PrecomputedTextCompat.create(text, params)
+        val result = pText.toSpannable()
+        withContext(Dispatchers.Main) {
+            ref.get()?.let { textView ->
+                textView.text = result
+            }
+        }
+    }
 }
 
 const val folderReplacementMarker = "§*§"



More information about the Android mailing list