diff --git a/src/xrt/auxiliary/CMakeLists.txt b/src/xrt/auxiliary/CMakeLists.txt index fa92cd003..86b302bcf 100644 --- a/src/xrt/auxiliary/CMakeLists.txt +++ b/src/xrt/auxiliary/CMakeLists.txt @@ -6,7 +6,7 @@ set(ANDROID_SOURCE_FILES android/android_ahardwarebuffer_allocator.h android/android_custom_surface.cpp android/android_custom_surface.h - android/android_globals.c + android/android_globals.cpp android/android_globals.h android/android_load_class.cpp android/android_load_class.hpp diff --git a/src/xrt/auxiliary/android/android_globals.c b/src/xrt/auxiliary/android/android_globals.cpp similarity index 68% rename from src/xrt/auxiliary/android/android_globals.c rename to src/xrt/auxiliary/android/android_globals.cpp index 9bf33bdf9..fd7b68f8b 100644 --- a/src/xrt/auxiliary/android/android_globals.c +++ b/src/xrt/auxiliary/android/android_globals.cpp @@ -10,6 +10,7 @@ #include "android_globals.h" #include +#include /*! * @todo Do we need locking here? Do we need to create global refs for the @@ -17,11 +18,11 @@ */ static struct { - struct _JavaVM *vm; - void *activity; - void *context; - struct _ANativeWindow *window; -} android_globals = {NULL, NULL, NULL}; + struct _JavaVM *vm = nullptr; + void *activity = nullptr; + void *context = nullptr; + struct _ANativeWindow *window = nullptr; +} android_globals; void android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity) @@ -33,11 +34,21 @@ android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity) void android_globals_store_vm_and_context(struct _JavaVM *vm, void *context) { - android_globals.vm = vm; android_globals.context = context; + if (android_globals_is_instance_of_activity(vm, context)) { + android_globals.activity = context; + } } +bool +android_globals_is_instance_of_activity(struct _JavaVM *vm, void *obj) +{ + jni::init(vm); + + 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) { diff --git a/src/xrt/auxiliary/android/android_globals.h b/src/xrt/auxiliary/android/android_globals.h index 5257c7d1e..95f4abc72 100644 --- a/src/xrt/auxiliary/android/android_globals.h +++ b/src/xrt/auxiliary/android/android_globals.h @@ -34,6 +34,13 @@ android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity); void android_globals_store_vm_and_context(struct _JavaVM *vm, void *context); + +/*! + * Is the provided jobject an instance of android.app.Activity? + */ +bool +android_globals_is_instance_of_activity(struct _JavaVM *vm, void *obj); + /*! * Retrieve the Java VM pointer previously stored, if any. */