[Android] PLaylistActivity: remove IO from main thread

Geoffrey Métais git at videolan.org
Fri Oct 4 16:28:58 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct  3 11:50:44 2019 +0200| [0f1014fb4e31d2c2f2a222bc912652525f1900de] | committer: Geoffrey Métais

PLaylistActivity: remove IO from main thread

(cherry picked from commit 312932f02d7415607938d4529118fbc4ab26ab79)

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

 .../src/org/videolan/vlc/gui/PlaylistActivity.kt   | 29 ++++++++++++----------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
index 7d3743055..5285faa00 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.kt
@@ -211,7 +211,7 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler, IL
     override fun onItemFocused(v: View, item: MediaLibraryItem) {}
 
     override fun onRemove(position: Int, item: MediaLibraryItem) {
-        val tracks = ArrayList(Arrays.asList(*item.tracks))
+        val tracks = ArrayList(listOf(*item.tracks))
         removeFromPlaylist(tracks, ArrayList(Arrays.asList(position)))
     }
 
@@ -373,21 +373,24 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler, IL
         val itemsRemoved = HashMap<Int, Long>()
         val playlist = viewModel.playlist as? AbstractPlaylist
                 ?: return
-
-        for (mediaItem in list) {
-            for (i in 0 until playlist.tracks.size) {
-                if (playlist.tracks[i].id == mediaItem.id) {
-                    itemsRemoved[i] = mediaItem.id
+        launch {
+            val tracks = withContext(Dispatchers.IO) { playlist.tracks }
+            for (mediaItem in list) {
+                for (i in tracks.indices) {
+                    if (tracks[i].id == mediaItem.id) {
+                        itemsRemoved[i] = mediaItem.id
+                    }
                 }
             }
-        }
-        for (index in indexes) playlist.remove(index)
-
-        UiTools.snackerWithCancel(binding.root, getString(R.string.removed_from_playlist_anonymous), null, Runnable {
-            for ((key, value) in itemsRemoved) {
-                playlist.add(value, key)
+            withContext(Dispatchers.IO) {
+                for (index in indexes) playlist.remove(index)
             }
-        })
+            if (isStarted()) UiTools.snackerWithCancel(binding.root, getString(R.string.removed_from_playlist_anonymous), null, Runnable {
+                for ((key, value) in itemsRemoved) {
+                    playlist.add(value, key)
+                }
+            })
+        }
     }
 
     companion object {



More information about the Android mailing list