From 4990af744b3d7d5dd052e5de54a5858b1cc140d0 Mon Sep 17 00:00:00 2001 From: Jing Ji Date: Thu, 11 May 2023 05:21:36 -0700 Subject: [PATCH] Allow the FGS with soft denied permission to start ..if it's allowed to have while-in-use capability. Permissions like mic/camera/location would have a soft-denied mode, where it basically means "MODE_IGNORED", we shouldn't throw exceptions in this case but silently fail. But if they have the while-in-use capability, it should be safe to allow them to start the FGS. Bug: 280936347 Test: atest CtsAppFgsTestCases Change-Id: I2e690f9dd9574f9ce0d3e7cb702eb53dced99a37 --- .../android/app/ForegroundServiceTypePolicy.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/java/android/app/ForegroundServiceTypePolicy.java b/core/java/android/app/ForegroundServiceTypePolicy.java index c0c59a24dd8da..19b31417e789a 100644 --- a/core/java/android/app/ForegroundServiceTypePolicy.java +++ b/core/java/android/app/ForegroundServiceTypePolicy.java @@ -18,6 +18,7 @@ package android.app; import static android.app.AppOpsManager.MODE_ALLOWED; import static android.app.AppOpsManager.MODE_FOREGROUND; +import static android.app.AppOpsManager.MODE_IGNORED; import static android.content.pm.PackageManager.PERMISSION_DENIED; import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; @@ -1078,9 +1079,9 @@ public abstract class ForegroundServiceTypePolicy { int checkPermission(@NonNull Context context, @NonNull String name, int callerUid, int callerPid, String packageName, boolean allowWhileInUse) { // Simple case, check if it's already granted. - @PackageManager.PermissionResult int result; - if ((result = PermissionChecker.checkPermissionForPreflight(context, name, - callerPid, callerUid, packageName)) == PERMISSION_GRANTED) { + @PermissionCheckerManager.PermissionResult int result; + if ((result = PermissionChecker.checkPermissionForPreflight(context, name, callerPid, + callerUid, packageName)) == PermissionCheckerManager.PERMISSION_GRANTED) { return PERMISSION_GRANTED; } if (allowWhileInUse && result == PermissionCheckerManager.PERMISSION_SOFT_DENIED) { @@ -1093,6 +1094,13 @@ public abstract class ForegroundServiceTypePolicy { if (currentMode == MODE_FOREGROUND) { // It's in foreground only mode and we're allowing while-in-use. return PERMISSION_GRANTED; + } else if (currentMode == MODE_IGNORED) { + // If it's soft denied with the mode "ignore", semantically it's a silent + // failure and no exception should be thrown, we might not want to allow + // the FGS. However, since the user has agreed with this permission + // (otherwise it's going to be a hard denial), and we're allowing + // while-in-use here, it's safe to allow the FGS run here. + return PERMISSION_GRANTED; } } }