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 Merged-In: I2e00c79216734f6c2db7d5b1347ab72a9be5cdc3
This commit is contained in:
@@ -953,7 +953,7 @@ public class ZenModeConfig implements Parcelable {
|
||||
|
||||
private static Uri safeUri(TypedXmlPullParser parser, String att) {
|
||||
final String val = parser.getAttributeValue(null, att);
|
||||
if (TextUtils.isEmpty(val)) return null;
|
||||
if (val == null) return null;
|
||||
return Uri.parse(val);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,22 +32,24 @@ import android.service.notification.Condition;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.service.notification.ZenModeConfig.EventInfo;
|
||||
import android.service.notification.ZenPolicy;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
import android.util.TypedXmlPullParser;
|
||||
import android.util.TypedXmlSerializer;
|
||||
import android.util.Xml;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.server.UiServiceTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@@ -189,8 +191,6 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
|
||||
@Test
|
||||
public void testRuleXml() throws Exception {
|
||||
String tag = "tag";
|
||||
|
||||
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
|
||||
rule.configurationActivity = new ComponentName("a", "a");
|
||||
rule.component = new ComponentName("b", "b");
|
||||
@@ -205,20 +205,11 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
rule.snoozing = true;
|
||||
rule.pkg = "b";
|
||||
|
||||
TypedXmlSerializer out = Xml.newFastSerializer();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.setOutput(new BufferedOutputStream(baos), "utf-8");
|
||||
out.startDocument(null, true);
|
||||
out.startTag(null, tag);
|
||||
ZenModeConfig.writeRuleXml(rule, out);
|
||||
out.endTag(null, tag);
|
||||
out.endDocument();
|
||||
writeRuleXml(rule, baos);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
|
||||
|
||||
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);
|
||||
// always resets on reboot
|
||||
assertFalse(fromXml.snoozing);
|
||||
@@ -237,75 +228,41 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
|
||||
@Test
|
||||
public void testRuleXml_pkg_component() throws Exception {
|
||||
String tag = "tag";
|
||||
|
||||
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
|
||||
rule.configurationActivity = new ComponentName("a", "a");
|
||||
rule.component = new ComponentName("b", "b");
|
||||
|
||||
TypedXmlSerializer out = Xml.newFastSerializer();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.setOutput(new BufferedOutputStream(baos), "utf-8");
|
||||
out.startDocument(null, true);
|
||||
out.startTag(null, tag);
|
||||
ZenModeConfig.writeRuleXml(rule, out);
|
||||
out.endTag(null, tag);
|
||||
out.endDocument();
|
||||
writeRuleXml(rule, baos);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleXml_pkg_configActivity() throws Exception {
|
||||
String tag = "tag";
|
||||
|
||||
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
|
||||
rule.configurationActivity = new ComponentName("a", "a");
|
||||
|
||||
TypedXmlSerializer out = Xml.newFastSerializer();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.setOutput(new BufferedOutputStream(baos), "utf-8");
|
||||
out.startDocument(null, true);
|
||||
out.startTag(null, tag);
|
||||
ZenModeConfig.writeRuleXml(rule, out);
|
||||
out.endTag(null, tag);
|
||||
out.endDocument();
|
||||
writeRuleXml(rule, baos);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
|
||||
|
||||
TypedXmlPullParser parser = Xml.newFastPullParser();
|
||||
parser.setInput(new BufferedInputStream(
|
||||
new ByteArrayInputStream(baos.toByteArray())), null);
|
||||
parser.nextTag();
|
||||
ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
|
||||
assertNull(fromXml.pkg);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleXml_getPkg_nullPkg() throws Exception {
|
||||
String tag = "tag";
|
||||
|
||||
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
|
||||
rule.enabled = true;
|
||||
rule.configurationActivity = new ComponentName("a", "a");
|
||||
|
||||
TypedXmlSerializer out = Xml.newFastSerializer();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.setOutput(new BufferedOutputStream(baos), "utf-8");
|
||||
out.startDocument(null, true);
|
||||
out.startTag(null, tag);
|
||||
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);
|
||||
writeRuleXml(rule, baos);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
|
||||
assertEquals("a", fromXml.getPkg());
|
||||
|
||||
fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE);
|
||||
@@ -313,25 +270,26 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZenPolicyXml_allUnset() throws Exception {
|
||||
String tag = "tag";
|
||||
public void testRuleXml_emptyConditionId() throws Exception {
|
||||
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();
|
||||
|
||||
TypedXmlSerializer out = Xml.newFastSerializer();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.setOutput(new BufferedOutputStream(baos), "utf-8");
|
||||
out.startDocument(null, true);
|
||||
out.startTag(null, tag);
|
||||
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);
|
||||
writePolicyXml(policy, baos);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ZenPolicy fromXml = readPolicyXml(bais);
|
||||
|
||||
// nothing was set, so we should have nothing from the parser
|
||||
assertNull(fromXml);
|
||||
@@ -339,8 +297,6 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
|
||||
@Test
|
||||
public void testZenPolicyXml() throws Exception {
|
||||
String tag = "tag";
|
||||
|
||||
ZenPolicy policy = new ZenPolicy.Builder()
|
||||
.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
|
||||
.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE)
|
||||
@@ -355,20 +311,10 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
.showVisualEffect(ZenPolicy.VISUAL_EFFECT_AMBIENT, true)
|
||||
.build();
|
||||
|
||||
TypedXmlSerializer out = Xml.newFastSerializer();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
out.setOutput(new BufferedOutputStream(baos), "utf-8");
|
||||
out.startDocument(null, true);
|
||||
out.startTag(null, tag);
|
||||
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);
|
||||
writePolicyXml(policy, baos);
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
ZenPolicy fromXml = readPolicyXml(bais);
|
||||
|
||||
assertNotNull(fromXml);
|
||||
assertEquals(policy.getPriorityCategoryCalls(), fromXml.getPriorityCategoryCalls());
|
||||
@@ -457,4 +403,45 @@ public class ZenModeConfigTest extends UiServiceTestCase {
|
||||
config.suppressedVisualEffects = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user