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

Commit 90cd3b25 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Use ApplicationInfo to check if changeId is disabled.

This avoids the need to make a PackageManager call
to query ApplicationInfo.

Bug: 377101020
Bug: 377193174
Test: atest services/tests/mockingservicestests/src/com/android/server/am/BroadcastFilterTest.java
Flag: com.android.server.am.restrict_priority_values
Change-Id: Ifbe50d3fab07926afa25dc4616d75e79bfc4441a
parent ee061534
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -554,7 +554,7 @@ class BroadcastController {
            }
            BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, callerFeatureId,
                    receiverId, permission, callingUid, userId, instantApp, visibleToInstantApps,
                    exported, mService.mPlatformCompat);
                    exported, callerApp.info, mService.mPlatformCompat);
            if (rl.containsFilter(filter)) {
                Slog.w(TAG, "Receiver with filter " + filter
                        + " already registered for pid " + rl.pid
+14 −6
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.annotation.Nullable;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.os.Binder;
import android.os.UserHandle;
import android.util.PrintWriterPrinter;
import android.util.Printer;
@@ -58,7 +60,7 @@ public final class BroadcastFilter extends IntentFilter {
    BroadcastFilter(IntentFilter _filter, ReceiverList _receiverList,
            String _packageName, String _featureId, String _receiverId, String _requiredPermission,
            int _owningUid, int _userId, boolean _instantApp, boolean _visibleToInstantApp,
            boolean _exported, PlatformCompat platformCompat) {
            boolean _exported, ApplicationInfo applicationInfo, PlatformCompat platformCompat) {
        super(_filter);
        receiverList = _receiverList;
        packageName = _packageName;
@@ -71,7 +73,8 @@ public final class BroadcastFilter extends IntentFilter {
        visibleToInstantApp = _visibleToInstantApp;
        exported = _exported;
        initialPriority = getPriority();
        setPriority(calculateAdjustedPriority(owningUid, initialPriority, platformCompat));
        setPriority(calculateAdjustedPriority(owningUid, initialPriority,
                applicationInfo, platformCompat));
    }

    public @Nullable String getReceiverClassName() {
@@ -125,14 +128,19 @@ public final class BroadcastFilter extends IntentFilter {

    @VisibleForTesting
    static int calculateAdjustedPriority(int owningUid, int priority,
            PlatformCompat platformCompat) {
            ApplicationInfo applicationInfo, PlatformCompat platformCompat) {
        if (!Flags.restrictPriorityValues()) {
            return priority;
        }
        if (!platformCompat.isChangeEnabledByUidInternalNoLogging(
                RESTRICT_PRIORITY_VALUES, owningUid)) {
        final long token = Binder.clearCallingIdentity();
        try {
            if (!platformCompat.isChangeEnabledInternalNoLogging(
                    RESTRICT_PRIORITY_VALUES, applicationInfo)) {
                return priority;
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        if (!UserHandle.isCore(owningUid)) {
            if (priority >= SYSTEM_HIGH_PRIORITY) {
                return SYSTEM_HIGH_PRIORITY - 1;
+3 −3
Original line number Diff line number Diff line
@@ -185,8 +185,8 @@ public abstract class BaseBroadcastQueueTest {

        doReturn(mAppStartInfoTracker).when(mProcessList).getAppStartInfoTracker();

        doReturn(true).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), anyInt());
        doReturn(true).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), any(ApplicationInfo.class));
        doReturn(true).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastRecord.LIMIT_PRIORITY_SCOPE), anyInt());
    }
@@ -298,7 +298,7 @@ public abstract class BaseBroadcastQueueTest {
        filter.setPriority(priority);
        final BroadcastFilter res = new BroadcastFilter(filter, receiverList,
                receiverList.app.info.packageName, null, null, null, receiverList.uid,
                receiverList.userId, false, false, true,
                receiverList.userId, false, false, true, receiverList.app.info,
                mock(PlatformCompat.class));
        receiverList.add(res);
        return res;
+10 −9
Original line number Diff line number Diff line
@@ -20,10 +20,13 @@ import static android.content.IntentFilter.SYSTEM_LOW_PRIORITY;

import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import android.content.pm.ApplicationInfo;
import android.os.Process;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
@@ -53,14 +56,14 @@ public class BroadcastFilterTest {
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        doReturn(true).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), any(ApplicationInfo.class));
    }

    @Test
    @EnableFlags(Flags.FLAG_RESTRICT_PRIORITY_VALUES)
    public void testCalculateAdjustedPriority() {
        doReturn(true).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), anyInt());

        {
            // Pairs of {initial-priority, expected-adjusted-priority}
            final Pair<Integer, Integer>[] priorities = new Pair[] {
@@ -95,8 +98,8 @@ public class BroadcastFilterTest {
    @Test
    @EnableFlags(Flags.FLAG_RESTRICT_PRIORITY_VALUES)
    public void testCalculateAdjustedPriority_withChangeIdDisabled() {
        doReturn(false).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), anyInt());
        doReturn(false).when(mPlatformCompat).isChangeEnabledInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), any(ApplicationInfo.class));

        {
            // Pairs of {initial-priority, expected-adjusted-priority}
@@ -132,9 +135,6 @@ public class BroadcastFilterTest {
    @Test
    @DisableFlags(Flags.FLAG_RESTRICT_PRIORITY_VALUES)
    public void testCalculateAdjustedPriority_withFlagDisabled() {
        doReturn(true).when(mPlatformCompat).isChangeEnabledByUidInternalNoLogging(
                eq(BroadcastFilter.RESTRICT_PRIORITY_VALUES), anyInt());

        {
            // Pairs of {initial-priority, expected-adjusted-priority}
            final Pair<Integer, Integer>[] priorities = new Pair[] {
@@ -215,6 +215,7 @@ public class BroadcastFilterTest {
        final String errorMsg = String.format("owner=%d; actualPriority=%d; expectedPriority=%d",
                owningUid, priority, expectedAdjustedPriority);
        assertWithMessage(errorMsg).that(BroadcastFilter.calculateAdjustedPriority(
                owningUid, priority, mPlatformCompat)).isEqualTo(expectedAdjustedPriority);
                owningUid, priority, mock(ApplicationInfo.class), mPlatformCompat))
                        .isEqualTo(expectedAdjustedPriority);
    }
}