Merge "[conflict] Merge "Trim strings added to persistent snoozed notification storage." into tm-dev am: c5034c21fe" into tm-mainline-prod

This commit is contained in:
Sam Saccone
2023-03-14 04:11:56 +00:00
committed by Android (Google) Code Review
2 changed files with 61 additions and 8 deletions

View File

@@ -62,6 +62,9 @@ import java.util.Set;
public class SnoozeHelper {
public static final int XML_SNOOZED_NOTIFICATION_VERSION = 1;
// A safe size for strings to be put in persistent storage, to avoid breaking the XML write.
static final int MAX_STRING_LENGTH = 1000;
protected static final String XML_TAG_NAME = "snoozed-notifications";
private static final String XML_SNOOZED_NOTIFICATION = "notification";
@@ -142,7 +145,7 @@ public class SnoozeHelper {
ArrayMap<String, Long> snoozed =
mPersistedSnoozedNotifications.get(getPkgKey(userId, pkg));
if (snoozed != null) {
time = snoozed.get(key);
time = snoozed.get(getTrimmedString(key));
}
}
if (time == null) {
@@ -156,7 +159,7 @@ public class SnoozeHelper {
ArrayMap<String, String> snoozed =
mPersistedSnoozedNotificationsWithContext.get(getPkgKey(userId, pkg));
if (snoozed != null) {
return snoozed.get(key);
return snoozed.get(getTrimmedString(key));
}
}
return null;
@@ -241,7 +244,8 @@ public class SnoozeHelper {
scheduleRepost(pkg, key, userId, duration);
Long activateAt = System.currentTimeMillis() + duration;
synchronized (mLock) {
storeRecordLocked(pkg, key, userId, mPersistedSnoozedNotifications, activateAt);
storeRecordLocked(pkg, getTrimmedString(key), userId, mPersistedSnoozedNotifications,
activateAt);
}
}
@@ -252,8 +256,10 @@ public class SnoozeHelper {
int userId = record.getUser().getIdentifier();
if (contextId != null) {
synchronized (mLock) {
storeRecordLocked(record.getSbn().getPackageName(), record.getKey(),
userId, mPersistedSnoozedNotificationsWithContext, contextId);
storeRecordLocked(record.getSbn().getPackageName(),
getTrimmedString(record.getKey()),
userId, mPersistedSnoozedNotificationsWithContext,
getTrimmedString(contextId));
}
}
snooze(record);
@@ -270,6 +276,13 @@ public class SnoozeHelper {
}
}
private String getTrimmedString(String key) {
if (key != null && key.length() > MAX_STRING_LENGTH) {
return key.substring(0, MAX_STRING_LENGTH);
}
return key;
}
private <T> void storeRecordLocked(String pkg, String key, Integer userId,
ArrayMap<String, ArrayMap<String, T>> targets, T object) {
@@ -374,12 +387,14 @@ public class SnoozeHelper {
}
protected void repost(String key, int userId, boolean muteOnReturn) {
final String trimmedKey = getTrimmedString(key);
NotificationRecord record;
synchronized (mLock) {
final String pkg = mPackages.remove(key);
mUsers.remove(key);
removeRecordLocked(pkg, key, userId, mPersistedSnoozedNotifications);
removeRecordLocked(pkg, key, userId, mPersistedSnoozedNotificationsWithContext);
removeRecordLocked(pkg, trimmedKey, userId, mPersistedSnoozedNotifications);
removeRecordLocked(pkg, trimmedKey, userId, mPersistedSnoozedNotificationsWithContext);
ArrayMap<String, NotificationRecord> records =
mSnoozedNotifications.get(getPkgKey(userId, pkg));
if (records == null) {

View File

@@ -246,6 +246,37 @@ public class SnoozeHelperTest extends UiServiceTestCase {
assertEquals("key2", captor2.getValue().getIntent().getStringExtra(EXTRA_KEY));
}
@Test
public void testLongTagPersistedNotification() throws Exception {
String longTag = "A".repeat(66000);
NotificationRecord r = getNotificationRecord("pkg", 1, longTag, UserHandle.SYSTEM);
mSnoozeHelper.snooze(r, 0);
// We store the full key in temp storage.
ArgumentCaptor<PendingIntent> captor = ArgumentCaptor.forClass(PendingIntent.class);
verify(mAm).setExactAndAllowWhileIdle(anyInt(), anyLong(), captor.capture());
assertEquals(66010, captor.getValue().getIntent().getStringExtra(EXTRA_KEY).length());
TypedXmlSerializer serializer = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
serializer.startDocument(null, true);
mSnoozeHelper.writeXml(serializer);
serializer.endDocument();
serializer.flush();
TypedXmlPullParser parser = Xml.newFastPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(baos.toByteArray())), "utf-8");
mSnoozeHelper.readXml(parser, 4);
mSnoozeHelper.scheduleRepostsForPersistedNotifications(5);
// We trim the key in persistent storage.
verify(mAm, times(2)).setExactAndAllowWhileIdle(anyInt(), anyLong(), captor.capture());
assertEquals(1000, captor.getValue().getIntent().getStringExtra(EXTRA_KEY).length());
}
@Test
public void testSnoozeForTime() throws Exception {
NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
@@ -578,13 +609,20 @@ public class SnoozeHelperTest extends UiServiceTestCase {
public void testClearData() {
// snooze 2 from same package
NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
NotificationRecord r2 = getNotificationRecord("pkg", 2, "two" + "2".repeat(66000),
UserHandle.SYSTEM);
mSnoozeHelper.snooze(r, 1000);
mSnoozeHelper.snooze(r2, 1000);
assertTrue(mSnoozeHelper.isSnoozed(
UserHandle.USER_SYSTEM, r.getSbn().getPackageName(), r.getKey()));
assertTrue(mSnoozeHelper.isSnoozed(
UserHandle.USER_SYSTEM, r2.getSbn().getPackageName(), r2.getKey()));
assertFalse(0L == mSnoozeHelper.getSnoozeTimeForUnpostedNotification(
r.getUser().getIdentifier(), r.getSbn().getPackageName(),
r.getSbn().getKey()));
assertFalse(0L == mSnoozeHelper.getSnoozeTimeForUnpostedNotification(
r2.getUser().getIdentifier(), r2.getSbn().getPackageName(),
r2.getSbn().getKey()));
// clear data
mSnoozeHelper.clearData(UserHandle.USER_SYSTEM, "pkg");