[Android] Protect ML callbacks lists

Geoffrey Métais git at videolan.org
Wed Jan 18 15:58:10 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jan 18 15:56:40 2017 +0100| [35fcc361aa569039052d1ef82944b2a00a2b8cd5] | committer: Geoffrey Métais

Protect ML callbacks lists

> https://code.videolan.org/videolan/vlc-android/commit/35fcc361aa569039052d1ef82944b2a00a2b8cd5
---

 .../org/videolan/medialibrary/Medialibrary.java    | 111 ++++++++++++++++-----
 1 file changed, 87 insertions(+), 24 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index b26ac4a..f03e7c3 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -12,6 +12,7 @@ import android.util.Log;
 
 import org.videolan.libvlc.util.VLCUtil;
 import org.videolan.medialibrary.interfaces.DevicesDiscoveryCb;
+import org.videolan.medialibrary.interfaces.EntryPointsEventsCb;
 import org.videolan.medialibrary.interfaces.MediaAddedCb;
 import org.videolan.medialibrary.interfaces.MediaUpdatedCb;
 import org.videolan.medialibrary.media.Album;
@@ -51,7 +52,8 @@ public class Medialibrary {
     private ArtistsModifiedCb mArtistsModifiedCb = null;
     private AlbumsAddedCb mAlbumsAddedCb = null;
     private AlbumsModifiedCb mAlbumsModifiedCb = null;
-    private volatile List<DevicesDiscoveryCb> devicesDiscoveryCbList = new ArrayList<>();
+    private final List<DevicesDiscoveryCb> devicesDiscoveryCbList = new ArrayList<>();
+    private final List<EntryPointsEventsCb> entryPointsEventsCbList = new ArrayList<>();
 
     private static Medialibrary sInstance;
 
@@ -294,43 +296,88 @@ public class Medialibrary {
     }
 
     public void onDiscoveryStarted(String entryPoint) {
-        if (!devicesDiscoveryCbList.isEmpty())
-            for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
-                cb.onDiscoveryStarted(entryPoint);
+        synchronized (devicesDiscoveryCbList) {
+            if (!devicesDiscoveryCbList.isEmpty())
+                for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
+                    cb.onDiscoveryStarted(entryPoint);
+        }
+        synchronized (entryPointsEventsCbList) {
+            if (!entryPointsEventsCbList.isEmpty())
+                for (EntryPointsEventsCb cb : entryPointsEventsCbList)
+                    cb.onDiscoveryStarted(entryPoint);
+        }
     }
 
     public void onDiscoveryProgress(String entryPoint) {
-        if (!devicesDiscoveryCbList.isEmpty())
-            for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
-                cb.onDiscoveryProgress(entryPoint);
+        synchronized (devicesDiscoveryCbList) {
+            if (!devicesDiscoveryCbList.isEmpty())
+                for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
+                    cb.onDiscoveryProgress(entryPoint);
+        }
+        synchronized (entryPointsEventsCbList) {
+            if (!entryPointsEventsCbList.isEmpty())
+                for (EntryPointsEventsCb cb : entryPointsEventsCbList)
+                    cb.onDiscoveryProgress(entryPoint);
+        }
     }
 
     public void onDiscoveryCompleted(String entryPoint) {
-        if (!devicesDiscoveryCbList.isEmpty())
-            for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
-                cb.onDiscoveryCompleted(entryPoint);
+        synchronized (devicesDiscoveryCbList) {
+            if (!devicesDiscoveryCbList.isEmpty())
+                for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
+                    cb.onDiscoveryCompleted(entryPoint);
+        }
+        synchronized (entryPointsEventsCbList) {
+            if (!entryPointsEventsCbList.isEmpty())
+                for (EntryPointsEventsCb cb : entryPointsEventsCbList)
+                    cb.onDiscoveryCompleted(entryPoint);
+        }
     }
 
     public void onParsingStatsUpdated(int percent) {
-        if (!devicesDiscoveryCbList.isEmpty())
-            for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
-                cb.onParsingStatsUpdated(percent);
+        synchronized (devicesDiscoveryCbList) {
+            if (!devicesDiscoveryCbList.isEmpty())
+                for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
+                    cb.onParsingStatsUpdated(percent);
+        }
     }
 
     void onReloadStarted(String entryPoint) {
-        if (!devicesDiscoveryCbList.isEmpty())
-            for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
-                cb.onReloadStarted(entryPoint);
+        synchronized (devicesDiscoveryCbList) {
+            if (!devicesDiscoveryCbList.isEmpty())
+                for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
+                    cb.onReloadStarted(entryPoint);
+        }
     }
     void onReloadCompleted(String entryPoint) {
-        if (!devicesDiscoveryCbList.isEmpty())
-            for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
-                cb.onReloadCompleted(entryPoint);
+        synchronized (devicesDiscoveryCbList) {
+            if (!devicesDiscoveryCbList.isEmpty())
+                for (DevicesDiscoveryCb cb : devicesDiscoveryCbList)
+                    cb.onReloadCompleted(entryPoint);
+        }
     }
 
-    void onEntryPointBanned(String entryPoint, boolean success) {}
-    void onEntryPointUnbanned(String entryPoint, boolean success) {}
-    void onEntryPointRemoved(String entryPoint, boolean success) {}
+    void onEntryPointBanned(String entryPoint, boolean success) {
+        synchronized (entryPointsEventsCbList) {
+            if (!entryPointsEventsCbList.isEmpty())
+                for (EntryPointsEventsCb cb : entryPointsEventsCbList)
+                    cb.onEntryPointBanned(entryPoint, success);
+        }
+    }
+    void onEntryPointUnbanned(String entryPoint, boolean success) {
+        synchronized (entryPointsEventsCbList) {
+            if (!entryPointsEventsCbList.isEmpty())
+                for (EntryPointsEventsCb cb : entryPointsEventsCbList)
+                    cb.onEntryPointUnbanned(entryPoint, success);
+        }
+    }
+    void onEntryPointRemoved(String entryPoint, boolean success) {
+        synchronized (entryPointsEventsCbList) {
+            if (!entryPointsEventsCbList.isEmpty())
+                for (EntryPointsEventsCb cb : entryPointsEventsCbList)
+                    cb.onEntryPointRemoved(entryPoint, success);
+        }
+    }
 
     public void setMediaUpdatedCb(MediaUpdatedCb mediaUpdatedCb, int flags) {
         if (!mIsInitiated)
@@ -385,11 +432,27 @@ public class Medialibrary {
     }
 
     public void addDeviceDiscoveryCb(DevicesDiscoveryCb cb) {
-        devicesDiscoveryCbList.add(cb);
+        synchronized (devicesDiscoveryCbList) {
+            devicesDiscoveryCbList.add(cb);
+        }
     }
 
     public void removeDeviceDiscoveryCb(DevicesDiscoveryCb cb) {
-        devicesDiscoveryCbList.remove(cb);
+        synchronized (devicesDiscoveryCbList) {
+            devicesDiscoveryCbList.remove(cb);
+        }
+    }
+
+    public void addEntryPointsEventsCb(EntryPointsEventsCb cb) {
+        synchronized (entryPointsEventsCbList) {
+            entryPointsEventsCbList.add(cb);
+        }
+    }
+
+    public void removeEntryPointsEventsCb(EntryPointsEventsCb cb) {
+        synchronized (entryPointsEventsCbList) {
+            entryPointsEventsCbList.remove(cb);
+        }
     }
 
     public void removeMediaAddedCb() {



More information about the Android mailing list