[Android] Update ML to match new API

Geoffrey Métais git at videolan.org
Wed Apr 4 18:35:31 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar 20 16:39:14 2018 +0100| [0818c5c99f0557bb7e8ca5b154cf410100b16d3e] | committer: Geoffrey Métais

Update ML to match new API

> https://code.videolan.org/videolan/vlc-android/commit/0818c5c99f0557bb7e8ca5b154cf410100b16d3e
---

 medialibrary/jni/AndroidMediaLibrary.cpp           | 17 ++++++++++++++
 medialibrary/jni/AndroidMediaLibrary.h             |  2 ++
 medialibrary/jni/medialibrary.cpp                  | 27 ++++++++++++++++------
 medialibrary/jni/utils.h                           |  1 +
 .../org/videolan/medialibrary/Medialibrary.java    | 12 +++++++---
 .../src/org/videolan/medialibrary/Tools.java       |  2 +-
 .../videolan/medialibrary/media/MediaWrapper.java  |  7 ++++++
 7 files changed, 57 insertions(+), 11 deletions(-)

diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 9df04fb6f..9cc3aac52 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -401,6 +401,13 @@ AndroidMediaLibrary::PlaylistDelete( int64_t playlistId )
     return p_ml->deletePlaylist(playlistId);
 }
 
+void
+AndroidMediaLibrary::requestThumbnail( int64_t media_id )
+{
+    medialibrary::MediaPtr media = p_ml->media(media_id);
+    if (media != nullptr) p_ml->requestThumbnail(media);
+}
+
 void
 AndroidMediaLibrary::onMediaAdded( std::vector<medialibrary::MediaPtr> mediaList )
 {
@@ -705,6 +712,16 @@ void AndroidMediaLibrary::onBackgroundTasksIdleChanged( bool isIdle )
     }
 }
 
+void AndroidMediaLibrary::onMediaThumbnailReady( medialibrary::MediaPtr media, bool success )
+{
+    JNIEnv *env = getEnv();
+    if (env != NULL && weak_thiz)
+    {
+        auto item = mediaToMediaWrapper(env, p_fields, media);
+        env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onMediaThumbnailReadyId, item, success);
+    }
+}
+
 JNIEnv *
 AndroidMediaLibrary::getEnv() {
     JNIEnv *env = (JNIEnv *)pthread_getspecific(jni_env_key);
diff --git a/medialibrary/jni/AndroidMediaLibrary.h b/medialibrary/jni/AndroidMediaLibrary.h
index 319dc91fd..f5dc2d454 100644
--- a/medialibrary/jni/AndroidMediaLibrary.h
+++ b/medialibrary/jni/AndroidMediaLibrary.h
@@ -86,6 +86,7 @@ public:
     bool playlistRemove(int64_t playlistId, int64_t mediaId);
     bool PlaylistDelete( int64_t playlistId );
 
+    void requestThumbnail( int64_t media_id );
 
     void onMediaAdded( std::vector<medialibrary::MediaPtr> media );
     void onMediaUpdated( std::vector<medialibrary::MediaPtr> media ) ;
@@ -116,6 +117,7 @@ public:
     void onEntryPointRemoved( const std::string& entryPoint, bool success );
     void onParsingStatsUpdated( uint32_t percent);
     void onBackgroundTasksIdleChanged( bool isIdle );
+    void onMediaThumbnailReady( medialibrary::MediaPtr media, bool success );
 
 private:
     void jni_detach_thread(void *data);
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index e4fb9fa67..8340bc895 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -679,8 +679,7 @@ getMediaStringMetadata(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id
 {
     AndroidMediaLibrary *aml = MediaLibrary_getInstance(env, medialibrary);
     medialibrary::MediaPtr media = aml->media(id);
-    if (media == nullptr)
-        return 0L;
+    if (media == nullptr) return 0L;
     const medialibrary::IMediaMetadata& metadata = media->metadata((medialibrary::IMedia::MetadataType)metadataType);
     return metadata.isSet() ? env->NewStringUTF(metadata.str().c_str()) : nullptr;
 }
@@ -689,10 +688,9 @@ void
 setMediaStringMetadata(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id, jint metadataType, jstring meta)
 {
     AndroidMediaLibrary *aml = MediaLibrary_getInstance(env, medialibrary);
-    const char *char_meta = env->GetStringUTFChars(meta, JNI_FALSE);
     medialibrary::MediaPtr media = aml->media(id);
-    if (media == nullptr)
-        return;
+    if (media == nullptr) return;
+    const char *char_meta = env->GetStringUTFChars(meta, JNI_FALSE);
     media->setMetadata((medialibrary::IMedia::MetadataType)metadataType, char_meta);
     env->ReleaseStringUTFChars(meta, char_meta);
 }
@@ -702,11 +700,21 @@ setMediaLongMetadata(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id,
 {
     AndroidMediaLibrary *aml = MediaLibrary_getInstance(env, medialibrary);
     medialibrary::MediaPtr media = aml->media(id);
-    if (media == nullptr)
-        return;
+    if (media == nullptr) return;
     media->setMetadata((medialibrary::IMedia::MetadataType)metadataType, meta);
 }
 
+void
+setMediaThumbnail(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id, jstring mrl)
+{
+    AndroidMediaLibrary *aml = MediaLibrary_getInstance(env, medialibrary);
+    medialibrary::MediaPtr media = aml->media(id);
+    if (media == nullptr) return;
+    const char *char_mrl = env->GetStringUTFChars(mrl, JNI_FALSE);
+    media->setThumbnail(char_mrl);
+    env->ReleaseStringUTFChars(mrl, char_mrl);
+}
+
 /*
  * Playlist methods
  */
@@ -837,6 +845,7 @@ static JNINativeMethod media_methods[] = {
     {"nativeGetMediaStringMetadata", "(Lorg/videolan/medialibrary/Medialibrary;JI)Ljava/lang/String;", (void*)getMediaStringMetadata },
     {"nativeSetMediaStringMetadata", "(Lorg/videolan/medialibrary/Medialibrary;JILjava/lang/String;)V", (void*)setMediaStringMetadata },
     {"nativeSetMediaLongMetadata", "(Lorg/videolan/medialibrary/Medialibrary;JIJ)V", (void*)setMediaLongMetadata },
+    {"nativeSetMediaThumbnail", "(Lorg/videolan/medialibrary/Medialibrary;JLjava/lang/String;)V", (void*)setMediaThumbnail },
 };
 
 static JNINativeMethod album_methods[] = {
@@ -1074,6 +1083,10 @@ int MediaLibraryJNI_OnLoad(JavaVM *vm, JNIEnv* env)
            ml_fields.MediaLibrary.onEntryPointRemovedId,
            ml_fields.MediaLibrary.clazz,
            "onEntryPointRemoved", "(Ljava/lang/String;Z)V");
+    GET_ID(GetMethodID,
+           ml_fields.MediaLibrary.onMediaThumbnailReadyId,
+           ml_fields.MediaLibrary.clazz,
+           "onMediaThumbnailReady", "(Lorg/videolan/medialibrary/media/MediaWrapper;Z)V");
 
 #undef GET_CLASS
 #undef GET_ID
diff --git a/medialibrary/jni/utils.h b/medialibrary/jni/utils.h
index 3bbd4d6a9..ebe60f7d1 100644
--- a/medialibrary/jni/utils.h
+++ b/medialibrary/jni/utils.h
@@ -56,6 +56,7 @@ struct fields {
         jmethodID onEntryPointBannedId;
         jmethodID onEntryPointUnbannedId;
         jmethodID onEntryPointRemovedId;
+        jmethodID onMediaThumbnailReadyId;
     } MediaLibrary;
     struct Album {
         jclass clazz;
diff --git a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
index 24ed40d61..04db3ce91 100644
--- a/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/Medialibrary.java
@@ -1,6 +1,8 @@
 package org.videolan.medialibrary;
 
 import android.Manifest;
+import android.arch.lifecycle.LiveData;
+import android.arch.lifecycle.MutableLiveData;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -16,7 +18,6 @@ import android.text.TextUtils;
 import android.util.Log;
 
 import org.videolan.libvlc.LibVLC;
-import org.videolan.libvlc.util.VLCUtil;
 import org.videolan.medialibrary.interfaces.DevicesDiscoveryCb;
 import org.videolan.medialibrary.interfaces.EntryPointsEventsCb;
 import org.videolan.medialibrary.interfaces.MediaAddedCb;
@@ -93,8 +94,7 @@ public class Medialibrary {
     }
 
     public int init(Context context) {
-        if (context == null)
-            return ML_INIT_FAILED;
+        if (context == null) return ML_INIT_FAILED;
         sContext = context;
         File extFilesDir = context.getExternalFilesDir(null);
         File dbDirectory = context.getDir("db", Context.MODE_PRIVATE);
@@ -531,6 +531,12 @@ public class Medialibrary {
         }
     }
 
+    public static LiveData<MediaWrapper> lastThumb = new MutableLiveData<>();
+    @SuppressWarnings({"unused", "unchecked"})
+    void onMediaThumbnailReady(MediaWrapper media, boolean success) {
+        if (success) ((MutableLiveData)lastThumb).postValue(media);
+    }
+
     public void setMediaUpdatedCb(MediaUpdatedCb mediaUpdatedCb, int flags) {
         if (!mIsInitiated)
             return;
diff --git a/medialibrary/src/org/videolan/medialibrary/Tools.java b/medialibrary/src/org/videolan/medialibrary/Tools.java
index 124e96249..ea4fa19ba 100644
--- a/medialibrary/src/org/videolan/medialibrary/Tools.java
+++ b/medialibrary/src/org/videolan/medialibrary/Tools.java
@@ -123,7 +123,7 @@ public class Tools {
         return sb.toString();
     }
 
-    static String encodeVLCMrl(String mrl) {
+    public static String encodeVLCMrl(String mrl) {
         if (mrl.startsWith("/")) mrl = "file://"+mrl;
         return Uri.encode(Uri.decode(mrl), ".-_~/()&!$*+,;='@:");
     }
diff --git a/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java b/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java
index cc03779f4..485538073 100644
--- a/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java
+++ b/medialibrary/src/org/videolan/medialibrary/media/MediaWrapper.java
@@ -615,10 +615,17 @@ public class MediaWrapper extends MediaLibraryItem implements Parcelable {
         return mId != 0;
     }
 
+    public void setThumbnail(String mrl) {
+        mArtworkURL = mrl;
+        final Medialibrary ml = Medialibrary.getInstance();
+        if (mId != 0 && ml.isInitiated()) nativeSetMediaThumbnail(ml, mId, Tools.encodeVLCMrl(mrl));
+    }
+
     private native long nativeGetMediaLongMetadata(Medialibrary ml, long id, int metaDataType);
     private native String nativeGetMediaStringMetadata(Medialibrary ml, long id, int metaDataType);
     private native void nativeSetMediaStringMetadata(Medialibrary ml, long id, int metaDataType, String metadataValue);
     private native void nativeSetMediaLongMetadata(Medialibrary ml, long id, int metaDataType, long metadataValue);
+    private native void nativeSetMediaThumbnail(Medialibrary ml, long id, String mrl);
 
     @Nullable
     public Media.Slave[] getSlaves() {



More information about the Android mailing list