DO NOT MERGE Add an OEM configurable limit for zen rules am: 3072d98c2d
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17045726 Change-Id: Id5f8b8657848ebdff6a057cf296d87d46f5e7677
This commit is contained in:
@@ -92,6 +92,7 @@ public class ZenModeHelper {
|
|||||||
|
|
||||||
// The amount of time rules instances can exist without their owning app being installed.
|
// The amount of time rules instances can exist without their owning app being installed.
|
||||||
private static final int RULE_INSTANCE_GRACE_PERIOD = 1000 * 60 * 60 * 72;
|
private static final int RULE_INSTANCE_GRACE_PERIOD = 1000 * 60 * 60 * 72;
|
||||||
|
static final int RULE_LIMIT_PER_PACKAGE = 100;
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final H mHandler;
|
private final H mHandler;
|
||||||
@@ -305,10 +306,10 @@ public class ZenModeHelper {
|
|||||||
int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner())
|
int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner())
|
||||||
+ getCurrentInstanceCount(automaticZenRule.getConfigurationActivity())
|
+ getCurrentInstanceCount(automaticZenRule.getConfigurationActivity())
|
||||||
+ 1;
|
+ 1;
|
||||||
if (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount) {
|
if (newRuleInstanceCount > RULE_LIMIT_PER_PACKAGE
|
||||||
|
|| (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount)) {
|
||||||
throw new IllegalArgumentException("Rule instance limit exceeded");
|
throw new IllegalArgumentException("Rule instance limit exceeded");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ZenModeConfig newConfig;
|
ZenModeConfig newConfig;
|
||||||
|
|||||||
@@ -20,10 +20,13 @@ import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
|
|||||||
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
|
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
|
||||||
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
|
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
|
||||||
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
|
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
|
||||||
|
import static com.android.server.notification.ZenModeHelper.RULE_LIMIT_PER_PACKAGE;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
import static junit.framework.Assert.assertFalse;
|
import static junit.framework.Assert.assertFalse;
|
||||||
|
import static junit.framework.Assert.assertNotNull;
|
||||||
import static junit.framework.TestCase.assertTrue;
|
import static junit.framework.TestCase.assertTrue;
|
||||||
|
import static junit.framework.TestCase.fail;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
@@ -48,7 +51,9 @@ import android.app.NotificationManager.Policy;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.ActivityInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.content.res.XmlResourceParser;
|
import android.content.res.XmlResourceParser;
|
||||||
import android.media.AudioAttributes;
|
import android.media.AudioAttributes;
|
||||||
@@ -77,6 +82,8 @@ import com.android.internal.util.FastXmlSerializer;
|
|||||||
import com.android.server.UiServiceTestCase;
|
import com.android.server.UiServiceTestCase;
|
||||||
import com.android.server.notification.ManagedServices.UserProfiles;
|
import com.android.server.notification.ManagedServices.UserProfiles;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -102,9 +109,12 @@ public class ZenModeHelperTest extends UiServiceTestCase {
|
|||||||
|
|
||||||
private static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE";
|
private static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE";
|
||||||
private static final String SCHEDULE_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE";
|
private static final String SCHEDULE_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE";
|
||||||
|
private static final String CUSTOM_PKG_NAME = "not.android";
|
||||||
|
private static final int CUSTOM_PKG_UID = 1;
|
||||||
|
|
||||||
ConditionProviders mConditionProviders;
|
ConditionProviders mConditionProviders;
|
||||||
@Mock NotificationManager mNotificationManager;
|
@Mock NotificationManager mNotificationManager;
|
||||||
|
@Mock PackageManager mPackageManager;
|
||||||
private Resources mResources;
|
private Resources mResources;
|
||||||
private TestableLooper mTestableLooper;
|
private TestableLooper mTestableLooper;
|
||||||
private ZenModeHelper mZenModeHelperSpy;
|
private ZenModeHelper mZenModeHelperSpy;
|
||||||
@@ -113,7 +123,7 @@ public class ZenModeHelperTest extends UiServiceTestCase {
|
|||||||
@Mock AppOpsManager mAppOps;
|
@Mock AppOpsManager mAppOps;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() throws PackageManager.NameNotFoundException {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mTestableLooper = TestableLooper.get(this);
|
mTestableLooper = TestableLooper.get(this);
|
||||||
@@ -135,6 +145,16 @@ public class ZenModeHelperTest extends UiServiceTestCase {
|
|||||||
mConditionProviders.addSystemProvider(new CountdownConditionProvider());
|
mConditionProviders.addSystemProvider(new CountdownConditionProvider());
|
||||||
mZenModeHelperSpy = spy(new ZenModeHelper(mContext, mTestableLooper.getLooper(),
|
mZenModeHelperSpy = spy(new ZenModeHelper(mContext, mTestableLooper.getLooper(),
|
||||||
mConditionProviders));
|
mConditionProviders));
|
||||||
|
|
||||||
|
ResolveInfo ri = new ResolveInfo();
|
||||||
|
ri.activityInfo = new ActivityInfo();
|
||||||
|
when(mPackageManager.queryIntentActivitiesAsUser(any(), anyInt(), anyInt())).thenReturn(
|
||||||
|
ImmutableList.of(ri));
|
||||||
|
when(mPackageManager.getPackageUidAsUser(eq(CUSTOM_PKG_NAME), anyInt()))
|
||||||
|
.thenReturn(CUSTOM_PKG_UID);
|
||||||
|
when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(
|
||||||
|
new String[] {getContext().getPackageName()});
|
||||||
|
mZenModeHelperSpy.mPm = mPackageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
private XmlResourceParser getDefaultConfigParser() throws IOException, XmlPullParserException {
|
private XmlResourceParser getDefaultConfigParser() throws IOException, XmlPullParserException {
|
||||||
@@ -1346,6 +1366,34 @@ public class ZenModeHelperTest extends UiServiceTestCase {
|
|||||||
assertEquals(zenRule.getName(), ruleInConfig.name);
|
assertEquals(zenRule.getName(), ruleInConfig.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddAutomaticZenRule_beyondSystemLimit() {
|
||||||
|
for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) {
|
||||||
|
ScheduleInfo si = new ScheduleInfo();
|
||||||
|
si.startHour = i;
|
||||||
|
AutomaticZenRule zenRule = new AutomaticZenRule("name" + i,
|
||||||
|
null,
|
||||||
|
new ComponentName("android", "ScheduleConditionProvider"),
|
||||||
|
ZenModeConfig.toScheduleConditionId(si),
|
||||||
|
new ZenPolicy.Builder().build(),
|
||||||
|
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
|
||||||
|
String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test");
|
||||||
|
assertNotNull(id);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
AutomaticZenRule zenRule = new AutomaticZenRule("name",
|
||||||
|
null,
|
||||||
|
new ComponentName("android", "ScheduleConditionProvider"),
|
||||||
|
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
|
||||||
|
new ZenPolicy.Builder().build(),
|
||||||
|
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
|
||||||
|
String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test");
|
||||||
|
fail("allowed too many rules to be created");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// yay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setupZenConfig() {
|
private void setupZenConfig() {
|
||||||
mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
|
mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
|
||||||
mZenModeHelperSpy.mConfig.allowAlarms = false;
|
mZenModeHelperSpy.mConfig.allowAlarms = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user