|
4 | 4 |
|
5 | 5 | from askui.models.shared.tool_tags import ToolTags |
6 | 6 | from askui.tools.android.agent_os import ANDROID_KEY, AndroidAgentOs, AndroidDisplay |
| 7 | +from askui.tools.android.uiautomator_hierarchy import UIElementCollection |
7 | 8 | from askui.utils.image_utils import scale_coordinates, scale_image_to_fit |
8 | 9 |
|
9 | 10 |
|
@@ -36,33 +37,38 @@ def screenshot(self) -> Image.Image: |
36 | 37 | self._target_resolution, |
37 | 38 | ) |
38 | 39 |
|
39 | | - def _scale_coordinates_back(self, x: int, y: int) -> Tuple[int, int]: |
| 40 | + def _scale_coordinates( |
| 41 | + self, |
| 42 | + x: int, |
| 43 | + y: int, |
| 44 | + from_agent: bool = True, |
| 45 | + ) -> Tuple[int, int]: |
40 | 46 | if self._real_screen_resolution is None: |
41 | 47 | self._real_screen_resolution = self._agent_os.screenshot().size |
42 | 48 |
|
43 | 49 | return scale_coordinates( |
44 | 50 | (x, y), |
45 | 51 | self._real_screen_resolution, |
46 | 52 | self._target_resolution, |
47 | | - inverse=True, |
| 53 | + inverse=from_agent, |
48 | 54 | ) |
49 | 55 |
|
50 | 56 | def tap(self, x: int, y: int) -> None: |
51 | | - x, y = self._scale_coordinates_back(x, y) |
| 57 | + x, y = self._scale_coordinates(x, y) |
52 | 58 | self._agent_os.tap(x, y) |
53 | 59 |
|
54 | 60 | def swipe( |
55 | 61 | self, x1: int, y1: int, x2: int, y2: int, duration_in_ms: int = 1000 |
56 | 62 | ) -> None: |
57 | | - x1, y1 = self._scale_coordinates_back(x1, y1) |
58 | | - x2, y2 = self._scale_coordinates_back(x2, y2) |
| 63 | + x1, y1 = self._scale_coordinates(x1, y1) |
| 64 | + x2, y2 = self._scale_coordinates(x2, y2) |
59 | 65 | self._agent_os.swipe(x1, y1, x2, y2, duration_in_ms) |
60 | 66 |
|
61 | 67 | def drag_and_drop( |
62 | 68 | self, x1: int, y1: int, x2: int, y2: int, duration_in_ms: int = 1000 |
63 | 69 | ) -> None: |
64 | | - x1, y1 = self._scale_coordinates_back(x1, y1) |
65 | | - x2, y2 = self._scale_coordinates_back(x2, y2) |
| 70 | + x1, y1 = self._scale_coordinates(x1, y1) |
| 71 | + x2, y2 = self._scale_coordinates(x2, y2) |
66 | 72 | self._agent_os.drag_and_drop(x1, y1, x2, y2, duration_in_ms) |
67 | 73 |
|
68 | 74 | def type(self, text: str) -> None: |
@@ -121,3 +127,17 @@ def push(self, local_path: str, remote_path: str) -> None: |
121 | 127 |
|
122 | 128 | def pull(self, remote_path: str, local_path: str) -> None: |
123 | 129 | self._agent_os.pull(remote_path, local_path) |
| 130 | + |
| 131 | + def get_ui_elements(self) -> UIElementCollection: |
| 132 | + ui_elemet_collection = self._agent_os.get_ui_elements() |
| 133 | + for element in ui_elemet_collection: |
| 134 | + if element.center is None: |
| 135 | + continue |
| 136 | + element.set_center( |
| 137 | + self._scale_coordinates( |
| 138 | + x=element.center[0], |
| 139 | + y=element.center[1], |
| 140 | + from_agent=False, |
| 141 | + ) |
| 142 | + ) |
| 143 | + return ui_elemet_collection |
0 commit comments