diff --git a/services/core/java/com/android/server/notification/RankingHelper.java b/services/core/java/com/android/server/notification/RankingHelper.java index 32de3166faae8..6d18c8393dd47 100644 --- a/services/core/java/com/android/server/notification/RankingHelper.java +++ b/services/core/java/com/android/server/notification/RankingHelper.java @@ -33,7 +33,6 @@ import android.content.pm.ParceledListSlice; import android.metrics.LogMaker; import android.os.Build; import android.os.UserHandle; -import android.provider.Settings; import android.service.notification.NotificationListenerService.Ranking; import android.text.TextUtils; import android.util.ArrayMap; @@ -508,6 +507,14 @@ public class RankingHelper implements RankingConfig { updateConfig(); } + int getPackagePriority(String pkg, int uid) { + return getOrCreateRecord(pkg, uid).priority; + } + + int getPackageVisibility(String pkg, int uid) { + return getOrCreateRecord(pkg, uid).visibility; + } + @Override public void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, boolean fromTargetApp) { @@ -608,6 +615,16 @@ public class RankingHelper implements RankingConfig { } r.channels.put(updatedChannel.getId(), updatedChannel); + if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) { + // copy settings to app level so they are inherited by new channels + // when the app migrates + r.importance = updatedChannel.getImportance(); + r.priority = updatedChannel.canBypassDnd() + ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT; + r.visibility = updatedChannel.getLockscreenVisibility(); + r.showBadge = updatedChannel.canShowBadge(); + } + MetricsLogger.action(getChannelLog(updatedChannel, pkg)); updateConfig(); } 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 bc5c29d514a55..ce899e31d7b6b 100644 --- a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java @@ -19,6 +19,7 @@ import static android.app.NotificationManager.IMPORTANCE_DEFAULT; import static android.app.NotificationManager.IMPORTANCE_HIGH; import static android.app.NotificationManager.IMPORTANCE_LOW; import static android.app.NotificationManager.IMPORTANCE_NONE; +import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.fail; @@ -48,6 +49,7 @@ import android.media.AudioAttributes; import android.net.Uri; import android.os.Build; import android.os.UserHandle; +import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; @@ -549,6 +551,56 @@ public class RankingHelperTest { assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false)); } + @Test + public void testUpdate_preUpgrade_updatesAppFields() throws Exception { + mHelper.setImportance(PKG, UID, IMPORTANCE_UNSPECIFIED); + assertTrue(mHelper.canShowBadge(PKG, UID)); + assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID)); + assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, + mHelper.getPackageVisibility(PKG, UID)); + + NotificationChannel defaultChannel = mHelper.getNotificationChannel( + PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false); + + defaultChannel.setShowBadge(false); + defaultChannel.setImportance(IMPORTANCE_NONE); + defaultChannel.setBypassDnd(true); + defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); + + mHelper.updateNotificationChannel(PKG, UID, defaultChannel); + + // ensure app level fields are changed + assertFalse(mHelper.canShowBadge(PKG, UID)); + assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG, UID)); + assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG, UID)); + assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG, UID)); + } + + @Test + public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception { + final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); + + mHelper.createNotificationChannel(PKG, UID, channel, false); + assertTrue(mHelper.canShowBadge(PKG, UID)); + assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID)); + assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, + mHelper.getPackageVisibility(PKG, UID)); + + channel.setShowBadge(false); + channel.setImportance(IMPORTANCE_NONE); + channel.setBypassDnd(true); + channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); + + mHelper.updateNotificationChannel(PKG, UID, channel); + + // ensure app level fields are not changed + assertTrue(mHelper.canShowBadge(PKG, UID)); + assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID)); + assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, + mHelper.getPackageVisibility(PKG, UID)); + assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID)); + } + @Test public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception { assertEquals(null, mHelper.getNotificationChannel(PKG, UID, "garbage", false));