Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e856096f authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Fix cross profile NLS notif event delivery

isManagedProfile was used to both determine if a primary
user can see profile notification and to determine
whether a service could be bound in a profile. This
cl separates the usages for clarity

Test: ManagedServicesTest
Test: add user, verify bound services in bugreport
Test: switch user, verify bound services in bugreport
Fixes: 245056829
Change-Id: I9b7b5d1e9270c516916585a2e9ba65acc2551454
parent 33c34d0a
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -1786,8 +1786,8 @@ abstract public class ManagedServices {
         * from receiving events from the profile.
         */
        public boolean isPermittedForProfile(int userId) {
            if (!mUserProfiles.canProfileUseBoundServices(userId)) {
                return false;
            if (!mUserProfiles.isProfileUser(userId)) {
                return true;
            }
            DevicePolicyManager dpm =
                    (DevicePolicyManager) mContext.getSystemService(DEVICE_POLICY_SERVICE);
@@ -1862,17 +1862,17 @@ abstract public class ManagedServices {
            }
        }

        public boolean canProfileUseBoundServices(int userId) {
        public boolean isProfileUser(int userId) {
            synchronized (mCurrentProfiles) {
                UserInfo user = mCurrentProfiles.get(userId);
                if (user == null) {
                    return false;
                }
                if (user.isManagedProfile() || user.isCloneProfile()) {
                    return false;
                }
                    return true;
                }
                return false;
            }
        }
    }

+3 −3
Original line number Diff line number Diff line
@@ -1828,7 +1828,7 @@ public class NotificationManagerService extends SystemService {
            } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
                mUserProfiles.updateCache(context);
                if (mUserProfiles.canProfileUseBoundServices(userId)) {
                if (!mUserProfiles.isProfileUser(userId)) {
                    // reload per-user settings
                    mSettingsObserver.update(null);
                    // Refresh managed services
@@ -1842,7 +1842,7 @@ public class NotificationManagerService extends SystemService {
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
                if (userId != USER_NULL) {
                    mUserProfiles.updateCache(context);
                    if (mUserProfiles.canProfileUseBoundServices(userId)) {
                    if (!mUserProfiles.isProfileUser(userId)) {
                        allowDefaultApprovedServices(userId);
                    }
                }
@@ -1860,7 +1860,7 @@ public class NotificationManagerService extends SystemService {
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
                mUserProfiles.updateCache(context);
                mAssistants.onUserUnlocked(userId);
                if (mUserProfiles.canProfileUseBoundServices(userId)) {
                if (!mUserProfiles.isProfileUser(userId)) {
                    mConditionProviders.onUserUnlocked(userId);
                    mListeners.onUserUnlocked(userId);
                    mZenModeHelper.onUserUnlocked(userId);
+24 −8
Original line number Diff line number Diff line
@@ -1704,8 +1704,8 @@ public class ManagedServicesTest extends UiServiceTestCase {
    }

    @Test
    public void testInfoIsPermittedForProfile_notAllowed() {
        when(mUserProfiles.canProfileUseBoundServices(anyInt())).thenReturn(false);
    public void testInfoIsPermittedForProfile_notProfile() {
        when(mUserProfiles.isProfileUser(anyInt())).thenReturn(false);

        IInterface service = mock(IInterface.class);
        when(service.asBinder()).thenReturn(mock(IBinder.class));
@@ -1714,12 +1714,12 @@ public class ManagedServicesTest extends UiServiceTestCase {
        services.registerSystemService(service, null, 10, 1000);
        ManagedServices.ManagedServiceInfo info = services.checkServiceTokenLocked(service);

        assertFalse(info.isPermittedForProfile(0));
        assertTrue(info.isPermittedForProfile(0));
    }

    @Test
    public void testInfoIsPermittedForProfile_allows() {
        when(mUserProfiles.canProfileUseBoundServices(anyInt())).thenReturn(true);
    public void testInfoIsPermittedForProfile_profileAndDpmAllows() {
        when(mUserProfiles.isProfileUser(anyInt())).thenReturn(true);
        when(mDpm.isNotificationListenerServicePermitted(anyString(), anyInt())).thenReturn(true);

        IInterface service = mock(IInterface.class);
@@ -1733,6 +1733,22 @@ public class ManagedServicesTest extends UiServiceTestCase {
        assertTrue(info.isPermittedForProfile(0));
    }

    @Test
    public void testInfoIsPermittedForProfile_profileAndDpmDenies() {
        when(mUserProfiles.isProfileUser(anyInt())).thenReturn(true);
        when(mDpm.isNotificationListenerServicePermitted(anyString(), anyInt())).thenReturn(false);

        IInterface service = mock(IInterface.class);
        when(service.asBinder()).thenReturn(mock(IBinder.class));
        ManagedServices services = new TestManagedServices(getContext(), mLock, mUserProfiles,
                mIpm, APPROVAL_BY_PACKAGE);
        services.registerSystemService(service, null, 10, 1000);
        ManagedServices.ManagedServiceInfo info = services.checkServiceTokenLocked(service);
        info.component = new ComponentName("a","b");

        assertFalse(info.isPermittedForProfile(0));
    }

    @Test
    public void testUserProfiles_canProfileUseBoundServices_managedProfile() {
        List<UserInfo> users = new ArrayList<>();
@@ -1750,9 +1766,9 @@ public class ManagedServicesTest extends UiServiceTestCase {
        ManagedServices.UserProfiles profiles = new ManagedServices.UserProfiles();
        profiles.updateCache(mContext);

        assertTrue(profiles.canProfileUseBoundServices(ActivityManager.getCurrentUser()));
        assertFalse(profiles.canProfileUseBoundServices(12));
        assertFalse(profiles.canProfileUseBoundServices(13));
        assertFalse(profiles.isProfileUser(ActivityManager.getCurrentUser()));
        assertTrue(profiles.isProfileUser(12));
        assertTrue(profiles.isProfileUser(13));
    }

    private void resetComponentsAndPackages() {