FreeRDP
android.c
1 
20 #include "android.h"
21 #include <jni.h>
22 
23 #include <winpr/winpr.h>
24 #include <winpr/assert.h>
25 
26 #include "../log.h"
27 
28 #define TAG WINPR_TAG("android")
29 
30 JavaVM* jniVm = NULL;
31 
32 WINPR_API jint JNI_OnLoad(JavaVM* vm, void* reserved)
33 {
34  WLog_INFO(TAG, "Setting up JNI environement...");
35 
36  jniVm = vm;
37  return JNI_VERSION_1_6;
38 }
39 
40 WINPR_API void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved)
41 {
42  JNIEnv* env = NULL;
43  WLog_INFO(TAG, "Tearing down JNI environement...");
44 
45  if ((*jniVm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
46  {
47  WLog_FATAL(TAG, "Failed to get the environment");
48  return;
49  }
50 }
51 
52 jboolean winpr_jni_attach_thread(JNIEnv** env)
53 {
54  WINPR_ASSERT(jniVm);
55 
56  if ((*jniVm)->GetEnv(jniVm, (void**)env, JNI_VERSION_1_4) != JNI_OK)
57  {
58  WLog_INFO(TAG, "android_java_callback: attaching current thread");
59  (*jniVm)->AttachCurrentThread(jniVm, env, NULL);
60 
61  if ((*jniVm)->GetEnv(jniVm, (void**)env, JNI_VERSION_1_4) != JNI_OK)
62  {
63  WLog_ERR(TAG, "android_java_callback: failed to obtain current JNI environment");
64  }
65 
66  return JNI_TRUE;
67  }
68 
69  return JNI_FALSE;
70 }
71 
72 /* attach current thread to JVM */
73 void winpr_jni_detach_thread(void)
74 {
75  WINPR_ASSERT(jniVm);
76  (*jniVm)->DetachCurrentThread(jniVm);
77 }