[Android] Prevent ArrayIndexOutOfBoundsException on track chapters

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


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Feb  1 07:56:41 2022 +0100| [77261ae28c8bc1334aa62954511284c230d52100] | committer: Nicolas Pomepuy

Prevent ArrayIndexOutOfBoundsException on track chapters

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

 application/vlc-android/src/org/videolan/vlc/PlaybackService.kt      | 5 +++--
 .../vlc-android/src/org/videolan/vlc/gui/view/AudioMediaSwitcher.kt  | 5 ++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index a67090221..52845ef2d 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -390,9 +390,10 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
 
     suspend fun getCurrentChapter(formatted:Boolean = false):String? {
         val currentChapter = withContext(Dispatchers.IO) {
-             getChapters(-1)?.get(chapterIdx)?.name
+            val chapters = getChapters(-1)
+            if (chapters?.size ?: 0 > 0 && chapterIdx >= 0) chapters?.get(chapterIdx)?.name else null
         }
-        return if (formatted) TextUtils.formatChapterTitle(this, currentChapter) else currentChapter
+        return if (currentChapter == null) null else if (formatted) TextUtils.formatChapterTitle(this, currentChapter) else currentChapter
     }
 
     suspend fun trackInfo(): String? {
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 cf79a2fb1..69cf4a8b1 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
@@ -30,7 +30,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import kotlinx.coroutines.withContext
 import org.videolan.vlc.PlaybackService
-import org.videolan.vlc.R
 import org.videolan.vlc.gui.helpers.AudioUtil
 
 @ObsoleteCoroutinesApi
@@ -107,8 +106,8 @@ abstract class AudioMediaSwitcher(context: Context, attrs: AttributeSet) : Fling
             addMediaView(inflater, service.titlePrev, service.artistPrev, service.albumPrev, coverPrev, prevTrackInfo)
             hasPrevious = true
         }
-        val chapter = service.getChapters(-1)?.get(service.chapterIdx)?.name
-        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)
+        val chapter = service.getCurrentChapter(true)
+        if (service.hasMedia()) addMediaView(inflater, if (!chapter.isNullOrEmpty()) chapter 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()) {



More information about the Android mailing list