[Android] Harden Medialibrary initialization

Geoffrey Métais git at videolan.org
Mon Aug 12 13:44:17 CEST 2019


vlc-android | branch: 3.2.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Aug  8 18:07:15 2019 +0200| [ae6c6a304d8facefc0aadd822ab6a84c7b7f6f05] | committer: Geoffrey Métais

Harden Medialibrary initialization

(cherry picked from commit ad48e895c1f93be0b221f6582cb560a73b2e79c8)

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

 .../src/org/videolan/medialibrary/MLServiceLocator.java       | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java b/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java
index d02f1a8d7..6fbcbf122 100644
--- a/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java
+++ b/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java
@@ -29,8 +29,10 @@ import org.videolan.medialibrary.stubs.StubPlaylist;
 public class MLServiceLocator {
 
     private static LocatorMode sMode = LocatorMode.VLC_ANDROID;
+    private static volatile AbstractMedialibrary instance;
 
     public static void setLocatorMode(LocatorMode mode) {
+        if (instance != null) throw new IllegalStateException("LocatorMode must be set before AbstractMedialibrary initialization");
         MLServiceLocator.sMode = mode;
     }
 
@@ -40,12 +42,11 @@ public class MLServiceLocator {
         TESTS,
     }
 
-    public static AbstractMedialibrary getAbstractMedialibrary() {
-        if (sMode == LocatorMode.VLC_ANDROID) {
-            return new Medialibrary();
-        } else {
-            return new StubMedialibrary();
+    public static synchronized AbstractMedialibrary getAbstractMedialibrary() {
+        if (instance == null) {
+            instance = sMode == LocatorMode.VLC_ANDROID ? new Medialibrary() : new StubMedialibrary();
         }
+        return instance;
     }
 
     // AbstractMediaWrapper



More information about the Android mailing list