[Android] Add bidirectional text marking to handle RTL languages

Robert Stone git at videolan.org
Fri Jul 9 09:12:04 UTC 2021


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Tue Jul  6 22:15:09 2021 -0700| [ac3c41f97c3fa7acc2d275f11d91f82a550bd2df] | committer: Nicolas Pomepuy

Add bidirectional text marking to handle RTL languages

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

 .../src/main/java/org/videolan/tools/Strings.kt    | 26 ++++++++++++++++++++++
 .../src/org/videolan/vlc/media/MediaUtils.kt       |  4 ++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/Strings.kt b/application/tools/src/main/java/org/videolan/tools/Strings.kt
index b10ba9def..9385a7ae6 100644
--- a/application/tools/src/main/java/org/videolan/tools/Strings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Strings.kt
@@ -96,4 +96,30 @@ fun String.firstLetterUppercase(): String {
     return if (length == 1) {
         toUpperCase(Locale.getDefault())
     } else Character.toUpperCase(this[0]) + substring(1).toLowerCase(Locale.getDefault())
+}
+
+fun String.abbreviate(maxLen: Int): String {
+    val ellipsis = "\u2026"
+    val trimmed = this.trim()
+    return if (trimmed.length > maxLen) trimmed.take(maxLen - 1).trim().plus(ellipsis)
+    else trimmed
+}
+
+fun String.markBidi(): String {
+    //right-to-left isolate
+    val rli = "\u2067"
+    //pop directional isolate
+    val pdi = "\u2069"
+    for (ch in this) {
+        when (Character.getDirectionality(ch)) {
+            Character.DIRECTIONALITY_RIGHT_TO_LEFT,
+            Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC,
+            Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING,
+            Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE -> return rli + this + pdi
+            Character.DIRECTIONALITY_LEFT_TO_RIGHT,
+            Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING,
+            Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE -> return this
+        }
+    }
+    return this
 }
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index 2512da9cc..ac9ecf406 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -31,13 +31,13 @@ import org.videolan.resources.util.getFromMl
 import org.videolan.tools.AppScope
 import org.videolan.tools.Settings
 import org.videolan.tools.localBroadcastManager
+import org.videolan.tools.markBidi
 import org.videolan.tools.safeOffer
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.DialogActivity
 import org.videolan.vlc.gui.dialogs.SubtitleDownloaderDialogFragment
-import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.providers.medialibrary.FoldersProvider
 import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
 import org.videolan.vlc.providers.medialibrary.VideoGroupsProvider
@@ -390,7 +390,7 @@ object MediaUtils {
         val artist = getMediaArtist(ctx, media)
         val album = getMediaAlbum(ctx, media)
         val desc = if (artist != getMediaString(ctx, R.string.unknown_artist) && album != getMediaString(ctx, R.string.unknown_album))
-            getMediaDescription(artist, album) else ""
+            getMediaDescription(artist.markBidi(), album.markBidi()) else ""
         sb.append(if (desc.isNotEmpty()) (if (sb.isNotEmpty()) " • $desc" else desc) else "")
         //Replace full-spaces with thin-spaces (Unicode 2009)
         return sb.toString().replace(" ", "\u2009")



More information about the Android mailing list