From d673007affab954132c36651277c04bbdf5a5456 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Fri, 4 Jan 2019 12:52:52 -0500 Subject: [PATCH] Only recently updated notifications can alert Even if something in e.g. the contacts database has changed Test: atest Change-Id: I10eaa3d34574296d554eb8bae5900c79af773dcd Fixes: 117868600 --- .../NotificationManagerService.java | 4 +--- .../notification/NotificationRecord.java | 9 ++++++++- .../notification/NotificationRecordTest.java | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index c2c88258b0676..e70de7729a53d 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -5698,19 +5698,17 @@ public class NotificationManagerService extends SystemService { } int indexBefore = findNotificationRecordIndexLocked(record); boolean interceptBefore = record.isIntercepted(); - float contactAffinityBefore = record.getContactAffinity(); int visibilityBefore = record.getPackageVisibilityOverride(); recon.applyChangesLocked(record); applyZenModeLocked(record); mRankingHelper.sort(mNotificationList); int indexAfter = findNotificationRecordIndexLocked(record); boolean interceptAfter = record.isIntercepted(); - float contactAffinityAfter = record.getContactAffinity(); int visibilityAfter = record.getPackageVisibilityOverride(); changed = indexBefore != indexAfter || interceptBefore != interceptAfter || visibilityBefore != visibilityAfter; if (interceptBefore && !interceptAfter - && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { + && record.isNewEnoughForAlerting(System.currentTimeMillis())) { buzzBeepBlinkLocked(record); } } diff --git a/services/core/java/com/android/server/notification/NotificationRecord.java b/services/core/java/com/android/server/notification/NotificationRecord.java index e2c64ca459a83..7cf7383515cee 100644 --- a/services/core/java/com/android/server/notification/NotificationRecord.java +++ b/services/core/java/com/android/server/notification/NotificationRecord.java @@ -92,6 +92,8 @@ public final class NotificationRecord { static final String TAG = "NotificationRecord"; static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); private static final int MAX_LOGTAG_LENGTH = 35; + // the period after which a notification is updated where it can make sound + private static final int MAX_SOUND_DELAY_MS = 2000; final StatusBarNotification sbn; IActivityManager mAm; UriGrantsManagerInternal mUgmInternal; @@ -125,7 +127,8 @@ public final class NotificationRecord { private long mVisibleSinceMs; // The most recent update time, or the creation time if no updates. - private long mUpdateTimeMs; + @VisibleForTesting + final long mUpdateTimeMs; // The most recent interruption time, or the creation time if no updates. Differs from the // above value because updates are filtered based on whether they actually interrupted the @@ -826,6 +829,10 @@ public final class NotificationRecord { return mIntercept; } + public boolean isNewEnoughForAlerting(long now) { + return getFreshnessMs(now) <= MAX_SOUND_DELAY_MS; + } + public void setHidden(boolean hidden) { mHidden = hidden; } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java index 65e640f5f73c8..1458266089c5b 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationRecordTest.java @@ -825,4 +825,24 @@ public class NotificationRecordTest extends UiServiceTestCase { assertNotEquals(-1, record.getLastAudiblyAlertedMs()); } + + @Test + public void testIsNewEnoughForAlerting_new() { + StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, + true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, + false /* lights */, false /* defaultLights */, groupId /* group */); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + + assertTrue(record.isNewEnoughForAlerting(record.mUpdateTimeMs)); + } + + @Test + public void testIsNewEnoughForAlerting_old() { + StatusBarNotification sbn = getNotification(PKG_O, true /* noisy */, + true /* defaultSound */, false /* buzzy */, false /* defaultBuzz */, + false /* lights */, false /* defaultLights */, groupId /* group */); + NotificationRecord record = new NotificationRecord(mMockContext, sbn, channel); + + assertFalse(record.isNewEnoughForAlerting(record.mUpdateTimeMs + (1000 * 60 * 60))); + } }