Skip to content

Commit 415ec99

Browse files
committed
WIP to improve retry logic
1 parent f77a04f commit 415ec99

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

adb/adb.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ADB struct {
2323
var Client *ADB
2424

2525
// New returns a new ADB instance.
26-
func New(serial string) (*ADB, error) {
26+
func New() (*ADB, error) {
2727
adb := ADB{}
2828
err := adb.findExe()
2929
if err != nil {
@@ -35,33 +35,41 @@ func New(serial string) (*ADB, error) {
3535
log.Debug("Killing existing ADB server if running")
3636
adb.KillServer()
3737

38-
// Managing devices
39-
devices, err := adb.Devices()
38+
// Confirm that we can call "adb devices" without errors
39+
_, err = adb.Devices()
4040
if err != nil {
4141
return nil, err
4242
}
43+
return &adb, nil
44+
}
45+
46+
func (a *ADB) SetSerial(serial string) (string, error) {
47+
devices, err := a.Devices()
48+
if err != nil {
49+
return "", err
50+
}
4351

4452
serial = strings.TrimSpace(serial)
4553
if len(devices) == 0 {
46-
return nil, fmt.Errorf("no devices connected to adb")
54+
return "", fmt.Errorf("no devices detected over ADB")
4755
}
56+
4857
if serial != "" {
4958
// Check that the serial match one of the devices
5059
// Can be replace with the go package slices in 1.21
5160
if !saveSlice.ContainsNoCase(devices, serial) {
5261
// Serial is not an existing device
53-
return nil, fmt.Errorf("serial %s not found in the device list", serial)
62+
return "", fmt.Errorf("serial %s not found in the device list", serial)
5463
}
55-
adb.Serial = serial
64+
a.Serial = serial
5665
} else {
5766
// Problem if multiple devices
5867
if len(devices) > 1 {
59-
return nil, fmt.Errorf("multiple devices connected, please provide a serial number")
68+
return "", fmt.Errorf("multiple devices connected, please stop AndroidQF and provide a serial number")
6069
}
61-
adb.Serial = ""
70+
a.Serial = ""
6271
}
63-
64-
return &adb, nil
72+
return a.Serial, nil
6573
}
6674

6775
// List existing devices
@@ -78,6 +86,7 @@ func (a *ADB) Devices() ([]string, error) {
7886
dev := strings.Split(s, "\t")
7987
if len(dev) == 2 {
8088
devices = append(devices, strings.TrimSpace(dev[0]))
89+
log.Debug("Found new device: ", dev[0])
8190
}
8291
}
8392

main.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,24 @@ func main() {
8282
}
8383

8484
log.Debug("Starting androidqf")
85-
adb.Client, err = adb.New(serial)
85+
adb.Client, err = adb.New()
8686
if err != nil {
87-
log.Fatal("Impossible to initialize adb: ", err)
87+
log.Fatal("Impossible to initialize ADB: ", err)
8888
}
8989

9090
// Initialization
9191
for {
92-
_, err = adb.Client.GetState()
93-
if err == nil {
94-
break
92+
serial, err = adb.Client.SetSerial(serial)
93+
if err != nil {
94+
log.Error(fmt.Sprintf("Error trying to connect over ADB: %s", err))
95+
} else {
96+
_, err = adb.Client.GetState()
97+
if err == nil {
98+
break
99+
}
100+
log.Debug(err)
101+
log.Error("Unable to get device state. Please make sure it is connected and authorized. Trying again in 5 seconds...")
95102
}
96-
log.Debug(err)
97-
log.Error("Unable to get device state. Please make sure it is connected and authorized. Trying again in 5 seconds...")
98103
time.Sleep(5 * time.Second)
99104
}
100105

0 commit comments

Comments
 (0)