#include #include #include #include #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, "", "()V"); return (*env)->NewObject(env, cls, init); } void InitPgpConnect(JNIEnv* env) { jclass cls = FindClass(env, "st/wow/git/passgo/PgpConnect"); printf("class = %p", cls); //jmethodID init = (*env)->GetMethodID(env, cls, "", "(Landroid/app/Activity;)V"); //printf("init = %p", init); //jobject ret = (*env)->NewObject(env, cls, init, act); //printf("obj = %p", ret); static const JNINativeMethod actMethods[] = { { .name = "activityResult", .signature = "(II)V", .fnPtr = activityResult }, { .name = "stringResult", .signature = "(ILjava/lang/String;)V", .fnPtr = stringResult }, { .name = "installComplete", .signature = "(Lst/wow/git/passgo/PgpConnect;)V", .fnPtr = installComplete }, }; (*env)->RegisterNatives(env, cls, actMethods, sizeof(actMethods)/sizeof(actMethods[0])); //return (*env)->NewGlobalRef(env, ret); } void GetId(JNIEnv* env, jobject p, int chint) { jclass cls = (*env)->GetObjectClass(env, p); printf("GetId(): cls = %p", cls); jmethodID mid = (*env)->GetMethodID(env, cls, "GetId", "(I)V"); printf("GetId(): mid = %p", mid); (*env)->CallObjectMethod(env, p, mid, chint); } void 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"); (*env)->CallObjectMethod(env, p, mid, data, chint); } 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); } } void 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"); (*env)->CallObjectMethod(env, p, mid, id, data, chint); } 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); } jobject NewGlobalRef(JNIEnv *env, jobject o) { return (*env)->NewGlobalRef(env, o); }