From 6c451262715f994ab98996e16007da0044a9dbac Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sun, 15 Aug 2021 00:45:51 +0200 Subject: [PATCH] Fix native method registration --- src/mediasessionclient.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/mediasessionclient.cpp b/src/mediasessionclient.cpp index 52c4c561..afff4781 100644 --- a/src/mediasessionclient.cpp +++ b/src/mediasessionclient.cpp @@ -41,20 +41,30 @@ static void seek(JNIEnv *env, jobject thiz, jlong position) Q_UNUSED(thiz) // implement seek } -void registerNativeMethods() { - JNINativeMethod methods[] {{"play", "()V", reinterpret_cast(play)}, +static const JNINativeMethod methods[] {{"play", "()V", reinterpret_cast(play)}, {"pause", "()V", reinterpret_cast(pause)}, {"stop", "()V", reinterpret_cast(stop)}, {"next", "()V", reinterpret_cast(next)}, {"seek", "(J)V", reinterpret_cast(seek)}}; - QAndroidJniObject javaClass("org/kde/kasts/KastsActivity"); - QAndroidJniEnvironment env; - jclass objectClass = env->GetObjectClass(javaClass.object()); - env->RegisterNatives(objectClass, - methods, - sizeof(methods) / sizeof(methods[0])); - env->DeleteLocalRef(objectClass); +Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) +{ + static bool initialized = false; + if (initialized) + return JNI_VERSION_1_6; + initialized = true; + + JNIEnv *env = nullptr; + if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK) { + qWarning() << "Failed to get JNI environment."; + return -1; + } + jclass theclass = env->FindClass("org/kde/kasts/KastsActivity"); + if (env->RegisterNatives(theclass, methods, sizeof(methods) / sizeof(JNINativeMethod)) < 0) { + qWarning() << "Failed to register native functions."; + return -1; + } + return JNI_VERSION_1_4; } MediaSessionClient::MediaSessionClient(AudioManager *audioPlayer, QObject *parent)