Skip to content

Commit aff802d

Browse files
author
Valentin Iftime
committed
Copy mSilent in NotificationCompat.Builder clone constructor
Ensure that the cloned notification is also silent. Test: NotificationCompatTest Bug: 436213106 Change-Id: I152a86dca1fcf4de08339557dccabc21593d4f5a
1 parent 0fc7aad commit aff802d

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

‎core/core/src/androidTest/java/androidx/core/app/NotificationCompatTest.java‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void setShortCriticalText() {
383383

384384
Notification notification = builder.build();
385385
assertThat(NotificationCompat.getShortCriticalText(notification))
386-
.isEqualTo(shortCriticalText);
386+
.isEqualTo(shortCriticalText);
387387

388388
notification = builder.setShortCriticalText(null).build();
389389
assertThat(NotificationCompat.getShortCriticalText(notification)).isNull();
@@ -1456,6 +1456,37 @@ public void testSetNotificationSilent_notSilenced() throws Throwable {
14561456
}
14571457
}
14581458

1459+
@Test
1460+
public void testSetNotificationSilent_clonedNotificationIsSilent_noGroup() {
1461+
// Create a silent notification
1462+
Notification notification = new NotificationCompat.Builder(mContext).setSilent(
1463+
true).build();
1464+
assertThat(NotificationCompat.isSilent(notification)).isTrue();
1465+
// Check that the copy notification is also silent
1466+
Notification notificationCopy = new NotificationCompat.Builder(mContext,
1467+
notification).build();
1468+
assertThat(NotificationCompat.isSilent(notificationCopy)).isTrue();
1469+
1470+
if (Build.VERSION.SDK_INT >= 26) {
1471+
assertThat(notification.getGroup()).isEqualTo(GROUP_KEY_SILENT);
1472+
assertThat(notificationCopy.getGroup()).isEqualTo(GROUP_KEY_SILENT);
1473+
}
1474+
}
1475+
1476+
@Test
1477+
public void testSetNotificationSilent_clonedNotificationIsSilent_grouped() {
1478+
// Create a silent notification
1479+
final String groupName = "testGrp";
1480+
Notification notification = new NotificationCompat.Builder(mContext).setGroup(
1481+
groupName).setSilent(true).build();
1482+
assertThat(NotificationCompat.isSilent(notification)).isTrue();
1483+
// Check that the copy notification is also silent
1484+
Notification notificationCopy = new NotificationCompat.Builder(mContext,
1485+
notification).build();
1486+
assertThat(NotificationCompat.isSilent(notificationCopy)).isTrue();
1487+
assertThat(NotificationCompat.getGroup(notificationCopy)).isEqualTo(groupName);
1488+
}
1489+
14591490
@Test
14601491
public void testGroupAlertBehavior_doesNotMuteIncorrectGroupNotifications() throws Throwable {
14611492
Notification n = new NotificationCompat.Builder(mActivityTestRule.getActivity())
@@ -2488,8 +2519,8 @@ public void testCallStyle_preservesOneCustomAction() {
24882519
} else if (Build.VERSION.SDK_INT == 33) {
24892520
// Could be either, so check presence but not ordering.
24902521
assertThat(actionTitles).containsExactly(
2491-
mContext.getString(R.string.call_notification_hang_up_action),
2492-
customAction.title.toString());
2522+
mContext.getString(R.string.call_notification_hang_up_action),
2523+
customAction.title.toString());
24932524
} else {
24942525
throw new AssertionError("All SDK_INT values are covered!");
24952526
}
@@ -2539,9 +2570,9 @@ public void testCallStyle_preservesTwoCustomActions() {
25392570
} else if (Build.VERSION.SDK_INT == 33) {
25402571
// Could be either, so check presence but not ordering.
25412572
assertThat(actionTitles).containsExactly(
2542-
mContext.getString(R.string.call_notification_hang_up_action),
2543-
customAction1.title.toString(),
2544-
customAction2.title.toString());
2573+
mContext.getString(R.string.call_notification_hang_up_action),
2574+
customAction1.title.toString(),
2575+
customAction2.title.toString());
25452576
} else {
25462577
throw new AssertionError("All SDK_INT values are covered!");
25472578
}

‎core/core/src/main/java/androidx/core/app/NotificationCompat.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ public Builder(@NonNull Context context,
11881188
.setFullScreenIntent(notification.fullScreenIntent,
11891189
NotificationCompat.getHighPriority(notification))
11901190
.setSound(notification.sound, notification.audioStreamType)
1191+
.setSilent(NotificationCompat.isSilent(notification))
11911192
.setVibrate(notification.vibrate)
11921193
.setLights(notification.ledARGB, notification.ledOnMS, notification.ledOffMS)
11931194
.setDefaults(notification.defaults)
@@ -9989,6 +9990,25 @@ public static boolean hasPromotableCharacteristics(@NonNull Notification notific
99899990
}
99909991
}
99919992

9993+
static boolean isSilent(@NonNull Notification notification) {
9994+
if (Build.VERSION.SDK_INT >= 26) {
9995+
if (NotificationCompat.GROUP_KEY_SILENT.equals(notification.getGroup())) {
9996+
return true;
9997+
}
9998+
9999+
if (isGroupSummary(notification)) {
10000+
return getGroupAlertBehavior(notification) == GROUP_ALERT_CHILDREN;
10001+
} else {
10002+
return getGroupAlertBehavior(notification) == GROUP_ALERT_SUMMARY;
10003+
}
10004+
} else {
10005+
return ((notification.defaults & DEFAULT_SOUND) == 0
10006+
&& (notification.defaults & DEFAULT_VIBRATE) == 0
10007+
&& notification.vibrate == null
10008+
&& notification.sound == null);
10009+
}
10010+
}
10011+
999210012
/** @deprecated This type should not be instantiated as it contains only static methods. */
999310013
@Deprecated
999410014
@SuppressWarnings("PrivateConstructorForUtilityClass")

0 commit comments

Comments
 (0)