[Android] Storages: Prevent IO from main thread

Geoffrey Métais git at videolan.org
Thu Dec 19 14:58:28 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Dec 19 12:23:44 2019 +0100| [704de5939ef6665689da1a0c03ee7b6faf586fc3] | committer: Nicolas Pomepuy

Storages: Prevent IO from main thread

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

 .../vlc/gui/browser/StorageBrowserAdapter.kt       | 24 +++++++++-------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
index 98db73252..0bafc890c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
@@ -27,10 +27,7 @@ import android.content.Context
 import android.net.Uri
 import android.view.View
 import androidx.databinding.ViewDataBinding
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.Job
-import kotlinx.coroutines.ObsoleteCoroutinesApi
-import kotlinx.coroutines.launch
+import kotlinx.coroutines.*
 import org.videolan.medialibrary.interfaces.AbstractMedialibrary
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
@@ -97,17 +94,16 @@ internal class StorageBrowserAdapter(fragment: StorageBrowserFragment) : BaseBro
     fun updateMediaDirs(context: Context) {
         mediaDirsLocation.clear()
 
-        val folders = if (!AbstractMedialibrary.getInstance().isInitiated) {
-            MediaParsingService.preselectedStorages.toTypedArray()
-        } else {
-            AbstractMedialibrary.getInstance().foldersList
-        }
-
-        folders.forEach {
-            mediaDirsLocation.add(Uri.decode(if (it.startsWith("file://")) it.substring(7) else it))
-        }
-
         updateJob = launch {
+            val folders = if (!AbstractMedialibrary.getInstance().isInitiated) {
+                MediaParsingService.preselectedStorages.toTypedArray()
+            } else {
+                withContext(Dispatchers.IO) { AbstractMedialibrary.getInstance().foldersList }
+            }
+
+            folders.forEach {
+                mediaDirsLocation.add(Uri.decode(if (it.startsWith("file://")) it.substring(7) else it))
+            }
             customDirsLocation = DirectoryRepository.getInstance(context).getCustomDirectories().map { it.path }
         }
     }



More information about the Android mailing list