[Android] Videos: prevent ClassCastException

Geoffrey Métais git at videolan.org
Thu May 7 12:17:05 CEST 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon May  4 15:56:25 2020 +0200| [cac359b8eb6bafb72e8560157b9706820d3deff6] | committer: Nicolas Pomepuy

Videos: prevent ClassCastException

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

 .../src/org/videolan/vlc/gui/video/VideoGridFragment.kt    | 14 +++++++-------
 .../vlc-android/src/org/videolan/vlc/media/MediaUtils.kt   |  4 +++-
 .../vlc/providers/medialibrary/MedialibraryProvider.kt     |  2 +-
 .../vlc/providers/medialibrary/VideoGroupsProvider.kt      |  2 +-
 .../org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt  | 14 +++++++++-----
 5 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index 4bba2e5d9..26b66df1c 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -429,7 +429,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
             is MediaWrapper -> when (option) {
                 CTX_PLAY_FROM_START -> viewModel.playVideo(activity, media, position, true)
                 CTX_PLAY_AS_AUDIO -> viewModel.playAudio(activity, media)
-                CTX_PLAY_ALL -> MediaUtils.playAll(activity, viewModel.provider as VideosProvider, position, false)
+                CTX_PLAY_ALL -> viewModel.play(position)
                 CTX_INFORMATION -> showInfoDialog(media)
                 CTX_DELETE -> removeItem(media)
                 CTX_APPEND -> MediaUtils.appendMedia(activity, media)
@@ -450,13 +450,13 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
 
             }
             is Folder -> when (option) {
-                CTX_PLAY -> lifecycleScope.launch { viewModel.play(position) }
-                CTX_APPEND -> lifecycleScope.launch { viewModel.append(position) }
+                CTX_PLAY -> viewModel.play(position)
+                CTX_APPEND -> viewModel.append(position)
                 CTX_ADD_TO_PLAYLIST -> viewModel.addItemToPlaylist(requireActivity(), position)
             }
             is VideoGroup -> when (option) {
-                CTX_PLAY -> lifecycleScope.launch { viewModel.play(position) }
-                CTX_APPEND -> lifecycleScope.launch { viewModel.append(position) }
+                CTX_PLAY -> viewModel.play(position)
+                CTX_APPEND -> viewModel.append(position)
                 CTX_ADD_TO_PLAYLIST -> viewModel.addItemToPlaylist(requireActivity(), position)
                 CTX_RENAME_GROUP -> renameGroup(media)
                 CTX_UNGROUP -> viewModel.ungroup(media)
@@ -483,7 +483,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
         }
     }
 
-    private suspend fun VideoAction.process() {
+    private fun VideoAction.process() {
         when (this) {
             is VideoClick -> {
                 onClick(position, item)
@@ -519,7 +519,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
         if (actionMode == null) startActionMode()
     }
 
-    private suspend fun onClick(position: Int, item: MediaLibraryItem) {
+    private fun onClick(position: Int, item: MediaLibraryItem) {
         when (item) {
             is MediaWrapper -> {
                 if (actionMode != null) {
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 9ee66c250..8ebda7bce 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -236,7 +236,9 @@ object MediaUtils {
             when (count) {
                 0 -> return at SuspendDialogCallback
                 in 1..MEDIALIBRARY_PAGE_SIZE -> play(withContext(Dispatchers.IO) {
-                    provider.getAll().toList()
+                    provider.getAll().flatMap {
+                        it.media(Medialibrary.SORT_DEFAULT, false, it.mediaCount(), 0).toList()
+                    }
                 })
                 else -> {
                     var index = 0
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
index 6a2bb8bd6..fe2bc20ee 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/MedialibraryProvider.kt
@@ -71,7 +71,7 @@ abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context,
 
     abstract fun getTotalCount(): Int
     abstract fun getPage(loadSize: Int, startposition: Int): Array<T>
-    abstract fun getAll(): Array<T>
+    abstract fun getAll(): Array<out T>
 
     override fun sort(sort: Int) {
         if (canSortBy(sort)) {
diff --git a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
index b638ea5b8..2766dde2a 100644
--- a/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/providers/medialibrary/VideoGroupsProvider.kt
@@ -14,7 +14,7 @@ class VideoGroupsProvider(context: Context, model: SortableModel) : Medialibrary
     override fun canSortByLastModified() = true
     override fun canSortByMediaNumber() = true
 
-    override fun getAll() : Array<MediaLibraryItem> = medialibrary.getVideoGroups(sort, desc, getTotalCount(), 0).extractSingles()
+    override fun getAll() : Array<VideoGroup> = medialibrary.getVideoGroups(sort, desc, getTotalCount(), 0)
 
     override fun getTotalCount() = medialibrary.getVideoGroupsCount(model.filterQuery)
 
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index e5414c738..0a40b86ec 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -83,8 +83,8 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
     }
 
     // Folders & Groups
-    internal suspend fun play(position: Int) {
-        val item = provider.pagedList.value?.get(position) ?: return
+    internal fun play(position: Int) = viewModelScope.launch {
+        val item = provider.pagedList.value?.get(position) ?: return at launch
         withContext(Dispatchers.IO) {
             when (item) {
                 is Folder -> item.getAll()
@@ -94,8 +94,8 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
         }?.let { MediaUtils.openList(context, it, 0) }
     }
 
-    internal suspend fun append(position: Int) {
-        val item = provider.pagedList.value?.get(position) ?: return
+    internal fun append(position: Int) = viewModelScope.launch {
+        val item = provider.pagedList.value?.get(position) ?: return at launch
         withContext(Dispatchers.IO) {
             when (item) {
                 is Folder -> item.getAll()
@@ -131,7 +131,11 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
         mw.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO)
         val settings = Settings.getInstance(context)
         if (settings.getBoolean(FORCE_PLAY_ALL, false)) {
-            MediaUtils.playAll(context, provider as VideosProvider, position, false)
+            when(val prov = provider) {
+                is VideosProvider -> MediaUtils.playAll(context, prov, position, false)
+                is FoldersProvider -> MediaUtils.playAllTracks(context, prov, position, false)
+                is VideoGroupsProvider -> MediaUtils.playAllTracks(context, prov, position, false)
+            }
         } else {
             if (fromStart) mw.addFlags(MediaWrapper.MEDIA_FROM_START)
             MediaUtils.openMedia(context, mw)



More information about the Android mailing list