Skip to content

Commit

Permalink
Merge pull request #1241 from ayush5harma/master
Browse files Browse the repository at this point in the history
changes to accessibility list-items
  • Loading branch information
doronz88 authored Oct 15, 2024
2 parents dcf4fee + d9e4218 commit 6706704
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
12 changes: 6 additions & 6 deletions pymobiledevice3/cli/developer.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def accessibility_notifications(service_provider: LockdownClient):

@accessibility.command('list-items', cls=Command)
def accessibility_list_items(service_provider: LockdownClient):
""" list items available in currently shown menu """
"""List items available in the currently shown menu."""

service = AccessibilityAudit(service_provider)
iterator = service.iter_events()
Expand All @@ -869,6 +869,7 @@ def accessibility_list_items(service_provider: LockdownClient):
service.move_focus_next()

first_item = None
items = []

for event in iterator:
if event.name != 'hostInspectorCurrentElementChanged:':
Expand All @@ -880,14 +881,13 @@ def accessibility_list_items(service_provider: LockdownClient):

if first_item is None:
first_item = current_item
else:
if first_item.caption == current_item.caption:
return
elif first_item.caption == current_item.caption:
return # Break if we encounter the first item again (loop)

print(f'{current_item.caption}: {current_item.element.identifier}')
items.append(current_item.to_dict())
print_json(items)
service.move_focus_next()


@developer.group('condition')
def condition():
""" Force a predefined condition """
Expand Down
46 changes: 45 additions & 1 deletion pymobiledevice3/services/accessibilityaudit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,54 @@ def __init__(self, fields):
def caption(self) -> str:
return self._fields.get('CaptionTextValue_v1')

@property
def spoken_description(self) -> str:
return self._fields.get('SpokenDescriptionValue_v1')

@property
def element(self) -> bytes:
return self._fields.get('ElementValue_v1')

@property
def platform_identifier(self) -> str:
"""Converts the element bytes to a hexadecimal string."""
return self.element.identifier.hex().upper()

@property
def estimated_uid(self) -> str:
"""Generates a UID from the platform identifier."""
hex_value = self.platform_identifier

if len(hex_value) % 2 != 0:
raise ValueError("Hex value length must be even.")

hex_bytes = bytes.fromhex(hex_value)

if len(hex_bytes) < 16:
raise ValueError("Hex value must contain at least 16 bytes.")

# Extract TimeLow bytes (indexes 12 to 15)
time_low_bytes = hex_bytes[12:16]
time_low = time_low_bytes.hex().upper()

# Extract ClockSeq bytes (indexes 0 to 1)
clock_seq_bytes = hex_bytes[0:2]
clock_seq = clock_seq_bytes.hex().upper()

# Construct UID with placeholder values for unused parts
uid = f"{time_low}-0000-0000-{clock_seq}-000000000000"

return uid

def to_dict(self) -> dict:
"""Serializes the focus element into a dictionary."""
return {
'platform_identifier': self.platform_identifier,
'estimated_uid': self.estimated_uid,
'caption': self.caption,
'spoken_description': self.spoken_description
}

def __str__(self):
return f'<Focused ElementCaption: {self.caption}>'

Expand All @@ -37,7 +81,7 @@ def __init__(self, fields):

@property
def identifier(self) -> bytes:
return self._fields['PlatformElementValue_v1'].NSdata
return self._fields['PlatformElementValue_v1']

def __repr__(self):
return f'<Element: {self.identifier}>'
Expand Down

0 comments on commit 6706704

Please sign in to comment.