Modifies Notifcn.build to retain provided extras

Modifies Notification.build to retain user-provided extras.

Bug: 169435530
Test: Adds unit tests to cover expected code behavior.
Change-Id: I8c1e58b14da1bc89dcba9978cab1df691f81c579
This commit is contained in:
Alexander Roederer
2023-03-01 22:26:37 +00:00
parent 37d2ef3482
commit 69abc21a53
2 changed files with 116 additions and 8 deletions

View File

@@ -5006,12 +5006,6 @@ public class Notification implements Parcelable
return mUserExtras;
}
private Bundle getAllExtras() {
final Bundle saveExtras = (Bundle) mUserExtras.clone();
saveExtras.putAll(mN.extras);
return saveExtras;
}
/**
* Add an action to this notification. Actions are typically displayed by
* the system as a button adjacent to the notification content.
@@ -6617,9 +6611,16 @@ public class Notification implements Parcelable
+ " vs bubble: " + mN.mBubbleMetadata.getShortcutId());
}
// first, add any extras from the calling code
// Adds any new extras provided by the user.
if (mUserExtras != null) {
mN.extras = getAllExtras();
final Bundle saveExtras = (Bundle) mUserExtras.clone();
if (SystemProperties.getBoolean(
"persist.sysui.notification.builder_extras_override", false)) {
mN.extras.putAll(saveExtras);
} else {
saveExtras.putAll(mN.extras);
mN.extras = saveExtras;
}
}
mN.creationTime = System.currentTimeMillis();

View File

@@ -33,6 +33,8 @@ import static android.app.Notification.EXTRA_MESSAGING_PERSON;
import static android.app.Notification.EXTRA_PEOPLE_LIST;
import static android.app.Notification.EXTRA_PICTURE;
import static android.app.Notification.EXTRA_PICTURE_ICON;
import static android.app.Notification.EXTRA_SUMMARY_TEXT;
import static android.app.Notification.EXTRA_TITLE;
import static android.app.Notification.MessagingStyle.Message.KEY_DATA_URI;
import static android.app.Notification.MessagingStyle.Message.KEY_SENDER_PERSON;
import static android.app.Notification.MessagingStyle.Message.KEY_TEXT;
@@ -76,6 +78,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemProperties;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
@@ -111,6 +114,9 @@ public class NotificationTest {
@Before
public void setUp() {
mContext = InstrumentationRegistry.getContext();
// TODO(b/169435530): remove this flag set once resolved.
SystemProperties.set("persist.sysui.notification.builder_extras_override",
Boolean.toString(false));
}
@Test
@@ -1481,6 +1487,107 @@ public class NotificationTest {
Assert.assertEquals(actionWithFreeformRemoteInput, remoteInputActionPair.second);
}
// Ensures that extras in a Notification Builder can be updated.
@Test
public void testExtras_cachedExtrasOverwrittenByUserProvided() {
// Sets the flag to new state.
// TODO(b/169435530): remove this set value once resolved.
SystemProperties.set("persist.sysui.notification.builder_extras_override",
Boolean.toString(true));
Bundle extras = new Bundle();
extras.putCharSequence(EXTRA_TITLE, "test title");
extras.putCharSequence(EXTRA_SUMMARY_TEXT, "summary text");
Notification.Builder builder = new Notification.Builder(mContext, "test id")
.addExtras(extras);
Notification notification = builder.build();
assertThat(notification.extras.getCharSequence(EXTRA_TITLE).toString()).isEqualTo(
"test title");
assertThat(notification.extras.getCharSequence(EXTRA_SUMMARY_TEXT).toString()).isEqualTo(
"summary text");
extras.putCharSequence(EXTRA_TITLE, "new title");
builder.addExtras(extras);
notification = builder.build();
assertThat(notification.extras.getCharSequence(EXTRA_TITLE).toString()).isEqualTo(
"new title");
assertThat(notification.extras.getCharSequence(EXTRA_SUMMARY_TEXT).toString()).isEqualTo(
"summary text");
}
// Ensures that extras in a Notification Builder can be updated by an extender.
@Test
public void testExtras_cachedExtrasOverwrittenByExtender() {
// Sets the flag to new state.
// TODO(b/169435530): remove this set value once resolved.
SystemProperties.set("persist.sysui.notification.builder_extras_override",
Boolean.toString(true));
Notification.CarExtender extender = new Notification.CarExtender().setColor(1234);
Notification notification = new Notification.Builder(mContext, "test id")
.extend(extender).build();
extender.setColor(5678);
Notification.Builder.recoverBuilder(mContext, notification).extend(extender).build();
Notification.CarExtender recoveredExtender = new Notification.CarExtender(notification);
assertThat(recoveredExtender.getColor()).isEqualTo(5678);
}
// Validates pre-flag flip behavior, that extras in a Notification Builder cannot be updated.
// TODO(b/169435530): remove this test once resolved.
@Test
public void testExtras_cachedExtrasOverwrittenByUserProvidedOld() {
// Sets the flag to old state.
SystemProperties.set("persist.sysui.notification.builder_extras_override",
Boolean.toString(false));
Bundle extras = new Bundle();
extras.putCharSequence(EXTRA_TITLE, "test title");
extras.putCharSequence(EXTRA_SUMMARY_TEXT, "summary text");
Notification.Builder builder = new Notification.Builder(mContext, "test id")
.addExtras(extras);
Notification notification = builder.build();
assertThat(notification.extras.getCharSequence(EXTRA_TITLE).toString()).isEqualTo(
"test title");
assertThat(notification.extras.getCharSequence(EXTRA_SUMMARY_TEXT).toString()).isEqualTo(
"summary text");
extras.putCharSequence(EXTRA_TITLE, "new title");
builder.addExtras(extras);
notification = builder.build();
assertThat(notification.extras.getCharSequence(EXTRA_TITLE).toString()).isEqualTo(
"test title");
assertThat(notification.extras.getCharSequence(EXTRA_SUMMARY_TEXT).toString()).isEqualTo(
"summary text");
}
// Validates pre-flag flip behavior, that extras in a Notification Builder cannot be updated
// by an extender.
// TODO(b/169435530): remove this test once resolved.
@Test
public void testExtras_cachedExtrasOverwrittenByExtenderOld() {
// Sets the flag to old state.
SystemProperties.set("persist.sysui.notification.builder_extras_override",
Boolean.toString(false));
Notification.CarExtender extender = new Notification.CarExtender().setColor(1234);
Notification notification = new Notification.Builder(mContext, "test id")
.extend(extender).build();
extender.setColor(5678);
Notification.Builder.recoverBuilder(mContext, notification).extend(extender).build();
Notification.CarExtender recoveredExtender = new Notification.CarExtender(notification);
assertThat(recoveredExtender.getColor()).isEqualTo(1234);
}
private void assertValid(Notification.Colors c) {
// Assert that all colors are populated
assertThat(c.getBackgroundColor()).isNotEqualTo(Notification.COLOR_INVALID);