[Android] Implement the ML callback when a media is converted to external

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


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Jun  8 13:39:14 2021 +0200| [8003c6ada6bc554ffd20f2b9965a4969971871a4] | committer: Nicolas Pomepuy

Implement the ML callback when a media is converted to external

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

 .../videolan/vlc/viewmodels/CallBackDelegate.kt    | 21 +++++++++++++++++++-
 medialibrary/jni/AndroidMediaLibrary.cpp           | 23 +++++++++++++---------
 medialibrary/jni/AndroidMediaLibrary.h             |  1 +
 medialibrary/jni/medialibrary.cpp                  |  4 ++++
 medialibrary/jni/utils.cpp                         | 15 ++++++++++++++
 medialibrary/jni/utils.h                           |  2 ++
 .../medialibrary/interfaces/Medialibrary.java      |  8 ++++++++
 7 files changed, 64 insertions(+), 10 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 f0ff861f9..115b0b5a0 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
@@ -31,6 +31,7 @@ import org.videolan.resources.AppContextProvider
 import org.videolan.tools.conflatedActor
 import org.videolan.tools.safeOffer
 import org.videolan.vlc.util.FileUtils
+import java.io.File
 
 interface ICallBackHandler {
     val medialibrary : Medialibrary
@@ -81,6 +82,18 @@ class CallBackDelegate : ICallBackHandler,
                         }
                     }
                 }
+                is MediaConvertedExternalAction -> {
+                    action.ids.forEach {mediaId ->
+                        AppContextProvider.appContext.getExternalFilesDir(null)?. let {
+                            val file = File(it.absolutePath + Medialibrary.MEDIALIB_FOLDER_NAME + "/$mediaId.jpg")
+                            if (file.exists()) {
+                                val media = medialibrary.getMedia(mediaId)
+                                //todo tell ML to remove the thumb in DB
+                            }
+                            FileUtils.deleteFile(file)
+                        }
+                    }
+                }
             }
         }
         medialibrary.addOnMedialibraryReadyListener(this at CallBackDelegate)
@@ -150,6 +163,11 @@ class CallBackDelegate : ICallBackHandler,
         deleteActor.safeOffer(MediaDeletedAction(ids))
     }
 
+    override fun onMediaConvertedToExternal(ids: LongArray) {
+        refreshActor.safeOffer(Unit)
+        deleteActor.safeOffer(MediaConvertedExternalAction(ids))
+    }
+
     override fun onArtistsAdded() { refreshActor.safeOffer(Unit) }
 
     override fun onArtistsModified() { refreshActor.safeOffer(Unit) }
@@ -184,4 +202,5 @@ class CallBackDelegate : ICallBackHandler,
 }
 
 sealed class MediaAction
-class MediaDeletedAction(val ids:LongArray): MediaAction()
\ No newline at end of file
+class MediaDeletedAction(val ids:LongArray): MediaAction()
+class MediaConvertedExternalAction(val ids:LongArray): MediaAction()
\ No newline at end of file
diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index d9b03de64..53726ff6d 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -730,21 +730,26 @@ void AndroidMediaLibrary::onMediaDeleted( std::set<int64_t> ids )
     {
         JNIEnv *env = getEnv();
         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);
+            jlongArray results = idArray(env, ids);
             env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaDeletedId, results);
             env->DeleteLocalRef(results);
         }
     }
 }
 
+void AndroidMediaLibrary::onMediaConvertedToExternal( 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) {
+            jlongArray results = idArray(env, ids);
+            env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaConvertedToExternalId, results);
+            env->DeleteLocalRef(results);
+        }
+    }
+}
+
 void AndroidMediaLibrary::onArtistsAdded( std::vector<medialibrary::ArtistPtr> artists )
 {
     if (m_mediaAddedType & (FLAG_MEDIA_ADDED_AUDIO_EMPTY|FLAG_MEDIA_ADDED_AUDIO))
diff --git a/medialibrary/jni/AndroidMediaLibrary.h b/medialibrary/jni/AndroidMediaLibrary.h
index 35299f510..b1ef950a6 100644
--- a/medialibrary/jni/AndroidMediaLibrary.h
+++ b/medialibrary/jni/AndroidMediaLibrary.h
@@ -143,6 +143,7 @@ public:
     void onMediaAdded( std::vector<medialibrary::MediaPtr> media );
     void onMediaModified( std::set<int64_t> media ) ;
     void onMediaDeleted( std::set<int64_t> ids ) ;
+    void onMediaConvertedToExternal( std::set<int64_t> ids ) ;
 
     void onArtistsAdded( std::vector<medialibrary::ArtistPtr> artists ) ;
     void onArtistsModified( std::set<int64_t> artist );
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index 23b7e1109..5ff62945f 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -2373,6 +2373,10 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
            ml_fields.MediaLibrary.onMediaDeletedId,
            ml_fields.MediaLibrary.clazz,
            "onMediaDeleted", "([J)V");
+    GET_ID(GetMethodID,
+           ml_fields.MediaLibrary.onMediaConvertedToExternalId,
+           ml_fields.MediaLibrary.clazz,
+           "onMediaConvertedToExternal", "([J)V");
     GET_ID(GetMethodID,
            ml_fields.MediaLibrary.onArtistsAddedId,
            ml_fields.MediaLibrary.clazz,
diff --git a/medialibrary/jni/utils.cpp b/medialibrary/jni/utils.cpp
index 01bd557e7..80f5d7042 100644
--- a/medialibrary/jni/utils.cpp
+++ b/medialibrary/jni/utils.cpp
@@ -261,6 +261,21 @@ convertSearchAggregateObject(JNIEnv* env, fields *fields, medialibrary::SearchAg
                           albums, artists, genres, videoList, tracksList, playlists);
 }
 
+jlongArray
+idArray(JNIEnv* env, std::set<int64_t> ids)
+{
+    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);
+    return results;
+}
+
 jobjectArray
 filteredArray(JNIEnv* env, jobjectArray array, jclass clazz, int removalCount)
 {
diff --git a/medialibrary/jni/utils.h b/medialibrary/jni/utils.h
index de75cb1a2..dce9e04ce 100644
--- a/medialibrary/jni/utils.h
+++ b/medialibrary/jni/utils.h
@@ -55,6 +55,7 @@ struct fields {
         jmethodID onMediaAddedId;
         jmethodID onMediaUpdatedId;
         jmethodID onMediaDeletedId;
+        jmethodID onMediaConvertedToExternalId;
         jmethodID onArtistsAddedId;
         jmethodID onArtistsModifiedId;
         jmethodID onArtistsDeletedId;
@@ -139,5 +140,6 @@ jobject convertVideoGroupObject(JNIEnv* env, fields *fields, medialibrary::Media
 jobject convertBookmarkObject(JNIEnv* env, fields *fields, medialibrary::BookmarkPtr const& bookmarkPtr);
 jobject convertSearchAggregateObject(JNIEnv* env, fields *fields, medialibrary::SearchAggregate const& searchAggregatePtr, jboolean includeMissing);
 jobjectArray filteredArray(JNIEnv* env, jobjectArray array, jclass clazz, int removalCount = -1);
+jlongArray idArray(JNIEnv* env, std::set<int64_t> ids);
 
 #endif //VLC_MEDIALIB_UTILS_H
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
index ef6a11252..dd01c4878 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
@@ -199,6 +199,7 @@ abstract public class Medialibrary {
         void onMediaAdded();
         void onMediaModified();
         void onMediaDeleted(long[] id);
+        void onMediaConvertedToExternal(long[] id);
     }
 
     public interface ArtistsCb {
@@ -299,6 +300,13 @@ abstract public class Medialibrary {
         }
     }
 
+    @SuppressWarnings("unused")
+    public void onMediaConvertedToExternal(long[] ids) {
+        synchronized (mMediaCbs) {
+            for (MediaCb cb : mMediaCbs) cb.onMediaConvertedToExternal(ids);
+        }
+    }
+
     @SuppressWarnings("unused")
     public void onArtistsAdded() {
         synchronized (mArtistsCbs) {



More information about the Android mailing list