android-go/examples/sensors/android.go

72 lines
1.5 KiB
Go
Raw Normal View History

2019-09-17 10:42:01 -04:00
//+build android
package main
import "C"
import (
"fmt"
"log"
"os"
2019-10-03 11:32:15 -04:00
"gioui.org/app"
2019-10-07 10:41:51 -04:00
ndk "git.wow.st/gmp/android-go/android27"
2019-09-17 10:42:01 -04:00
)
var (
tag *ndk.Char
)
func mkHomeDir() {
msg := ndk.CharWithGoString("getting datadir")
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
msg.Free()
2019-10-07 10:41:51 -04:00
godir, _ := app.DataDir()
2019-09-17 10:42:01 -04:00
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")
2019-10-03 11:32:15 -04:00
go apiLevel()
go mkHomeDir()
go getSensors()
2019-09-17 10:42:01 -04:00
}