forked from kunaldawn/goandroid
/
goandroid.go
67 lines (61 loc) · 2.8 KB
/
goandroid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package goandroid
import (
"github.com/kunaldawn/goandroid/activity"
"github.com/kunaldawn/goandroid/adbutility"
"github.com/kunaldawn/goandroid/device"
"github.com/kunaldawn/goandroid/display"
"github.com/kunaldawn/goandroid/input"
"github.com/kunaldawn/goandroid/view"
)
// AndroidManager struct defines a android device manager with an associated
// adb endpoint and adb operation timeout. All devices returned by this android
// manader will be having this adb operation timeout.
type AndroidManager struct {
Endpoint adbutility.AdbEndpoint // Associated adb endpoint
Timeout int // Default adb operation timeout in seconds
}
// Android struct defines an android device. Android device has raw device
// communication interface via Device struct, device input interaction interface
// via Input struct, device ui view query interface via View struct, device display
// information interface via Display struct and device application activity
// interface via Activity struct. See their respective documentation for
// list of available mechanisms.
type Android struct {
Device device.Device // Raw adb device communication interface
Input input.InputManager // Device input interaction interface
View view.DeviceView // Device UI View query interface
Display display.Display // Device display insterface
Activity activity.Activity // Device application activity interface
}
// GetAndroidManager method returns a new AndroidManager instance based on specified
// adb executable path and adb operation timeout. Please note that adb executable must
// be present on the specified path. If adb is on system path then just pass "adb".
// Timeout specified is in seconds and all adb commands will timeout after specified
// seconds.
func GetNewAndroidManager(timeout int, adb string) AndroidManager {
return AndroidManager{Endpoint: adbutility.GetNewAdbEndpoint(adb), Timeout: timeout}
}
// GetNewAndroidDevice method returns a new Android device instance based on the
// specified serial number.
func (am AndroidManager) GetNewAndroidDevice(serial string) Android {
dev := device.NewDevice(serial, am.Timeout, am.Endpoint)
inp := input.NewInputManager(dev)
viw := view.NewDeviceView(dev)
disp := display.NewDisplay(dev)
act := activity.NewActivity(dev)
return Android{dev, inp, viw, disp, act}
}
// GetAttachedAndroidDevices method returns list of attached android devices
// to the system. It returns error if any error occured wile performing adb operation.
func (am AndroidManager) GetAttachedAndroidDevices() ([]Android, error) {
serials, err := am.Endpoint.GetAttachedDevices(am.Timeout)
if err != nil {
return []Android{}, err
}
devices := []Android{}
for index := range serials {
dev := am.GetNewAndroidDevice(serials[index])
devices = append(devices, dev)
}
return devices, nil
}