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

Commit 1cc88e77 authored by András Kurucz's avatar András Kurucz
Browse files

Restore empty ZenRule#conditionId after a device restart

We allow to create a new ZenRule with an empty (not null) conditionId. When restarting the device, we restore these rules form an XML. We didn't restore an empty conditionId. It lead to skip restoring the whole ZenConfig containing a rule with empty conditionId, which resulted in deleting all the rules in the given config.

Fixes: 264663537
Test: ZenModeConfigTest
Test: create some ZenRules manually form the Settings app, create a new ZenRule with an empty conditionId and add it with NotificationManager#addAutomaticZenRule using a test app, restart the device, see that all the rules are restored
Change-Id: I2e00c79216734f6c2db7d5b1347ab72a9be5cdc3
parent 1b487930
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -953,7 +953,7 @@ public class ZenModeConfig implements Parcelable {


    private static Uri safeUri(TypedXmlPullParser parser, String att) {
    private static Uri safeUri(TypedXmlPullParser parser, String att) {
        final String val = parser.getAttributeValue(null, att);
        final String val = parser.getAttributeValue(null, att);
        if (TextUtils.isEmpty(val)) return null;
        if (val == null) return null;
        return Uri.parse(val);
        return Uri.parse(val);
    }
    }


+75 −88
Original line number Original line Diff line number Diff line
@@ -32,9 +32,9 @@ import android.service.notification.Condition;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.EventInfo;
import android.service.notification.ZenModeConfig.EventInfo;
import android.service.notification.ZenPolicy;
import android.service.notification.ZenPolicy;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Xml;
import android.util.Xml;


import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.runner.AndroidJUnit4;


import com.android.modules.utils.TypedXmlPullParser;
import com.android.modules.utils.TypedXmlPullParser;
@@ -43,11 +43,13 @@ import com.android.server.UiServiceTestCase;


import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParserException;


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


@SmallTest
@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
@@ -189,8 +191,6 @@ public class ZenModeConfigTest extends UiServiceTestCase {


    @Test
    @Test
    public void testRuleXml() throws Exception {
    public void testRuleXml() throws Exception {
        String tag = "tag";

        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        rule.configurationActivity = new ComponentName("a", "a");
        rule.configurationActivity = new ComponentName("a", "a");
        rule.component = new ComponentName("b", "b");
        rule.component = new ComponentName("b", "b");
@@ -205,20 +205,11 @@ public class ZenModeConfigTest extends UiServiceTestCase {
        rule.snoozing = true;
        rule.snoozing = true;
        rule.pkg = "b";
        rule.pkg = "b";


        TypedXmlSerializer out = Xml.newFastSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        out.setOutput(new BufferedOutputStream(baos), "utf-8");
        writeRuleXml(rule, baos);
        out.startDocument(null, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        out.startTag(null, tag);
        ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
        ZenModeConfig.writeRuleXml(rule, out);
        out.endTag(null, tag);
        out.endDocument();


        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        parser.nextTag();
        ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
        assertEquals("b", fromXml.pkg);
        assertEquals("b", fromXml.pkg);
        // always resets on reboot
        // always resets on reboot
        assertFalse(fromXml.snoozing);
        assertFalse(fromXml.snoozing);
@@ -237,75 +228,41 @@ public class ZenModeConfigTest extends UiServiceTestCase {


    @Test
    @Test
    public void testRuleXml_pkg_component() throws Exception {
    public void testRuleXml_pkg_component() throws Exception {
        String tag = "tag";

        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        rule.configurationActivity = new ComponentName("a", "a");
        rule.configurationActivity = new ComponentName("a", "a");
        rule.component = new ComponentName("b", "b");
        rule.component = new ComponentName("b", "b");


        TypedXmlSerializer out = Xml.newFastSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        out.setOutput(new BufferedOutputStream(baos), "utf-8");
        writeRuleXml(rule, baos);
        out.startDocument(null, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        out.startTag(null, tag);
        ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
        ZenModeConfig.writeRuleXml(rule, out);
        out.endTag(null, tag);
        out.endDocument();


        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        parser.nextTag();
        ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
        assertEquals("b", fromXml.pkg);
        assertEquals("b", fromXml.pkg);
    }
    }


    @Test
    @Test
    public void testRuleXml_pkg_configActivity() throws Exception {
    public void testRuleXml_pkg_configActivity() throws Exception {
        String tag = "tag";

        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        rule.configurationActivity = new ComponentName("a", "a");
        rule.configurationActivity = new ComponentName("a", "a");


        TypedXmlSerializer out = Xml.newFastSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        out.setOutput(new BufferedOutputStream(baos), "utf-8");
        writeRuleXml(rule, baos);
        out.startDocument(null, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        out.startTag(null, tag);
        ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
        ZenModeConfig.writeRuleXml(rule, out);
        out.endTag(null, tag);
        out.endDocument();


        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        parser.nextTag();
        ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
        assertNull(fromXml.pkg);
        assertNull(fromXml.pkg);
    }
    }


    @Test
    @Test
    public void testRuleXml_getPkg_nullPkg() throws Exception {
    public void testRuleXml_getPkg_nullPkg() throws Exception {
        String tag = "tag";

        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        rule.enabled = true;
        rule.enabled = true;
        rule.configurationActivity = new ComponentName("a", "a");
        rule.configurationActivity = new ComponentName("a", "a");


        TypedXmlSerializer out = Xml.newFastSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        out.setOutput(new BufferedOutputStream(baos), "utf-8");
        writeRuleXml(rule, baos);
        out.startDocument(null, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        out.startTag(null, tag);
        ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
        ZenModeConfig.writeRuleXml(rule, out);
        out.endTag(null, tag);
        out.endDocument();

        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        parser.nextTag();
        ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
        assertEquals("a", fromXml.getPkg());
        assertEquals("a", fromXml.getPkg());


        fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE);
        fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE);
@@ -313,25 +270,26 @@ public class ZenModeConfigTest extends UiServiceTestCase {
    }
    }


    @Test
    @Test
    public void testZenPolicyXml_allUnset() throws Exception {
    public void testRuleXml_emptyConditionId() throws Exception {
        String tag = "tag";
        ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
        rule.conditionId = Uri.EMPTY;


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writeRuleXml(rule, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ZenModeConfig.ZenRule fromXml = readRuleXml(bais);

        assertEquals(rule.condition, fromXml.condition);
    }

    @Test
    public void testZenPolicyXml_allUnset() throws Exception {
        ZenPolicy policy = new ZenPolicy.Builder().build();
        ZenPolicy policy = new ZenPolicy.Builder().build();


        TypedXmlSerializer out = Xml.newFastSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        out.setOutput(new BufferedOutputStream(baos), "utf-8");
        writePolicyXml(policy, baos);
        out.startDocument(null, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        out.startTag(null, tag);
        ZenPolicy fromXml = readPolicyXml(bais);
        ZenModeConfig.writeZenPolicyXml(policy, out);
        out.endTag(null, tag);
        out.endDocument();

        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        parser.nextTag();
        ZenPolicy fromXml = ZenModeConfig.readZenPolicyXml(parser);


        // nothing was set, so we should have nothing from the parser
        // nothing was set, so we should have nothing from the parser
        assertNull(fromXml);
        assertNull(fromXml);
@@ -339,8 +297,6 @@ public class ZenModeConfigTest extends UiServiceTestCase {


    @Test
    @Test
    public void testZenPolicyXml() throws Exception {
    public void testZenPolicyXml() throws Exception {
        String tag = "tag";

        ZenPolicy policy = new ZenPolicy.Builder()
        ZenPolicy policy = new ZenPolicy.Builder()
                .allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
                .allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
                .allowMessages(ZenPolicy.PEOPLE_TYPE_NONE)
                .allowMessages(ZenPolicy.PEOPLE_TYPE_NONE)
@@ -355,20 +311,10 @@ public class ZenModeConfigTest extends UiServiceTestCase {
                .showVisualEffect(ZenPolicy.VISUAL_EFFECT_AMBIENT, true)
                .showVisualEffect(ZenPolicy.VISUAL_EFFECT_AMBIENT, true)
                .build();
                .build();


        TypedXmlSerializer out = Xml.newFastSerializer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        out.setOutput(new BufferedOutputStream(baos), "utf-8");
        writePolicyXml(policy, baos);
        out.startDocument(null, true);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        out.startTag(null, tag);
        ZenPolicy fromXml = readPolicyXml(bais);
        ZenModeConfig.writeZenPolicyXml(policy, out);
        out.endTag(null, tag);
        out.endDocument();

        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(
                new ByteArrayInputStream(baos.toByteArray())), null);
        parser.nextTag();
        ZenPolicy fromXml = ZenModeConfig.readZenPolicyXml(parser);


        assertNotNull(fromXml);
        assertNotNull(fromXml);
        assertEquals(policy.getPriorityCategoryCalls(), fromXml.getPriorityCategoryCalls());
        assertEquals(policy.getPriorityCategoryCalls(), fromXml.getPriorityCategoryCalls());
@@ -457,4 +403,45 @@ public class ZenModeConfigTest extends UiServiceTestCase {
        config.suppressedVisualEffects = 0;
        config.suppressedVisualEffects = 0;
        return config;
        return config;
    }
    }

    private void writeRuleXml(ZenModeConfig.ZenRule rule, ByteArrayOutputStream os)
            throws IOException {
        String tag = "tag";

        TypedXmlSerializer out = Xml.newFastSerializer();
        out.setOutput(new BufferedOutputStream(os), "utf-8");
        out.startDocument(null, true);
        out.startTag(null, tag);
        ZenModeConfig.writeRuleXml(rule, out);
        out.endTag(null, tag);
        out.endDocument();
    }

    private ZenModeConfig.ZenRule readRuleXml(ByteArrayInputStream is)
            throws XmlPullParserException, IOException {
        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(is), null);
        parser.nextTag();
        return ZenModeConfig.readRuleXml(parser);
    }

    private void writePolicyXml(ZenPolicy policy, ByteArrayOutputStream os) throws IOException {
        String tag = "tag";

        TypedXmlSerializer out = Xml.newFastSerializer();
        out.setOutput(new BufferedOutputStream(os), "utf-8");
        out.startDocument(null, true);
        out.startTag(null, tag);
        ZenModeConfig.writeZenPolicyXml(policy, out);
        out.endTag(null, tag);
        out.endDocument();
    }

    private ZenPolicy readPolicyXml(ByteArrayInputStream is)
            throws XmlPullParserException, IOException {
        TypedXmlPullParser parser = Xml.newFastPullParser();
        parser.setInput(new BufferedInputStream(is), null);
        parser.nextTag();
        return ZenModeConfig.readZenPolicyXml(parser);
    }
}
}