]> sigrok.org Git - pulseview.git/blob - android/envsetup.cpp
Added files for building Android APK
[pulseview.git] / android / envsetup.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 Marcus Comstedt <marcus@mc.pp.se>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <jni.h>
21 #include <stdlib.h>
22
23 jint JNI_OnLoad(JavaVM *vm, void *reserved)
24 {
25         JNIEnv* env;
26
27         (void)reserved;
28
29         if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
30                 return -1;
31         }
32
33         jclass envc = env->FindClass("org/sigrok/androidutils/Environment");
34         jmethodID getEnv =  env->GetStaticMethodID(envc, "getEnvironment",
35                                                    "()[Ljava/lang/String;");
36         jobjectArray envs =
37                 (jobjectArray)env->CallStaticObjectMethod(envc, getEnv);
38         jsize i, envn = env->GetArrayLength(envs);
39         for (i=0; i<envn; i+=2) {
40                 jstring key = (jstring)env->GetObjectArrayElement(envs, i);
41                 jstring value = (jstring)env->GetObjectArrayElement(envs, i+1);
42                 const char *utfkey = env->GetStringUTFChars(key, 0);
43                 const char *utfvalue = env->GetStringUTFChars(value, 0);
44                 setenv(utfkey, utfvalue, 1);
45                 env->ReleaseStringUTFChars(value, utfvalue);
46                 env->ReleaseStringUTFChars(key, utfkey);
47                 env->DeleteLocalRef(value);
48                 env->DeleteLocalRef(key);
49         }
50         env->DeleteLocalRef(envs);
51         env->DeleteLocalRef(envc);
52
53         return JNI_VERSION_1_6;
54 }
55
56 void JNI_OnUnload(JavaVM *vm, void *reserved)
57 {
58         (void)vm;
59         (void)reserved;
60 }