Validate zen policy/filter agreement
Test: atest
Fixes: 130205844
Change-Id: I734e4364b2af1b35eaad7c99d437e90f6fb0fcf4
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index f2e56b5..1dd36eeb 100644
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -29,6 +29,7 @@
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.app.NotificationManager.IMPORTANCE_NONE;
+import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECTS_UNSET;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
@@ -3419,8 +3420,7 @@
}
@Override
- public String addAutomaticZenRule(AutomaticZenRule automaticZenRule)
- throws RemoteException {
+ public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null");
Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null");
if (automaticZenRule.getOwner() == null
@@ -3429,6 +3429,11 @@
"Rule must have a conditionproviderservice and/or configuration activity");
}
Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null");
+ if (automaticZenRule.getZenPolicy() != null
+ && automaticZenRule.getInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY) {
+ throw new IllegalArgumentException("ZenPolicy is only applicable to "
+ + "INTERRUPTION_FILTER_PRIORITY filters");
+ }
enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule");
return mZenModeHelper.addAutomaticZenRule(automaticZenRule,
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
index ca7a71e..cf42aa8 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
@@ -71,8 +71,10 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import android.app.Activity;
import android.app.ActivityManager;
import android.app.AppOpsManager;
+import android.app.AutomaticZenRule;
import android.app.IActivityManager;
import android.app.INotificationManager;
import android.app.ITransientNotification;
@@ -117,6 +119,7 @@
import android.service.notification.NotificationStats;
import android.service.notification.NotifyingApp;
import android.service.notification.StatusBarNotification;
+import android.service.notification.ZenPolicy;
import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableContext;
@@ -4967,6 +4970,29 @@
assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment());
}
+ public void testAutomaticZenRuleValidation_policyFilterAgreement() throws Exception {
+ ComponentName owner = mock(ComponentName.class);
+ ZenPolicy zenPolicy = new ZenPolicy.Builder().allowAlarms(true).build();
+ boolean isEnabled = true;
+ AutomaticZenRule rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class),
+ zenPolicy, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled);
+
+ try {
+ mBinderService.addAutomaticZenRule(rule);
+ fail("Zen policy only aplies to priority only mode");
+ } catch (IllegalArgumentException e) {
+ // yay
+ }
+
+ rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class),
+ zenPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, isEnabled);
+ mBinderService.addAutomaticZenRule(rule);
+
+ rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class),
+ null, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled);
+ mBinderService.addAutomaticZenRule(rule);
+ }
+
public void testAreNotificationsEnabledForPackage_crossUser() throws Exception {
try {
mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(),