//+build android //go:generate mkdir -p classes //go:generate javac -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar -classpath openpgp-api.jar -d classes PgpConnect.java //go:generate jar cf PgpConnect.jar -C classes . //go:generate rm -rf classes package passgo /* #import #import "jni_android.h" */ import "C" import ( "fmt" "io/ioutil" "path" "log" "strings" "sync" "gioui.org/app" _ "gioui.org/app/permission/storage" ) var ( h *app.Handle waitch chan struct{} w *app.Window pgp PGP InitPgpOnce sync.Once ) func init() { waitch = make(chan struct{}) } //export installComplete func installComplete(env *C.JNIEnv, class C.jclass, p C.jobject) { log.Printf("InstallComplete()") pgp = PGP(C.NewGlobalRef(env, p)) 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 stopPgp() { waitch = make(chan struct{}) w = nil } func connect() { <-waitch } //Clip copies a string to the clipboard func Clip(x string) { fmt.Println("Clipboard not implemented for this platform") } 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) return ret, err } func (s *Store) nativeEncrypt(pw string) ([]byte, error) { connect() ret, err := Encrypt("", pw) 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() log.Printf("calling pgp.GetId()") id, err := pgp.GetId() log.Printf("pgp.GetId() returned") return []string{id}, err }