From 8611bfb5e017d4301722adca37574712894c3559 Mon Sep 17 00:00:00 2001 From: Greg Date: Wed, 20 Nov 2019 13:53:40 -0500 Subject: [PATCH] Cleanups in Android implementation. --- impl_android.go | 12 +++--------- jni_android.c | 21 +++++++++++---------- jni_android.go | 3 ++- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/impl_android.go b/impl_android.go index f1d1790..fb205e0 100644 --- a/impl_android.go +++ b/impl_android.go @@ -72,28 +72,22 @@ func Clip(x string) { func (s *Store) nativeDecrypt(name string) (string, error) { connect() - log.Printf("Decrypt(): connected") data, err := ioutil.ReadFile(path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, ""))) if err != nil { return "", fmt.Errorf("Error reading file: %s", err) } ret, err := pgp.Decrypt(string(data)) - log.Printf("Decrypt(): got %s", ret) + log.Printf("nativeDecrypt(): got %s", ret) return ret, err } func (s *Store) nativeEncrypt(pw string) ([]byte, error) { connect() - ret, err := Encrypt("", pw) + ret, err := pgp.Encrypt(s.Id, pw) + log.Printf("nativeEncrypt(): got %s", ret) return []byte(ret), err } -func Encrypt(id, data string) (string, error) { - connect() - log.Printf("Encrypt(): connected") - return pgp.Encrypt(id, data) -} - func nativeIdentities() ([]string, error) { log.Printf("nativeIdentities()") connect() diff --git a/jni_android.c b/jni_android.c index ecf7ee7..5521615 100644 --- a/jni_android.c +++ b/jni_android.c @@ -35,10 +35,6 @@ 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[] = { { @@ -59,7 +55,6 @@ InitPgpConnect(JNIEnv* env) { }; (*env)->RegisterNatives(env, cls, actMethods, sizeof(actMethods)/sizeof(actMethods[0])); - //return (*env)->NewGlobalRef(env, ret); } void @@ -74,10 +69,12 @@ GetId(JNIEnv* env, jobject p, int 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); + if (datalen > 0) { + (*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); + (*env)->CallVoidMethod(env, p, mid, data, chint); } void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) { @@ -92,12 +89,16 @@ void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) { 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); + if (idlen > 0) { + (*env)->SetByteArrayRegion(env, id, 0, idlen, cid); + } jbyteArray data = (*env)->NewByteArray(env, datalen); - (*env)->SetByteArrayRegion(env, data, 0, datalen, cdata); + if (datalen > 0) { + (*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); + (*env)->CallVoidMethod(env, p, mid, id, data, chint); } void CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID) { diff --git a/jni_android.go b/jni_android.go index 44f7b14..e626204 100644 --- a/jni_android.go +++ b/jni_android.go @@ -1,7 +1,7 @@ package passgo /* -#cgo LDFLAGS: -landroid +#cgo LDFLAGS: -landroid -llog #include #include "jni_android.h" @@ -84,6 +84,7 @@ func (p PGP) GetId() (string, error) { curint := chint chint++ chmux.Unlock() + RunInJVM(func(env *JNIEnv) { C.GetId(env, (C.jobject)(p), C.int(curint)) })