[Android] Medialibrary: Implement exception handler

Geoffrey Métais git at videolan.org
Thu Oct 3 10:08:47 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Oct  2 10:55:25 2019 +0200| [fd78602bbe320e9fd9cbfc3030db3683620c1592] | committer: Geoffrey Métais

Medialibrary: Implement exception handler

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

 medialibrary/jni/AndroidMediaLibrary.cpp               | 11 +++++++++++
 medialibrary/jni/AndroidMediaLibrary.h                 |  2 ++
 medialibrary/jni/medialibrary.cpp                      |  4 ++++
 medialibrary/jni/utils.h                               |  1 +
 .../medialibrary/interfaces/AbstractMedialibrary.java  | 18 ++++++++++++++++++
 5 files changed, 36 insertions(+)

diff --git a/medialibrary/jni/AndroidMediaLibrary.cpp b/medialibrary/jni/AndroidMediaLibrary.cpp
index 02e507dc7..9ff728af4 100644
--- a/medialibrary/jni/AndroidMediaLibrary.cpp
+++ b/medialibrary/jni/AndroidMediaLibrary.cpp
@@ -917,6 +917,17 @@ void AndroidMediaLibrary::onMediaThumbnailReady( medialibrary::MediaPtr media, m
     }
 }
 
+bool AndroidMediaLibrary::onUnhandledException( const char* context, const char* errMsg )
+{
+    JNIEnv *env = getEnv();
+    jstring ctx = env->NewStringUTF(context);
+    jstring msg = env->NewStringUTF(errMsg);
+    env->CallVoidMethod(weak_thiz, p_fields->MediaLibrary.onUnhandledExceptionId, ctx, msg);
+    env->DeleteLocalRef(ctx);
+    env->DeleteLocalRef(msg);
+    return true;
+}
+
 JNIEnv *
 AndroidMediaLibrary::getEnv() {
     JNIEnv *env = (JNIEnv *)pthread_getspecific(jni_env_key);
diff --git a/medialibrary/jni/AndroidMediaLibrary.h b/medialibrary/jni/AndroidMediaLibrary.h
index befde4c82..0e96c9a07 100644
--- a/medialibrary/jni/AndroidMediaLibrary.h
+++ b/medialibrary/jni/AndroidMediaLibrary.h
@@ -149,6 +149,8 @@ public:
                                bool success );
     void onHistoryChanged( medialibrary::HistoryType historyType );
 
+    bool onUnhandledException( const char* /* context */, const char* /* errMsg */ );
+
 private:
     void jni_detach_thread(void *data);
     JNIEnv *getEnv();
diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index 812158d66..c40216ce5 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -2315,6 +2315,10 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
            ml_fields.MediaLibrary.onMediaThumbnailReadyId,
            ml_fields.MediaLibrary.clazz,
            "onMediaThumbnailReady", "(Lorg/videolan/medialibrary/interfaces/media/AbstractMediaWrapper;Z)V");
+    GET_ID(GetMethodID,
+           ml_fields.MediaLibrary.onUnhandledExceptionId,
+           ml_fields.MediaLibrary.clazz,
+           "onUnhandledException", "(Ljava/lang/String;Ljava/lang/String;)V");
 
 #undef GET_CLASS
 #undef GET_ID
diff --git a/medialibrary/jni/utils.h b/medialibrary/jni/utils.h
index 4e66e1bd8..d20a3b705 100644
--- a/medialibrary/jni/utils.h
+++ b/medialibrary/jni/utils.h
@@ -79,6 +79,7 @@ struct fields {
         jmethodID onEntryPointAddedId;
         jmethodID onEntryPointRemovedId;
         jmethodID onMediaThumbnailReadyId;
+        jmethodID onUnhandledExceptionId;
     } MediaLibrary;
     struct Album {
         jclass clazz;
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java b/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java
index 111ba1af0..3bea67e21 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/AbstractMedialibrary.java
@@ -80,6 +80,7 @@ abstract public class AbstractMedialibrary {
     protected volatile boolean isMedialibraryStarted = false;
     protected final List<DevicesDiscoveryCb> devicesDiscoveryCbList = new ArrayList<>();
     protected final List<EntryPointsEventsCb> entryPointsEventsCbList = new ArrayList<>();
+    private MedialibraryExceptionHandler mExceptionHandler;
     protected static Context sContext;
     public static LiveData<AbstractMediaWrapper> lastThumb = new SingleEvent<>();
 
@@ -205,6 +206,19 @@ abstract public class AbstractMedialibrary {
         void onDeviceChange();
     }
 
+    public interface MedialibraryExceptionHandler {
+        void onUnhandledException(String context, String errMsg);
+    }
+
+    public MedialibraryExceptionHandler getExceptionHandler() {
+        return mExceptionHandler;
+    }
+
+    public void setExceptionHandler(MedialibraryExceptionHandler mExceptionHandler) {
+        this.mExceptionHandler = mExceptionHandler;
+    }
+
+
     // If media is not in ML, find it with its path
     public AbstractMediaWrapper findMedia(AbstractMediaWrapper mw) {
         if (mIsInitiated && mw != null && mw.getId() == 0L) {
@@ -388,6 +402,10 @@ abstract public class AbstractMedialibrary {
             }
         }
     }
+    @SuppressWarnings("unused")
+    public void onUnhandledException(String context, String errMsg) {
+        if (mExceptionHandler != null) mExceptionHandler.onUnhandledException(context, errMsg);
+    }
 
     @SuppressWarnings("unused")
     public void onReloadStarted(String entryPoint) {



More information about the Android mailing list