passgo/impl_android.go

94 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
2019-11-20 11:52:19 -05:00
pgp PGP
installCompleteOnce 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 Java_st_wow_git_passgo_PgpConnect_installComplete
func Java_st_wow_git_passgo_PgpConnect_installComplete(env *C.JNIEnv, class C.jclass, p C.jobject) {
2019-11-20 11:52:19 -05:00
log.Printf("InstallComplete()")
pgp = PGP(C.NewGlobalRef(env, p))
h = app.PlatformHandle()
SetJVM(h.JVM)
installCompleteOnce.Do(func() {
close(waitch)
})
2019-11-20 11:52:19 -05:00
}
func InitPgp(w *app.Window) {
log.Printf("InitPgp()")
w.RegisterFragment("st/wow/git/passgo/PgpConnect")
2019-11-20 11:52:19 -05:00
}
func stopPgp() {
waitch = make(chan struct{})
}
func connect() {
<-waitch
}
2019-09-10 11:31:57 -04:00
//Clip copies a string to the clipboard
func Clip(x string) {
2019-11-20 14:56:11 -05:00
pgp.Clip(x)
2019-09-10 11:31:57 -04:00
}
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))
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)
2019-11-20 11:52:19 -05:00
return []byte(ret), err
}
2019-09-10 11:31:57 -04:00
func nativeIdentities() ([]string, error) {
//NOT IMPLEMENTED ON ANDROID
return []string{}, nil
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
}