[Android] Put the factory fallback directly in the FactoryManager as the Application may not have been instantiated

Nicolas Pomepuy git at videolan.org
Mon Oct 26 14:34:28 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Oct 26 14:02:52 2020 +0100| [cfb4b342a8d9c75db9033343554fee2be3a69d37] | committer: Nicolas Pomepuy

Put the factory fallback directly in the FactoryManager as the Application may not have been instantiated

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

 libvlc/src/org/videolan/libvlc/FactoryManager.java | 14 +++++++++++++-
 libvlc/src/org/videolan/libvlc/MediaFactory.java   |  3 ---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/FactoryManager.java b/libvlc/src/org/videolan/libvlc/FactoryManager.java
index c7f281ba4f..d6e91f8193 100644
--- a/libvlc/src/org/videolan/libvlc/FactoryManager.java
+++ b/libvlc/src/org/videolan/libvlc/FactoryManager.java
@@ -1,6 +1,10 @@
 package org.videolan.libvlc;
 
+import android.util.Log;
+
 import org.videolan.libvlc.interfaces.IComponentFactory;
+import org.videolan.libvlc.interfaces.ILibVLCFactory;
+import org.videolan.libvlc.interfaces.IMediaFactory;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -13,6 +17,14 @@ public class FactoryManager {
     }
 
     public static IComponentFactory getFactory(String factoryId) {
-        return factories.get(factoryId);
+        IComponentFactory factory = factories.get(factoryId);
+        // Fallback in case the factories have not been populated. It happens in some occasions when the custom Application class has not been instantiated (probably due to the app being in a backup routine)
+        if (factory == null) {
+            Log.e("FactoryManager", "Factory doesn't exist. Falling back to hard coded one");
+            if (factoryId.equals(IMediaFactory.factoryId)) registerFactory(IMediaFactory.factoryId, new MediaFactory());
+            if (factoryId.equals(ILibVLCFactory.factoryId)) registerFactory(ILibVLCFactory.factoryId, new LibVLCFactory());
+            factory = factories.get(factoryId);
+        }
+        return factory;
     }
 }
diff --git a/libvlc/src/org/videolan/libvlc/MediaFactory.java b/libvlc/src/org/videolan/libvlc/MediaFactory.java
index 1e34cd7e46..62b7445882 100644
--- a/libvlc/src/org/videolan/libvlc/MediaFactory.java
+++ b/libvlc/src/org/videolan/libvlc/MediaFactory.java
@@ -10,9 +10,6 @@ import org.videolan.libvlc.interfaces.IMediaFactory;
 import java.io.FileDescriptor;
 
 public class MediaFactory implements IMediaFactory {
-    static {
-        FactoryManager.registerFactory(IMediaFactory.factoryId, new MediaFactory());
-    }
 
     @Override
     public IMedia getFromLocalPath(ILibVLC ILibVLC, String path) {



More information about the Android mailing list