[Android] Implement the medialibrary folder callbacks

Nicolas Pomepuy git at videolan.org
Tue Mar 14 11:59:19 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Feb 20 13:30:40 2023 +0100| [9a7dcee338038f512b66b907c77f98aa225c0663] | committer: Nicolas Pomepuy

Implement the medialibrary folder callbacks

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

 .../videolan/vlc/viewmodels/CallBackDelegate.kt    | 17 ++++++++-
 .../vlc/viewmodels/mobile/VideosViewModel.kt       |  1 +
 medialibrary/jni/AndroidMediaLibrary.cpp           | 41 ++++++++++++++--------
 medialibrary/jni/medialibrary.cpp                  | 12 +++++++
 .../medialibrary/interfaces/Medialibrary.java      | 40 +++++++++++++++++++++
 5 files changed, 96 insertions(+), 15 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 b5291c4649..3874c57523 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/CallBackDelegate.kt
@@ -45,6 +45,7 @@ interface ICallBackHandler {
     fun watchPlaylists()
     fun watchHistory()
     fun watchMediaGroups()
+    fun watchFolders()
     fun pause()
     fun resume()
 }
@@ -58,7 +59,8 @@ class CallBackDelegate : ICallBackHandler,
         Medialibrary.GenresCb,
         Medialibrary.PlaylistsCb,
         Medialibrary.HistoryCb,
-        Medialibrary.MediaGroupCb
+        Medialibrary.MediaGroupCb,
+        Medialibrary.FoldersCb
 {
 
     override val medialibrary = Medialibrary.getInstance()
@@ -72,6 +74,7 @@ class CallBackDelegate : ICallBackHandler,
     private var playlistsCb = false
     private var historyCb = false
     private var mediaGroupsCb = false
+    private var foldersCb = false
     var paused = false
         set(value) {
             field = value
@@ -165,6 +168,11 @@ class CallBackDelegate : ICallBackHandler,
         mediaGroupsCb = true
     }
 
+    override fun watchFolders() {
+        medialibrary.addFoldersCb(this)
+        foldersCb = true
+    }
+
     override fun releaseCallbacks() {
         medialibrary.removeOnMedialibraryReadyListener(this)
         medialibrary.removeOnDeviceChangeListener(this)
@@ -175,6 +183,7 @@ class CallBackDelegate : ICallBackHandler,
         if (playlistsCb) medialibrary.removePlaylistCb(this)
         if (historyCb) medialibrary.removeHistoryCb(this)
         if (mediaGroupsCb) medialibrary.removeMediaGroupCb(this)
+        if (foldersCb) medialibrary.removeFoldersCb(this)
         refreshActor.close()
     }
 
@@ -198,6 +207,12 @@ class CallBackDelegate : ICallBackHandler,
         deleteActor.trySend(MediaConvertedExternalAction(ids))
     }
 
+    override fun onFoldersAdded() { refreshActor.trySend(Unit) }
+
+    override fun onFoldersModified() { refreshActor.trySend(Unit) }
+
+    override fun onFoldersDeleted() { refreshActor.trySend(Unit) }
+
     override fun onArtistsAdded() { refreshActor.trySend(Unit) }
 
     override fun onArtistsModified() { refreshActor.trySend(Unit) }
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index 2f05a0c625..43a73e492e 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -72,6 +72,7 @@ class VideosViewModel(context: Context, type: VideoGroupingType, val folder: Fol
     init {
         watchMedia()
         watchMediaGroups()
+        watchFolders()
     }
 
     class Factory(val context: Context, private val groupingType: VideoGroupingType, val folder: Folder? = null, val group: VideoGroup? = null) : ViewModelProvider.NewInstanceFactory() {
diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 1782d461fb..b7d6dff513 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -1066,6 +1066,33 @@ void AndroidMediaLibrary::onMediaGroupsDeleted( std::set<int64_t> mediaGroupsIds
     }
 }
 
+void AndroidMediaLibrary::onFoldersAdded( std::vector<medialibrary::FolderPtr> folder )
+{
+    JNIEnv *env = getEnv();
+    if (env != nullptr && weak_thiz)
+    {
+        env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onFoldersAddedId);
+    }
+}
+
+void AndroidMediaLibrary::onFoldersModified( std::set<int64_t> foldersIds )
+{
+    JNIEnv *env = getEnv();
+    if (env != nullptr && weak_thiz)
+    {
+        env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onFoldersModifiedId);
+    }
+}
+
+void AndroidMediaLibrary::onFoldersDeleted( std::set<int64_t> foldersIds )
+{
+    JNIEnv *env = getEnv();
+    if (env != nullptr && weak_thiz)
+    {
+        env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onFoldersDeletedId);
+    }
+}
+
 void AndroidMediaLibrary::onBookmarksAdded( std::vector<medialibrary::BookmarkPtr> )
 {
 }
@@ -1147,20 +1174,6 @@ AndroidMediaLibrary::detachCurrentThread() {
     myVm->DetachCurrentThread();
 }
 
-void
-AndroidMediaLibrary::onFoldersAdded( std::vector<medialibrary::FolderPtr> )
-{
-}
-
-void
-AndroidMediaLibrary::onFoldersModified( std::set<int64_t> )
-{
-}
-
-void AndroidMediaLibrary::onFoldersDeleted( std::set<int64_t> )
-{
-}
-
 /*
  * Subscriptions
  */
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index a7e164e76f..1573dd544a 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -2948,6 +2948,18 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
            ml_fields.MediaLibrary.onMediaGroupDeletedId,
            ml_fields.MediaLibrary.clazz,
            "onMediaGroupDeleted", "()V");
+    GET_ID(GetMethodID,
+           ml_fields.MediaLibrary.onFoldersAddedId,
+           ml_fields.MediaLibrary.clazz,
+           "onFoldersAdded", "()V");
+    GET_ID(GetMethodID,
+           ml_fields.MediaLibrary.onFoldersModifiedId,
+           ml_fields.MediaLibrary.clazz,
+           "onFoldersModified", "()V");
+    GET_ID(GetMethodID,
+           ml_fields.MediaLibrary.onFoldersDeletedId,
+           ml_fields.MediaLibrary.clazz,
+           "onFoldersDeleted", "()V");
     GET_ID(GetMethodID,
            ml_fields.MediaLibrary.onPlaylistsModifiedId,
            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 679558717b..5f7e646558 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/Medialibrary.java
@@ -110,6 +110,7 @@ abstract public class Medialibrary {
     protected final List<PlaylistsCb> mPlaylistCbs = new ArrayList<>();
     protected final List<HistoryCb> mHistoryCbs = new ArrayList<>();
     protected final List<MediaGroupCb> mMediaGroupCbs = new ArrayList<>();
+    protected final List<FoldersCb> mFoldersCbs = new ArrayList<>();
     protected final List<OnMedialibraryReadyListener> onMedialibraryReadyListeners = new ArrayList<>();
     protected final List<OnDeviceChangeListener> onDeviceChangeListeners = new ArrayList<>();
     protected volatile boolean isMedialibraryStarted = false;
@@ -243,6 +244,12 @@ abstract public class Medialibrary {
         void onMediaGroupsDeleted();
     }
 
+    public interface FoldersCb {
+        void onFoldersAdded();
+        void onFoldersModified();
+        void onFoldersDeleted();
+    }
+
     public interface OnMedialibraryReadyListener {
         void onMedialibraryReady();
         void onMedialibraryIdle();
@@ -426,6 +433,27 @@ abstract public class Medialibrary {
         }
     }
 
+    @SuppressWarnings("unused")
+    public void onFoldersAdded() {
+        synchronized (mFoldersCbs) {
+            for (FoldersCb cb : mFoldersCbs) cb.onFoldersAdded();
+        }
+    }
+
+    @SuppressWarnings("unused")
+    public void onFoldersModified() {
+        synchronized (mFoldersCbs) {
+            for (FoldersCb cb : mFoldersCbs) cb.onFoldersModified();
+        }
+    }
+
+    @SuppressWarnings("unused")
+    public void onFoldersDeleted() {
+        synchronized (mFoldersCbs) {
+            for (FoldersCb cb : mFoldersCbs) cb.onFoldersDeleted();
+        }
+    }
+
     public void onDiscoveryStarted() {
         synchronized (devicesDiscoveryCbList) {
             if (!devicesDiscoveryCbList.isEmpty())
@@ -646,6 +674,18 @@ abstract public class Medialibrary {
         }
     }
 
+    public void addFoldersCb(FoldersCb foldersCb) {
+        synchronized (mFoldersCbs) {
+            this.mFoldersCbs.add(foldersCb);
+        }
+    }
+
+    public void removeFoldersCb(FoldersCb foldersCb) {
+        synchronized (mFoldersCbs) {
+            this.mFoldersCbs.remove(foldersCb);
+        }
+    }
+
     public void addDeviceDiscoveryCb(DevicesDiscoveryCb cb) {
         synchronized (devicesDiscoveryCbList) {
             if (!devicesDiscoveryCbList.contains(cb))



More information about the Android mailing list