diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 9151e4e1779a3..d78fbdb53d8d3 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -3815,15 +3815,18 @@ public class NotificationManagerService extends SystemService { enforceDeletingChannelHasNoFgService(pkg, callingUser, channelId); cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, callingUser, REASON_CHANNEL_REMOVED, null); - mPreferencesHelper.deleteNotificationChannel(pkg, callingUid, channelId); - // Remove from both recent notification archive and notification history - mArchive.removeChannelNotifications(pkg, callingUser, channelId); - mHistoryManager.deleteNotificationChannel(pkg, callingUid, channelId); - mListeners.notifyNotificationChannelChanged(pkg, - UserHandle.getUserHandleForUid(callingUid), - mPreferencesHelper.getNotificationChannel(pkg, callingUid, channelId, true), - NOTIFICATION_CHANNEL_OR_GROUP_DELETED); - handleSavePolicyFile(); + boolean previouslyExisted = mPreferencesHelper.deleteNotificationChannel( + pkg, callingUid, channelId); + if (previouslyExisted) { + // Remove from both recent notification archive and notification history + mArchive.removeChannelNotifications(pkg, callingUser, channelId); + mHistoryManager.deleteNotificationChannel(pkg, callingUid, channelId); + mListeners.notifyNotificationChannelChanged(pkg, + UserHandle.getUserHandleForUid(callingUid), + mPreferencesHelper.getNotificationChannel(pkg, callingUid, channelId, true), + NOTIFICATION_CHANNEL_OR_GROUP_DELETED); + handleSavePolicyFile(); + } } @Override diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java index 03676b5556d97..96bde3df1e685 100644 --- a/services/core/java/com/android/server/notification/PreferencesHelper.java +++ b/services/core/java/com/android/server/notification/PreferencesHelper.java @@ -1126,20 +1126,21 @@ public class PreferencesHelper implements RankingConfig { } @Override - public void deleteNotificationChannel(String pkg, int uid, String channelId) { + public boolean deleteNotificationChannel(String pkg, int uid, String channelId) { synchronized (mPackagePreferences) { PackagePreferences r = getPackagePreferencesLocked(pkg, uid); if (r == null) { - return; + return false; } NotificationChannel channel = r.channels.get(channelId); if (channel != null) { - deleteNotificationChannelLocked(channel, pkg, uid); + return deleteNotificationChannelLocked(channel, pkg, uid); } + return false; } } - private void deleteNotificationChannelLocked(NotificationChannel channel, String pkg, int uid) { + private boolean deleteNotificationChannelLocked(NotificationChannel channel, String pkg, int uid) { if (!channel.isDeleted()) { channel.setDeleted(true); channel.setDeletedTimeMs(System.currentTimeMillis()); @@ -1151,7 +1152,9 @@ public class PreferencesHelper implements RankingConfig { if (mAreChannelsBypassingDnd && channel.canBypassDnd()) { updateChannelsBypassingDnd(); } + return true; } + return false; } @Override diff --git a/services/core/java/com/android/server/notification/RankingConfig.java b/services/core/java/com/android/server/notification/RankingConfig.java index b1d654671c2d7..398259333e166 100644 --- a/services/core/java/com/android/server/notification/RankingConfig.java +++ b/services/core/java/com/android/server/notification/RankingConfig.java @@ -53,7 +53,7 @@ public interface RankingConfig { NotificationChannel getConversationNotificationChannel(String pkg, int uid, String channelId, String conversationId, boolean returnParentIfNoConversationChannel, boolean includeDeleted); - void deleteNotificationChannel(String pkg, int uid, String channelId); + boolean deleteNotificationChannel(String pkg, int uid, String channelId); void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId); void permanentlyDeleteNotificationChannels(String pkg, int uid); ParceledListSlice getNotificationChannels(String pkg, int uid, diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index a522b5c6161e9..1f543a17885a8 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -94,6 +94,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import android.app.ActivityManager; @@ -2425,6 +2426,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean())) .thenReturn(mTestNotificationChannel); + when(mPreferencesHelper.deleteNotificationChannel(eq(PKG), anyInt(), + eq(mTestNotificationChannel.getId()))).thenReturn(true); reset(mListeners); mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), @@ -2432,6 +2435,22 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); } + @Test + public void testDeleteChannelOnlyDoExtraWorkIfExisted() throws Exception { + List associations = new ArrayList<>(); + associations.add("a"); + when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) + .thenReturn(associations); + mService.setPreferencesHelper(mPreferencesHelper); + when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), + eq(mTestNotificationChannel.getId()), anyBoolean())) + .thenReturn(null); + reset(mListeners); + mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); + verifyNoMoreInteractions(mListeners); + verifyNoMoreInteractions(mHistoryManager); + } + @Test public void testDeleteChannelGroupNotifyListener() throws Exception { List associations = new ArrayList<>(); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java index 3a51ff2fb5fa0..23da02c0e4a14 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java @@ -3332,6 +3332,17 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertNull(mHelper.getNotificationChannel(PKG_O, UID_O, "id", true)); } + @Test + public void testDeleted_twice() throws Exception { + mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger, + mAppOpsManager, mStatsEventBuilderFactory); + + mHelper.createNotificationChannel( + PKG_P, UID_P, createNotificationChannel("id", "id", 2), true, false); + assertTrue(mHelper.deleteNotificationChannel(PKG_P, UID_P, "id")); + assertFalse(mHelper.deleteNotificationChannel(PKG_P, UID_P, "id")); + } + @Test public void testDeleted_recentTime() throws Exception { mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger,