2015-04-12 20:34:20 +00:00
|
|
|
// An app demonstrating most of the library's features.
|
2015-04-11 21:45:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-12 20:34:20 +00:00
|
|
|
"flag"
|
2015-04-11 21:45:29 +00:00
|
|
|
"fmt"
|
2015-04-12 20:34:20 +00:00
|
|
|
"io/ioutil"
|
2015-04-11 21:45:29 +00:00
|
|
|
"log"
|
2015-05-17 19:33:41 +00:00
|
|
|
"time"
|
2015-04-11 21:45:29 +00:00
|
|
|
|
|
|
|
adb "github.com/zach-klippenstein/goadb"
|
2015-05-17 19:33:41 +00:00
|
|
|
"github.com/zach-klippenstein/goadb/util"
|
2015-04-11 21:45:29 +00:00
|
|
|
)
|
|
|
|
|
2015-05-03 13:11:09 +00:00
|
|
|
var port = flag.Int("p", adb.AdbPort, "")
|
2015-04-12 20:34:20 +00:00
|
|
|
|
2015-04-11 21:45:29 +00:00
|
|
|
func main() {
|
2015-04-12 20:34:20 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
client := adb.NewHostClient(adb.ClientConfig{})
|
2015-05-03 13:11:09 +00:00
|
|
|
|
2015-04-11 21:45:29 +00:00
|
|
|
fmt.Println("Starting server…")
|
2015-07-11 21:32:04 +00:00
|
|
|
adb.StartServer()
|
2015-04-11 21:45:29 +00:00
|
|
|
|
|
|
|
serverVersion, err := client.GetServerVersion()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Server version:", serverVersion)
|
|
|
|
|
|
|
|
devices, err := client.ListDevices()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Devices:")
|
|
|
|
for _, device := range devices {
|
|
|
|
fmt.Printf("\t%+v\n", *device)
|
|
|
|
}
|
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
PrintDeviceInfoAndError(adb.AnyDevice())
|
|
|
|
PrintDeviceInfoAndError(adb.AnyLocalDevice())
|
|
|
|
PrintDeviceInfoAndError(adb.AnyUsbDevice())
|
2015-04-12 20:34:20 +00:00
|
|
|
|
|
|
|
serials, err := client.ListDeviceSerials()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, serial := range serials {
|
2015-07-11 21:32:04 +00:00
|
|
|
PrintDeviceInfoAndError(adb.DeviceWithSerial(serial))
|
2015-04-12 20:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 19:33:41 +00:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Watching for device state changes.")
|
2015-09-06 05:44:36 +00:00
|
|
|
watcher := adb.NewDeviceWatcher(adb.ClientConfig{})
|
2015-05-17 19:33:41 +00:00
|
|
|
for event := range watcher.C() {
|
|
|
|
fmt.Printf("\t[%s]%+v\n", time.Now(), event)
|
|
|
|
}
|
|
|
|
if watcher.Err() != nil {
|
|
|
|
printErr(watcher.Err())
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
//fmt.Println("Killing server…")
|
|
|
|
//client.KillServer()
|
|
|
|
}
|
|
|
|
|
2015-05-17 19:33:41 +00:00
|
|
|
func printErr(err error) {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *util.Err:
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
if err.Cause != nil {
|
|
|
|
fmt.Print("caused by ")
|
|
|
|
printErr(err.Cause)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
func PrintDeviceInfoAndError(descriptor adb.DeviceDescriptor) {
|
|
|
|
device := adb.NewDeviceClient(adb.ClientConfig{}, descriptor)
|
2015-04-12 20:34:20 +00:00
|
|
|
if err := PrintDeviceInfo(device); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintDeviceInfo(device *adb.DeviceClient) error {
|
|
|
|
serialNo, err := device.GetSerial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
devPath, err := device.GetDevicePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state, err := device.GetState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(device)
|
|
|
|
fmt.Printf("\tserial no: %s\n", serialNo)
|
|
|
|
fmt.Printf("\tdevPath: %s\n", devPath)
|
|
|
|
fmt.Printf("\tstate: %s\n", state)
|
|
|
|
|
|
|
|
cmdOutput, err := device.RunCommand("pwd")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("\terror running command:", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("\tcmd output: %s\n", cmdOutput)
|
|
|
|
|
|
|
|
stat, err := device.Stat("/sdcard")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("\terror stating /sdcard:", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("\tstat \"/sdcard\": %+v\n", stat)
|
|
|
|
|
|
|
|
fmt.Println("\tfiles in \"/\":")
|
|
|
|
entries, err := device.ListDirEntries("/")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("\terror listing files:", err)
|
|
|
|
} else {
|
|
|
|
for entries.Next() {
|
|
|
|
fmt.Printf("\t%+v\n", *entries.Entry())
|
|
|
|
}
|
|
|
|
if entries.Err() != nil {
|
|
|
|
fmt.Println("\terror listing files:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 13:11:09 +00:00
|
|
|
fmt.Println("\tnon-existent file:")
|
|
|
|
stat, err = device.Stat("/supercalifragilisticexpialidocious")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("\terror:", err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("\tstat: %+v\n", stat)
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
fmt.Print("\tload avg: ")
|
|
|
|
loadavgReader, err := device.OpenRead("/proc/loadavg")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("\terror opening file:", err)
|
|
|
|
} else {
|
|
|
|
loadAvg, err := ioutil.ReadAll(loadavgReader)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("\terror reading file:", err)
|
|
|
|
} else {
|
|
|
|
fmt.Println(string(loadAvg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|