From 2a67ee89556e721ae9e325c4d523e576af2b9a3f Mon Sep 17 00:00:00 2001 From: Svetoslav Ganov Date: Thu, 16 Feb 2017 18:57:57 -0800 Subject: [PATCH] The system can post notifications for any package The platform is able to post notifications on behalf of any app and muting the notifications for this app should properly work by attributing the notification not to the system but to the app on whose behalf the notication is posted. Test: manual bug:34966823 Change-Id: I1dd022dee166b1c159e0ad68dd4876b510fc8a26 --- .../NotificationManagerService.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index f2b55640f20fd..86d48ef3f3b80 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -3044,6 +3044,10 @@ public class NotificationManagerService extends SystemService { throw new IllegalArgumentException("null not allowed: pkg=" + pkg + " id=" + id + " notification=" + notification); } + + // The system can post notifications for any package, let us resolve that. + final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); + // Fix the notification as best we can. try { final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( @@ -3063,13 +3067,13 @@ public class NotificationManagerService extends SystemService { channelId = (new Notification.TvExtender(notification)).getChannel(); } final NotificationChannel channel = mRankingHelper.getNotificationChannelWithFallback(pkg, - callingUid, channelId, false /* includeDeleted */); + notificationUid, channelId, false /* includeDeleted */); final StatusBarNotification n = new StatusBarNotification( - pkg, opPkg, id, tag, callingUid, callingPid, notification, + pkg, opPkg, id, tag, notificationUid, callingPid, notification, user, null, System.currentTimeMillis()); final NotificationRecord r = new NotificationRecord(getContext(), n, channel); - if (!checkDisqualifyingFeatures(userId, callingUid, id,tag, r)) { + if (!checkDisqualifyingFeatures(userId, notificationUid, id,tag, r)) { return; } @@ -3095,6 +3099,19 @@ public class NotificationManagerService extends SystemService { idOut[0] = id; } + private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { + // The system can post notifications on behalf of any package it wants + if (isCallerSystem() && opPackageName != null && !"android".equals(opPackageName)) { + try { + return getContext().getPackageManager() + .getPackageUidAsUser(opPackageName, userId); + } catch (NameNotFoundException e) { + /* ignore */ + } + } + return callingUid; + } + /** * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. *