passgo/impl_android.go

99 lines
1.9 KiB
Go
Raw Normal View History

2019-09-10 11:31:57 -04:00
//+build android
2019-11-20 11:52:19 -05:00
//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
2019-09-10 11:31:57 -04:00
package passgo
2019-11-20 11:52:19 -05:00
/*
#import <jni.h>
#import "jni_android.h"
*/
import "C"
2019-09-10 11:31:57 -04:00
import (
"fmt"
2019-11-20 11:52:19 -05:00
"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
2019-09-10 11:31:57 -04:00
)
2019-11-20 11:52:19 -05:00
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
}
2019-09-10 11:31:57 -04:00
//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) {
2019-11-20 11:52:19 -05:00
connect()
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))
2019-11-20 13:53:40 -05:00
log.Printf("nativeDecrypt(): got %s", ret)
2019-11-20 11:52:19 -05:00
return ret, err
2019-09-10 11:31:57 -04:00
}
func (s *Store) nativeEncrypt(pw string) ([]byte, error) {
2019-11-20 11:52:19 -05:00
connect()
2019-11-20 13:53:40 -05:00
ret, err := pgp.Encrypt(s.Id, pw)
log.Printf("nativeEncrypt(): got %s", ret)
2019-11-20 11:52:19 -05:00
return []byte(ret), err
}
2019-09-10 11:31:57 -04:00
func nativeIdentities() ([]string, error) {
2019-11-20 11:52:19 -05:00
log.Printf("nativeIdentities()")
connect()
log.Printf("calling pgp.GetId()")
id, err := pgp.GetId()
log.Printf("pgp.GetId() returned")
return []string{id}, err
2019-09-10 11:31:57 -04:00
}