fix: adb wm size may contain both Physical and Override size

This commit is contained in:
lilong.129
2023-06-01 21:47:12 +08:00
parent ac2ccc2c79
commit 11b96d0728

View File

@@ -57,17 +57,29 @@ func (ad *adbDriver) BatteryInfo() (batteryInfo BatteryInfo, err error) {
func (ad *adbDriver) WindowSize() (size Size, err error) {
// adb shell wm size
resp, err := ad.adbClient.RunShellCommand("wm", "size")
output, err := ad.adbClient.RunShellCommand("wm", "size")
if err != nil {
return size, errors.Wrap(err, "get window size failed with adb")
}
// output may contain both Physical and Override size
// Physical size: 1080x2340
s := strings.Trim(strings.Split(resp, ": ")[1], "\n")
ss := strings.Split(s, "x")
width, _ := strconv.Atoi(ss[0])
height, _ := strconv.Atoi(ss[1])
size = Size{Width: width, Height: height}
// Override size: 1080x2220
var resolution string
sizeList := strings.Split(output, "\n")
log.Info().Msgf("window size: %v", sizeList)
for _, size := range sizeList {
if strings.Contains(size, "Physical") {
resolution = strings.Split(size, ": ")[1]
// 1080x2340
ss := strings.Split(resolution, "x")
width, _ := strconv.Atoi(ss[0])
height, _ := strconv.Atoi(ss[1])
return Size{Width: width, Height: height}, nil
}
}
err = errors.New("physical window size not found by adb")
return
}