[Android] Set null media in arrays to filter correctly then

Geoffrey Métais git at videolan.org
Mon Apr 10 15:43:37 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Apr 10 10:27:57 2017 +0200| [9e6d850ef41b67343d05c234cbdb597951dac978] | committer: Geoffrey Métais

Set null media in arrays to filter correctly then

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

 medialibrary/jni/AndroidMediaLibrary.cpp | 26 +++++++++++++++---------
 medialibrary/jni/medialibrary.cpp        | 35 +++++++++++++-------------------
 2 files changed, 30 insertions(+), 31 deletions(-)

diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index c272a95..6f730dd 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -415,14 +415,17 @@ AndroidMediaLibrary::onMediaAdded( std::vector<medialibrary::MediaPtr> mediaList
         {
             mediaRefs = (jobjectArray) env->NewObjectArray(mediaList.size(), p_fields->MediaWrapper.clazz, NULL);
             index = -1;
+            jobject item;
             for (medialibrary::MediaPtr const& media : mediaList) {
                 medialibrary::IMedia::Type type = media->type();
-                if (!((type == medialibrary::IMedia::Type::Audio && m_mediaAddedType & FLAG_MEDIA_ADDED_AUDIO) ||
-                        (type == medialibrary::IMedia::Type::Video && m_mediaAddedType & FLAG_MEDIA_ADDED_VIDEO)))
-                    continue;
-                jobject item = mediaToMediaWrapper(env, p_fields, media);
+                if ((type == medialibrary::IMedia::Type::Audio && m_mediaAddedType & FLAG_MEDIA_ADDED_AUDIO) ||
+                        (type == medialibrary::IMedia::Type::Video && m_mediaAddedType & FLAG_MEDIA_ADDED_VIDEO))
+                    item = mediaToMediaWrapper(env, p_fields, media);
+                else
+                    item = nullptr;
                 env->SetObjectArrayElement(mediaRefs, ++index, item);
-                env->DeleteLocalRef(item);
+                if (item != nullptr)
+                    env->DeleteLocalRef(item);
             }
         }
 
@@ -459,14 +462,17 @@ void AndroidMediaLibrary::onMediaUpdated( std::vector<medialibrary::MediaPtr> me
         {
             mediaRefs = (jobjectArray) env->NewObjectArray(mediaList.size(), p_fields->MediaWrapper.clazz, NULL);
             index = -1;
+            jobject item;
             for (medialibrary::MediaPtr const& media : mediaList) {
                 medialibrary::IMedia::Type type = media->type();
-                if (!((type == medialibrary::IMedia::Type::Audio && m_mediaUpdatedType & FLAG_MEDIA_UPDATED_AUDIO) ||
-                        (type == medialibrary::IMedia::Type::Video && m_mediaUpdatedType & FLAG_MEDIA_UPDATED_VIDEO)))
-                    continue;
-                jobject item = mediaToMediaWrapper(env, p_fields, media);
+                if ((type == medialibrary::IMedia::Type::Audio && m_mediaUpdatedType & FLAG_MEDIA_UPDATED_AUDIO) ||
+                        (type == medialibrary::IMedia::Type::Video && m_mediaUpdatedType & FLAG_MEDIA_UPDATED_VIDEO))
+                    item = mediaToMediaWrapper(env, p_fields, media);
+                else
+                    item = nullptr;
                 env->SetObjectArrayElement(mediaRefs, ++index, item);
-                env->DeleteLocalRef(item);
+                if (item != nullptr)
+                    env->DeleteLocalRef(item);
             }
         }
         if (index > -1)
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index eb00759..e99f6d2 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -199,9 +199,8 @@ lastMediaPLayed(JNIEnv* env, jobject thiz)
     int index = -1, drops = 0;
     for(medialibrary::MediaPtr const& media : mediaPlayed) {
         jobject item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(mediaRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(mediaRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }
@@ -249,9 +248,8 @@ getVideos(JNIEnv* env, jobject thiz)
     int index = -1, drops = 0;
     for(medialibrary::MediaPtr const& media : videoFiles) {
         jobject item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(videoRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(videoRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }
@@ -267,9 +265,8 @@ getInternalAudio(JNIEnv* env, jobject thiz, medialibrary::SortingCriteria sort =
     int index = -1, drops = 0;
     for(medialibrary::MediaPtr const& media : audioFiles) {
         jobject item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(audioRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(audioRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }
@@ -529,9 +526,8 @@ getTracksFromAlbum(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id)
     jobject item = nullptr;
     for(medialibrary::MediaPtr const& media : tracks) {
         item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(mediaRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(mediaRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }
@@ -556,9 +552,8 @@ getMediaFromArtist(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id)
     int index = -1, drops = 0;
     for(medialibrary::MediaPtr const& media : mediaList) {
         jobject item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(mediaRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(mediaRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }
@@ -594,9 +589,8 @@ getMediaFromGenre(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id)
     int index = -1, drops = 0;
     for(medialibrary::MediaPtr const& media : mediaList) {
         jobject item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(mediaRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(mediaRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }
@@ -687,9 +681,8 @@ getMediaFromPlaylist(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id)
     int index = -1, drops = 0;
     for(medialibrary::MediaPtr const& media : mediaList) {
         jobject item = mediaToMediaWrapper(env, &ml_fields, media);
-        if (item != nullptr)
-            env->SetObjectArrayElement(mediaRefs, ++index, item);
-        else
+        env->SetObjectArrayElement(mediaRefs, ++index, item);
+        if (item == nullptr)
             ++drops;
         env->DeleteLocalRef(item);
     }



More information about the Android mailing list