Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f654a2c authored by Jeff DeCew's avatar Jeff DeCew
Browse files

Fix an likely ClassCastException in Notification.visitUris

The code (and unit test) treated EXTRA_AUDIO_CONTENTS_URI as a Uri, but the extra is very explicitly documented as a String.  Sample code and everything.

Fixes: 182997213
Test: atest NotificationManagerServiceTest#testVisitUris
Test: atest NotificationManagerServiceTest#testVisitUris_audioContentsString
Change-Id: I3cfc657f88d8e273270c777b199ce056ff2ffc67
parent 7003e6d3
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -2673,7 +2673,17 @@ public class Notification implements Parcelable
        if (headsUpContentView != null) headsUpContentView.visitUris(visitor);

        if (extras != null) {
            visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
            // NOTE: The documentation of EXTRA_AUDIO_CONTENTS_URI explicitly says that it is a
            // String representation of a Uri, but the previous implementation (and unit test) of
            // this method has always treated it as a Uri object. Given the inconsistency,
            // supporting both going forward is the safest choice.
            Object audioContentsUri = extras.get(EXTRA_AUDIO_CONTENTS_URI);
            if (audioContentsUri instanceof Uri) {
                visitor.accept((Uri) audioContentsUri);
            } else if (audioContentsUri instanceof String) {
                visitor.accept(Uri.parse((String) audioContentsUri));
            }

            if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
                visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
            }
+18 −0
Original line number Diff line number Diff line
@@ -4037,6 +4037,24 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        verify(visitor, times(1)).accept(eq(backgroundImage));
    }

    @Test
    public void testVisitUris_audioContentsString() throws Exception {
        final Uri audioContents = Uri.parse("content://com.example/audio");

        Bundle extras = new Bundle();
        extras.putString(Notification.EXTRA_AUDIO_CONTENTS_URI, audioContents.toString());

        Notification n = new Notification.Builder(mContext, "a")
                .setContentTitle("notification with uris")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .addExtras(extras)
                .build();

        Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class);
        n.visitUris(visitor);
        verify(visitor, times(1)).accept(eq(audioContents));
    }

    @Test
    public void testSetNotificationPolicy_preP_setOldFields() {
        ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class);