summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Julia Reynolds <juliacr@google.com> 2019-05-01 21:23:43 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2019-05-01 21:23:43 +0000
commit5e947edd12dbe9d6bac7f188d2e60bcbb51a7392 (patch)
tree39fe569ce28cbb2bcc8afa0d9bab27413cd0ab51
parentdc22284752e5d15479ebf1bfb8fb0d1389815e31 (diff)
parent4afe264811d47023e26a3c9bce2d2d266ad83069 (diff)
Merge "Mirror primary noti assistant to work profile" into qt-dev
-rw-r--r--services/core/java/com/android/server/notification/NotificationManagerService.java59
-rw-r--r--services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java221
2 files changed, 160 insertions, 120 deletions
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 55191dbbbf40..57375325825f 100644
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -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);
- }
- return;
- }
- if (!granted || mAllowedManagedServicePackages.test(assistant.getPackageName(), userId,
- mAssistants.getRequiredPermission())) {
- mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(),
- userId, false, granted);
- mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(),
- userId, true, granted);
+ 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(
- NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
- .setPackage(assistant.getPackageName())
- .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
- UserHandle.of(userId), null);
+ getContext().sendBroadcastAsUser(
+ new Intent(ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED)
+ .setPackage(assistant.getPackageName())
+ .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
+ UserHandle.of(userId), null);
- handleSavePolicyFile();
+ handleSavePolicyFile();
+ }
+ }
}
}
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 cbca087872d9..3ce1317829f7 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
@@ -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,19 +2170,45 @@ 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);
+ verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
+ c.flattenToString(), 0, false, true);
+ verify(mListeners, never()).setPackageOrComponentEnabled(
+ 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());
}
@@ -2212,14 +2219,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
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,25 +2237,59 @@ 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(
+ 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);
- } catch (SecurityException e) {
- if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
- throw e;
- }
- }
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());
}
@@ -2257,13 +2297,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@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());