[Android] Medialibrary: fix context leak

Nicolas Pomepuy git at videolan.org
Thu Sep 7 08:21:02 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Jul 25 09:00:13 2023 +0200| [adc0ad8940af871c7ef13c613c13bc094b88b96d] | committer: Nicolas Pomepuy

Medialibrary: fix context leak

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

 .../org/videolan/medialibrary/MLContextTools.java  | 47 ++++++++++++++++++++++
 .../videolan/medialibrary/MedialibraryImpl.java    | 10 +++--
 .../medialibrary/interfaces/Medialibrary.java      |  5 ---
 .../medialibrary/interfaces/media/Album.java       |  3 +-
 .../medialibrary/interfaces/media/Artist.java      |  7 ++--
 .../medialibrary/stubs/StubMedialibrary.java       |  6 +--
 6 files changed, 62 insertions(+), 16 deletions(-)

diff --git a/medialibrary/src/org/videolan/medialibrary/MLContextTools.java b/medialibrary/src/org/videolan/medialibrary/MLContextTools.java
new file mode 100644
index 0000000000..7ff6efbc14
--- /dev/null
+++ b/medialibrary/src/org/videolan/medialibrary/MLContextTools.java
@@ -0,0 +1,47 @@
+/*
+ * ************************************************************************
+ *  ContextTools.java
+ * *************************************************************************
+ * Copyright © 2023 VLC authors and VideoLAN
+ * Author: Nicolas POMEPUY
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * **************************************************************************
+ *
+ *
+ */
+
+package org.videolan.medialibrary;
+
+import android.content.Context;
+
+public final class MLContextTools {
+    private static MLContextTools sInstance;
+    private Context mContext;
+
+    public void setContext(Context context) {
+        this.mContext = context.getApplicationContext();
+    }
+
+    public static MLContextTools getInstance() {
+        if (sInstance == null) {
+            sInstance = new MLContextTools();
+        }
+        return sInstance;
+    }
+
+    public Context getContext() {
+        return mContext;
+    }
+}
\ No newline at end of file
diff --git a/medialibrary/src/org/videolan/medialibrary/MedialibraryImpl.java b/medialibrary/src/org/videolan/medialibrary/MedialibraryImpl.java
index 55199cee95..b0231daccd 100644
--- a/medialibrary/src/org/videolan/medialibrary/MedialibraryImpl.java
+++ b/medialibrary/src/org/videolan/medialibrary/MedialibraryImpl.java
@@ -25,10 +25,12 @@ import android.net.Uri;
 import android.text.TextUtils;
 import android.util.Log;
 
-import androidx.annotation.*;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.WorkerThread;
 
 import org.videolan.libvlc.LibVLC;
-import org.videolan.libvlc.util.*;
+import org.videolan.libvlc.util.VLCUtil;
 import org.videolan.medialibrary.interfaces.Medialibrary;
 import org.videolan.medialibrary.interfaces.media.Album;
 import org.videolan.medialibrary.interfaces.media.Artist;
@@ -48,7 +50,7 @@ public class MedialibraryImpl extends Medialibrary {
     public boolean construct(Context context) {
         if (context == null) throw new IllegalStateException("context cannot be null");
         if (mIsInitiated) return false;
-        sContext = context;
+        MLContextTools.getInstance().setContext(context);
         final File extFilesDir = context.getExternalFilesDir(null);
         File dbDirectory = context.getDir("db", Context.MODE_PRIVATE);
         if (extFilesDir == null || !extFilesDir.exists()
@@ -83,7 +85,7 @@ public class MedialibraryImpl extends Medialibrary {
     public int init(Context context) {
         if (context == null) return ML_INIT_FAILED;
         if (mIsInitiated) return ML_INIT_ALREADY_INITIALIZED;
-        if (sContext == null) throw new IllegalStateException("Medialibrary construct has to be called before init");
+        if (MLContextTools.getInstance().getContext() == null) throw new IllegalStateException("Medialibrary construct has to be called before init");
         File dbDirectory = context.getDir("db", Context.MODE_PRIVATE);
         int initCode = nativeInit(dbDirectory + VLC_MEDIA_DB_NAME);
         if (initCode == ML_INIT_DB_CORRUPTED) {
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
index 5f7e646558..1c2211f10b 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
@@ -117,15 +117,10 @@ abstract public class Medialibrary {
     protected final List<DevicesDiscoveryCb> devicesDiscoveryCbList = new ArrayList<>();
     protected final List<EntryPointsEventsCb> entryPointsEventsCbList = new ArrayList<>();
     private MedialibraryExceptionHandler mExceptionHandler;
-    protected static Context sContext;
     public static final LiveData<MediaWrapper> lastThumb = new SingleEvent<>();
 
     protected static final Medialibrary instance = MLServiceLocator.getAbstractMedialibrary();
 
-    public static Context getContext() {
-        return sContext;
-    }
-
     public static LiveData<Boolean> getState() {
         return sRunning;
     }
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/media/Album.java b/medialibrary/src/org/videolan/medialibrary/interfaces/media/Album.java
index 72dc6d3dec..6e422edd30 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/media/Album.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/media/Album.java
@@ -7,6 +7,7 @@ import android.util.Log;
 
 import org.videolan.BuildConfig;
 import org.videolan.libvlc.util.VLCUtil;
+import org.videolan.medialibrary.MLContextTools;
 import org.videolan.medialibrary.MLServiceLocator;
 import org.videolan.medialibrary.R;
 import org.videolan.medialibrary.interfaces.Medialibrary;
@@ -14,7 +15,7 @@ import org.videolan.medialibrary.media.MediaLibraryItem;
 
 public abstract class Album extends MediaLibraryItem {
     public static class SpecialRes {
-        public static String UNKNOWN_ALBUM = Medialibrary.getContext().getString(R.string.unknown_album);
+        public static String UNKNOWN_ALBUM = MLContextTools.getInstance().getContext().getString(R.string.unknown_album);
     }
 
     protected int releaseYear;
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/media/Artist.java b/medialibrary/src/org/videolan/medialibrary/interfaces/media/Artist.java
index f42e7578d4..21e9c92c5a 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/media/Artist.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/media/Artist.java
@@ -4,8 +4,9 @@ import android.os.Parcel;
 import android.os.Parcelable;
 
 import org.videolan.libvlc.util.VLCUtil;
-import org.videolan.medialibrary.R;
+import org.videolan.medialibrary.MLContextTools;
 import org.videolan.medialibrary.MLServiceLocator;
+import org.videolan.medialibrary.R;
 import org.videolan.medialibrary.interfaces.Medialibrary;
 import org.videolan.medialibrary.media.MediaLibraryItem;
 
@@ -19,8 +20,8 @@ abstract public class Artist extends MediaLibraryItem {
     private int presentTracksCount;
 
     public static class SpecialRes {
-        public static String UNKNOWN_ARTIST = Medialibrary.getContext().getString(R.string.unknown_artist);
-        public static String VARIOUS_ARTISTS = Medialibrary.getContext().getString(R.string.various_artists);
+        public static String UNKNOWN_ARTIST = MLContextTools.getInstance().getContext().getString(R.string.unknown_artist);
+        public static String VARIOUS_ARTISTS = MLContextTools.getInstance().getContext().getString(R.string.various_artists);
     }
 
     public Artist(long id, String name, String shortBio, String artworkMrl, String musicBrainzId, int albumsCount, int tracksCount, int presentTracksCount, boolean isFavorite) {
diff --git a/medialibrary/src/org/videolan/medialibrary/stubs/StubMedialibrary.java b/medialibrary/src/org/videolan/medialibrary/stubs/StubMedialibrary.java
index 665ccd2bbf..4716c56b45 100644
--- a/medialibrary/src/org/videolan/medialibrary/stubs/StubMedialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/stubs/StubMedialibrary.java
@@ -7,6 +7,7 @@ import android.webkit.URLUtil;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
+import org.videolan.medialibrary.MLContextTools;
 import org.videolan.medialibrary.MLServiceLocator;
 import org.videolan.medialibrary.Tools;
 import org.videolan.medialibrary.interfaces.Medialibrary;
@@ -30,13 +31,12 @@ public class StubMedialibrary extends Medialibrary {
     private StubDataSource dt = StubDataSource.getInstance();
 
     public boolean construct(Context context) {
-        if (context == null) return false;
-        sContext = context;
         return true;
     }
 
     public int init(Context context) {
-        if (context == null || sContext == null) return ML_INIT_FAILED;
+        if (context == null) return ML_INIT_FAILED;
+        MLContextTools.getInstance().setContext(context);
         return ML_INIT_SUCCESS;
     }
 



More information about the Android mailing list