[Android] Delete the video thumbs generated by the system when the medias are removed from the ML

Nicolas Pomepuy git at videolan.org
Thu Jun 10 11:15:41 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Jun  7 14:30:16 2021 +0200| [5ee44076b8c3d1ffc278d0a872f1e5ab68bce409] | committer: Nicolas Pomepuy

Delete the video thumbs generated by the system when the medias are removed from the ML

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

 .../videolan/vlc/viewmodels/CallBackDelegate.kt    | 25 +++++++++++++++++++++-
 medialibrary/jni/AndroidMediaLibrary.cpp           | 14 +++++++++++-
 medialibrary/jni/medialibrary.cpp                  |  2 +-
 .../medialibrary/interfaces/Medialibrary.java      |  6 +++---
 4 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
index 8c91bce0e..f0ff861f9 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
@@ -22,10 +22,15 @@
 package org.videolan.vlc.viewmodels
 
 import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.channels.Channel
 import kotlinx.coroutines.channels.SendChannel
+import kotlinx.coroutines.channels.actor
 import org.videolan.medialibrary.interfaces.Medialibrary
+import org.videolan.resources.AppContextProvider
 import org.videolan.tools.conflatedActor
 import org.videolan.tools.safeOffer
+import org.videolan.vlc.util.FileUtils
 
 interface ICallBackHandler {
     val medialibrary : Medialibrary
@@ -55,6 +60,7 @@ class CallBackDelegate : ICallBackHandler,
 
     override val medialibrary = Medialibrary.getInstance()
     private lateinit var refreshActor: SendChannel<Unit>
+    private lateinit var deleteActor: SendChannel<MediaAction>
 
     private var mediaCb = false
     private var artistsCb = false
@@ -66,6 +72,17 @@ class CallBackDelegate : ICallBackHandler,
 
     override fun CoroutineScope.registerCallBacks(refresh: () -> Unit) {
         refreshActor = conflatedActor { refresh() }
+        deleteActor = actor(context = Dispatchers.IO, capacity = Channel.UNLIMITED) {
+            for (action in channel) when (action) {
+                is MediaDeletedAction -> {
+                    action.ids.forEach {mediaId ->
+                        AppContextProvider.appContext.getExternalFilesDir(null)?. let {
+                            FileUtils.deleteFile(it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME + "/$mediaId.jpg" )
+                        }
+                    }
+                }
+            }
+        }
         medialibrary.addOnMedialibraryReadyListener(this at CallBackDelegate)
         medialibrary.addOnDeviceChangeListener(this at CallBackDelegate)
     }
@@ -128,7 +145,10 @@ class CallBackDelegate : ICallBackHandler,
 
     override fun onMediaModified() { refreshActor.safeOffer(Unit) }
 
-    override fun onMediaDeleted() { refreshActor.safeOffer(Unit) }
+    override fun onMediaDeleted(ids: LongArray) {
+        refreshActor.safeOffer(Unit)
+        deleteActor.safeOffer(MediaDeletedAction(ids))
+    }
 
     override fun onArtistsAdded() { refreshActor.safeOffer(Unit) }
 
@@ -162,3 +182,6 @@ class CallBackDelegate : ICallBackHandler,
 
     override fun onMediaGroupsDeleted() { refreshActor.safeOffer(Unit) }
 }
+
+sealed class MediaAction
+class MediaDeletedAction(val ids:LongArray): MediaAction()
\ No newline at end of file
diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 4cb8770ff..d9b03de64 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -729,7 +729,19 @@ void AndroidMediaLibrary::onMediaDeleted( std::set<int64_t> ids )
     if (m_mediaAddedType & (FLAG_MEDIA_ADDED_AUDIO_EMPTY|FLAG_MEDIA_ADDED_AUDIO|FLAG_MEDIA_ADDED_VIDEO|FLAG_MEDIA_ADDED_VIDEO_EMPTY))
     {
         JNIEnv *env = getEnv();
-        if (env != NULL && weak_thiz) env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaDeletedId);
+        if (env != NULL && weak_thiz) {
+            jlongArray results;
+            int i = 0;
+            results = (jlongArray)env->NewLongArray(ids.size());
+            jlong fill[ids.size()];
+            for (auto id : ids) {
+                fill[i] = id;
+                i++;
+            }
+            env->SetLongArrayRegion(results, 0, ids.size(), fill);
+            env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaDeletedId, results);
+            env->DeleteLocalRef(results);
+        }
     }
 }
 
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index f5e0ca9e2..23b7e1109 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -2372,7 +2372,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
     GET_ID(GetMethodID,
            ml_fields.MediaLibrary.onMediaDeletedId,
            ml_fields.MediaLibrary.clazz,
-           "onMediaDeleted", "()V");
+           "onMediaDeleted", "([J)V");
     GET_ID(GetMethodID,
            ml_fields.MediaLibrary.onArtistsAddedId,
            ml_fields.MediaLibrary.clazz,
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
index ed7a36c22..ef6a11252 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
@@ -198,7 +198,7 @@ abstract public class Medialibrary {
     public interface MediaCb {
         void onMediaAdded();
         void onMediaModified();
-        void onMediaDeleted();
+        void onMediaDeleted(long[] id);
     }
 
     public interface ArtistsCb {
@@ -293,9 +293,9 @@ abstract public class Medialibrary {
     }
 
     @SuppressWarnings("unused")
-    public void onMediaDeleted() {
+    public void onMediaDeleted(long[] ids) {
         synchronized (mMediaCbs) {
-            for (MediaCb cb : mMediaCbs) cb.onMediaDeleted();
+            for (MediaCb cb : mMediaCbs) cb.onMediaDeleted(ids);
         }
     }
 



More information about the Android mailing list