Add get screen resolution code

This commit is contained in:
2022-08-02 21:29:10 -07:00
parent 0bf89f9a2d
commit cab458ce53
4 changed files with 95 additions and 2 deletions

View File

@@ -2,6 +2,8 @@ package adb
import (
"context"
"regexp"
"strconv"
"time"
"github.com/google/shlex"
@@ -26,8 +28,26 @@ func (d Device) Shell(ctx context.Context, command string) (stdout string, stder
// adb shell wm size
// Physical size: 1440x3120
func (d Device) GetScreenResolution(ctx context.Context) (width int, lenght int, err error) {
return 0, 0, nil
func (d Device) GetScreenResolution(ctx context.Context) (width int, height int, err error) {
cmd := []string{"-s", string(d.SerialNo), "shell", "wm", "size"}
stdout, _, _, err := execute(ctx, cmd)
if err != nil {
return -1, -1, err
}
return parseScreenResolution(stdout)
}
// Parses input, example:
// Physical size: 1440x3040
func parseScreenResolution(in string) (int, int, error) {
r := regexp.MustCompile(`Physical size: ([0-9]+)x([0-9]+)`)
resStr := r.FindStringSubmatch(in)
if len(resStr) != 3 {
return -1, -1, ErrResolutionParseFail
}
w, _ := strconv.Atoi(resStr[1])
h, _ := strconv.Atoi(resStr[2])
return w, h, nil
}
func (d Device) Tap(ctx context.Context, X, Y int) error {