Skip to content

let your agent control your phone

fix(android): refresh current display bounds after rotation (#64)

Tyyyy4e0ca049ba9b
diff --git a/src/phone_harness/android.py b/src/phone_harness/android.py
index d7d0506..2532695 100644
--- a/src/phone_harness/android.py
+++ b/src/phone_harness/android.py
@@ -345,20 +345,30 @@ class Android(Backend):
# --- screen -------------------------------------------------------------
def _screen_bounds(self):
- if self._bounds is None:
- if self._resolve() is None:
- return None
+ """Current default-display pixels, including rotation and size changes."""
+ if self._resolve() is None:
+ return None
+ try:
+ out = self._sh("dumpsys window displays")
+ except RuntimeError:
+ out = ""
+ # wm size describes the unrotated display. WindowManager's current
+ # dimensions match the screenshot and input coordinate space. Read
+ # them each time: one connection can outlive a rotation or wm resize.
+ m = re.search(r"Display: mDisplayId=0\b[^\n]*\n[^\n]*\bcur=(\d+)x(\d+)", out)
+ if not m:
+ # Retain support for devices whose WindowManager dump differs.
try:
out = self._sh("wm size")
except RuntimeError:
return None
m = (re.search(r"Override size:\s*(\d+)x(\d+)", out)
or re.search(r"Physical size:\s*(\d+)x(\d+)", out))
- if not m:
- return None
- self._bounds = {"x": 0, "y": 0, "w": int(m.group(1)),
- "h": int(m.group(2)),
- "id": os.environ.get("ANDROID_SERIAL")}
+ if not m:
+ return None
+ self._bounds = {"x": 0, "y": 0, "w": int(m.group(1)),
+ "h": int(m.group(2)),
+ "id": os.environ.get("ANDROID_SERIAL")}
return self._bounds
def _screen_require(self):
diff --git a/tests/test_android_screen_bounds.py b/tests/test_android_screen_bounds.py
new file mode 100644
index 0000000..99db290
--- /dev/null
+++ b/tests/test_android_screen_bounds.py
@@ -0,0 +1,65 @@
+import os
+import unittest
+from unittest.mock import patch
+
+from phone_harness.android import Android
+
+
+def display(width, height, display_id=0):
+ return (f" Display: mDisplayId={display_id} (organized)\n"
+ f" init=1080x2280 440dpi cur={width}x{height} app={width}x{height}\n")
+
+
+class AndroidScreenBoundsTests(unittest.TestCase):
+ def setUp(self):
+ env = patch.dict(os.environ)
+ env.start()
+ self.addCleanup(env.stop)
+ self.phone = Android(serial="test-device")
+
+ def test_bounds_follow_rotation_and_resize_on_same_connection(self):
+ current = [1080, 2280]
+ def shell(command):
+ if command == "dumpsys window displays":
+ return display(*current)
+ return "Physical size: 1080x2280"
+ with patch.object(self.phone, "_sh", side_effect=shell):
+ for size in [(1080, 2280), (2280, 1080), (720, 1280), (1080, 2280)]:
+ with self.subTest(size=size):
+ current[:] = size
+ bounds = self.phone.send("screen.bounds")
+ self.assertEqual((bounds["w"], bounds["h"]), size)
+ self.assertEqual(bounds["id"], "test-device")
+
+ def test_secondary_display_is_not_used(self):
+ output = display(800, 600, 2) + display(2280, 1080)
+ with patch.object(self.phone, "_sh", return_value=output):
+ bounds = self.phone.send("screen.bounds")
+ self.assertEqual((bounds["w"], bounds["h"]), (2280, 1080))
+
+ def test_unknown_dump_falls_back_to_override_size(self):
+ def shell(command):
+ return ("unknown format" if command == "dumpsys window displays"
+ else "Physical size: 1080x2280\nOverride size: 720x1280")
+ with patch.object(self.phone, "_sh", side_effect=shell):
+ bounds = self.phone.send("screen.bounds")
+ self.assertEqual((bounds["w"], bounds["h"]), (720, 1280))
+
+ def test_unavailable_window_dump_falls_back_to_physical_size(self):
+ def shell(command):
+ if command == "dumpsys window displays":
+ raise RuntimeError("not available")
+ return "Physical size: 1080x2280"
+ with patch.object(self.phone, "_sh", side_effect=shell):
+ bounds = self.phone.send("screen.bounds")
+ self.assertEqual((bounds["w"], bounds["h"]), (1080, 2280))
+
+ def test_missing_dimensions_do_not_reuse_stale_bounds(self):
+ with patch.object(self.phone, "_sh", return_value="Physical size: 1080x2280"):
+ self.assertIsNotNone(self.phone.send("screen.bounds"))
+ with patch.object(self.phone, "_sh", return_value="unavailable"):
+ self.assertIsNone(self.phone.send("screen.bounds"))
+
+
+if __name__ == "__main__":
+ unittest.main()