Only recently updated notifications can alert

Even if something in e.g. the contacts database
has changed

Test: atest
Change-Id: I10eaa3d34574296d554eb8bae5900c79af773dcd
Fixes: 117868600
This commit is contained in:
Julia Reynolds
2019-01-04 12:52:52 -05:00
parent 5c5513f2c7
commit d673007aff
3 changed files with 29 additions and 4 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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)));
}
}