[Android] Use media ctx for groups with only one video

Nicolas Pomepuy git at videolan.org
Wed Dec 11 14:45:07 CET 2019


vlc-android | branch: 3.2.x | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Wed Dec 11 13:42:57 2019 +0100| [bb8e00deab1817fbd15b2617f4c9e4ebcfb50c41] | committer: Nicolas Pomepuy

Use media ctx for groups with only one video

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

 .../vlc/gui/videogroups/VideoGroupsFragment.kt     | 44 ++++++++++++++++++++--
 vlc-android/src/org/videolan/vlc/util/Constants.kt |  2 +
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/videogroups/VideoGroupsFragment.kt b/vlc-android/src/org/videolan/vlc/gui/videogroups/VideoGroupsFragment.kt
index 006518a9f..82e903735 100644
--- a/vlc-android/src/org/videolan/vlc/gui/videogroups/VideoGroupsFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/videogroups/VideoGroupsFragment.kt
@@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView
 import kotlinx.android.synthetic.main.videogroups_fragment.*
 import kotlinx.coroutines.*
 import kotlinx.coroutines.channels.actor
+import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.interfaces.media.AbstractVideoGroup
 import org.videolan.tools.MultiSelectHelper
 import org.videolan.tools.isStarted
@@ -18,6 +19,7 @@ import org.videolan.vlc.databinding.VideogroupsFragmentBinding
 import org.videolan.vlc.gui.SecondaryActivity
 import org.videolan.vlc.gui.browser.MediaBrowserFragment
 import org.videolan.vlc.gui.dialogs.CtxActionReceiver
+import org.videolan.vlc.gui.dialogs.SavePlaylistDialog
 import org.videolan.vlc.gui.dialogs.showContext
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.media.MediaUtils
@@ -28,7 +30,6 @@ import org.videolan.vlc.util.*
 import org.videolan.vlc.viewmodels.mobile.VideogroupsViewModel
 import org.videolan.vlc.viewmodels.mobile.getViewModel
 
-
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
 class VideoGroupsFragment : MediaBrowserFragment<VideogroupsViewModel>(), CtxActionReceiver {
@@ -37,7 +38,7 @@ class VideoGroupsFragment : MediaBrowserFragment<VideogroupsViewModel>(), CtxAct
     private lateinit var adapter: VideoGroupsAdapter
 
     private val actor = actor<VideoGroupAction> {
-        for (action in channel) when(action) {
+        for (action in channel) when (action) {
             is VideoGroupClick -> {
                 if (actionMode != null) {
                     adapter.multiSelectHelper.toggleSelection(action.position)
@@ -59,7 +60,12 @@ class VideoGroupsFragment : MediaBrowserFragment<VideogroupsViewModel>(), CtxAct
                 }
             }
             is VideoGroupCtxClick -> {
-                showContext(requireActivity(), this at VideoGroupsFragment, action.position, action.group.title, CTX_FOLDER_FLAGS)
+                if (action.group.mediaCount() == 1) {
+                    context?.getFromMl { action.group.media(0, false, 1, 0) }?.get(0)?.let { media ->
+                        showContext(requireActivity(), this at VideoGroupsFragment, action.position, media.title, CTX_VIDEO_GROUP_TEMP_FLAGS)
+                    }
+                } else
+                    showContext(requireActivity(), this at VideoGroupsFragment, action.position, action.group.title, CTX_FOLDER_FLAGS)
             }
         }
     }
@@ -163,12 +169,42 @@ class VideoGroupsFragment : MediaBrowserFragment<VideogroupsViewModel>(), CtxAct
     }
 
     override fun onCtxAction(position: Int, option: Int) {
-        when (option) {
+        val item = adapter.getItem(position)
+        if (item?.mediaCount() == 1) {
+            launch {
+                context?.getFromMl { item.media(0, false, 1, 0) }?.get(0)?.let { media ->
+                    when (option) {
+                        CTX_PLAY_FROM_START -> playVideo(media, true)
+                        CTX_PLAY_AS_AUDIO -> playAudio(media)
+                        CTX_INFORMATION -> showInfoDialog(media)
+                        CTX_DELETE -> removeItem(media)
+                        CTX_APPEND -> MediaUtils.appendMedia(activity, media)
+                        CTX_PLAY_NEXT -> MediaUtils.insertNext(requireActivity(), media.tracks)
+                        CTX_DOWNLOAD_SUBTITLES -> MediaUtils.getSubs(requireActivity(), media)
+                        CTX_ADD_TO_PLAYLIST -> UiTools.addToPlaylist(requireActivity(), media.tracks, SavePlaylistDialog.KEY_NEW_TRACKS)
+                        else -> {
+                        }
+                    }
+                }
+
+            }
+        } else when (option) {
             CTX_PLAY -> viewModel.play(position)
             CTX_APPEND -> viewModel.append(position)
             CTX_ADD_TO_PLAYLIST -> viewModel.addToPlaylist(requireActivity(), position)
         }
     }
+
+    private fun playVideo(media: AbstractMediaWrapper, fromStart: Boolean) {
+        media.removeFlags(AbstractMediaWrapper.MEDIA_FORCE_AUDIO)
+        if (fromStart) media.addFlags(AbstractMediaWrapper.MEDIA_FROM_START)
+        MediaUtils.openMedia(requireContext(), media)
+    }
+
+    private fun playAudio(media: AbstractMediaWrapper) {
+        media.addFlags(AbstractMediaWrapper.MEDIA_FORCE_AUDIO)
+        MediaUtils.openMedia(activity, media)
+    }
 }
 
 sealed class VideoGroupAction
diff --git a/vlc-android/src/org/videolan/vlc/util/Constants.kt b/vlc-android/src/org/videolan/vlc/util/Constants.kt
index a9706b650..3ac293e88 100644
--- a/vlc-android/src/org/videolan/vlc/util/Constants.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Constants.kt
@@ -167,6 +167,8 @@ const val CTX_DOWNLOAD_SUBTITLES_PLAYER = 1 shl 24
 const val CTX_COPY = 1 shl 25
 
 const val CTX_VIDEO_FLAGS = CTX_APPEND or CTX_PLAY_NEXT or CTX_DELETE or CTX_DOWNLOAD_SUBTITLES or CTX_INFORMATION or CTX_PLAY_ALL or CTX_PLAY_AS_AUDIO or CTX_ADD_TO_PLAYLIST
+//this is a temporary flag only used in 3.2
+const val CTX_VIDEO_GROUP_TEMP_FLAGS = CTX_VIDEO_FLAGS and CTX_PLAY_ALL.inv()
 const val CTX_TRACK_FLAGS = CTX_APPEND or CTX_PLAY_NEXT or CTX_DELETE or CTX_INFORMATION or CTX_PLAY_ALL or CTX_ADD_TO_PLAYLIST or CTX_SET_RINGTONE
 const val CTX_AUDIO_FLAGS = CTX_PLAY or CTX_APPEND or CTX_PLAY_NEXT or CTX_ADD_TO_PLAYLIST
 const val CTX_PLAYLIST_FLAGS = CTX_AUDIO_FLAGS or CTX_DELETE



More information about the Android mailing list