[Android] Medialibrary: don't store a Context in a static instance

Thomas Guillem git at videolan.org
Fri Nov 4 09:32:40 CET 2016


vlc-android | branch: master | Thomas Guillem <thomas at gllm.fr> | Fri Nov  4 09:31:08 2016 +0100| [d66e11626b922107034c2ab4525316e5e9d54517] | committer: Thomas Guillem

Medialibrary: don't store a Context in a static instance

This instance won't be destroyed and will leak the context.

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

 .../src/org/videolan/medialibrary/Medialibrary.java       | 15 +++++++--------
 vlc-android/src/org/videolan/vlc/gui/MainActivity.java    |  4 ++--
 .../src/org/videolan/vlc/gui/tv/MainTvActivity.java       |  2 +-
 3 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 5ca0c7d..464a8ab 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -59,7 +59,6 @@ public class Medialibrary {
     private static final MediaWrapper[] EMPTY_COLLECTION = {};
 
     private long mInstanceID;
-    private Context mContext;
     private boolean mIsInitiated = false;
 
     private MediaUpdatedCb mediaUpdatedCb = null;
@@ -78,13 +77,13 @@ public class Medialibrary {
     }
 
     private Medialibrary(Context context) {
-        mContext = context instanceof Application ? context : context.getApplicationContext();
-        if (canReadStorage())
-            init();
+        context = context instanceof Application ? context : context.getApplicationContext();
+        if (canReadStorage(context))
+            init(context);
     }
 
-    public void init() {
-        nativeInit(mContext.getCacheDir()+"/vlc_media.db", mContext.getExternalFilesDir(null).getAbsolutePath()+"/thumbs");
+    public void init(Context context) {
+        nativeInit(context.getCacheDir()+"/vlc_media.db", context.getExternalFilesDir(null).getAbsolutePath()+"/thumbs");
         mIsInitiated = true;
     }
 
@@ -403,8 +402,8 @@ public class Medialibrary {
     private native void nativeSetMediaAddedCbFlag(int flags);
     private native SearchAggregate nativeSearch(String query);
 
-    private boolean canReadStorage() {
-        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(mContext,
+    private boolean canReadStorage(Context context) {
+        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context,
                 Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 701385e..bed289b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -113,7 +113,7 @@ public class MainActivity extends AudioPlayerContainerActivity implements Device
     private static final int ACTIVITY_UPDATE_PROGRESS = 7;
 
 
-    Medialibrary mMediaLibrary;
+    private Medialibrary mMediaLibrary;
 
     private HackyDrawerLayout mDrawerLayout;
     private NavigationView mNavigationView;
@@ -252,7 +252,7 @@ public class MainActivity extends AudioPlayerContainerActivity implements Device
                 // If request is cancelled, the result arrays are empty.
                 if (grantResults.length > 0
                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
-                    mMediaLibrary.init();
+                    mMediaLibrary.init(this);
                     ((VLCApplication) VLCApplication.getAppContext()).discoverStorages(mMediaLibrary);
                 } else
                     Permissions.showStoragePermissionDialog(this, false);
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 49d9349..5ca5de5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -210,7 +210,7 @@ public class MainTvActivity extends BaseTvActivity implements OnItemViewSelected
                 // If request is cancelled, the result arrays are empty.
                 if (grantResults.length > 0
                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
-                    mMediaLibrary.init();
+                    mMediaLibrary.init(this);
                     ((VLCApplication) VLCApplication.getAppContext()).discoverStorages(mMediaLibrary);
                 } else {
                     Permissions.showStoragePermissionDialog(this, false);



More information about the Android mailing list