[Android] Browser: change listener in IO threadpool

Geoffrey Métais git at videolan.org
Mon Sep 23 18:10:47 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Sep 16 16:03:34 2019 +0200| [11f0da629dfdeb16b83a859357b9fc9f8a110448] | committer: Geoffrey Métais

Browser: change listener in IO threadpool

Avoid ANRs

(cherry picked from commit e2252491bd66636a4c76cb746c2ee1f85fd97788)

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

 .../org/videolan/vlc/providers/BrowserProvider.kt    | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 9e49d14ef..6fabaaa54 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -133,16 +133,22 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
         loading.postValue(false)
     }
 
-    private fun filesFlow(url: String? = this.url, interact : Boolean = true) = channelFlow<Media> {
-        val listener = object : EventListener {
-            override fun onMediaAdded(index: Int, media: Media) { if (!isClosedForSend) offer(media.apply { retain() }) }
+    private suspend fun filesFlow(url: String? = this.url, interact : Boolean = true) = withContext(Dispatchers.IO) {
+        channelFlow {
+            val listener = object : EventListener {
+                override fun onMediaAdded(index: Int, media: Media) {
+                    if (!isClosedForSend) offer(media.apply { retain() })
+                }
 
-            override fun onBrowseEnd() { if (!isClosedForSend) close() }
+                override fun onBrowseEnd() {
+                    if (!isClosedForSend) close()
+                }
 
-            override fun onMediaRemoved(index: Int, media: Media) {}
+                override fun onMediaRemoved(index: Int, media: Media) {}
+            }
+            requestBrowsing(url, listener, interact)
+            awaitClose { mediabrowser?.changeEventListener(null) }
         }
-        requestBrowsing(url, listener, interact)
-        awaitClose { mediabrowser?.changeEventListener(null) }
     }
 
     protected open fun addMedia(media: MediaLibraryItem) = dataset.add(media)



More information about the Android mailing list