Skip to content

Commit

Permalink
update the auto_grant_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 27, 2024
1 parent 5d20e56 commit 42b4457
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion mobile_tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ def test_session_window_size(dev: u2.Device):


def test_auto_grant_permissions(dev: u2.Device):
dev.app_auto_grant_permissions('com.tencent.mm')
dev.app_auto_grant_permissions("io.appium.android.apis")

78 changes: 40 additions & 38 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,47 +896,49 @@ def app_info(self, package_name: str) -> Dict[str, Any]:
"versionCode": info.version_code,
}

def app_auto_grant_permissions(self, package_name: str) -> bool:
""" auto grant runtime permissions to target app,prevent dynamic permission pop-up window to pop up
def app_auto_grant_permissions(self, package_name: str):
""" auto grant permissions
Args:
package_name (str): package name
Returns:
bool of operate
Help of "adb shell pm":
grant [--user USER_ID] PACKAGE PERMISSION
revoke [--user USER_ID] PACKAGE PERMISSION
These commands either grant or revoke permissions to apps. The permissions
must be declared as used in the app's manifest, be runtime permissions
(protection level dangerous), and the app targeting SDK greater than Lollipop MR1 (API level 22).
Help of "Android official pm" see <https://developer.android.com/tools/adb#pm>
Grant a permission to an app. On devices running Android 6.0 (API level 23) and higher,
the permission can be any permission declared in the app manifest.
On devices running Android 5.1 (API level 22) and lower,
must be an optional permission defined by the app.
"""
device_api_version = self.shell(['getprop', 'ro.build.version.sdk']).output
device_api_version = device_api_version.rstrip("\n")
output, _ = self.shell(['dumpsys', 'package', f'{package_name}'])

group_pattern = re.compile(r'^(\s*' + 'runtime' + r' permissions:[\s\S]+)', re.MULTILINE)
group_matcher = group_pattern.search(output)
if not group_pattern:
return False
group_match = group_matcher.group(1)
lines = group_match.split("\n")
if len(lines) < 2:
return False
title_indent = len(lines[0]) - len(lines[0].lstrip())
if device_api_version is None or int(device_api_version) < 23:
print('Skipping permissions grant option,only target api greater or equal to 23 support')
return True
for i in range(1, len(lines)):
line = lines[i]
current_indent = len(line) - len(line.lstrip())

if current_indent <= title_indent:
break

permission_name_pattern = re.compile(r'android\.\w*\.?permission\.\w+')
permission_name_matcher = permission_name_pattern.search(line)

if not permission_name_matcher:
continue
else:
permission_name = permission_name_matcher.group()
print(f'auto grant permission {permission_name}')
self.shell(['pm', 'grant', package_name, permission_name])
return True
sdk_version_output = self.shell(['getprop', 'ro.build.version.sdk']).output.strip()
sdk_version = int(sdk_version_output) if sdk_version_output.isdigit() else None
if sdk_version is None:
logger.warning("can't get sdk version")
return
if sdk_version < 23:
# TODO: support android 5.1 (API 22) and lower
logger.warning("auto grant permissions only support android 6.0+ (API 23+)")
return

dumpsys_package_output = self.shell(['dumpsys', 'package', package_name]).output
target_sdk_match = re.search(r'targetSdk=(\d+)', dumpsys_package_output)
if not target_sdk_match:
logger.warning("can't get targetSdk from dumpsys package")
return
target_sdk = int(target_sdk_match.group(1))
if target_sdk < 22:
logger.warning("auto grant permissions only support app targetSdk >= 22")
return

permissions = re.findall(r'(android\.\w*\.?permission\.\w+): granted=false', dumpsys_package_output)
for permission in permissions:
self.shell(['pm', 'grant', package_name, permission])
logger.info(f'auto grant permission {permission}')


class _DeprecatedMixIn:
Expand Down

0 comments on commit 42b4457

Please sign in to comment.