Merge "Mirror primary noti assistant to work profile" into qt-dev

This commit is contained in:
Julia Reynolds
2019-05-01 21:23:43 +00:00
committed by Android (Google) Code Review
2 changed files with 162 additions and 122 deletions

View File

@@ -26,6 +26,7 @@ import static android.app.Notification.FLAG_ONGOING_EVENT;
import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED;
import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED;
import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED;
import static android.app.NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.app.NotificationManager.IMPORTANCE_NONE;
@@ -3754,7 +3755,7 @@ public class NotificationManagerService extends SystemService {
pkg, userId, true, granted);
getContext().sendBroadcastAsUser(new Intent(
NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
.setPackage(pkg)
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT),
UserHandle.of(userId), null);
@@ -3914,7 +3915,7 @@ public class NotificationManagerService extends SystemService {
userId, true, granted);
getContext().sendBroadcastAsUser(new Intent(
NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
.setPackage(listener.getPackageName())
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
UserHandle.of(userId), null);
@@ -3930,7 +3931,9 @@ public class NotificationManagerService extends SystemService {
public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant,
int userId, boolean granted) {
checkCallerIsSystemOrSystemUiOrShell();
mAssistants.setUserSet(userId, true);
for (UserInfo ui : mUm.getEnabledProfiles(userId)) {
mAssistants.setUserSet(ui.id, true);
}
final long identity = Binder.clearCallingIdentity();
try {
setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted);
@@ -4144,30 +4147,36 @@ public class NotificationManagerService extends SystemService {
@VisibleForTesting
protected void setNotificationAssistantAccessGrantedForUserInternal(
ComponentName assistant, int userId, boolean granted) {
if (assistant == null) {
ComponentName allowedAssistant = CollectionUtils.firstOrNull(
mAssistants.getAllowedComponents(userId));
if (allowedAssistant != null) {
setNotificationAssistantAccessGrantedForUserInternal(
allowedAssistant, userId, false);
ComponentName assistant, int baseUserId, boolean granted) {
List<UserInfo> users = mUm.getEnabledProfiles(baseUserId);
if (users != null) {
for (UserInfo user : users) {
int userId = user.id;
if (assistant == null) {
ComponentName allowedAssistant = CollectionUtils.firstOrNull(
mAssistants.getAllowedComponents(userId));
if (allowedAssistant != null) {
setNotificationAssistantAccessGrantedForUserInternal(
allowedAssistant, userId, false);
}
continue;
}
if (!granted || mAllowedManagedServicePackages.test(assistant.getPackageName(),
userId, mAssistants.getRequiredPermission())) {
mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(),
userId, false, granted);
mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(),
userId, true, granted);
getContext().sendBroadcastAsUser(
new Intent(ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
.setPackage(assistant.getPackageName())
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
UserHandle.of(userId), null);
handleSavePolicyFile();
}
}
return;
}
if (!granted || mAllowedManagedServicePackages.test(assistant.getPackageName(), userId,
mAssistants.getRequiredPermission())) {
mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(),
userId, false, granted);
mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(),
userId, true, granted);
getContext().sendBroadcastAsUser(new Intent(
NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
.setPackage(assistant.getPackageName())
.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
UserHandle.of(userId), null);
handleSavePolicyFile();
}
}

View File

@@ -62,6 +62,7 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
@@ -96,6 +97,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Icon;
@@ -327,6 +329,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
LocalServices.removeServiceForTest(WindowManagerInternal.class);
LocalServices.addService(WindowManagerInternal.class, mWindowManagerInternal);
doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any());
mService = new TestableNotificationManagerService(mContext);
// Use this testable looper.
@@ -375,20 +379,16 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
when(mAssistants.isAdjustmentAllowed(anyString())).thenReturn(true);
try {
mService.init(mTestableLooper.getLooper(),
mPackageManager, mPackageManagerClient, mockLightsManager,
mListeners, mAssistants, mConditionProviders,
mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager,
mGroupHelper, mAm, mAppUsageStats,
mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal,
mAppOpsManager, mUm);
mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mService.init(mTestableLooper.getLooper(),
mPackageManager, mPackageManagerClient, mockLightsManager,
mListeners, mAssistants, mConditionProviders,
mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager,
mGroupHelper, mAm, mAppUsageStats,
mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal,
mAppOpsManager, mUm);
mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
mService.setAudioManager(mAudioManager);
// Tests call directly into the Binder.
@@ -2080,14 +2080,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
public void testSetListenerAccessForUser() throws Exception {
UserHandle user = UserHandle.of(10);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationListenerAccessGrantedForUser(
c, user.getIdentifier(), true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationListenerAccessGrantedForUser(c, user.getIdentifier(), true);
verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any());
verify(mListeners, times(1)).setPackageOrComponentEnabled(
@@ -2101,15 +2095,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@Test
public void testSetAssistantAccessForUser() throws Exception {
UserHandle user = UserHandle.of(10);
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 10;
uis.add(ui);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationAssistantAccessGrantedForUser(
c, user.getIdentifier(), true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
when(mUm.getEnabledProfiles(10)).thenReturn(uis);
mBinderService.setNotificationAssistantAccessGrantedForUser(c, user.getIdentifier(), true);
verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any());
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
@@ -2150,14 +2143,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
public void testSetDndAccessForUser() throws Exception {
UserHandle user = UserHandle.of(10);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationPolicyAccessGrantedForUser(
c.getPackageName(), user.getIdentifier(), true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationPolicyAccessGrantedForUser(
c.getPackageName(), user.getIdentifier(), true);
verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any());
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
@@ -2171,13 +2158,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@Test
public void testSetListenerAccess() throws Exception {
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationListenerAccessGranted(c, true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationListenerAccessGranted(c, true);
verify(mListeners, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 0, true, true);
@@ -2189,14 +2170,14 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@Test
public void testSetAssistantAccess() throws Exception {
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 0;
uis.add(ui);
when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationAssistantAccessGranted(c, true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationAssistantAccessGranted(c, true);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 0, true, true);
@@ -2206,20 +2187,45 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
any(), anyInt(), anyBoolean(), anyBoolean());
}
@Test
public void testSetAssistantAccess_multiProfile() throws Exception {
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 0;
uis.add(ui);
UserInfo ui10 = new UserInfo();
ui10.id = 10;
uis.add(ui10);
when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis);
ComponentName c = ComponentName.unflattenFromString("package/Component");
mBinderService.setNotificationAssistantAccessGranted(c, true);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 0, true, true);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 10, true, true);
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 0, false, true);
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 10, false, true);
verify(mListeners, never()).setPackageOrComponentEnabled(
any(), anyInt(), anyBoolean(), anyBoolean());
}
@Test
public void testSetAssistantAccess_nullWithAllowedAssistant() throws Exception {
ArrayList<ComponentName> componentList = new ArrayList<>();
ComponentName c = ComponentName.unflattenFromString("package/Component");
componentList.add(c);
when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList);
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 0;
uis.add(ui);
when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis);
try {
mBinderService.setNotificationAssistantAccessGranted(null, true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationAssistantAccessGranted(null, true);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 0, true, false);
@@ -2231,39 +2237,68 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@Test
public void testSetAssistantAccessForUser_nullWithAllowedAssistant() throws Exception {
UserHandle user = UserHandle.of(10);
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 10;
uis.add(ui);
UserHandle user = ui.getUserHandle();
ArrayList<ComponentName> componentList = new ArrayList<>();
ComponentName c = ComponentName.unflattenFromString("package/Component");
componentList.add(c);
when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList);
when(mUm.getEnabledProfiles(10)).thenReturn(uis);
try {
mBinderService.setNotificationAssistantAccessGrantedForUser(
null, user.getIdentifier(), true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationAssistantAccessGrantedForUser(
null, user.getIdentifier(), true);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), user.getIdentifier(), true, false);
verify(mAssistants).setUserSet(10, true);
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), user.getIdentifier(), false, false);
verify(mListeners, never()).setPackageOrComponentEnabled(
any(), anyInt(), anyBoolean(), anyBoolean());
}
@Test
public void testSetAssistantAccessForUser_workProfile_nullWithAllowedAssistant()
throws Exception {
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 0;
uis.add(ui);
UserInfo ui10 = new UserInfo();
ui10.id = 10;
uis.add(ui10);
UserHandle user = ui.getUserHandle();
ArrayList<ComponentName> componentList = new ArrayList<>();
ComponentName c = ComponentName.unflattenFromString("package/Component");
componentList.add(c);
when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList);
when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis);
mBinderService.setNotificationAssistantAccessGrantedForUser(
null, user.getIdentifier(), true);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), user.getIdentifier(), true, false);
verify(mAssistants, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), ui10.id, true, false);
verify(mAssistants).setUserSet(0, true);
verify(mAssistants).setUserSet(10, true);
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), user.getIdentifier(), false, false);
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), ui10.id, false, false);
verify(mListeners, never()).setPackageOrComponentEnabled(
any(), anyInt(), anyBoolean(), anyBoolean());
}
@Test
public void testSetDndAccess() throws Exception {
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
c.getPackageName(), 0, true, true);
@@ -2291,6 +2326,12 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception {
when(mActivityManager.isLowRamDevice()).thenReturn(true);
ComponentName c = ComponentName.unflattenFromString("package/Component");
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 0;
uis.add(ui);
when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis);
mBinderService.setNotificationAssistantAccessGranted(c, true);
verify(mListeners, never()).setPackageOrComponentEnabled(
@@ -2320,13 +2361,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true);
when(mActivityManager.isLowRamDevice()).thenReturn(true);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationListenerAccessGranted(c, true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationListenerAccessGranted(c, true);
verify(mListeners, times(1)).setPackageOrComponentEnabled(
c.flattenToString(), 0, true, true);
@@ -2341,13 +2377,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true);
when(mActivityManager.isLowRamDevice()).thenReturn(true);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationAssistantAccessGranted(c, true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
List<UserInfo> uis = new ArrayList<>();
UserInfo ui = new UserInfo();
ui.id = 0;
uis.add(ui);
when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis);
mBinderService.setNotificationAssistantAccessGranted(c, true);
verify(mListeners, never()).setPackageOrComponentEnabled(
anyString(), anyInt(), anyBoolean(), anyBoolean());
@@ -2362,13 +2398,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true);
when(mActivityManager.isLowRamDevice()).thenReturn(true);
ComponentName c = ComponentName.unflattenFromString("package/Component");
try {
mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
} catch (SecurityException e) {
if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
throw e;
}
}
mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
verify(mListeners, never()).setPackageOrComponentEnabled(
anyString(), anyInt(), anyBoolean(), anyBoolean());