[Android] Fix TV playlist crash when playlist is really long

Nicolas Pomepuy git at videolan.org
Wed May 27 10:10:47 CEST 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed May 27 08:44:47 2020 +0200| [2a81180a4232a7eaed480357e59cf5110dff1f33] | committer: Nicolas Pomepuy

Fix TV playlist crash when playlist is really long

Fixes #1165

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

 .../src/main/java/org/videolan/television/ui/TvUtil.kt         |  8 ++++++++
 .../videolan/television/ui/audioplayer/AudioPlayerActivity.kt  |  7 +++++--
 .../org/videolan/television/ui/details/MediaListActivity.kt    |  5 +++--
 .../vlc-android/src/org/videolan/vlc/media/MediaUtils.kt       | 10 ++++++++++
 4 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/TvUtil.kt b/application/television/src/main/java/org/videolan/television/ui/TvUtil.kt
index 96ed9afa6..b8ffd3493 100644
--- a/application/television/src/main/java/org/videolan/television/ui/TvUtil.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/TvUtil.kt
@@ -38,6 +38,7 @@ import androidx.leanback.widget.DiffCallback
 import androidx.leanback.widget.ListRow
 import kotlinx.coroutines.*
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
+import org.videolan.medialibrary.interfaces.media.Playlist
 import org.videolan.medialibrary.media.DummyItem
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.moviepedia.database.models.MediaMetadataWithImages
@@ -124,6 +125,13 @@ object TvUtil {
         activity.startActivity(intent)
     }
 
+    fun playPlaylist(activity: Activity, playlist: Playlist, position: Int = 0) {
+        val intent = Intent(activity, AudioPlayerActivity::class.java)
+        intent.putExtra(AudioPlayerActivity.MEDIA_PLAYLIST, playlist.id)
+        intent.putExtra(AudioPlayerActivity.MEDIA_POSITION, position)
+        activity.startActivity(intent)
+    }
+
     @Suppress("UNCHECKED_CAST")
     fun openMedia(activity: FragmentActivity, item: Any?, model: BaseModel<out MediaLibraryItem>?) {
         when (item) {
diff --git a/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt b/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
index 84b9e02aa..e73834aa8 100644
--- a/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
@@ -91,9 +91,11 @@ class AudioPlayerActivity : BaseTvActivity() {
             }
         })
         model.playerState.observe(this, Observer { playerState -> update(playerState) })
-        val medialist = intent.getParcelableArrayListExtra<MediaWrapper>(MEDIA_LIST)
         val position = intent.getIntExtra(MEDIA_POSITION, 0)
-        if (medialist != null) MediaUtils.openList(this, medialist, position)
+        if (intent.hasExtra(MEDIA_PLAYLIST))
+            intent.getLongExtra(MEDIA_PLAYLIST, -1L).let { MediaUtils.openPlaylist(this, it) }
+        else
+            intent.getParcelableArrayListExtra<MediaWrapper>(MEDIA_LIST)?.let { MediaUtils.openList(this, it, position) }
         playToPause = AnimatedVectorDrawableCompat.create(this, R.drawable.anim_play_pause)!!
         pauseToPlay = AnimatedVectorDrawableCompat.create(this, R.drawable.anim_pause_play)!!
     }
@@ -285,6 +287,7 @@ class AudioPlayerActivity : BaseTvActivity() {
         const val TAG = "VLC/AudioPlayerActivity"
 
         const val MEDIA_LIST = "media_list"
+        const val MEDIA_PLAYLIST = "media_playlist"
         const val MEDIA_POSITION = "media_position"
 
         //PAD navigation
diff --git a/application/television/src/main/java/org/videolan/television/ui/details/MediaListActivity.kt b/application/television/src/main/java/org/videolan/television/ui/details/MediaListActivity.kt
index 4111415ff..8428cd4c1 100644
--- a/application/television/src/main/java/org/videolan/television/ui/details/MediaListActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/details/MediaListActivity.kt
@@ -15,6 +15,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.medialibrary.Tools
+import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.medialibrary.interfaces.media.Playlist
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.resources.ITEM
@@ -79,7 +80,7 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler {
         binding.totalTime = Tools.millisToString(item.tracks.sumByDouble { it.length.toDouble() }.toLong())
 
 
-        binding.play.setOnClickListener { TvUtil.playMedia(this, item.tracks.toMutableList()) }
+        binding.play.setOnClickListener { if (item is Playlist) TvUtil.playPlaylist(this, item as Playlist)  else TvUtil.playMedia(this, item.tracks.toMutableList())}
         binding.append.setOnClickListener { MediaUtils.appendMedia(this, item.tracks) }
         binding.insertNext.setOnClickListener { MediaUtils.insertNext(this, item.tracks) }
         binding.addPlaylist.setOnClickListener { addToPlaylist(item.tracks, SavePlaylistDialog.KEY_NEW_TRACKS) }
@@ -125,7 +126,7 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler {
     }
 
     override fun onClickPlay(v: View, position: Int) {
-        TvUtil.playMedia(this, item.tracks.toList(), position)
+        if (item is Playlist) TvUtil.playPlaylist(this, item as Playlist, position)  else  TvUtil.playMedia(this, item.tracks.toList(), position)
     }
 
     override fun onClickPlayNext(v: View, position: Int) {
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 8ebda7bce..cd4ad935a 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -298,6 +298,16 @@ object MediaUtils {
         }
     }
 
+    @JvmOverloads
+    fun openPlaylist(context: Context?, playlistId: Long, position: Int = 0, shuffle: Boolean = false) {
+        if (playlistId == -1L || context == null) return
+        SuspendDialogCallback(context) { service ->
+           val playlist =  context.getFromMl { getPlaylist(playlistId) }
+            service.load(playlist.getPagedTracks(playlist.realTracksCount, 0), position)
+            if (shuffle && !service.isShuffling) service.shuffle()
+        }
+    }
+
     fun openUri(context: Context?, uri: Uri?) {
         if (uri == null || context == null) return
         SuspendDialogCallback(context) { service ->



More information about the Android mailing list