[Android] Change the way we format the media chapter titles

Nicolas Pomepuy git at videolan.org
Tue Feb 8 14:39:35 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Jan 24 10:24:41 2022 +0100| [c385be4472cad33526567c5bb91dd369ea369c28] | committer: Nicolas Pomepuy

Change the way we format the media chapter titles

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

 application/resources/src/main/res/values/strings.xml        |  1 -
 .../vlc-android/src/org/videolan/vlc/PlaybackService.kt      |  9 ++++++++-
 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt            |  4 ++--
 .../src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt  |  6 ++----
 .../src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt      |  2 +-
 .../vlc-android/src/org/videolan/vlc/util/TextUtils.kt       | 12 ++++++++++++
 6 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index 27e8f59ef..546157f88 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -495,7 +495,6 @@
     <string name="playlist_save">Save Playlist</string>
     <string name="playlist_name_hint">Playlist name</string>
     <string name="go_to_chapter">Go to chapter…</string>
-    <string name="chapter">Chapter</string>
     <string name="current_chapter">Chapter: %s</string>
     <string name="resume_from_position">Resume from last position</string>
     <string name="confirm_resume">Resume from last position?</string>
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index c525af71a..a67090221 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -388,6 +388,13 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
             return next?.artworkMrl
         }
 
+    suspend fun getCurrentChapter(formatted:Boolean = false):String? {
+        val currentChapter = withContext(Dispatchers.IO) {
+             getChapters(-1)?.get(chapterIdx)?.name
+        }
+        return if (formatted) TextUtils.formatChapterTitle(this, currentChapter) else currentChapter
+    }
+
     suspend fun trackInfo(): String? {
         val mediaWrapper = playlistManager.getCurrentMedia() ?: return null
         val media = withContext(Dispatchers.IO) {
@@ -979,7 +986,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
         val ctx = this
         val length = length
         lastLength = length
-        val chapterTitle = if (lastChaptersCount > 0) getChapters(-1)?.elementAtOrNull(lastChapter)?.name else null
+        val chapterTitle = if (lastChaptersCount > 0) getCurrentChapter(true) else null
         val displayMsg = subtitleMessage.poll()
         val bob = withContext(Dispatchers.Default) {
             val carMode = AndroidDevices.isCarMode(ctx)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index ecb559441..f50eefc84 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -309,8 +309,8 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
         binding.audioMediaSwitcher.updateMedia(playlistModel.service)
         binding.coverMediaSwitcher.updateMedia(playlistModel.service)
 
-        val chapter = playlistModel.service!!.getChapters(-1)?.get(playlistModel.service!!.chapterIdx)?.name
-        binding.songTitle?.text = if (!chapter.isNullOrEmpty()) getString(R.string.current_chapter, chapter) else  playlistModel.title
+        val chapter = playlistModel.service?.getCurrentChapter(true)
+        binding.songTitle?.text = if (!chapter.isNullOrEmpty()) chapter else  playlistModel.title
         binding.songSubtitle?.text = if (!chapter.isNullOrEmpty()) TextUtils.separatedString(playlistModel.title, playlistModel.artist) else TextUtils.separatedString(playlistModel.artist, playlistModel.album)
         binding.songTitle?.isSelected = true
         binding.songSubtitle?.isSelected = true
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
index aafd4d6e4..c76324fe2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
@@ -37,6 +37,7 @@ import org.videolan.medialibrary.Tools
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.ChapterListItemBinding
+import org.videolan.vlc.util.TextUtils
 import org.videolan.vlc.util.launchWhenStarted
 import java.util.*
 
@@ -78,10 +79,7 @@ class SelectChapterDialog : VLCBottomSheetDialogFragment(), IOnChapterSelectedLi
         val chapterData = ArrayList<Chapter>()
 
         for (i in 0 until chaptersCount) {
-            val name: String = if (chapters!![i].name == null || chapters[i].name == "")
-                resources.getString(R.string.chapter) + " " + i
-            else
-                chapters[i].name
+            val name: String = TextUtils.formatChapterTitle(requireActivity(), chapters!![i].name)
             chapterData.add(Chapter(name, Tools.millisToString(chapters[i].timeOffset)))
         }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt b/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
index df5444c99..cf79a2fb1 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt
@@ -108,7 +108,7 @@ abstract class AudioMediaSwitcher(context: Context, attrs: AttributeSet) : Fling
             hasPrevious = true
         }
         val chapter = service.getChapters(-1)?.get(service.chapterIdx)?.name
-        if (service.hasMedia()) addMediaView(inflater, if (!chapter.isNullOrEmpty()) service.getString(R.string.current_chapter, chapter) else  service.title, if (!chapter.isNullOrEmpty()) service.title else service.artist, if (!chapter.isNullOrEmpty()) service.artist else service.album, coverCurrent, trackInfo)
+        if (service.hasMedia()) addMediaView(inflater, if (!chapter.isNullOrEmpty()) service.getCurrentChapter(true) else  service.title, if (!chapter.isNullOrEmpty()) service.title else service.artist, if (!chapter.isNullOrEmpty()) service.artist else service.album, coverCurrent, trackInfo)
         if (service.hasNext()) addMediaView(inflater, service.titleNext, service.artistNext, service.albumNext, coverNext, nextTrackInfo)
 
         if (service.hasPrevious() && service.hasMedia()) {
diff --git a/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt b/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
index 14b73ae7d..20e9ff6ec 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/TextUtils.kt
@@ -24,6 +24,9 @@
 
 package org.videolan.vlc.util
 
+import android.content.Context
+import org.videolan.vlc.R
+
 object TextUtils {
 
     /**
@@ -48,4 +51,13 @@ object TextUtils {
      */
     fun separatedString(pieces: Array<String?>) = pieces.filter { it?.isNotBlank() == true }.joinToString(separator = " $separator ")
 
+    /**
+     * Formats the chapter title by prepending "Chapter:" if the current title is made of only non alpha chars
+     *
+     * @param context the context to use to retrieve the string
+     * @param title the title to format
+     * @return a formatted string
+     */
+    fun formatChapterTitle(context: Context, title: String?) = if (title?.firstOrNull { it.isLetter() } == null) context.getString(R.string.current_chapter, title) else title
+
 }
\ No newline at end of file



More information about the Android mailing list