-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
127 lines (101 loc) · 3.9 KB
/
Copy pathcontroller.py
File metadata and controls
127 lines (101 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import re
import subprocess
import time
from lxml import etree
import parser as pr
DESC_ALIASES = {
"Printing History": ("Printing History", "History"),
"brand_logo": ("brand_logo", "knife"),
}
def _candidate_descs(desc):
return DESC_ALIASES.get(desc, (desc,))
def go_to_printing_history():
os.system("adb shell input keyevent KEYCODE_BACK")
tap_by_desc("Me")
tap_by_desc("Printing History")
def get_devices():
go_to_device_page("Savage")
tap_by_desc("brand_logo")
return list(pr.parse_screen(long_clickable_only=False).keys())[:-1]
def go_to_device_page(machine):
os.system("adb shell input keyevent KEYCODE_BACK")
tap_by_desc("Devices")
if not find_by_desc("brand_logo"):
os.system("adb shell input keyevent KEYCODE_BACK")
tap_by_desc("Devices")
tap_by_desc("brand_logo")
list_screen = pr.parse_screen(long_clickable_only=False)
if not tap_by_desc(machine):
tap_by_desc(machine, whole_match=False)
machine_screen = pr.parse_screen(long_clickable_only=False)
if machine_screen.keys() == list_screen.keys():
os.system("adb shell input keyevent KEYCODE_BACK")
time.sleep(1)
def tap_by_desc(desc, whole_match: bool = True):
candidates = _candidate_descs(desc)
for candidate in candidates:
node = find_by_desc(candidate) if whole_match else find_by_desc_including(candidate)
if node:
tap_by_bounds(node)
return True
return False
def find_by_desc(desc):
"""Return the bounds of the first node whose normalized label matches desc."""
os.system("adb shell uiautomator dump /sdcard/view.xml")
os.system("adb pull /sdcard/view.xml >/dev/null")
tree = etree.parse("view.xml")
target = pr.normalize_ui_text(desc)
for node in tree.iterfind(".//node"):
for raw_value in (node.get("content-desc", ""), node.get("text", "")):
normalized = pr.normalize_ui_text(raw_value)
if not normalized:
continue
if normalized == target or target in normalized.split("\n"):
return node.get("bounds")
print(f"Element '{desc}' not found")
return False
def find_by_desc_including(desc):
"""
Return the bounds of the first node whose content-desc or text contains `desc`.
"""
os.system("adb shell uiautomator dump /sdcard/view.xml")
os.system("adb pull /sdcard/view.xml >/dev/null")
tree = etree.parse("view.xml")
target = pr.normalize_ui_text(desc)
for node in tree.iterfind(".//node"):
for raw_value in (node.get("content-desc", ""), node.get("text", "")):
normalized = pr.normalize_ui_text(raw_value)
if normalized and target in normalized:
return node.get("bounds")
print(f"Element '{desc}' not found")
return False
def tap_by_bounds(bounds):
"""
Tap the screen at the center of the given bounds string.
"""
x, y = get_bounds_center(bounds)
time.sleep(1)
subprocess.run(["adb", "shell", "input", "tap", str(x), str(y)])
print(f"Tapped at {x},{y}")
def scroll_up(screen):
swipe_by_bounds(list(screen.values())[1], list(screen.values())[len(screen) - 2])
def scroll_down(screen):
swipe_by_bounds(list(screen.values())[len(screen) - 2], list(screen.values())[1])
def swipe_by_bounds(bounds1, bounds2):
"""
Swipe from the center of bounds1 to the center of bounds2.
"""
x1, y1 = get_bounds_center(bounds1)
x2, y2 = get_bounds_center(bounds2)
time.sleep(1)
subprocess.run(["adb", "shell", "input", "swipe", str(x1), str(y1), str(x2), str(y2)])
print(f"Swiped from {x1},{y1} to {x2},{y2}")
def get_bounds_center(bounds_str):
"""
Returns tuple[int, int]: (x, y) coordinates representing the center of the bounds.
"""
nums = list(map(int, re.findall(r'\d+', bounds_str)))
x = (nums[0] + nums[2]) // 2
y = (nums[1] + nums[3]) // 2
return x, y