[Android] Incorporate enhanced title and subtitle formatting
Robert Stone
git at videolan.org
Wed Nov 20 06:47:54 UTC 2024
vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Wed Aug 7 21:56:07 2024 -0700| [b2f372db36418320a713f751fbef7a2224fcdf70] | committer: Duncan McNamara
Incorporate enhanced title and subtitle formatting
> https://code.videolan.org/videolan/vlc-android/commit/b2f372db36418320a713f751fbef7a2224fcdf70
---
.../src/org/videolan/vlc/PlaybackService.kt | 41 +++++++++++++++++++---
.../src/org/videolan/vlc/media/MediaUtils.kt | 10 +++---
.../src/org/videolan/vlc/util/Kextensions.kt | 4 +++
3 files changed, 46 insertions(+), 9 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 6dc34dcced..29dfd1bbfa 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -37,6 +37,7 @@ import android.content.IntentFilter
import android.content.SharedPreferences
import android.content.pm.ServiceInfo
import android.content.res.Configuration
+import android.graphics.Typeface
import android.media.AudioManager
import android.media.audiofx.AudioEffect
import android.net.Uri
@@ -50,6 +51,9 @@ import android.support.v4.media.MediaDescriptionCompat
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
+import android.text.Spannable
+import android.text.style.RelativeSizeSpan
+import android.text.style.StyleSpan
import android.util.Log
import android.view.View
import android.widget.TextView
@@ -149,6 +153,7 @@ import org.videolan.tools.SHOW_SEEK_IN_COMPACT_NOTIFICATION
import org.videolan.tools.Settings
import org.videolan.tools.getContextWithLocale
import org.videolan.tools.getResourceUri
+import org.videolan.tools.markBidi
import org.videolan.tools.readableSize
import org.videolan.vlc.car.VLCCarService
import org.videolan.vlc.gui.AudioPlayerContainerActivity
@@ -179,6 +184,7 @@ import org.videolan.vlc.util.ThumbnailsProvider
import org.videolan.vlc.util.Util
import org.videolan.vlc.util.VLCAudioFocusHelper
import org.videolan.vlc.util.awaitMedialibraryStarted
+import org.videolan.vlc.util.firstNotNullAsSpannable
import org.videolan.vlc.util.isSchemeHttpOrHttps
import org.videolan.vlc.util.isSchemeStreaming
import org.videolan.vlc.widget.MiniPlayerAppWidgetProvider
@@ -1153,10 +1159,35 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
putLong(MediaMetadataCompat.METADATA_KEY_DURATION, if (length != 0L) length else -1L)
}
if (carMode) {
- bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, chapterTitle ?: title)
- bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, displayMsg
- ?: MediaUtils.getDisplaySubtitle(ctx, media, currentMediaPosition, mediaListSize))
- bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, MediaUtils.getMediaAlbum(ctx, media))
+ var carTitle = title
+ var carSubtitle = MediaUtils.getDisplaySubtitle(ctx, media)
+ val queueInfo = MediaUtils.getQueuePosition(currentMediaPosition, mediaListSize)
+
+ /* Add the queue position information to the underlying string */
+ when (settings.getInt("android_auto_queue_info_pos_val", 3)) {
+ 1 -> carTitle = TextUtils.separatedString(queueInfo, carTitle.markBidi())
+ 2 -> carTitle = TextUtils.separatedString(carTitle.markBidi(), queueInfo)
+ 3 -> carSubtitle = TextUtils.separatedString(queueInfo, carSubtitle)
+ }
+ /**
+ * This section allows for variability in the title and subtitle contents.
+ */
+ // Set Display Title
+ val displayTitle = arrayOf(chapterTitle, carTitle).firstNotNullAsSpannable()?.also {
+ val titleScaleFactor = settings.getInt("android_auto_title_scale_val", 100) / 100f
+ it.setSpan(RelativeSizeSpan(titleScaleFactor), 0, it.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+ }
+ // Set Display Subtitle Font Size
+ val displaySubtitle = arrayOf(displayMsg, carSubtitle).firstNotNullAsSpannable()?.also {
+ val subTitleScaleFactor = settings.getInt("android_auto_subtitle_scale_val", 100) / 100f
+ it.setSpan(RelativeSizeSpan(subTitleScaleFactor), 0, it.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+ // Add italics for subtitle messages
+ if (displayMsg != null) { it.setSpan(StyleSpan(Typeface.ITALIC), 0, it.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) }
+ }
+ // Add the data to the Bundle
+ bob.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, displayTitle)
+ bob.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, displaySubtitle)
+ bob.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, MediaUtils.getMediaAlbum(ctx, media))
}
if (Permissions.canReadStorage(ctx) && coverOnLockscreen) {
val albumArtUri = when {
@@ -1559,7 +1590,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
mediaSession.setPlaybackState(playbackState)
}
- fun displaySubtitleMessage(vararg messages: String) {
+ fun displaySubtitleMessage(vararg messages: String?) {
var endTime = System.currentTimeMillis()
subtitleMessage.clear()
messages.forEach { msg ->
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 73ee8998d6..0d8f4ff60f 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -336,14 +336,16 @@ object MediaUtils {
return TextUtils.separatedString(prefix, suffix)
}
- fun getDisplaySubtitle(ctx: Context, media: MediaWrapper, mediaPosition: Int, mediaSize: Int): String {
+ fun getDisplaySubtitle(ctx: Context, media: MediaWrapper): String? {
val album = getMediaAlbum(ctx, media)
val artist = getMediaArtist(ctx, media)
val isAlbumUnknown = album == getMediaString(ctx, R.string.unknown_album)
val isArtistUnknown = artist == getMediaString(ctx, R.string.unknown_artist)
- val prefix = if (mediaSize > 1) "${mediaPosition + 1} / $mediaSize" else null
- val suffix = if (!isArtistUnknown && !isAlbumUnknown) TextUtils.separatedString('-', artist.markBidi(), album.markBidi()) else null
- return TextUtils.separatedString(prefix, suffix)
+ return if (!isArtistUnknown && !isAlbumUnknown) TextUtils.separatedString('-', artist.markBidi(), album.markBidi()) else null
+ }
+
+ fun getQueuePosition(mediaPosition: Int, mediaSize: Int): String? {
+ return if (mediaSize > 1) "${mediaPosition + 1} / $mediaSize" else null
}
fun getMediaTitle(mediaWrapper: MediaWrapper) = mediaWrapper.title
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 9ad431b215..ffc6072341 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -331,6 +331,10 @@ fun CharSequence.getPresenceDescriptionSpan(context: Context):SpannableString {
return string
}
+fun Array<out CharSequence?>.firstNotNullAsSpannable(): SpannableString? {
+ return firstNotNullOfOrNull { it?.let(::SpannableString) }
+}
+
fun Int.toPixel(): Int {
val metrics = Resources.getSystem().displayMetrics
val px = toFloat() * (metrics.densityDpi / 160f)
More information about the Android
mailing list