-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathadb_utils.py
More file actions
280 lines (245 loc) · 11.1 KB
/
Copy pathadb_utils.py
File metadata and controls
280 lines (245 loc) · 11.1 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import subprocess
import datetime
import urllib.parse
import logging
import os
import io
import PIL.Image as Image
from typing import List, Dict, Any, Optional
from audio.audio_play import play_random_audio, VoiceType
from image_hash.hash_find import start_hash_find
from audio.tts import run_tts
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s")
# ---------------------------------------------------------------------------
# Low‑level helpers
# ---------------------------------------------------------------------------
def _run(cmd: List[str], timeout: int = 30) -> bytes:
"""Run a shell command and return raw stdout (raises on non‑zero exit)."""
logger.debug("$ %s", " ".join(cmd))
return subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=timeout)
def _adb_prefix(serial: str | None) -> List[str]:
return ["adb", "-s", serial] if serial else ["adb"]
def _resize_pillow(origin_img, max_line_res: int = 1120):
"""Resize PIL image so that longest edge ≤ `max_line_res` using Lanczos."""
w, h = origin_img.size
if max_line_res is not None:
max_line = max_line_res
if h > max_line:
w = int(w * max_line / h)
h = max_line
if w > max_line:
h = int(h * max_line / w)
w = max_line
return origin_img.resize((w, h), resample=Image.Resampling.LANCZOS)
def _encode_text_for_adb(text: str) -> str:
"""Encode text for adb shell input. URL‑encode spaces as %s."""
def _esc(ch: str) -> str:
if ord(ch) < 128 and ch != " ":
return ch
if ch == " ":
return "%s"
return f"\\u{ord(ch):04x}"
return "".join(_esc(c) for c in text)
def _encode_ascii_for_adb(text: str) -> str:
"""Encode ASCII‑only string for `adb shell input text …` (spaces→%s)."""
return text.replace(" ", "%s")
# ---------------------------------------------------------------------------
# AndroidDevice class
# ---------------------------------------------------------------------------
class AndroidDevice:
"""Encapsulates a single, already‑connected Android handset."""
_yadb_pushed: bool = False
_yadb_local: str = os.path.join(os.path.dirname(__file__), "yadb/yadb")
def __init__(self, serial: str | None, audio_enable=False):
self.serial: str | None = serial
self.width: int = 0
self.height: int = 0
self.last_req_time: datetime.datetime = datetime.datetime.now()
self.audio_enable = audio_enable
# ---------- internal ----------
def _adb(self, *args: str, timeout: int = 30) -> bytes:
return _run(_adb_prefix(self.serial) + list(args), timeout)
def _ensure_yadb(self):
if AndroidDevice._yadb_pushed:
return
if not os.path.exists(AndroidDevice._yadb_local):
raise FileNotFoundError(f"yadb helper not found: {AndroidDevice._yadb_local}")
self._adb("push", AndroidDevice._yadb_local, "/data/local/tmp")
AndroidDevice._yadb_pushed = True
logger.info("yadb pushed to device for Unicode input support")
# ---------- public API ----------
def refresh_resolution(self) -> None:
"""Query and cache `wm size` (sets .width / .height)."""
raw = self._adb("shell", "wm", "size").decode()
try:
size_line = raw.split("Physical size: ")[1].splitlines()[0]
self.width, self.height = map(int, size_line.split("x"))
logger.info("Device %s resolution: %dx%d", self.serial or "<default>",
self.width, self.height)
except Exception as exc:
raise RuntimeError(f"Failed to parse wm size output: {raw}") from exc
# -------------------------------------------------------------------
# Step: execute user action
# -------------------------------------------------------------------
def step(self, data: Dict[str, Any]) -> None:
"""Execute a control step on the device (tap/swipe/key/text/clear)."""
logger.debug("Step: %s", data)
if "POINT" in data:
self._handle_point(data)
if "PRESS" in data:
self._handle_press(data["PRESS"])
if "TYPE" in data:
self._handle_type(data["TYPE"])
if "CLEAR" in data:
self._adb("shell", "input", "keyevent", "KEYCODE_CLEAR")
self.last_req_time = datetime.datetime.now()
if ("STATUS", "finish") in data.items() or ("STATUS", "impossible") in data.items():
logger.info("Task finished")
return True
return False
# -------------------------------------------------------------------
# State snapshot
# -------------------------------------------------------------------
def state(self) -> Dict[str, Any]:
return {
"width": self.width,
"height": self.height,
"last_req_time": self.last_req_time.isoformat(),
"screenshot": self.screenshot(),
}
# --- Device state ---------------------------------------------------
def screenshot(self, max_side: Optional[int] = None) -> Image.Image:
"""Grab screen; return Pillow Image. Optionally down‑scale with user rule."""
png_bytes = self._adb("exec-out", "screencap", "-p")
img = Image.open(io.BytesIO(png_bytes))
if max_side is not None:
img = _resize_pillow(img, max_side)
return img
# =================== private helpers ===================
def _handle_point(self, data: Dict[str, Any]) -> None:
search_thershold = 150
x1, y1 = data["POINT"]
print("point:",x1,y1)
x = int(x1 / 1000 * self.width)
y = int(y1 / 1000 * self.height)
if "to" in data:
if self.audio_enable:
play_random_audio(VoiceType.SWIPE)
if isinstance(data["to"], list):
x2, y2 = data["to"]
x2 = int(x2 / 1000 * self.width)
y2 = int(y2 / 1000 * self.height)
else: # directional swipe (up/down/left/right)
dirs = {
"up": (0, -0.15),
"down": (0, 0.15),
"left": (-0.15, 0),
"right": (0.15, 0),
}
if data["to"] not in dirs:
raise ValueError(f"Invalid swipe direction: {data['to']}")
dx_ratio, dy_ratio = dirs[data["to"]]
x2 = int(max(min(x + dx_ratio * self.width, self.width), 0))
y2 = int(max(min(y + dy_ratio * self.height, self.height), 0))
dur = str(data.get("duration", 150))
# Perform swipe
self._adb("shell", "input", "swipe", str(x), str(y), str(x2), str(y2), dur)
else: # simple tap
if self.audio_enable:
result = start_hash_find()
if result == "openapp":
play_random_audio(VoiceType.OPENAPP)
if result == "entersearch":
if y1< search_thershold:
play_random_audio(VoiceType.ENTERSEARCH)
else:
play_random_audio(VoiceType.ENTERPAGE)
if result == "enterpage":
if y1< search_thershold:
play_random_audio(VoiceType.ENTERSEARCH)
else:
play_random_audio(VoiceType.ENTERPAGE)
# Show tap location
#print("tap:", x,y)
self._adb("shell", "input", "tap", str(x), str(y))
def _handle_press(self, key: str) -> None:
if self.audio_enable:
play_random_audio(VoiceType.PRESS)
KEYS = {
"HOME": "KEYCODE_HOME",
"BACK": "KEYCODE_BACK",
"MENU": "KEYCODE_MENU",
"ENTER": "KEYCODE_ENTER",
"APPSELECT": "KEYCODE_APP_SWITCH",
"power": "KEYCODE_POWER",
"volume_up": "KEYCODE_VOLUME_UP",
"volume_down": "KEYCODE_VOLUME_DOWN",
"volume_mute": "KEYCODE_VOLUME_MUTE",
}
if key not in KEYS:
raise ValueError(f"Unknown PRESS value: {key}")
self._adb("shell", "input", "keyevent", KEYS[key])
# def _handle_type(self, raw):
# decoded = urllib.parse.unquote(raw)
# self._adb("shell", "am", "broadcast", '-a', 'ADB_INPUT_TEXT', '--es msg' , decoded)
# # self._adb("shell", "input", "text", decoded)
def _handle_type(self, raw):
text = urllib.parse.unquote(raw)
if self.audio_enable:
run_tts(text, output="assets/audio/voice_inputtext/text.mp3")
play_random_audio(VoiceType.TYPE)
play_random_audio(VoiceType.INPUTTEXT)
if all(ord(c) < 128 for c in text): # quick ASCII path
self._adb("shell", "input", "text", _encode_ascii_for_adb(text))
return
# Unicode → yadb
self._ensure_yadb()
safe = text.replace("'", "'\\''") # escape sigingle quotes for sh
cmd = (
"app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp "
"com.ysbing.yadb.Main -keyboard '%s'" % safe
)
self._adb("shell", cmd)
# ---------------------------------------------------------------------------
# Public utility function
# ---------------------------------------------------------------------------
def list_connected_devices() -> List[str]:
"""列出所有已连接的ADB设备"""
lines = _run(["adb", "devices"]).decode().strip().splitlines()[1:]
return [l.split()[0] for l in lines if l.strip() and "device" in l]
# 支持指定设备序列号
def setup_device(serial: str | None = None, audio_enable=False) -> AndroidDevice:
"""创建AndroidDevice实例,可指定设备序列号"""
if serial is None:
devices = list_connected_devices()
if not devices:
raise RuntimeError("No authorised Android device found. Plug in & check adb.")
if len(devices) > 1:
logger.warning("Multiple devices detected; defaulting to the first (%s).", devices[0])
serial = devices[0]
dev = AndroidDevice(serial, audio_enable=audio_enable)
dev.refresh_resolution()
return dev
def change_ui_settings(mode: str = "open"):
mode = mode.strip()
if mode == "open":
subprocess.run("adb shell settings put system pointer_location 1", shell=True)
elif mode == "close":
subprocess.run("adb shell settings put system pointer_location 0", shell=True)
else:
raise ValueError("Invalid Openmode")
# ---------------------------------------------------------------------------
# Demo – run this file directly to test
# ---------------------------------------------------------------------------
if __name__ == "__main__":
device = setup_device()
logger.info("Device ready: serial=%s (%dx%d)", device.serial, device.width, device.height)
# Example: tap centre, take screenshot
x = 900
y = 800
device.step({"POINT": [x, y]})
png = device.screenshot()
target = os.path.join("screenshots", "screencap.png")
# logger.info("Screenshot saved → %s (%d bytes)", target, len(png))