Skip to content

Commit

Permalink
Add display physical size and dp
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmilano committed Apr 3, 2024
1 parent fde8970 commit 41699df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/helper/screen-size
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env python3

from com.dtmilano.android.viewclient import ViewClient

print(ViewClient.view_client_helper().device.display_physical_size())
35 changes: 32 additions & 3 deletions src/com/dtmilano/android/uiautomator/uiautomatorhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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'):
"""
Expand Down

0 comments on commit 41699df

Please sign in to comment.