From 4fc504b89af05d3953ae9fc5e2d3571e7e719fb4 Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 21 Nov 2019 18:18:47 -0500 Subject: [PATCH] Restructure for automatic Fragment registration by Gio. --- PgpConnect.java | 33 ++++++--------------- cmd/passgo-gui/main.go | 8 ++--- impl_android.go | 32 ++++++++++---------- jni_android.c | 66 +----------------------------------------- jni_android.go | 46 ++--------------------------- jni_android.h | 7 ----- 6 files changed, 31 insertions(+), 161 deletions(-) diff --git a/PgpConnect.java b/PgpConnect.java index 5253833..b7aad63 100644 --- a/PgpConnect.java +++ b/PgpConnect.java @@ -31,43 +31,30 @@ import android.Manifest; import android.content.pm.PackageManager; public class PgpConnect extends Fragment { - Activity act; Context ctx; Handler handler; - OpenPgpServiceConnection mServiceConnection; ClipboardManager cb; + OpenPgpServiceConnection mServiceConnection; final int PERMISSIONS_REQUEST = 1; - public PgpConnect(Activity act) { - Log.d("gio", "PgpConnect(Activity)"); - act = act; - ctx = act.getApplicationContext(); - this.handler = new Handler(ctx.getMainLooper()); - - final FragmentManager fm = act.getFragmentManager(); - final Fragment frag = this; - handler.post(new Runnable() { - public void run() { - Log.d("gio", "PgpConnect(): adding fragment"); - FragmentTransaction ft = fm.beginTransaction(); - ft.add(frag, "PgpConnect"); - ft.commitNow(); - - if (ctx.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST); - } - } - }); + public PgpConnect() { + Log.d("gio", "PgpConnect()"); } @Override public void onAttach(Context ctx) { super.onAttach(ctx); Log.d("gio", "onAttach()"); + if (ctx instanceof Activity) { + Log.d("gio", "It's an Activity!"); + } this.ctx = ctx; this.handler = new Handler(ctx.getMainLooper()); mServiceConnection = new OpenPgpServiceConnection(ctx, "org.sufficientlysecure.keychain"); mServiceConnection.bindToService(); cb = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE); + if (ctx.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST); + } installComplete(this); } @@ -84,7 +71,6 @@ public class PgpConnect extends Fragment { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("gio", "onActivityResult(" + requestCode + "): " + resultCode); super.onActivityResult(requestCode, resultCode, data); - //activityResult(requestCode, resultCode); if (resultCode != Activity.RESULT_OK) { Log.d("gio", "onActivityResult: not OK"); stringResult(requestCode, null); @@ -291,6 +277,5 @@ public class PgpConnect extends Fragment { } static private native void installComplete(PgpConnect p); - static private native void activityResult(int requestCode, int resultCode); static private native void stringResult(int requestCode, String result); } diff --git a/cmd/passgo-gui/main.go b/cmd/passgo-gui/main.go index c1346a1..a23b6e4 100644 --- a/cmd/passgo-gui/main.go +++ b/cmd/passgo-gui/main.go @@ -827,11 +827,11 @@ func eventLoop() { return case system.StageEvent: if e.Stage == system.StageRunning { - initPgp(w) + go func() { + initPgp(w) + updateIdBtns() + }() } - go func() { - updateIdBtns() - }() case system.FrameEvent: gtx.Reset(e.Config, e.Size) diff --git a/impl_android.go b/impl_android.go index 8fe0e75..ae65233 100644 --- a/impl_android.go +++ b/impl_android.go @@ -27,38 +27,36 @@ import ( var ( h *app.Handle waitch chan struct{} - w *app.Window + //w *app.Window pgp PGP - InitPgpOnce sync.Once + installCompleteOnce sync.Once ) func init() { waitch = make(chan struct{}) } -//export installComplete -func installComplete(env *C.JNIEnv, class C.jclass, p C.jobject) { +//export Java_st_wow_git_passgo_PgpConnect_installComplete +func Java_st_wow_git_passgo_PgpConnect_installComplete(env *C.JNIEnv, class C.jclass, p C.jobject) { log.Printf("InstallComplete()") pgp = PGP(C.NewGlobalRef(env, p)) - close(waitch) + h = app.PlatformHandle() + SetJVM(h.JVM) + installCompleteOnce.Do(func() { + close(waitch) + }) } -func InitPgp(x *app.Window) { - InitPgpOnce.Do(func() { - log.Printf("InitPgp()") - w = x - h = app.PlatformHandle() - SetJVM(h.JVM, h.Context) - RunInJVM(func(env *JNIEnv) { - C.InitPgpConnect(env) - }) - w.RegisterDelegate("st/wow/git/passgo/PgpConnect") - }) +func InitPgp(w *app.Window) { + log.Printf("InitPgp()") + err := w.RegisterFragment("st/wow/git/passgo/PgpConnect") + if err != nil { + log.Printf("Error! %s", err) + } } func stopPgp() { waitch = make(chan struct{}) - w = nil } func connect() { diff --git a/jni_android.c b/jni_android.c index f97632c..ecf7865 100644 --- a/jni_android.c +++ b/jni_android.c @@ -2,61 +2,9 @@ #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); - - 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); @@ -77,7 +25,7 @@ Decrypt(JNIEnv* env, jobject p, char* cdata, int datalen, int chint) { (*env)->CallVoidMethod(env, p, mid, data, chint); } -void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) { +void Java_st_wow_git_passgo_PgpConnect_stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) { if (response == 0) { goStringResult(env, cls, requestCode, 0); } else { @@ -116,18 +64,6 @@ 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); } diff --git a/jni_android.go b/jni_android.go index efb85b6..722fd33 100644 --- a/jni_android.go +++ b/jni_android.go @@ -1,7 +1,7 @@ package passgo /* -#cgo LDFLAGS: -landroid -llog +#cgo LDFLAGS: -landroid #include #include "jni_android.h" @@ -20,54 +20,17 @@ import ( var theJVM *C.JavaVM type JNIEnv = C.JNIEnv -func SetJVM(jvm, context uintptr) { +func SetJVM(jvm uintptr) { log.Print("set theJVM") theJVM = (*C.JavaVM)(unsafe.Pointer(jvm)) - RunInJVM(func(env *C.JNIEnv) { - C.SetLoader(env, (C.jobject)(context)) - }) -} - -func FindClass(env *C.JNIEnv, name string) C.jclass { - cname := C.CString(name) - defer C.free(unsafe.Pointer(cname)) - return C.FindClass((*C.JNIEnv)(env), cname) } func JniCallVoidMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) { C.CallVoidMethod(env, obj, methodID) } -func JniCallVoidMethod1(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID, arg uintptr) { - C.CallVoidMethod1(env, obj, methodID, C.jobject(unsafe.Pointer(arg))) -} - -func JniCallIntMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) int { - return (int)(C.CallIntMethod(env, obj, methodID)) -} - -func JniGetMethodID(env *C.JNIEnv, cls C.jclass, name, sig string) C.jmethodID { - cname := C.CString(name) - defer C.free(unsafe.Pointer(cname)) - csig := C.CString(sig) - defer C.free(unsafe.Pointer(csig)) - return C.GetMethodID(env, cls, cname, csig) -} - -func CreateObject(env *C.JNIEnv, cls C.jclass) C.jobject { - return C.CreateObject(env, (C.jclass)(cls)) -} - type PGP C.jobject -func InitPgpConnect(env uintptr) { - C.InitPgpConnect((*C.JNIEnv)(unsafe.Pointer(env))) -} - -//func NewPgpConnect(env uintptr, act uintptr) PGP { -// return (PGP)(C.NewPgpConnect((*C.JNIEnv)(unsafe.Pointer(env)), (C.jobject)(unsafe.Pointer(act)))) -//} - var ( fragChans map[int]chan string chmux sync.Mutex @@ -180,11 +143,6 @@ func RunInJVM(f func(env *C.JNIEnv)) { f(env) } -//export activityResult -func activityResult(env *C.JNIEnv, class C.jclass, request, result C.jint) { - log.Printf("activityResult (%d): %d", request, result) -} - //export goStringResult func goStringResult(env *C.JNIEnv, class C.jclass, request C.jint, str *C.char) { fragChans[int(request)] <- C.GoString(str) diff --git a/jni_android.h b/jni_android.h index e9f7205..c89eb22 100644 --- a/jni_android.h +++ b/jni_android.h @@ -1,18 +1,11 @@ #include -void SetLoader(JNIEnv* env, jobject context); -jclass FindClass(JNIEnv* env, char* name); -jobject CreateObject(JNIEnv* env, jclass cls); -void InitPgpConnect(JNIEnv* env); void GetId(JNIEnv* env, jobject p, int chint); void Decrypt(JNIEnv* env, jobject p, char* cdata, int datalen, int chint); void Encrypt(JNIEnv* env, jobject p, char* cid, int idlen, char* cdata, int datalen, int chint); void Clip(JNIEnv* env, jobject p, char* cdata, int datalen); void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response); void CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID); -void CallVoidMethod1(JNIEnv *env, jobject obj, jmethodID methodID, jobject arg); -jint CallIntMethod(JNIEnv *env, jobject obj, jmethodID methodID); -jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig); jint GetEnv(JavaVM *vm, JNIEnv **env, jint version); jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args); jint DetachCurrentThread(JavaVM *vm);