Skip to content

Commit 41699df

Browse files
committed
Add display physical size and dp
1 parent fde8970 commit 41699df

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

examples/helper/screen-size

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /usr/bin/env python3
2+
3+
from com.dtmilano.android.viewclient import ViewClient
4+
5+
print(ViewClient.view_client_helper().device.display_physical_size())

src/com/dtmilano/android/uiautomator/uiautomatorhelper.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
from __future__ import print_function
2222

23-
__version__ = '23.2.0'
23+
__version__ = '23.3.0'
2424

25+
import math
2526
import os
2627
import platform
2728
import re
@@ -317,6 +318,22 @@ def display_real_size(self):
317318
"""
318319
return self.uiAutomatorHelper.api_instance.device_display_real_size_get()
319320

321+
def display_physical_size(self):
322+
"""
323+
Gets the physical width, height and diagonal
324+
:return: physical width, height and diagonal
325+
:rtype: dict
326+
"""
327+
drs = self.uiAutomatorHelper.api_instance.device_display_real_size_get()
328+
display = self.dumpsys("display")
329+
m = re.search(r"density (\d+), ([\d.]+) x ([\d.]+) dpi", display)
330+
assert len(m.groups()) == 3
331+
density, xdpi, ydpi = m.groups()
332+
pw = drs.x / float(xdpi)
333+
ph = drs.y / float(xdpi)
334+
diag = math.sqrt(pw * pw + ph * ph)
335+
return {"physical_width": round(pw, 2), "physical_height": round(ph, 2), "screen": round(diag, 2)}
336+
320337
def dumpsys(self, service, **kwargs) -> str:
321338
"""
322339
:see https://github.com/dtmilano/CulebraTester2-public/blob/master/openapi.yaml
@@ -449,7 +466,18 @@ def click(self, x: int, y: int):
449466
"""
450467
check_response(self.uiAutomatorHelper.api_instance.ui_device_click_get(x=x, y=y))
451468

452-
def drag(self, start_x: int, start_y: int, end_x: int, end_y:int , steps: int) -> None:
469+
def display_size_dp(self):
470+
"""
471+
Returns the default display size in dp (device-independent pixel).
472+
473+
The returned display size is adjusted per screen rotation. Also this will return the actual size of the
474+
screen, rather than adjusted per system decorations (like status bar).
475+
:return: the DPs
476+
:rtype:
477+
"""
478+
return self.uiAutomatorHelper.api_instance.ui_device_display_size_dp_get()
479+
480+
def drag(self, start_x: int, start_y: int, end_x: int, end_y: int, steps: int) -> None:
453481
"""Performs a swipe from one coordinate to another coordinate.
454482
455483
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) -
463491
:param int end_y: end y (required)
464492
:param int steps: is the number of move steps sent to the system (required)
465493
"""
466-
check_response(self.uiAutomatorHelper.api_instance.ui_device_drag_get(start_x, start_y, end_x, end_y, steps))
494+
check_response(
495+
self.uiAutomatorHelper.api_instance.ui_device_drag_get(start_x, start_y, end_x, end_y, steps))
467496

468497
def dump_window_hierarchy(self, _format='JSON'):
469498
"""

0 commit comments

Comments
 (0)