Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pyclashbot/emulators/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyclashbot.emulators.adb_base import AdbBasedController
from pyclashbot.utils.cancellation import interruptible_sleep
from pyclashbot.utils.platform import Platform, is_linux
from pyclashbot.emulators import adb_base

# Set to True for verbose ADB command logging
DEBUG = False
Expand Down Expand Up @@ -68,6 +69,7 @@ def get_screen_props(self):
"""Gets the current screen size and density of the device."""
size_result = self.adb("shell wm size")
density_result = self.adb("shell wm density")
mstable_result = self.adb("shell dumpsys window")

size = None
density = None
Expand All @@ -86,6 +88,14 @@ def get_screen_props(self):
if match:
density = int(match.group(1))

if mstable_result.returncode == 0:
output = mstable_result.stdout.strip()
# mStable=[0,80]
match = re.search(r"mStable=\[(\d+),(\d+)\]", output)
if match:
adb_base.android_x_start = int(match.group(1))
adb_base.android_y_start = int(match.group(2))

return size, density

def handle_screen_size_and_density(self):
Expand All @@ -96,7 +106,7 @@ def handle_screen_size_and_density(self):
self.logger.log("Checking screen size and density...")
current_size, current_density = self.get_screen_props()

required_size = "419x633"
required_size = f"{419 + adb_base.android_x_start}x{633 + adb_base.android_y_start}"
required_density = 160

# Store original values if they haven't been stored yet
Expand All @@ -110,7 +120,7 @@ def handle_screen_size_and_density(self):
# Check and set size
if current_size != required_size:
self.logger.log(f"Current size {current_size} is not the required {required_size}. Setting it now.")
self.set_screen_size(419, 633)
self.set_screen_size(419 + adb_base.android_x_start, 633 + adb_base.android_y_start)
else:
self.logger.log("Screen size is already correct.")

Expand Down
10 changes: 10 additions & 0 deletions pyclashbot/emulators/adb_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pyclashbot.emulators.base import BaseEmulatorController
from pyclashbot.utils.cancellation import interruptible_sleep

android_x_start, android_y_start = 0, 0


class AdbBasedController(BaseEmulatorController, ABC):
"""
Expand Down Expand Up @@ -58,13 +60,19 @@ def _check_app_installed(self, package_name: str) -> bool:

def click(self, x_coord: int, y_coord: int, clicks: int = 1, interval: float = 0.0):
"""Click on the screen using ADB input tap."""
x_coord += android_x_start
y_coord += android_y_start
for _ in range(max(1, clicks)):
self.adb(f"shell input tap {x_coord} {y_coord}")
if clicks > 1:
interruptible_sleep(max(0.0, interval))

def swipe(self, x_coord1: int, y_coord1: int, x_coord2: int, y_coord2: int):
"""Swipe on the screen using ADB input swipe."""
x_coord1 += android_x_start
y_coord1 += android_y_start
x_coord2 += android_x_start
y_coord2 += android_y_start
self.adb(f"shell input swipe {x_coord1} {y_coord1} {x_coord2} {y_coord2}")

def screenshot(self) -> np.ndarray:
Expand All @@ -89,6 +97,8 @@ def screenshot(self) -> np.ndarray:

if img is None:
raise ValueError("Failed to decode screenshot. Image data may be corrupt or empty.")
else:
img = img[android_y_start:, android_x_start:]

return img

Expand Down