[Android] Medialibrary: fix last thumb 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:08:10 2023 +0200| [c245074622b72d15211a6dc3b0097849b6dfe516] | committer: Nicolas Pomepuy

Medialibrary: fix last thumb leak

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

 .../videolan/vlc/gui/video/VideoGridFragment.kt    |  3 +-
 .../org/videolan/vlc/gui/video/VideoListAdapter.kt |  6 +--
 .../src/org/videolan/vlc/util/TvChannels.kt        |  6 +--
 .../src/org/videolan/medialibrary/EventTools.java  | 43 ++++++++++++++++++++++
 .../medialibrary/interfaces/Medialibrary.java      |  5 +--
 5 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
index 5c30fadb8f..6bf84dac06 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.kt
@@ -41,6 +41,7 @@ import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
+import org.videolan.medialibrary.EventTools
 import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.medialibrary.interfaces.media.Folder
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
@@ -121,7 +122,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
             }
             viewModel = getViewModel(grouping, folder, parentGroup)
             setDataObservers()
-            Medialibrary.lastThumb.observe(this, thumbObs)
+            EventTools.getInstance().lastThumb.observe(this, thumbObs)
             videoListAdapter.events.onEach { it.process() }.launchWhenStarted(lifecycleScope)
         }
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.kt
index b325dc197b..b1e408fb70 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.kt
@@ -38,8 +38,8 @@ import androidx.paging.PagedListAdapter
 import androidx.recyclerview.widget.DiffUtil
 import kotlinx.coroutines.launch
 import org.videolan.libvlc.util.AndroidUtil
+import org.videolan.medialibrary.EventTools
 import org.videolan.medialibrary.Tools
-import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.medialibrary.interfaces.media.Folder
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.medialibrary.interfaces.media.VideoGroup
@@ -75,11 +75,11 @@ class VideoListAdapter(private var isSeenMediaMarkerVisible: Boolean
     }
 
     init {
-        Medialibrary.lastThumb.observeForever(thumbObs)
+        EventTools.getInstance().lastThumb.observeForever(thumbObs)
     }
 
     fun release() {
-        Medialibrary.lastThumb.removeObserver(thumbObs)
+        EventTools.getInstance().lastThumb.removeObserver(thumbObs)
     }
 
     val all: List<MediaLibraryItem>
diff --git a/application/vlc-android/src/org/videolan/vlc/util/TvChannels.kt b/application/vlc-android/src/org/videolan/vlc/util/TvChannels.kt
index d553ea8ad4..765f5faa6c 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/TvChannels.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/TvChannels.kt
@@ -33,7 +33,7 @@ import androidx.lifecycle.lifecycleScope
 import androidx.tvprovider.media.tv.TvContractCompat
 import androidx.tvprovider.media.tv.WatchNextProgram
 import kotlinx.coroutines.*
-import org.videolan.medialibrary.interfaces.Medialibrary
+import org.videolan.medialibrary.EventTools
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.resources.util.getFromMl
 import org.videolan.tools.AppScope
@@ -106,7 +106,7 @@ suspend fun insertWatchNext(context: Context, mw: MediaWrapper) {
 }
 
 suspend fun updateNextProgramAfterThumbnailGeneration(lifecycleOwner: LifecycleOwner, context: Context, mw: MediaWrapper) {
-    Medialibrary.lastThumb.observe(lifecycleOwner) { media ->
+    EventTools.getInstance().lastThumb.observe(lifecycleOwner) { media ->
         lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
             var cursor: Cursor? = null
             try {
@@ -134,7 +134,7 @@ suspend fun updateNextProgramAfterThumbnailGeneration(lifecycleOwner: LifecycleO
                 cursor?.close()
             }
         }
-        Medialibrary.lastThumb.removeObservers(lifecycleOwner)
+        EventTools.getInstance().lastThumb.removeObservers(lifecycleOwner)
     }
 }
 
diff --git a/medialibrary/src/org/videolan/medialibrary/EventTools.java b/medialibrary/src/org/videolan/medialibrary/EventTools.java
new file mode 100644
index 0000000000..b3b1a82e7a
--- /dev/null
+++ b/medialibrary/src/org/videolan/medialibrary/EventTools.java
@@ -0,0 +1,43 @@
+/*
+ * ************************************************************************
+ *  EventTools.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 androidx.lifecycle.LiveData;
+
+import org.videolan.medialibrary.interfaces.media.MediaWrapper;
+
+public final class EventTools {
+    private static EventTools sInstance;
+    public final LiveData<MediaWrapper> lastThumb = new SingleEvent<>();
+
+
+    public static EventTools getInstance() {
+        if (sInstance == null) {
+            sInstance = new EventTools();
+        }
+        return sInstance;
+    }
+
+}
\ No newline at end of file
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
index 1c2211f10b..0db5141dc5 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
@@ -34,8 +34,8 @@ import androidx.core.content.ContextCompat;
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 
+import org.videolan.medialibrary.EventTools;
 import org.videolan.medialibrary.MLServiceLocator;
-import org.videolan.medialibrary.SingleEvent;
 import org.videolan.medialibrary.Tools;
 import org.videolan.medialibrary.interfaces.media.Album;
 import org.videolan.medialibrary.interfaces.media.Artist;
@@ -117,7 +117,6 @@ abstract public class Medialibrary {
     protected final List<DevicesDiscoveryCb> devicesDiscoveryCbList = new ArrayList<>();
     protected final List<EntryPointsEventsCb> entryPointsEventsCbList = new ArrayList<>();
     private MedialibraryExceptionHandler mExceptionHandler;
-    public static final LiveData<MediaWrapper> lastThumb = new SingleEvent<>();
 
     protected static final Medialibrary instance = MLServiceLocator.getAbstractMedialibrary();
 
@@ -582,7 +581,7 @@ abstract public class Medialibrary {
     //    public static LiveData<MediaWrapper> lastThumb = new SingleEvent<>();
     @SuppressWarnings({"unused", "unchecked"})
     public void onMediaThumbnailReady(MediaWrapper media, boolean success) {
-        if (success) ((MutableLiveData<MediaWrapper>)lastThumb).postValue(media);
+        if (success) ((MutableLiveData<MediaWrapper>) EventTools.getInstance().lastThumb).postValue(media);
     }
 
     public void addMediaCb(MediaCb mediaUpdatedCb) {



More information about the Android mailing list