tap and swipe replay works

This commit is contained in:
2022-08-02 23:12:35 -07:00
parent cab458ce53
commit 3e348641d8
4 changed files with 84 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package adb
import (
"context"
"errors"
"log"
"regexp"
"strconv"
@@ -151,10 +152,15 @@ func (d Device) CaptureSequence(ctx context.Context) (t TapSequence, err error)
// this command will never finish without ctx expiring. As a result,
// it will always return error code 130 if successful
stdout, _, errCode, err := execute(ctx, []string{"shell", "getevent", "-tl"})
if errCode != 130 {
// TODO remove log output here
log.Printf("Expected error code 130, but got %d\n", errCode)
// TODO better error checking here
if errors.Is(err, ErrUnspecified) {
err = nil
}
if errCode != 130 && errCode != -1 {
// TODO remove log output here
log.Printf("Expected error code 130 or -1, but got %d\n", errCode)
}
if stdout == "" {
return TapSequence{}, ErrStdoutEmpty
}

View File

@@ -0,0 +1,35 @@
package main
import (
"context"
"fmt"
"github.com/taigrr/adb"
)
var command string
func init() {
// TODO allow for any input to be used as the command
command = "ls"
}
func main() {
ctx := context.TODO()
devs, err := adb.Devices(ctx)
if err != nil {
fmt.Printf("Error enumerating devices: %v\n", err)
return
}
for _, dev := range devs {
if !dev.IsAuthorized {
fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo)
continue
}
err := dev.GoHome(ctx)
if err != nil {
// handle error here
fmt.Printf("Error: %v\n", err)
}
}
}

View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"time"
"github.com/taigrr/adb"
)
@@ -15,7 +16,8 @@ func init() {
}
func main() {
ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
devs, err := adb.Devices(ctx)
if err != nil {
fmt.Printf("Error enumerating devices: %v\n", err)
@@ -26,11 +28,18 @@ func main() {
fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo)
continue
}
w, h, err := dev.GetScreenResolution(ctx)
//w, h, err := dev.GetScreenResolution(ctx)
//if err != nil {
// // handle error here
// fmt.Printf("Error: %v\n", err)
//}
fmt.Printf("Begin tapping on device %s now...\n", dev.SerialNo)
t, err := dev.CaptureSequence(ctx)
if err != nil {
// handle error here
fmt.Printf("Error: %v\n", err)
fmt.Printf("Error capturing sequence: %v\n", err)
return
}
fmt.Printf("%s screen resolution: %dx%d\n", dev.SerialNo, w, h)
fmt.Println("Sequence captured, replaying now...")
dev.ReplayTapSequence(context.TODO(), t)
}
}

View File

@@ -51,7 +51,13 @@ func parseScreenResolution(in string) (int, int, error) {
}
func (d Device) Tap(ctx context.Context, X, Y int) error {
return nil
cmd := []string{
"-s", string(d.SerialNo), "shell",
"input", "tap",
strconv.Itoa(X), strconv.Itoa(Y),
}
_, _, _, err := execute(ctx, cmd)
return err
}
// Simulates a long press
@@ -59,30 +65,44 @@ func (d Device) Tap(ctx context.Context, X, Y int) error {
// Under the hood, this calls swipe with the same start and end coordinates
// with a duration of 250ms
func (d Device) LongPress(ctx context.Context, X, Y int) error {
return nil
return d.Swipe(ctx, X, Y, X, Y, time.Millisecond*250)
}
func (d Device) Swipe(ctx context.Context, X1, Y1, X2, Y2 int, duration time.Duration) error {
return nil
cmd := []string{
"-s", string(d.SerialNo), "shell",
"input", "swipe",
strconv.Itoa(X1), strconv.Itoa(Y1),
strconv.Itoa(X2), strconv.Itoa(Y2),
strconv.Itoa(int(duration.Milliseconds())),
}
_, _, _, err := execute(ctx, cmd)
return err
}
// Equivalent to pressing the home button
//
// Calls `input keyevent KEYCODE_HOME` under the hood
func (d Device) GoHome(ctx context.Context) error {
return nil
cmd := []string{"-s", string(d.SerialNo), "shell", "input", "keyevent", "KEYCODE_HOME"}
_, _, _, err := execute(ctx, cmd)
return err
}
//Equivalent to pressing the back button
//
// Calls `input keyevent KEYCODE_BACK` under the hood
func (d Device) GoBack(ctx context.Context) error {
return nil
cmd := []string{"-s", string(d.SerialNo), "shell", "input", "keyevent", "KEYCODE_BACK"}
_, _, _, err := execute(ctx, cmd)
return err
}
// Equivalent to pushing the app switcher. You probably want to call this twice.
//
// Calls `input keyevent KEYCODE_APP_SWITCH` under the hood
func (d Device) SwitchApp(ctx context.Context) error {
return nil
cmd := []string{"-s", string(d.SerialNo), "shell", "input", "keyevent", "KEYCODE_APP_SWITCH"}
_, _, _, err := execute(ctx, cmd)
return err
}