android-go/examples/pgp/jni_android.c

117 lines
3.7 KiB
C

#include <stdlib.h>
#include <jni.h>
#include <android/log.h>
#include "jni_android.h"
#include "_cgo_export.h"
static jobject gClassLoader;
static jmethodID gFindClassMethod;
void
SetLoader(JNIEnv* env, jobject context) {
jclass cclass = (*env)->GetObjectClass(env, context);
jmethodID gcl_id = (*env)->GetMethodID(env, cclass, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject loader = (*env)->CallObjectMethod(env, context, gcl_id);
gClassLoader = (*env)->NewGlobalRef(env, loader);
jclass lclass = (*env)->GetObjectClass(env, loader);
gFindClassMethod = (*env)->GetMethodID(env, lclass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
}
jclass
FindClass(JNIEnv* env, char* name) {
jstring strClassName = (*env)->NewStringUTF(env, name);
return (*env)->CallObjectMethod(env, gClassLoader, gFindClassMethod, strClassName);
}
jobject
CreateObject(JNIEnv* env, jclass cls) {
jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "()V");
return (*env)->NewObject(env, cls, init);
}
jobject
NewPgpConnect(JNIEnv* env, jobject act) {
jclass cls = FindClass(env, "st/wow/git/pgp/PgpConnect");
jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "(Landroid/app/Activity;)V");
jobject ret = (*env)->NewObject(env, cls, init, act);
//cls = (*env)->GetObjectClass(env, ret);
static const JNINativeMethod actMethods[] = {
{
.name = "activityResult",
.signature = "(II)V",
.fnPtr = activityResult
},
{
.name = "stringResult",
.signature = "(ILjava/lang/String;)V",
.fnPtr = stringResult
},
};
(*env)->RegisterNatives(env, cls, actMethods, sizeof(actMethods)/sizeof(actMethods[0]));
return (*env)->NewGlobalRef(env, ret);
}
const char*
Decrypt(JNIEnv* env, jobject p, char* cdata, int datalen, int chint) {
jbyteArray data = (*env)->NewByteArray(env, datalen);
(*env)->SetByteArrayRegion(env, data, 0, datalen, cdata);
jclass cls = (*env)->GetObjectClass(env, p);
jmethodID mid = (*env)->GetMethodID(env, cls, "Decrypt", "([BI)V");
jstring ret = (*env)->CallObjectMethod(env, p, mid, data, chint);
return (*env)->GetStringUTFChars(env, ret, NULL);
}
void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) {
if (response == 0) {
goStringResult(env, cls, requestCode, 0);
} else {
char* str = (*env)->GetStringUTFChars(env, response, NULL);
goStringResult(env, cls, requestCode, str);
}
}
const char*
Encrypt(JNIEnv* env, jobject p, char* cid, int idlen, char* cdata, int datalen, int chint) {
jbyteArray id = (*env)->NewByteArray(env, idlen);
(*env)->SetByteArrayRegion(env, id, 0, idlen, cid);
jbyteArray data = (*env)->NewByteArray(env, datalen);
(*env)->SetByteArrayRegion(env, data, 0, datalen, cdata);
jclass cls = (*env)->GetObjectClass(env, p);
jmethodID mid = (*env)->GetMethodID(env, cls, "Encrypt", "([B[BI)V");
jstring ret = (*env)->CallObjectMethod(env, p, mid, id, data, chint);
return (*env)->GetStringUTFChars(env, ret, NULL);
}
void CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
(*env)->CallVoidMethod(env, obj, methodID);
}
void CallVoidMethod1(JNIEnv *env, jobject obj, jmethodID methodID, jobject arg) {
(*env)->CallVoidMethod(env, obj, methodID, arg);
}
jint CallIntMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
return (*env)->CallIntMethod(env, obj, methodID);
}
jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
return (*env)->GetMethodID(env, clazz, name, sig);
}
jint GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
return (*vm)->GetEnv(vm, (void **)env, version);
}
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args) {
return (*vm)->AttachCurrentThread(vm, p_env, thr_args);
}
jint DetachCurrentThread(JavaVM *vm) {
return (*vm)->DetachCurrentThread(vm);
}