[Android] PlaylistManager: Work with copy of medialist

Geoffrey Métais git at videolan.org
Thu Jan 24 14:43:26 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 24 14:42:00 2019 +0100| [462faeeb0567b179a8d5bb1c38d31f1aa0974f87] | committer: Geoffrey Métais

PlaylistManager: Work with copy of medialist

> https://code.videolan.org/videolan/vlc-android/commit/462faeeb0567b179a8d5bb1c38d31f1aa0974f87
---

 .../org/videolan/vlc/media/MediaWrapperList.java   | 33 ++++++++--------------
 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 11 ++++----
 .../src/org/videolan/vlc/util/Kextensions.kt       |  5 ++--
 3 files changed, 21 insertions(+), 28 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java b/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java
index 2cee1811c..744719e55 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java
+++ b/vlc-android/src/org/videolan/vlc/media/MediaWrapperList.java
@@ -20,13 +20,10 @@
  *****************************************************************************/
 package org.videolan.vlc.media;
 
-import org.videolan.medialibrary.Medialibrary;
 import org.videolan.medialibrary.media.MediaWrapper;
-import org.videolan.vlc.VLCApplication;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.ListIterator;
 
 import androidx.annotation.Nullable;
 
@@ -155,15 +152,24 @@ public class MediaWrapperList {
         return isValid(position) ? mInternalList.get(position) : null;
     }
 
-    public synchronized List<MediaWrapper> getAll() {
-        return mInternalList;
+    public synchronized List<MediaWrapper> getCopy() {
+        return new ArrayList<>(mInternalList);
+    }
+
+    public synchronized void replaceWith(List<MediaWrapper> list) {
+        mInternalList.clear();
+        mInternalList.addAll(list);
+    }
+
+    public synchronized void map(List<MediaWrapper> list) {
+        mInternalList.addAll(list);
     }
 
     /**
      * @param position The index of the media in the list
      * @return null if not found
      */
-    public synchronized String getMRL(int position) {
+    private synchronized String getMRL(int position) {
         if (!isValid(position)) return null;
         return mInternalList.get(position).getLocation();
     }
@@ -172,21 +178,6 @@ public class MediaWrapperList {
         return mVideoCount == 0;
     }
 
-    public void updateWithMLMeta() {
-    final ListIterator<MediaWrapper> iter = mInternalList.listIterator();
-    final Medialibrary ml = VLCApplication.getMLInstance();
-    while (iter.hasNext()) {
-        final MediaWrapper media = iter.next();
-        if (media.getId() == 0L) {
-            final MediaWrapper mw = ml.findMedia(media);
-            if (mw.getId() != 0) {
-                if (mw.getType() == MediaWrapper.TYPE_ALL) mw.setType(media.getType());
-                synchronized (this) { iter.set(mw); }
-            }
-        }
-    }
-}
-
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 5db87ddfe..37f14456c 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -110,10 +110,9 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
             saveMediaList()
             savePosition()
             mediaList.removeEventListener(this at PlaylistManager)
-            mediaList.clear()
             previous.clear()
             videoBackground = false
-            withContext(Dispatchers.IO) { for (media in list) mediaList.add(medialibrary.findMedia(media)) }
+            mediaList.replaceWith(list)
             if (!hasMedia()) {
                 Log.w(TAG, "Warning: empty media list, nothing to play !")
                 return at launch
@@ -127,7 +126,9 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
             clearABRepeat()
             playIndex(currentIndex)
             onPlaylistLoaded()
-            if (mlUpdate) launch(Dispatchers.IO) { mediaList.updateWithMLMeta() }
+            if (mlUpdate) {
+                mediaList.replaceWith(withContext(Dispatchers.IO) { mediaList.copy.updateWithMLMeta() } )
+            }
         }
     }
 
@@ -469,7 +470,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         if (getCurrentMedia() === null) return
         val locations = StringBuilder()
         withContext(Dispatchers.Default) {
-            val list = mediaList.all.takeIf { it.isNotEmpty() } ?: return at withContext
+            val list = mediaList.copy.takeIf { it.isNotEmpty() } ?: return at withContext
             for (mw in list) locations.append(" ").append(Uri.encode(Uri.decode(mw.uri.toString())))
             //We save a concatenated String because putStringSet is APIv11.
             settings.edit()
@@ -690,7 +691,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
     fun getMediaListSize()= mediaList.size()
 
-    fun getMediaList(): List<MediaWrapper> = mediaList.all.toList()
+    fun getMediaList(): List<MediaWrapper> = mediaList.copy
 
     fun toggleABRepeat() {
         val time = player.getCurrentTime()
diff --git a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 6ed471bfb..f2fc4f2e5 100644
--- a/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -87,7 +87,7 @@ suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary
 
 
 fun List<MediaWrapper>.getWithMLMeta() : List<MediaWrapper> {
-    if (this is MutableList<MediaWrapper>) return apply { updateWithMLMeta() }
+    if (this is MutableList<MediaWrapper>) return updateWithMLMeta()
     val list = mutableListOf<MediaWrapper>()
     val ml = VLCApplication.getMLInstance()
     for (media in this) {
@@ -101,7 +101,7 @@ fun List<MediaWrapper>.getWithMLMeta() : List<MediaWrapper> {
 }
 
 
-fun MutableList<MediaWrapper>.updateWithMLMeta() {
+fun MutableList<MediaWrapper>.updateWithMLMeta() : MutableList<MediaWrapper> {
     val iter = listIterator()
     val ml = VLCApplication.getMLInstance()
     while (iter.hasNext()) {
@@ -114,6 +114,7 @@ fun MutableList<MediaWrapper>.updateWithMLMeta() {
             }
         }
     }
+    return this
 }
 
 suspend fun String.scanAllowed() = withContext(Dispatchers.IO) {



More information about the Android mailing list