diff --git a/services/core/java/com/android/server/notification/ManagedServices.java b/services/core/java/com/android/server/notification/ManagedServices.java index 7d31287663d5e..e6bc79679a8f2 100644 --- a/services/core/java/com/android/server/notification/ManagedServices.java +++ b/services/core/java/com/android/server/notification/ManagedServices.java @@ -1065,7 +1065,7 @@ abstract public class ManagedServices { } } - protected void setComponentState(ComponentName component, boolean enabled) { + protected void setComponentState(ComponentName component, int userId, boolean enabled) { boolean previous = !mSnoozingForCurrentProfiles.contains(component); if (previous == enabled) { return; @@ -1082,20 +1082,15 @@ abstract public class ManagedServices { component.flattenToShortString()); synchronized (mMutex) { - final IntArray userIds = mUserProfiles.getCurrentProfileIds(); - - for (int i = 0; i < userIds.size(); i++) { - final int userId = userIds.get(i); - if (enabled) { - if (isPackageOrComponentAllowed(component.flattenToString(), userId) - || isPackageOrComponentAllowed(component.getPackageName(), userId)) { - registerServiceLocked(component, userId); - } else { - Slog.d(TAG, component + " no longer has permission to be bound"); - } + if (enabled) { + if (isPackageOrComponentAllowed(component.flattenToString(), userId) + || isPackageOrComponentAllowed(component.getPackageName(), userId)) { + registerServiceLocked(component, userId); } else { - unregisterServiceLocked(component, userId); + Slog.d(TAG, component + " no longer has permission to be bound"); } + } else { + unregisterServiceLocked(component, userId); } } } diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index cc965d38ced44..d3ac9c2611659 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -4456,13 +4456,14 @@ public class NotificationManagerService extends SystemService { @Override public void requestBindListener(ComponentName component) { checkCallerIsSystemOrSameApp(component.getPackageName()); + int uid = Binder.getCallingUid(); final long identity = Binder.clearCallingIdentity(); try { ManagedServices manager = mAssistants.isComponentEnabledForCurrentProfiles(component) ? mAssistants : mListeners; - manager.setComponentState(component, true); + manager.setComponentState(component, UserHandle.getUserId(uid), true); } finally { Binder.restoreCallingIdentity(identity); } @@ -4470,12 +4471,14 @@ public class NotificationManagerService extends SystemService { @Override public void requestUnbindListener(INotificationListener token) { + int uid = Binder.getCallingUid(); final long identity = Binder.clearCallingIdentity(); try { // allow bound services to disable themselves synchronized (mNotificationLock) { final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); - info.getOwner().setComponentState(info.component, false); + info.getOwner().setComponentState( + info.component, UserHandle.getUserId(uid), false); } } finally { Binder.restoreCallingIdentity(identity); @@ -4967,11 +4970,12 @@ public class NotificationManagerService extends SystemService { @Override public void requestUnbindProvider(IConditionProvider provider) { + int uid = Binder.getCallingUid(); final long identity = Binder.clearCallingIdentity(); try { // allow bound services to disable themselves final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); - info.getOwner().setComponentState(info.component, false); + info.getOwner().setComponentState(info.component, UserHandle.getUserId(uid), false); } finally { Binder.restoreCallingIdentity(identity); } @@ -4980,9 +4984,10 @@ public class NotificationManagerService extends SystemService { @Override public void requestBindProvider(ComponentName component) { checkCallerIsSystemOrSameApp(component.getPackageName()); + int uid = Binder.getCallingUid(); final long identity = Binder.clearCallingIdentity(); try { - mConditionProviders.setComponentState(component, true); + mConditionProviders.setComponentState(component, UserHandle.getUserId(uid), true); } finally { Binder.restoreCallingIdentity(identity); } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java index c337ccd67db8a..cdb7230a66cbd 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ManagedServicesTest.java @@ -1361,6 +1361,66 @@ public class ManagedServicesTest extends UiServiceTestCase { assertTrue(service.isBound(cn, 0)); } + @Test + public void testSetComponentState() throws Exception { + Context context = mock(Context.class); + PackageManager pm = mock(PackageManager.class); + ApplicationInfo ai = new ApplicationInfo(); + ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; + + when(context.getPackageName()).thenReturn(mContext.getPackageName()); + when(context.getUserId()).thenReturn(mContext.getUserId()); + when(context.getPackageManager()).thenReturn(pm); + when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai); + + ManagedServices service = new TestManagedServices(context, mLock, mUserProfiles, mIpm, + APPROVAL_BY_COMPONENT); + ComponentName cn = ComponentName.unflattenFromString("a/a"); + + service.registerSystemService(cn, 0); + when(context.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); + ServiceConnection sc = (ServiceConnection) args[1]; + sc.onServiceConnected(cn, mock(IBinder.class)); + return true; + }); + + service.registerSystemService(cn, mZero.id); + + service.setComponentState(cn, mZero.id, false); + verify(context).unbindService(any()); + } + + @Test + public void testSetComponentState_workProfile() throws Exception { + Context context = mock(Context.class); + PackageManager pm = mock(PackageManager.class); + ApplicationInfo ai = new ApplicationInfo(); + ai.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; + + when(context.getPackageName()).thenReturn(mContext.getPackageName()); + when(context.getUserId()).thenReturn(mContext.getUserId()); + when(context.getPackageManager()).thenReturn(pm); + when(pm.getApplicationInfo(anyString(), anyInt())).thenReturn(ai); + + ManagedServices service = new TestManagedServices(context, mLock, mUserProfiles, mIpm, + APPROVAL_BY_COMPONENT); + ComponentName cn = ComponentName.unflattenFromString("a/a"); + + service.registerSystemService(cn, 0); + when(context.bindServiceAsUser(any(), any(), anyInt(), any())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); + ServiceConnection sc = (ServiceConnection) args[1]; + sc.onServiceConnected(cn, mock(IBinder.class)); + return true; + }); + + service.registerSystemService(cn, mZero.id); + + service.setComponentState(cn, mTen.id, false); + verify(context, never()).unbindService(any()); + } + @Test public void testOnPackagesChanged_nullValuesPassed_noNullPointers() { for (int approvalLevel : new int[] {APPROVAL_BY_COMPONENT, APPROVAL_BY_PACKAGE}) {