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

Nicolas Pomepuy git at videolan.org
Tue Nov 3 15:47:09 CET 2020


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

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

(cherry picked from commit cfb4b342a8d9c75db9033343554fee2be3a69d37)

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

 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 c7f281ba4..d6e91f819 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 1e34cd7e4..62b744588 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