Skip to content

Commit

Permalink
feat: add function to hdc wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
codematrixer committed Oct 6, 2024
1 parent 3cb57b5 commit def4cd5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
29 changes: 1 addition & 28 deletions hmdriver2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,44 +80,17 @@ def current_app(self) -> Tuple[str, str]:
"""
Get the current foreground application information.
This method executes a shell command to dump the current application state and extracts
the package_name and page_name of the application that is in the foreground state.
Returns:
Tuple[str, str]: A tuple contain the package_name andpage_name of the foreground application.
If no foreground application is found, returns (None, None).
"""

def __extract_info(output: str):
results = []

mission_blocks = re.findall(r'Mission ID #[\s\S]*?isKeepAlive: false\s*}', output)
if not mission_blocks:
return results

for block in mission_blocks:
if 'state #FOREGROUND' in block:
bundle_name_match = re.search(r'bundle name \[(.*?)\]', block)
main_name_match = re.search(r'main name \[(.*?)\]', block)
if bundle_name_match and main_name_match:
package_name = bundle_name_match.group(1)
page_name = main_name_match.group(1)
results.append((package_name, page_name))

return results

data: CommandResult = self.hdc.shell("aa dump -l")
output = data.output
results = __extract_info(output)
return results[0] if results else (None, None)
return self.hdc.current_app()

def get_app_info(self, package_name: str) -> Dict:
"""
Get detailed information about a specific application.
This method executes a shell command to dump the application information for the given package name
and parses the output as JSON to extract the application details.
Args:
package_name (str): The package name of the application to retrieve information for.
Expand Down
44 changes: 43 additions & 1 deletion hmdriver2/hdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import shlex
import re
import subprocess
from typing import Union, List, Dict
from typing import Union, List, Dict, Tuple

from . import logger
from .utils import FreePort
Expand Down Expand Up @@ -127,6 +127,38 @@ def start_app(self, package_name: str, ability_name: str):
def stop_app(self, package_name: str):
return self.shell(f"aa force-stop {package_name}")

def current_app(self) -> Tuple[str, str]:
"""
Get the current foreground application information.
Returns:
Tuple[str, str]: A tuple contain the package_name andpage_name of the foreground application.
If no foreground application is found, returns (None, None).
"""

def __extract_info(output: str):
results = []

mission_blocks = re.findall(r'Mission ID #[\s\S]*?isKeepAlive: false\s*}', output)
if not mission_blocks:
return results

for block in mission_blocks:
if 'state #FOREGROUND' in block:
bundle_name_match = re.search(r'bundle name \[(.*?)\]', block)
main_name_match = re.search(r'main name \[(.*?)\]', block)
if bundle_name_match and main_name_match:
package_name = bundle_name_match.group(1)
page_name = main_name_match.group(1)
results.append((package_name, page_name))

return results

data: CommandResult = self.shell("aa dump -l")
output = data.output
results = __extract_info(output)
return results[0] if results else (None, None)

def wakeup(self):
self.shell("power-shell wakeup")

Expand Down Expand Up @@ -172,6 +204,16 @@ def cpu_abi(self) -> str:
data = self.shell("param get const.product.cpu.abilist").output
return self.__split_text(data)

def display_size(self) -> Tuple[int, int]:
data = self.shell("hidumper -s RenderService -a screen").output
match = re.search(r'activeMode:\s*(\d+)x(\d+),\s*refreshrate=\d+', data)

if match:
w = int(match.group(1))
h = int(match.group(2))
return (w, h)
return (0, 0)

def send_key(self, key_code: Union[KeyCode, int]) -> None:
if isinstance(key_code, KeyCode):
key_code = key_code.value
Expand Down

0 comments on commit def4cd5

Please sign in to comment.