[Android] Make browser provider reusable

Geoffrey Métais git at videolan.org
Thu Jul 19 13:18:11 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jul 19 11:58:48 2018 +0200| [386b17a02180cf43859823f24b7a5660f12c366a] | committer: Geoffrey Métais

Make browser provider reusable

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

 .../org/videolan/vlc/providers/BrowserProvider.kt  | 27 +++++++++++++++-------
 .../videolan/vlc/providers/FilePickerProvider.kt   |  2 +-
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index dc9a5d8e7..3de145fb3 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -53,7 +53,7 @@ abstract class BrowserProvider(val dataset: LiveDataset<MediaLibraryItem>, val u
     init {
         fetch()
     }
-    protected lateinit var mediabrowser: MediaBrowser
+    protected var mediabrowser: MediaBrowser? = null
 
     private val foldersContentMap = SimpleArrayMap<MediaLibraryItem, MutableList<MediaLibraryItem>>()
     private lateinit var browserChannel : Channel<Media>
@@ -63,7 +63,7 @@ abstract class BrowserProvider(val dataset: LiveDataset<MediaLibraryItem>, val u
     internal val medialibrary = Medialibrary.getInstance()
 
     protected open fun initBrowser() {
-        if (!this::mediabrowser.isInitialized) mediabrowser = MediaBrowser(VLCInstance.get(), this, browserHandler)
+        if (mediabrowser == null) mediabrowser = MediaBrowser(VLCInstance.get(), this, browserHandler)
     }
 
     open fun fetch() {
@@ -86,7 +86,10 @@ abstract class BrowserProvider(val dataset: LiveDataset<MediaLibraryItem>, val u
         browserChannel = Channel(Channel.UNLIMITED)
         requestBrowsing(url)
         job = uiJob(false) {
-            for (media in browserChannel) addMedia(findMedia(media))
+            for (media in browserChannel) {
+                if (isActive) addMedia(findMedia(media))
+                else return at uiJob
+            }
             parseSubDirectories()
         }
     }
@@ -106,7 +109,7 @@ abstract class BrowserProvider(val dataset: LiveDataset<MediaLibraryItem>, val u
         return true
     }
 
-    private suspend fun parseSubDirectories() {
+    internal open suspend fun parseSubDirectories() {
         if (dataset.value.isEmpty()) return
         val currentMediaList = dataset.value.toList()
         launch(browserContext, parent = job) {
@@ -133,7 +136,7 @@ abstract class BrowserProvider(val dataset: LiveDataset<MediaLibraryItem>, val u
                 }
                 // request parsing
                 browserChannel = Channel(Channel.UNLIMITED)
-                mediabrowser.browse(current.uri, 0)
+                mediabrowser?.browse(current.uri, 0)
                 // retrieve subitems
                 for (media in browserChannel) {
                     val type = media.type
@@ -193,13 +196,21 @@ abstract class BrowserProvider(val dataset: LiveDataset<MediaLibraryItem>, val u
 
     private fun requestBrowsing(url: String?) = launch(browserContext) {
         initBrowser()
-        if (url != null) mediabrowser.browse(Uri.parse(url), getFlags())
-        else mediabrowser.discoverNetworkShares()
+        mediabrowser?.let {
+            if (url != null) it.browse(Uri.parse(url), getFlags())
+            else it.discoverNetworkShares()
+        }
     }
 
     fun stop() = job?.cancel()
 
-    fun release() = launch(BrowserProvider.browserContext) { if (this at BrowserProvider::mediabrowser.isInitialized) mediabrowser.release() }
+    fun release() = launch(BrowserProvider.browserContext) {
+        if (this at BrowserProvider::browserChannel.isInitialized) browserChannel.close()
+        mediabrowser?.let {
+            it.release()
+            mediabrowser = null
+        }
+    }
 
     fun saveList(media: MediaWrapper) = foldersContentMap[media]?.let { if (!it.isEmpty()) prefetchLists[media.location] = it }
 
diff --git a/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt b/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt
index bfb3cf1e1..a426c8195 100644
--- a/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/FilePickerProvider.kt
@@ -34,7 +34,7 @@ class FilePickerProvider(dataset: LiveDataset<MediaLibraryItem>, url: String?) :
 
     override fun initBrowser() {
         super.initBrowser()
-        mediabrowser.setIgnoreFileTypes("db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv")
+        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) {



More information about the Android mailing list