Use rule package name in addAutomaticZenRule; specify "android" for all system apps
This is a roll forward of two reverted changes combined into one: commitb6d0441662commite5e51116fbIt additionally fixes an issue where in multi-user profiles (such as a guest user), rules would be incorrectly identified as not created by the system and would therefore fail to be created in settings. Bug: 257477671 Bug: 245236706 Bug: 242537431 Test: NotificationManagerServiceTest; ZenModeHelperTest; manually verified that it's possible to create zen schedules from guest mode Change-Id: I0c4c705cfe5fc875151958957daaf8657fbc21a7 Merged-In: I0c4c705cfe5fc875151958957daaf8657fbc21a7 (cherry picked from commit7261cdd30b)
This commit is contained in:
@@ -4486,7 +4486,16 @@ public class NotificationManagerService extends SystemService {
|
||||
}
|
||||
enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule");
|
||||
|
||||
return mZenModeHelper.addAutomaticZenRule(pkg, automaticZenRule,
|
||||
// If the calling app is the system (from any user), take the package name from the
|
||||
// rule's owner rather than from the caller's package.
|
||||
String rulePkg = pkg;
|
||||
if (isCallingAppIdSystem()) {
|
||||
if (automaticZenRule.getOwner() != null) {
|
||||
rulePkg = automaticZenRule.getOwner().getPackageName();
|
||||
}
|
||||
}
|
||||
|
||||
return mZenModeHelper.addAutomaticZenRule(rulePkg, automaticZenRule,
|
||||
"addAutomaticZenRule");
|
||||
}
|
||||
|
||||
@@ -8651,6 +8660,12 @@ public class NotificationManagerService extends SystemService {
|
||||
return uid == Process.SYSTEM_UID;
|
||||
}
|
||||
|
||||
protected boolean isCallingAppIdSystem() {
|
||||
final int uid = Binder.getCallingUid();
|
||||
final int appid = UserHandle.getAppId(uid);
|
||||
return appid == Process.SYSTEM_UID;
|
||||
}
|
||||
|
||||
protected boolean isUidSystemOrPhone(int uid) {
|
||||
final int appid = UserHandle.getAppId(uid);
|
||||
return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID
|
||||
|
||||
@@ -305,7 +305,7 @@ public class ZenModeHelper {
|
||||
|
||||
public String addAutomaticZenRule(String pkg, AutomaticZenRule automaticZenRule,
|
||||
String reason) {
|
||||
if (!isSystemRule(automaticZenRule)) {
|
||||
if (!ZenModeConfig.SYSTEM_AUTHORITY.equals(pkg)) {
|
||||
PackageItemInfo component = getServiceInfo(automaticZenRule.getOwner());
|
||||
if (component == null) {
|
||||
component = getActivityInfo(automaticZenRule.getConfigurationActivity());
|
||||
@@ -554,11 +554,6 @@ public class ZenModeHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSystemRule(AutomaticZenRule rule) {
|
||||
return rule.getOwner() != null
|
||||
&& ZenModeConfig.SYSTEM_AUTHORITY.equals(rule.getOwner().getPackageName());
|
||||
}
|
||||
|
||||
private ServiceInfo getServiceInfo(ComponentName owner) {
|
||||
Intent queryIntent = new Intent();
|
||||
queryIntent.setComponent(owner);
|
||||
|
||||
@@ -312,6 +312,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
||||
private static class TestableNotificationManagerService extends NotificationManagerService {
|
||||
int countSystemChecks = 0;
|
||||
boolean isSystemUid = true;
|
||||
boolean isSystemAppId = true;
|
||||
int countLogSmartSuggestionsVisible = 0;
|
||||
// If true, don't enqueue the PostNotificationRunnables, just trap them
|
||||
boolean trapEnqueuedNotifications = false;
|
||||
@@ -341,6 +342,12 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
||||
return isSystemUid;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isCallingAppIdSystem() {
|
||||
countSystemChecks++;
|
||||
return isSystemUid || isSystemAppId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isCallerSystemOrPhone() {
|
||||
countSystemChecks++;
|
||||
@@ -5957,6 +5964,65 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
|
||||
mBinderService.addAutomaticZenRule(rule, mContext.getPackageName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAutomaticZenRule_systemCallTakesPackageFromOwner() throws Exception {
|
||||
mService.isSystemUid = true;
|
||||
ZenModeHelper mockZenModeHelper = mock(ZenModeHelper.class);
|
||||
when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt()))
|
||||
.thenReturn(true);
|
||||
mService.setZenHelper(mockZenModeHelper);
|
||||
ComponentName owner = new ComponentName("android", "ProviderName");
|
||||
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_PRIORITY, isEnabled);
|
||||
mBinderService.addAutomaticZenRule(rule, "com.android.settings");
|
||||
|
||||
// verify that zen mode helper gets passed in a package name of "android"
|
||||
verify(mockZenModeHelper).addAutomaticZenRule(eq("android"), eq(rule), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAutomaticZenRule_systemAppIdCallTakesPackageFromOwner() throws Exception {
|
||||
// The multi-user case: where the calling uid doesn't match the system uid, but the calling
|
||||
// *appid* is the system.
|
||||
mService.isSystemUid = false;
|
||||
mService.isSystemAppId = true;
|
||||
ZenModeHelper mockZenModeHelper = mock(ZenModeHelper.class);
|
||||
when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt()))
|
||||
.thenReturn(true);
|
||||
mService.setZenHelper(mockZenModeHelper);
|
||||
ComponentName owner = new ComponentName("android", "ProviderName");
|
||||
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_PRIORITY, isEnabled);
|
||||
mBinderService.addAutomaticZenRule(rule, "com.android.settings");
|
||||
|
||||
// verify that zen mode helper gets passed in a package name of "android"
|
||||
verify(mockZenModeHelper).addAutomaticZenRule(eq("android"), eq(rule), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAutomaticZenRule_nonSystemCallTakesPackageFromArg() throws Exception {
|
||||
mService.isSystemUid = false;
|
||||
mService.isSystemAppId = false;
|
||||
ZenModeHelper mockZenModeHelper = mock(ZenModeHelper.class);
|
||||
when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt()))
|
||||
.thenReturn(true);
|
||||
mService.setZenHelper(mockZenModeHelper);
|
||||
ComponentName owner = new ComponentName("android", "ProviderName");
|
||||
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_PRIORITY, isEnabled);
|
||||
mBinderService.addAutomaticZenRule(rule, "another.package");
|
||||
|
||||
// verify that zen mode helper gets passed in the package name from the arg, not the owner
|
||||
verify(mockZenModeHelper).addAutomaticZenRule(
|
||||
eq("another.package"), eq(rule), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAreNotificationsEnabledForPackage_crossUser() throws Exception {
|
||||
try {
|
||||
|
||||
@@ -1635,6 +1635,36 @@ public class ZenModeHelperTest extends UiServiceTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAutomaticZenRule_claimedSystemOwner() {
|
||||
// Make sure anything that claims to have a "system" owner but not actually part of the
|
||||
// system package still gets limited on number of rules
|
||||
for (int i = 0; i < RULE_LIMIT_PER_PACKAGE; i++) {
|
||||
ScheduleInfo si = new ScheduleInfo();
|
||||
si.startHour = i;
|
||||
AutomaticZenRule zenRule = new AutomaticZenRule("name" + i,
|
||||
new ComponentName("android", "ScheduleConditionProvider" + i),
|
||||
null, // configuration activity
|
||||
ZenModeConfig.toScheduleConditionId(si),
|
||||
new ZenPolicy.Builder().build(),
|
||||
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
|
||||
String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test");
|
||||
assertNotNull(id);
|
||||
}
|
||||
try {
|
||||
AutomaticZenRule zenRule = new AutomaticZenRule("name",
|
||||
new ComponentName("android", "ScheduleConditionProviderFinal"),
|
||||
null, // configuration activity
|
||||
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
|
||||
new ZenPolicy.Builder().build(),
|
||||
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
|
||||
String id = mZenModeHelperSpy.addAutomaticZenRule("pkgname", zenRule, "test");
|
||||
fail("allowed too many rules to be created");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// yay
|
||||
}
|
||||
}
|
||||
|
||||
private void setupZenConfig() {
|
||||
mZenModeHelperSpy.mZenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS;
|
||||
mZenModeHelperSpy.mConfig.allowAlarms = false;
|
||||
|
||||
Reference in New Issue
Block a user