Created
September 11, 2014 16:57
-
-
Save Zagrebelin/aff1b87b1f1d524237b6 to your computer and use it in GitHub Desktop.
How to control Android device via ADB: emulate click and swipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import cmd | |
import os | |
def parseCoord(pos, limit): | |
""" | |
'40' => 40px | |
'10%' => 10% of limit | |
""" | |
if type(pos) is int: | |
return pos | |
elif type(pos) is str: | |
if pos[-1]=='%': | |
pos = int(pos[:-1])*limit/100 | |
else: | |
pos = int(pos) | |
return pos | |
else: | |
print(pos, type(pos)) | |
x` | |
class MyPhone(object): | |
max_x = 540 | |
max_y = 960 | |
def shell(self, cmd): | |
os.system("adb shell %s"%cmd) | |
def send_touch_event(info): | |
self.shell("sendevent /dev/input/event3 %s"%info) | |
def click(self, x, y): | |
x = parseCoord(x, max_x) | |
y = parseCoord(y, max_y) | |
self.send_touch_event("1 330 1") | |
self.send_touch_event("3 48 1") | |
self.send_touch_event("3 53 %d"%x) | |
self.send_touch_event("3 54 %d"%y) | |
self.send_touch_event("0 2 0") | |
self.send_touch_event("0 0 0") | |
self.send_touch_event("1 330 0") | |
self.send_touch_event("0 2 0") | |
self.send_touch_event("0 0000 0") | |
def swipe(self, x1, y1, x2, y2): | |
x1 = parseCoord(x1, max_x) | |
x2 = parseCoord(x2, max_x) | |
y1 = parseCoord(y1, max_y) | |
y2 = parseCoord(y2, max_y) | |
self.send_touch_event("1 330 1") | |
self.send_touch_event("3 48 1") | |
self.send_touch_event("3 53 %d"%x1) | |
self.send_touch_event("3 54 %d"%y1) | |
self.send_touch_event("0 2 0") | |
self.send_touch_event("0 0 0") | |
self.send_touch_event("3 53 %d"%x2) | |
self.send_touch_event("3 54 %d"%y2) | |
self.send_touch_event("0 2 0") | |
self.send_touch_event("0 0 0") | |
self.send_touch_event("1 330 0") | |
self.send_touch_event("0 2 0") | |
self.send_touch_event("0 0000 0") | |
class MyCmd(cmd.Cmd): | |
def __init__(self, phone): | |
self.phone = phone | |
def do_click(self, line): | |
"""click x y""" | |
x, y = line.split() | |
self.phone.click(x, y) | |
def do_menu(self, line): | |
self.phone.click(self.phone.max_x/2, self.phone.max_y*0.95) | |
def do_swipe(self, line): | |
""" | |
swipe rtl | |
swipe ltr | |
swipe x1 y2 x2 y2 | |
""" | |
line = line.strip() | |
if line=="rtl": | |
self.swipe("100%", '50%', '50%', '50%') | |
elif line=="ltr": | |
self.swipe("0%", '50%', '50%', '50%') | |
else: | |
coords = line.split() | |
if len(coords)==4: | |
self.swipe(*line.split()) | |
def do_back(self, *args): | |
self.phone.shell("input keyevent 4") | |
def do_exit(self, *args): | |
return True | |
if __name__ == "__main__": | |
MyCmd(MyPhone()).cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment