android-go/examples/pgp/os_android.go
2020-06-11 10:42:43 -04:00

84 lines
1.6 KiB
Go

//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 main
import (
"fmt"
"log"
"time"
"gioui.org/app"
)
var (
h *app.Handle
connected bool
ready bool
wch chan struct{}
w *app.Window
pgp PGP
)
func init() {
wch = make(chan struct{})
}
func initPgp(x *app.Window) {
w = x
close(wch)
}
func connect(w *app.Window) bool {
<-wch
if !connected {
log.Print("PlatformHandle()")
h = app.PlatformHandle()
log.Print("SetJVM()")
SetJVM(h.JVM, h.Context)
log.Print("SetJVM() returned")
log.Print("calling window.Run()()")
w.Run(func(env app.JNIEnv, act app.Activity) {
log.Print("in window.Run() -- NewPgpConnect()")
pgp = NewPgpConnect(env.JNIEnv, act.Activity)
})
connected = true
}
return true
}
func checkPermission(w *app.Window) bool {
if !connect(w) {
return false
}
//return pgp.CheckPermission(env)
return false
}
func callJni(w *app.Window) {
if !connect(w) {
return
}
labchan <- fmt.Sprintf("JVM = %d", h.JVM)
labchan <- fmt.Sprintf("Context = %d", h.Context)
time.Sleep(time.Second / 5)
val, err := pgp.Encrypt("greg_pomerantz@yahoo.com", "hi there")
if err != nil {
fmt.Printf("Encryption error: %s", err)
labchan <- err.Error()
return
}
log.Print("val = ", val)
val2, err := pgp.Decrypt(val)
if err != nil {
fmt.Printf("Decryption error: %s", err)
labchan <- err.Error()
return
}
labchan <- "result = " + val2
}