From 666ccf023f836690b986c0c4016b828fa8ffe203 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Mon, 18 Jun 2018 10:19:20 -0400 Subject: [PATCH] Fixes for enqueued adjustments - Accept adjustments that arrie too late - send through the normal 'apply adjustment to posted notification' flow - Apply adjustments that are received onEnqueue Test: runtest systemui-notification Bug: 110347047 Change-Id: Ic4c9839cf2f63ef1689d72eb291f6bfae8f0674b --- .../NotificationManagerService.java | 21 ++++++---- .../NotificationManagerServiceTest.java | 39 +++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 0897b8a52ad6a..32f17442d62b5 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -3408,20 +3408,27 @@ public class NotificationManagerService extends SystemService { @Override public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, - Adjustment adjustment) throws RemoteException { + Adjustment adjustment) { + boolean foundEnqueued = false; final long identity = Binder.clearCallingIdentity(); try { synchronized (mNotificationLock) { mAssistants.checkServiceTokenLocked(token); int N = mEnqueuedNotifications.size(); for (int i = 0; i < N; i++) { - final NotificationRecord n = mEnqueuedNotifications.get(i); - if (Objects.equals(adjustment.getKey(), n.getKey()) - && Objects.equals(adjustment.getUser(), n.getUserId())) { - applyAdjustment(n, adjustment); + final NotificationRecord r = mEnqueuedNotifications.get(i); + if (Objects.equals(adjustment.getKey(), r.getKey()) + && Objects.equals(adjustment.getUser(), r.getUserId())) { + applyAdjustment(r, adjustment); + r.applyAdjustments(); + foundEnqueued = true; break; } } + if (!foundEnqueued) { + // adjustment arrived too late to apply to enqueued; apply to posted + applyAdjustmentFromAssistant(token, adjustment); + } } } finally { Binder.restoreCallingIdentity(identity); @@ -3430,7 +3437,7 @@ public class NotificationManagerService extends SystemService { @Override public void applyAdjustmentFromAssistant(INotificationListener token, - Adjustment adjustment) throws RemoteException { + Adjustment adjustment) { final long identity = Binder.clearCallingIdentity(); try { synchronized (mNotificationLock) { @@ -3446,7 +3453,7 @@ public class NotificationManagerService extends SystemService { @Override public void applyAdjustmentsFromAssistant(INotificationListener token, - List adjustments) throws RemoteException { + List adjustments) { final long identity = Binder.clearCallingIdentity(); try { 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 3b2139045d223..96c948f2ff109 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -2404,6 +2404,45 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { verify(handler, timeout(300).times(1)).scheduleSendRankingUpdate(); } + @Test + public void testTooLateAdjustmentTriggersUpdate() throws Exception { + final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); + mService.addNotification(r); + NotificationManagerService.WorkerHandler handler = mock( + NotificationManagerService.WorkerHandler.class); + mService.setHandler(handler); + + Bundle signals = new Bundle(); + signals.putInt(Adjustment.KEY_USER_SENTIMENT, + NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE); + Adjustment adjustment = new Adjustment( + r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); + mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); + + waitForIdle(); + + verify(handler, timeout(300).times(1)).scheduleSendRankingUpdate(); + } + + @Test + public void testEnqueuedAdjustmentAppliesAdjustments() throws Exception { + final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); + mService.addEnqueuedNotification(r); + NotificationManagerService.WorkerHandler handler = mock( + NotificationManagerService.WorkerHandler.class); + mService.setHandler(handler); + + Bundle signals = new Bundle(); + signals.putInt(Adjustment.KEY_USER_SENTIMENT, + NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE); + Adjustment adjustment = new Adjustment( + r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); + mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); + + assertEquals(NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE, + r.getUserSentiment()); + } + @Test public void testRecents() throws Exception { Set expected = new HashSet<>();