Fix reading of ZenModeConfig.manualRule in MODES_UI transition
Presence of the manualRule on pre-modes_ui xml meant that DND was active, thus to preserve this state when upgrading to modes_ui we must set a condition with STATE_TRUE. This conversion already existed, but it had an issue -- if the rule didn't have a conditionId set (very likely, since only DND-with-a-timeout adds one) then the result of the conversion would be a rule with _null_ conditionId but a condition with _empty_ id. This mismatch caused the isValidConfig() check to fail, thus discarding the previous configuration altogether!
This issue manifested as "rules are gone on flag flip" (although this wouldn't be noticeable if only "default" rules, like the built-in ones or Bedtime, were the only ones present). It also resulted in DND turning off.
Fixes: 369678857
Test: atest ZenModeConfigTest
Flag: android.app.modes_ui
Change-Id: I2787e8593b5305bfbe07681c5d0b5e19a3c12ed0
diff --git a/core/java/android/service/notification/ZenModeConfig.java b/core/java/android/service/notification/ZenModeConfig.java
index 303197d..e173255 100644
--- a/core/java/android/service/notification/ZenModeConfig.java
+++ b/core/java/android/service/notification/ZenModeConfig.java
@@ -1079,9 +1079,12 @@
// in ensureManualZenRule() and setManualZenMode().
rt.manualRule.pkg = PACKAGE_ANDROID;
rt.manualRule.type = AutomaticZenRule.TYPE_OTHER;
- rt.manualRule.condition = new Condition(
- rt.manualRule.conditionId != null ? rt.manualRule.conditionId
- : Uri.EMPTY, "", Condition.STATE_TRUE);
+ // conditionId in rule must match condition.id to pass isValidManualRule().
+ if (rt.manualRule.conditionId == null) {
+ rt.manualRule.conditionId = Uri.EMPTY;
+ }
+ rt.manualRule.condition = new Condition(rt.manualRule.conditionId, "",
+ Condition.STATE_TRUE);
}
}
return rt;
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java
index 84c4f62..5709d88 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java
@@ -1010,7 +1010,39 @@
@Test
@EnableFlags(Flags.FLAG_MODES_UI)
- public void testConfigXml_manualRule_upgradeWhenExisting() throws Exception {
+ public void testConfigXml_manualRuleWithoutCondition_upgradeWhenExisting() throws Exception {
+ // prior to modes_ui, it's possible to have a non-null manual rule that doesn't have much
+ // data on it because it's meant to indicate that the manual rule is on by merely existing.
+ ZenModeConfig config = new ZenModeConfig();
+ config.manualRule = new ZenModeConfig.ZenRule();
+ config.manualRule.enabled = true;
+ config.manualRule.pkg = "android";
+ config.manualRule.zenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS;
+ config.manualRule.conditionId = null;
+ config.manualRule.enabler = "test";
+
+ // write out entire config xml
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ writeConfigXml(config, XML_VERSION_MODES_API, /* forBackup= */ false, baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ ZenModeConfig fromXml = readConfigXml(bais);
+
+
+ // The result should be valid and contain a manual rule; the rule should have a non-null
+ // ZenPolicy and a condition whose state is true. The conditionId should be default.
+ assertThat(fromXml.isValid()).isTrue();
+ assertThat(fromXml.manualRule).isNotNull();
+ assertThat(fromXml.manualRule.zenPolicy).isNotNull();
+ assertThat(fromXml.manualRule.condition).isNotNull();
+ assertThat(fromXml.manualRule.condition.state).isEqualTo(STATE_TRUE);
+ assertThat(fromXml.manualRule.conditionId).isEqualTo(Uri.EMPTY);
+ assertThat(fromXml.manualRule.enabler).isEqualTo("test");
+ assertThat(fromXml.isManualActive()).isTrue();
+ }
+
+ @Test
+ @EnableFlags(Flags.FLAG_MODES_UI)
+ public void testConfigXml_manualRuleWithCondition_upgradeWhenExisting() throws Exception {
// prior to modes_ui, it's possible to have a non-null manual rule that doesn't have much
// data on it because it's meant to indicate that the manual rule is on by merely existing.
ZenModeConfig config = new ZenModeConfig();
@@ -1029,6 +1061,7 @@
// The result should have a manual rule; it should have a non-null ZenPolicy and a condition
// whose state is true. The conditionId and enabler data should also be preserved.
+ assertThat(fromXml.isValid()).isTrue();
assertThat(fromXml.manualRule).isNotNull();
assertThat(fromXml.manualRule.zenPolicy).isNotNull();
assertThat(fromXml.manualRule.condition).isNotNull();