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

Commit d1f905fd authored by gavin.yoo's avatar gavin.yoo Committed by BK Choi
Browse files

Modify the logic that updates the information of enabled services in...

Modify the logic that updates the information of enabled services in ManagedServices to handle the visible background user in MUMD

- mEnabledServicesForCurrentProfiles, mEnabledServicesPackageNames
is designed only for the current user and work profile.
- We added a condition to prevent visible background users from updating
mEnabledServicesForCurrentProfiles, mEnabledServicesPackageNames

Bug: 355106764
Flag: EXEMPT bugfix
Test: atest ManagedServicesTest
      atest CtsNotificationTestCases
(cherry picked from https://partner-android-review.googlesource.com/q/commit:c89012e6799e0a71b8f841567ea7be46f3064008)

Change-Id: If93d9d014447647cdd1fc48ec6fae91146ea9f81
parent 221ae24e
Loading
Loading
Loading
Loading
+23 −5
Original line number Diff line number Diff line
@@ -75,7 +75,9 @@ import com.android.internal.util.XmlUtils;
import com.android.internal.util.function.TriPredicate;
import com.android.modules.utils.TypedXmlPullParser;
import com.android.modules.utils.TypedXmlSerializer;
import com.android.server.LocalServices;
import com.android.server.notification.NotificationManagerService.DumpFilter;
import com.android.server.pm.UserManagerInternal;
import com.android.server.utils.TimingsTraceAndSlog;

import org.xmlpull.v1.XmlPullParser;
@@ -134,6 +136,7 @@ abstract public class ManagedServices {
    private final UserProfiles mUserProfiles;
    protected final IPackageManager mPm;
    protected final UserManager mUm;
    private final UserManagerInternal mUserManagerInternal;
    private final Config mConfig;
    private final Handler mHandler = new Handler(Looper.getMainLooper());

@@ -195,6 +198,7 @@ abstract public class ManagedServices {
        mConfig = getConfig();
        mApprovalLevel = APPROVAL_BY_COMPONENT;
        mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
    }

    abstract protected Config getConfig();
@@ -1372,9 +1376,14 @@ abstract public class ManagedServices {
    @GuardedBy("mMutex")
    protected void populateComponentsToBind(SparseArray<Set<ComponentName>> componentsToBind,
            final IntArray activeUsers,
            SparseArray<ArraySet<ComponentName>> approvedComponentsByUser) {
            SparseArray<ArraySet<ComponentName>> approvedComponentsByUser,
            boolean isVisibleBackgroundUser) {
        // When it is a visible background user in Automotive MUMD environment,
        // don't clear mEnabledServicesForCurrentProfile and mEnabledServicesPackageNames.
        if (!isVisibleBackgroundUser) {
            mEnabledServicesForCurrentProfiles.clear();
            mEnabledServicesPackageNames.clear();
        }
        final int nUserIds = activeUsers.size();

        for (int i = 0; i < nUserIds; ++i) {
@@ -1395,7 +1404,12 @@ abstract public class ManagedServices {
            }

            componentsToBind.put(userId, add);

            // When it is a visible background user in Automotive MUMD environment,
            // skip adding items to mEnabledServicesForCurrentProfile
            // and mEnabledServicesPackageNames.
            if (isVisibleBackgroundUser) {
                continue;
            }
            mEnabledServicesForCurrentProfiles.addAll(userComponents);

            for (int j = 0; j < userComponents.size(); j++) {
@@ -1443,7 +1457,10 @@ abstract public class ManagedServices {
        IntArray userIds = mUserProfiles.getCurrentProfileIds();
        boolean rebindAllCurrentUsers = mUserProfiles.isProfileUser(userToRebind, mContext)
                && allowRebindForParentUser();
        boolean isVisibleBackgroundUser = false;
        if (userToRebind != USER_ALL && !rebindAllCurrentUsers) {
            isVisibleBackgroundUser =
                    mUserManagerInternal.isVisibleBackgroundFullUser(userToRebind);
            userIds = new IntArray(1);
            userIds.add(userToRebind);
        }
@@ -1458,7 +1475,8 @@ abstract public class ManagedServices {

            // Filter approvedComponentsByUser to collect all of the components that are allowed
            // for the currently active user(s).
            populateComponentsToBind(componentsToBind, userIds, approvedComponentsByUser);
            populateComponentsToBind(componentsToBind, userIds, approvedComponentsByUser,
                    isVisibleBackgroundUser);

            // For every current non-system connection, disconnect services that are no longer
            // approved, or ALL services if we are force rebinding
+4 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.testing.TestableContext;

import androidx.test.InstrumentationRegistry;

import com.android.server.pm.UserManagerInternal;
import com.android.server.uri.UriGrantsManagerInternal;

import org.junit.After;
@@ -41,6 +42,7 @@ import org.mockito.MockitoAnnotations;

public class UiServiceTestCase {
    @Mock protected PackageManagerInternal mPmi;
    @Mock protected UserManagerInternal mUmi;
    @Mock protected UriGrantsManagerInternal mUgmInternal;

    protected static final String PKG_N_MR1 = "com.example.n_mr1";
@@ -92,6 +94,8 @@ public class UiServiceTestCase {
                    }
                });

        LocalServices.removeServiceForTest(UserManagerInternal.class);
        LocalServices.addService(UserManagerInternal.class, mUmi);
        LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
        LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
        when(mUgmInternal.checkGrantUriPermission(
+30 −1
Original line number Diff line number Diff line
@@ -1542,6 +1542,7 @@ public class ManagedServicesTest extends UiServiceTestCase {
        assertTrue(componentsToUnbind.get(0).contains(ComponentName.unflattenFromString("c/c")));
    }

    @SuppressWarnings("GuardedBy")
    @Test
    public void populateComponentsToBind() {
        ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm,
@@ -1565,7 +1566,8 @@ public class ManagedServicesTest extends UiServiceTestCase {

        SparseArray<Set<ComponentName>> componentsToBind = new SparseArray<>();

        service.populateComponentsToBind(componentsToBind, users, approvedComponentsByUser);
        service.populateComponentsToBind(componentsToBind, users, approvedComponentsByUser,
                /* isVisibleBackgroundUser= */ false);

        assertEquals(2, componentsToBind.size());
        assertEquals(1, componentsToBind.get(0).size());
@@ -1575,6 +1577,33 @@ public class ManagedServicesTest extends UiServiceTestCase {
        assertTrue(componentsToBind.get(10).contains(ComponentName.unflattenFromString("c/c")));
    }

    @SuppressWarnings("GuardedBy")
    @Test
    public void populateComponentsToBind_isVisibleBackgroundUser_addComponentsToBindButNotAddToEnabledComponent() {
        ManagedServices service = new TestManagedServices(getContext(), mLock, mUserProfiles, mIpm,
                APPROVAL_BY_COMPONENT);

        SparseArray<ArraySet<ComponentName>> approvedComponentsByUser = new SparseArray<>();
        ArraySet<ComponentName> allowed = new ArraySet<>();
        allowed.add(ComponentName.unflattenFromString("pkg1/cmp1"));
        approvedComponentsByUser.put(11, allowed);
        IntArray users = new IntArray();
        users.add(11);

        SparseArray<Set<ComponentName>> componentsToBind = new SparseArray<>();

        service.populateComponentsToBind(componentsToBind, users, approvedComponentsByUser,
                /* isVisibleBackgroundUser= */ true);

        assertEquals(1, componentsToBind.size());
        assertEquals(1, componentsToBind.get(11).size());
        assertTrue(componentsToBind.get(11).contains(ComponentName.unflattenFromString(
                "pkg1/cmp1")));
        assertThat(service.isComponentEnabledForCurrentProfiles(
                new ComponentName("pkg1", "cmp1"))).isFalse();
        assertThat(service.isComponentEnabledForPackage("pkg1")).isFalse();
    }

    @Test
    public void testOnNullBinding() throws Exception {
        Context context = mock(Context.class);