summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Matías Hernández <matiashe@google.com> 2024-08-23 12:32:48 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2024-08-23 12:32:48 +0000
commit4c02b701a98713c92c88514b36183ea3a61b043e (patch)
tree3d0ee5dcbb3f3e81ab11b7a376400c74c260822d
parentd0a4c0da8333b4db5b96bf880386f0086cba788a (diff)
parentd2b75eb709806c85ac99cf1575475a069ccff0a3 (diff)
Merge "Allow apps to recreate rules if changing the owner component" into main
-rw-r--r--services/core/java/com/android/server/notification/ZenModeHelper.java14
-rw-r--r--services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java43
2 files changed, 54 insertions, 3 deletions
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index ee3f48d9bd3f..06d1285e9192 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -485,7 +485,7 @@ public class ZenModeHelper {
newConfig = mConfig.copy();
ZenRule rule = new ZenRule();
populateZenRule(pkg, automaticZenRule, rule, origin, /* isNew= */ true);
- rule = maybeRestoreRemovedRule(newConfig, rule, automaticZenRule, origin);
+ rule = maybeRestoreRemovedRule(newConfig, pkg, rule, automaticZenRule, origin);
newConfig.automaticRules.put(rule.id, rule);
maybeReplaceDefaultRule(newConfig, automaticZenRule);
@@ -498,7 +498,7 @@ public class ZenModeHelper {
}
@GuardedBy("mConfigLock")
- private ZenRule maybeRestoreRemovedRule(ZenModeConfig config, ZenRule ruleToAdd,
+ private ZenRule maybeRestoreRemovedRule(ZenModeConfig config, String pkg, ZenRule ruleToAdd,
AutomaticZenRule azrToAdd, @ConfigOrigin int origin) {
if (!Flags.modesApi()) {
return ruleToAdd;
@@ -522,10 +522,18 @@ public class ZenModeHelper {
if (origin != ORIGIN_APP) {
return ruleToAdd; // Okay to create anew.
}
+ if (Flags.modesUi()) {
+ if (!Objects.equals(ruleToRestore.pkg, pkg)
+ || !Objects.equals(ruleToRestore.component, azrToAdd.getOwner())) {
+ // Apps are not allowed to change the owner via updateAutomaticZenRule(). Thus, if
+ // they have to, delete+add is their only option.
+ return ruleToAdd;
+ }
+ }
// "Preserve" the previous rule by considering the azrToAdd an update instead.
// Only app-modifiable fields will actually be modified.
- populateZenRule(ruleToRestore.pkg, azrToAdd, ruleToRestore, origin, /* isNew= */ false);
+ populateZenRule(pkg, azrToAdd, ruleToRestore, origin, /* isNew= */ false);
return ruleToRestore;
}
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
index dd2b84504e51..6f10370e972a 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
@@ -5538,6 +5538,49 @@ public class ZenModeHelperTest extends UiServiceTestCase {
}
@Test
+ @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
+ public void removeAndAddAutomaticZenRule_ifChangingComponent_isAllowedAndDoesNotRestore() {
+ // Start with a rule.
+ mZenModeHelper.mConfig.automaticRules.clear();
+ AutomaticZenRule rule = new AutomaticZenRule.Builder("Test", CONDITION_ID)
+ .setOwner(new ComponentName("first", "owner"))
+ .setInterruptionFilter(INTERRUPTION_FILTER_ALL)
+ .build();
+ String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule,
+ ORIGIN_APP, "add it", CUSTOM_PKG_UID);
+
+ // User customizes it.
+ AutomaticZenRule userUpdate = new AutomaticZenRule.Builder(rule)
+ .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
+ .build();
+ mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI,
+ "userUpdate", SYSTEM_UID);
+
+ // App deletes it. It's preserved for a possible restoration.
+ mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it",
+ CUSTOM_PKG_UID);
+ assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0);
+ assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(1);
+
+ // App adds it again, but this time with a different owner!
+ AutomaticZenRule readdingWithDifferentOwner = new AutomaticZenRule.Builder(rule)
+ .setOwner(new ComponentName("second", "owner"))
+ .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS)
+ .build();
+ String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(),
+ readdingWithDifferentOwner, ORIGIN_APP, "add it again", CUSTOM_PKG_UID);
+
+ // Verify that the rule was NOT restored:
+ assertThat(newRuleId).isNotEqualTo(ruleId);
+ AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId);
+ assertThat(finalRule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_ALARMS);
+ assertThat(finalRule.getOwner()).isEqualTo(new ComponentName("second", "owner"));
+
+ // Also, we discarded the "deleted rule" since we found it but decided not to use it.
+ assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(0);
+ }
+
+ @Test
@EnableFlags(FLAG_MODES_API)
public void removeAutomaticZenRule_preservedForRestoringByPackageAndConditionId() {
mContext.getTestablePermissions().setPermission(Manifest.permission.MANAGE_NOTIFICATIONS,