From 3118ac44a3ca390eedff8a38c5b5f9a628052246 Mon Sep 17 00:00:00 2001 From: Yuri Lin Date: Tue, 18 Jan 2022 16:22:28 -0500 Subject: [PATCH] Output FGS notification permission state to log. This field is populated on creation of the ServiceRecord as well as whenever the ServiceRecord is asked to post or update a notification. Confirmed manually via statsd_testdrive, by turning on and off app notification permissions and then using play store app installs to trigger foreground services. Bug: 194833441 Bug: 214065327 Test: TreeHugger, statsd_testdrive Change-Id: I9fcf9950dfb93114f96eb11eaa73b89c98ae5ed3 --- .../com/android/server/am/ActiveServices.java | 3 ++- .../com/android/server/am/ServiceRecord.java | 24 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 7993936cd5687..164dc5d1fc9fa 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -6794,7 +6794,8 @@ public final class ActiveServices { r.mFgsNotificationShown, durationMs, r.mStartForegroundCount, - ActivityManagerUtils.hashComponentNameForAtom(r.shortInstanceName)); + ActivityManagerUtils.hashComponentNameForAtom(r.shortInstanceName), + r.mFgsHasNotificationPermission); } boolean canAllowWhileInUsePermissionInFgsLocked(int callingPid, int callingUid, diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java index b3e46cd0b5269..9b32e61763f36 100644 --- a/services/core/java/com/android/server/am/ServiceRecord.java +++ b/services/core/java/com/android/server/am/ServiceRecord.java @@ -175,7 +175,6 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN // FGS notification was shown before the FGS finishes, or it wasn't deferred in the first place. boolean mFgsNotificationShown; // Whether FGS package has permissions to show notifications. - // TODO(b/194833441): Output this field to logs in ActiveServices#logFGSStateChangeLocked. boolean mFgsHasNotificationPermission; // allow the service becomes foreground service? Service started from background may not be @@ -593,6 +592,10 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN userId = UserHandle.getUserId(appInfo.uid); createdFromFg = callerIsFg; updateKeepWarmLocked(); + // initialize notification permission state; this'll be updated whenever there's an attempt + // to post or update a notification, but that doesn't cover the time before the first + // notification + updateFgsHasNotificationPermission(); } public ServiceState getTracker() { @@ -950,6 +953,25 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN return lastStartId; } + private void updateFgsHasNotificationPermission() { + // Do asynchronous communication with notification manager to avoid deadlocks. + final String localPackageName = packageName; + final int appUid = appInfo.uid; + + ams.mHandler.post(new Runnable() { + public void run() { + NotificationManagerInternal nm = LocalServices.getService( + NotificationManagerInternal.class); + if (nm == null) { + return; + } + // Record whether the package has permission to notify the user + mFgsHasNotificationPermission = nm.areNotificationsEnabledForPackage( + localPackageName, appUid); + } + }); + } + public void postNotification() { if (isForeground && foregroundNoti != null && app != null) { final int appUid = appInfo.uid;