android: Add support for casting Context to Activity

This commit is contained in:
Ryan Pavlik 2021-04-09 17:00:17 -05:00
parent 43e0206abc
commit 65a7b490a8
3 changed files with 25 additions and 7 deletions

View file

@ -6,7 +6,7 @@ set(ANDROID_SOURCE_FILES
android/android_ahardwarebuffer_allocator.h android/android_ahardwarebuffer_allocator.h
android/android_custom_surface.cpp android/android_custom_surface.cpp
android/android_custom_surface.h android/android_custom_surface.h
android/android_globals.c android/android_globals.cpp
android/android_globals.h android/android_globals.h
android/android_load_class.cpp android/android_load_class.cpp
android/android_load_class.hpp android/android_load_class.hpp

View file

@ -10,6 +10,7 @@
#include "android_globals.h" #include "android_globals.h"
#include <stddef.h> #include <stddef.h>
#include <wrap/android.app.h>
/*! /*!
* @todo Do we need locking here? Do we need to create global refs for the * @todo Do we need locking here? Do we need to create global refs for the
@ -17,11 +18,11 @@
*/ */
static struct static struct
{ {
struct _JavaVM *vm; struct _JavaVM *vm = nullptr;
void *activity; void *activity = nullptr;
void *context; void *context = nullptr;
struct _ANativeWindow *window; struct _ANativeWindow *window = nullptr;
} android_globals = {NULL, NULL, NULL}; } android_globals;
void void
android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity) 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 void
android_globals_store_vm_and_context(struct _JavaVM *vm, void *context) android_globals_store_vm_and_context(struct _JavaVM *vm, void *context)
{ {
android_globals.vm = vm; android_globals.vm = vm;
android_globals.context = context; 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 void
android_globals_store_window(struct _ANativeWindow *window) android_globals_store_window(struct _ANativeWindow *window)
{ {

View file

@ -34,6 +34,13 @@ android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity);
void void
android_globals_store_vm_and_context(struct _JavaVM *vm, void *context); 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. * Retrieve the Java VM pointer previously stored, if any.
*/ */