android-go/examples/sensors/os_android.go
2019-10-18 14:40:35 -04:00

68 lines
1.5 KiB
Go

package main
import (
"fmt"
"log"
"os"
"gioui.org/app"
ndk "git.wow.st/gmp/android-go/android27"
)
var (
tag *ndk.Char
)
func mkHomeDir() {
msg := ndk.CharWithGoString("getting datadir")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
godir, _ := app.DataDir()
labchan <- fmt.Sprintf("DataDir is %s", godir)
msg = ndk.CharWithGoString("ok. calling mkdir")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
if _, err := os.Stat(godir); !os.IsNotExist(err) {
labchan <- "already exists"
return
}
if err := os.MkdirAll(godir, 0700); err != nil {
fmt.Sprintf("Error creating directory: %s", err)
labchan <- fmt.Sprintf("Error creating directory: %s", err)
} else {
labchan <- "created"
}
}
func apiLevel() {
msg := ndk.CharWithGoString("hello log world")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
apilevel := ndk.AndroidGetDeviceApiLevel()
message := fmt.Sprintf("API level is %d", apilevel)
labchan <- message
msg = ndk.CharWithGoString(message)
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
}
func getSensors() {
pkg := ndk.CharWithGoString("st.wow.git.jni")
sm := ndk.ASensorManagerGetInstanceForPackage(pkg)
pkg.Free()
sens := ndk.ASensorManagerGetDefaultSensor(sm, ndk.ASENSOR_TYPE_ACCELEROMETER)
stp := ndk.ASensorGetStringType(sens)
labchan <- "sensor" + stp.String()
}
func init() {
tag = ndk.CharWithGoString("gio")
log.Print("Android starting")
go apiLevel()
go mkHomeDir()
go getSensors()
}