Add user param when handling app initiated bind/unbind

Test: ManagedServicesTest;  manual unbind/bind while checking
bind state in dumpsys
Fixes: 186019010

Change-Id: Idd315b12b6eda1a6bdeb8b3cc73b6a8d0d0c7646
This commit is contained in:
Julia Reynolds
2021-11-16 14:27:13 -05:00
parent f6a47736b3
commit d422517725
3 changed files with 77 additions and 17 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}

View File

@@ -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}) {