struct.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package adb
  2. import (
  3. "log"
  4. "strings"
  5. "notabug.org/Umnik/GoAndroidSDK/v2/components/executor"
  6. "notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
  7. )
  8. // Adb represents adb util
  9. type Adb struct {
  10. binPath string
  11. }
  12. // Device represents Android device
  13. type Device struct {
  14. serial string
  15. deviceType string
  16. usb string
  17. product string
  18. model string
  19. device string
  20. transportId string
  21. }
  22. func (d Device) Serial() string {
  23. return d.serial
  24. }
  25. func (adb Adb) BinPath() string {
  26. return adb.binPath
  27. }
  28. func (adb Adb) Version() ([]byte, error) {
  29. return executor.RunSync(adb, "version")
  30. }
  31. func (adb Adb) KillServer() ([]byte, error) {
  32. return executor.RunSync(adb, "kill-server")
  33. }
  34. func (adb Adb) ConnectedDevices() (devices []Device) {
  35. data, err := executor.RunSync(adb, "devices", "-l")
  36. if err != nil {
  37. log.Println(err)
  38. return nil
  39. }
  40. for _, rawLineDevice := range deviceLines(data) {
  41. var deviceDescription [7]string
  42. for _, part := range strings.Split(rawLineDevice, " ") {
  43. part = strings.TrimSpace(part)
  44. if len(part) == 0 {
  45. continue
  46. }
  47. switch {
  48. default:
  49. deviceDescription[0] = part
  50. case strings.HasPrefix(part, "device") || (strings.HasPrefix(part, "emulator") && !strings.HasPrefix(part, "emulator-")):
  51. deviceDescription[1] = part
  52. case strings.HasPrefix(part, "usb:"):
  53. deviceDescription[2] = misc.TwoSpotValue(part)
  54. case strings.HasPrefix(part, "product:"):
  55. deviceDescription[3] = misc.TwoSpotValue(part)
  56. case strings.HasPrefix(part, "model:"):
  57. deviceDescription[4] = misc.TwoSpotValue(part)
  58. case strings.HasPrefix(part, "device:"):
  59. deviceDescription[5] = misc.TwoSpotValue(part)
  60. case strings.HasPrefix(part, "transport_id:"):
  61. deviceDescription[6] = misc.TwoSpotValue(part)
  62. }
  63. }
  64. devices = append(devices, NewDevice(deviceDescription[0],
  65. deviceDescription[1],
  66. deviceDescription[2],
  67. deviceDescription[3],
  68. deviceDescription[4],
  69. deviceDescription[5],
  70. deviceDescription[6]))
  71. }
  72. return
  73. }
  74. func deviceLines(data []byte) (result []string) {
  75. for _, line := range strings.Split(string(data), "\n") {
  76. line = strings.TrimSpace(line)
  77. if len(line) == 0 || strings.HasPrefix(line, "List of") {
  78. continue
  79. }
  80. result = append(result, line)
  81. }
  82. return
  83. }