From de93388430b25225033dbe7dc8f5d02a8c1307e5 Mon Sep 17 00:00:00 2001 From: Seungho Lee Date: Wed, 31 Oct 2018 21:49:09 +0900 Subject: [PATCH] Check if notification is valid before it finally vibrates because it can be canceled as soon as enqeued Test: Build and run StatusBarTest/_NotifyBuilder Test: Set notification sound and vibration Test: Make notification with + button, and cancel it with - button quickly TEst: Check notification not vibrating Change-Id: Ia21f45c165f863ed4143da3707b492c89e58387c Signed-off-by: Seungho Lee --- .../notification/NotificationManagerService.java | 13 +++++++++++-- .../server/notification/BuzzBeepBlinkTest.java | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 8bd6e547a73c4..d50fdfa9f61e9 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -5457,8 +5457,17 @@ public class NotificationManagerService extends SystemService { try { Thread.sleep(waitMs); } catch (InterruptedException e) { } - mVibrator.vibrate(record.sbn.getUid(), record.sbn.getPackageName(), - effect, "Notification (delayed)", record.getAudioAttributes()); + + // Notifications might be canceled before it actually vibrates due to waitMs, + // so need to check the notification still valide for vibrate. + synchronized (mNotificationLock) { + if (mNotificationsByKey.get(record.getKey()) != null) { + mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), + effect, "Notification (delayed)", record.getAudioAttributes()); + } else { + Slog.e(TAG, "No vibration for canceled notification : " + record.getKey()); + } + } }).start(); } else { mVibrator.vibrate(record.sbn.getUid(), record.sbn.getPackageName(), diff --git a/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java b/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java index afbe6bc21d0db..9d847514435e9 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java @@ -1014,6 +1014,22 @@ public class BuzzBeepBlinkTest extends UiServiceTestCase { assertEquals(-1, s.getLastAudiblyAlertedMs()); } + @Test + public void testCanceledNoisyNeverVibrate() throws Exception { + NotificationRecord r = getBuzzyBeepyNotification(); + + final int waitMs = mAudioManager.getFocusRampTimeMs( + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, + r.getAudioAttributes()); + + mService.buzzBeepBlinkLocked(r); + mService.clearNotifications(); + + verifyNeverVibrate(); + Thread.sleep(waitMs); + verifyNeverVibrate(); + } + @Test public void testEmptyUriSoundTreatedAsNoSound() throws Exception { NotificationChannel channel = new NotificationChannel("test", "test", IMPORTANCE_HIGH);