[Android] medialibrary: use utils::jni::longArray for native long arrays

Hugo Beauzée-Luyssen git at videolan.org
Mon Sep 27 09:18:54 UTC 2021


vlc-android | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Fri Sep 24 15:23:48 2021 +0200| [3a85bc787e2b35780e889c4306a587b1fd1d8987] | committer: Nicolas Pomepuy

medialibrary: use utils::jni::longArray for native long arrays

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

 medialibrary/jni/AndroidMediaLibrary.cpp | 10 ++++------
 medialibrary/jni/utils.cpp               |  7 +++----
 medialibrary/jni/utils.h                 |  3 ++-
 3 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 87e981f20..e7dd12b2c 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -735,9 +735,8 @@ void AndroidMediaLibrary::onMediaDeleted( std::set<int64_t> ids )
     {
         JNIEnv *env = getEnv();
         if (env != NULL && weak_thiz) {
-            jlongArray results = idArray(env, ids);
-            env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaDeletedId, results);
-            env->DeleteLocalRef(results);
+            auto results = idArray(env, ids);
+            env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaDeletedId, results.get());
         }
     }
 }
@@ -748,9 +747,8 @@ void AndroidMediaLibrary::onMediaConvertedToExternal( std::set<int64_t> ids )
     {
         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);
+            auto results = idArray(env, ids);
+            env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaConvertedToExternalId, results.get());
         }
     }
 }
diff --git a/medialibrary/jni/utils.cpp b/medialibrary/jni/utils.cpp
index 3761b7ac9..151b218a2 100644
--- a/medialibrary/jni/utils.cpp
+++ b/medialibrary/jni/utils.cpp
@@ -231,18 +231,17 @@ convertSearchAggregateObject(JNIEnv* env, fields *fields, medialibrary::SearchAg
     };
 }
 
-jlongArray
+utils::jni::longArray
 idArray(JNIEnv* env, std::set<int64_t> ids)
 {
-    jlongArray results;
     int i = 0;
-    results = (jlongArray)env->NewLongArray(ids.size());
+    utils::jni::longArray results{ env, (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->SetLongArrayRegion(results.get(), 0, ids.size(), fill);
     return results;
 }
 
diff --git a/medialibrary/jni/utils.h b/medialibrary/jni/utils.h
index afa8d9fc4..77ccb9135 100644
--- a/medialibrary/jni/utils.h
+++ b/medialibrary/jni/utils.h
@@ -135,6 +135,7 @@ private:
 using string = localref<jstring>;
 using object = localref<jobject>;
 using objectArray = localref<jobjectArray>;
+using longArray = localref<jlongArray>;
 
 }
 }
@@ -239,7 +240,7 @@ utils::jni::object convertVideoGroupObject(JNIEnv* env, fields *fields, medialib
 utils::jni::object convertBookmarkObject(JNIEnv* env, fields *fields, medialibrary::BookmarkPtr const& bookmarkPtr);
 utils::jni::object convertSearchAggregateObject(JNIEnv* env, fields *fields, medialibrary::SearchAggregate const& searchAggregatePtr, jboolean includeMissing);
 utils::jni::objectArray filteredArray(JNIEnv* env, utils::jni::objectArray array, jclass clazz, int removalCount = -1);
-jlongArray idArray(JNIEnv* env, std::set<int64_t> ids);
+utils::jni::longArray idArray(JNIEnv* env, std::set<int64_t> ids);
 utils::jni::string vlcNewStringUTF(JNIEnv* env, const char* psz_string);
 
 #endif //VLC_MEDIALIB_UTILS_H



More information about the Android mailing list