forked from minhalvp/android-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadbdevicemanager.py
More file actions
187 lines (159 loc) · 7.18 KB
/
Copy pathadbdevicemanager.py
File metadata and controls
187 lines (159 loc) · 7.18 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
import os
import subprocess
import sys
from PIL import Image as PILImage
from ppadb.client import Client as AdbClient
class AdbDeviceManager:
def __init__(self, device_name: str | None = None, exit_on_error: bool = True) -> None:
"""
Initialize the ADB Device Manager
Args:
device_name: Optional name/serial of the device to manage.
If None, attempts to auto-select if only one device is available.
exit_on_error: Whether to exit the program if device initialization fails
"""
if not self.check_adb_installed():
error_msg = "adb is not installed or not in PATH. Please install adb and ensure it is in your PATH."
if exit_on_error:
print(error_msg, file=sys.stderr)
sys.exit(1)
else:
raise RuntimeError(error_msg)
available_devices = self.get_available_devices()
if not available_devices:
error_msg = "No devices connected. Please connect a device and try again."
if exit_on_error:
print(error_msg, file=sys.stderr)
sys.exit(1)
else:
raise RuntimeError(error_msg)
selected_device_name: str | None = None
if device_name:
if device_name not in available_devices:
error_msg = f"Device {device_name} not found. Available devices: {available_devices}"
if exit_on_error:
print(error_msg, file=sys.stderr)
sys.exit(1)
else:
raise RuntimeError(error_msg)
selected_device_name = device_name
else: # No device_name provided, try auto-selection
if len(available_devices) == 1:
selected_device_name = available_devices[0]
print(
f"No device specified, automatically selected: {selected_device_name}")
elif len(available_devices) > 1:
error_msg = f"Multiple devices connected: {available_devices}. Please specify a device in config.yaml or connect only one device."
if exit_on_error:
print(error_msg, file=sys.stderr)
sys.exit(1)
else:
raise RuntimeError(error_msg)
# If len(available_devices) == 0, it's already caught by the earlier check
# At this point, selected_device_name should always be set due to the logic above
# Initialize the device
self.device = AdbClient().device(selected_device_name)
@staticmethod
def check_adb_installed() -> bool:
"""Check if ADB is installed on the system."""
try:
subprocess.run(["adb", "version"], check=True,
stdout=subprocess.PIPE)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
@staticmethod
def get_available_devices() -> list[str]:
"""Get a list of available devices."""
return [device.serial for device in AdbClient().devices()]
def get_packages(self) -> str:
command = "pm list packages"
packages = self.device.shell(command).strip().split("\n")
result = [package[8:] for package in packages]
output = "\n".join(result)
return output
def get_package_action_intents(self, package_name: str) -> list[str]:
command = f"dumpsys package {package_name}"
output = self.device.shell(command)
resolver_table_start = output.find("Activity Resolver Table:")
if resolver_table_start == -1:
return []
resolver_section = output[resolver_table_start:]
non_data_start = resolver_section.find("\n Non-Data Actions:")
if non_data_start == -1:
return []
section_end = resolver_section[non_data_start:].find("\n\n")
if section_end == -1:
non_data_section = resolver_section[non_data_start:]
else:
non_data_section = resolver_section[
non_data_start: non_data_start + section_end
]
actions = []
for line in non_data_section.split("\n"):
line = line.strip()
if line.startswith("android.") or line.startswith("com."):
actions.append(line)
return actions
def execute_adb_shell_command(self, command: str) -> str:
"""Executes an ADB command and returns the output."""
if command.startswith("adb shell "):
command = command[10:]
elif command.startswith("adb "):
command = command[4:]
result = self.device.shell(command)
return result
def take_screenshot(self) -> None:
self.device.shell("screencap -p /sdcard/screenshot.png")
self.device.pull("/sdcard/screenshot.png", "screenshot.png")
self.device.shell("rm /sdcard/screenshot.png")
# compressing the ss to avoid "maximum call stack exceeded" error on claude desktop
with PILImage.open("screenshot.png") as img:
width, height = img.size
new_width = int(width * 0.3)
new_height = int(height * 0.3)
resized_img = img.resize(
(new_width, new_height), PILImage.Resampling.LANCZOS
)
resized_img.save(
"compressed_screenshot.png", "PNG", quality=85, optimize=True
)
def get_uilayout(self) -> str:
self.device.shell("uiautomator dump")
self.device.pull("/sdcard/window_dump.xml", "window_dump.xml")
self.device.shell("rm /sdcard/window_dump.xml")
import re
import xml.etree.ElementTree as ET
def calculate_center(bounds_str):
matches = re.findall(r"\[(\d+),(\d+)\]", bounds_str)
if len(matches) == 2:
x1, y1 = map(int, matches[0])
x2, y2 = map(int, matches[1])
center_x = (x1 + x2) // 2
center_y = (y1 + y2) // 2
return center_x, center_y
return None
tree = ET.parse("window_dump.xml")
root = tree.getroot()
clickable_elements = []
for element in root.findall(".//node[@clickable='true']"):
text = element.get("text", "")
content_desc = element.get("content-desc", "")
bounds = element.get("bounds", "")
# Only include elements that have either text or content description
if text or content_desc:
center = calculate_center(bounds)
element_info = "Clickable element:"
if text:
element_info += f"\n Text: {text}"
if content_desc:
element_info += f"\n Description: {content_desc}"
element_info += f"\n Bounds: {bounds}"
if center:
element_info += f"\n Center: ({center[0]}, {center[1]})"
clickable_elements.append(element_info)
if not clickable_elements:
return "No clickable elements found with text or description"
else:
result = "\n\n".join(clickable_elements)
return result