t/android: Store jvm/context in service-lib

This commit is contained in:
Jarvis Huang 2022-08-31 16:57:49 +08:00 committed by Ryan Pavlik
parent 25e96a508c
commit 52c790d1a9
3 changed files with 41 additions and 13 deletions

View file

@ -13,31 +13,32 @@
#include <wrap/android.app.h>
/*!
* @todo Do we need locking here? Do we need to create global refs for the
* supplied jobjects?
* @todo Do we need locking here?
*/
static struct
{
struct _JavaVM *vm = nullptr;
void *activity = nullptr;
void *context = nullptr;
jni::Object activity = {};
jni::Object context = {};
struct _ANativeWindow *window = nullptr;
} android_globals;
void
android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity)
{
jni::init(vm);
android_globals.vm = vm;
android_globals.activity = activity;
android_globals.activity = jni::Object((jobject)activity);
}
void
android_globals_store_vm_and_context(struct _JavaVM *vm, void *context)
{
jni::init(vm);
android_globals.vm = vm;
android_globals.context = context;
android_globals.context = jni::Object((jobject)context);
if (android_globals_is_instance_of_activity(vm, context)) {
android_globals.activity = context;
android_globals.activity = jni::Object((jobject)context);
}
}
@ -49,6 +50,7 @@ android_globals_is_instance_of_activity(struct _JavaVM *vm, void *obj)
auto activity_cls = jni::Class(wrap::android::app::Activity::getTypeName());
return JNI_TRUE == jni::env()->IsInstanceOf((jobject)obj, activity_cls.getHandle());
}
void
android_globals_store_window(struct _ANativeWindow *window)
{
@ -70,15 +72,12 @@ android_globals_get_vm()
void *
android_globals_get_activity()
{
return android_globals.activity;
return android_globals.activity.getHandle();
}
void *
android_globals_get_context()
{
void *ret = android_globals.context;
if (ret == NULL) {
ret = android_globals.activity;
}
return ret;
return android_globals.context.isNull() ? android_globals.activity.getHandle()
: android_globals.context.getHandle();
}

View file

@ -8,6 +8,9 @@
package org.freedesktop.monado.openxr_runtime;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import dagger.hilt.android.HiltAndroidApp;
@ -16,4 +19,16 @@ import dagger.hilt.android.HiltAndroidApp;
*/
@HiltAndroidApp
public class MonadoOpenXrApplication extends Application {
static {
System.loadLibrary("monado-service");
}
@Override
public void onCreate() {
super.onCreate();
nativeStoreContext(getApplicationContext());
}
private native void nativeStoreContext(@NonNull Context context);
}

View file

@ -169,3 +169,17 @@ Java_org_freedesktop_monado_ipc_MonadoImpl_nativeShutdownServer(JNIEnv *env, job
return IpcServerHelper::instance().shutdownServer();
}
extern "C" JNIEXPORT void JNICALL
Java_org_freedesktop_monado_openxr_1runtime_MonadoOpenXrApplication_nativeStoreContext(JNIEnv *env,
jobject thiz,
jobject context)
{
JavaVM *jvm = nullptr;
jint result = env->GetJavaVM(&jvm);
assert(result == JNI_OK);
assert(jvm);
jni::init(env);
android_globals_store_vm_and_context(jvm, context);
}