[Android] Setup ML in one place and only when launching UI

Geoffrey Métais git at videolan.org
Fri Dec 23 11:13:34 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Dec 23 11:12:29 2016 +0100| [1e2e40a77777a21262f8b6d552c9671e0c9448f2] | committer: Geoffrey Métais

Setup ML in one place and only when launching UI

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

 .../src/org/videolan/medialibrary/Medialibrary.java     | 17 ++++++++---------
 vlc-android/src/org/videolan/vlc/StartActivity.java     | 15 +++++++++++----
 vlc-android/src/org/videolan/vlc/VLCApplication.java    |  9 +++------
 vlc-android/src/org/videolan/vlc/gui/MainActivity.java  |  2 +-
 .../src/org/videolan/vlc/gui/tv/MainTvActivity.java     |  2 +-
 5 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 82a1f7a..4ca53ec 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -76,12 +76,6 @@ public class Medialibrary {
         System.loadLibrary("mla");
     }
 
-    private Medialibrary(Context context) {
-        context = context instanceof Application ? context : context.getApplicationContext();
-        if (canReadStorage(context))
-            init(context);
-    }
-
     public void init(Context context) {
         nativeInit(context.getCacheDir()+"/vlc_media.db", context.getExternalFilesDir(null).getAbsolutePath()+"/thumbs");
         mIsInitiated = true;
@@ -95,6 +89,7 @@ public class Medialibrary {
     public String[] getDevices() {
         return nativeDevices();
     }
+
     public void addDevice(String uuid, String path, boolean removable) {
         nativeAddDevice(uuid, path, removable);
         for (String folder : banList)
@@ -119,14 +114,14 @@ public class Medialibrary {
 
     @Override
     protected void finalize() throws Throwable {
-        super.finalize();
         if (mIsInitiated)
             nativeRelease();
+        super.finalize();
     }
 
-    public static synchronized Medialibrary getInstance(Context context) {
+    public static synchronized Medialibrary getInstance() {
         if (sInstance == null)
-            sInstance = new Medialibrary(context);
+            sInstance = new Medialibrary();
         return sInstance;
     }
 
@@ -230,6 +225,10 @@ public class Medialibrary {
         return !mIsInitiated || nativeIsWorking();
     }
 
+    public boolean isInitiated() {
+        return mIsInitiated;
+    }
+
     public boolean increasePlayCount(long mediaId) {
         return mIsInitiated && mediaId > 0 && nativeIncreasePlayCount(mediaId);
     }
diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index f6ab079..bf4846d 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -42,6 +42,7 @@ import org.videolan.vlc.gui.tv.audioplayer.AudioPlayerActivity;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import org.videolan.vlc.media.MediaUtils;
 import org.videolan.vlc.util.AndroidDevices;
+import org.videolan.vlc.util.Permissions;
 import org.videolan.vlc.util.Util;
 
 import java.io.FileNotFoundException;
@@ -49,6 +50,8 @@ import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import static org.videolan.vlc.VLCApplication.getMLInstance;
+
 public class StartActivity extends Activity {
 
     public final static String TAG = "VLC/StartActivity";
@@ -63,10 +66,14 @@ public class StartActivity extends Activity {
                 startActivity(intent.setClass(this, VideoPlayerActivity.class));
             else
                 MediaUtils.openMediaNoUi(intent.getData());
-        } else if (intent != null && TextUtils.equals(intent.getAction(), AudioPlayerContainerActivity.ACTION_SHOW_PLAYER)) {
-            startActivity(new Intent(this, showTvUi() ? AudioPlayerActivity.class : MainActivity.class));
-        } else
-            startActivity(new Intent(this, showTvUi() ? MainTvActivity.class : MainActivity.class));
+        } else {
+            if (intent != null && TextUtils.equals(intent.getAction(), AudioPlayerContainerActivity.ACTION_SHOW_PLAYER))
+                startActivity(new Intent(this, showTvUi() ? AudioPlayerActivity.class : MainActivity.class));
+            else
+                startActivity(new Intent(this, showTvUi() ? MainTvActivity.class : MainActivity.class));
+            if (!getMLInstance().isInitiated() && Permissions.canReadStorage())
+                VLCApplication.setupMedialibrary(getMLInstance());
+        }
         finish();
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index f7f2b30..d9fa2df 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -40,7 +40,6 @@ import org.videolan.vlc.gui.dialogs.VlcProgressDialog;
 import org.videolan.vlc.gui.helpers.AudioUtil;
 import org.videolan.vlc.gui.helpers.BitmapCache;
 import org.videolan.vlc.util.AndroidDevices;
-import org.videolan.vlc.util.Permissions;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.VLCInstance;
 
@@ -127,9 +126,6 @@ public class VLCApplication extends Application {
         // Disable remote control receiver on Fire TV.
         if (!AndroidDevices.hasTsp())
             AndroidDevices.setRemoteControlReceiverEnabled(false);
-
-        if (Permissions.canReadStorage())
-            discoverStorages(getMLInstance());
     }
 
     /**
@@ -231,10 +227,11 @@ public class VLCApplication extends Application {
     }
 
     public static synchronized Medialibrary getMLInstance() {
-        return Medialibrary.getInstance(instance);
+        return Medialibrary.getInstance();
     }
 
-    public void discoverStorages(final Medialibrary ml) {
+    public static void setupMedialibrary(final Medialibrary ml) {
+        ml.init(getAppContext());
         for (String storage : AndroidDevices.getMediaDirectories())
             ml.addDevice(storage, storage, TextUtils.equals(storage, AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY));
         if (ml.getFoldersList().length == 0) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 6d3f645..6523ed2 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -254,7 +254,7 @@ public class MainActivity extends AudioPlayerContainerActivity implements Device
                 if (grantResults.length > 0
                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                     mMediaLibrary.init(this);
-                    ((VLCApplication) VLCApplication.getAppContext()).discoverStorages(mMediaLibrary);
+                    ((VLCApplication) VLCApplication.getAppContext()).setupMedialibrary(mMediaLibrary);
                 } else
                     Permissions.showStoragePermissionDialog(this, false);
                 break;
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
index 7e58afc..37a6aca 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -227,7 +227,7 @@ public class MainTvActivity extends BaseTvActivity implements OnItemViewSelected
                 if (grantResults.length > 0
                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                     mMediaLibrary.init(this);
-                    ((VLCApplication) VLCApplication.getAppContext()).discoverStorages(mMediaLibrary);
+                    ((VLCApplication) VLCApplication.getAppContext()).setupMedialibrary(mMediaLibrary);
                 } else {
                     Permissions.showStoragePermissionDialog(this, false);
                 }



More information about the Android mailing list