From 41699df164feec12d9faa7f87e41189e3ea1cda9 Mon Sep 17 00:00:00 2001 From: Diego Torres Milano Date: Tue, 2 Apr 2024 22:19:25 -0700 Subject: [PATCH] Add display physical size and dp --- examples/helper/screen-size | 5 +++ .../android/uiautomator/uiautomatorhelper.py | 35 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100755 examples/helper/screen-size diff --git a/examples/helper/screen-size b/examples/helper/screen-size new file mode 100755 index 00000000..12d569ef --- /dev/null +++ b/examples/helper/screen-size @@ -0,0 +1,5 @@ +#! /usr/bin/env python3 + +from com.dtmilano.android.viewclient import ViewClient + +print(ViewClient.view_client_helper().device.display_physical_size()) \ No newline at end of file diff --git a/src/com/dtmilano/android/uiautomator/uiautomatorhelper.py b/src/com/dtmilano/android/uiautomator/uiautomatorhelper.py index 770c366f..07fb9737 100644 --- a/src/com/dtmilano/android/uiautomator/uiautomatorhelper.py +++ b/src/com/dtmilano/android/uiautomator/uiautomatorhelper.py @@ -20,8 +20,9 @@ from __future__ import print_function -__version__ = '23.2.0' +__version__ = '23.3.0' +import math import os import platform import re @@ -317,6 +318,22 @@ def display_real_size(self): """ return self.uiAutomatorHelper.api_instance.device_display_real_size_get() + def display_physical_size(self): + """ + Gets the physical width, height and diagonal + :return: physical width, height and diagonal + :rtype: dict + """ + drs = self.uiAutomatorHelper.api_instance.device_display_real_size_get() + display = self.dumpsys("display") + m = re.search(r"density (\d+), ([\d.]+) x ([\d.]+) dpi", display) + assert len(m.groups()) == 3 + density, xdpi, ydpi = m.groups() + pw = drs.x / float(xdpi) + ph = drs.y / float(xdpi) + diag = math.sqrt(pw * pw + ph * ph) + return {"physical_width": round(pw, 2), "physical_height": round(ph, 2), "screen": round(diag, 2)} + def dumpsys(self, service, **kwargs) -> str: """ :see https://github.com/dtmilano/CulebraTester2-public/blob/master/openapi.yaml @@ -449,7 +466,18 @@ def click(self, x: int, y: int): """ check_response(self.uiAutomatorHelper.api_instance.ui_device_click_get(x=x, y=y)) - def drag(self, start_x: int, start_y: int, end_x: int, end_y:int , steps: int) -> None: + def display_size_dp(self): + """ + Returns the default display size in dp (device-independent pixel). + + The returned display size is adjusted per screen rotation. Also this will return the actual size of the + screen, rather than adjusted per system decorations (like status bar). + :return: the DPs + :rtype: + """ + return self.uiAutomatorHelper.api_instance.ui_device_display_size_dp_get() + + def drag(self, start_x: int, start_y: int, end_x: int, end_y: int, steps: int) -> None: """Performs a swipe from one coordinate to another coordinate. Performs a swipe from one coordinate to another coordinate. You can control the smoothness and speed of the @@ -463,7 +491,8 @@ def drag(self, start_x: int, start_y: int, end_x: int, end_y:int , steps: int) - :param int end_y: end y (required) :param int steps: is the number of move steps sent to the system (required) """ - check_response(self.uiAutomatorHelper.api_instance.ui_device_drag_get(start_x, start_y, end_x, end_y, steps)) + check_response( + self.uiAutomatorHelper.api_instance.ui_device_drag_get(start_x, start_y, end_x, end_y, steps)) def dump_window_hierarchy(self, _format='JSON'): """