Restructure for automatic Fragment registration by Gio.

This commit is contained in:
Greg 2019-11-21 18:18:47 -05:00
parent 23df75fd15
commit 4fc504b89a
6 changed files with 31 additions and 161 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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() {

View File

@ -2,61 +2,9 @@
#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);
@ -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);
}

View File

@ -1,7 +1,7 @@
package passgo
/*
#cgo LDFLAGS: -landroid -llog
#cgo LDFLAGS: -landroid
#include <stdlib.h>
#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)

View File

@ -1,18 +1,11 @@
#include <jni.h>
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);