From 423b9fc83d736326b8ddadbfcd6fd5f181bf9fec Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Wed, 9 Nov 2016 09:51:08 -0500 Subject: [PATCH] Move notification channel from record to sbn. So notificationlisteners know about it. Test: runtest systemui-notification Change-Id: I28fc86730d3f7ad246a0164f44157594f57430c0 --- api/current.txt | 3 +- api/system-current.txt | 3 +- api/test-current.txt | 3 +- .../notification/StatusBarNotification.java | 32 ++++++++++++------- .../statusbar/phone/PhoneStatusBar.java | 4 +-- .../NotificationManagerService.java | 23 +++++++------ .../notification/NotificationRecord.java | 14 +++----- .../notification/BuzzBeepBlinkTest.java | 10 +++--- .../server/notification/GroupHelperTest.java | 6 +++- .../notification/ImportanceExtractorTest.java | 6 ++-- .../notification/RankingHelperTest.java | 20 ++++++------ .../server/notification/SnoozeHelperTest.java | 10 ++++-- 12 files changed, 74 insertions(+), 60 deletions(-) diff --git a/api/current.txt b/api/current.txt index cd6ca4220c37d..0443d5944fb48 100644 --- a/api/current.txt +++ b/api/current.txt @@ -34985,7 +34985,7 @@ package android.service.notification { } public class StatusBarNotification implements android.os.Parcelable { - ctor public StatusBarNotification(java.lang.String, java.lang.String, int, java.lang.String, int, int, int, android.app.Notification, android.os.UserHandle, long); + ctor public deprecated StatusBarNotification(java.lang.String, java.lang.String, int, java.lang.String, int, int, int, android.app.Notification, android.os.UserHandle, long); ctor public StatusBarNotification(android.os.Parcel); method public android.service.notification.StatusBarNotification clone(); method public int describeContents(); @@ -34993,6 +34993,7 @@ package android.service.notification { method public int getId(); method public java.lang.String getKey(); method public android.app.Notification getNotification(); + method public android.app.NotificationChannel getNotificationChannel(); method public java.lang.String getOverrideGroupKey(); method public java.lang.String getPackageName(); method public long getPostTime(); diff --git a/api/system-current.txt b/api/system-current.txt index 1abc265c811d2..d9c9dffd1c665 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -37784,7 +37784,7 @@ package android.service.notification { } public class StatusBarNotification implements android.os.Parcelable { - ctor public StatusBarNotification(java.lang.String, java.lang.String, int, java.lang.String, int, int, int, android.app.Notification, android.os.UserHandle, long); + ctor public deprecated StatusBarNotification(java.lang.String, java.lang.String, int, java.lang.String, int, int, int, android.app.Notification, android.os.UserHandle, long); ctor public StatusBarNotification(android.os.Parcel); method public android.service.notification.StatusBarNotification clone(); method public int describeContents(); @@ -37792,6 +37792,7 @@ package android.service.notification { method public int getId(); method public java.lang.String getKey(); method public android.app.Notification getNotification(); + method public android.app.NotificationChannel getNotificationChannel(); method public java.lang.String getOverrideGroupKey(); method public java.lang.String getPackageName(); method public long getPostTime(); diff --git a/api/test-current.txt b/api/test-current.txt index b79a92fae68d9..37c5671c8daf7 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -35074,7 +35074,7 @@ package android.service.notification { } public class StatusBarNotification implements android.os.Parcelable { - ctor public StatusBarNotification(java.lang.String, java.lang.String, int, java.lang.String, int, int, int, android.app.Notification, android.os.UserHandle, long); + ctor public deprecated StatusBarNotification(java.lang.String, java.lang.String, int, java.lang.String, int, int, int, android.app.Notification, android.os.UserHandle, long); ctor public StatusBarNotification(android.os.Parcel); method public android.service.notification.StatusBarNotification clone(); method public int describeContents(); @@ -35082,6 +35082,7 @@ package android.service.notification { method public int getId(); method public java.lang.String getKey(); method public android.app.Notification getNotification(); + method public android.app.NotificationChannel getNotificationChannel(); method public java.lang.String getOverrideGroupKey(); method public java.lang.String getPackageName(); method public long getPostTime(); diff --git a/core/java/android/service/notification/StatusBarNotification.java b/core/java/android/service/notification/StatusBarNotification.java index be0b47cc58ac4..dfb6b86399120 100644 --- a/core/java/android/service/notification/StatusBarNotification.java +++ b/core/java/android/service/notification/StatusBarNotification.java @@ -17,6 +17,7 @@ package android.service.notification; import android.app.Notification; +import android.app.NotificationChannel; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -42,25 +43,21 @@ public class StatusBarNotification implements Parcelable { private final Notification notification; private final UserHandle user; private final long postTime; + private final NotificationChannel channel; private Context mContext; // used for inflation & icon expansion /** @hide */ - public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, - int initialPid, int score, Notification notification, UserHandle user) { - this(pkg, opPkg, id, tag, uid, initialPid, score, notification, user, - System.currentTimeMillis()); - } - - /** @hide */ - public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, - int initialPid, Notification notification, UserHandle user, String overrideGroupKey, - long postTime) { + public StatusBarNotification(String pkg, String opPkg, NotificationChannel channel, int id, + String tag, int uid, int initialPid, Notification notification, UserHandle user, + String overrideGroupKey, long postTime) { if (pkg == null) throw new NullPointerException(); if (notification == null) throw new NullPointerException(); + if (channel == null) throw new IllegalArgumentException(); this.pkg = pkg; this.opPkg = opPkg; + this.channel = channel; this.id = id; this.tag = tag; this.uid = uid; @@ -73,6 +70,7 @@ public class StatusBarNotification implements Parcelable { this.groupKey = groupKey(); } + @Deprecated public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, int initialPid, int score, Notification notification, UserHandle user, long postTime) { @@ -90,6 +88,7 @@ public class StatusBarNotification implements Parcelable { this.postTime = postTime; this.key = key(); this.groupKey = groupKey(); + this.channel = null; } public StatusBarNotification(Parcel in) { @@ -113,6 +112,7 @@ public class StatusBarNotification implements Parcelable { } this.key = key(); this.groupKey = groupKey(); + this.channel = NotificationChannel.CREATOR.createFromParcel(in); } private String key() { @@ -182,6 +182,7 @@ public class StatusBarNotification implements Parcelable { } else { out.writeInt(0); } + this.channel.writeToParcel(out, flags); } public int describeContents() { @@ -208,14 +209,14 @@ public class StatusBarNotification implements Parcelable { public StatusBarNotification cloneLight() { final Notification no = new Notification(); this.notification.cloneInto(no, false); // light copy - return new StatusBarNotification(this.pkg, this.opPkg, + return new StatusBarNotification(this.pkg, this.opPkg, this.channel, this.id, this.tag, this.uid, this.initialPid, no, this.user, this.overrideGroupKey, this.postTime); } @Override public StatusBarNotification clone() { - return new StatusBarNotification(this.pkg, this.opPkg, + return new StatusBarNotification(this.pkg, this.opPkg, this.channel, this.id, this.tag, this.uid, this.initialPid, this.notification.clone(), this.user, this.overrideGroupKey, this.postTime); } @@ -334,6 +335,13 @@ public class StatusBarNotification implements Parcelable { return overrideGroupKey; } + /** + * Returns the channel this notification was posted to. + */ + public NotificationChannel getNotificationChannel() { + return channel; + } + /** * @hide */ diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java index a9aa2edd1780a..599fcba068e0c 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java @@ -1649,9 +1649,9 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode, newNotification.headsUpContentView = sbn.getNotification().headsUpContentView; StatusBarNotification newSbn = new StatusBarNotification(sbn.getPackageName(), - sbn.getOpPkg(), + sbn.getOpPkg(), sbn.getNotificationChannel(), sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), - 0, newNotification, sbn.getUser(), sbn.getPostTime()); + newNotification, sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); updateNotification(newSbn, null); mKeysKeptForRemoteInput.add(entry.key); diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index ac0003c521aa0..df5d6e73ce815 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -1671,10 +1671,10 @@ public class NotificationManagerService extends SystemService { final StatusBarNotification sbnOut = new StatusBarNotification( sbn.getPackageName(), sbn.getOpPkg(), + sbn.getNotificationChannel(), sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), - 0, // hide score from apps sbn.getNotification().clone(), - sbn.getUser(), sbn.getPostTime()); + sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); list.add(sbnOut); } } @@ -2467,15 +2467,14 @@ public class NotificationManagerService extends SystemService { } final StatusBarNotification summarySbn = new StatusBarNotification(adjustedSbn.getPackageName(), - adjustedSbn.getOpPkg(), Integer.MAX_VALUE, + adjustedSbn.getOpPkg(), + adjustedSbn.getNotificationChannel(), + Integer.MAX_VALUE, GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), adjustedSbn.getInitialPid(), summaryNotification, adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, System.currentTimeMillis()); - summaryRecord = new NotificationRecord(getContext(), summarySbn, - mRankingHelper.getNotificationChannel(adjustedSbn.getPackageName(), - adjustedSbn.getUid(), - adjustedSbn.getNotification().getNotificationChannel())); + summaryRecord = new NotificationRecord(getContext(), summarySbn); summaries.put(pkg, summarySbn.getKey()); } } @@ -2722,9 +2721,11 @@ public class NotificationManagerService extends SystemService { throw new IllegalArgumentException("null not allowed: pkg=" + pkg + " id=" + id + " notification=" + notification); } + final NotificationChannel channel = mRankingHelper.getNotificationChannelWithFallback(pkg, + callingUid, notification.getNotificationChannel()); final StatusBarNotification n = new StatusBarNotification( - pkg, opPkg, id, tag, callingUid, callingPid, 0, notification, - user); + pkg, opPkg, channel, id, tag, callingUid, callingPid, notification, + user, null, System.currentTimeMillis()); // Limit the number of notifications that any given package except the android // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. @@ -2787,9 +2788,7 @@ public class NotificationManagerService extends SystemService { Notification.PRIORITY_MAX); // setup local book-keeping - final NotificationRecord r = new NotificationRecord(getContext(), n, - mRankingHelper.getNotificationChannelWithFallback(pkg, callingUid, - n.getNotification().getNotificationChannel())); + final NotificationRecord r = new NotificationRecord(getContext(), n); mHandler.post(new EnqueueNotificationRunnable(userId, r)); idOut[0] = id; diff --git a/services/core/java/com/android/server/notification/NotificationRecord.java b/services/core/java/com/android/server/notification/NotificationRecord.java index a1256db61cba7..965257c873e9c 100644 --- a/services/core/java/com/android/server/notification/NotificationRecord.java +++ b/services/core/java/com/android/server/notification/NotificationRecord.java @@ -101,11 +101,8 @@ public final class NotificationRecord { private String mUserExplanation; private String mPeopleExplanation; - private NotificationChannel mNotificationChannel; - @VisibleForTesting - public NotificationRecord(Context context, StatusBarNotification sbn, - NotificationChannel channel) + public NotificationRecord(Context context, StatusBarNotification sbn) { this.sbn = sbn; mOriginalFlags = sbn.getNotification().flags; @@ -114,7 +111,6 @@ public final class NotificationRecord { mUpdateTimeMs = mCreationTimeMs; mContext = context; stats = new NotificationUsageStats.SingleNotificationStats(); - mNotificationChannel = channel; mImportance = defaultImportance(); } @@ -148,8 +144,8 @@ public final class NotificationRecord { || (n.defaults & Notification.DEFAULT_VIBRATE) != 0 || n.sound != null || n.vibrate != null - || mNotificationChannel.shouldVibrate() - || mNotificationChannel.getRingtone() != null; + || sbn.getNotificationChannel().shouldVibrate() + || sbn.getNotificationChannel().getRingtone() != null; stats.isNoisy = isNoisy; if (!isNoisy && importance > IMPORTANCE_LOW) { @@ -287,7 +283,7 @@ public final class NotificationRecord { pw.println(prefix + " mVisibleSinceMs=" + mVisibleSinceMs); pw.println(prefix + " mUpdateTimeMs=" + mUpdateTimeMs); pw.println(prefix + " mSuppressedVisualEffects= " + mSuppressedVisualEffects); - pw.println(prefix + " mNotificationChannel= " + mNotificationChannel); + pw.println(prefix + " notificationChannel= " + notification.getNotificationChannel()); } @@ -535,6 +531,6 @@ public final class NotificationRecord { } public NotificationChannel getChannel() { - return mNotificationChannel; + return sbn.getNotificationChannel(); } } diff --git a/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java b/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java index 4570a4b432a8d..d20e351157cb2 100644 --- a/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java +++ b/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java @@ -226,11 +226,11 @@ public class BuzzBeepBlinkTest { if (insistent) { n.flags |= Notification.FLAG_INSISTENT; } - StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, id, mTag, mUid, mPid, - mScore, n, mUser, System.currentTimeMillis()); - NotificationRecord r = new NotificationRecord(getContext(), sbn, - new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "misc", - NotificationManager.IMPORTANCE_DEFAULT)); + NotificationChannel channel = + new NotificationChannel("test", "test", NotificationManager.IMPORTANCE_HIGH); + StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, channel, id, mTag, mUid, + mPid, n, mUser, null, System.currentTimeMillis()); + NotificationRecord r = new NotificationRecord(getContext(), sbn); mService.addNotification(r); return r; } diff --git a/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java b/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java index 22b674b93b09e..6c3f44778ae64 100644 --- a/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java @@ -31,6 +31,7 @@ import org.mockito.MockitoAnnotations; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.os.UserHandle; @@ -68,7 +69,10 @@ public class GroupHelperTest { if (groupKey != null) { nb.setGroup(groupKey); } - return new StatusBarNotification(pkg, pkg, id, tag, 0, 0, 0, nb.build(), user); + NotificationChannel channel = + new NotificationChannel("test", "test", NotificationManager.IMPORTANCE_LOW); + return new StatusBarNotification(pkg, pkg, channel, id, tag, 0, 0, nb.build(), user, null, + System.currentTimeMillis()); } private StatusBarNotification getSbn(String pkg, int id, String tag, diff --git a/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java b/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java index 305b5e0acd84f..6bc96754e200d 100644 --- a/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java +++ b/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java @@ -69,9 +69,9 @@ public class ImportanceExtractorTest { .setDefaults(Notification.DEFAULT_SOUND); Notification n = builder.build(); - StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid, - mPid, mScore, n, mUser, System.currentTimeMillis()); - NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); + StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, channel, mId, mTag, mUid, + mPid, n, mUser, null, System.currentTimeMillis()); + NotificationRecord r = new NotificationRecord(getContext(), sbn); return r; } diff --git a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java index 629146f190061..92c67b5b0aa90 100644 --- a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java @@ -98,8 +98,8 @@ public class RankingHelperTest { .setWhen(1205) .build(); mRecordGroupGSortA = new NotificationRecord(getContext(), new StatusBarNotification( - "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortA, user), - getDefaultChannel()); + "package", "package", getDefaultChannel(), 1, null, 0, 0, mNotiGroupGSortA, user, + null, System.currentTimeMillis())); mNotiGroupGSortB = new Notification.Builder(getContext()) .setContentTitle("B") @@ -108,24 +108,24 @@ public class RankingHelperTest { .setWhen(1200) .build(); mRecordGroupGSortB = new NotificationRecord(getContext(), new StatusBarNotification( - "package", "package", 1, null, 0, 0, 0, mNotiGroupGSortB, user), - getDefaultChannel()); + "package", "package", getDefaultChannel(), 1, null, 0, 0, mNotiGroupGSortB, user, + null, System.currentTimeMillis())); mNotiNoGroup = new Notification.Builder(getContext()) .setContentTitle("C") .setWhen(1201) .build(); mRecordNoGroup = new NotificationRecord(getContext(), new StatusBarNotification( - "package", "package", 1, null, 0, 0, 0, mNotiNoGroup, user), - getDefaultChannel()); + "package", "package", getDefaultChannel(), 1, null, 0, 0, mNotiNoGroup, user, + null, System.currentTimeMillis())); mNotiNoGroup2 = new Notification.Builder(getContext()) .setContentTitle("D") .setWhen(1202) .build(); mRecordNoGroup2 = new NotificationRecord(getContext(), new StatusBarNotification( - "package", "package", 1, null, 0, 0, 0, mNotiNoGroup2, user), - getDefaultChannel()); + "package", "package", getDefaultChannel(), 1, null, 0, 0, mNotiNoGroup2, user, + null, System.currentTimeMillis())); mNotiNoGroupSortA = new Notification.Builder(getContext()) .setContentTitle("E") @@ -133,8 +133,8 @@ public class RankingHelperTest { .setSortKey("A") .build(); mRecordNoGroupSortA = new NotificationRecord(getContext(), new StatusBarNotification( - "package", "package", 1, null, 0, 0, 0, mNotiNoGroupSortA, user), - getDefaultChannel()); + "package", "package", getDefaultChannel(), 1, null, 0, 0, mNotiNoGroupSortA, user, + null, System.currentTimeMillis())); final ApplicationInfo legacy = new ApplicationInfo(); legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; diff --git a/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java b/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java index ffc45ea96e91e..7a3ee7f4c0689 100644 --- a/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java @@ -178,9 +178,13 @@ public class SnoozeHelperTest { .setWhen(1205) .build(); return new NotificationRecord(getContext(), new StatusBarNotification( - pkg, pkg, id, tag, 0, 0, 0, n, user), - new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name", - NotificationManager.IMPORTANCE_HIGH)); + pkg, pkg, getDefaultChannel(), id, tag, 0, 0, n, user, null, + System.currentTimeMillis())); + } + + private NotificationChannel getDefaultChannel() { + return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name", + NotificationManager.IMPORTANCE_LOW); } }