98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
"""Small adb helpers for driving the phone during capture.
|
|
|
|
Usage:
|
|
python tools/phone.py shot [out.png] # screenshot
|
|
python tools/phone.py texts # list visible texts with centers
|
|
python tools/phone.py tap <text> # tap first node whose text/desc contains <text>
|
|
python tools/phone.py tapxy <x> <y>
|
|
python tools/phone.py back
|
|
python tools/phone.py bind # bind CA dir into every app process
|
|
"""
|
|
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import xml.etree.ElementTree as ET
|
|
from pathlib import Path
|
|
|
|
PKG = "com.zte.softda"
|
|
|
|
|
|
def adb(*args: str, binary: bool = False):
|
|
out = subprocess.run(["adb", *args], capture_output=True, check=False)
|
|
return out.stdout if binary else out.stdout.decode("utf-8", "replace")
|
|
|
|
|
|
def su(cmd: str) -> str:
|
|
return adb("shell", f"su -c '{cmd}'")
|
|
|
|
|
|
def shot(path: str = "data/screen.png") -> None:
|
|
adb("shell", "screencap -p /data/local/tmp/s.png")
|
|
adb("pull", "/data/local/tmp/s.png", path)
|
|
print(path)
|
|
|
|
|
|
def nodes():
|
|
adb("shell", "uiautomator dump /data/local/tmp/ui.xml")
|
|
xml = adb("shell", "cat /data/local/tmp/ui.xml")
|
|
root = ET.fromstring(xml[xml.index("<"):])
|
|
for n in root.iter("node"):
|
|
label = n.get("text") or n.get("content-desc") or ""
|
|
m = re.findall(r"\d+", n.get("bounds", ""))
|
|
if label and len(m) == 4:
|
|
x1, y1, x2, y2 = map(int, m)
|
|
yield label, (x1 + x2) // 2, (y1 + y2) // 2
|
|
|
|
|
|
def texts() -> None:
|
|
for label, x, y in nodes():
|
|
print(f"{x:5d} {y:5d} {label}")
|
|
|
|
|
|
def tap(text: str) -> None:
|
|
for label, x, y in nodes():
|
|
if text in label:
|
|
adb("shell", "input", "tap", str(x), str(y))
|
|
print(f"tap {label!r} at {x},{y}")
|
|
return
|
|
print(f"not found: {text}")
|
|
sys.exit(1)
|
|
|
|
|
|
BIND_SCRIPT = f"""\
|
|
for P in $(ps -A -o PID,NAME | awk '$2 ~ /^{PKG}/ {{print $1}}'); do
|
|
grep -q conscrypt/cacerts /proc/$P/mountinfo && continue
|
|
nsenter --mount=/proc/$P/ns/mnt -- /system/bin/mount --bind \\
|
|
/data/local/tmp/cacerts /apex/com.android.conscrypt/cacerts && echo "bound $P"
|
|
done
|
|
"""
|
|
|
|
|
|
def bind() -> None:
|
|
local = Path(tempfile.gettempdir()) / "bindall.sh"
|
|
local.write_bytes(BIND_SCRIPT.encode())
|
|
adb("push", str(local), "/data/local/tmp/bindall.sh")
|
|
print(su("sh /data/local/tmp/bindall.sh").strip() or "all processes already bound")
|
|
|
|
|
|
def main() -> None:
|
|
cmd, *rest = sys.argv[1:] or ["texts"]
|
|
if cmd == "shot":
|
|
shot(*rest)
|
|
elif cmd == "texts":
|
|
texts()
|
|
elif cmd == "tap":
|
|
tap(" ".join(rest))
|
|
elif cmd == "tapxy":
|
|
adb("shell", "input", "tap", *rest)
|
|
elif cmd == "back":
|
|
adb("shell", "input", "keyevent", "4")
|
|
elif cmd == "bind":
|
|
bind()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|