[Android] Browsers: Fix subtitles picker

Geoffrey Métais git at videolan.org
Thu Aug 8 13:53:43 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Aug  5 16:31:36 2019 +0200| [8b7a555d99976265cb3931fa65b4120f20ffcad1] | committer: Geoffrey Métais

Browsers: Fix subtitles picker

Also improves factorization in browser providers

(cherry picked from commit 1a9feed0edb4d340bf97a5373ae7df47dac96ae7)

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

 .../org/videolan/vlc/providers/BrowserProvider.kt  | 28 ++++++++++++----------
 .../videolan/vlc/providers/FilePickerProvider.kt   | 10 +++++---
 .../org/videolan/vlc/providers/NetworkProvider.kt  |  4 ++--
 .../org/videolan/vlc/providers/StorageProvider.kt  | 24 ++-----------------
 4 files changed, 26 insertions(+), 40 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index b4080468b..a2267a72d 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -79,7 +79,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
                 is Browse -> browseImpl(action.url)
                 BrowseRoot -> browseRootImpl()
                 Refresh -> browseImpl(url)
-                ParseSubDirectories -> parseSubDirectoriesImpl()
+                is ParseSubDirectories -> parseSubDirectoriesImpl(action.list)
                 ClearListener -> withContext(Dispatchers.IO) { mediabrowser?.changeEventListener(null) }
                 Release -> withContext(Dispatchers.IO) {
                     mediabrowser?.release()
@@ -130,7 +130,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
             val value: MutableList<MediaLibraryItem> = browserChannel.mapNotNullTo(mutableListOf()) { findMedia(it) }
             computeHeaders(value)
             dataset.value = value
-            parseSubDirectories()
+            parseSubDirectories(value)
         }
         loading.postValue(false)
     }
@@ -145,7 +145,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         browserActor.post(Refresh)
     }
 
-    fun computeHeaders(value: MutableList<MediaLibraryItem>) {
+    open fun computeHeaders(value: MutableList<MediaLibraryItem>) {
         privateHeaders.clear()
         for ((position, item) in value.withIndex()) {
             val previous = when {
@@ -161,13 +161,13 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         (liveHeaders as MutableLiveData).postValue(privateHeaders.clone())
     }
 
-    internal open fun parseSubDirectories() {
-        browserActor.post(ParseSubDirectories)
+    internal open fun parseSubDirectories(list : List<MediaLibraryItem>? = null) {
+        browserActor.post(ParseSubDirectories(list))
     }
 
-    private suspend fun parseSubDirectoriesImpl() {
-        if (dataset.value.isEmpty()) return
-        val currentMediaList = withContext(Dispatchers.Main) { dataset.value.toList().also { if (url != null) computeHeaders(dataset.value) } }
+    private suspend fun parseSubDirectoriesImpl(list : List<MediaLibraryItem>? = null) {
+        if (list === null && dataset.value.isEmpty()) return
+        val currentMediaList = list ?: withContext(Dispatchers.Main) { dataset.value.toList() }
         val directories: MutableList<AbstractMediaWrapper> = ArrayList()
         val files: MutableList<AbstractMediaWrapper> = ArrayList()
         foldersContentMap.clear()
@@ -199,9 +199,11 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
                     // retrieve subitems
                     for (media in browserChannel) {
                         val mw = findMedia(media) ?: continue
-                        val type = mw.type
-                        if (type == AbstractMediaWrapper.TYPE_DIR) directories.add(mw)
-                        else files.add(mw)
+                        if (mw is AbstractMediaWrapper) {
+                            val type = mw.type
+                            if (type == AbstractMediaWrapper.TYPE_DIR) directories.add(mw)
+                            else files.add(mw)
+                        } else if (mw is Storage) directories.add(MLServiceLocator.getAbstractMediaWrapper(media))
                     }
                     // all subitems are in
                     getDescription(directories.size, files.size).takeIf { it.isNotEmpty() }?.let {
@@ -243,7 +245,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         return sb.toString()
     }
 
-    private suspend fun findMedia(media: Media): AbstractMediaWrapper? {
+    protected open suspend fun findMedia(media: Media): MediaLibraryItem? {
         val mw = MLServiceLocator.getAbstractMediaWrapper(media)
         media.release()
         if (!mw.isMedia()) {
@@ -306,6 +308,6 @@ private sealed class BrowserAction
 private class Browse(val url: String?) : BrowserAction()
 private object BrowseRoot : BrowserAction()
 private object Refresh : BrowserAction()
-private object ParseSubDirectories : BrowserAction()
+private class ParseSubDirectories(val list : List<MediaLibraryItem>? = null) : BrowserAction()
 private object ClearListener : BrowserAction()
 private object Release : BrowserAction()
diff --git a/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt
index 5c7c5b1c8..c4ac9548a 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt
@@ -21,7 +21,9 @@
 package org.videolan.vlc.providers
 
 import android.content.Context
+import org.videolan.libvlc.Media
 import org.videolan.libvlc.util.MediaBrowser
+import org.videolan.medialibrary.MLServiceLocator
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.vlc.util.LiveDataset
@@ -37,9 +39,11 @@ class FilePickerProvider(context: Context, dataset: LiveDataset<MediaLibraryItem
         mediabrowser?.setIgnoreFileTypes("db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv")
     }
 
-    override fun addMedia(media: MediaLibraryItem) {
-        if (media is AbstractMediaWrapper && (media.type == AbstractMediaWrapper.TYPE_SUBTITLE || media.type == AbstractMediaWrapper.TYPE_DIR)) super.addMedia(media)
+    override suspend fun findMedia(media: Media) = MLServiceLocator.getAbstractMediaWrapper(media)?.takeIf { mw ->
+        mw.type == AbstractMediaWrapper.TYPE_DIR || mw.type == AbstractMediaWrapper.TYPE_SUBTITLE
     }
 
-    override fun parseSubDirectories() {}
+    override fun computeHeaders(value: MutableList<MediaLibraryItem>) {}
+
+    override fun parseSubDirectories(list : List<MediaLibraryItem>?) {}
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt b/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
index 3a26db227..d42f3f774 100644
--- a/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/NetworkProvider.kt
@@ -83,8 +83,8 @@ class NetworkProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
 
     }
 
-    override fun parseSubDirectories() {
-        if (url != null) super.parseSubDirectories()
+    override fun parseSubDirectories(list : List<MediaLibraryItem>?) {
+        if (url != null) super.parseSubDirectories(list)
     }
 
     override fun stop() {
diff --git a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
index d93d9cacd..a4d8b4608 100644
--- a/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/StorageProvider.kt
@@ -23,11 +23,7 @@ package org.videolan.vlc.providers
 import android.content.Context
 import android.net.Uri
 import android.text.TextUtils
-import kotlinx.coroutines.channels.Channel
-import kotlinx.coroutines.channels.filter
-import kotlinx.coroutines.channels.mapTo
 import org.videolan.libvlc.Media
-import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.Storage
 import org.videolan.vlc.R
@@ -63,25 +59,9 @@ class StorageProvider(context: Context, dataset: LiveDataset<MediaLibraryItem>,
         dataset.value = storagesList
     }
 
-    override fun addMedia(media: MediaLibraryItem) {
-        if (media.itemType == MediaLibraryItem.TYPE_MEDIA) {
-            if ((media as AbstractMediaWrapper).type == AbstractMediaWrapper.TYPE_DIR) super.addMedia(Storage(media.uri))
-            return
-        } else if (media.itemType != MediaLibraryItem.TYPE_STORAGE) return
-        super.addMedia(media)
-    }
+    override suspend fun findMedia(media: Media) = media.takeIf { it.isStorage() }?.let { Storage(it.uri) }
 
-    override suspend fun browseImpl(url: String?) {
-        if (url == null) super.browseImpl(url)
-        else {
-            browserChannel = Channel(Channel.UNLIMITED)
-            requestBrowsing(url)
-            val value: MutableList<MediaLibraryItem> = browserChannel.filter { it.isStorage() }.mapTo(mutableListOf()) { Storage(it.uri)}
-            dataset.value = value
-            parseSubDirectories()
-            loading.postValue(false)
-        }
-    }
+    override fun computeHeaders(value: MutableList<MediaLibraryItem>) {}
 }
 
 private fun Media.isStorage() = type == Media.Type.Directory



More information about the Android mailing list