forked from BHznJNs/InputShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb_controller.py
78 lines (70 loc) · 2.41 KB
/
adb_controller.py
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
import re
import os
import sys
import subprocess
import adbutils
from pathlib import Path
from utils.logger import LogType, LOGGER
script_path = Path(__file__).resolve().parent
adb_relative_path = "adb-bin/adb.exe"
adb_bin_path = Path.joinpath(script_path, adb_relative_path)
os.environ["ADBUTILS_ADB_PATH"] = str(adb_bin_path)
ADB_BIN_PATH = str(adb_bin_path)
def start_adb_server() -> bool:
command = f"{ADB_BIN_PATH} start-server"
try:
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
process.wait()
return True
except Exception as e:
process.terminate()
LOGGER.write(LogType.Error, "ADB server failed to start: " + str(e))
return False
def try_pairing(addr: str, pairing_code: str, timeout=3.0) -> bool:
command = f"{ADB_BIN_PATH} pair {addr} {pairing_code}"
try:
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
process.wait(timeout)
_, stderr = process.communicate()
if stderr: raise Exception(stderr)
return True
except Exception as e:
process.terminate()
LOGGER.write(LogType.Error, "ADB failed to pair: " + str(e))
return False
def try_connect_device(addr: str, timeout: float=3.0) -> adbutils.AdbClient | None:
client = adbutils.AdbClient()
try:
output = client.connect(addr, timeout)
assert len(client.device_list()) > 0
LOGGER.write(LogType.Server, output)
except adbutils.AdbTimeout as e:
client.disconnect(addr)
LOGGER.write(LogType.Error, "Connect timeout: " + str(e))
return None
except Exception as e:
client.disconnect(addr)
LOGGER.write(LogType.Error, "Connect failed: " + str(e))
return None
return client
def get_display_size(adb_client: adbutils.AdbClient) -> tuple[int, int]:
device = adb_client.device_list()[0]
output = str(device.shell("dumpsys window displays"))
size_pattern = re.compile(r'cur=\d+x\d+')
size_match = size_pattern.search(output)
if size_match is None:
LOGGER.write(LogType.Error, "Get device size failed.")
sys.exit(1)
size = size_match.group(0).split('=')[1]
width, height = map(int, size.split('x'))
return (width, height)