144 lines
4.7 KiB
C
144 lines
4.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);
|
|
}
|
|
|
|
void
|
|
AddFragment(JNIEnv* env, jobject act, jobject frag) {
|
|
jclass cls = (*env)->GetObjectClass(env, act);
|
|
jmethodID mid = (*env)->GetMethodID(env, cls, "getFragmentManager", "()Landroid/app/FragmentManager;");
|
|
jobject fm = (*env)->CallObjectMethod(env, act, mid);
|
|
cls = (*env)->GetObjectClass(env, fm);
|
|
mid = (*env)->GetMethodID(env, cls, "beginTransaction", "()Landroid/app/FragmentTransaction;");
|
|
jobject ft = (*env)->CallObjectMethod(env, fm, mid);
|
|
cls = (*env)->GetObjectClass(env, ft);
|
|
mid = (*env)->GetMethodID(env, cls, "add", "(Landroid/app/Fragment;)V");
|
|
(*env)->CallVoidMethod(env, ft, mid, frag);
|
|
mid = (*env)->GetMethodID(env, cls, "commitNow", "()V");
|
|
(*env)->CallVoidMethod(env, ft, mid);
|
|
|
|
cls = (*env)->GetObjectClass(env, frag);
|
|
static const JNINativeMethod fragMethods[] = {
|
|
{
|
|
.name = "activityResult",
|
|
.signature = "(II)V",
|
|
.fnPtr = activityResult
|
|
},
|
|
{
|
|
.name = "stringResult",
|
|
.signature = "(ILjava/lang/String;)V",
|
|
.fnPtr = stringResult
|
|
},
|
|
};
|
|
|
|
(*env)->RegisterNatives(env, cls, fragMethods, sizeof(fragMethods)/sizeof(fragMethods[0]));
|
|
}
|
|
|
|
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 = FindClass(env, "st/wow/git/pgp/PgpFragment");
|
|
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)Ljava/lang/String;");
|
|
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) {
|
|
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)Ljava/lang/String;");
|
|
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);
|
|
}
|
|
|