[Android] Harden Medialibrary initialization

Geoffrey Métais git at videolan.org
Fri Aug 9 13:32:11 CEST 2019


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

Harden Medialibrary initialization

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

 .../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