From efae2e8c3230d93ebb020b41880a2d7e93e18747 Mon Sep 17 00:00:00 2001 From: muendelezaji Date: Tue, 26 Jul 2016 16:18:02 +0100 Subject: [PATCH] Android permission grant for API 23 and above Issue: On some devices, _grant_requested_permissions may throw an error when trying to grant permissions that were already granted at install time using 'adb install -g'. Fix: Surround permission grant method for API 23+ with try/except Issue: Currently, _grant_requested_permissions skips all lines after the first line not starting with 'android.permission'. This causes a problem for apps where some required permissions appear after a non-matching line e.g. Google Slides. Fix: Don't break on non-matching line until end of section is reached --- wlauto/common/android/workload.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/wlauto/common/android/workload.py b/wlauto/common/android/workload.py index f784946a..b1fdfeaa 100644 --- a/wlauto/common/android/workload.py +++ b/wlauto/common/android/workload.py @@ -23,7 +23,7 @@ from wlauto.core.workload import Workload from wlauto.core.resource import NO_ONE from wlauto.common.android.resources import ApkFile from wlauto.common.resources import ExtensionAsset, Executable -from wlauto.exceptions import WorkloadError, ResourceError, ConfigError +from wlauto.exceptions import WorkloadError, ResourceError, ConfigError, DeviceError from wlauto.utils.android import ApkInfo, ANDROID_NORMAL_PERMISSIONS from wlauto.utils.types import boolean from wlauto.utils.revent import ReventParser @@ -289,14 +289,24 @@ class ApkWorkload(Workload): for line in lines: if "android.permission." in line: permissions.append(line.split(":")[0].strip()) - else: + # Matching either of these means the end of requested permissions section + elif "install permissions:" in line or "runtime permissions:" in line: break - for permission in permissions: + for permission in set(permissions): # "Normal" Permisions are automatically granted and cannot be changed permission_name = permission.rsplit('.', 1)[1] if permission_name not in ANDROID_NORMAL_PERMISSIONS: - self.device.execute("pm grant {} {}".format(self.package, permission)) + # On some API 23+ devices, this may fail with a SecurityException + # on previously granted permissions. In that case, just skip as it + # is not fatal to the workload execution + try: + self.device.execute("pm grant {} {}".format(self.package, permission)) + except DeviceError as e: + if "not a changeable permission" in e.message: + self.logger.debug(e) + else: + raise e def do_post_install(self, context): """ May be overwritten by derived classes."""