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

Commit 86450e87 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Improve backup and restore for empty sounds"

parents 9c560902 aca215a6
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -644,7 +644,7 @@ public final class NotificationChannel implements Parcelable {

    @Nullable
    private Uri restoreSoundUri(Context context, @Nullable Uri uri) {
        if (uri == null) {
        if (uri == null || Uri.EMPTY.equals(uri)) {
            return null;
        }
        ContentResolver contentResolver = context.getContentResolver();
@@ -680,7 +680,7 @@ public final class NotificationChannel implements Parcelable {

    private Uri getSoundForBackup(Context context) {
        Uri sound = getSound();
        if (sound == null) {
        if (sound == null || Uri.EMPTY.equals(sound)) {
            return null;
        }
        Uri canonicalSound = context.getContentResolver().canonicalize(sound);
+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ public class NotificationChannels extends SystemUI {
        NotificationChannel screenshotChannel = new NotificationChannel(SCREENSHOTS_HEADSUP,
                name, NotificationManager.IMPORTANCE_HIGH); // pop on screen

        screenshotChannel.setSound(Uri.parse(""), // silent
        screenshotChannel.setSound(null, // silent
                new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
        screenshotChannel.setBlockableSystem(true);

+2 −2
Original line number Diff line number Diff line
@@ -103,7 +103,7 @@ public class ChannelsTest extends SysuiTestCase {
                NotificationManager.IMPORTANCE_MIN);
        NotificationChannel newChannel =
                NotificationChannels.createScreenshotChannel("newName", legacyChannel);
        assertEquals(Uri.EMPTY, newChannel.getSound());
        assertEquals(null, newChannel.getSound());
        assertEquals("newName", newChannel.getName());
        // MIN importance not user locked, so HIGH wins out.
        assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance());
@@ -113,7 +113,7 @@ public class ChannelsTest extends SysuiTestCase {
    public void testInheritFromLegacy_noLegacyExists() {
        NotificationChannel newChannel =
                NotificationChannels.createScreenshotChannel("newName", null);
        assertEquals(Uri.EMPTY, newChannel.getSound());
        assertEquals(null, newChannel.getSound());
        assertEquals("newName", newChannel.getName());
        assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance());
    }
+25 −0
Original line number Diff line number Diff line
@@ -19,20 +19,26 @@ package com.android.server.notification;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;

import android.app.NotificationChannel;
import android.net.Uri;
import android.os.Parcel;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Xml;

import com.android.internal.util.FastXmlSerializer;
import com.android.server.UiServiceTestCase;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

@SmallTest
@@ -68,4 +74,23 @@ public class NotificationChannelTest extends UiServiceTestCase {
        serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
        channel.writeXml(serializer);
    }

    @Test
    public void testBackupEmptySound() throws Exception {
        NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
        channel.setSound(Uri.EMPTY, null);

        XmlSerializer serializer = new FastXmlSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
        channel.writeXmlForBackup(serializer, getContext());

        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        NotificationChannel restored = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
        restored.populateFromXmlForRestore(parser, getContext());

        assertNull(restored.getSound());
    }
}