android: Added a global variable to check the current SDK version
[strongswan.git] / src / frontends / android / jni / libandroidbridge / android_jni.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "android_jni.h"
19
20 #include <library.h>
21 #include <threading/thread_value.h>
22
23 /**
24 * JVM
25 */
26 static JavaVM *android_jvm;
27
28 jclass *android_charonvpnservice_class;
29 jclass *android_charonvpnservice_builder_class;
30 android_sdk_version_t android_sdk_version;
31
32 /**
33 * Thread-local variable. Only used because of the destructor
34 */
35 static thread_value_t *androidjni_threadlocal;
36
37 /**
38 * Thread-local destructor to ensure that a native thread is detached
39 * from the JVM even if androidjni_detach_thread() is not called.
40 */
41 static void attached_thread_cleanup(void *arg)
42 {
43 (*android_jvm)->DetachCurrentThread(android_jvm);
44 }
45
46 /*
47 * Described in header
48 */
49 void androidjni_attach_thread(JNIEnv **env)
50 {
51 if ((*android_jvm)->GetEnv(android_jvm, (void**)env,
52 JNI_VERSION_1_6) == JNI_OK)
53 { /* already attached or even a Java thread */
54 return;
55 }
56 (*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
57 /* use a thread-local value with a destructor that automatically detaches
58 * the thread from the JVM before it terminates, if not done manually */
59 androidjni_threadlocal->set(androidjni_threadlocal, (void*)*env);
60 }
61
62 /*
63 * Described in header
64 */
65 void androidjni_detach_thread()
66 {
67 if (androidjni_threadlocal->get(androidjni_threadlocal))
68 { /* only do this if we actually attached this thread */
69 androidjni_threadlocal->set(androidjni_threadlocal, NULL);
70 (*android_jvm)->DetachCurrentThread(android_jvm);
71 }
72 }
73
74 /**
75 * Called when this library is loaded by the JVM
76 */
77 jint JNI_OnLoad(JavaVM *vm, void *reserved)
78 {
79 JNIEnv *env;
80 jclass jversion;
81 jfieldID jsdk_int;
82
83 android_jvm = vm;
84
85 if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
86 {
87 return -1;
88 }
89
90 androidjni_threadlocal = thread_value_create(attached_thread_cleanup);
91
92 android_charonvpnservice_class =
93 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
94 JNI_PACKAGE_STRING "/CharonVpnService"));
95 android_charonvpnservice_builder_class =
96 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
97 JNI_PACKAGE_STRING "/CharonVpnService$BuilderAdapter"));
98
99 jversion = (*env)->FindClass(env, "android/os/Build$VERSION");
100 jsdk_int = (*env)->GetStaticFieldID(env, jversion, "SDK_INT", "I");
101 android_sdk_version = (*env)->GetStaticIntField(env, jversion, jsdk_int);
102
103 return JNI_VERSION_1_6;
104 }
105
106 /**
107 * Called when this library is unloaded by the JVM (which never happens on
108 * Android)
109 */
110 void JNI_OnUnload(JavaVM *vm, void *reserved)
111 {
112 androidjni_threadlocal->destroy(androidjni_threadlocal);
113 }
114