| 5 | from .controller import Controller |
| 6 | |
| 7 | class AndroidController(Controller): |
| 8 | def __init__(self, adb_path): |
| 9 | self.adb_path = adb_path |
| 10 | |
| 11 | def get_screenshot(self, save_path): |
| 12 | command = self.adb_path + " shell rm /sdcard/screenshot.png" |
| 13 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 14 | time.sleep(0.5) |
| 15 | command = self.adb_path + " shell screencap -p /sdcard/screenshot.png" |
| 16 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 17 | time.sleep(0.5) |
| 18 | command = self.adb_path + f" pull /sdcard/screenshot.png {shlex.quote(save_path)}" |
| 19 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 20 | |
| 21 | if not os.path.exists(save_path): |
| 22 | return False |
| 23 | else: |
| 24 | return True |
| 25 | |
| 26 | def tap(self, x, y): |
| 27 | command = self.adb_path + f" shell input tap {x} {y}" |
| 28 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 29 | |
| 30 | def type(self, text): |
| 31 | text = text.replace("\\n", "_").replace("\n", "_") |
| 32 | for char in text: |
| 33 | if char == ' ': |
| 34 | command = self.adb_path + " shell input text %s" |
| 35 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 36 | elif char == '_': |
| 37 | command = self.adb_path + " shell input keyevent 66" |
| 38 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 39 | elif 'a' <= char <= 'z' or 'A' <= char <= 'Z' or char.isdigit(): |
| 40 | command = self.adb_path + f" shell input text {char}" |
| 41 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 42 | elif char in '-.,!?@\'°/:;()': |
| 43 | command = self.adb_path + f" shell input text \"{char}\"" |
| 44 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 45 | else: |
| 46 | command = self.adb_path + f" shell am broadcast -a ADB_INPUT_TEXT --es msg \"{char}\"" |
| 47 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 48 | |
| 49 | def slide(self, x1, y1, x2, y2): |
| 50 | command = self.adb_path + f" shell input swipe {x1} {y1} {x2} {y2} 500" |
| 51 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 52 | |
| 53 | def back(self): |
| 54 | command = self.adb_path + " shell input keyevent 4" |
| 55 | subprocess.run(command, capture_output=True, text=True, shell=True) |
| 56 | |
| 57 | def home(self): |
| 58 | command = self.adb_path + " shell am start -a android.intent.action.MAIN -c android.intent.category.HOME" |
| 59 | subprocess.run(command, capture_output=True, text=True, shell=True) |