mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-01 12:46:12 +00:00
t/android: Store jvm/context in service-lib
This commit is contained in:
parent
25e96a508c
commit
52c790d1a9
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue