passgo/jni_android.c

136 lines
3.9 KiB
C
Raw Normal View History

2019-11-20 11:52:19 -05:00
#include <stdlib.h>
#include <stdio.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);
}
void
InitPgpConnect(JNIEnv* env) {
jclass cls = FindClass(env, "st/wow/git/passgo/PgpConnect");
printf("class = %p", cls);
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]));
}
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);
2019-11-20 13:53:40 -05:00
if (datalen > 0) {
(*env)->SetByteArrayRegion(env, data, 0, datalen, cdata);
}
2019-11-20 11:52:19 -05:00
jclass cls = (*env)->GetObjectClass(env, p);
jmethodID mid = (*env)->GetMethodID(env, cls, "Decrypt", "([BI)V");
2019-11-20 13:53:40 -05:00
(*env)->CallVoidMethod(env, p, mid, data, chint);
2019-11-20 11:52:19 -05:00
}
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);
2019-11-20 13:53:40 -05:00
if (idlen > 0) {
(*env)->SetByteArrayRegion(env, id, 0, idlen, cid);
}
2019-11-20 11:52:19 -05:00
jbyteArray data = (*env)->NewByteArray(env, datalen);
2019-11-20 13:53:40 -05:00
if (datalen > 0) {
(*env)->SetByteArrayRegion(env, data, 0, datalen, cdata);
}
2019-11-20 11:52:19 -05:00
jclass cls = (*env)->GetObjectClass(env, p);
jmethodID mid = (*env)->GetMethodID(env, cls, "Encrypt", "([B[BI)V");
2019-11-20 13:53:40 -05:00
(*env)->CallVoidMethod(env, p, mid, id, data, chint);
2019-11-20 11:52:19 -05:00
}
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);
}